Skip to content

Commit 7fdfa93

Browse files
committed
stmhal: Add Timer class: simple TIM control, incl callback on IRQ.
Simple but functional timer control. More sophistication will eventually be added, or for now just use direct register access :) Also added pyb.freq() function to get MCU clock frequencies.
1 parent e5f8a77 commit 7fdfa93

6 files changed

Lines changed: 382 additions & 105 deletions

File tree

stmhal/main.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ void __fatal_error(const char *msg) {
7878
led_toggle(((i++) & 3) + 1);
7979
for (volatile uint delay = 0; delay < 10000000; delay++) {
8080
}
81-
if (i >= 8) {
81+
if (i >= 16) {
82+
// to conserve power
8283
__WFI();
8384
}
8485
}
@@ -457,15 +458,12 @@ int main(void) {
457458
}
458459
#endif
459460

461+
timer_init0();
462+
460463
#if MICROPY_HW_ENABLE_RNG
461464
rng_init0();
462465
#endif
463466

464-
#if MICROPY_HW_ENABLE_TIMER
465-
// timer
466-
//timer_init();
467-
#endif
468-
469467
i2c_init0();
470468
spi_init0();
471469

stmhal/modpyb.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "pyexec.h"
1515
#include "led.h"
1616
#include "pin.h"
17+
#include "timer.h"
1718
#include "extint.h"
1819
#include "usrsw.h"
1920
#include "rng.h"
@@ -31,14 +32,7 @@
3132
#include "modpyb.h"
3233
#include "ff.h"
3334

34-
STATIC mp_obj_t pyb_unique_id(void) {
35-
// get unique id; 96 bits
36-
byte *id = (byte*)0x1fff7a10;
37-
return mp_obj_new_bytes(id, 12);
38-
}
39-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_unique_id_obj, pyb_unique_id);
40-
41-
// get lots of info about the board
35+
// print lots of info about the board
4236
STATIC mp_obj_t pyb_info(uint n_args, const mp_obj_t *args) {
4337
// get and print unique id; 96 bits
4438
{
@@ -103,28 +97,44 @@ STATIC mp_obj_t pyb_info(uint n_args, const mp_obj_t *args) {
10397

10498
return mp_const_none;
10599
}
106-
107100
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_info_obj, 0, 1, pyb_info);
108101

102+
// get unique MCU id; 96 bits = 12 bytes
103+
STATIC mp_obj_t pyb_unique_id(void) {
104+
byte *id = (byte*)0x1fff7a10;
105+
return mp_obj_new_bytes(id, 12);
106+
}
107+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_unique_id_obj, pyb_unique_id);
108+
109+
// get clock frequencies
110+
// TODO should also be able to set frequency via this function
111+
STATIC mp_obj_t pyb_freq(void) {
112+
mp_obj_t tuple[4] = {
113+
mp_obj_new_int(HAL_RCC_GetSysClockFreq()),
114+
mp_obj_new_int(HAL_RCC_GetHCLKFreq()),
115+
mp_obj_new_int(HAL_RCC_GetPCLK1Freq()),
116+
mp_obj_new_int(HAL_RCC_GetPCLK2Freq()),
117+
};
118+
return mp_obj_new_tuple(4, tuple);
119+
}
120+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_freq_obj, pyb_freq);
121+
109122
// sync all file systems
110123
STATIC mp_obj_t pyb_sync(void) {
111124
storage_flush();
112125
return mp_const_none;
113126
}
114-
115127
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_sync_obj, pyb_sync);
116128

117129
STATIC mp_obj_t pyb_millis(void) {
118130
return mp_obj_new_int(HAL_GetTick());
119131
}
120-
121132
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
122133

123134
STATIC mp_obj_t pyb_delay(mp_obj_t count) {
124135
HAL_Delay(mp_obj_get_int(count));
125136
return mp_const_none;
126137
}
127-
128138
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay);
129139

