From 41640820749f4aa70c892b45d7f14a1afb43c4a7 Mon Sep 17 00:00:00 2001 From: Yilin Sun Date: Sat, 18 Mar 2023 10:18:53 +0800 Subject: [PATCH] Implemented sleep and usleep syscalls. Signed-off-by: Yilin Sun --- README.md | 8 ++++++++ lib/mruby | 2 +- src/app_syscalls.c | 10 ++++++++++ src/mrb_machine_impl/mrb_machine_adc_impl.c | 15 +++++++++------ 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 57a3b69..63dbc5d 100644 --- a/README.md +++ b/README.md @@ -19,4 +19,12 @@ led_red.toggle # Toggle output ```ruby ana_bg = Machine::ADC.new(52) # BandGap ana_bg.value +``` + +```ruby +1000.times do |_| + puts "[#{Time.now}] BandGap voltage: #{'%.2f' % (ana_bg.value.to_f / 1000.0)}mV" + led_red.toggle + usleep(100 * 1000) +end ``` \ No newline at end of file diff --git a/lib/mruby b/lib/mruby index 6be5185..309be95 160000 --- a/lib/mruby +++ b/lib/mruby @@ -1 +1 @@ -Subproject commit 6be5185d38c88b22a9681f4937d47d568651ab73 +Subproject commit 309be9522ba70fc1371ea50b3033b49f4428a01f diff --git a/src/app_syscalls.c b/src/app_syscalls.c index e7ab79c..ad72e0d 100644 --- a/src/app_syscalls.c +++ b/src/app_syscalls.c @@ -300,4 +300,14 @@ int _kill(int pid, int sig) { int _lseek(int file, int offset, int whence) { return 0; +} + +unsigned int sleep(unsigned int sec) { + vTaskDelay(pdMS_TO_TICKS(sec * 1000)); + return 0; +} + +int usleep(useconds_t us) { + vTaskDelay(pdMS_TO_TICKS(us / 1000)); + return 0; } \ No newline at end of file diff --git a/src/mrb_machine_impl/mrb_machine_adc_impl.c b/src/mrb_machine_impl/mrb_machine_adc_impl.c index e6234e0..da6189e 100644 --- a/src/mrb_machine_impl/mrb_machine_adc_impl.c +++ b/src/mrb_machine_impl/mrb_machine_adc_impl.c @@ -77,6 +77,7 @@ static void mrb_machine_adc_impl_init_peripheral(void) { LPADC_DoAutoCalibration(ADC0); NVIC_SetPriority(ADC0_IRQn, 5); + EnableIRQ(ADC0_IRQn); } int mrb_machine_adc_impl_config(uint32_t channel) { @@ -143,8 +144,6 @@ int mrb_machine_adc_impl_read(uint32_t channel) { } LPADC_EnableInterrupts(ADC0, kLPADC_FIFO0WatermarkInterruptEnable); - EnableIRQ(ADC0_IRQn); - LPADC_DoSoftwareTrigger(ADC0, 1U); if (xSemaphoreTake(s_adc_semaphore, pdMS_TO_TICKS(500)) != pdTRUE) { @@ -169,11 +168,15 @@ int mrb_machine_adc_impl_read(uint32_t channel) { void ADC0_IRQHandler(void) { BaseType_t higher_priority_woken = pdFALSE; - DisableIRQ(ADC0_IRQn); + uint32_t flags = LPADC_GetStatusFlags(ADC0); + LPADC_ClearStatusFlags(ADC0, flags); - xSemaphoreGiveFromISR(s_adc_semaphore, &higher_priority_woken); + if (flags & kLPADC_ResultFIFO0ReadyFlag) { + LPADC_DisableInterrupts(ADC0, kLPADC_FIFO0WatermarkInterruptEnable); + xSemaphoreGiveFromISR(s_adc_semaphore, &higher_priority_woken); - if (higher_priority_woken) { - portYIELD_FROM_ISR(higher_priority_woken); + if (higher_priority_woken) { + portYIELD_FROM_ISR(higher_priority_woken); + } } } \ No newline at end of file