Driver API for Universal Synchronous Asynchronous Receiver/Transmitter (Driver_USART.h) More...
Driver API for Universal Synchronous Asynchronous Receiver/Transmitter (Driver_USART.h)
The Universal Synchronous Asynchronous Receiver/Transmitter (USART) implements a synchronous and asynchronous serial bus for exchanging data. When only asynchronous mode is supported it is called Universal Asynchronous Receiver/Transmitter (UART). Almost all microcontrollers have a serial interface (UART/USART peripheral). A UART is a simple device to send data to a PC via a terminal emulation program (Hyperterm, TeraTerm) or to another microcontroller. A UART takes bytes of data and transmits the individual bits in a sequential mode. At the destination, a second UART reassembles the bits into complete bytes. Each UART contains a shift register for converting between serial and parallel transmission forms. Wikipedia offers more information about the Universal asynchronous receiver/transmitter.
USART API
The following header files define the Application Programming Interface (API) for the USART interface:
The driver implementation is a typical part of the Device Family Pack (DFP) that supports the peripherals of the microcontroller family.
Driver Functions
The driver functions are published in the access struct as explained in Common Driver Functions
Example Code
The following example code shows the usage of the USART interface for asynchronous communication.
#include "cmsis_os.h"
#include <stdio.h>
#include <string.h>
void myUART_Thread(void const *argument);
osThreadId tid_myUART_Thread;
void myUSART_callback(uint32_t event)
{
uint32_t mask;
if (event & mask) {
osSignalSet(tid_myUART_Thread, 0x01);
}
__breakpoint(0);
}
__breakpoint(0);
}
}
void myUART_Thread(const void* args)
{
char cmd;
#ifdef DEBUG
if(version.
api< 0x200)
{
return;
}
{
return;
}
#endif
USARTdrv->
Send(
"\nPress Enter to receive a message", 34);
osSignalWait(0x01, osWaitForever);
while (1)
{
osSignalWait(0x01, osWaitForever);
if (cmd == 13)
{
USARTdrv->
Send(
"\nHello World!", 12);
osSignalWait(0x01, osWaitForever);
}
}
}
Access structure of the USART Driver.
The functions of the USART driver are accessed by function pointers exposed by this structure. Refer to Common Driver Functions for overview information.
Each instance of an USART interface provides such an access structure. The instance is identified by a postfix number in the symbol name of the access structure, for example:
A middleware configuration setting allows connecting the middleware to a specific driver instance Driver_USARTn. The default is 0, which connects a middleware to the first instance of a driver.
int32_t(* Send)(const void *data, uint32_t num)Pointer to ARM_USART_Send : Start sending data to USART transmitter.
int32_t(* Receive)(void *data, uint32_t num)Pointer to ARM_USART_Receive : Start receiving data from USART receiver.
int32_t(* Transfer)(const void *data_out, void *data_in, uint32_t num)Pointer to ARM_USART_Transfer : Start sending/receiving data to/from USART.
int32_t(* Control)(uint32_t control, uint32_t arg)Pointer to ARM_USART_Control : Control USART Interface.
struct ARM_USART_CAPABILITIESUSART Device Driver Capabilities.
An USART driver can be implemented with different capabilities. The data fields of this structure encode the capabilities implemented by this driver.
Returned by:
Data Fields uint32_t asynchronous: 1 supports UART (Asynchronous) mode uint32_t synchronous_master: 1 supports Synchronous Master mode uint32_t synchronous_slave: 1 supports Synchronous Slave mode uint32_t single_wire: 1 supports UART Single-wire mode uint32_t irda: 1 supports UART IrDA mode uint32_t smart_card: 1 supports UART Smart Card mode uint32_t smart_card_clock: 1 Smart Card Clock generator available. uint32_t flow_control_rts: 1 RTS Flow Control available. uint32_t flow_control_cts: 1 CTS Flow Control available. uint32_t event_tx_complete: 1 Transmit completed event: ARM_USART_EVENT_TX_COMPLETE. uint32_t event_rx_timeout: 1 Signal receive character timeout event: ARM_USART_EVENT_RX_TIMEOUT. uint32_t rts: 1 RTS Line: 0=not available, 1=available. uint32_t cts: 1 CTS Line: 0=not available, 1=available. uint32_t dtr: 1 DTR Line: 0=not available, 1=available. uint32_t dsr: 1 DSR Line: 0=not available, 1=available. uint32_t dcd: 1 DCD Line: 0=not available, 1=available. uint32_t ri: 1 RI Line: 0=not available, 1=available. uint32_t event_cts: 1 Signal CTS change event: ARM_USART_EVENT_CTS. uint32_t event_dsr: 1 Signal DSR change event: ARM_USART_EVENT_DSR. uint32_t event_dcd: 1 Signal DCD change event: ARM_USART_EVENT_DCD. uint32_t event_ri: 1 Signal RI change event: ARM_USART_EVENT_RI. uint32_t reserved: 11 Reserved (must be zero)USART Status.
Structure with information about the status of the USART. The data fields encode busy flags and error flags.
Returned by:
Data Fields uint32_t tx_busy: 1 Transmitter busy flag. uint32_t rx_busy: 1 Receiver busy flag. uint32_t tx_underflow: 1 Transmit data underflow detected (cleared on start of next send operation) uint32_t rx_overflow: 1 Receive data overflow detected (cleared on start of next receive operation) uint32_t rx_break: 1 Break detected on receive (cleared on start of next receive operation) uint32_t rx_framing_error: 1 Framing error detected on receive (cleared on start of next receive operation) uint32_t rx_parity_error: 1 Parity error detected on receive (cleared on start of next receive operation) uint32_t reserved: 25 struct ARM_USART_MODEM_STATUSUSART Modem Status.
Structure with information about the status of modem lines. The data fields encode states of modem status lines.
Returned by:
Data Fields uint32_t cts: 1 CTS state: 1=Active, 0=Inactive. uint32_t dsr: 1 DSR state: 1=Active, 0=Inactive. uint32_t dcd: 1 DCD state: 1=Active, 0=Inactive. uint32_t ri: 1 RI state: 1=Active, 0=Inactive. uint32_t reserved: 28USART Modem Control.
Specifies values for controlling the modem control lines.
Parameter for:
Enumerator ARM_USART_RTS_CLEARDeactivate RTS.
ARM_USART_RTS_SETActivate RTS.
ARM_USART_DTR_CLEARDeactivate DTR.
ARM_USART_DTR_SETActivate DTR.
Get driver version.
The function ARM_USART_GetVersion returns version information of the driver implementation in ARM_DRIVER_VERSION
Example:
void setup_usart (void) {
drv_info = &Driver_USART0;
if(version.
api< 0x10A) {
return;
}
}
Get driver capabilities.
The function ARM_USART_GetCapabilities returns information about capabilities in this driver implementation. The data fields of the structure ARM_USART_CAPABILITIES encode various capabilities, for example: supported modes, if hardware and driver are capable of signaling events using the ARM_USART_SignalEvent callback function ...
Example:
void read_capabilities (void) {
drv_info = &Driver_USART0;
}
Initialize USART Interface.
The function ARM_USART_Initialize initializes the USART interface. It is called when the middleware component starts operation.
The function performs the following operations:
The parameter cb_event is a pointer to the ARM_USART_SignalEvent callback function; use a NULL pointer when no callback signals are required.
Example:
De-initialize USART Interface.
The function ARM_USART_Uninitialize de-initializes the resources of USART interface.
It is called when the middleware component stops operation and releases the software resources used by the interface.
Control USART Interface Power.
The function ARM_USART_PowerControl operates the power modes of the USART interface.
The parameter state sets the operation and can have the following values:
Refer to Function Call Sequence for more information.
int32_t ARM_USART_Send ( const void * data, uint32_t num )Start sending data to USART transmitter.
This functions ARM_USART_Send is used in asynchronous mode to send data to the USART transmitter. It can also be used in synchronous mode when sending data only (received data is ignored).
Transmitter needs to be enabled by calling ARM_USART_Control with ARM_USART_CONTROL_TX as the control parameter and 1 as argument.
The function parameters specify the buffer with data and the number of items to send. The item size is defined by the data type which depends on the configured number of data bits.
Data type is:
Calling the function ARM_USART_Send only starts the send operation. The function is non-blocking and returns as soon as the driver has started the operation (driver typically configures DMA or the interrupt system for continuous transfer). When in synchronous slave mode the operation is only registered and started when the master starts the transfer. During the operation it is not allowed to call this function again or any other data transfer function when in synchronous mode. Also the data buffer must stay allocated and the contents of unsent data must not be modified. When send operation is completed (requested number of items sent) the ARM_USART_EVENT_SEND_COMPLETE event is generated. Progress of send operation can also be monitored by reading the number of items already sent by calling ARM_USART_GetTxCount.
After send operation has completed there might still be some data left in the driver's hardware buffer which is still being transmitted. When all data has been physically transmitted the ARM_USART_EVENT_TX_COMPLETE event is generated (if supported and reported by event_tx_complete in ARM_USART_CAPABILITIES). At that point also the tx_busy data field in ARM_USART_STATUS is cleared.
Status of the transmitter can be monitored by calling the ARM_USART_GetStatus and checking the tx_busy flag which indicates if transmission is still in progress.
When in synchronous slave mode and transmitter is enabled but send/receive/transfer operation is not started and data is requested by the master then the ARM_USART_EVENT_TX_UNDERFLOW event is generated.
Send operation can be aborted by calling ARM_USART_Control with ARM_USART_ABORT_SEND as the control parameter.
int32_t ARM_USART_Receive ( void * data, uint32_t num )Start receiving data from USART receiver.
This functions ARM_USART_Receive is used in asynchronous mode to receive data from the USART receiver. It can also be used in synchronous mode when receiving data only (transmits the default value as specified by ARM_USART_Control with ARM_USART_SET_DEFAULT_TX_VALUE as control parameter).
Receiver needs to be enabled by calling ARM_USART_Control with ARM_USART_CONTROL_RX as the control parameter and 1 as argument.
The function parameters specify the buffer for data and the number of items to receive. The item size is defined by the data type which depends on the configured number of data bits.
Data type is:
Calling the function ARM_USART_Receive only starts the receive operation. The function is non-blocking and returns as soon as the driver has started the operation (driver typically configures DMA or the interrupt system for continuous transfer). When in synchronous slave mode the operation is only registered and started when the master starts the transfer. During the operation it is not allowed to call this function again or any other data transfer function when in synchronous mode. Also the data buffer must stay allocated. When receive operation is completed (requested number of items received) the ARM_USART_EVENT_RECEIVE_COMPLETE event is generated. Progress of receive operation can also be monitored by reading the number of items already received by calling ARM_USART_GetRxCount.
Status of the receiver can be monitored by calling the ARM_USART_GetStatus and checking the rx_busy flag which indicates if reception is still in progress.
During reception the following events can be generated (in asynchronous mode):
ARM_USART_EVENT_RX_OVERFLOW event is also generated when receiver is enabled but data is lost because receive operation in asynchronous mode or receive/send/transfer operation in synchronous slave mode has not been started.
Receive operation can be aborted by calling ARM_USART_Control with ARM_USART_ABORT_RECEIVE as the control parameter.
int32_t ARM_USART_Transfer ( const void * data_out, void * data_in, uint32_t num )Start sending/receiving data to/from USART transmitter/receiver.
This functions ARM_USART_Transfer is used in synchronous mode to transfer data via USART. It synchronously sends data to the USART transmitter and receives data from the USART receiver.
Transmitter needs to be enabled by calling ARM_USART_Control with ARM_USART_CONTROL_TX as the control parameter and 1 as argument. Receiver needs to be enabled by calling ARM_USART_Control with ARM_USART_CONTROL_RX as the control parameter and 1 as argument.
The function parameters specify the buffer with data to send, the buffer for data to receive and the number of items to transfer. The item size is defined by the data type which depends on the configured number of data bits.
Data type is:
Calling the function ARM_USART_Transfer only starts the transfer operation. The function is non-blocking and returns as soon as the driver has started the operation (driver typically configures DMA or the interrupt system for continuous transfer). When in synchronous slave mode the operation is only registered and started when the master starts the transfer. During the operation it is not allowed to call this function or any other data transfer function again. Also the data buffers must stay allocated and the contents of unsent data must not be modified. When transfer operation is completed (requested number of items transferred) the ARM_USART_EVENT_TRANSFER_COMPLETE event is generated. Progress of transfer operation can also be monitored by reading the number of items already transferred by calling ARM_USART_GetTxCount or ARM_USART_GetRxCount.
Status of the transmitter or receiver can be monitored by calling the ARM_USART_GetStatus and checking the tx_busy or rx_busy flag.
When in synchronous slave mode also the following events can be generated:
Transfer operation can also be aborted by calling ARM_USART_Control with ARM_USART_ABORT_TRANSFER as the control parameter.
uint32_t ARM_USART_GetTxCount ( void )Get transmitted data count.
The function ARM_USART_GetTxCount returns the number of the currently transmitted data items during ARM_USART_Send and ARM_USART_Transfer operation.
uint32_t ARM_USART_GetRxCount ( void )Get received data count.
The function ARM_USART_GetRxCount returns the number of the currently received data items during ARM_USART_Receive and ARM_USART_Transfer operation.
int32_t ARM_USART_Control ( uint32_t control, uint32_t arg )Control USART Interface.
The function ARM_USART_Control control the USART interface settings and execute various operations.
The parameter control sets the operation and is explained in the table below. Values from different categories can be ORed with the exception of Miscellaneous Operations.
The parameter arg provides, depending on the operation, additional information, for example the baudrate.
The table lists the available control operations.
Example
Get USART status.
The function ARM_USART_GetStatus retrieves the current USART interface status.
Set USART Modem Control line state.
The function ARM_USART_SetModemControl activates or deactivates the selected USART modem control line.
The function ARM_USART_GetModemStatus returns information about status of the modem lines.
Get USART Modem Status lines state.
The function ARM_USART_GetModemStatus returns the current USART Modem Status lines state.
The function ARM_USART_SetModemControl sets the modem control lines of the USART.
void ARM_USART_SignalEvent ( uint32_t event )Signal USART Events.
The function ARM_USART_SignalEvent is a callback function registered by the function ARM_USART_Initialize.
The parameter event indicates one or more events that occurred during driver operation. Each event is encoded in a separate bit and therefore it is possible to signal multiple events within the same call.
Not every event is necessarily generated by the driver. This depends on the implemented capabilities stored in the data fields of the structure ARM_USART_CAPABILITIES, which can be retrieved with the function ARM_USART_GetCapabilities.
The following events can be generated:
Parameter event Bit Description supported when ARM_USART_CAPABILITIES ARM_USART_EVENT_SEND_COMPLETE 0 Occurs after call to ARM_USART_Send to indicate that all the data to be sent was processed by the driver. All the data might have been already transmitted or parts of it are still queued in transmit buffers. The driver is ready for the next call to ARM_USART_Send; however USART may still transmit data. always supported ARM_USART_EVENT_RECEIVE_COMPLETE 1 Occurs after call to ARM_USART_Receive to indicate that all the data has been received. The driver is ready for the next call to ARM_USART_Receive. always supported ARM_USART_EVENT_TRANSFER_COMPLETE 2 Occurs after call to ARM_USART_Transfer to indicate that all the data has been transferred. The driver is ready for the next call to ARM_USART_Transfer. always supported ARM_USART_EVENT_TX_COMPLETE 3 Occurs after call to ARM_USART_Send to indicate that all the data has been physically transmitted on the wires. data field event_tx_complete = 1 ARM_USART_EVENT_TX_UNDERFLOW 4 Occurs in synchronous slave mode when data is requested by the master but send/receive/transfer operation has not been started. Data field rx_underflow = 1 of ARM_USART_STATUS. always supported ARM_USART_EVENT_RX_OVERFLOW 5 Occurs when data is lost during receive/transfer operation or when data is lost because receive operation in asynchronous mode or receive/send/transfer operation in synchronous slave mode has not been started. Data field rx_overflow = 1 of ARM_USART_STATUS. always supported ARM_USART_EVENT_RX_TIMEOUT 6 Occurs during receive when idle time is detected between consecutive characters (idle time is hardware dependent). data field event_rx_timeout = 1 ARM_USART_EVENT_RX_BREAK 7 Occurs when break is detected during receive. Data field rx_break = 1 of ARM_USART_STATUS. always supported ARM_USART_EVENT_RX_FRAMING_ERROR 8 Occurs when framing error is detected during receive. Data field rx_framing_error = 1 of ARM_USART_STATUS. always supported ARM_USART_EVENT_RX_PARITY_ERROR 9 Occurs when parity error is detected during receive. Data field rx_parity_error = 1 of ARM_USART_STATUS. always supported ARM_USART_EVENT_CTS 10 Indicates that CTS modem line state has changed. Data field cts of ARM_USART_MODEM_STATUS has changed. data field event_cts = 1 andRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4