11 typedef int (*rx_callback_t)(
unsigned char);
15 static volatile uint8_t transmitting[
NUM_UARTS];
17 #ifdef UART_CONF_DEFAULT_TXBUFSIZE
18 #define UART_DEFAULT_TXBUFSIZE UART_CONF_DEFAULT_TXBUFSIZE
20 #define UART_DEFAULT_TXBUFSIZE 8
22 #ifdef UART0_CONF_TXBUFSIZE
23 #define UART0_TXBUFSIZE UART0_CONF_TXBUFSIZE
25 #define UART0_TXBUFSIZE UART_DEFAULT_TXBUFSIZE
27 #ifdef UART1_CONF_TXBUFSIZE
28 #define UART1_TXBUFSIZE UART1_CONF_TXBUFSIZE
30 #define UART1_TXBUFSIZE UART_DEFAULT_TXBUFSIZE
32 #ifdef UART2_CONF_TXBUFSIZE
33 #define UART2_TXBUFSIZE UART2_CONF_TXBUFSIZE
35 #define UART2_TXBUFSIZE UART_DEFAULT_TXBUFSIZE
37 #ifdef UART3_CONF_TXBUFSIZE
38 #define UART3_TXBUFSIZE UART3_CONF_TXBUFSIZE
40 #define UART3_TXBUFSIZE UART_DEFAULT_TXBUFSIZE
42 #ifdef UART4_CONF_TXBUFSIZE
43 #define UART4_TXBUFSIZE UART4_CONF_TXBUFSIZE
45 #define UART4_TXBUFSIZE UART_DEFAULT_TXBUFSIZE
51 static uint8_t uart0_txbuf_data[UART0_TXBUFSIZE];
54 static uint8_t uart1_txbuf_data[UART1_TXBUFSIZE];
57 static uint8_t uart2_txbuf_data[UART2_TXBUFSIZE];
60 static uint8_t uart3_txbuf_data[UART3_TXBUFSIZE];
63 static uint8_t uart4_txbuf_data[UART4_TXBUFSIZE];
66 static uint8_t uart5_txbuf_data[UART5_TXBUFSIZE];
69 static inline void tx_irq_handler(
const unsigned int uart_num,
const uint8_t s1) {
70 volatile UART_Type *uart_dev = UART[uart_num];
71 if((s1 & UART_S1_TC_MASK) && (uart_dev->
C2 & UART_C2_TCIE_MASK) && (transmitting[uart_num] != 0)) {
73 LLWU_UNINHIBIT_STOP();
75 uart_dev->
C2 &= ~(UART_C2_TCIE_MASK);
76 transmitting[uart_num] = 0;
79 if((s1 & UART_S1_TDRE_MASK) && (uart_dev->
C2 & UART_C2_TIE_MASK)) {
84 uart_dev->
C2 &= ~(UART_C2_TIE_MASK);
86 uart_dev->
C2 |= UART_C2_TCIE_MASK;
89 transmitting[uart_num] = 1;
90 uart_dev->
D = (uint8_t)(ret & 0xff);
95 static inline void rx_irq_handler(
const unsigned int uart_num,
const uint8_t s1) {
96 volatile UART_Type *uart_dev = UART[uart_num];
97 if((s1 & UART_S1_RDRF_MASK) && (rx_callback[uart_num] !=
NULL)) {
98 (rx_callback[uart_num])(uart_dev->
D);
101 if((uart_dev->
S2 & UART_S2_RXEDGIF_MASK)) {
103 uart_dev->
S2 |= UART_S2_RXEDGIF_MASK;
154 uart_init(
const unsigned int uart_num, uint32_t module_clk_hz,
const uint32_t baud)
156 volatile UART_Type *uart_dev = UART[uart_num];
159 if (module_clk_hz == 0) {
180 sbr =
UART_SBR(module_clk_hz, baud);
187 uart_dev->
C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK);
194 uart_dev->
BDH = (uart_dev->
BDH & ~(UART_BDH_SBR_MASK)) | UART_BDH_SBR(sbr >> 8);
196 uart_dev->
BDL = (uart_dev->
BDL & ~(UART_BDL_SBR_MASK)) | UART_BDL_SBR(sbr);
198 uart_dev->
C4 = (uart_dev->
C4 & ~(UART_C4_BRFA_MASK)) | UART_C4_BRFA(brfa);
201 uart_dev->
C2 |= UART_C2_TE_MASK | UART_C2_RE_MASK;
203 transmitting[uart_num] = 0;
207 #if UART0_CONF_ENABLE
209 ringbuf_init(&uart_txbuf[uart_num], &uart0_txbuf_data[0],
sizeof(uart0_txbuf_data));
213 #if UART1_CONF_ENABLE
215 ringbuf_init(&uart_txbuf[uart_num], &uart1_txbuf_data[0],
sizeof(uart1_txbuf_data));
219 #if UART2_CONF_ENABLE
221 ringbuf_init(&uart_txbuf[uart_num], &uart2_txbuf_data[0],
sizeof(uart2_txbuf_data));
225 #if UART3_CONF_ENABLE
227 ringbuf_init(&uart_txbuf[uart_num], &uart3_txbuf_data[0],
sizeof(uart3_txbuf_data));
231 #if UART4_CONF_ENABLE
233 ringbuf_init(&uart_txbuf[uart_num], &uart4_txbuf_data[0],
sizeof(uart4_txbuf_data));
237 #if UART5_CONF_ENABLE
239 ringbuf_init(&uart_txbuf[uart_num], &uart5_txbuf_data[0],
sizeof(uart5_txbuf_data));
250 uart_putchar(
const unsigned int uart_num,
const char ch)
252 volatile UART_Type *uart_dev = UART[uart_num];
255 while(
ringbuf_put(&uart_txbuf[uart_num], ch) == 0);
257 MK60_ENTER_CRITICAL_REGION();
260 uart_dev->
C2 |= UART_C2_TIE_MASK;
265 if (transmitting[uart_num] == 0) {
268 MK60_LEAVE_CRITICAL_REGION();
277 uart_putstring(
const unsigned int uart_num,
const char *str)
281 uart_putchar(uart_num, *p++);
286 uart_enable_rx_interrupt(
const unsigned int uart_num)
289 volatile UART_Type *uart_dev = UART[uart_num];
292 uart_dev->
C2 |= UART_C2_RIE_MASK;
293 uart_dev->
BDH |= UART_BDH_RXEDGIE_MASK;
297 uart_disable_rx_interrupt(
const unsigned int uart_num)
300 volatile UART_Type *uart_dev = UART[uart_num];
303 uart_dev->
C2 &= ~(UART_C2_RIE_MASK);
307 uart_set_rx_callback(
const unsigned int uart_num, rx_callback_t callback)
309 rx_callback[uart_num] = callback;
312 #if UART0_CONF_ENABLE
319 tx_irq_handler(0, s1);
320 rx_irq_handler(0, s1);
324 #if UART1_CONF_ENABLE
331 tx_irq_handler(1, s1);
332 rx_irq_handler(1, s1);
336 #if UART2_CONF_ENABLE
343 tx_irq_handler(2, s1);
344 rx_irq_handler(2, s1);
348 #if UART3_CONF_ENABLE
355 tx_irq_handler(3, s1);
356 rx_irq_handler(3, s1);
360 #if UART4_CONF_ENABLE
367 tx_irq_handler(4, s1);
368 rx_irq_handler(4, s1);
372 #if UART5_CONF_ENABLE
379 tx_irq_handler(5, s1);
380 rx_irq_handler(5, s1);
__IO uint8_t BDL
UART Baud Rate Registers: Low, offset: 0x1.
UART0 Receive/Transmit interrupt.
__STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
Enable External Interrupt.
K60 clock configuration defines.
#define UART_BRFA(f, b)
UART module fine adjust parameter based on module frequency f and desired baud rate b...
Header file for the ring buffer library
UART3 Receive/Transmit interrupt.
__IO uint8_t C4
UART Control Register 4, offset: 0xA.
void uart_init(const unsigned int uart_num, uint32_t module_clk_hz, const uint32_t baud)
Initialize UART.
Structure that holds the state of a ring buffer.
UART - Register Layout Typedef.
#define NUM_UARTS
Number of UART modules in CPU.
#define NULL
The null pointer.
#define SIM
Peripheral SIM base pointer.
int ringbuf_get(struct ringbuf *r)
Get a byte from the ring buffer.
void uart_module_enable(const unsigned int uart_num)
Enable the clock gate to an UART module.
#define UART3
Peripheral UART3 base pointer.
#define UART_SBR(f, b)
UART module SBR parameter based on module frequency f and desired baud rate b.
Board configuration defines for Mulle platform.
K60 hardware register header wrapper.
#define DEBUGGER_BREAK(sig)
Make the CPU signal to the debugger and break execution by issuing a bkpt instruction.
__IO uint8_t C1
UART Control Register 1, offset: 0x2.
__IO uint8_t BDH
UART Baud Rate Registers: High, offset: 0x0.
void ringbuf_init(struct ringbuf *r, uint8_t *dataptr, uint8_t size)
Initialize a ring buffer.
Provide common UART routines for MK60DZ10.
#define UART4
Peripheral UART4 base pointer.
#define UART1
Peripheral UART1 base pointer.
__IO uint8_t S2
UART Status Register 2, offset: 0x5.
UART5 Receive/Transmit interrupt.
UART2 Receive/Transmit interrupt.
__I uint8_t S1
UART Status Register 1, offset: 0x4.
UART1 Receive/Transmit interrupt.
#define UART2
Peripheral UART2 base pointer.
Provide common UART routines for MK60DZ10.
K60 interrupt save/restore macros.
#define UART0
Peripheral UART0 base pointer.
#define UART5
Peripheral UART5 base pointer.
UART4 Receive/Transmit interrupt.
#define BITBAND_REG(Reg, Bit)
Macro to access a single bit of a peripheral register (bit band region 0x40000000 to 0x400FFFFF) usin...
uint32_t SystemSysClock
Current system clock frequency.
__IO uint8_t D
UART Data Register, offset: 0x7.
__IO uint8_t C2
UART Control Register 2, offset: 0x3.
uint32_t SystemBusClock
Current bus clock frequency.
int ringbuf_put(struct ringbuf *r, uint8_t c)
Insert a byte into the ring buffer.