1 /***************************************************************************//**
2 * \file cy_retarget_io.c
3 *
4 * \brief
5 * Provides APIs for retargeting stdio to UART hardware contained on the Cypress
6 * kits.
7 *
8 ********************************************************************************
9 * \copyright
10 * Copyright 2018-2019 Cypress Semiconductor Corporation
11 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 *     http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *******************************************************************************/
25 
26 #include "cy_retarget_io_pdl.h"
27 
28 #include "cycfg_peripherals.h"
29 
30 #include "cy_sysint.h"
31 #include "cy_scb_uart.h"
32 
33 #if defined(__cplusplus)
34 extern "C" {
35 #endif
36 
37 /* Tracks the previous character sent to output stream */
38 #ifdef CY_RETARGET_IO_CONVERT_LF_TO_CRLF
39 static char cy_retarget_io_stdout_prev_char = 0;
40 #endif /* CY_RETARGET_IO_CONVERT_LF_TO_CRLF */
41 
42 cy_stc_scb_uart_context_t CYBSP_UART_context;
43 
44 static uint8_t cy_retarget_io_getchar(void);
45 static void cy_retarget_io_putchar(char c);
46 
47 #if defined(__ARMCC_VERSION) /* ARM-MDK */
48     /***************************************************************************
49     * Function Name: fputc
50     ***************************************************************************/
fputc(int ch,FILE * f)51     __attribute__((weak)) int fputc(int ch, FILE *f)
52     {
53         (void)f;
54     #ifdef CY_RETARGET_IO_CONVERT_LF_TO_CRLF
55         if ((char)ch == '\n' && cy_retarget_io_stdout_prev_char != '\r')
56         {
57             cy_retarget_io_putchar('\r');
58         }
59 
60         cy_retarget_io_stdout_prev_char = (char)ch;
61     #endif /* CY_RETARGET_IO_CONVERT_LF_TO_CRLF */
62         cy_retarget_io_putchar(ch);
63         return (ch);
64     }
65 #elif defined (__ICCARM__) /* IAR */
66     #include <yfuns.h>
67 
68     /***************************************************************************
69     * Function Name: __write
70     ***************************************************************************/
__write(int handle,const unsigned char * buffer,size_t size)71     __weak size_t __write(int handle, const unsigned char * buffer, size_t size)
72     {
73         size_t nChars = 0;
74         /* This template only writes to "standard out", for all other file
75         * handles it returns failure. */
76         if (handle != _LLIO_STDOUT)
77         {
78             return (_LLIO_ERROR);
79         }
80         if (buffer != NULL)
81         {
82             for (/* Empty */; nChars < size; ++nChars)
83             {
84             #ifdef CY_RETARGET_IO_CONVERT_LF_TO_CRLF
85                 if (*buffer == '\n' && cy_retarget_io_stdout_prev_char != '\r')
86                 {
87                     cy_retarget_io_putchar('\r');
88                 }
89 
90                 cy_retarget_io_stdout_prev_char = *buffer;
91             #endif /* CY_RETARGET_IO_CONVERT_LF_TO_CRLF */
92                 cy_retarget_io_putchar(*buffer);
93                 ++buffer;
94             }
95         }
96         return (nChars);
97     }
98 #else /* (__GNUC__)  GCC */
99     /* Add an explicit reference to the floating point printf library to allow
100     the usage of floating point conversion specifier. */
101     __asm (".global _printf_float");
102     /***************************************************************************
103     * Function Name: _write
104     ***************************************************************************/
_write(int fd,const char * ptr,int len)105     __attribute__((weak)) int _write (int fd, const char *ptr, int len)
106     {
107         int nChars = 0;
108         (void)fd;
109         if (ptr != NULL)
110         {
111             for (/* Empty */; nChars < len; ++nChars)
112             {
113             #ifdef CY_RETARGET_IO_CONVERT_LF_TO_CRLF
114                 if (*ptr == '\n' && cy_retarget_io_stdout_prev_char != '\r')
115                 {
116                     cy_retarget_io_putchar('\r');
117                 }
118 
119                 cy_retarget_io_stdout_prev_char = *ptr;
120             #endif /* CY_RETARGET_IO_CONVERT_LF_TO_CRLF */
121                 cy_retarget_io_putchar((uint32_t)*ptr);
122                 ++ptr;
123             }
124         }
125         return (nChars);
126     }
127 #endif
128 
129 
130 #if defined(__ARMCC_VERSION) /* ARM-MDK */
131     /***************************************************************************
132     * Function Name: fgetc
133     ***************************************************************************/
fgetc(FILE * f)134     __attribute__((weak)) int fgetc(FILE *f)
135     {
136         (void)f;
137         return (cy_retarget_io_getchar());
138     }
139 #elif defined (__ICCARM__) /* IAR */
__read(int handle,unsigned char * buffer,size_t size)140     __weak size_t __read(int handle, unsigned char * buffer, size_t size)
141     {
142         /* This template only reads from "standard in", for all other file
143         handles it returns failure. */
144         if ((handle != _LLIO_STDIN) || (buffer == NULL))
145         {
146             return (_LLIO_ERROR);
147         }
148         else
149         {
150             *buffer = cy_retarget_io_getchar();
151             return (1);
152         }
153     }
154 #else /* (__GNUC__)  GCC */
155     /* Add an explicit reference to the floating point scanf library to allow
156     the usage of floating point conversion specifier. */
157     __asm (".global _scanf_float");
_read(int fd,char * ptr,int len)158     __attribute__((weak)) int _read (int fd, char *ptr, int len)
159     {
160         int nChars = 0;
161         (void)fd;
162         if (ptr != NULL)
163         {
164             for(/* Empty */;nChars < len;++ptr)
165             {
166                 *ptr = (char)cy_retarget_io_getchar();
167                 ++nChars;
168                 if((*ptr == '\n') || (*ptr == '\r'))
169                 {
170                     break;
171                 }
172             }
173         }
174         return (nChars);
175     }
176 #endif
177 
cy_retarget_io_getchar(void)178 static uint8_t cy_retarget_io_getchar(void)
179 {
180     uint32_t read_value = Cy_SCB_UART_Get(CYBSP_UART_HW);
181     while (read_value == CY_SCB_UART_RX_NO_DATA)
182     {
183         read_value = Cy_SCB_UART_Get(CYBSP_UART_HW);
184     }
185 
186     return (uint8_t)read_value;
187 }
188 
cy_retarget_io_putchar(char c)189 static void cy_retarget_io_putchar(char c)
190 {
191     uint32_t count = 0;
192     while (count == 0)
193     {
194         count = Cy_SCB_UART_Put(CYBSP_UART_HW, c);
195     }
196 }
197 
cy_retarget_io_pdl_setbaud(CySCB_Type * base,uint32_t baudrate)198 static cy_rslt_t cy_retarget_io_pdl_setbaud(CySCB_Type *base, uint32_t baudrate)
199 {
200     cy_rslt_t result = CY_RSLT_TYPE_ERROR;
201 
202     uint8_t oversample_value = 8u;
203     uint8_t frac_bits = 0u;
204     uint32_t divider;
205 
206     Cy_SCB_UART_Disable(base, NULL);
207 
208     result = (cy_rslt_t) Cy_SysClk_PeriphDisableDivider(CY_SYSCLK_DIV_16_BIT, 0);
209 
210     divider = ((Cy_SysClk_ClkPeriGetFrequency() * (1 << frac_bits)) + ((baudrate * oversample_value) / 2)) / (baudrate * oversample_value) - 1;
211 
212     if (result == CY_RSLT_SUCCESS)
213     {
214         result = (cy_rslt_t) Cy_SysClk_PeriphSetDivider(CY_SYSCLK_DIV_16_BIT, 0u, divider);
215     }
216 
217     if (result == CY_RSLT_SUCCESS)
218     {
219         result = Cy_SysClk_PeriphEnableDivider(CY_SYSCLK_DIV_16_BIT, 0u);
220     }
221 
222     Cy_SCB_UART_Enable(base);
223 
224     return result;
225 }
226 
cy_retarget_io_pdl_init(uint32_t baudrate)227 cy_rslt_t cy_retarget_io_pdl_init(uint32_t baudrate)
228 {
229     cy_rslt_t result = CY_RSLT_TYPE_ERROR;
230 
231     result = (cy_rslt_t)Cy_SCB_UART_Init(CYBSP_UART_HW, &CYBSP_UART_config, &CYBSP_UART_context);
232 
233     if (result == CY_RSLT_SUCCESS)
234     {
235         result = cy_retarget_io_pdl_setbaud(CYBSP_UART_HW, baudrate);
236     }
237 
238     if (result == CY_RSLT_SUCCESS)
239     {
240         Cy_SCB_UART_Enable(CYBSP_UART_HW);
241     }
242 
243     return result;
244 }
245 
246 /**
247  * @brief Wait while UART completes transfer. Try for tries_count times -
248  *        once each 10 millisecons.
249  */
cy_retarget_io_wait_tx_complete(CySCB_Type * base,uint32_t tries_count)250 void cy_retarget_io_wait_tx_complete(CySCB_Type *base, uint32_t tries_count)
251 {
252     while(tries_count > 0)
253     {
254         if (!Cy_SCB_UART_IsTxComplete(base)) {
255             Cy_SysLib_DelayCycles(10 * cy_delayFreqKhz);
256             tries_count -= 1;
257         } else {
258             return;
259         }
260     }
261 }
262 
cy_retarget_io_pdl_deinit()263 void cy_retarget_io_pdl_deinit()
264 {
265     Cy_SCB_UART_DeInit(CYBSP_UART_HW);
266 }
267 
268 #if defined(__cplusplus)
269 }
270 #endif
271