GlobLib
HAL and API libraries for MCUs and hardware.

This module contains low level functions for USART interaction. More...

Files

file  stm32f103cb_usart.h
 Header file for stm32f103cb USART.
 

Macros

#define USART_DEFAULT_BAUD   USART_BAUD_9600
 The default baud rate used by setup function.
 
#define USART_DEFAULT_STOP   USART_STOP_ONE
 The default stop bits used by setup function.
 
#define USART_DEFAULT_DATA   USART_DATA_EIGHT
 The default data frame used by setup function.
 
#define USART_DEFAULT_PARITY   USART_PARITY_NONE
 The default parity used by setup function.
 

Typedefs

typedef void(* v_fp_u8) (uint8_t)
 Function pointer typedef for void function with uint8_t parameter.
 

Enumerations

Functions

mcu_error USART_setup (usart_periph peripheral)
 Initialize USART port. More...
 
mcu_error USART_setBaud (usart_periph peripheral, usart_baud baud)
 Set the baud rate for the port. More...
 
v_fp_u8 USART_add_put (usart_periph peripheral)
 Return the address of the peripheral put function. More...
 
mcu_error USART_put (usart_periph peripheral, uint8_t byte)
 Send a byte on the given peripheral. More...
 
uint8_t USART_get (usart_periph peripheral)
 Get a byte on the given peripheral (blocking). More...
 
mcu_error USART_setStop (usart_periph peripheral, usart_stop stopBits)
 Set the number of stop bits for the port. More...
 
mcu_error USART_setParity (usart_periph peripheral, usart_parity parity)
 Set the parity for the port. More...
 
mcu_error USART_setData (usart_periph peripheral, usart_data data)
 Set the data frame size for the port. More...
 
mcu_error USART_setRxISR (usart_periph peripheral, void(*new_handler)(uint8_t received))
 Sets the target function called when recieve ISR is triggered. More...
 
mcu_error USART_setTxISR (usart_periph peripheral, void(*new_handler)(void))
 Sets the target function called when transmit ISR is triggered. More...
 
mcu_error USART_enableISR (usart_periph peripheral, usart_isr isr_type)
 Enables the specified ISR function. More...
 
mcu_error USART_disableISR (usart_periph peripheral, usart_isr isr_type)
 Disables the specified ISR function. More...
 

Detailed Description

This module contains low level functions for USART interaction.

The USART peripherals are mapped to the following pins for the ports

Any USART data structure created needs to be initialized with USART_setup(), this function initializes the given usart peripheral with the following parameters

Author
Stuart Ianna
Version
0.1
Date
May 2018
Warning
None
Bug:
None
Todo:
  • Add function for interrupt priority configuration
Compilers
  • arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
Minimum working example (blocking)
//Change to include appropriate MCU target
int main(void){
//Setup port 1
//ISR are enabled by default, they need to be disabled for blocking read
while(1){
//Echo characters back to the terminal
}
return 0;
}
Minimum working example (interrupts)
//Change to include appropriate MCU target
void dataAvailable(uint8_t byte);
void dataSent(void);
int main(void){
//Setup port 1
//Retarget the ISRs to local functions
USART_setRxISR(USART_1,&dataAvailable);
USART_setTxISR(USART_1,&dataSent);
while(1){
//Nothing to do here
}
return 0;
}
//This is now called when a byte is available to be read
void dataAvailable(uint8_t byte){
//Echo characters back to terminal
}
//This is now called when a byte has finished being sent
void dataSent(void){
//Nothing to do here
}
Full example
example_usart.c

Enumeration Type Documentation

enum usart_baud

Baud rate options for the port.

These value are utilised with function USART_setBaud()

Enumerator
USART_BAUD_2400 

2400bps baud rate

USART_BAUD_4800 

4800bps buad rate

USART_BAUD_9600 

9600bps baud rate

USART_BAUD_19200 

19200bps baud rate

USART_BAUD_38400 

38400bps baud rate

USART_BAUD_57600 

57600bps baud rate

USART_BAUD_115200 

115200bps baud rate

Definition at line 122 of file stm32f103cb_usart.h.

enum usart_data

Data frame lengths for the port.

These values are utilised with function USART_setData()

Enumerator
USART_DATA_EIGHT 

Eight bit data frame for the port.

USART_DATA_NINE 

Nine bit data frame for the port.

Definition at line 112 of file stm32f103cb_usart.h.

enum usart_isr

ISR values for the port.

These values are utilised with function USART_enableISR() and USART_disableISR()

Enumerator
USART_TX 

Transmit ISR.

USART_RX 

Recieve ISR.

Definition at line 137 of file stm32f103cb_usart.h.

Parity option available for the port.

These values are utilised with function USART_setParity()

Enumerator
USART_NONE 

No parity for the port.

USART_ODD 

Odd parity for the port.

USART_EVEN 

Even parity for the port.

Definition at line 91 of file stm32f103cb_usart.h.

