This chapter describes the programming interface of the serial manager component.
The serial manager component provides a series of APIs to operate different serial port types. The port types it supports are UART, USB CDC and SWO.
|
enum | serial_port_type_t {
kSerialPort_Uart = 1U,
kSerialPort_UsbCdc,
kSerialPort_Swo,
kSerialPort_UsbCdcVirtual
} |
| serial port type More...
|
|
enum | serial_manager_status_t {
kStatus_SerialManager_Success = kStatus_Success,
kStatus_SerialManager_Error = MAKE_STATUS(kStatusGroup_SERIALMANAGER, 1),
kStatus_SerialManager_Busy = MAKE_STATUS(kStatusGroup_SERIALMANAGER, 2),
kStatus_SerialManager_Notify = MAKE_STATUS(kStatusGroup_SERIALMANAGER, 3),
kStatus_SerialManager_Canceled,
kStatus_SerialManager_HandleConflict = MAKE_STATUS(kStatusGroup_SERIALMANAGER, 5),
kStatus_SerialManager_RingBufferOverflow,
kStatus_SerialManager_NotConnected = MAKE_STATUS(kStatusGroup_SERIALMANAGER, 7)
} |
| serial manager error code More...
|
|
|
serial_manager_status_t | SerialManager_Init (serial_handle_t serialHandle, serial_manager_config_t *config) |
| Initializes a serial manager module with the serial manager handle and the user configuration structure. More...
|
|
serial_manager_status_t | SerialManager_Deinit (serial_handle_t serialHandle) |
| De-initializes the serial manager module instance. More...
|
|
serial_manager_status_t | SerialManager_OpenWriteHandle (serial_handle_t serialHandle, serial_write_handle_t writeHandle) |
| Opens a writing handle for the serial manager module. More...
|
|
serial_manager_status_t | SerialManager_CloseWriteHandle (serial_write_handle_t writeHandle) |
| Closes a writing handle for the serial manager module. More...
|
|
serial_manager_status_t | SerialManager_OpenReadHandle (serial_handle_t serialHandle, serial_read_handle_t readHandle) |
| Opens a reading handle for the serial manager module. More...
|
|
serial_manager_status_t | SerialManager_CloseReadHandle (serial_read_handle_t readHandle) |
| Closes a reading for the serial manager module. More...
|
|
serial_manager_status_t | SerialManager_WriteBlocking (serial_write_handle_t writeHandle, uint8_t *buffer, uint32_t length) |
| Transmits data with the blocking mode. More...
|
|
serial_manager_status_t | SerialManager_ReadBlocking (serial_read_handle_t readHandle, uint8_t *buffer, uint32_t length) |
| Reads data with the blocking mode. More...
|
|
serial_manager_status_t | SerialManager_WriteNonBlocking (serial_write_handle_t writeHandle, uint8_t *buffer, uint32_t length) |
| Transmits data with the non-blocking mode. More...
|
|
serial_manager_status_t | SerialManager_ReadNonBlocking (serial_read_handle_t readHandle, uint8_t *buffer, uint32_t length) |
| Reads data with the non-blocking mode. More...
|
|
serial_manager_status_t | SerialManager_TryRead (serial_read_handle_t readHandle, uint8_t *buffer, uint32_t length, uint32_t *receivedLength) |
| Tries to read data. More...
|
|
serial_manager_status_t | SerialManager_CancelWriting (serial_write_handle_t writeHandle) |
| Cancels unfinished send transmission. More...
|
|
serial_manager_status_t | SerialManager_CancelReading (serial_read_handle_t readHandle) |
| Cancels unfinished receive transmission. More...
|
|
serial_manager_status_t | SerialManager_InstallTxCallback (serial_write_handle_t writeHandle, serial_manager_callback_t callback, void *callbackParam) |
| Installs a TX callback and callback parameter. More...
|
|
serial_manager_status_t | SerialManager_InstallRxCallback (serial_read_handle_t readHandle, serial_manager_callback_t callback, void *callbackParam) |
| Installs a RX callback and callback parameter. More...
|
|
serial_manager_status_t | SerialManager_EnterLowpower (serial_handle_t serialHandle) |
| Prepares to enter low power consumption. More...
|
|
serial_manager_status_t | SerialManager_ExitLowpower (serial_handle_t serialHandle) |
| Restores from low power consumption. More...
|
|
static bool | SerialManager_needPollingIsr (void) |
| Check if need polling ISR. More...
|
|
struct serial_manager_config_t |
uint8_t* serial_manager_config_t::ringBuffer |
Besides, the memory space cannot be free during the lifetime of the serial manager module.
struct serial_manager_callback_message_t |
Data Fields |
uint8_t * | buffer |
| Transferred buffer.
|
|
uint32_t | length |
| Transferred data length.
|
|
#define SERIAL_MANAGER_HANDLE_SIZE (SERIAL_MANAGER_HANDLE_SIZE_TEMP + 120U) |
Definition of serial manager handle size.
#define SERIAL_MANAGER_HANDLE_DEFINE |
( |
|
name | ) |
uint32_t name[((SERIAL_MANAGER_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t))] |
This macro is used to define a 4 byte aligned serial manager handle. Then use "(serial_handle_t)name" to get the serial manager handle.
The macro should be global and could be optional. You could also define serial manager handle by yourself.
This is an example,
- Parameters
-
name | The name string of the serial manager handle. |
This macro is used to define a 4 byte aligned serial manager write handle. Then use "(serial_write_handle_t)name" to get the serial manager write handle.
The macro should be global and could be optional. You could also define serial manager write handle by yourself.
This is an example,
- Parameters
-
name | The name string of the serial manager write handle. |
#define SERIAL_MANAGER_READ_HANDLE_DEFINE |
( |
|
name | ) |
uint32_t name[((SERIAL_MANAGER_READ_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t))] |
This macro is used to define a 4 byte aligned serial manager read handle. Then use "(serial_read_handle_t)name" to get the serial manager read handle.
The macro should be global and could be optional. You could also define serial manager read handle by yourself.
This is an example,
- Parameters
-
name | The name string of the serial manager read handle. |
#define SERIAL_MANAGER_USE_COMMON_TASK (1U) |
#define SERIAL_MANAGER_TASK_PRIORITY (2U) |
#define SERIAL_MANAGER_TASK_STACK_SIZE (1000U) |
Enumerator |
---|
kSerialPort_Uart |
Serial port UART.
|
kSerialPort_UsbCdc |
Serial port USB CDC.
|
kSerialPort_Swo |
Serial port SWO.
|
kSerialPort_UsbCdcVirtual |
Serial port USB CDC Virtual.
|
Enumerator |
---|
kStatus_SerialManager_Success |
Success.
|
kStatus_SerialManager_Error |
Failed.
|
kStatus_SerialManager_Busy |
Busy.
|
kStatus_SerialManager_Notify |
Ring buffer is not empty.
|
kStatus_SerialManager_Canceled |
the non-blocking request is canceled
|
kStatus_SerialManager_HandleConflict |
The handle is opened.
|
kStatus_SerialManager_RingBufferOverflow |
The ring buffer is overflowed.
|
kStatus_SerialManager_NotConnected |
The host is not connected.
|
This function configures the Serial Manager module with user-defined settings. The user can configure the configuration structure. The parameter serialHandle is a pointer to point to a memory space of size SERIAL_MANAGER_HANDLE_SIZE allocated by the caller. The Serial Manager module supports three types of serial port, UART (includes UART, USART, LPSCI, LPUART, etc), USB CDC and swo. Please refer to serial_port_type_t for serial port setting. These three types can be set by using serial_manager_config_t.
Example below shows how to use this API to configure the Serial Manager. For UART,
* #define SERIAL_MANAGER_RING_BUFFER_SIZE (256U)
* static uint8_t s_ringBuffer[SERIAL_MANAGER_RING_BUFFER_SIZE];
*
*
For USB CDC,
* #define SERIAL_MANAGER_RING_BUFFER_SIZE (256U)
* static uint8_t s_ringBuffer[SERIAL_MANAGER_RING_BUFFER_SIZE];
*
*
- Parameters
-
serialHandle | Pointer to point to a memory space of size SERIAL_MANAGER_HANDLE_SIZE allocated by the caller. The handle should be 4 byte aligned, because unaligned access doesn't be supported on some devices. You can define the handle in the following two ways: SERIAL_MANAGER_HANDLE_DEFINE(serialHandle); or uint32_t serialHandle[((SERIAL_MANAGER_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t))]; |
config | Pointer to user-defined configuration structure. |
- Return values
-
kStatus_SerialManager_Error | An error occurred. |
kStatus_SerialManager_Success | The Serial Manager module initialization succeed. |
This function de-initializes the serial manager module instance. If the opened writing or reading handle is not closed, the function will return kStatus_SerialManager_Busy.
- Parameters
-
serialHandle | The serial manager module handle pointer. |
- Return values
-
kStatus_SerialManager_Success | The serial manager de-initialization succeed. |
kStatus_SerialManager_Busy | Opened reading or writing handle is not closed. |
This function Opens a writing handle for the serial manager module. If the serial manager needs to be used in different tasks, the task should open a dedicated write handle for itself by calling SerialManager_OpenWriteHandle. Since there can only one buffer for transmission for the writing handle at the same time, multiple writing handles need to be opened when the multiple transmission is needed for a task.
- Parameters
-
serialHandle | The serial manager module handle pointer. The handle should be 4 byte aligned, because unaligned access doesn't be supported on some devices. |
writeHandle | The serial manager module writing handle pointer. The handle should be 4 byte aligned, because unaligned access doesn't be supported on some devices. You can define the handle in the following two ways: SERIAL_MANAGER_WRITE_HANDLE_DEFINE(writeHandle); or uint32_t writeHandle[((SERIAL_MANAGER_WRITE_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t))]; |
- Return values
-
kStatus_SerialManager_Error | An error occurred. |
kStatus_SerialManager_HandleConflict | The writing handle was opened. |
kStatus_SerialManager_Success | The writing handle is opened. |
Example below shows how to use this API to write data. For task 1,
* static uint8_t s_nonBlockingWelcome1[] = "This is non-blocking writing log for task1!\r\n";
* Task1_SerialManagerTxCallback,
* s_serialWriteHandle1);
* s_nonBlockingWelcome1,
* sizeof(s_nonBlockingWelcome1) - 1U);
*
For task 2,
* static uint8_t s_nonBlockingWelcome2[] = "This is non-blocking writing log for task2!\r\n";
* Task2_SerialManagerTxCallback,
* s_serialWriteHandle2);
* s_nonBlockingWelcome2,
* sizeof(s_nonBlockingWelcome2) - 1U);
*
This function Closes a writing handle for the serial manager module.
- Parameters
-
writeHandle | The serial manager module writing handle pointer. |
- Return values
-
kStatus_SerialManager_Success | The writing handle is closed. |
This function Opens a reading handle for the serial manager module. The reading handle can not be opened multiple at the same time. The error code kStatus_SerialManager_Busy would be returned when the previous reading handle is not closed. And there can only be one buffer for receiving for the reading handle at the same time.
- Parameters
-
serialHandle | The serial manager module handle pointer. The handle should be 4 byte aligned, because unaligned access doesn't be supported on some devices. |
readHandle | The serial manager module reading handle pointer. The handle should be 4 byte aligned, because unaligned access doesn't be supported on some devices. You can define the handle in the following two ways: SERIAL_MANAGER_READ_HANDLE_DEFINE(readHandle); or uint32_t readHandle[((SERIAL_MANAGER_READ_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t))]; |
- Return values
-
kStatus_SerialManager_Error | An error occurred. |
kStatus_SerialManager_Success | The reading handle is opened. |
kStatus_SerialManager_Busy | Previous reading handle is not closed. |
Example below shows how to use this API to read data.
* static uint8_t s_nonBlockingBuffer[64];
* APP_SerialManagerRxCallback,
* s_serialReadHandle);
* s_nonBlockingBuffer,
* sizeof(s_nonBlockingBuffer));
*
This function Closes a reading for the serial manager module.
- Parameters
-
readHandle | The serial manager module reading handle pointer. |
- Return values
-
kStatus_SerialManager_Success | The reading handle is closed. |
This is a blocking function, which polls the sending queue, waits for the sending queue to be empty. This function sends data using an interrupt method. The interrupt of the hardware could not be disabled. And There can only one buffer for transmission for the writing handle at the same time.
- Note
- The function SerialManager_WriteBlocking and the function SerialManager_WriteNonBlocking cannot be used at the same time. And, the function SerialManager_CancelWriting cannot be used to abort the transmission of this function.
- Parameters
-
writeHandle | The serial manager module handle pointer. |
buffer | Start address of the data to write. |
length | Length of the data to write. |
- Return values
-
kStatus_SerialManager_Success | Successfully sent all data. |
kStatus_SerialManager_Busy | Previous transmission still not finished; data not all sent yet. |
kStatus_SerialManager_Error | An error occurred. |
This is a blocking function, which polls the receiving buffer, waits for the receiving buffer to be full. This function receives data using an interrupt method. The interrupt of the hardware could not be disabled. And There can only one buffer for receiving for the reading handle at the same time.
- Note
- The function SerialManager_ReadBlocking and the function SerialManager_ReadNonBlocking cannot be used at the same time. And, the function SerialManager_CancelReading cannot be used to abort the transmission of this function.
- Parameters
-
readHandle | The serial manager module handle pointer. |
buffer | Start address of the data to store the received data. |
length | The length of the data to be received. |
- Return values
-
kStatus_SerialManager_Success | Successfully received all data. |
kStatus_SerialManager_Busy | Previous transmission still not finished; data not all received yet. |
kStatus_SerialManager_Error | An error occurred. |
This is a non-blocking function, which returns directly without waiting for all data to be sent. When all data is sent, the module notifies the upper layer through a TX callback function and passes the status parameter kStatus_SerialManager_Success. This function sends data using an interrupt method. The interrupt of the hardware could not be disabled. And There can only one buffer for transmission for the writing handle at the same time.
- Note
- The function SerialManager_WriteBlocking and the function SerialManager_WriteNonBlocking cannot be used at the same time. And, the TX callback is mandatory before the function could be used.
- Parameters
-
writeHandle | The serial manager module handle pointer. |
buffer | Start address of the data to write. |
length | Length of the data to write. |
- Return values
-
kStatus_SerialManager_Success | Successfully sent all data. |
kStatus_SerialManager_Busy | Previous transmission still not finished; data not all sent yet. |
kStatus_SerialManager_Error | An error occurred. |
This is a non-blocking function, which returns directly without waiting for all data to be received. When all data is received, the module driver notifies the upper layer through a RX callback function and passes the status parameter kStatus_SerialManager_Success. This function receives data using an interrupt method. The interrupt of the hardware could not be disabled. And There can only one buffer for receiving for the reading handle at the same time.
- Note
- The function SerialManager_ReadBlocking and the function SerialManager_ReadNonBlocking cannot be used at the same time. And, the RX callback is mandatory before the function could be used.
- Parameters
-
readHandle | The serial manager module handle pointer. |
buffer | Start address of the data to store the received data. |
length | The length of the data to be received. |
- Return values
-
kStatus_SerialManager_Success | Successfully received all data. |
kStatus_SerialManager_Busy | Previous transmission still not finished; data not all received yet. |
kStatus_SerialManager_Error | An error occurred. |
The function tries to read data from internal ring buffer. If the ring buffer is not empty, the data will be copied from ring buffer to up layer buffer. The copied length is the minimum of the ring buffer and up layer length. After the data is copied, the actual data length is passed by the parameter length. And There can only one buffer for receiving for the reading handle at the same time.
- Parameters
-
readHandle | The serial manager module handle pointer. |
buffer | Start address of the data to store the received data. |
length | The length of the data to be received. |
receivedLength | Length received from the ring buffer directly. |
- Return values
-
kStatus_SerialManager_Success | Successfully received all data. |
kStatus_SerialManager_Busy | Previous transmission still not finished; data not all received yet. |
kStatus_SerialManager_Error | An error occurred. |
The function cancels unfinished send transmission. When the transfer is canceled, the module notifies the upper layer through a TX callback function and passes the status parameter kStatus_SerialManager_Canceled.
- Note
- The function SerialManager_CancelWriting cannot be used to abort the transmission of the function SerialManager_WriteBlocking.
- Parameters
-
writeHandle | The serial manager module handle pointer. |
- Return values
-
kStatus_SerialManager_Success | Get successfully abort the sending. |
kStatus_SerialManager_Error | An error occurred. |
The function cancels unfinished receive transmission. When the transfer is canceled, the module notifies the upper layer through a RX callback function and passes the status parameter kStatus_SerialManager_Canceled.
- Note
- The function SerialManager_CancelReading cannot be used to abort the transmission of the function SerialManager_ReadBlocking.
- Parameters
-
readHandle | The serial manager module handle pointer. |
- Return values
-
kStatus_SerialManager_Success | Get successfully abort the receiving. |
kStatus_SerialManager_Error | An error occurred. |
This function is used to install the TX callback and callback parameter for the serial manager module. When any status of TX transmission changed, the driver will notify the upper layer by the installed callback function. And the status is also passed as status parameter when the callback is called.
- Parameters
-
writeHandle | The serial manager module handle pointer. |
callback | The callback function. |
callbackParam | The parameter of the callback function. |
- Return values
-
kStatus_SerialManager_Success | Successfully install the callback. |
This function is used to install the RX callback and callback parameter for the serial manager module. When any status of RX transmission changed, the driver will notify the upper layer by the installed callback function. And the status is also passed as status parameter when the callback is called.
- Parameters
-
readHandle | The serial manager module handle pointer. |
callback | The callback function. |
callbackParam | The parameter of the callback function. |
- Return values
-
kStatus_SerialManager_Success | Successfully install the callback. |
This function is used to prepare to enter low power consumption.
- Parameters
-
serialHandle | The serial manager module handle pointer. |
- Return values
-
kStatus_SerialManager_Success | Successful operation. |
This function is used to restore from low power consumption.
- Parameters
-
serialHandle | The serial manager module handle pointer. |
- Return values
-
kStatus_SerialManager_Success | Successful operation. |
static bool SerialManager_needPollingIsr |
( |
void |
| ) |
|
|
inlinestatic |
This function is used to check if need polling ISR.
- Parameters
-
- Return values
-