embedded-software
reusable software modules for embedded systems
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
uart.h File Reference
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include "project_settings.h"
#include "buffer.h"
#include "hal_uart.h"
Include dependency graph for uart.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

enum  uart_errors {
  BREAK_ERROR = 0, PARITY_ERROR, FRAMING_ERROR, OVERRUN_ERROR,
  NUM_UART_ERRORS
}
 
void UART_Init (uint8_t channel)
 
void UART_ReconfigureBaud (uint8_t channel, uint32_t baud)
 Change baud rate. More...
 
void UART_WriteByte (uint8_t channel, uint8_t c)
 Write a single byte to UART. More...
 
int8_t UART_Write (uint8_t channel, uint8_t *data, uint16_t length)
 Write chunk of data to UART tx buffer. More...
 
uint8_t UART_IsTransmitting (uint8_t channel)
 Returns 0 or num bytes to indicate if the UART is transmitting. More...
 
void UART_Tick (void)
 Main UART routine which calls subscribers and handles errors. More...
 
int8_t UART_RegisterReceiver (uint8_t channel, void(*fn)(uint8_t))
 register a character receiver for a UART channel to receive data More...
 
void UART_UnregisterReceiver (uint8_t channel, void(*fn)(uint8_t))
 Unregister a receiver added by UART_RegisterReceiver() More...
 
void UART_RegisterErrorCallback (uint8_t channel, void(*callback)(enum uart_errors))
 Register callback function to handle UART errors. More...
 
void UART_RegisterTxOverwriteCallback (uint8_t channel, void(*overwriteCallback)(void))
 Register callback function to be called when/if the UART Tx buffer overflows. More...
 
void UART_RegisterRxOverwriteCallback (uint8_t channel, void(*overwriteCallback)(void))
 Register callback function to be called when/if the UART Rx buffer overflows. More...
 
void hal_UART_Init (uint8_t channel, uint32_t baud)
 initialize UART channel at specified baud rate More...
 
void hal_UART_ReconfigureBaud (uint8_t channel, uint32_t baud)
 change the baud rate for the UART channel More...
 
void UART_Tx_Handler (uint8_t channel)
 UART transmit interrupt handler. More...
 
void UART_Rx_Handler (uint8_t channel)
 UART receive interrupt handler. More...
 
void UART_Error_Handler (uint8_t channel, enum uart_errors error)
 UART error (interrupt) handler. More...
 
void hal_UART_Enable (uint8_t channel)
 Enable UART peripheral for given channel. More...
 
void hal_UART_Disable (uint8_t channel)
 Disable UART peripheral for given channel. More...
 
uint8_t hal_UART_SpaceAvailable (uint8_t channel)
 return 0 if no space is available in the hardware UART tx buffer More...
 
uint8_t hal_UART_DataAvailable (uint8_t channel)
 return 0 if no data is available in the hardware UART rx buffer More...
 
void hal_UART_ClearRxIF (uint8_t channel)
 clear the UART rx interrupt flags More...
 
void hal_UART_ClearTxIF (uint8_t channel)
 clear the UART tx interrupt flags More...
 
void hal_UART_EnableRxInterrupt (uint8_t channel)
 enable the UART rx interrupt More...
 
void hal_UART_EnableTxInterrupt (uint8_t channel)
 enable the UART tx interrupt More...
 
void hal_UART_DisableRxInterrupt (uint8_t channel)
 disable the UART rx interrupt More...
 
void hal_UART_DisableTxInterrupt (uint8_t channel)
 disable the UART tx interrupt More...
 
uint8_t hal_UART_RxInterruptEnabled (uint8_t channel)
 Is UART rx interrupt enabled. More...
 
uint8_t hal_UART_TxInterruptEnabled (uint8_t channel)
 Is UART tx interrupt enabled. More...
 
uint8_t hal_UART_RxByte (uint8_t channel)
 Get character from UART hardware rx register. More...
 
void hal_UART_TxByte (uint8_t channel, uint8_t data)
 send character to UART hardware tx register More...
 

Detailed Description

Created on: Mar 12, 2014

Author
: Michael Muhlbaier Updated on: Feb 7, 2015
Anthony Merlino

Use this module for basic UART communications. Completely interrupt driven and buffered.

Normal usage is:

Enumeration Type Documentation

NOTE: This enumeration has to occur BEFORE the include for the hal_uart.h because hal_uart.h includes this uart.h file so that it can see this error enumeration.