130140
STATIC mp_obj_t pyb_udelay(mp_obj_t usec) {
@@ -241,8 +251,9 @@ MP_DECLARE_CONST_FUN_OBJ(pyb_usb_mode_obj); // defined in main.c
241251
STATIC const mp_map_elem_t pyb_module_globals_table[] = {
242252
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_pyb) },
243253

244-
{ MP_OBJ_NEW_QSTR(MP_QSTR_unique_id), (mp_obj_t)&pyb_unique_id_obj },
245254
{ MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_info_obj },
255+
{ MP_OBJ_NEW_QSTR(MP_QSTR_unique_id), (mp_obj_t)&pyb_unique_id_obj },
256+
{ MP_OBJ_NEW_QSTR(MP_QSTR_freq), (mp_obj_t)&pyb_freq_obj },
246257
{ MP_OBJ_NEW_QSTR(MP_QSTR_gc), (mp_obj_t)&pyb_gc_obj },
247258
{ MP_OBJ_NEW_QSTR(MP_QSTR_repl_info), (mp_obj_t)&pyb_set_repl_info_obj },
248259

@@ -264,6 +275,8 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
264275
{ MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&pyb_udelay_obj },
265276
{ MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&pyb_sync_obj },
266277

278+
{ MP_OBJ_NEW_QSTR(MP_QSTR_Timer), (mp_obj_t)&pyb_timer_type },
279+
267280
#if MICROPY_HW_ENABLE_RNG
268281
{ MP_OBJ_NEW_QSTR(MP_QSTR_rng), (mp_obj_t)&pyb_rng_get_obj },
269282
#endif

stmhal/qstrdefsport.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ Q(all)
9191
Q(send)
9292
Q(recv)
9393

94+
// for Timer class
95+
Q(Timer)
96+
Q(counter)
97+
Q(prescaler)
98+
Q(period)
99+
Q(callback)
100+
Q(freq)
101+
Q(mode)
102+
Q(div)
103+
94104
// for ExtInt class
95105
Q(ExtInt)
96106
Q(pin)

stmhal/stm32f4xx_it.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,55 @@ void RTC_WKUP_IRQHandler(void) {
355355
Handle_EXTI_Irq(EXTI_RTC_WAKEUP);
356356
}
357357

358+
void TIM1_BRK_TIM9_IRQHandler(void) {
359+
timer_irq_handler(9);
360+
}
361+
362+
void TIM1_UP_TIM10_IRQHandler(void) {
363+
timer_irq_handler(1);
364+
timer_irq_handler(10);
365+
}
366+
367+
void TIM1_TRG_COM_TIM11_IRQHandler(void) {
368+
timer_irq_handler(11);
369+
}
370+
371+
void TIM2_IRQHandler(void) {
372+
timer_irq_handler(2);
373+
}
374+
358375
void TIM3_IRQHandler(void) {
359376
HAL_TIM_IRQHandler(&TIM3_Handle);
360377
}
361378

379+
void TIM4_IRQHandler(void) {
380+
timer_irq_handler(4);
381+
}
382+
362383
void TIM5_IRQHandler(void) {
384+
timer_irq_handler(5);
363385
HAL_TIM_IRQHandler(&TIM5_Handle);
364386
}
365387

388+
void TIM6_DAC_IRQHandler(void) {
389+
timer_irq_handler(6);
390+
}
391+
392+
void TIM7_IRQHandler(void) {
393+
timer_irq_handler(7);
394+
}
395+
396+
void TIM8_BRK_TIM12_IRQHandler(void) {
397+
timer_irq_handler(12);
398+
}
399+
400+
void TIM8_UP_TIM13_IRQHandler(void) {
401+
timer_irq_handler(8);
402+
timer_irq_handler(13);
403+
}
404+
405+
void TIM8_TRG_COM_TIM14_IRQHandler(void) {
406+
timer_irq_handler(14);
407+
}
408+
366409
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

0 commit comments

Comments
 (0)