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
|
#include "include.h"
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <driverlib/gpio.h>
#include <driverlib/pin_map.h>
#include <driverlib/rom.h>
#include <driverlib/sysctl.h>
#include <driverlib/timer.h>
#include <inc/hw_ints.h>
#include "inc/hw_uart.h"
#include <inc/hw_memmap.h>
#include <driverlib/uart.h>
#include "driverlib/interrupt.h"
#include "utils/uartstdio.h"
///
/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/System.h>
/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/gates/GateMutex.h>
#include <ti/sysbios/hal/Hwi.h>
#include <ti/sysbios/knl/Semaphore.h>
#include <ti/sysbios/knl/Task.h>
#include <stdbool.h>
#include <stdint.h>
/* driverlib Header files */
#include <inc/hw_ints.h>
#include <inc/hw_types.h>
#include <inc/hw_uart.h>
#include <driverlib/rom.h>
#include <driverlib/sysctl.h>
#include <driverlib/usb.h>
#include <inc/hw_memmap.h>
//#include <ti/drivers/UART.h>
/* usblib Header files */
#include <usblib/usb-ids.h>
#include <usblib/usblib.h>
#include <usblib/usbcdc.h>
#include <usblib/device/usbdevice.h>
#include <usblib/device/usbdcdc.h>
#include <usblib/device/usbddfu-rt.h>
#include <usblib/device/usbdcomp.h>
#include "Communication/CommunicationTask.h"
#include <utils/ustdlib.h>
#include "Common/Utilities/Utils.h"
///
#define U0_BAUDRATE 115200
void UARTIntHandler(UArg arg0);
void Init_U0_Interrupt()
{
//
// Configure and enable UART interrupts.
//
ROM_UARTIntClear(UART0_BASE, ROM_UARTIntStatus(UART0_BASE, false));
//ROM_UARTIntEnable(UART0_BASE, (UART_INT_OE | UART_INT_BE | UART_INT_PE |
// UART_INT_FE | UART_INT_RT | UART_INT_TX | UART_INT_RX));
//MAP_UARTIntRegister(INT_UART0,UARTIntHandler,0);
/* Install interrupt handler */
Error_Block eb;
Error_init(&eb);
Hwi_Handle hwi;
hwi = Hwi_create(INT_UART0, UARTIntHandler, NULL, &eb);// int number 21 Embedded.cfg
if (hwi == NULL)
{
System_abort("Can't create UART Hwi");
}
ROM_UARTIntEnable(UART0_BASE, /*UART_INT_TX |*/ UART_INT_RX );
ROM_UARTEnable(UART0_BASE);
UARTFIFODisable(UART0_BASE);
//
// Enable interrupts now that the application is ready to start.
//
ROM_IntEnable(INT_UART0);
IntMasterEnable();
}
void Init_U0(void)
{
//UART_Handle handle;
//UART_Params params;
// ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
// SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
// SYSCTL_CFG_VCO_480), 120000000);
// SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);//OK
//
// Enable GPIO port A which is used for UART0 pins.
// TODO: change this to whichever GPIO port you are using.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Configure the pin muxing for UART0 functions on port A0 and A1.
// This step is not necessary if your part does not support pin muxing.
// TODO: change this to select the port/pin you are using.
//
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
//
// Enable UART0 so that we can configure the clock.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
//
// Use the internal 16MHz oscillator as the UART clock source.
//
//UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
//
// Select the alternate (UART) function for these pins.
// TODO: change this to select the port/pin you are using.
//
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Initialize the UART for console I/O.
//
UARTStdioConfig(0, U0_BAUDRATE, 120000000);
//UARTprintf("InitConsole_U0 1\n");
//UARTprintf("InitConsole_U0 2\n");
/*UART_Params_init(¶ms);
params.baudRate = 115200;
params.writeDataMode = UART_DATA_BINARY;
params.readDataMode = UART_DATA_BINARY;
params.readReturnMode = UART_RETURN_FULL;
params.readEcho = UART_ECHO_OFF;
handle = UART_open(0, ¶ms);
if (!handle) {
LOG_ERROR (handle,"UART did not open");
}*/
Init_U0_Interrupt();
}
int U0_expected_message_size = 0;
int U0_current_message_size = 0;
int U0_size_bar = 0;
uint32_t Uart_tx_Counter = 0;
uint32_t Uart_rx_Counter = 0;
//*****************************************************************************
//
// The UART interrupt handler.
//
//*****************************************************************************
//void UART0IntHandler(void)
void UARTIntHandler(UArg arg0)
{
uint32_t ui32Status;
uint8_t ui8Char;
static uint8_t size[4];
//
// Get the interrrupt status.
//
ui32Status = ROM_UARTIntStatus(UART0_BASE, true);
//
// Clear the asserted interrupts.
//
ROM_UARTIntClear(UART0_BASE, ui32Status);
while(ROM_UARTCharsAvail(UART0_BASE))
{
if (U0_expected_message_size == 0)
{
size[U0_size_bar++] = (char)ROM_UARTCharGetNonBlocking(UART0_BASE);
if (U0_size_bar == 4)
{
U0_expected_message_size = *(int *)size;
if (initArray( U0_expected_message_size) == 0)
U0_expected_message_size = 0;
U0_size_bar = 0;
}
}
else
{
ui8Char = (char)ROM_UARTCharGetNonBlocking(UART0_BASE);
insertArray(ui8Char);
U0_current_message_size++;
if (U0_current_message_size == U0_expected_message_size)
{
CommunicationTaskMessageReceived(U0_current_message_size);
Uart_rx_Counter+=(4+U0_current_message_size);
U0_expected_message_size = 0;
U0_current_message_size = 0;
break;
}
}
}
}
uint32_t Uart_Tx(char *buffer,size_t length)
{
size_t sentlength = 0;
uint8_t size[4],i =0;
size[3] = (length>>24) & 0xFF;
size[2] = (length>>16) & 0xFF;
size[1] = (length>>8) & 0xFF;
size[0] = length & 0xFF;
//ret = UART_write(0, size, 4);
//if (ret == 4)
// ret = UART_write(0, buffer, length);
Uart_tx_Counter+=(4+length);
while (i < 4)
{
if (ROM_UARTSpaceAvail(UART0_BASE))
{
ROM_UARTCharPut(UART0_BASE,size[i++]);
}
}
while (sentlength < length)
{
if (ROM_UARTSpaceAvail(UART0_BASE))
{
ROM_UARTCharPut(UART0_BASE,buffer[sentlength++]);
}
}
// if (ret!=length)
// return 1;
return 0;
}
|