Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb9d8f65

Browse files
committed
rp2: Refactor soft timer to use hardware timer alarm.
Progress towards removing pico-sdk alarm pool, due to a known issue.This work was funded through GitHub Sponsors.Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent9feb068 commitb9d8f65

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

‎ports/rp2/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ int main(int argc, char **argv) {
7676
// This is a tickless port, interrupts should always trigger SEV.
7777
SCB->SCR |=SCB_SCR_SEVONPEND_Msk;
7878

79+
soft_timer_init();
80+
7981
#ifMICROPY_HW_ENABLE_UART_REPL
8082
bi_decl(bi_program_feature("UART REPL"))
8183
setup_default_uart();

‎ports/rp2/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@
147147
#defineMICROPY_SSL_MBEDTLS (1)
148148
#defineMICROPY_PY_LWIP_SOCK_RAW (MICROPY_PY_LWIP)
149149

150+
#defineMICROPY_HW_SOFT_TIMER_ALARM_NUM (2)
151+
150152
// fatfs configuration
151153
#defineMICROPY_FATFS_ENABLE_LFN (1)
152154
#defineMICROPY_FATFS_LFN_CODE_PAGE 437/* 1=SFN/ANSI 437=LFN/U.S.(OEM) */

‎ports/rp2/mphalport.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include"hardware/irq.h"
2728
#include"py/runtime.h"
2829
#include"py/stream.h"
2930
#include"py/mphal.h"
@@ -46,8 +47,6 @@
4647
// microseconds since the Epoch.
4748
STATICuint64_ttime_us_64_offset_from_epoch;
4849

49-
staticalarm_id_tsoft_timer_alarm_id=0;
50-
5150
#ifMICROPY_HW_ENABLE_UART_REPL||MICROPY_HW_USB_CDC
5251

5352
#ifndefMICROPY_HW_STDIN_BUFFER_LEN
@@ -273,21 +272,23 @@ uint32_t storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blo
273272
panic_unsupported();
274273
}
275274

276-
staticint64_tsoft_timer_callback(alarm_id_tid,void*user_data) {
277-
soft_timer_alarm_id=0;
278-
pendsv_schedule_dispatch(PENDSV_DISPATCH_SOFT_TIMER,soft_timer_handler);
279-
return0;// don't reschedule this alarm
280-
}
281-
282275
uint32_tsoft_timer_get_ms(void) {
283276
returnmp_hal_ticks_ms();
284277
}
285278

286279
voidsoft_timer_schedule_at_ms(uint32_tticks_ms) {
287-
if (soft_timer_alarm_id!=0) {
288-
cancel_alarm(soft_timer_alarm_id);
289-
}
290280
int32_tms=soft_timer_ticks_diff(ticks_ms,mp_hal_ticks_ms());
291281
ms=MAX(0,ms);
292-
soft_timer_alarm_id=add_alarm_in_ms(ms,soft_timer_callback,NULL, true);
282+
if (hardware_alarm_set_target(MICROPY_HW_SOFT_TIMER_ALARM_NUM,delayed_by_ms(get_absolute_time(),ms))) {
283+
// "missed" hardware alarm target
284+
hardware_alarm_force_irq(MICROPY_HW_SOFT_TIMER_ALARM_NUM);
285+
}
286+
}
287+
288+
voidsoft_timer_init(void) {
289+
hardware_alarm_claim(MICROPY_HW_SOFT_TIMER_ALARM_NUM);
290+
hardware_alarm_set_callback(MICROPY_HW_SOFT_TIMER_ALARM_NUM, (void*)soft_timer_handler);
291+
// soft timer IRQ handler has to run at PendSV priority. adjusting priority here so we don't
292+
// need to trigger PendSV the timer IRQ.
293+
NVIC_SetPriority(TIMER_IRQ_0_IRQn+MICROPY_HW_SOFT_TIMER_ALARM_NUM,PICO_LOWEST_IRQ_PRIORITY);
293294
}

‎shared/runtime/softtimer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ static inline void soft_timer_reinsert(soft_timer_entry_t *entry, uint32_t initi
7373
#if !defined(MICROPY_SOFT_TIMER_TICKS_MS)
7474
// IF MICROPY_SOFT_TIMER_TICKS_MS is not defined then the port must provide the
7575
// following timer functions:
76+
// - soft_timer_init() to initialize soft timer hardware as needed.
7677
// - soft_timer_get_ms() must return a 32-bit millisecond counter that wraps around.
7778
// - soft_timer_schedule_at_ms(ticks_ms) must schedule a callback of soft_timer_handler()
7879
// when the above millisecond counter reaches the given ticks_ms value. If ticks_ms
7980
// is behind the current counter (using int32_t arithmetic) then the callback should
8081
// be scheduled immediately. The callback of soft_timer_handler() should be made at
8182
// pend-SV IRQ level, or equivalent.
83+
voidsoft_timer_init(void);
8284
uint32_tsoft_timer_get_ms(void);
8385
voidsoft_timer_schedule_at_ms(uint32_tticks_ms);
8486
#endif

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp