From 42621fd9f360c34c4e0105ac3f2301c1316517f8 Mon Sep 17 00:00:00 2001 From: liuyihui Date: Wed, 19 Apr 2023 18:15:42 +0800 Subject: [PATCH] feat: uart interrupt; remove: custom sysinit --- include/derivative.h | 4 +++- include/systick.h | 10 ---------- include/uart.h | 10 ++++++---- main.c | 8 ++++---- src/derivative.c | 11 +++++++++++ src/startup.c | 13 +++---------- 6 files changed, 27 insertions(+), 29 deletions(-) diff --git a/include/derivative.h b/include/derivative.h index c49682f..1520edd 100644 --- a/include/derivative.h +++ b/include/derivative.h @@ -3,7 +3,6 @@ #include "MKL25Z4.h" // Define Clock Settings - #define CORCLK_DEFAULT 20970000u // Default Core clock, 20.97 Mhz #define BUSCLK_DEFAULT 10500000u // Default Bus Rate clock, 10.5 Mhz, half of CORCLK @@ -18,3 +17,6 @@ extern uint32_t CORCLK; extern uint32_t BUSCLK; +extern volatile uint32_t ms_ticks; +extern void SysTick_Handler(void); +extern void UART1_IRQHandler(void); diff --git a/include/systick.h b/include/systick.h index ade8de2..f25b60b 100644 --- a/include/systick.h +++ b/include/systick.h @@ -6,19 +6,9 @@ extern volatile uint32_t ms_ticks; static inline void spin(volatile uint32_t count) { - if (count == 0) return; while (count--) asm("nop"); } -static inline void systick_init(uint32_t ticks) { - if ((ticks - 1) > 0xffffff) return; // Systick timer is 24 bit - SysTick->LOAD = ticks - 1; - SysTick->VAL = 0; - SysTick->CTRL = SysTick_CTRL_ENABLE_Msk // Enable systick timer - | SysTick_CTRL_TICKINT_Msk // Enable interrupt - | SysTick_CTRL_CLKSOURCE_Msk; // Use butin-in clock -} - // t: expiration time, prd: period static inline bool timer_expired(uint32_t *t, uint32_t prd) { if (ms_ticks + prd < *t) *t = 0; // Time wrapped? Reset timer diff --git a/include/uart.h b/include/uart.h index e6f4816..d29993d 100644 --- a/include/uart.h +++ b/include/uart.h @@ -42,6 +42,11 @@ static inline void uart_init(UART_Type *UART, unsigned long baud) { } static inline void uart_rie_enable(UART_Type *UART) { + // Enable UART interrupt + if (UART == UART1) + NVIC_EnableIRQ(UART1_IRQn); + else if (UART == UART2) + NVIC_EnableIRQ(UART2_IRQn); UART->C2 |= UART_C2_RIE_MASK; // Receiver interrupt enable } @@ -77,10 +82,7 @@ static inline size_t uart_getline(UART_Type *UART, char *buf) { while (!uart_read_ready(UART)) asm("nop"); *(uint8_t *)buf = (unsigned char)uart_read_byte(UART); cnt += 1; - if (*(uint8_t *)buf == 0x0d) { - *(uint8_t *)buf = 0x0a; - break; - } + if (*(uint8_t *)buf == 0x0a) break; (uint8_t *)buf++; } return cnt; diff --git a/main.c b/main.c index 4a317ad..8ba9a92 100644 --- a/main.c +++ b/main.c @@ -6,9 +6,9 @@ int main(void) { // Initialize - systick_init(CORCLK / 1000); // Period of systick timer : 1ms - uart_init(UART_MSG, 9600); // Initialize UART1 with PC - uart_rie_enable(UART_MSG); // Enable UART1 receive interrupt + SysTick_Config(CORCLK / 1000); // Period of systick timer : 1ms + uart_init(UART_MSG, 9600); // Initialize UART1 with PC + uart_rie_enable(UART_MSG); // Enable UART1 receive interrupt uart_printf(UART_MSG, "System Clock: %lu\r\n", CORCLK); uart_printf(UART_MSG, "Bus Clock: %lu\r\n", BUSCLK); @@ -16,7 +16,7 @@ int main(void) { uint32_t timer = 0; for (;;) { if (timer_expired(&timer, 1000)) { - uart_printf(UART_MSG, "LED: %d, tick: %lu\r\n", GPIOC->PDOR & BIT(12) ? 1 : 0, ms_ticks); + uart_printf(UART_MSG, "UART RD: %d, tick: %lu\r\n", UART_MSG->S1 & UART_S1_RDRF_MASK ? 1 : 0, ms_ticks); } } } diff --git a/src/derivative.c b/src/derivative.c index c7470d0..518814f 100644 --- a/src/derivative.c +++ b/src/derivative.c @@ -1,4 +1,5 @@ #include "derivative.h" +#include "uart.h" uint32_t CORCLK = CORCLK_DEFAULT; uint32_t BUSCLK = BUSCLK_DEFAULT; @@ -7,3 +8,13 @@ uint32_t BUSCLK = BUSCLK_DEFAULT; volatile uint32_t ms_ticks; void SysTick_Handler(void) { ms_ticks++; } + +void UART1_IRQHandler(void) { + if (UART1->S1 & UART_S1_RDRF_MASK) { + char buf[64]; + size_t len = uart_getline(UART1, buf); + uart_printf(UART_MSG, "%d: ", len); + uart_write_buf(UART_MSG, buf, len); + buf[0] = UART1->D; + } +} diff --git a/src/startup.c b/src/startup.c index ede8068..0056fa7 100644 --- a/src/startup.c +++ b/src/startup.c @@ -21,8 +21,8 @@ static void copy_data(void) { memcpy(_sdata, _sidata, (size_t)(_edata - _sdata)); } -uint32_t MCGOUTClock; -uint16_t Divider; +static uint32_t MCGOUTClock; +static uint16_t Divider; static void FLL_clock(void) { /* @@ -173,14 +173,6 @@ __attribute__((naked, noreturn)) void _reset(void) { // Interrupt service routine (ISR) extern void _estack(void); // Defined in link.ld -/* - Set tab (the vector table) in the section ".vectors" - and the size of the vector table is 16 + 32 - the first 16 * 4 bytes are reserved for the Cortex-M0+ core - the first one is the initial stack pointer - the second one is the initial program counter -*/ - void Default_Handler() { __asm("bkpt"); } void NMI_Handler() __attribute__((weak, alias("Default_Handler"))); void HardFault_Handler() __attribute__((weak, alias("Default_Handler"))); @@ -188,6 +180,7 @@ void SVC_Handler() __attribute__((weak, alias("Default_Handler"))); void PendSV_Handler() __attribute__((weak, alias("Default_Handler"))); void SysTick_Handler() __attribute__((weak, alias("Default_Handler"))); +// Interrupt request (IRQ) Handlers void DMA0_IRQHandler() __attribute__((weak, alias("Default_Handler"))); void DMA1_IRQHandler() __attribute__((weak, alias("Default_Handler"))); void DMA2_IRQHandler() __attribute__((weak, alias("Default_Handler")));