- Note
- To have the full flexibility for retargeting different channels to different target hardware, each USART user code template contains a function to initialize the USART (here
stdin_init
). When using the same hardware for multiple channels, the initialization functions need to be merged in one single function. Call this function during the initialization of the device's peripherals.
#include "retarget_stdin.h"
#include "Driver_USART.h"
#define USART_DRV_NUM 0
#define USART_BAUDRATE 115200
#define _USART_Driver_(n) Driver_USART##n
#define USART_Driver_(n) _USART_Driver_(n)
extern ARM_DRIVER_USART USART_Driver_(USART_DRV_NUM);
#define ptrUSART (&USART_Driver_(USART_DRV_NUM))
int stdin_init (void) {
int32_t status;
status = ptrUSART->Initialize(NULL);
if (status != ARM_DRIVER_OK) return (-1);
status = ptrUSART->PowerControl(ARM_POWER_FULL);
if (status != ARM_DRIVER_OK) return (-1);
status = ptrUSART->Control(ARM_USART_MODE_ASYNCHRONOUS |
ARM_USART_DATA_BITS_8 |
ARM_USART_PARITY_NONE |
ARM_USART_STOP_BITS_1 |
ARM_USART_FLOW_CONTROL_NONE,
USART_BAUDRATE);
if (status != ARM_DRIVER_OK) return (-1);
status = ptrUSART->Control(ARM_USART_CONTROL_RX, 1);
if (status != ARM_DRIVER_OK) return (-1);
return (0);
}
int stdin_getchar (void) {
uint8_t buf[1];
if (ptrUSART->Receive(buf, 1) != ARM_DRIVER_OK) {
return (-1);
}
while (ptrUSART->GetRxCount() != 1);
return (buf[0]);
}