-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyuart.c
357 lines (317 loc) · 11.5 KB
/
myuart.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* myserial.c
*
* Created on: 2017/5/25
*/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "myuart.h"
#include "dvfs.h"
#include "portable.h"
#ifdef __TOOLS_MSP__
#include "driverlib.h"
#elif defined(__STM32__)
#include STM32_HAL_HEADER
#else
#error "Please defined __MSP430__, __MSP432__ or __STM32__ according to the target board"
#endif
int uartsetup = 0;
#ifdef __MSP430__
typedef EUSCI_A_UART_initParam EUSCI_CONFIG_PARAMS;
#elif defined(__MSP432__)
typedef eUSCI_UART_Config EUSCI_CONFIG_PARAMS;
#endif
#ifdef __TOOLS_MSP__
// The following structure will configure the EUSCI_A port to run at 9600 baud from an 1~24MHz ACLK
// The baud rate values were calculated at: http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSP430BaudRateConverter/index.html
// See also https://dev.ti.com/tirex/explore/node?node=ACmvnDrzuRlhbVcxPmBGTQ__z-lQYNj__LATEST for an MSP432 example
const EUSCI_CONFIG_PARAMS UartParams[] = {
{//1MHz
EUSCI_A_UART_CLOCKSOURCE_SMCLK,
6, // clockPrescalar
8, // firstModReg
17, // secondModReg
EUSCI_A_UART_NO_PARITY,
EUSCI_A_UART_LSB_FIRST,
EUSCI_A_UART_ONE_STOP_BIT,
EUSCI_A_UART_MODE,
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION
},{//2.66MHz
EUSCI_A_UART_CLOCKSOURCE_SMCLK,
17, // clockPrescalar
5, // firstModReg
2, // secondModReg
EUSCI_A_UART_NO_PARITY,
EUSCI_A_UART_LSB_FIRST,
EUSCI_A_UART_ONE_STOP_BIT,
EUSCI_A_UART_MODE,
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION
},{//3.5MHz
EUSCI_A_UART_CLOCKSOURCE_SMCLK,
22, // clockPrescalar
12, // firstModReg
107, // secondModReg
EUSCI_A_UART_NO_PARITY,
EUSCI_A_UART_LSB_FIRST,
EUSCI_A_UART_ONE_STOP_BIT,
EUSCI_A_UART_MODE,
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION
},{//4MHz
EUSCI_A_UART_CLOCKSOURCE_SMCLK,
26, // clockPrescalar
0, // firstModReg
214, // secondModReg
EUSCI_A_UART_NO_PARITY,
EUSCI_A_UART_LSB_FIRST,
EUSCI_A_UART_ONE_STOP_BIT,
EUSCI_A_UART_MODE,
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION
},{//5.33MHz
EUSCI_A_UART_CLOCKSOURCE_SMCLK,
34, // clockPrescalar
11, // firstModReg
17, // secondModReg
EUSCI_A_UART_NO_PARITY,
EUSCI_A_UART_LSB_FIRST,
EUSCI_A_UART_ONE_STOP_BIT,
EUSCI_A_UART_MODE,
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION
},{//7MHz
EUSCI_A_UART_CLOCKSOURCE_SMCLK,
45, // clockPrescalar
9, // firstModReg
17, // secondModReg
EUSCI_A_UART_NO_PARITY,
EUSCI_A_UART_LSB_FIRST,
EUSCI_A_UART_ONE_STOP_BIT,
EUSCI_A_UART_MODE,
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION
},{//8MHz
EUSCI_A_UART_CLOCKSOURCE_SMCLK,
52, // clockPrescalar
1, // firstModReg
73, // secondModReg
EUSCI_A_UART_NO_PARITY,
EUSCI_A_UART_LSB_FIRST,
EUSCI_A_UART_ONE_STOP_BIT,
EUSCI_A_UART_MODE,
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION
},{//16MHz
EUSCI_A_UART_CLOCKSOURCE_SMCLK,
104, // clockPrescalar
2, // firstModReg
182, // secondModReg
EUSCI_A_UART_NO_PARITY,
EUSCI_A_UART_LSB_FIRST,
EUSCI_A_UART_ONE_STOP_BIT,
EUSCI_A_UART_MODE,
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION
},{//12MHz
EUSCI_A_UART_CLOCKSOURCE_SMCLK,
78, // clockPrescalar
2, // firstModReg
0, // secondModReg
EUSCI_A_UART_NO_PARITY,
EUSCI_A_UART_LSB_FIRST,
EUSCI_A_UART_ONE_STOP_BIT,
EUSCI_A_UART_MODE,
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION
},{//24MHz (24000000 Hz)
EUSCI_A_UART_CLOCKSOURCE_SMCLK,
13, // clockPrescalar
0, // firstModReg
37, // secondModReg
EUSCI_A_UART_NO_PARITY,
EUSCI_A_UART_LSB_FIRST,
EUSCI_A_UART_ONE_STOP_BIT,
EUSCI_A_UART_MODE,
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION
},{//48MHz (48000000 Hz)
EUSCI_A_UART_CLOCKSOURCE_SMCLK,
26, // clockPrescalar
0, // firstModReg
111, // secondModReg
EUSCI_A_UART_NO_PARITY,
EUSCI_A_UART_LSB_FIRST,
EUSCI_A_UART_ONE_STOP_BIT,
EUSCI_A_UART_MODE,
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION
}};
#elif defined(__STM32__)
extern UART_HandleTypeDef huart2;
#endif
void uart_putc(char c) {
#ifdef __MSP430__
EUSCI_A_UART_transmitData(EUSCI_A0_BASE, (uint8_t)c);
#elif defined(__MSP432__)
MAP_UART_transmitData(EUSCI_A0_BASE, (uint8_t)c);
#endif
}
void print2uart(const char* format,...)
{
const char *traverse;
int i;
long l;
unsigned long ul;
char *s;
//Module 1: Initializing Myprintf's arguments
va_list arg;
va_start(arg, format);
for(traverse = format; *traverse != '\0'; traverse++)
{
while( *traverse != '%' && *traverse != '\0' )
{
uart_putc(*traverse);
traverse++;
}
if(*traverse == '\0')
break;
traverse++;
//Module 2: Fetching and executing arguments
switch(*traverse)
{
case 'c' :
i = va_arg(arg,int); //Fetch char argument
uart_putc(i);
break;
case 'L' :
l = va_arg(arg,long); //Fetch Decimal/Integer argument
if(l<0)
{
l = -l;
uart_putc('-');
}
print2uart(convertl(l,10));
break;
case 'l' :
ul = va_arg(arg,unsigned long); //Fetch Decimal/Integer argument
print2uart(convertl(ul,10));
break;
case 'd' :
i = va_arg(arg,int); //Fetch Decimal/Integer argument
if(i<0)
{
i = -i;
uart_putc('-');
}
print2uart(convert(i,10));
break;
case 's':
s = va_arg(arg,char *); //Fetch string
print2uart(s);
break;
case 'x':
i = va_arg(arg,unsigned int); //Fetch Hexadecimal representation
print2uart(convert(i,16));
break;
}
}
//Module 3: Closing argument list to necessary clean-up
va_end(arg);
}
static char print2uart_new_buf[64];
void print2uart_new(const char* format,...)
{
if (uartsetup == 0) {
return;
}
va_list arg;
va_start(arg, format);
vsnprintf(print2uart_new_buf, 64, format, arg);
print2uartlength(print2uart_new_buf, strlen(print2uart_new_buf));
va_end(arg);
}
void dummyprint(const char* format,...)
{
return;
}
void print2uartlength(char* str,int length)
{
#ifdef __TOOLS_MSP__
int i;
for(i = 0; i < length; i++)
{
uart_putc(*(str+i));
}
#elif defined(__STM32__)
HAL_UART_Transmit(&huart2, (uint8_t*)str, strlen(str), 0xffff);
#endif
}
char *convert(unsigned int num, int base)
{
static char Representation[]= "0123456789ABCDEF";
static char buffer[50];
char *ptr;
ptr = &buffer[49];
*ptr = '\0';
do
{
*--ptr = Representation[num%base];
num /= base;
}while(num != 0);
return(ptr);
}
char *convertl(unsigned long num, int base)
{
static char Representation[]= "0123456789ABCDEF";
static char buffer[50];
char *ptr;
ptr = &buffer[49];
*ptr = '\0';
do
{
*--ptr = Representation[num%base];
num /= base;
}while(num != 0);
return(ptr);
}
/* Initialize serial */
void uartinit()
{
if(uartsetup == 0){
#ifdef __MSP430__
// Configure UART
EUSCI_A_UART_initParam param = UartParams[FreqLevel-1];
if(STATUS_FAIL == EUSCI_A_UART_init(EUSCI_A0_BASE, ¶m))
return;
EUSCI_A_UART_enable(EUSCI_A0_BASE);
EUSCI_A_UART_clearInterrupt(EUSCI_A0_BASE,
EUSCI_A_UART_RECEIVE_INTERRUPT);
// Enable USCI_A0 RX interrupt
EUSCI_A_UART_enableInterrupt(EUSCI_A0_BASE,
EUSCI_A_UART_RECEIVE_INTERRUPT); // Enable interrupt
// Enable globale interrupt
__enable_interrupt();
// Select UART TXD on P2.0
GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2, GPIO_PIN0, GPIO_SECONDARY_MODULE_FUNCTION);
#elif defined(__MSP432__)
/* Selecting P1.2 and P1.3 in UART mode */
MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);
eUSCI_UART_Config param = UartParams[FreqLevel-1];
/* Configuring UART Module */
MAP_UART_initModule(EUSCI_A0_BASE, ¶m);
/* Enable UART module */
MAP_UART_enableModule(EUSCI_A0_BASE);
/* Enabling interrupts */
MAP_UART_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT);
MAP_Interrupt_enableInterrupt(INT_EUSCIA0);
MAP_Interrupt_enableSleepOnIsrExit();
MAP_Interrupt_enableMaster();
#endif
uartsetup = 1;
}
}
#ifdef __MSP432__
// triggered when keyboard input is received in minicom
void EUSCIA0_IRQHandler(void) {
// Ref: MSP432 example uart_pc_echo_12mhz_brclk
uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_BASE);
MAP_UART_clearInterruptFlag(EUSCI_A0_BASE, status);
if(status & EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG)
{
MAP_UART_transmitData(EUSCI_A0_BASE, MAP_UART_receiveData(EUSCI_A0_BASE));
}
}
#endif