Skip to content

Commit 61b7c09

Browse files
robert-hhdpgeorge
authored andcommitted
mimxrt/machine_bitstream: Add bitstream function to machine module.
Following the code example for ESP32 of Jim Mussard. As a side effect: - mp_hal_ticks_cpu() was implemented, - mp_hal_get_cpu_freq() and mp_hal_ticks_cpu_init() were added and used. - mp_hal_pin_high() and mp_hal_pin_low() were changed for symmetry
1 parent 87f97e4 commit 61b7c09

File tree

5 files changed

+92
-4
lines changed

5 files changed

+92
-4
lines changed

ports/mimxrt/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ SRC_C += \
152152
fatfs_port.c \
153153
led.c \
154154
machine_adc.c \
155+
machine_bitstream.c \
155156
machine_i2c.c \
156157
machine_led.c \
157158
machine_pin.c \

ports/mimxrt/machine_bitstream.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Jim Mussared
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
// This is a translation of the cycle counter implementation in ports/stm32/machine_bitstream.c.
28+
29+
#include "py/mpconfig.h"
30+
#include "py/mphal.h"
31+
32+
#if MICROPY_PY_MACHINE_BITSTREAM
33+
34+
#define NS_TICKS_OVERHEAD (6)
35+
36+
void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) __attribute__((section(".ram_functions")));
37+
void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) {
38+
uint32_t fcpu_mhz = mp_hal_get_cpu_freq() / 1000000;
39+
// Convert ns to us ticks [high_time_0, period_0, high_time_1, period_1].
40+
for (size_t i = 0; i < 4; ++i) {
41+
timing_ns[i] = fcpu_mhz * timing_ns[i] / 1000;
42+
if (timing_ns[i] > NS_TICKS_OVERHEAD) {
43+
timing_ns[i] -= NS_TICKS_OVERHEAD;
44+
}
45+
if (i % 2 == 1) {
46+
// Convert low_time to period (i.e. add high_time).
47+
timing_ns[i] += timing_ns[i - 1];
48+
}
49+
}
50+
// Enable the CPU cycle counter, which is not always enabled.
51+
mp_hal_ticks_cpu_init();
52+
53+
uint32_t irq_state = mp_hal_quiet_timing_enter();
54+
55+
for (size_t i = 0; i < len; ++i) {
56+
uint8_t b = buf[i];
57+
for (size_t j = 0; j < 8; ++j) {
58+
uint32_t start_ticks = mp_hal_ticks_cpu();
59+
mp_hal_pin_high(pin);
60+
uint32_t *t = &timing_ns[b >> 6 & 2];
61+
while ((mp_hal_ticks_cpu() - start_ticks) < t[0]) {
62+
}
63+
mp_hal_pin_low(pin);
64+
b <<= 1;
65+
while ((mp_hal_ticks_cpu() - start_ticks) < t[1]) {
66+
}
67+
}
68+
}
69+
70+
mp_hal_quiet_timing_exit(irq_state);
71+
}
72+
73+
#endif // MICROPY_PY_MACHINE_BITSTREAM

ports/mimxrt/modmachine.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727

2828
#include "py/runtime.h"
29+
#include "extmod/machine_bitstream.h"
2930
#include "extmod/machine_mem.h"
3031
#include "extmod/machine_i2c.h"
3132
#include "extmod/machine_pulse.h"
@@ -45,7 +46,7 @@ STATIC mp_obj_t machine_reset(void) {
4546
MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset);
4647

4748
STATIC mp_obj_t machine_freq(void) {
48-
return MP_OBJ_NEW_SMALL_INT(CLOCK_GetFreq(kCLOCK_CpuClk));
49+
return MP_OBJ_NEW_SMALL_INT(mp_hal_get_cpu_freq());
4950
}
5051
MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq);
5152

@@ -97,6 +98,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
9798
{ MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
9899
{ MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
99100

101+
#if MICROPY_PY_MACHINE_BITSTREAM
102+
{ MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) },
103+
#endif
100104
{ MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
101105
};
102106
STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);

ports/mimxrt/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ uint32_t trng_random_u32(void);
124124
#define MICROPY_PY_USELECT (1)
125125
#define MICROPY_PY_MACHINE (1)
126126
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
127+
#define MICROPY_PY_MACHINE_BITSTREAM (1)
127128
#define MICROPY_PY_MACHINE_PULSE (1)
128129
#define MICROPY_PY_MACHINE_I2C (1)
129130
#define MICROPY_PY_MACHINE_SOFTI2C (1)

ports/mimxrt/mphalport.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <stdint.h>
3131
#include "ticks.h"
3232
#include "pin.h"
33+
#include "fsl_clock.h"
3334

3435
#define MP_HAL_PIN_FMT "%q"
3536

@@ -39,8 +40,8 @@
3940
#define mp_hal_pin_input(p) machine_pin_set_mode(p, PIN_MODE_IN);
4041
#define mp_hal_pin_output(p) machine_pin_set_mode(p, PIN_MODE_OUT);
4142
#define mp_hal_pin_open_drain(p) machine_pin_set_mode(p, PIN_MODE_OPEN_DRAIN);
42-
#define mp_hal_pin_high(p) (GPIO_PinWrite(p->gpio, p->pin, 1U))
43-
#define mp_hal_pin_low(p) (GPIO_PinWrite(p->gpio, p->pin, 0U))
43+
#define mp_hal_pin_high(p) (p->gpio->DR_SET = 1 << p->pin)
44+
#define mp_hal_pin_low(p) (p->gpio->DR_CLEAR = 1 << p->pin)
4445
#define mp_hal_pin_write(p, value) (GPIO_PinWrite(p->gpio, p->pin, value))
4546
#define mp_hal_pin_toggle(p) (GPIO_PortToggle(p->gpio, (1 << p->pin)))
4647
#define mp_hal_pin_read(p) (GPIO_PinReadPadStatus(p->gpio, p->pin))
@@ -72,8 +73,16 @@ static inline void mp_hal_delay_us(mp_uint_t us) {
7273

7374
#define mp_hal_delay_us_fast(us) mp_hal_delay_us(us)
7475

76+
static inline void mp_hal_ticks_cpu_init(void) {
77+
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
78+
}
79+
7580
static inline mp_uint_t mp_hal_ticks_cpu(void) {
76-
return 0;
81+
return DWT->CYCCNT;
82+
}
83+
84+
static inline mp_uint_t mp_hal_get_cpu_freq(void) {
85+
return CLOCK_GetCpuClkFreq();
7786
}
7887

7988

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy