app_serial.c
7.63 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
*********************************************************************************************************
* EXAMPLE CODE
*
* (c) Copyright 2003-2009; Micrium, Inc.; Weston, FL
*
* All rights reserved. Protected by international copyright laws.
* Knowledge of the source code may NOT be used to develop a similar product.
* Please help us continue to provide the Embedded community with the finest
* software available. Your honesty is greatly appreciated.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* uC/Serial
* Application Code
*
* Filename : app_serial.c
* Version : V1.00
* Programmer(s) : FT
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define APP_SERIAL_MODULE
#include <app_serial.h>
/*
*********************************************************************************************************
* ENABLE
*********************************************************************************************************
*/
#if (APP_CFG_SERIAL_EN == DEF_ENABLED)
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
#if (APP_SERIAL_CFG_TRACE_EN == DEF_ENABLED)
static SERIAL_IF_NBR App_SerTraceIF_Nbr;
#endif
/*
*********************************************************************************************************
* FUNCTION PROTOTYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* App_SerialInit()
*
* Description : Initialize Serial module
*
* Argument(s) : none.
*
* Caller(s) : Application
*
* Return(s) : none.
*
* Note(s) : none.
*********************************************************************************************************
*/
CPU_BOOLEAN App_SerialInit (void)
{
SERIAL_ERR err;
#if (APP_SERIAL_CFG_TRACE_EN == DEF_ENABLED)
SERIAL_IF_CFG uart_cfg;
#endif
Serial_Init(); /* Initial Serial module */
Serial_DevDrvAdd((CPU_CHAR *)"USART3", /* Add serial interface/device. */
(SERIAL_DEV_CFG *)&SerialDevCfg_STM32_USART3,
(CPU_SIZE_T ) 52u,
(CPU_SIZE_T ) 52u,
(SERIAL_ERR *)&err);
if (err != SERIAL_ERR_NONE) {
return (DEF_FAIL);
}
#if (APP_SERIAL_CFG_TRACE_EN == DEF_ENABLED)
uart_cfg.Baudrate = SERIAL_BAUDRATE_115200;
uart_cfg.DataBits = SERIAL_DATABITS_8;
uart_cfg.StopBits = SERIAL_STOPBITS_1;
uart_cfg.Parity = SERIAL_PARITY_NONE;
uart_cfg.FlowCtrl = SERIAL_FLOW_CTRL_NONE;
App_SerTraceIF_Nbr = Serial_Open((CPU_CHAR *)APP_SERIAL_CFG_TRACE_PORT_NAME,
(SERIAL_IF_CFG *)&uart_cfg,
(SERIAL_ERR *)&err);
if (err != SERIAL_ERR_NONE) {
return (DEF_FAIL);
}
Serial_SetLineDrv((SERIAL_IF_NBR ) App_SerTraceIF_Nbr,
(SERIAL_LINE_DRV_API *)&SerialLine_TTY ,
(SERIAL_ERR *)&err);
Serial_Wr((SERIAL_IF_NBR )App_SerTraceIF_Nbr,
(void *)"\n\n",
(CPU_SIZE_T )2,
(CPU_INT32U )0,
(SERIAL_ERR *)&err);
if (err != SERIAL_ERR_NONE) {
return (DEF_FAIL);
}
#endif
return (DEF_OK);
}
/*
*********************************************************************************************************
* App_SerPrintf()
*
* Description : Output 'printf()' statements
*
* Argument(s) : p_fmt Pointer to format string.
*
* Return(s) : The number of octets written.
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (APP_SERIAL_CFG_TRACE_EN == DEF_ENABLED)
void App_SerPrintf (CPU_CHAR *p_fmt,
...)
{
CPU_CHAR str[80u + 1u];
CPU_SIZE_T len;
SERIAL_ERR err;
va_list vArgs;
va_start(vArgs, p_fmt);
vsprintf((char *)str,
(char const *)p_fmt,
vArgs);
va_end(vArgs);
len = Str_Len(str);
Serial_Wr((SERIAL_IF_NBR )App_SerTraceIF_Nbr,
(void *)&str[0],
(CPU_SIZE_T )len,
(CPU_INT32U )0,
(SERIAL_ERR *)&err);
}
#endif
/*
*********************************************************************************************************
* App_SerPrintf()
*
* Description : Read a string from the serial terminal.
*
* Argument(s) : p_str Pointer to string.
*
* len String length.
*
* Return(s) : The number of octets written.
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (APP_SERIAL_CFG_TRACE_EN == DEF_ENABLED)
void App_SerStrRd (CPU_CHAR *p_str,
CPU_SIZE_T len)
{
SERIAL_ERR err;
Serial_Rd((SERIAL_IF_NBR )App_SerTraceIF_Nbr,
(void *)p_str,
(CPU_SIZE_T )len,
(CPU_INT32U )0,
(SERIAL_ERR *)&err);
}
#endif
/*
*********************************************************************************************************
* App_SerStrWr()
*
* Description : Write an string in serial interface.
*
* Argument(s) : p_fmt Pointer to format string.
*
* Return(s) : The number of octets written.
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
#if (APP_SERIAL_CFG_TRACE_EN == DEF_ENABLED)
void App_SerStrWr (CPU_CHAR *p_str)
{
CPU_SIZE_T len;
SERIAL_ERR err;
if (p_str == (CPU_CHAR *)0) {
return;
}
len = Str_Len(p_str);
Serial_Wr((SERIAL_IF_NBR )App_SerTraceIF_Nbr,
(void *)p_str,
(CPU_SIZE_T )len,
(CPU_INT32U )0,
(SERIAL_ERR *)&err);
}
#endif
#endif