-
Notifications
You must be signed in to change notification settings - Fork 0
/
pic.c
208 lines (170 loc) · 4.49 KB
/
pic.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
#include <pic18fregs.h>
#include "usb_config.h"
#include "GenericTypeDefs.h"
#include <usb_stack.h>
#include <cdc.h>
#include <cdc_descriptors.h>
#define USB_INTERRUPTS 1
#define LED0 PORTBbits.RB0
#define LED1 PORTBbits.RB1
#define LED2 PORTBbits.RB2
#define LED3 PORTBbits.RB3
#define swled(LED) (((LED) = (LED) ? 0 : 1), delay_ms(500))
void delay_ms(long ms)
{
long i;
while (ms--)
for (i=0; i < 335; i++);
}
void main(void)
{
unsigned char c,x,y,led;
unsigned long l;
// Default all pins to digital
ADCON1 |= 0x0F;
// Disable USB external transceiver to allow RB1/RB2
UCFGbits.UTRDIS = 0;
// set pin to output
TRISBbits.TRISB0 = 0;
TRISBbits.TRISB1 = 0;
TRISBbits.TRISB2 = 0;
TRISBbits.TRISB3 = 0;
LED0 = 1;
LED1 = LED2 = LED3 = 0;
// TODO: Remove magic with macro
usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS);
usb_start();
initCDC();
usbbufflush();
do {
if (!TestUsbInterruptEnabled())
USBDeviceTasks();
switch(usb_device_state){
case DETACHED_STATE:
LED1 = 0; LED2 = 0; LED3 = 0;
case ATTACHED_STATE:
LED1 = 0; LED2 = 0; LED3 = 1;
case POWERED_STATE:
LED1 = 0; LED2 = 1; LED3 = 0;
case DEFAULT_STATE:
LED1 = 0; LED2 = 1; LED3 = 1;
case ADR_PENDING_STATE:
LED1 = 1; LED2 = 0; LED3 = 0;
case ADDRESS_STATE:
LED1 = 1; LED2 = 0; LED3 = 1;
case CONFIGURED_STATE:
LED1 = 1; LED2 = 1; LED3 = 0;
}
} while (usb_device_state < CONFIGURED_STATE);
while(1);
#if 0
// Initiate Osc with 8MHz (only relevant if you use the internal oscillator)
//OSCCON = 0x70;
initCDC();
// TODO: Remove magic with macro
usb_init(cdc_device_descriptor, cdc_config_descriptor, cdc_str_descs, USB_NUM_STRINGS);
usb_start();
#if defined (USB_INTERRUPTS)
//EnableUsbInterrupt(USB_TRN + USB_SOF + USB_UERR + USB_URST);
EnableUsbInterrupt(USB_STALL + USB_IDLE + USB_TRN + USB_ACTIV + USB_SOF + USB_UERR + USB_URST);
EnableUsbInterrupts();
#endif
usbbufflush(); //flush USB input buffer system
led = UCFGbits.UTRDIS;
LED0=0;
LED1=0;
LED2=0;
LED3=0;
while(1){
LED1 = LED1 ? 0 : 1;
for(l = 0; l < 10; ++l);
}
LED0=1;
LED1=1;
LED2=1;
LED3=1;
delay_ms(500);
#if 0
while(1)
{
LED0 = 1;
delay_ms(250);
LED0 = 0;
delay_ms(250);
}
#endif
LED0 = 0;
LED1 = 0;
LED2 = 0;
LED3 = 0;
delay_ms(500);
while(1);
do
{ LED2=1;
LED3 = 0;
#ifndef USB_INTERRUPTS
//service USB tasks
//Guaranteed one pass in polling mode
//even when usb_device_state == CONFIGURED_STATE
if (!TestUsbInterruptEnabled())
{
USBDeviceTasks();
LED3 = 1;
}
#endif
if ((usb_device_state < DEFAULT_STATE))
{ // JTR2 no suspendControl available yet || (USBSuspendControl==1) ){
LED0=1;
LED1=0;
} else if (usb_device_state < CONFIGURED_STATE)
{
LED0=1;
LED1=1;
}else if((usb_device_state == CONFIGURED_STATE))
{
LED0=0;
LED1=1;
}
LED2=0;
delay_ms(1000);
} while (usb_device_state < CONFIGURED_STATE);
LED0 = 0;
LED1 = 0;
LED2 = 1;
#if 1
while(1)
{
LED0 = led;
led = led ? 0 : 1;
#ifndef USB_INTERRUPTS
//service USB tasks
//Guaranteed one pass in polling mode
//even when usb_device_state == CONFIGURED_STATE
if (!TestUsbInterruptEnabled())
USBDeviceTasks();
#endif
usbbufservice(); //service USB buffer system
if (usbbufgetbyte(&c) == 1)
{
WaitInReady();
cdc_In_buffer[0] = c; //answer OK
putUnsignedCharArrayUsbUsart(cdc_In_buffer, 1);
}
}
#endif
// Disable USB external transceiver to allow RB1/RB2
UCFGbits.UTRDIS = 0;
// set pin to output
TRISBbits.TRISB0 = 0;
TRISBbits.TRISB1 = 0;
TRISBbits.TRISB2 = 0;
// start a loop blinking the led
while(1)
{
LED0 = 1;
delay_ms(250);
LED0 = 0;
delay_ms(250);
}
#endif
}