aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Code_Composer/twine_usblib_demo/main.c
blob: 84cbf46b761bd07e3ddafdcc7d1c25b7b06bfb70 (plain)
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
#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"

//*****************************************************************************
//
// 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;


//*****************************************************************************
//
// 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);

     clear();

     MessageContainer* container = message_container__unpack(NULL, length, (uint8_t*)buffer);


    if (container->type == MESSAGE_TYPE__CalculateRequest)
    {
             CalculateRequest* request = calculate_request__unpack(NULL, container->data.len, container->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;

             MessageContainer responseContainer = MESSAGE_CONTAINER__INIT;
             responseContainer.completed = true;
             responseContainer.token = container->token;
             responseContainer.has_completed = true;
             responseContainer.has_data = true;
             responseContainer.has_type = true;
             responseContainer.type = MESSAGE_TYPE__CalculateResponse;

             uint8_t* d = malloc(calculate_response__get_packed_size(&response));
             size_t size = calculate_response__pack(&response, d);
             responseContainer.data.data = d;
             responseContainer.data.len = size;

             uint8_t* output = malloc(message_container__get_packed_size(&responseContainer));
             size = message_container__pack(&responseContainer, output);

             writeLine("Sending Response: ");
             writeFloat(response.sum);

             SendChars(output, size);
    }

    free(container);

//    Person* p = person__unpack (NULL, length, (uint8_t*)buffer);
//    void* buf = malloc (length);
//    person__pack (p, buf);
//    SendChars(buf,length);
//    free(p);
//    free(buf);
}

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);

    RegisterReceiveCallback(&receive_callback);
    StartUSB(ui32SysClock);

	while(1){};
}