1 /*-----------------------------------------------------------------------------
2  * Name:    stdout_USART.c
3  * Purpose: STDOUT USART Template
4  * Rev.:    1.0.0
5  *-----------------------------------------------------------------------------*/
6 
7 /* Copyright (c) 2013 - 2023 ARM LIMITED
8 
9    All rights reserved.
10    Redistribution and use in source and binary forms, with or without
11    modification, are permitted provided that the following conditions are met:
12    - Redistributions of source code must retain the above copyright
13      notice, this list of conditions and the following disclaimer.
14    - Redistributions in binary form must reproduce the above copyright
15      notice, this list of conditions and the following disclaimer in the
16      documentation and/or other materials provided with the distribution.
17    - Neither the name of ARM nor the names of its contributors may be used
18      to endorse or promote products derived from this software without
19      specific prior written permission.
20    *
21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24    ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
25    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31    POSSIBILITY OF SUCH DAMAGE.
32    ---------------------------------------------------------------------------*/
33 
34 #include "Driver_USART.h"
35 
36 //-------- <<< Use Configuration Wizard in Context Menu >>> --------------------
37 
38 // <h>STDOUT USART Interface
39 
40 //   <o>Connect to hardware via Driver_USART# <0-255>
41 //   <i>Select driver control block for USART interface
42 #define USART_DRV_NUM           0
43 
44 //   <o>Baudrate
45 #define USART_BAUDRATE          115200
46 
47 // </h>
48 
49 
50 #define _USART_Driver_(n)  Driver_USART##n
51 #define  USART_Driver_(n) _USART_Driver_(n)
52 
53 extern ARM_DRIVER_USART  USART_Driver_(USART_DRV_NUM);
54 #define ptrUSART       (&USART_Driver_(USART_DRV_NUM))
55 
56 
57 /**
58   Initialize stdout
59 
60   \return          0 on success, or -1 on error.
61 */
stdout_init(void)62 int stdout_init (void) {
63   int32_t status;
64 
65   status = ptrUSART->Initialize(NULL);
66   if (status != ARM_DRIVER_OK) return (-1);
67 
68   status = ptrUSART->PowerControl(ARM_POWER_FULL);
69   if (status != ARM_DRIVER_OK) return (-1);
70 
71   status = ptrUSART->Control(ARM_USART_MODE_ASYNCHRONOUS |
72                              ARM_USART_DATA_BITS_8       |
73                              ARM_USART_PARITY_NONE       |
74                              ARM_USART_STOP_BITS_1       |
75                              ARM_USART_FLOW_CONTROL_NONE,
76                              USART_BAUDRATE);
77   if (status != ARM_DRIVER_OK) return (-1);
78 
79   status = ptrUSART->Control(ARM_USART_CONTROL_TX, 1);
80   if (status != ARM_DRIVER_OK) return (-1);
81 
82   return (0);
83 }
84 
85 
86 /**
87   Put a character to the stdout
88 
89   \param[in]   ch  Character to output
90   \return          The character written, or -1 on write error.
91 */
stdout_putchar(int ch)92 int stdout_putchar (int ch) {
93   uint8_t buf[1];
94 
95   buf[0] = ch;
96   if (ptrUSART->Send(buf, 1) != ARM_DRIVER_OK) {
97     return (-1);
98   }
99   while (ptrUSART->GetTxCount() != 1);
100   return (ch);
101 }
102