Use RTC for C library time source.

Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
Yilin Sun 2023-03-08 16:40:56 +08:00
parent a7fe04e077
commit d5bf9c6f26
3 changed files with 64 additions and 10 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.10)
project(lpcxpresso_55s69_template)
project(lpc55s69_mruby)
enable_language(CXX)
enable_language(ASM)

View File

@ -1,5 +1,6 @@
#include <malloc.h>
#include <string.h>
#include <time.h>
/* FreeRTOS */
#include "FreeRTOS.h"
@ -120,6 +121,14 @@ int app_mrb_repl_init(void) {
mrb_show_version(s_mrb_state);
mrb_show_copyright(s_mrb_state);
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
printf("Time: %s", asctime(timeinfo));
return 0;
mrbc_cxt_alloc_fail:
@ -206,8 +215,6 @@ int app_mrb_repl_exec(bool *exit) {
free(line);
printf("%s\n", s_ruby_code);
struct mrb_parser_state *parser = mrb_parser_new(s_mrb_state);
if (parser == NULL) {
*exit = true;
@ -227,7 +234,6 @@ int app_mrb_repl_exec(bool *exit) {
} else {
/* TODO: Compile block and feed to VM */
/* TODO: Adjust CXT->lineno */
/* FIXME: Leakage found in this code path, FIX IT!! */
mrb_gc_arena_restore(s_mrb_state, s_gc_arena_idx);

View File

@ -1,12 +1,15 @@
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
/* Board */
#include "pin_mux.h"
/* SDK drivers */
#include "fsl_power.h"
#include "fsl_rtc.h"
#include "fsl_usart.h"
/* FreeRTOS */
@ -31,7 +34,9 @@
#define APP_STDIO_UART_EVENT_RX_DONE (1 << 1U)
#define APP_STDIO_UART_EVENT_ERROR (1 << 2U)
static SemaphoreHandle_t s_malloc_lock = NULL;
#define APP_TIME_RTC_INIT_RETRIES 32
static SemaphoreHandle_t s_malloc_lock = NULL;
static SemaphoreHandle_t s_stdio_tx_lock = NULL;
static SemaphoreHandle_t s_stdio_rx_lock = NULL;
@ -39,7 +44,7 @@ static EventGroupHandle_t s_stdio_event = NULL;
/* ========== Internal Use Functions ========== */
static int app_stdio_init(void) {
int ret = 0;
int ret = 0;
s_stdio_tx_lock = xSemaphoreCreateMutex();
if (s_stdio_tx_lock == NULL) {
@ -72,7 +77,7 @@ static int app_stdio_init(void) {
usart_config.stopBitCount = kUSART_OneStopBit;
usart_config.parityMode = kUSART_ParityDisabled;
uint32_t stdio_fc_clk_freq = CLOCK_GetFlexCommClkFreq(APP_STDIO_UART_ID);
uint32_t stdio_fc_clk_freq = CLOCK_GetFlexCommClkFreq(APP_STDIO_UART_ID);
if (USART_Init(APP_STDIO_UART_INSTANCE, &usart_config, stdio_fc_clk_freq) != kStatus_Success) {
ret = -1;
goto free_event_exit;
@ -99,7 +104,7 @@ free_tx_sem_exit:
}
void APP_STDIO_UART_IRQ_HANDLER(void) {
uint32_t event_flag = 0UL;
uint32_t event_flag = 0UL;
uint32_t interrupts_enabled = USART_GetEnabledInterrupts(APP_STDIO_UART_INSTANCE);
uint32_t fifo_int_status = APP_STDIO_UART_INSTANCE->FIFOINTSTAT;
@ -122,6 +127,35 @@ void APP_STDIO_UART_IRQ_HANDLER(void) {
portYIELD_FROM_ISR(higher_prio_task_woken);
}
static int app_rtc_time_init(void) {
int ret = 0;
POWER_DisablePD(kPDRUNCFG_PD_XTAL32K);
POWER_EnablePD(kPDRUNCFG_PD_FRO32K);
CLOCK_AttachClk(kXTAL32K_to_OSC32K);
RTC_Init(RTC);
RTC_EnableTimer(RTC, true);
RTC_EnableWakeupTimer(RTC, true);
CLOCK_SetRtc1hzClkDiv(32768U);
uint32_t rtc_sec = RTC_GetSecondsTimerCount(RTC);
uint8_t rtc_retries = 0U;
do {
SDK_DelayAtLeastUs(120 * 1000, CLOCK_GetCoreSysClkFreq());
rtc_retries++;
if (rtc_retries >= APP_TIME_RTC_INIT_RETRIES) {
ret = -1;
break;
}
} while (rtc_sec == RTC_GetSecondsTimerCount(RTC));
return ret;
}
int app_syscalls_init(void) {
s_malloc_lock = xSemaphoreCreateRecursiveMutex();
if (s_malloc_lock == NULL) {
@ -132,6 +166,10 @@ int app_syscalls_init(void) {
return -2;
}
if (app_rtc_time_init() != 0) {
return -3;
}
return 0;
}
@ -232,12 +270,22 @@ int _fstat(int file, struct stat *st) {
}
}
int _getpid() {
int _gettimeofday(struct timeval *ptimeval, void *ptimezone) {
ptimeval->tv_sec = RTC_GetSecondsTimerCount(RTC);
ptimeval->tv_usec = 0ULL;
return 0;
}
int _getpid(void) {
return 1;
}
int _isatty(int file) {
return 1;
if (file == STDIN_FILENO || file == STDOUT_FILENO || file == STDERR_FILENO) {
return 1;
}
return 0;
}
int _kill(int pid, int sig) {