Peripheral ports available on the MCU.

These values are used with function USART_setup()

Enumerator
USART_1 

First peripheral port.

USART_2 

Second peripheral port.

USART_3 

Third peripheral port.

Definition at line 79 of file stm32f103cb_usart.h.

enum usart_stop

Stop bit options available for the port.

These values are utilised with function USART_setStop()

Enumerator
USART_STOP_ONE 

One stop bit for the port.

USART_STOP_TWO 

Two stop bits for the port.

Definition at line 102 of file stm32f103cb_usart.h.

Function Documentation

v_fp_u8 USART_add_put ( usart_periph  peripheral)

Return the address of the peripheral put function.

Parameters
peripheralThe USART peripheral to get.
Returns
Error number as defined in mcu_error.

Definition at line 860 of file stm32f103cb_usart.c.

mcu_error USART_disableISR ( usart_periph  peripheral,
usart_isr  isr_type 
)

Disables the specified ISR function.

By default, ISRs are enabled. This can be used to disable them.

Parameters
peripheralThe USART peripheral to modify.
isr_typeThe type of ISR to change, define by usart_isr
Returns
Error number as defined in mcu_error.

Definition at line 1047 of file stm32f103cb_usart.c.

mcu_error USART_enableISR ( usart_periph  peripheral,
usart_isr  isr_type 
)

Enables the specified ISR function.

Parameters
peripheralThe USART peripheral to modify.
isr_typeThe type of ISR to change, define by usart_isr
Returns
Error number as defined in mcu_error.

Definition at line 1136 of file stm32f103cb_usart.c.

uint8_t USART_get ( usart_periph  peripheral)

Get a byte on the given peripheral (blocking).

Parameters
peripheralThe usart number to send on.
Returns
The byte recieved on the port.

Definition at line 891 of file stm32f103cb_usart.c.

mcu_error USART_put ( usart_periph  peripheral,
uint8_t  byte 
)

Send a byte on the given peripheral.

Parameters
peripheralThe usart number to send on.
byteThe byte to send.
Returns
Error number as defined in mcu_error.

Definition at line 826 of file stm32f103cb_usart.c.

mcu_error USART_setBaud ( usart_periph  peripheral,
usart_baud  baud 
)

Set the baud rate for the port.

Parameters
peripheralThe USART peripheral to modify.
baudBaud rate defined by usart_baud to be used.
Returns
Error number as defined in mcu_error.

Definition at line 250 of file stm32f103cb_usart.c.

mcu_error USART_setData ( usart_periph  peripheral,
usart_data  data 
)

Set the data frame size for the port.

Parameters
peripheralThe USART peripheral to modify.
dataData frame size as define in in usart_data.
Returns
Error number as defined in mcu_error.

Definition at line 565 of file stm32f103cb_usart.c.

mcu_error USART_setParity ( usart_periph  peripheral,
usart_parity  parity 
)

Set the parity for the port.

Parameters
peripheralThe USART peripheral to modify.
parityParity option defined in usart_parity.
Returns
Error number as defined in mcu_error.

Definition at line 687 of file stm32f103cb_usart.c.

mcu_error USART_setRxISR ( usart_periph  peripheral,
void(*)(uint8_t received)  new_handler 
)

Sets the target function called when recieve ISR is triggered.

The recieve ISR is called when a byte is ready to be read in the receive buffer.

By default, the ISR flag is cleared first then the function specified by this pointer is called.

If no function has been specified this pointed to NULL.

The function specified must be of this type.

Parameters
peripheralThe USART peripheral to modify.
new_handlerAddress of the function for the ISR to point to.
Returns
Error number as defined in mcu_error.

Definition at line 983 of file stm32f103cb_usart.c.

mcu_error USART_setStop ( usart_periph  peripheral,
usart_stop  stopBits 
)

Set the number of stop bits for the port.

Parameters
peripheralThe USART peripheral to modify.
stopBitsNumber of stop bits to use defined in usart_stop.
Returns
Error number as defined in mcu_error.

Definition at line 443 of file stm32f103cb_usart.c.

mcu_error USART_setTxISR ( usart_periph  peripheral,
void(*)(void)  new_handler 
)

Sets the target function called when transmit ISR is triggered.

The transmit ISR is called when a byte has finished being sent from the buffer.

By default, the ISR flag is cleared first then the function specified by this pointer is called.

If no function has been specified this pointed to NULL.

The function specified must be of this type.

Parameters
peripheralThe USART peripheral to modify.
new_handlerAddress of the function for the ISR to point to.
Returns
Error number as defined in mcu_error.

Definition at line 1015 of file stm32f103cb_usart.c.

mcu_error USART_setup ( usart_periph  peripheral)

Initialize USART port.

This must be called before any other feature is used.

This sets up the given USART peripheral to the default macro defined settings.

Parameters
peripheralThe USART peripheral to modify.
Returns
Error number as defined in mcu_error.

Definition at line 79 of file stm32f103cb_usart.c.