-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdio_UART.c
117 lines (90 loc) · 2.87 KB
/
stdio_UART.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Water Tower Monitor
*
* Copyright (c) 2015, longfeng.xiao <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <cmsis_os.h>
#include "Driver_UART.h"
//-------- <<< Use Configuration Wizard in Context Menu >>> --------------------
// <h>STDIN UART Interface
// <o>Connect to hardware via Driver_UART# <0-255>
// <i>Select driver control block for UART interface
#define UART_DRV_NUM 1
// <o>Baudrate
#define UART_BAUDRATE 115200
// </h>
#define _UART_Driver_(n) Driver_UART##n
#define UART_Driver_(n) _UART_Driver_(n)
extern ARM_DRIVER_UART UART_Driver_(UART_DRV_NUM);
#define ptrUART (&UART_Driver_(UART_DRV_NUM))
extern int stdin_getchar (void);
extern int stderr_putchar (int ch);
extern int stdout_putchar (int ch);
/**
Initialize stdin
\return 0 on success, or -1 on error.
*/
int stdioInit (void) {
int32_t status;
status = ptrUART->Initialize(NULL, 0);
if (status != ARM_UART_OK) return (-1);
status = ptrUART->PowerControl(ARM_POWER_FULL);
if (status != ARM_UART_OK) return (-1);
status = ptrUART->Configure(UART_BAUDRATE, 8, ARM_UART_PARITY_NONE,
ARM_UART_STOP_BITS_1,
ARM_UART_FLOW_CONTROL_NONE);
if (status != ARM_UART_OK) return (-1);
return (0);
}
/**
Get a character from stdin
\return The next character from the input, or -1 on read error.
*/
int stdin_getchar (void) {
uint8_t buf[1];
if (ptrUART->ReadData(buf, 1) == 0) {
return (-1);
}
return (buf[0]);
}
/**
Put a character to the stderr
\param[in] ch Character to output
\return The character written, or -1 on write error.
*/
int stderr_putchar (int ch) {
uint8_t buf[1];
buf[0] = ch;
while (ptrUART->WriteData(buf, 1) != 1) {
osThreadYield();
}
return (ch);
}
/**
Put a character to the stdout
\param[in] ch Character to output
\return The character written, or -1 on write error.
*/
int stdout_putchar (int ch) {
uint8_t buf[1];
buf[0] = ch;
while (ptrUART->WriteData(buf, 1) != 1) {
osThreadYield();
}
return (ch);
}