fix bootloader for atm2560
[protos/xbee-avr.git] / DualVirtualSerial.c
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2011.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this\r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in\r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting\r
17   documentation, and that the name of the author not be used in\r
18   advertising or publicity pertaining to distribution of the\r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 /** \file\r
32  *\r
33  *  Main source file for the DualVirtualSerial demo. This file contains the main tasks of\r
34  *  the demo and is responsible for the initial application hardware configuration.\r
35  */\r
36 \r
37 #include "DualVirtualSerial.h"\r
38 \r
39 \r
40 #include <aversive.h>\r
41 #include <aversive/error.h>\r
42 #include <aversive/queue.h>\r
43 \r
44 #include <scheduler.h>\r
45 #include <clock_time.h>\r
46 #include <parse.h>\r
47 #include <rdline.h>\r
48 #include <timer.h>\r
49 \r
50 #include "xbee_neighbor.h"\r
51 #include "xbee_atcmd.h"\r
52 #include "xbee_stats.h"\r
53 #include "xbee_buf.h"\r
54 #include "xbee_proto.h"\r
55 #include "xbee.h"\r
56 \r
57 #include "callout.h"\r
58 #include "main.h"\r
59 #include "cmdline.h"\r
60 \r
61 /** LUFA CDC Class driver interface configuration and state information. This structure is\r
62  *  passed to all CDC Class driver functions, so that multiple instances of the same class\r
63  *  within a device can be differentiated from one another. This is for the first CDC interface,\r
64  *  which sends strings to the host for each joystick movement.\r
65  */\r
66 USB_ClassInfo_CDC_Device_t VirtualSerial1_CDC_Interface =\r
67         {\r
68                 .Config =\r
69                         {\r
70                                 .ControlInterfaceNumber           = 0,\r
71 \r
72                                 .DataINEndpointNumber             = CDC1_TX_EPNUM,\r
73                                 .DataINEndpointSize               = CDC_TXRX_EPSIZE,\r
74                                 .DataINEndpointDoubleBank         = false,\r
75 \r
76                                 .DataOUTEndpointNumber            = CDC1_RX_EPNUM,\r
77                                 .DataOUTEndpointSize              = CDC_TXRX_EPSIZE,\r
78                                 .DataOUTEndpointDoubleBank        = false,\r
79 \r
80                                 .NotificationEndpointNumber       = CDC1_NOTIFICATION_EPNUM,\r
81                                 .NotificationEndpointSize         = CDC_NOTIFICATION_EPSIZE,\r
82                                 .NotificationEndpointDoubleBank   = false,\r
83                         },\r
84         };\r
85 \r
86 /** LUFA CDC Class driver interface configuration and state information. This structure is\r
87  *  passed to all CDC Class driver functions, so that multiple instances of the same class\r
88  *  within a device can be differentiated from one another. This is for the second CDC interface,\r
89  *  which echos back all received data from the host.\r
90  */\r
91 USB_ClassInfo_CDC_Device_t VirtualSerial2_CDC_Interface =\r
92         {\r
93                 .Config =\r
94                         {\r
95                                 .ControlInterfaceNumber           = 2,\r
96 \r
97                                 .DataINEndpointNumber             = CDC2_TX_EPNUM,\r
98                                 .DataINEndpointSize               = CDC_TXRX_EPSIZE,\r
99                                 .DataINEndpointDoubleBank         = false,\r
100 \r
101                                 .DataOUTEndpointNumber            = CDC2_RX_EPNUM,\r
102                                 .DataOUTEndpointSize              = CDC_TXRX_EPSIZE,\r
103                                 .DataOUTEndpointDoubleBank        = false,\r
104 \r
105                                 .NotificationEndpointNumber       = CDC2_NOTIFICATION_EPNUM,\r
106                                 .NotificationEndpointSize         = CDC_NOTIFICATION_EPSIZE,\r
107                                 .NotificationEndpointDoubleBank   = false,\r
108                         },\r
109         };\r
110 \r
111 \r
112 /** Configures the board hardware and chip peripherals for the demo's functionality. */\r
113 void SetupHardware(void)\r
114 {\r
115         /* Disable watchdog if enabled by bootloader/fuses */\r
116         MCUSR &= ~(1 << WDRF);\r
117         wdt_disable();\r
118 \r
119         /* Disable clock division */\r
120         clock_prescale_set(clock_div_1);\r
121 \r
122         /* Hardware Initialization */\r
123         Joystick_Init();\r
124         LEDs_Init();\r
125         USB_Init();\r
126 }\r
127 \r
128 /** Checks for changes in the position of the board joystick, sending strings to the host upon each change\r
129  *  through the first of the CDC interfaces.\r
130  */\r
131 void CheckJoystickMovement(void)\r
132 {\r
133         uint8_t     JoyStatus_LCL = Joystick_GetStatus();\r
134         char*       ReportString  = NULL;\r
135         static bool ActionSent = false;\r
136 \r
137         if (JoyStatus_LCL & JOY_UP)\r
138           ReportString = "Joystick Up\r\n";\r
139         else if (JoyStatus_LCL & JOY_DOWN)\r
140           ReportString = "Joystick Down\r\n";\r
141         else if (JoyStatus_LCL & JOY_LEFT)\r
142           ReportString = "Joystick Left\r\n";\r
143         else if (JoyStatus_LCL & JOY_RIGHT)\r
144           ReportString = "Joystick Right\r\n";\r
145         else if (JoyStatus_LCL & JOY_PRESS)\r
146           ReportString = "Joystick Pressed\r\n";\r
147         else\r
148           ActionSent = false;\r
149 \r
150         if ((ReportString != NULL) && (ActionSent == false))\r
151         {\r
152                 ActionSent = true;\r
153 \r
154                 CDC_Device_SendString(&VirtualSerial1_CDC_Interface, ReportString);\r
155         }\r
156 }\r
157 \r
158 /** Event handler for the library USB Connection event. */\r
159 void EVENT_USB_Device_Connect(void)\r
160 {\r
161         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);\r
162 }\r
163 \r
164 /** Event handler for the library USB Disconnection event. */\r
165 void EVENT_USB_Device_Disconnect(void)\r
166 {\r
167         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
168 }\r
169 \r
170 /** Event handler for the library USB Configuration Changed event. */\r
171 void EVENT_USB_Device_ConfigurationChanged(void)\r
172 {\r
173         bool ConfigSuccess = true;\r
174 \r
175         ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial1_CDC_Interface);\r
176         ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial2_CDC_Interface);\r
177 \r
178         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);\r
179 \r
180         rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);\r
181 }\r
182 \r
183 /** Event handler for the library USB Control Request reception event. */\r
184 void EVENT_USB_Device_ControlRequest(void)\r
185 {\r
186         CDC_Device_ProcessControlRequest(&VirtualSerial1_CDC_Interface);\r
187         CDC_Device_ProcessControlRequest(&VirtualSerial2_CDC_Interface);\r
188 }\r
189 \r