ADuCM4x50 Device Drivers API Reference Manual  Release 4.0.0.0
retarget_uart.c
1 
15 #include <stdio.h>
16 #include <stdint.h>
17 #include <drivers/uart/adi_uart.h>
18 #include <retarget_uart_config.h>
19 
20 
21 /* Amount of memory required by the driver for Rx and TX only. */
22 #define ADI_UART_MEMORY_SIZE (ADI_UART_BIDIR_MEMORY_SIZE)
23 
24 #define UART_DEVICE_NUM 0
25 
26 /* Handle for the UART device */
27 static ADI_UART_HANDLE hDevice;
28 
29 /* Memory for the driver */
30 static uint8_t UartDeviceMem[ADI_UART_MEMORY_SIZE];
31 
32 bool Init_Uart(void) {
33 #if ADI_UART_SETUP_PINMUX
34  static bool pinmux_done = false;
35 
36  if (!pinmux_done) {
37  /* Configure GPIO0 pins 10 and 11 for UART0 TX and RX */
38  uint32_t gpio0_cfg = *pREG_GPIO0_CFG;
39  gpio0_cfg &= ~(BITM_GPIO_CFG_PIN10 | BITM_GPIO_CFG_PIN11);
40  gpio0_cfg |= (1u << BITP_GPIO_CFG_PIN10) | (1u << BITP_GPIO_CFG_PIN11);
41  *pREG_GPIO0_CFG = gpio0_cfg;
42  pinmux_done = true;
43  }
44 #endif
45 
46  if (hDevice == NULL) {
47  /* Open the UART device. Data transfer is bidirectional with NORMAL mode by default. */
48  if(adi_uart_Open(UART_DEVICE_NUM,
50  UartDeviceMem,
51  ADI_UART_MEMORY_SIZE,
52  &hDevice) != ADI_UART_SUCCESS) {
53  return false;
54  }
55  }
56 
57  return true;
58 }
59 
60 void Uninit_Uart(void) {
61  /* Close the UART device */
62  adi_uart_Close(hDevice);
63  hDevice = NULL;
64 }
65 
66 static int write_to_uart(int ch) {
67  uint32_t hwError;
68  if ((Init_Uart() == false) || (adi_uart_Write(hDevice, &ch, /*nBufSize=*/1, /*bDMA=*/false, &hwError) != ADI_UART_SUCCESS)) {
69  return EOF;
70  } else {
71  return ch;
72  }
73 }
74 
75 int stdout_putchar(int ch) {
76  return write_to_uart(ch);
77 }
78 
79 int stderr_putchar(int ch) {
80  return write_to_uart(ch);
81 }
82 
83 int stdin_getchar(void) {
84  return EOF;
85 }
86 
87 int ttywrch(int ch) {
88  return write_to_uart(ch);
89 }
90 
91 void _sys_exit(int exit_value) {
92 #if ADI_UART_EXIT_BREAKPOINT
93  __BKPT(0);
94 #else
95  while (1) {
96  /* Do nothing */
97  }
98 #endif
99 }
ADI_UART_RESULT adi_uart_Close(ADI_UART_HANDLE const hDevice)
Uninitialize the memory for the specified UART instance.
Definition: adi_uart.c:475
ADI_UART_RESULT adi_uart_Write(ADI_UART_HANDLE const hDevice, void *const pBuffer, uint32_t const nBufSize, bool const bDMA, uint32_t *pHwError)
Submit the buffer for transmitting the data in ADI_UART_DATA_TRANSFER_MODE_BLOCKING....
Definition: adi_uart.c:1490
struct _ADI_UART_DEVICE * ADI_UART_HANDLE
Definition: adi_uart.h:55
ADI_UART_RESULT adi_uart_Open(uint32_t const nDeviceNum, ADI_UART_DIRECTION const eDirection, void *pMemory, uint32_t const nMemSize, ADI_UART_HANDLE *const phDevice)
Initialization function for the UART device.
Definition: adi_uart.c:284