feat: uart interrupt; remove: custom sysinit

This commit is contained in:
liuyihui 2023-04-19 18:15:42 +08:00
parent 3e1079ec1b
commit 42621fd9f3
6 changed files with 27 additions and 29 deletions

View File

@ -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);

View File

@ -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

View File

@ -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;

8
main.c
View File

@ -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);
}
}
}

View File

@ -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;
}
}

View File

@ -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")));