• 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 "__SEGGER_RTL_Int.h"
48 static int _stdin_ungot  = EOF;
49 struct __SEGGER_RTL_FILE_impl { /* NOTE: Provides implementation for FILE */
50     int stub; /* only needed so impl has size != 0. */
51 };
52 
53 static FILE __SEGGER_RTL_stdin_file  = { 0 };  /* stdin reads from UART */
54 static FILE __SEGGER_RTL_stdout_file = { 0 };  /* stdout writes to UART */
55 static FILE __SEGGER_RTL_stderr_file = { 0 };  /* stderr writes to UART */
56 
57 FILE *stdin  = &__SEGGER_RTL_stdin_file;  /* NOTE: Provide implementation of stdin for RTL. */
58 FILE *stdout = &__SEGGER_RTL_stdout_file; /* NOTE: Provide implementation of stdout for RTL. */
59 FILE *stderr = &__SEGGER_RTL_stderr_file; /* NOTE: Provide implementation of stderr for RTL. */
60 
__SEGGER_RTL_X_file_write(__SEGGER_RTL_FILE * file,const char * data,unsigned int size)61 int __SEGGER_RTL_X_file_write(__SEGGER_RTL_FILE *file, const char *data, unsigned int size)
62 {
63     int count;
64     (void)file;
65     for (count = 0; count < size; count++) {
66         if (data[count] == '\n') {
67             while (status_success != uart_send_byte(g_console_uart, '\r')) {
68             }
69         }
70         while (status_success != uart_send_byte(g_console_uart, data[count])) {
71         }
72     }
73     while (status_success != uart_flush(g_console_uart)) {
74     }
75     return count;
76 
77 }
78 
__SEGGER_RTL_X_file_read(__SEGGER_RTL_FILE * file,char * s,unsigned int size)79 int __SEGGER_RTL_X_file_read(__SEGGER_RTL_FILE *file, char *s, unsigned int size)
80 {
81     (void)file;
82     while (status_success != uart_receive_byte(g_console_uart, (uint8_t *)s)) {
83     }
84     return 1;
85 }
86 
__SEGGER_RTL_X_file_stat(__SEGGER_RTL_FILE * stream)87 int __SEGGER_RTL_X_file_stat(__SEGGER_RTL_FILE *stream)
88 {
89     return 0;
90 }
91 
__SEGGER_RTL_X_file_bufsize(__SEGGER_RTL_FILE * stream)92 int __SEGGER_RTL_X_file_bufsize(__SEGGER_RTL_FILE *stream)
93 {
94     return 1;
95 }
96 
__SEGGER_RTL_X_file_unget(__SEGGER_RTL_FILE * stream,int c)97 int __SEGGER_RTL_X_file_unget(__SEGGER_RTL_FILE *stream, int c)
98 {
99     if (stream == stdin) {
100         if (c != EOF && _stdin_ungot == EOF) {
101             _stdin_ungot = c;
102         } else {
103             c = EOF;
104         }
105     } else {
106         c = EOF;
107     }
108     return c;
109 }
110 
__SEGGER_RTL_X_file_flush(__SEGGER_RTL_FILE * __stream)111 int  __SEGGER_RTL_X_file_flush(__SEGGER_RTL_FILE *__stream)
112 {
113     return 1;
114 }
115 
116 #endif
117 
_write(int file,char * data,int size)118 int _write(int file, char *data, int size)
119 {
120     int count;
121     (void)file;
122     for (count = 0; count < size; count++) {
123         if (data[count] == '\n') {
124             while (status_success != uart_send_byte(g_console_uart, '\r')) {
125             }
126         }
127         while (status_success != uart_send_byte(g_console_uart, data[count])) {
128         }
129     }
130     while (status_success != uart_flush(g_console_uart)) {
131     }
132     return count;
133 }
134 
_read(int file,char * s,int size)135 int _read(int file, char *s, int size)
136 {
137     (void)file;
138     while (status_success != uart_receive_byte(g_console_uart, (uint8_t *)s)) {
139     }
140     return 1;
141 }
142 
_fstat(int file,struct stat * s)143 int _fstat(int file, struct stat *s)
144 {
145     s->st_mode = S_IFCHR;
146     return 0;
147 }
148