SRD Standelone programmation box: uart0.c File Reference

| SRD Standelone programmation box 2.0 (16/11/15 10:00:00) Documentation of the SRD standelone programmation box

- Main Page
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Enumerations Enumerator Macros Pages
Functions | Variables
uart0.c File Reference

Source file for UART0 driver. More...

`#include "uart0.h"`
Include dependency graph for uart0.c:

Go to the source code of this file.

## Functions

void | UART0_IRQHandler (void)
| Interrupt handler for this uart. More...
void | Uart0Init (unsigned long baudrate)
| Initialize this uart. More...
void | Uart0SendData (char *data, unsigned short dataLength)
| send data other uart. More...
void | Uart0SendString (char *string)
| send ASCII string other uart. More...
char | Uart0ReadChar (void)

## Variables

tUartCtrl | Uart0Ctrl
char | rxBuf [UART0_RX_FIFO_SIZE]
| Build RX and TX fifo used by this uart. More...
char | txBuf [UART0_TX_FIFO_SIZE]
| Build RX and TX fifo used by this uart. More...

## Detailed Description

Source file for UART0 driver.

Author
  Thibaut LALUQUE 3D-Ouest

Date
  26/09/2014

Version
  1.0

supported device : MKL16Z256VLH4

All Right Reserved.

Definition in file uart0.c.

## Function Documentation

void UART0_IRQHandler | ( | void |  | ) |

Interrupt handler for this uart.

Definition at line 84 of file uart0.c.

References tUartFlags::bits, tUartFlags::charReceived, tUartFifo::data, tUartCtrl::flags, tUartFifo::lastIn, tUartFifo::lastOut, tUartFlags::moduleError, tUartFifo::nbrChar, tUartCtrl::rx, tUartFlags::rxOverrun, tUartFlags::stringReceived, tUartCtrl::tx, UART0_RX_FIFO_SIZE, UART0_TX_FIFO_SIZE, UART0ClearAllErrorFlag, UART0DisableTransmitter, and UART0DisableTransmitterInterrupt.

