• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <sys/stat.h>
9 #include "hpm_debug_console.h"
10 #include "hpm_uart_drv.h"
11 
12 static UART_Type* g_console_uart = NULL;
13 
console_init(console_config_t * cfg)14 hpm_stat_t console_init(console_config_t *cfg)
15 {
16     hpm_stat_t stat = status_fail;
17 
18     if (cfg->type == CONSOLE_TYPE_UART) {
19         uart_config_t config = {0};
20         uart_default_config((UART_Type *)cfg->base, &config);
21         config.src_freq_in_hz = cfg->src_freq_in_hz;
22         config.baudrate = cfg->baudrate;
23         stat = uart_init((UART_Type *)cfg->base, &config);
24         if (status_success == stat) {
25             g_console_uart = (UART_Type *)cfg->base;
26         }
27     }
28 
29     return stat;
30 }
31 
console_receive_byte(void)32 uint8_t console_receive_byte(void)
33 {
34     uint8_t c;
35     while (status_success != uart_receive_byte(g_console_uart, &c)) {
36     };
37     return c;
38 }
39 
console_send_byte(uint8_t c)40 void console_send_byte(uint8_t c)
41 {
42     while (status_success != uart_send_byte(g_console_uart, c)) {
43     }
44 }
45 
46 #ifdef __SEGGER_RTL_VERSION
47 #include <stdio.h>
48 #include "__SEGGER_RTL_Int.h"
49 static int _stdin_ungot  = EOF;
50 struct __SEGGER_RTL_FILE_impl { /* NOTE: Provides implementation for FILE */
51     int stub; /* only needed so impl has size != 0. */
52 };
53 
54 static FILE __SEGGER_RTL_stdin_file  = { 0 };  /* stdin reads from UART */
55 static FILE __SEGGER_RTL_stdout_file = { 0 };  /* stdout writes to UART */
56 static FILE __SEGGER_RTL_stderr_file = { 0 };  /* stderr writes to UART */
57 
58 FILE *stdin  = &__SEGGER_RTL_stdin_file;  /* NOTE: Provide implementation of stdin for RTL. */
59 FILE *stdout = &__SEGGER_RTL_stdout_file; /* NOTE: Provide implementation of stdout for RTL. */
60 FILE *stderr = &__SEGGER_RTL_stderr_file; /* NOTE: Provide implementation of stderr for RTL. */
61 
__SEGGER_RTL_X_file_write(__SEGGER_RTL_FILE * file,const char * data,unsigned int size)62 int __SEGGER_RTL_X_file_write(__SEGGER_RTL_FILE *file, const char *data, unsigned int size)
63 {
64     unsigned int count;
65     (void)file;
66     for (count = 0; count < size; count++) {
67         if (data[count] == '\n') {
68             while (status_success != uart_send_byte(g_console_uart, '\r')) {
69             }
70         }
71         while (status_success != uart_send_byte(g_console_uart, data[count])) {
72         }
73     }
74     while (status_success != uart_flush(g_console_uart)) {
75     }
76     return count;
77 
78 }
79 
__SEGGER_RTL_X_file_read(__SEGGER_RTL_FILE * file,char * s,unsigned int size)80 int __SEGGER_RTL_X_file_read(__SEGGER_RTL_FILE *file, char *s, unsigned int size)
81 {
82     (void)file;
83     (void) size;
84     while (status_success != uart_receive_byte(g_console_uart, (uint8_t *)s)) {
85     }
86     return 1;
87 }
88 
__SEGGER_RTL_X_file_stat(__SEGGER_RTL_FILE * stream)89 int __SEGGER_RTL_X_file_stat(__SEGGER_RTL_FILE *stream)
90 {
91     (void) stream;
92     return 0;
93 }
94 
__SEGGER_RTL_X_file_bufsize(__SEGGER_RTL_FILE * stream)95 int __SEGGER_RTL_X_file_bufsize(__SEGGER_RTL_FILE *stream)
96 {
97     (void) stream;
98     return 1;
99 }
100 
__SEGGER_RTL_X_file_unget(__SEGGER_RTL_FILE * stream,int c)101 int __SEGGER_RTL_X_file_unget(__SEGGER_RTL_FILE *stream, int c)
102 {
103     if (stream == stdin) {
104         if (c != EOF && _stdin_ungot == EOF) {
105             _stdin_ungot = c;
106         } else {
107             c = EOF;
108         }
109     } else {
110         c = EOF;
111     }
112     return c;
113 }
114 
__SEGGER_RTL_X_file_flush(__SEGGER_RTL_FILE * __stream)115 int  __SEGGER_RTL_X_file_flush(__SEGGER_RTL_FILE *__stream)
116 {
117     (void) __stream;
118     return 1;
119 }
120 
121 #endif
122 
_write(int file,char * data,int size)123 int _write(int file, char *data, int size)
124 {
125     int count;
126     (void)file;
127     for (count = 0; count < size; count++) {
128         if (data[count] == '\n') {
129             while (status_success != uart_send_byte(g_console_uart, '\r')) {
130             }
131         }
132         while (status_success != uart_send_byte(g_console_uart, data[count])) {
133         }
134     }
135     while (status_success != uart_flush(g_console_uart)) {
136     }
137     return count;
138 }
139 
_read(int file,char * s,int size)140 int _read(int file, char *s, int size)
141 {
142     (void)file;
143     (void) size;
144     while (status_success != uart_receive_byte(g_console_uart, (uint8_t *)s)) {
145     }
146     return 1;
147 }
148 
_fstat(int file,struct stat * s)149 int _fstat(int file, struct stat *s)
150 {
151     (void) file;
152     s->st_mode = S_IFCHR;
153     return 0;
154 }
155