Enumerator
BREAK_ERROR 
PARITY_ERROR 
FRAMING_ERROR 
OVERRUN_ERROR 
NUM_UART_ERRORS 

Function Documentation

void hal_UART_ClearRxIF ( uint8_t  channel)

clear the UART rx interrupt flags

Here is the caller graph for this function:

void hal_UART_ClearTxIF ( uint8_t  channel)

clear the UART tx interrupt flags

Here is the caller graph for this function:

uint8_t hal_UART_DataAvailable ( uint8_t  channel)

return 0 if no data is available in the hardware UART rx buffer

REQUIRED

void hal_UART_Disable ( uint8_t  channel)

Disable UART peripheral for given channel.

Here is the caller graph for this function:

void hal_UART_DisableRxInterrupt ( uint8_t  channel)

disable the UART rx interrupt

void hal_UART_DisableTxInterrupt ( uint8_t  channel)

disable the UART tx interrupt

REQUIRED

void hal_UART_Enable ( uint8_t  channel)

Enable UART peripheral for given channel.

Here is the caller graph for this function:

void hal_UART_EnableRxInterrupt ( uint8_t  channel)

enable the UART rx interrupt

Here is the caller graph for this function:

void hal_UART_EnableTxInterrupt ( uint8_t  channel)

enable the UART tx interrupt

REQUIRED

Here is the caller graph for this function:

void hal_UART_Init ( uint8_t  channel,
uint32_t  baud 
)

initialize UART channel at specified baud rate

REQUIRED

This function should:

  • Initialize the UART module for the channel specified
  • Map UART pins if needed
  • Configure UART interrupts
Parameters
channel- UART channel to initialize
baud- integer baud rate (bits/s) to configure UART for

there is some issue with the PIO_WPMR write protect register that is preventing the USART from being configured when this happens. For now the workaround is to set-up the USART first. Commenting the following line of code out will leave the write protect in a disabled state. This is how I (JE?) initially discovered the workaround.

there is some issue with the PIO_WPMR write protect register that is preventing the USART from being configured when this happens. For now the workaround is to set-up the USART first. Commenting the following line of code out will leave the write protect in a disabled state. This is how I (JE?) initially discovered the workaround.

there is some issue with the PIO_WPMR write protect register that is preventing the USART from being configured when this happens. For now the workaround is to set-up the USART first. Commenting the following line of code out will leave the write protect in a disabled state. This is how I (JE?) initially discovered the workaround.

Todo:
add logic to determine which clock to use

there is some issue with the PIO_WPMR write protect register that is preventing the USART from being configured when this happens. For now the workaround is to set-up the USART first. Commenting the following line of code out will leave the write protect in a disabled state. This is how I (JE?) initially discovered the workaround.

there is some issue with the PIO_WPMR write protect register that is preventing the USART from being configured when this happens. For now the workaround is to set-up the USART first. Commenting the following line of code out will leave the write protect in a disabled state. This is how I (JE?) initially discovered the workaround.

there is some issue with the PIO_WPMR write protect register that is preventing the USART from being configured when this happens. For now the workaround is to set-up the USART first. Commenting the following line of code out will leave the write protect in a disabled state. This is how I (JE?) initially discovered the workaround.

Todo:
add logic to determine which clock to use

Here is the call graph for this function:

Here is the caller graph for this function:

void hal_UART_ReconfigureBaud ( uint8_t  channel,
uint32_t  baud 
)

change the baud rate for the UART channel

Parameters
channel- UART channel
baud- integer baud rate (bits/s) to configure UART for
uint8_t hal_UART_RxByte ( uint8_t  channel)

Get character from UART hardware rx register.

REQUIRED

Here is the caller graph for this function:

uint8_t hal_UART_RxInterruptEnabled ( uint8_t  channel)

Is UART rx interrupt enabled.

uint8_t hal_UART_SpaceAvailable ( uint8_t  channel)

return 0 if no space is available in the hardware UART tx buffer

REQUIRED

void hal_UART_TxByte ( uint8_t  channel,
uint8_t  data 
)

send character to UART hardware tx register

REQUIRED

uint8_t hal_UART_TxInterruptEnabled ( uint8_t  channel)

Is UART tx interrupt enabled.

REQUIRED

void UART_Error_Handler ( uint8_t  channel,
enum uart_errors  error 
)

UART error (interrupt) handler.

This function is implemented by the UART module (uart.c) but should be called by the error ISR for the given UART channel or when an error condition is detected by the HAL.

Parameters
channel- channel the interrupt occurred on