84 {
85
86 /*=== char receive ===*/
87 if(UART0->S1 & 0x20) {
88
89 char received = UART0_D;
90
91 #ifdef UART0_STRING_AUTO_REMOVE
92 if(received != UART0_STRING_AUTO_REMOVE) { //char will not be auto removed
93 #else
94 {
95 #endif
96
97 if(Uart0Ctrl.rx.nbrChar < UART0_RX_FIFO_SIZE) { //If FIFO is not full add new char to it
98 if(Uart0Ctrl.rx.lastIn < UART0_RX_FIFO_SIZE-1) {
99 Uart0Ctrl.rx.lastIn++;
100 } else {
101 Uart0Ctrl.rx.lastIn = 0;
102 }
103
104 Uart0Ctrl.rx.data[Uart0Ctrl.rx.lastIn] = received;
105 Uart0Ctrl.rx.nbrChar++;
106 Uart0Ctrl.flags.bits.charReceived = 1;
107 #ifdef UART0_STRING_ENDCHAR
108 if(received == UART0_STRING_ENDCHAR) { //Check if it's a end of frame
109 Uart0Ctrl.flags.bits.stringReceived = 1;
110 }
111 #endif
112 } else {
113 Uart0Ctrl.flags.bits.rxOverrun = 1; //FIFO is full -> raise error flag
114 }
115 }
116 }
117
118 /*=== Transmit data ===*/
119 if(UART0->S1 & 0x80) {
120 if(Uart0Ctrl.tx.nbrChar != 0) { //If the FIFO s not empty -> send next char
121 if(Uart0Ctrl.tx.lastOut < UART0_TX_FIFO_SIZE-1) {
122 Uart0Ctrl.tx.lastOut++;
123 } else {
124 Uart0Ctrl.tx.lastOut = 0;
125 }
126 UART0->D = Uart0Ctrl.tx.data[Uart0Ctrl.tx.lastOut];
127 Uart0Ctrl.tx.nbrChar--;
128 } else {
129 UART0DisableTransmitterInterrupt(); //If FIFO is empty -> turn off Tx interrupt
130 UART0DisableTransmitter();
131 }
132 }
133
134
135 /*=== Error module ===*/
136 if(UART0->S1 & 0x0F) {
137 Uart0Ctrl.flags.bits.moduleError = 1;
138 UART0ClearAllErrorFlag();
139 }
140
141 }
tUartCtrl::rx
tUartFifo rx
Definition: uart_common.h:77
UART0DisableTransmitterInterrupt
#define UART0DisableTransmitterInterrupt()
Enable / Disable transmitter and its interrupt.
Definition: uart0.h:203
tUartFifo::lastOut
unsigned short lastOut
Definition: uart_common.h:48
tUartFlags::rxOverrun
unsigned char rxOverrun
Definition: uart_common.h:63
tUartFlags::bits
struct tUartFlags::@21 bits
UART0_TX_FIFO_SIZE
#define UART0_TX_FIFO_SIZE
Define the TX fifo size for this uart.
Definition: uart0.h:45
UART0_RX_FIFO_SIZE
#define UART0_RX_FIFO_SIZE
Define the RX fifo size for this uart.
Definition: uart0.h:37
Uart0Ctrl
tUartCtrl Uart0Ctrl
Definition: uart0.c:69
tUartFlags::moduleError
unsigned char moduleError
Definition: uart_common.h:62
tUartFifo::data
char * data
Definition: uart_common.h:46
tUartFlags::stringReceived
unsigned char stringReceived
Definition: uart_common.h:61
UART0ClearAllErrorFlag
#define UART0ClearAllErrorFlag()
Clear all error flag.
Definition: uart0.h:221
tUartCtrl::flags
tUartFlags flags
Definition: uart_common.h:79
tUartCtrl::tx
tUartFifo tx
Definition: uart_common.h:78
UART0DisableTransmitter
#define UART0DisableTransmitter()
Enable / Disable transmitter and its interrupt.
Definition: uart0.h:201
tUartFifo::lastIn
unsigned short lastIn
Definition: uart_common.h:47
tUartFifo::nbrChar
unsigned short nbrChar
Definition: uart_common.h:49
tUartFlags::charReceived
unsigned char charReceived
Definition: uart_common.h:60

void Uart0Init | ( | unsigned long | baudrate | ) |

Initialize this uart.

Parameters

  [in] | baudrate | (unsigned long) : baudrate of this uart.

Definition at line 150 of file uart0.c.

References tUartFlags::all, tUartFifo::data, tUartCtrl::flags, IPR_NVIC_UART0_MASK, IPR_NVIC_UART0_REG, IPR_NVIC_UART0_SHIFT, tUartFifo::lastIn, tUartFifo::lastOut, tUartFifo::nbrChar, NVIC_UART0_MASK, NVIC_UART0_REG, tUartCtrl::rx, rxBuf, tUartCtrl::tx, txBuf, UART0_CLOCK_SOURCE, UART0_INTERRUPT_PRIORITY, UART0_NBR_STOP_BIT, UART0_RX_STANDBY_IS_LOW, UART0_SEND_MSB_FIRST, UART0_TX_STANDBY_IS_LOW, UART0_WORK_IN_WAIT_MODE, UART0ClearAllErrorFlag, UART0ClockGate(), and UART0PinMappingPeripheral().

Referenced by CR95UartInit().

150 {
151
152 /*-- initialize Uart control structure --*/
153 Uart0Ctrl.rx.data = rxBuf;
154 Uart0Ctrl.rx.lastIn =0;
155 Uart0Ctrl.rx.lastOut =0;
156 Uart0Ctrl.rx.nbrChar =0;
157 Uart0Ctrl.tx.data = txBuf;
158 Uart0Ctrl.tx.lastIn =0;
159 Uart0Ctrl.tx.lastOut =0;
160 Uart0Ctrl.tx.nbrChar =0;
161 Uart0Ctrl.flags.all = 0;
162
163 /*-- Turn on the clock of the uart module --*/
164 UART0ClockGate(1);
165
166 /*-- Make sure to disable the UART module --*/
167 UART0->C2 = 0;
168
169 /*-- Map the uart IO --*/
170 UART0PinMappingPeripheral();
171
172 /*--Configure UART registers --*/
173 unsigned long BR = (UART0_CLOCK_SOURCE / baudrate)>>4;
174 UART0->BDH = ((UART0_NBR_STOP_BIT-1)<<5) | ((BR >> 8) & 0x0000001F); //Set stop bit and baudrate
175 UART0->BDL = BR & 0x000000FF; //Set baudrate
176 #if defined(UART0_USE_ODD_PARITY) //Set parity mode and state for idle
177 UART0->C1 = (UART0_WORK_IN_WAIT_MODE<<6) | 0x03 | UART0_C1_ILT_MASK;
178 #elif defined(UART0_USE_EVEN_PARITY)
179 UART0->C1 = (UART0_WORK_IN_WAIT_MODE<<6) | 0x02 | UART0_C1_ILT_MASK;
180 #else
181 UART0->C1 = (UART0_WORK_IN_WAIT_MODE<<6) | UART0_C1_ILT_MASK;
182 #endif
183
184 UART0->S2 = (UART0_SEND_MSB_FIRST<<5) | (UART0_RX_STANDBY_IS_LOW<<4); //Set rx idle state and endian mode
185 UART0->C3 = (UART0_TX_STANDBY_IS_LOW<<4) | 0x00; //Set tx idle state and disable all error interrupt
186 UART0->C4 = 0x0F; //Set oversamplig to 4
187 UART0->C5 = UART0_C5_BOTHEDGE_MASK;
188
189 /*-- Set interrupt configuration --*/
190 UART0ClearAllErrorFlag();
191 NVIC->IP[IPR_NVIC_UART0_REG] = (NVIC->IP[IPR_NVIC_UART0_REG] & ~IPR_NVIC_UART0_MASK) | ( (UART0_INTERRUPT_PRIORITY & 0x03) << IPR_NVIC_UART0_SHIFT);
192 NVIC->ISER[NVIC_UART0_REG] |= NVIC_UART0_MASK;
193 }
UART0_SEND_MSB_FIRST
#define UART0_SEND_MSB_FIRST
Define the Endian transmission mode.
Definition: uart0.h:115
tUartFlags::all
unsigned char all
Definition: uart_common.h:67
tUartCtrl::rx
tUartFifo rx
Definition: uart_common.h:77
UART0_TX_STANDBY_IS_LOW
#define UART0_TX_STANDBY_IS_LOW
Define the TX standby state of this uart.
Definition: uart0.h:103
rxBuf
char rxBuf[UART0_RX_FIFO_SIZE]
Build RX and TX fifo used by this uart.
Definition: uart0.c:59
NVIC_UART0_MASK
#define NVIC_UART0_MASK
Define NVIC mask and shift values used by specific peripherals.
Definition: DSP_mkl16z256vlh4.h:69
UART0_RX_STANDBY_IS_LOW
#define UART0_RX_STANDBY_IS_LOW
Define the RX standby state of this uart.
Definition: uart0.h:91
tUartFifo::lastOut
unsigned short lastOut
Definition: uart_common.h:48
UART0_INTERRUPT_PRIORITY
#define UART0_INTERRUPT_PRIORITY
Define the uart interrupt priority.
Definition: uart0.h:127
Uart0Ctrl
tUartCtrl Uart0Ctrl
Definition: uart0.c:69
UART0ClockGate
void UART0ClockGate(unsigned char bool)
Functions to enable/disable the main clock of the specified uart.
Definition: DSP_mkl16z256vlh4.c:73
IPR_NVIC_UART0_SHIFT
#define IPR_NVIC_UART0_SHIFT
Define NVIC PIT register used by specific peripherals.
Definition: DSP_mkl16z256vlh4.h:121
tUartFifo::data
char * data
Definition: uart_common.h:46
UART0_CLOCK_SOURCE
#define UART0_CLOCK_SOURCE
Define clock source for peripehrals.
Definition: DSP_mkl16z256vlh4.h:311
UART0ClearAllErrorFlag
#define UART0ClearAllErrorFlag()
Clear all error flag.
Definition: uart0.h:221
tUartCtrl::flags
tUartFlags flags
Definition: uart_common.h:79
UART0_WORK_IN_WAIT_MODE
#define UART0_WORK_IN_WAIT_MODE
Define the parity .
Definition: uart0.h:79
UART0PinMappingPeripheral
void UART0PinMappingPeripheral(void)
Functions to MAP the defined IO of the uart module x in the rigth pin.
Definition: DSP_mkl16z256vlh4.c:157
IPR_NVIC_UART0_REG
#define IPR_NVIC_UART0_REG
Define NVIC PIT register used by specific peripherals.
Definition: DSP_mkl16z256vlh4.h:97
UART0_NBR_STOP_BIT
#define UART0_NBR_STOP_BIT
Define the number of stop bit used on this uart.
Definition: uart0.h:54
IPR_NVIC_UART0_MASK
#define IPR_NVIC_UART0_MASK
Define NVIC PIT register used by specific peripherals.
Definition: DSP_mkl16z256vlh4.h:122
tUartCtrl::tx
tUartFifo tx
Definition: uart_common.h:78
tUartFifo::lastIn
unsigned short lastIn
Definition: uart_common.h:47
tUartFifo::nbrChar
unsigned short nbrChar
Definition: uart_common.h:49
NVIC_UART0_REG
#define NVIC_UART0_REG
Define NVIC register used by specific peripherals.
Definition: DSP_mkl16z256vlh4.h:44
txBuf
char txBuf[UART0_TX_FIFO_SIZE]
Build RX and TX fifo used by this uart.
Definition: uart0.c:60

Here is the call graph for this function:

Here is the caller graph for this function:

char Uart0ReadChar | ( | void |  | ) |

Definition at line 249 of file uart0.c.

References tUartFlags::bits, tUartFlags::charReceived, tUartFifo::data, tUartCtrl::flags, tUartFifo::lastOut, tUartFifo::nbrChar, tUartCtrl::rx, tUartFlags::stringReceived, and UART0_RX_FIFO_SIZE.

Referenced by CR95UartReadChar().

249 {
250 if(Uart0Ctrl.rx.nbrChar != 0) {
251
252 /*-- Compute next to out --*/
253 if(Uart0Ctrl.rx.lastOut < UART0_RX_FIFO_SIZE-1) {
254 Uart0Ctrl.rx.lastOut++;
255 } else {
256 Uart0Ctrl.rx.lastOut = 0;
257 }
258
259 char read = Uart0Ctrl.rx.data[Uart0Ctrl.rx.lastOut];
260
261 /*-- ENTER CRITICAL SECTION --*/
262 UART0->C2 &= ~UART_C2_RIE_MASK; //Disable interrupt for critical section
263 Uart0Ctrl.rx.nbrChar--;
264 if(Uart0Ctrl.rx.nbrChar == 0) {
265 Uart0Ctrl.flags.bits.charReceived = 0;
266 Uart0Ctrl.flags.bits.stringReceived = 0;
267 }
268 UART0->C2 |= UART_C2_RIE_MASK; //Re-enable interrupt
269 /*-- EXIT CRITICAL SECTION --*/
270
271 return read;
272 } else {
273 return '0';
274 }
275 }
tUartCtrl::rx
tUartFifo rx
Definition: uart_common.h:77
tUartFifo::lastOut
unsigned short lastOut
Definition: uart_common.h:48
tUartFlags::bits
struct tUartFlags::@21 bits
UART0_RX_FIFO_SIZE
#define UART0_RX_FIFO_SIZE
Define the RX fifo size for this uart.
Definition: uart0.h:37
Uart0Ctrl
tUartCtrl Uart0Ctrl
Definition: uart0.c:69
tUartFifo::data
char * data
Definition: uart_common.h:46
tUartFlags::stringReceived
unsigned char stringReceived
Definition: uart_common.h:61
tUartCtrl::flags
tUartFlags flags
Definition: uart_common.h:79
tUartFifo::nbrChar
unsigned short nbrChar
Definition: uart_common.h:49
tUartFlags::charReceived
unsigned char charReceived
Definition: uart_common.h:60

Here is the caller graph for this function:

void Uart0SendData | ( | char * | data,
|  | unsigned short | dataLength
| ) |  |

send data other uart.

Parameters

  [in] | data | (char*) : Pointer on the data to send.
  [in] | dataLength | (unsigned short) : length of the data to send.

Definition at line 196 of file uart0.c.

References tUartFlags::bits, tUartFifo::data, tUartCtrl::flags, tUartFifo::lastIn, tUartFifo::nbrChar, tUartCtrl::tx, tUartFlags::txOverrun, UART0_TX_FIFO_SIZE, UART0DisableTransmitterInterrupt, UART0EnableTransmitter, and UART0EnableTransmitterInterrupt.

Referenced by CR95UartSendData().

196 {
197 unsigned char error = 0;
198
199 while(dataLength != 0 && error == 0) { //While data is not completely transfert into the fifo and no error occurs
200 if(Uart0Ctrl.tx.nbrChar < UART0_TX_FIFO_SIZE) { //If the FIFO is not full
201
202 //Insert char in the buffer
203 if(Uart0Ctrl.tx.lastIn < UART0_TX_FIFO_SIZE-1) {
204 Uart0Ctrl.tx.lastIn++;
205 } else {
206 Uart0Ctrl.tx.lastIn = 0;
207 }
208
209 Uart0Ctrl.tx.data[Uart0Ctrl.tx.lastIn] = *data;
210 data++;
211 UART0DisableTransmitterInterrupt(); //CRITICAL SECTION
212 Uart0Ctrl.tx.nbrChar++; //Update nbr char in FIFO
213 UART0EnableTransmitterInterrupt(); //END OF CRITICAL SECTION
214 UART0EnableTransmitter();
215 dataLength--;
216 } else { //FIFO is full -> raise error flag.
217 error = 1;
218 Uart0Ctrl.flags.bits.txOverrun = 1;
219 }
220 }
221 }
UART0DisableTransmitterInterrupt
#define UART0DisableTransmitterInterrupt()
Enable / Disable transmitter and its interrupt.
Definition: uart0.h:203
tUartFlags::txOverrun
unsigned char txOverrun
Definition: uart_common.h:64
tUartFlags::bits
struct tUartFlags::@21 bits
UART0_TX_FIFO_SIZE
#define UART0_TX_FIFO_SIZE
Define the TX fifo size for this uart.
Definition: uart0.h:45
UART0EnableTransmitter
#define UART0EnableTransmitter()
Enable / Disable transmitter and its interrupt.
Definition: uart0.h:200
Uart0Ctrl
tUartCtrl Uart0Ctrl
Definition: uart0.c:69
tUartFifo::data
char * data
Definition: uart_common.h:46
tUartCtrl::flags
tUartFlags flags
Definition: uart_common.h:79
UART0EnableTransmitterInterrupt
#define UART0EnableTransmitterInterrupt()
Enable / Disable transmitter and its interrupt.
Definition: uart0.h:202
tUartCtrl::tx
tUartFifo tx
Definition: uart_common.h:78
tUartFifo::lastIn
unsigned short lastIn
Definition: uart_common.h:47
tUartFifo::nbrChar
unsigned short nbrChar
Definition: uart_common.h:49

Here is the caller graph for this function:

void Uart0SendString | ( | char * | string | ) |

send ASCII string other uart.

Parameters

  [in] | data | (char*) : Pointer on the string to send.

The string must be end with '\0'.

Definition at line 224 of file uart0.c.

References tUartFlags::bits, tUartFifo::data, tUartCtrl::flags, tUartFifo::lastIn, tUartFifo::nbrChar, tUartCtrl::tx, tUartFlags::txOverrun, UART0_TX_FIFO_SIZE, UART0DisableTransmitterInterrupt, and UART0EnableTransmitterInterrupt.

Referenced by CR95UartSendString().

224 {
225 unsigned char error = 0;
226
227 while( (*string) != '\0' && error == 0) { //No error and string are not completely copied in the FIFO
228 if(Uart0Ctrl.tx.nbrChar < UART0_TX_FIFO_SIZE) { //FIFO is not full
229
230 //Insert char into fifo
231 if(Uart0Ctrl.tx.lastIn < UART0_TX_FIFO_SIZE-1) {
232 Uart0Ctrl.tx.lastIn++;
233 } else {
234 Uart0Ctrl.tx.lastIn = 0;
235 }
236
237 Uart0Ctrl.tx.data[Uart0Ctrl.tx.lastIn] = *string;
238 string++;
239 UART0DisableTransmitterInterrupt(); //CRITICAL SECTION
240 Uart0Ctrl.tx.nbrChar++; //Update nbr char in FIFO
241 UART0EnableTransmitterInterrupt(); //END OF CRITICAL SECTION
242 } else { //FIFO is full -> raise flag
243 error = 1;
244 Uart0Ctrl.flags.bits.txOverrun = 1;
245 }
246 }
247 }
UART0DisableTransmitterInterrupt
#define UART0DisableTransmitterInterrupt()
Enable / Disable transmitter and its interrupt.
Definition: uart0.h:203
tUartFlags::txOverrun
unsigned char txOverrun
Definition: uart_common.h:64
tUartFlags::bits
struct tUartFlags::@21 bits
UART0_TX_FIFO_SIZE
#define UART0_TX_FIFO_SIZE
Define the TX fifo size for this uart.
Definition: uart0.h:45
Uart0Ctrl
tUartCtrl Uart0Ctrl
Definition: uart0.c:69
tUartFifo::data
char * data
Definition: uart_common.h:46
tUartCtrl::flags
tUartFlags flags
Definition: uart_common.h:79
UART0EnableTransmitterInterrupt
#define UART0EnableTransmitterInterrupt()
Enable / Disable transmitter and its interrupt.
Definition: uart0.h:202
tUartCtrl::tx
tUartFifo tx
Definition: uart_common.h:78
tUartFifo::lastIn
unsigned short lastIn
Definition: uart_common.h:47
tUartFifo::nbrChar
unsigned short nbrChar
Definition: uart_common.h:49

Here is the caller graph for this function:

## Variable Documentation

char rxBuf[UART0_RX_FIFO_SIZE]

Build RX and TX fifo used by this uart.

Definition at line 59 of file uart0.c.

Referenced by Uart0Init().

char txBuf[UART0_TX_FIFO_SIZE]

Build RX and TX fifo used by this uart.

Definition at line 60 of file uart0.c.

Referenced by Uart0Init().

tUartCtrl Uart0Ctrl

Definition at line 69 of file uart0.c.

Referenced by CR95UartGetUartStructCtrl(), and CR95UartResetReceiver().