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

Headerfile for uart0 peripheral driver. More...

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

Go to the source code of this file.

## Macros

#define | UART0_RX_FIFO_SIZE (64)
| Define the RX fifo size for this uart. More...
#define | UART0_TX_FIFO_SIZE (64)
| Define the TX fifo size for this uart. More...
#define | UART0_NBR_STOP_BIT (2)
| Define the number of stop bit used on this uart. More...
#define | UART0_WORK_IN_WAIT_MODE (0)
| Define the parity . More...
#define | UART0_RX_STANDBY_IS_LOW (0)
| Define the RX standby state of this uart. More...
#define | UART0_TX_STANDBY_IS_LOW (0)
| Define the TX standby state of this uart. More...
#define | UART0_SEND_MSB_FIRST (0)
| Define the Endian transmission mode. More...
#define | UART0_INTERRUPT_PRIORITY (3)
| Define the uart interrupt priority. More...
#define | UART0ClearAllErrorFlag() (UART0->S1 = 0xFF)
| Clear all error flag. More...
#define | UART0EnableTransmitter() (UART0->C2 |= 0x08)
| Enable / Disable transmitter and its interrupt. More...
#define | UART0DisableTransmitter() (UART0->C2 &= ~0x08)
| Enable / Disable transmitter and its interrupt. More...
#define | UART0EnableTransmitterInterrupt() (UART0->C2 |= 0x80)
| Enable / Disable transmitter and its interrupt. More...
#define | UART0DisableTransmitterInterrupt() (UART0->C2 &= ~0x80)
| Enable / Disable transmitter and its interrupt. More...
#define | UART0EnableReceiver() (UART0->C2 |= 0x24)
| Enable / Disable receiver and its interrupt. More...
#define | UART0DisableReceiver() (UART0->C2 &= ~0x24)
| Enable / Disable receiver and its interrupt. More...

## Functions

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

## Detailed Description

Headerfile for uart0 peripheral driver.

Author
  Thibaut LALUQUE 3D-Ouest

Date
  26/09/2014

Version
  1.0

supported device : MKL16Z256VLH4 All Right Reserved.

Definition in file uart0.h.

## Macro Definition Documentation

#define UART0_INTERRUPT_PRIORITY (3)

Define the uart interrupt priority.

0 is the greatest priority, 3 is the lowest.

Definition at line 127 of file uart0.h.

Referenced by Uart0Init().

#define UART0_NBR_STOP_BIT (2)

Define the number of stop bit used on this uart.

Definition at line 54 of file uart0.h.

Referenced by Uart0Init().

#define UART0_RX_FIFO_SIZE (64)

Define the RX fifo size for this uart.

Definition at line 37 of file uart0.h.

Referenced by UART0_IRQHandler(), and Uart0ReadChar().

#define UART0_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 uart0.h.

Referenced by Uart0Init().

#define UART0_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 uart0.h.

Referenced by Uart0Init().

#define UART0_TX_FIFO_SIZE (64)

Define the TX fifo size for this uart.

Definition at line 45 of file uart0.h.

Referenced by UART0_IRQHandler(), Uart0SendData(), and Uart0SendString().

#define UART0_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 uart0.h.

Referenced by Uart0Init().

#define UART0_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 uart0.h.

Referenced by Uart0Init().

#define UART0ClearAllErrorFlag | ( |  | ) | (UART0->S1 = 0xFF)

Clear all error flag.

Definition at line 221 of file uart0.h.

Referenced by CR95UartClearAllErrorFlag(), UART0_IRQHandler(), and Uart0Init().

#define UART0DisableReceiver | ( |  | ) | (UART0->C2 &= ~0x24)

Enable / Disable receiver and its interrupt.

Definition at line 212 of file uart0.h.

Referenced by CR95UartDisableReceiver().

#define UART0DisableTransmitter | ( |  | ) | (UART0->C2 &= ~0x08)

Enable / Disable transmitter and its interrupt.

Definition at line 201 of file uart0.h.

Referenced by CR95UartDisableTransmitter(), and UART0_IRQHandler().

#define UART0DisableTransmitterInterrupt | ( |  | ) | (UART0->C2 &= ~0x80)

Enable / Disable transmitter and its interrupt.

Definition at line 203 of file uart0.h.

Referenced by CR95UartDisableTransmitter(), UART0_IRQHandler(), Uart0SendData(), and Uart0SendString().

#define UART0EnableReceiver | ( |  | ) | (UART0->C2 |= 0x24)

Enable / Disable receiver and its interrupt.

Definition at line 211 of file uart0.h.

Referenced by CR95UartEnableReceiver().

#define UART0EnableTransmitter | ( |  | ) | (UART0->C2 |= 0x08)

Enable / Disable transmitter and its interrupt.

Definition at line 200 of file uart0.h.

Referenced by CR95UartEnableTransmitter(), and Uart0SendData().

#define UART0EnableTransmitterInterrupt | ( |  | ) | (UART0->C2 |= 0x80)

Enable / Disable transmitter and its interrupt.

Definition at line 202 of file uart0.h.

Referenced by CR95UartEnableTransmitter(), Uart0SendData(), and Uart0SendString().

## Function Documentation

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

tUartCtrl Uart0Ctrl

Definition at line 69 of file uart0.c.

Referenced by CR95UartGetUartStructCtrl(), and CR95UartResetReceiver().