Skip to content

Commit cd9b14b

Browse files
committed
stmhal/modutime: Refactor to use extmod's version of ticks_cpu.
1 parent 5c93d0b commit cd9b14b

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

stmhal/modutime.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,6 @@ STATIC mp_obj_t time_time(void) {
130130
}
131131
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
132132

133-
STATIC mp_obj_t time_ticks_cpu(void) {
134-
static bool enabled = false;
135-
if (!enabled) {
136-
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
137-
DWT->CYCCNT = 0;
138-
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
139-
enabled = true;
140-
}
141-
return MP_OBJ_NEW_SMALL_INT(DWT->CYCCNT & MP_SMALL_INT_POSITIVE_MASK);
142-
}
143-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_cpu_obj, time_ticks_cpu);
144-
145133
STATIC const mp_map_elem_t time_module_globals_table[] = {
146134
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_utime) },
147135

@@ -153,7 +141,7 @@ STATIC const mp_map_elem_t time_module_globals_table[] = {
153141
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_us), (mp_obj_t)&mp_utime_sleep_us_obj },
154142
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&mp_utime_ticks_ms_obj },
155143
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&mp_utime_ticks_us_obj },
156-
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_cpu), (mp_obj_t)&time_ticks_cpu_obj },
144+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_cpu), (mp_obj_t)&mp_utime_ticks_cpu_obj },
157145
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&mp_utime_ticks_diff_obj },
158146
};
159147

stmhal/mphalport.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "usb.h"
88
#include "uart.h"
99

10+
bool mp_hal_ticks_cpu_enabled = false;
11+
1012
// this table converts from HAL_StatusTypeDef to POSIX errno
1113
const byte mp_hal_status_to_errno_table[4] = {
1214
[HAL_OK] = 0,
@@ -71,6 +73,15 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
7173
}
7274
}
7375

76+
void mp_hal_ticks_cpu_enable(void) {
77+
if (!mp_hal_ticks_cpu_enabled) {
78+
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
79+
DWT->CYCCNT = 0;
80+
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
81+
mp_hal_ticks_cpu_enabled = true;
82+
}
83+
}
84+
7485
void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio) {
7586
if (0) {
7687
#ifdef __GPIOA_CLK_ENABLE

stmhal/mphalport.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,24 @@ NORETURN void mp_hal_raise(HAL_StatusTypeDef status);
3535
void mp_hal_set_interrupt_char(int c); // -1 to disable
3636

3737
// timing functions
38+
3839
#include "stmhal/systick.h"
40+
3941
#define mp_hal_delay_ms HAL_Delay
4042
#define mp_hal_delay_us(us) sys_tick_udelay(us)
4143
#define mp_hal_delay_us_fast(us) sys_tick_udelay(us)
4244
#define mp_hal_ticks_ms HAL_GetTick
4345
#define mp_hal_ticks_us() sys_tick_get_microseconds()
4446

47+
extern bool mp_hal_ticks_cpu_enabled;
48+
void mp_hal_ticks_cpu_enable(void);
49+
static inline mp_uint_t mp_hal_ticks_cpu(void) {
50+
if (!mp_hal_ticks_cpu_enabled) {
51+
mp_hal_ticks_cpu_enable();
52+
}
53+
return DWT->CYCCNT;
54+
}
55+
4556
// C-level pin HAL
4657
#include "stmhal/pin.h"
4758
#define mp_hal_pin_obj_t const pin_obj_t*

0 commit comments

Comments
 (0)