SRD Standelone programmation box: uart2.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
uart2.c File Reference

Source file for UART2 driver. More...

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

Go to the source code of this file.

## Functions

void | UART2_IRQHandler (void)
| Interrupt handler for this uart. More...
void | Uart2Init (unsigned long baudrate)
| Initialize this uart. More...
void | Uart2SendData (char *data, unsigned short dataLength)
| send data other uart. More...
void | Uart2SendString (char *string)
| send ASCII string other uart. More...
char | Uart2ReadChar (void)

## Variables

tUartCtrl | Uart2Ctrl
char | rxBuf [UART2_RX_FIFO_SIZE]
| Build RX and TX fifo used by this uart. More...
char | txBuf [UART2_TX_FIFO_SIZE]
| Build RX and TX fifo used by this uart. More...

## Detailed Description

Source file for UART2 driver.

Author
  Thibaut LALUQUE 3D-Ouest

Date
  26/09/2014

Version
  1.0

supported device : MKL16Z256VLH4

All Right Reserved.

Definition in file uart2.c.

## Function Documentation

void UART2_IRQHandler | ( | void |  | ) |

Interrupt handler for this uart.

Definition at line 84 of file uart2.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, UART2_RX_FIFO_SIZE, UART2_TX_FIFO_SIZE, UART2ClearAllErrorFlag, and UART2DisableTransmitter.

84 {
85
86 /*=== char receive ===*/
87 if(UART2->S1 & 0x20) {
88
89 char received = UART2_D;
90
91 #ifdef UART2_STRING_AUTO_REMOVE
92 if(received != UART2_STRING_AUTO_REMOVE) { //char will not be auto removed
93 #else
94 {
95 #endif
96
97 if(Uart2Ctrl.rx.nbrChar < UART2_RX_FIFO_SIZE) { //If FIFO is not full add new char to it
98 if(Uart2Ctrl.rx.lastIn < UART2_RX_FIFO_SIZE-1) {
99 Uart2Ctrl.rx.lastIn++;
100 } else {
101 Uart2Ctrl.rx.lastIn = 0;
102 }
103
104 Uart2Ctrl.rx.data[Uart2Ctrl.rx.lastIn] = received;
105 Uart2Ctrl.rx.nbrChar++;
106 Uart2Ctrl.flags.bits.charReceived = 1;
107 #ifdef UART2_STRING_ENDCHAR
108 if(received == UART2_STRING_ENDCHAR) { //Check if it's a end of frame
109 Uart2Ctrl.flags.bits.stringReceived = 1;
110 }
111 #endif
112 } else {
113 Uart2Ctrl.flags.bits.rxOverrun = 1; //FIFO is full -> raise error flag
114 }
115 }
116 }
117
118 /*=== Transmit data ===*/
119 if(UART2->S1 & 0x80) {
120 if(Uart2Ctrl.tx.nbrChar != 0) { //If the FIFO s not empty -> send next char
121 if(Uart2Ctrl.tx.lastOut < UART2_TX_FIFO_SIZE-1) {
122 Uart2Ctrl.tx.lastOut++;
123 } else {
124 Uart2Ctrl.tx.lastOut = 0;
125 }
126 UART2->D = Uart2Ctrl.tx.data[Uart2Ctrl.tx.lastOut];
127 Uart2Ctrl.tx.nbrChar--;
128 } else {
129 UART2DisableTransmitter(); //If FIFO is empty -> turn off Tx interrupt
130 }
131 }
132
133
134 /*=== Error module ===*/
135 if(UART2->S1 & 0x0F) {
136 Uart2Ctrl.flags.bits.moduleError = 1;
137 UART2ClearAllErrorFlag();
138 }
139
140 }
tUartCtrl::rx
tUartFifo rx
Definition: uart_common.h:77
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
UART2_TX_FIFO_SIZE
#define UART2_TX_FIFO_SIZE
Define the TX fifo size for this uart.
Definition: uart2.h:45
UART2DisableTransmitter
#define UART2DisableTransmitter()
Enable / Disable transmitter and its interrupt.
Definition: uart2.h:201
UART2ClearAllErrorFlag
#define UART2ClearAllErrorFlag()
Clear all error flag.
Definition: uart2.h:219
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
tUartCtrl::flags
tUartFlags flags
Definition: uart_common.h:79
UART2_RX_FIFO_SIZE
#define UART2_RX_FIFO_SIZE
Define the RX fifo size for this uart.
Definition: uart2.h:37
Uart2Ctrl
tUartCtrl Uart2Ctrl
Definition: uart2.c:69
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
tUartFlags::charReceived
unsigned char charReceived
Definition: uart_common.h:60

void Uart2Init | ( | unsigned long | baudrate | ) |

Initialize this uart.

Parameters

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

Definition at line 149 of file uart2.c.

References tUartFlags::all, tUartFifo::data, tUartCtrl::flags, IPR_NVIC_UART2_MASK, IPR_NVIC_UART2_REG, IPR_NVIC_UART2_SHIFT, tUartFifo::lastIn, tUartFifo::lastOut, tUartFifo::nbrChar, NVIC_UART2_MASK, NVIC_UART2_REG, tUartCtrl::rx, rxBuf, tUartCtrl::tx, txBuf, UART2_CLOCK_SOURCE, UART2_INTERRUPT_PRIORITY, UART2_NBR_STOP_BIT, UART2_RX_STANDBY_IS_LOW, UART2_SEND_MSB_FIRST, UART2_TX_STANDBY_IS_LOW, UART2_WORK_IN_WAIT_MODE, UART2ClearAllErrorFlag, UART2ClockGate(), and UART2PinMappingPeripheral().

Referenced by CR95UartInit().

149 {
150
151 /*-- initialize Uart control structure --*/
152 Uart2Ctrl.rx.data = rxBuf;
153 Uart2Ctrl.rx.lastIn =0;
154 Uart2Ctrl.rx.lastOut =0;
155 Uart2Ctrl.rx.nbrChar =0;
156 Uart2Ctrl.tx.data = txBuf;
157 Uart2Ctrl.tx.lastIn =0;
158 Uart2Ctrl.tx.lastOut =0;
159 Uart2Ctrl.tx.nbrChar =0;
160 Uart2Ctrl.flags.all = 0;
161
162 /*-- Turn on the clock of the uart module --*/
163 UART2ClockGate(1);
164
165 /*-- Make sure to disable the UART module --*/
166 UART2->C2 = 0;
167
168 /*-- Map the uart IO --*/
169 UART2PinMappingPeripheral();
170
171 /*--Configure UART registers --*/
172 unsigned long BR = (UART2_CLOCK_SOURCE / (16*baudrate) );
173 UART2->BDH = ((UART2_NBR_STOP_BIT-1)<<5) | ((BR >> 8) & 0x0000001F); //Set stop bit and baudrate
174 UART2->BDL = BR & 0x000000FF; //Set baudrate
175 #if defined(UART2_USE_ODD_PARITY) //Set parity mode and state for idle
176 UART2->C1 = (UART2_WORK_IN_WAIT_MODE<<6) | 0x03;
177 #elif defined(UART2_USE_EVEN_PARITY)
178 UART2->C1 = (UART2_WORK_IN_WAIT_MODE<<6) | 0x02;
179 #else
180 UART2->C1 = (UART2_WORK_IN_WAIT_MODE<<6);
181 #endif
182
183 UART2->S2 = (UART2_SEND_MSB_FIRST<<5) | (UART2_RX_STANDBY_IS_LOW<<4); //Set rx idle state and endian mode
184 UART2->C3 = (UART2_TX_STANDBY_IS_LOW<<4) | 0x0F; //Set tx idle state and enable all interrupt error
185 UART2->C4 = 0x00;
186
187 /*-- Set interrupt configuration --*/
188 UART2ClearAllErrorFlag();
189 NVIC->IP[IPR_NVIC_UART2_REG] = (NVIC->IP[IPR_NVIC_UART2_REG] & ~IPR_NVIC_UART2_MASK) | ( (UART2_INTERRUPT_PRIORITY & 0x03) << IPR_NVIC_UART2_SHIFT);
190 NVIC->ISER[NVIC_UART2_REG] |= NVIC_UART2_MASK;
191 }
tUartFlags::all
unsigned char all
Definition: uart_common.h:67
tUartCtrl::rx
tUartFifo rx
Definition: uart_common.h:77
rxBuf
char rxBuf[UART2_RX_FIFO_SIZE]
Build RX and TX fifo used by this uart.
Definition: uart2.c:59
NVIC_UART2_REG
#define NVIC_UART2_REG
Define NVIC register used by specific peripherals.
Definition: DSP_mkl16z256vlh4.h:46
UART2PinMappingPeripheral
void UART2PinMappingPeripheral(void)
Definition: DSP_mkl16z256vlh4.c:233
UART2_INTERRUPT_PRIORITY
#define UART2_INTERRUPT_PRIORITY
Define the uart interrupt priority.
Definition: uart2.h:127
UART2_WORK_IN_WAIT_MODE
#define UART2_WORK_IN_WAIT_MODE
Define the parity .
Definition: uart2.h:79
UART2_TX_STANDBY_IS_LOW
#define UART2_TX_STANDBY_IS_LOW
Define the TX standby state of this uart.
Definition: uart2.h:103
IPR_NVIC_UART2_REG
#define IPR_NVIC_UART2_REG
Define NVIC PIT register used by specific peripherals.
Definition: DSP_mkl16z256vlh4.h:99
tUartFifo::lastOut
unsigned short lastOut
Definition: uart_common.h:48
IPR_NVIC_UART2_MASK
#define IPR_NVIC_UART2_MASK
Define NVIC PIT register used by specific peripherals.
Definition: DSP_mkl16z256vlh4.h:126
UART2ClearAllErrorFlag
#define UART2ClearAllErrorFlag()
Clear all error flag.
Definition: uart2.h:219
UART2ClockGate
void UART2ClockGate(unsigned char bool)
Definition: DSP_mkl16z256vlh4.c:106
NVIC_UART2_MASK
#define NVIC_UART2_MASK
Define NVIC mask and shift values used by specific peripherals.
Definition: DSP_mkl16z256vlh4.h:73
tUartFifo::data
char * data
Definition: uart_common.h:46
tUartCtrl::flags
tUartFlags flags
Definition: uart_common.h:79
UART2_CLOCK_SOURCE
#define UART2_CLOCK_SOURCE
Define clock source for peripehrals.
Definition: DSP_mkl16z256vlh4.h:321
IPR_NVIC_UART2_SHIFT
#define IPR_NVIC_UART2_SHIFT
Define NVIC PIT register used by specific peripherals.
Definition: DSP_mkl16z256vlh4.h:125
Uart2Ctrl
tUartCtrl Uart2Ctrl
Definition: uart2.c:69
tUartCtrl::tx
tUartFifo tx
Definition: uart_common.h:78
txBuf
char txBuf[UART2_TX_FIFO_SIZE]
Build RX and TX fifo used by this uart.
Definition: uart2.c:60
UART2_SEND_MSB_FIRST
#define UART2_SEND_MSB_FIRST
Define the Endian transmission mode.
Definition: uart2.h:115
tUartFifo::lastIn
unsigned short lastIn
Definition: uart_common.h:47
tUartFifo::nbrChar
unsigned short nbrChar
Definition: uart_common.h:49
UART2_NBR_STOP_BIT
#define UART2_NBR_STOP_BIT
Define the number of stop bit used on this uart.
Definition: uart2.h:54
UART2_RX_STANDBY_IS_LOW
#define UART2_RX_STANDBY_IS_LOW
Define the RX standby state of this uart.
Definition: uart2.h:91

Here is the call graph for this function:

Here is the caller graph for this function:

char Uart2ReadChar | ( | void |  | ) |

Definition at line 246 of file uart2.c.

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

Referenced by CR95UartReadChar().

246 {
247 if(Uart2Ctrl.rx.nbrChar != 0) {
248
249 /*-- Compute next to out --*/
250 if(Uart2Ctrl.rx.lastOut < UART2_RX_FIFO_SIZE-1) {
251 Uart2Ctrl.rx.lastOut++;
252 } else {
253 Uart2Ctrl.rx.lastOut = 0;
254 }
255
256 char read = Uart2Ctrl.rx.data[Uart2Ctrl.rx.lastOut];
257
258 /*-- ENTER CRITICAL SECTION --*/
259 UART2->C2 &= ~UART_C2_RIE_MASK; //Disable interrupt for critical section
260 Uart2Ctrl.rx.nbrChar--;
261 if(Uart2Ctrl.rx.nbrChar == 0) {
262 Uart2Ctrl.flags.bits.charReceived = 0;
263 Uart2Ctrl.flags.bits.stringReceived = 0;
264 }
265 UART2->C2 |= UART_C2_RIE_MASK; //Re-enable interrupt
266 /*-- EXIT CRITICAL SECTION --*/
267
268 return read;
269 } else {
270 return '0';
271 }
272 }
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
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
UART2_RX_FIFO_SIZE
#define UART2_RX_FIFO_SIZE
Define the RX fifo size for this uart.
Definition: uart2.h:37
Uart2Ctrl
tUartCtrl Uart2Ctrl
Definition: uart2.c:69
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 Uart2SendData | ( | 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 194 of file uart2.c.

References tUartFlags::bits, tUartFifo::data, tUartCtrl::flags, tUartFifo::lastIn, tUartFifo::nbrChar, tUartCtrl::tx, tUartFlags::txOverrun, UART2_TX_FIFO_SIZE, UART2DisableTransmitter, and UART2EnableTransmitter.

Referenced by CR95UartSendData().

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

References tUartFlags::bits, tUartFifo::data, tUartCtrl::flags, tUartFifo::lastIn, tUartFifo::nbrChar, tUartCtrl::tx, tUartFlags::txOverrun, UART2_TX_FIFO_SIZE, UART2DisableTransmitter, and UART2EnableTransmitter.

Referenced by CR95UartSendString().

221 {
222 unsigned char error = 0;
223
224 while( (*string) != '\0' && error == 0) { //No error and string are not completely copied in the FIFO
225 if(Uart2Ctrl.tx.nbrChar < UART2_TX_FIFO_SIZE) { //FIFO is not full
226
227 //Insert char into fifo
228 if(Uart2Ctrl.tx.lastIn < UART2_TX_FIFO_SIZE-1) {
229 Uart2Ctrl.tx.lastIn++;
230 } else {
231 Uart2Ctrl.tx.lastIn = 0;
232 }
233
234 Uart2Ctrl.tx.data[Uart2Ctrl.tx.lastIn] = *string;
235 string++;
236 UART2DisableTransmitter(); //CRITICAL SECTION
237 Uart2Ctrl.tx.nbrChar++; //Update nbr char in FIFO
238 UART2EnableTransmitter(); //END OF CRITICAL SECTION
239 } else { //FIFO is full -> raise flag
240 error = 1;
241 Uart2Ctrl.flags.bits.txOverrun = 1;
242 }
243 }
244 }
tUartFlags::txOverrun
unsigned char txOverrun
Definition: uart_common.h:64
tUartFlags::bits
struct tUartFlags::@21 bits
UART2_TX_FIFO_SIZE
#define UART2_TX_FIFO_SIZE
Define the TX fifo size for this uart.
Definition: uart2.h:45
UART2DisableTransmitter
#define UART2DisableTransmitter()
Enable / Disable transmitter and its interrupt.
Definition: uart2.h:201
UART2EnableTransmitter
#define UART2EnableTransmitter()
Enable / Disable transmitter and its interrupt.
Definition: uart2.h:200
tUartFifo::data
char * data
Definition: uart_common.h:46
tUartCtrl::flags
tUartFlags flags
Definition: uart_common.h:79
Uart2Ctrl
tUartCtrl Uart2Ctrl
Definition: uart2.c:69
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[UART2_RX_FIFO_SIZE]

Build RX and TX fifo used by this uart.

Definition at line 59 of file uart2.c.

Referenced by Uart2Init().

char txBuf[UART2_TX_FIFO_SIZE]

Build RX and TX fifo used by this uart.

Definition at line 60 of file uart2.c.

Referenced by Uart2Init().

tUartCtrl Uart2Ctrl

Definition at line 69 of file uart2.c.

Referenced by CR95UartGetUartStructCtrl(), and CR95UartResetReceiver().