8000 cc3200: Refactor "ticks" functions to use common extmod implementation. · nickzoic/micropython-esp32@b7d27e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit b7d27e3

Browse files
committed
cc3200: Refactor "ticks" functions to use common extmod implementation.
The port now uses the common mp_utime_ticks_{ms,us,cpu,add,diff} functions from extmod/utime_mphal.c. The mp_utime_sleep_XXX functions are still cc3200-specific because they handle the GIL differently to the ones in extmod. The files misc/mpsystick.[ch] have been removed because they contain 2 unused functions, and the other remaining function is renamed to mp_hal_ticks_us and moved to hal/cc3200_hal.c.
1 parent a4a439c commit b7d27e3

File tree

7 files changed

+27
-140
lines changed

7 files changed

+27
-140
lines changed

cc3200/application.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ APP_MISC_SRC_C = $(addprefix misc/,\
7777
mpirq.c \
7878
mperror.c \
7979
mpexception.c \
80-
mpsystick.c \
8180
)
8281

8382
APP_MODS_SRC_C = $(addprefix mods/,\

cc3200/hal/cc3200_hal.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@ mp_uint_t mp_hal_ticks_ms(void) {
108108
return HAL_tickCount;
109109
}
110110

111+
// The SysTick timer counts down at HAL_FCPU_HZ, so we can use that knowledge
112+
// to grab a microsecond counter.
113+
mp_uint_t mp_hal_ticks_us(void) {
114+
mp_uint_t irq_state = disable_irq();
115+
uint32_t counter = SysTickValueGet();
116+
uint32_t milliseconds = mp_hal_ticks_ms();
117+
enable_irq(irq_state);
118+
119+
uint32_t load = SysTickPeriodGet();
120+
counter = load - counter; // Convert from decrementing to incrementing
121+
return (milliseconds * 1000) + ((counter * 1000) / load);
122+
}
123+
111124
void mp_hal_delay_ms(mp_uint_t delay) {
112125
// only if we are not within interrupt context and interrupts are enabled
113126
if ((HAL_NVIC_INT_CTRL_REG & HAL_VECTACTIVE_MASK) == 0 && query_irq() == IRQ_STATE_ENABLED) {
@@ -211,4 +224,3 @@ static void hal_TickInit (void) {
211224
MAP_SysTickEnable();
212225
}
213226
#endif
214-

cc3200/hal/cc3200_hal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#include <stdint.h>
3131
#include <stdbool.h>
3232

33+
#include "hal/utils.h"
34+
#include "hal/systick.h"
35+
3336
/******************************************************************************
3437
DEFINE CONSTANTS
3538
******************************************************************************/
@@ -64,4 +67,7 @@ extern void HAL_SystemDeInit (void);
6467
extern void HAL_IncrementTick(void);
6568
extern void mp_hal_set_interrupt_char (int c);
6669

70+
#define mp_hal_delay_us(usec) UtilsDelay(UTILS_DELAY_US_TO_COUNT(usec))
71+
#define mp_hal_ticks_cpu() (SysTickPeriodGet() - SysTickValueGet())
72+
6773
#endif /* CC3200_LAUNCHXL_HAL_CC3200_HAL_H_ */

cc3200/misc/mpsystick.c

Lines changed: 0 additions & 71 deletions
This file was deleted.

cc3200/misc/mpsystick.h

Lines changed: 0 additions & 35 deletions
This file was deleted.

cc3200/mods/modutime.c

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "py/obj.h"
3434
#include "py/smallint.h"
3535
#include "py/mphal.h"
36+
#include "extmod/utime_mphal.h"
3637
#include "timeutils.h"
3738
#include "inc/hw_types.h"
3839
#include "inc/hw_ints.h"
@@ -41,7 +42,6 @@
4142
#include "prcm.h"
4243
#include "systick.h"
4344
#include "pybrtc.h"
44-
#include "mpsystick.h"
4545
#include "mpexception.h"
4646
#include "utils.h"
4747

@@ -143,38 +143,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_ms_obj, time_sleep_ms);
143143
STATIC mp_obj_t time_sleep_us (mp_obj_t usec_in) {
144144
mp_int_t usec = mp_obj_get_int(usec_in);
145145
if (usec > 0) {
146-
UtilsDelay(UTILS_DELAY_US_TO_COUNT(usec));
146+
mp_hal_delay_us(usec);
147147
}
148148
return mp_const_none;
149149
}
150150
STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_us_obj, time_sleep_us);
151151

152-
STATIC mp_obj_t time_ticks_ms(void) {
153-
// We want to "cast" the 32 bit unsigned into a 30-bit small-int
154-
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & MP_SMALL_INT_POSITIVE_MASK);
155-
}
156-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_ms_obj, time_ticks_ms);
157-
158-
STATIC mp_obj_t time_ticks_us(void) {
159-
// We want to "cast" the 32 bit unsigned into a 30-bit small-int
160-
return MP_OBJ_NEW_SMALL_INT(sys_tick_get_microseconds() & MP_SMALL_INT_POSITIVE_MASK);
161-
}
162-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_us_obj, time_ticks_us);
163-
164-
STATIC mp_obj_t time_ticks_cpu(void) {
165-
// We want to "cast" the 32 bit unsigned into a 30-bit small-int
166-
return MP_OBJ_NEW_SMALL_INT((SysTickPeriodGet() - SysTickValueGet()) & MP_SMALL_INT_POSITIVE_MASK);
167-
}
168-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_cpu_obj, time_ticks_cpu);
169-
170-
STATIC mp_obj_t time_ticks_diff(mp_obj_t t0, mp_obj_t t1) {
171-
// We want to "cast" the 32 bit unsigned into a 30-bit small-int
172-
uint32_t start = mp_obj_get_int(t0);
173-
uint32_t end = mp_obj_get_int(t1);
174-
return MP_OBJ_NEW_SMALL_INT((end - start) & MP_SMALL_INT_POSITIVE_MASK);
175-
}
176-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(time_ticks_diff_obj, time_ticks_diff);
177-
178152
STATIC const mp_map_elem_t time_module_globals_table[] = {
179153
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_utime) },
180154

@@ -186,10 +160,11 @@ STATIC const mp_map_elem_t time_module_globals_table[] = {
186160
// MicroPython additions
187161
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_ms), (mp_obj_t)&time_sleep_ms_obj },
188162
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_us), (mp_obj_t)&time_sleep_us_obj },
189-
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&time_ticks_ms_obj },
190-
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&time_ticks_us_obj },
191-
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_cpu), (mp_obj_t)&time_ticks_cpu_obj },
192-
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&time_ticks_diff_obj },
163+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&mp_utime_ticks_ms_obj },
164+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&mp_utime_ticks_us_obj },
165+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_cpu), (mp_obj_t)&mp_utime_ticks_cpu_obj },
166+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_add), (mp_obj_t)&mp_utime_ticks_add_obj },
167+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&mp_utime_ticks_diff_obj },
193168
};
194169

195170
STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);

cc3200/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
#define MICROPY_PY_UHEAPQ (0)
117117
#define MICROPY_PY_UHASHLIB (0)
118118
#define MICROPY_PY_USELECT (1)
119+
#define MICROPY_PY_UTIME_MP_HAL (1)
119120

120121
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
121122
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (0)

0 commit comments

Comments
 (0)
0