px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_uart_stdio.c
1/* =============================================================================
2 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
3 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
4 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
5 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
6 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
7
8 Copyright (c) 2018 Pieter Conradie <https://piconomix.com>
9
10 License: MIT
11 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
12
13 Title: px_uart_stdio.h : Module to redirect stdio output (e.g. printf) to a UART
14 Author(s): Pieter Conradie
15 Creation Date: 2018-02-06
16
17============================================================================= */
18
19/* _____STANDARD INCLUDES____________________________________________________ */
20#include <string.h>
21#include "px_compiler.h"
22
23/* _____PROJECT INCLUDES_____________________________________________________ */
24#include "px_uart_stdio.h"
25
26/* _____LOCAL DEFINITIONS____________________________________________________ */
27
28/* _____MACROS_______________________________________________________________ */
29
30/* _____GLOBAL VARIABLES_____________________________________________________ */
31
32/* _____LOCAL FUNCTION DECLARATIONS__________________________________________ */
33
34/* _____LOCAL VARIABLES______________________________________________________ */
35/// UART handle
36px_uart_handle_t * px_uart_stdio_handle;
37
38#ifdef PX_COMPILER_ARM_CC
39#pragma import(__use_no_semihosting_swi)
40struct __FILE
41{
42 int handle;
43};
44
45FILE __stdout;
46FILE __stdin;
47#endif
48
49/* _____LOCAL FUNCTIONS______________________________________________________ */
50#ifdef PX_COMPILER_GCC_ARM
51int _write(int file, char * ptr, int len)
52{
53 int i;
54
55 for(i = 0; i < len; i++)
56 {
58 }
59 return len;
60}
61
62int _read(int file, char * ptr, int len)
63{
64 int i;
65
66 for(i = 0; i < len; i++)
67 {
68 *ptr++ = px_uart_stdio_getchar();
69 }
70 return len;
71}
72#endif
73
74#ifdef PX_COMPILER_ARM_CC
75int fputc(int ch, FILE * f)
76{
78
79 return 0;
80}
81
82int fgetc(FILE * f)
83{
84 return px_uart_stdio_getchar();
85}
86
87void _sys_exit(int return_code)
88{
89 while(1);
90}
91#endif
92
93/* _____GLOBAL FUNCTIONS_____________________________________________________ */
95{
96 // Save handle
97 px_uart_stdio_handle = handle;
98
99#ifdef PX_COMPILER_GCC_ARM
100 // Disable IO buffering in libc to avoid putchar() bug. See:
101 // https://bugs.launchpad.net/gcc-arm-embedded/+bug/1380268
102 setvbuf(stdout, NULL, _IONBF, 0);
103#endif
104}
105
107{
108 // New line character?
109 if(data == '\n')
110 {
111 // Prepend a carriage return
112 px_uart_putchar(px_uart_stdio_handle, '\r');
113 }
114 // Send character over UART
115 px_uart_putchar(px_uart_stdio_handle, (uint8_t)data);
116
117 return 0;
118}
119
121{
122 // Receive character over UART
123 return (int)px_uart_getchar(px_uart_stdio_handle);
124}
#define NULL
NULL pointer.
Definition: px_defs.h:49
void px_uart_stdio_init(px_uart_handle_t *handle)
Initialise stdio stream to use a UART driver.
Definition: px_uart_stdio.c:94
int px_uart_stdio_putchar(char data)
Function to send a byte.
int px_uart_stdio_getchar(void)
Function to receive a byte.
void px_uart_putchar(px_uart_handle_t *handle, char data)
Write one byte.
Definition: px_uart.c:632
char px_uart_getchar(px_uart_handle_t *handle)
Read one byte.
Definition: px_uart.c:706
Define UART handle.
Definition: px_uart.h:141