Here is the caller graph for this function:

void UART_Init ( uint8_t  channel)

Initialize UART module

Example usage:

UART_Init(UART0_CHANNEL);
Parameters
channel- The channel of UART to be used. Macros for these should be defined in the HAL of the specific device.

Here is the call graph for this function:

Here is the caller graph for this function:

uint8_t UART_IsTransmitting ( uint8_t  channel)

Returns 0 or num bytes to indicate if the UART is transmitting.

Parameters
channel- UART channel to check
Returns
0 if not transmitting, number of bytes in buffer otherwise

Here is the call graph for this function:

Here is the caller graph for this function:

void UART_ReconfigureBaud ( uint8_t  channel,
uint32_t  baud 
)

Change baud rate.

Parameters
channel- The channel of the UART to have its baud rate changed.
baud- The new baud rate.

Here is the call graph for this function:

Here is the caller graph for this function:

void UART_RegisterErrorCallback ( uint8_t  channel,
void(*)(enum uart_errors callback 
)

Register callback function to handle UART errors.

Will call the callback function when a UART error occurs. The callback should have no return value and a enum uart_errors input.

Parameters
channel- UART channel to tie the error callback function to
callback- function pointer to callback function: void CallbackName(enum uart_errors error)
int8_t UART_RegisterReceiver ( uint8_t  channel,
void(*)(uint8_t)  fn 
)

register a character receiver for a UART channel to receive data

The receiver function registered will be called by UART_Tick whenever a byte is received. The receiver function should have no return value and a char input.

Parameters
channel- UART channel to register receiver with
fn- function pointer to receiver function: void ReceiverName(char c)
Returns
-1 if channel is out of range, -2 if receiver list is full, 0 if successful

Here is the caller graph for this function:

void UART_RegisterRxOverwriteCallback ( uint8_t  channel,
void(*)(void)  overwriteCallback 
)

Register callback function to be called when/if the UART Rx buffer overflows.

Will be called when trying to transmit data and the Rx buffer is full. The callback should have no return value and no input.

Parameters
channel- UART channel to tie the Rx overwrite callback to
overwriteCallback- function pointer to callback function: void CallbackName(void)
void UART_RegisterTxOverwriteCallback ( uint8_t  channel,
void(*)(void)  overwriteCallback 
)

Register callback function to be called when/if the UART Tx buffer overflows.

Will be called when trying to transmit data and the Tx buffer is full. The callback should have no return value and no input.

Parameters
channel- UART channel to tie the tx overwrite callback to
overwriteCallback- function pointer to callback function: void CallbackName(void)
void UART_Rx_Handler ( uint8_t  channel)

UART receive interrupt handler.

REQUIRED to be called by HAL

This function is implemented by the UART module (uart.c) but should be called by the ISR for the given UART channel.

Parameters
channel- channel the interrupt occurred on

Here is the call graph for this function:

void UART_Tick ( void  )

Main UART routine which calls subscribers and handles errors.

If the task management module is used and USE_MODULE_TASK is defined then this function will automatically run via the task management system. Otherwise the user should call UART_Tick() frequently to handle incoming data.

Here is the call graph for this function:

Here is the caller graph for this function:

void UART_Tx_Handler ( uint8_t  channel)

UART transmit interrupt handler.

REQUIRED to be called by HAL

This function is implemented by the UART module (uart.c) but should be called by the ISR for the given UART channel.

Parameters
channel- channel the interrupt occurred on

Here is the call graph for this function:

void UART_UnregisterReceiver ( uint8_t  channel,
void(*)(uint8_t)  fn 
)

Unregister a receiver added by UART_RegisterReceiver()

Parameters
channel- UART channel to unregister receiver on
fn- function pointer to receiver function: void ReceiverName(char c)

Here is the caller graph for this function:

int8_t UART_Write ( uint8_t  channel,
uint8_t *  data,
uint16_t  length 
)

Write chunk of data to UART tx buffer.

Parameters
channel- the UART channel to send on
data- a pointer to the data to send
length- the amount of data to send
Returns
0 = success, -1 = not enough room in buffer, -2 = Invalid channel

Here is the call graph for this function:

Here is the caller graph for this function:

void UART_WriteByte ( uint8_t  channel,
uint8_t  c 
)

Write a single byte to UART.

Transmit a single byte over the UART channel

Parameters
channel- The UART channel to send the byte over
c- the byte to send

Here is the call graph for this function:

Here is the caller graph for this function: