SRD Standelone programmation box: uart2.h 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
Macros | Functions | Variables
uart2.h File Reference

Headerfile for uart2 peripheral driver. More...

`#include "uart_common.h"`
Include dependency graph for uart2.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Macros

#define | UART2_RX_FIFO_SIZE (64)
| Define the RX fifo size for this uart. More...
#define | UART2_TX_FIFO_SIZE (64)
| Define the TX fifo size for this uart. More...
#define | UART2_NBR_STOP_BIT (2)
| Define the number of stop bit used on this uart. More...
#define | UART2_WORK_IN_WAIT_MODE (0)
| Define the parity . More...
#define | UART2_RX_STANDBY_IS_LOW (0)
| Define the RX standby state of this uart. More...
#define | UART2_TX_STANDBY_IS_LOW (0)
| Define the TX standby state of this uart. More...
#define | UART2_SEND_MSB_FIRST (0)
| Define the Endian transmission mode. More...
#define | UART2_INTERRUPT_PRIORITY (3)
| Define the uart interrupt priority. More...
#define | UART2ClearAllErrorFlag() {UART2->S1;UART2->D;}
| Clear all error flag. More...
#define | UART2EnableTransmitter() (UART2->C2 |= 0x88)
| Enable / Disable transmitter and its interrupt. More...
#define | UART2DisableTransmitter() (UART2->C2 &= 0x77)
| Enable / Disable transmitter and its interrupt. More...
#define | UART2EnableReceiver() (UART2->C2 |= 0x24)
| Enable / Disable receiver and its interrupt. More...
#define | UART2DisableReceiver() (UART2->C2 &= 0xDB)
| Enable / Disable receiver and its interrupt. More...

## Functions

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

## Detailed Description

Headerfile for uart2 peripheral driver.

Author
  Thibaut LALUQUE 3D-Ouest

Date
  26/09/2014

Version
  1.0

supported device : MKL16Z256VLH4 All Right Reserved.

Definition in file uart2.h.

## Macro Definition Documentation

#define UART2_INTERRUPT_PRIORITY (3)

Define the uart interrupt priority.

0 is the greatest priority, 3 is the lowest.

Definition at line 127 of file uart2.h.

Referenced by Uart2Init().

#define UART2_NBR_STOP_BIT (2)

Define the number of stop bit used on this uart.

Definition at line 54 of file uart2.h.

Referenced by Uart2Init().

#define UART2_RX_FIFO_SIZE (64)

Define the RX fifo size for this uart.

Definition at line 37 of file uart2.h.

Referenced by UART2_IRQHandler(), and Uart2ReadChar().

#define UART2_RX_STANDBY_IS_LOW (0)

Define the RX standby state of this uart.

Set 1 if the RX standby state is the low level of line, else set to 0.

Definition at line 91 of file uart2.h.

Referenced by Uart2Init().

#define UART2_SEND_MSB_FIRST (0)

Define the Endian transmission mode.

0 allow to send LSB first, set to 1 allow to send MSB first.

Definition at line 115 of file uart2.h.

Referenced by Uart2Init().

#define UART2_TX_FIFO_SIZE (64)

Define the TX fifo size for this uart.

Definition at line 45 of file uart2.h.

Referenced by UART2_IRQHandler(), Uart2SendData(), and Uart2SendString().

#define UART2_TX_STANDBY_IS_LOW (0)

Define the TX standby state of this uart.

Set 1 if the TX standby state is the low level of line, else set to 0.

Definition at line 103 of file uart2.h.

Referenced by Uart2Init().

#define UART2_WORK_IN_WAIT_MODE (0)

Define the parity .

Define if uart work in idle mode or not.

Set to 1 to specify that this uart work in Idle mode, set to 0 to stop this uart in idle mode.

Definition at line 79 of file uart2.h.

Referenced by Uart2Init().

#define UART2ClearAllErrorFlag | ( |  | ) | {UART2->S1;UART2->D;}

Clear all error flag.

Definition at line 219 of file uart2.h.

Referenced by CR95UartClearAllErrorFlag(), UART2_IRQHandler(), and Uart2Init().

#define UART2DisableReceiver | ( |  | ) | (UART2->C2 &= 0xDB)

Enable / Disable receiver and its interrupt.

Definition at line 210 of file uart2.h.

Referenced by CR95UartDisableReceiver().

#define UART2DisableTransmitter | ( |  | ) | (UART2->C2 &= 0x77)

Enable / Disable transmitter and its interrupt.

Definition at line 201 of file uart2.h.

Referenced by CR95UartDisableTransmitter(), UART2_IRQHandler(), Uart2SendData(), and Uart2SendString().

#define UART2EnableReceiver | ( |  | ) | (UART2->C2 |= 0x24)

Enable / Disable receiver and its interrupt.

Definition at line 209 of file uart2.h.

Referenced by CR95UartEnableReceiver().

#define UART2EnableTransmitter | ( |  | ) | (UART2->C2 |= 0x88)

Enable / Disable transmitter and its interrupt.

Definition at line 200 of file uart2.h.

Referenced by CR95UartEnableTransmitter(), Uart2SendData(), and Uart2SendString().

## Function Documentation

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

tUartCtrl Uart2Ctrl

Definition at line 69 of file uart2.c.

Referenced by CR95UartGetUartStructCtrl(), and CR95UartResetReceiver().