1: /*******************************************************************************
2: Copyright 2016 Microchip Technology Inc. (www.microchip.com)
3:
4: Licensed under the Apache License, Version 2.0 (the "License");
5: you may not use this file except in compliance with the License.
6: You may obtain a copy of the License at
7:
8: http://www.apache.org/licenses/LICENSE-2.0
9:
10: Unless required by applicable law or agreed to in writing, software
11: distributed under the License is distributed on an "AS IS" BASIS,
12: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: See the License for the specific language governing permissions and
14: limitations under the License.
15:
16: To request to license the code under the MLA license (www.microchip.com/mla_license),
17: please contact mla_licensing@microchip.com
18: *
19: Firmware has modified by M_shi_Lab
20: *******************************************************************************/
21:
22: /** INCLUDES *******************************************************/
23: #include "system.h"
24:
25: #include <stdint.h>
26: #include <string.h>
27: #include <stddef.h>
28:
29: #include "usb.h"
30:
31: #include "app_led_usb_status.h"
32: #include "app_device_cdc_basic.h"
33: #include "usb_config.h"
34:
35: #define _XTAL_FREQ 48000000
36:
37: /** VARIABLES ******************************************************/
38:
39: //static bool buttonPressed;
40: //static char buttonMessage[] = "Button pressed.\r\n";
41: static uint8_t readBuffer[CDC_DATA_OUT_EP_SIZE];
42: static uint8_t writeBuffer[CDC_DATA_IN_EP_SIZE];
43:
44: void PIC3Start(); //A
45: void PIC2_Int(); //B
46: void PIC2_Cont(); //C
47: void PIC1_Start(); //D
48: void USB_PIC_Cont_1st(); //E
49: void USB_PIC_Cont_2nd(); //F
50: void USB_PIC_BKT_Down(); //G
51: void USB_PIC_BKT_Up(); //H
52: void Filter_Open(); //I
53: void Filter_Close(); //J
54: void SS_UpTo_8000(); //K
55: void Captor();
56: void SpeedUp();
57: void SpeedDown();
58: //Emmergency
59: void USB_PIC_Cont_1st2();
60: void USB_PIC_Cont_2nd2();
61: void USB_PIC_BKT_Down2();
62: void USB_PIC_BKT_Up2();
63: /*********************************************************************
64: * Function: void APP_DeviceCDCBasicDemoInitialize(void);
65: *
66: * Overview: Initializes the demo code
67: *
68: * PreCondition: None
69: *
70: * Input: None
71: *
72: * Output: None
73: *
74: ********************************************************************/
75: void APP_DeviceCDCBasicDemoInitialize()
76: {
77: line_coding.bCharFormat = 0;
78: line_coding.bDataBits = 8;
79: line_coding.bParityType = 0;
80: line_coding.dwDTERate = 9600;
81:
82: // buttonPressed = false;
83: }
84:
85: /*********************************************************************
86: * Function: void APP_DeviceCDCBasicDemoTasks(void);
87: *
88: * Overview: Keeps the demo running.
89: *
90: * PreCondition: The demo should have been initialized and started via
91: * the APP_DeviceCDCBasicDemoInitialize() and APP_DeviceCDCBasicDemoStart() demos
92: * respectively.
93: *
94: * Input: None
95: *
96: * Output: None
97: *
98: ********************************************************************/
99: void APP_DeviceCDCBasicDemoTasks()
100: {
101: TRISB = 0b00000000;
102: TRISC = 0b00000000;
103: PORTB = 0b00000000;
104: PORTC = 0b00000000;
105:
106:
107: char Commander;
108: int i;
109: RA3 = 1;
110: /* If the USB device isn't configured yet, we can't really do anything
111: * else since we don't have a host to talk to. So jump back to the
112: * top of the while loop. */
113: if( USBGetDeviceState() < CONFIGURED_STATE )
114: {
115: return;
116: }
117:
118: /* If we are currently suspended, then we need to see if we need to
119: * issue a remote wakeup. In either case, we shouldn't process any
120: * keyboard commands since we aren't currently communicating to the host
121: * thus just continue back to the start of the while loop. */
122: if( USBIsDeviceSuspended()== true )
123: {
124: return;
125: }
126:
127: /* If the user has pressed the button associated with this demo, then we
128: * are going to send a "Button Pressed" message to the terminal.
129: */
130: if(BUTTON_IsPressed(BUTTON_DEVICE_CDC_BASIC_DEMO) == true)
131: {
132: /* Make sure that we only send the message once per button press and
133: * not continuously as the button is held.
134: */
135: // if(buttonPressed == false)
136: {
137: /* Make sure that the CDC driver is ready for a transmission.
138: */
139: if(mUSBUSARTIsTxTrfReady() == true)
140: {
141: // putrsUSBUSART(buttonMessage);
142: // buttonPressed = true;
143: }
144: }
145: }
146: else
147: {
148: /* If the button is released, we can then allow a new message to be
149: * sent the next time the button is pressed.
150: */
151: //buttonPressed = false;
152: }
153:
154: /* Check to see if there is a transmission in progress, if there isn't, then
155: * we can see about performing an echo response to data received.
156: */
157: if( USBUSARTIsTxTrfReady() == true)
158: {
159: uint8_t i;
160: uint8_t numBytesRead;
161:
162: numBytesRead = getsUSBUSART(readBuffer, sizeof(readBuffer));
163:
164: /* For every byte that was read... */
165: for(i=0; i<numBytesRead; i++)
166: {
167: switch(readBuffer[i])
168: {
169: /* If we receive new line or line feed commands, just echo
170: * them direct.
171: */
172: case 0x0A:
173: case 0x0D:
174: // writeBuffer[i] = readBuffer[i];
175: // break;
176:
177: /* If we receive something else, then echo it plus one
178: * so that if we receive 'a', we echo 'b' so that the
179: * user knows that it isn't the echo enabled on their
180: * terminal program.
181: */
182: default:
183: writeBuffer[i] = readBuffer[i];
184: // break;
185:
186: //default:
187:
188: Commander = readBuffer[0];
189:
190: if(Commander=='A')
191: {
192: PIC3Start();
193: }
194: elseif(Commander=='B')
195: {
196: PIC2_Cont();
197: }
198: elseif(Commander=='C')
199: {
200: PIC2_Int();
201: }
202: elseif(Commander == 'D')
203: {
204: PIC1_Start();
205: }
206: elseif(Commander=='E')
207: {
208: USB_PIC_Cont_1st();
209: }
210: elseif(Commander=='F')
211: {
212: USB_PIC_Cont_2nd();
213: }
214: elseif(Commander=='G')
215: {
216: USB_PIC_BKT_Down();
217: }
218: elseif(Commander=='H')
219: {
220: USB_PIC_BKT_Up();
221: }
222: elseif(Commander=='I')
223: {
224: Filter_Open();
225: }
226: elseif(Commander=='J')
227: {
228: Filter_Close();
229: }
230: elseif(Commander=='K')
231: {
232: SS_UpTo_8000();
233: }
234: break;
235: }
236: if(numBytesRead > 0)
237: {
238: /* After processing all of the received data, we need to send out
239: * the "echo" data now.
240: */
241: putUSBUSART(writeBuffer,numBytesRead);
242: }
243:
244:
245: }
246: }
247: if(RA3 == 0)
248: {
249: Filter_Open();
250: PIC2_Cont();
251: USB_PIC_Cont_1st2();
252: PIC1_Start();
253: for(i=1; i<=4; i++)
254: {
255: USB_PIC_BKT_Down2();
256: USB_PIC_BKT_Up2();
257: }
258: __delay_ms(1000);
259: USB_PIC_Cont_2nd2();
260: PIC2_Int();
261: Filter_Close();
262:
263: }
264: CDCTxService();
265: }
266:
267: void PIC3Start()
268: {
269: int i;
270: LATCbits.LATC5 = 0;
271: LATCbits.LATC6 = 0;
272: __delay_ms(10);
273: LATCbits.LATC5 = 1;
274: for(i=1; i<=10; i++)
275: {
276: __delay_ms(100);
277: }
278: LATCbits.LATC5 = 0;
279: }
280:
281: void PIC2_Int()
282: {
283: int i;
284: LATCbits.LATC3 = 0;
285: LATCbits.LATC4 = 0;
286: __delay_ms(10);
287: LATCbits.LATC3 = 1;
288: for(i=1; i<=20; i++)
289: {
290: __delay_ms(100);
291: }
292: LATCbits.LATC3 = 0;
293:
294: return;
295: }
296:
297: void PIC2_Cont()
298: {
299: int i;
300: LATCbits.LATC3 = 0;
301: LATCbits.LATC4 = 0;
302: __delay_ms(10);
303: LATCbits.LATC4 = 1;
304: for(i=1; i<=20; i++)
305: {
306: __delay_ms(100);
307: }
308: LATCbits.LATC4 = 0;
309:
310: return;
311: }
312:
313: void PIC1_Start()
314: {
315: LATCbits.LATC7=0;
316:
317: LATCbits.LATC7=1;
318: __delay_ms(100);
319: LATCbits.LATC7=0;
320:
321: return;
322: }
323:
324: void USB_PIC_Cont_1st()
325: {
326: int i;
327:
328: LATBbits.LATB6=0;
329: LATBbits.LATB7=0;
330: __delay_ms(20);
331: LATBbits.LATB6 = 1;
332: __delay_ms(20);
333: LATBbits.LATB7 = 1;
334: for(i=1; i<=20;i++)
335: {
336: __delay_ms(100);
337: }
338: LATBbits.LATB7 = 0;
339: __delay_ms(20);
340: LATBbits.LATB6 = 0;
341: __delay_ms(20);
342: SpeedUp();
343: SpeedUp();
344:
345: return;
346: }
347:
348: void USB_PIC_Cont_2nd()
349: {
350: int i;
351:
352: SpeedDown();
353: SpeedDown();
354:
355: LATBbits.LATB6=0;
356: LATBbits.LATB7=0;
357: __delay_ms(20);
358: LATBbits.LATB6 = 1;
359: __delay_ms(20);
360: LATBbits.LATB7 = 1;
361: for(i=1; i<=20;i++)
362: {
363: __delay_ms(100);
364: }
365: LATBbits.LATB7 = 0;
366: __delay_ms(20);
367: LATBbits.LATB6 = 0;
368: __delay_ms(20);
369: return;
370: }
371:
372: void USB_PIC_BKT_Down()
373: {
374: int i;
375:
376: for(i=1 ;i<=2 ; i++)
377: {
378: Captor();
379: __delay_ms(900);
380: SpeedDown();
381: __delay_ms(50);
382:
383: }
384: for(i=1 ;i<=1 ; i++)
385: {
386: Captor();
387: __delay_ms(1000);
388: SpeedDown();
389: __delay_ms(50);
390: }
391: for(i=1 ;i<=3 ; i++)
392: {
393: Captor();
394: __delay_ms(1000);
395: SpeedDown();
396: __delay_ms(50);
397: }
398: Captor();
399: __delay_ms(1100);
400:
401: return;
402: }
403:
404: void USB_PIC_BKT_Up()
405: {
406: int i;
407:
408: Captor();
409: __delay_ms(1000);
410: for(i=1 ;i<=3 ; i++)
411: {
412: SpeedUp();
413: __delay_ms(50);
414: Captor();
415: __delay_ms(1000);
416: }
417: for(i=1 ;i<=2 ; i++)
418: {
419: SpeedUp();
420: __delay_ms(50);
421: Captor();
422: __delay_ms(1000);
423:
424: }
425: for(i=1 ;i<=1 ; i++)
426: {
427: SpeedUp();
428: __delay_ms(50);
429: Captor();
430: __delay_ms(900);
431: }
432: SpeedUp();
433: __delay_ms(50);
434: return;
435: }
436:
437: void Filter_Open()
438: {
439: int k, b;
440:
441: for(k=0; k<20; k++)
442: {
443: LATCbits.LATC2 = 1;
444: {
445: __delay_us(1500);
446: }
447: LATCbits.LATC2 = 0;
448: for(b=1; b<=37; b++)
449: {
450: __delay_us(500);
451: }
452: }
453: __delay_ms(200);
454: for(k=0; k<20; k++)
455: {
456: LATCbits.LATC1 = 1;
457: {
458: __delay_us(1500);
459: }
460: LATCbits.LATC1 = 0;
461: for(b=1; b<=37; b++)
462: {
463: __delay_us(500);
464: }
465: }
466: __delay_ms(200);
467: for(k=0; k<20; k++)
468: {
469: LATCbits.LATC0 = 1;
470: {
471: __delay_us(1500);
472: }
473: LATCbits.LATC0 = 0;
474: for(b=1; b<=37; b++)
475: {
476: __delay_us(500);
477: }
478: }
479: __delay_ms(200);
480:
481: return;
482: }
483:
484: void Filter_Close()
485: {
486: int k, b;
487:
488: for(k=0; k<25; k++)
489: {
490: LATCbits.LATC2 = 1;
491: {
492: __delay_us(50);
493: }
494: LATCbits.LATC2 = 0;
495: __delay_us(450);
496: for(b=1; b<=39; b++)
497: {
498: __delay_us(500);
499: }
500: }
501: __delay_ms(200);
502: for(k=0; k<25; k++)
503: {
504: LATCbits.LATC1 = 1;
505: {
506: __delay_us(50);
507: }
508: LATCbits.LATC1 = 0;
509: __delay_us(450);
510: for(b=1; b<=39; b++)
511: {
512: __delay_us(500);
513: }
514: }
515: __delay_ms(200);
516: for(k=0; k<25; k++)
517: {
518: LATCbits.LATC0 = 1;
519: {
520: __delay_us(50);
521: }
522: LATCbits.LATC0 = 0;
523: __delay_us(450);
524: for(b=1; b<=39; b++)
525: {
526: __delay_us(500);
527: }
528: }
529: __delay_ms(200);
530:
531: return;
532: }
533:
534: void SS_UpTo_8000()
535: {
536: int i;
537:
538: for(i=1; i<=7; i++)
539: {
540: SpeedUp();
541: }
542:
543: return;
544: }
545:
546: void SS_Down_To_500()
547: {
548: int i;
549:
550: for(i=1; i<=2; i++)
551: {
552: SpeedDown();
553: }
554:
555: return;
556: }
557:
558: void Captor()
559: {
560: LATBbits.LATB6=0;
561: LATBbits.LATB7=0;
562:
563: LATBbits.LATB6 = 1;
564: __delay_ms(20);
565: LATBbits.LATB7 = 1;
566: __delay_ms(40);
567: LATBbits.LATB7 = 0;
568: __delay_ms(20);
569: LATBbits.LATB6 = 0;
570: __delay_ms(20);
571:
572: return;
573: }
574:
575: void SpeedDown()
576: {
577: int i;
578:
579: LATBbits.LATB4 = 0;
580: LATBbits.LATB5 = 0;
581:
582: for(i=1; i<=4; i++)
583: {
584: LATBbits.LATB4=1;
585: __delay_ms(25);
586: LATBbits.LATB5=1;
587: __delay_ms(25);
588: LATBbits.LATB4=0;
589: __delay_ms(25);
590: LATBbits.LATB5=0;
591: __delay_ms(25);
592: }
593:
594: return;
595: }
596:
597: void SpeedUp()
598: {
599: int i;
600:
601: LATBbits.LATB4 = 0;
602: LATBbits.LATB5 = 0;
603:
604: for(i=1; i<=4; i++)
605: {
606: LATBbits.LATB5=1;
607: __delay_ms(25);
608: LATBbits.LATB4=1;
609: __delay_ms(25);
610: LATBbits.LATB5=0;
611: __delay_ms(25);
612: LATBbits.LATB4=0;
613: __delay_ms(25);
614: }
615:
616: return;
617: }
618: void USB_PIC_BKT_Down2()
619: {
620: int i;
621:
622: Captor();
623: __delay_ms(900);
624: SpeedDown();
625: __delay_ms(50);
626:
627: for(i=1 ;i<=4 ; i++)
628: {
629: Captor();
630: __delay_ms(1100);
631: SpeedDown();
632: __delay_ms(50);
633: }
634: Captor();
635: __delay_ms(1500);
636: SpeedDown();
637: __delay_ms(50);
638: Captor();
639: __delay_ms(2500);
640:
641: return;
642: }
643:
644: void USB_PIC_BKT_Up2()
645: {
646: int i;
647:
648: Captor();
649: __delay_ms(2500);
650:
651: SpeedUp();
652: __delay_ms(30);
653: Captor();
654: __delay_ms(1500);
655:
656: for(i=1 ;i<=4 ; i++)
657: {
658: SpeedUp();
659: __delay_ms(30);
660: Captor();
661: __delay_ms(1100);
662: }
663:
664: SpeedUp();
665: __delay_ms(50);
666: Captor();
667: __delay_ms(900);
668:
669: SpeedUp();
670: SpeedUp();
671: SpeedDown();
672:
673: return;
674: }
675: void USB_PIC_Cont_1st2()
676: {
677: int i;
678:
679: LATBbits.LATB6=0;
680: LATBbits.LATB7=0;
681: __delay_ms(20);
682: LATBbits.LATB6 = 1;
683: __delay_ms(20);
684: LATBbits.LATB7 = 1;
685: for(i=1; i<=15;i++)
686: {
687: __delay_ms(1000);
688: }
689: LATBbits.LATB7 = 0;
690: __delay_ms(20);
691: LATBbits.LATB6 = 0;
692: __delay_ms(20);
693: SpeedUp();
694: return;
695: }
696:
697: void USB_PIC_Cont_2nd2()
698: {
699: int i;
700:
701: SpeedDown();
702:
703: LATBbits.LATB6=0;
704: LATBbits.LATB7=0;
705: __delay_ms(20);
706: LATBbits.LATB6 = 1;
707: __delay_ms(20);
708: LATBbits.LATB7 = 1;
709: for(i=1; i<=20;i++)
710: {
711: __delay_ms(1000);
712: }
713: LATBbits.LATB7 = 0;
714: __delay_ms(20);
715: LATBbits.LATB6 = 0;
716: __delay_ms(20);
717: return;
718: }