-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.c
109 lines (94 loc) · 2.39 KB
/
Main.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
/*
* Copyright (C) 2017 Johan Bergkvist
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include <xc.h>
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"
#include "usb_device.h"
#include "usb_device_generic.h"
#include "Usb.h"
#include "Pic16F.h"
#include "Pic18F.h"
#include "Pic32F.h"
#include "Pins.h"
//---------------------------------------------------------------------------
// AllocatePins16And18
//---------------------------------------------------------------------------
void AllocatePins16And18(void)
{
static uint8_t pins_allocated = 0;
if (pins_allocated != 0)
{
return;
}
pins_allocated = 1;
//
// Set output/inputs for PIC16 and PIC18 devices.
//
VDD_SetDigitalOutput();
PGD_SetDigitalOutput();
PGC_SetDigitalOutput();
//
// Initialise the outputs.
//
VDD = 0;
VPP = 0;
PGC = 0;
PGD_OUT = 0;
}
//---------------------------------------------------------------------------
// ProcessMessage
//---------------------------------------------------------------------------
void ProcessMessage(unsigned char *message, unsigned char length)
{
switch (message[0] & 0xf0)
{
case 0x00:
AllocatePins16And18();
ProcessMessage18(message, length);
break;
case 0x10:
ProcessMessage32(message, length);
break;
case 0x20:
AllocatePins16And18();
ProcessMessage16(message, length);
break;
}
}
//---------------------------------------------------------------------------
// main
//---------------------------------------------------------------------------
void main(void)
{
SYSTEM_Initialize();
USBDeviceInit();
//
// Initialise the outputs.
//
VPP = 0;
MCLR = 1;
//
// Set output/inputs. Pins shared with PIC32 are set after we know what
// device is being programmed.
//
VPP_SetDigitalOutput();
MCLR_SetDigitalOutput();
while(1)
{
USBDeviceTasks();
if((USBDeviceState < CONFIGURED_STATE) || (USBSuspendControl == 1))
{
continue;
}
if(!USBHandleBusy(out_handle))
{
unsigned char length = (unsigned char)USBHandleGetLength(out_handle);
ProcessMessage(out_packet, length);
out_handle = USBGenRead(USBGEN_EP_NUM, out_packet, USBGEN_EP_SIZE);
}
}
}