ADuCM302x Device Drivers API Reference Manual  Release 3.1.2.0
retarget_uart.c
1 /*
2 ** I/O redirection support over UART, via SSL/DD.
3 ** Copyright (C) 2017 Analog Devices, Inc. All Rights Reserved.
4 **
5 ** This file is intended for use with the ARM:Compiler:IO:*:User
6 ** components, which set up redirection of stdout and stderr.
7 */
8 
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <drivers/uart/adi_uart.h>
12 #include <retarget_uart_config.h>
13 
14 
15 /* Amount of memory required by the driver for Rx and TX only. */
16 #define ADI_UART_MEMORY_SIZE (ADI_UART_BIDIR_MEMORY_SIZE)
17 
18 #define UART_DEVICE_NUM 0
19 
20 /* Handle for the UART device */
21 static ADI_UART_HANDLE hDevice;
22 
23 /* Memory for the driver */
24 static uint8_t UartDeviceMem[ADI_UART_MEMORY_SIZE];
25 
26 bool Init_Uart(void) {
27 #if ADI_UART_SETUP_PINMUX
28  static bool pinmux_done = false;
29 
30  if (!pinmux_done) {
31  /* Configure GPIO0 pins 10 and 11 for UART0 TX and RX */
32  uint32_t gpio0_cfg = *pREG_GPIO0_CFG;
33  gpio0_cfg &= ~(BITM_GPIO_CFG_PIN10 | BITM_GPIO_CFG_PIN11);
34  gpio0_cfg |= (1u << BITP_GPIO_CFG_PIN10) | (1u << BITP_GPIO_CFG_PIN11);
35  *pREG_GPIO0_CFG = gpio0_cfg;
36  pinmux_done = true;
37  }
38 #endif
39 
40  if (hDevice == NULL) {
41  /* Open the UART device. Data transfer is bidirectional with NORMAL mode by default. */
42  if(adi_uart_Open(UART_DEVICE_NUM,
44  UartDeviceMem,
45  ADI_UART_MEMORY_SIZE,
46  &hDevice) != ADI_UART_SUCCESS) {
47  return false;
48  }
49  }
50 
51  return true;
52 }
53 
54 void Uninit_Uart(void) {
55  /* Close the UART device */
56  adi_uart_Close(hDevice);
57  hDevice = NULL;
58 }
59 
60 static int write_to_uart(int ch) {
61  uint32_t hwError;
62  if ((Init_Uart() == false) || (adi_uart_Write(hDevice, &ch, /*nBufSize=*/1, /*bDMA=*/false, &hwError) != ADI_UART_SUCCESS)) {
63  return EOF;
64  } else {
65  return ch;
66  }
67 }
68 
69 int stdout_putchar(int ch) {
70  return write_to_uart(ch);
71 }
72 
73 int stderr_putchar(int ch) {
74  return write_to_uart(ch);
75 }
76 
77 int stdin_getchar(void) {
78  return EOF;
79 }
80 
81 int ttywrch(int ch) {
82  return write_to_uart(ch);
83 }
84 
85 void _sys_exit(int exit_value) {
86 #if ADI_UART_EXIT_BREAKPOINT
87  __BKPT(0);
88 #else
89  while (1) {
90  /* Do nothing */
91  }
92 #endif
93 }
ADI_UART_RESULT adi_uart_Close(ADI_UART_HANDLE const hDevice)
Uninitialize the memory for the specified UART instance.
Definition: adi_uart.c:507
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. Call to this function will not return until the entire buffer is transmitted. Returns error if this function is called when device is operating in ADI_UART_DATA_TRANSFER_MODE_NONBLOCKING. i.e Function "adi_uart_SubmitTxBuffer()" is called and the transfer is not yet complete.
Definition: adi_uart.c:1166
struct _ADI_UART_DEVICE * ADI_UART_HANDLE
Definition: adi_uart.h:86
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:315