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
|
#include <protobuf-c/person-pb-c.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_uart.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "driverlib/usb.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "drivers/pinout.h"
#include "usblib/usblib.h"
#include "usblib/usbcdc.h"
#include "usblib/usb-ids.h"
#include "usblib/device/usbdevice.h"
#include "usblib/device/usbdcdc.h"
#include "utils/ustdlib.h"
#include "usb_serial_adapter.h"
#include "graphics_adapter.h"
#include "MessageContainer.pb-c.h"
#include "CalculateRequest.pb-c.h"
#include "CalculateResponse.pb-c.h"
#include "ProgressRequest.pb-c.h";
#include "ProgressResponse.pb-c.h";
//*****************************************************************************
//
// The system tick rate expressed both as ticks per second and a millisecond
// period.
//
//*****************************************************************************
#define TICKS_PER_SECOND 100
//*****************************************************************************
//
// Global system tick counter
//
//*****************************************************************************
static volatile uint32_t g_ui32SysTickCount = 0;
// Flags used to pass commands from interrupt context to the main loop.
static volatile uint32_t g_ui32Flags;
MessageContainer createContainer(MessageType type, char* token, protobuf_c_boolean completed, void* response, size_t (*packPtr)(void*, uint8_t*), size_t (*sizePtr)(void*));
//*****************************************************************************
//
// Interrupt handler for the system tick counter.
//
//*****************************************************************************
void SysTickHandler(void)
{
// Update our system time.
g_ui32SysTickCount++;
}
//*****************************************************************************
//
// Interrupt handler for the UART which we are redirecting via USB.
//
//*****************************************************************************
void USB0Handler(void)
{
InitUSB();
}
void receive_callback(char* buffer, size_t length)
{
//SendChars(buffer,length);
//draw_string(buffer, length);
//draw_image((uint8_t *)buffer);
writeLine("Received ");
writeFloat(length);
writeString(" bytes");
writeLine("Parsing message...");
MessageContainer* requestContainer = message_container__unpack(NULL, length, (uint8_t*)buffer);
MessageContainer responseContainer;
if (requestContainer->type == MESSAGE_TYPE__CalculateRequest)
{
CalculateRequest* request = calculate_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);
writeLine("Calculate Request: ");
writeFloat(request->a);
writeString(" + ");
writeFloat(request->b);
CalculateResponse response = CALCULATE_RESPONSE__INIT;
response.sum = request->a + request->b;
response.has_sum = true;
responseContainer = createContainer(MESSAGE_TYPE__CalculateResponse, requestContainer->token, true, &response, &calculate_response__pack, &calculate_response__get_packed_size);
writeLine("Sending Response: ");
writeFloat(response.sum);
free(request);
}
else if (requestContainer->type == MESSAGE_TYPE__ProgressRequest)
{
ProgressRequest* request = progress_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);
writeLine("Progress Request...");
ProgressResponse response = PROGRESS_RESPONSE__INIT;
response.has_progress = true;
int i = 0;
for (i = 0; i < request->amount; i++)
{
response.progress = i;
responseContainer = createContainer(MESSAGE_TYPE__ProgressResponse, requestContainer->token, false, &response, &progress_response__pack, &progress_response__get_packed_size);
uint8_t* container_buffer = malloc(message_container__get_packed_size(&responseContainer));
size_t container_size = message_container__pack(&responseContainer, container_buffer);
writeLine("Sending Progress: ");
writeFloat(response.progress);
SendChars((char*)container_buffer, container_size);
int co = 0;
for (co = 0; co < request->delay; co++)
{
__delay_cycles(1000000);
}
}
responseContainer = createContainer(MESSAGE_TYPE__ProgressResponse, requestContainer->token, true, &response, &progress_response__pack, &progress_response__get_packed_size);
uint8_t* container_buffer = malloc(message_container__get_packed_size(&responseContainer));
size_t container_size = message_container__pack(&responseContainer, container_buffer);
writeLine("Progress Completed!");
SendChars((char*)container_buffer, container_size);
return;
}
uint8_t* container_buffer = malloc(message_container__get_packed_size(&responseContainer));
size_t container_size = message_container__pack(&responseContainer, container_buffer);
SendChars((char*)container_buffer, container_size);
free(container_buffer);
free(requestContainer);
}
MessageContainer createContainer(MessageType type, char* token, protobuf_c_boolean completed, void* response, size_t (*packPtr)(void*, uint8_t*), size_t (*sizePtr)(void*))
{
MessageContainer container = MESSAGE_CONTAINER__INIT;
container.completed = completed;
container.token = token;
container.has_completed = true;
container.has_data = true;
container.has_type = true;
container.type = type;
uint8_t* response_buffer = malloc((*sizePtr)(response));
size_t response_size = (*packPtr)(response, response_buffer);
container.data.data = response_buffer;
container.data.len = response_size;
free(response);
return container;
}
int main(void)
{
uint32_t ui32SysClock, ui32PLLRate;
// Set the system clock to run at 120MHz from the PLL.
ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_480), 120000000);
// Configure the device pins.
PinoutSet();
// Save the PLL rate used by this application.
SysCtlVCOGet(SYSCTL_XTAL_25MHZ, &ui32PLLRate);
// Enable the system tick.
ROM_SysTickPeriodSet(ui32SysClock / TICKS_PER_SECOND);
ROM_SysTickIntEnable();
ROM_SysTickEnable();
init_graphics(ui32SysClock);
clear();
RegisterReceiveCallback(&receive_callback);
StartUSB(ui32SysClock);
while(1){};
}
|