Skip to content

Commit 2adb071

Browse files
committed
millis(): fix backward count on limit hit
1 parent 060e221 commit 2adb071

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

cores/arduino/IRQManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#define EXTERNAL_PIN_PRIORITY 12
2121
#define UART_SCI_PRIORITY 12
2222
#define USB_PRIORITY 12
23-
#define AGT_PRIORITY 12
23+
#define AGT_PRIORITY 14
2424
#define RTC_PRIORITY 12
2525
#define I2C_MASTER_PRIORITY 12
2626
#define I2C_SLAVE_PRIORITY 12

cores/arduino/time.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ static FspTimer main_timer;
2828
static uint32_t _top_counter;
2929

3030
static void timer_micros_callback(timer_callback_args_t __attribute((unused)) *p_args) {
31-
agt_time_ms += 10; //10ms
31+
agt_time_ms += 1; //1ms
3232
}
3333

3434
void startAgt() {
35-
main_timer.begin(TIMER_MODE_PERIODIC, AGT_TIMER, 0, (uint32_t) 0x7530, 0x3a98, (timer_source_div_t) 3, timer_micros_callback);
35+
main_timer.begin(TIMER_MODE_PERIODIC, AGT_TIMER, 0, 2000.0f, 0.5f, timer_micros_callback);
3636
IRQManager::getInstance().addPeripheral(IRQ_AGT,(void*)main_timer.get_cfg());
3737
main_timer.open();
3838
_top_counter = main_timer.get_counter();
@@ -45,19 +45,15 @@ unsigned long millis()
4545
#ifdef AZURE_RTOS_THREADX
4646
return tx_time_get();
4747
#else
48-
// Convert time to ms
49-
NVIC_DisableIRQ(main_timer.get_cfg()->cycle_end_irq);
50-
uint32_t time_ms = ((main_timer.get_period_raw() - main_timer.get_counter()) / main_timer.get_cfg()->source_div)/1000 + agt_time_ms;
51-
NVIC_EnableIRQ(main_timer.get_cfg()->cycle_end_irq);
52-
return time_ms;
48+
return agt_time_ms;
5349
#endif
5450
}
5551

5652
unsigned long micros() {
57-
53+
5854
// Convert time to us
5955
NVIC_DisableIRQ(main_timer.get_cfg()->cycle_end_irq);
60-
uint32_t time_us = ((main_timer.get_period_raw() - main_timer.get_counter()) / main_timer.get_cfg()->source_div) + agt_time_ms * 1000;
56+
uint32_t time_us = ((main_timer.get_period_raw() - main_timer.get_counter()) * 1000 / main_timer.get_period_raw()) + (agt_time_ms * 1000);
6157
NVIC_EnableIRQ(main_timer.get_cfg()->cycle_end_irq);
6258
return time_us;
6359
}

0 commit comments

Comments
 (0)