3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
33 * Main source file for the StillImageHost demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
37 #include "StillImageHost.h"
39 /** Main program entry point. This routine configures the hardware required by the application, then
40 * starts the scheduler to run the application tasks.
46 puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY
47 "Still Image Host Demo running.\r\n" ESC_INVERSE_OFF
));
49 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
58 /** Configures the board hardware and chip peripherals for the demo's functionality. */
59 void SetupHardware(void)
61 /* Disable watchdog if enabled by bootloader/fuses */
62 MCUSR
&= ~(1 << WDRF
);
65 /* Disable Clock Division */
66 CLKPR
= (1 << CLKPCE
);
69 /* Hardware Initialization */
70 SerialStream_Init(9600, false);
75 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
76 * starts the library USB task to begin the enumeration and USB management process.
78 void EVENT_USB_DeviceAttached(void)
80 puts_P(PSTR("Device Attached.\r\n"));
81 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
84 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
85 * stops the library USB task management process.
87 void EVENT_USB_DeviceUnattached(void)
89 puts_P(PSTR("\r\nDevice Unattached.\r\n"));
90 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
93 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
94 * enumerated by the host and is now ready to be used by the application.
96 void EVENT_USB_DeviceEnumerationComplete(void)
98 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
101 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
102 void EVENT_USB_HostError(const uint8_t ErrorCode
)
106 puts_P(PSTR(ESC_BG_RED
"Host Mode Error\r\n"));
107 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode
);
109 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
113 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
114 * enumerating an attached USB device.
116 void EVENT_USB_DeviceEnumerationFailed(const uint8_t ErrorCode
, const uint8_t SubErrorCode
)
118 puts_P(PSTR(ESC_BG_RED
"Dev Enum Error\r\n"));
119 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode
);
120 printf_P(PSTR(" -- Sub Error Code %d\r\n"), SubErrorCode
);
121 printf_P(PSTR(" -- In State %d\r\n"), USB_HostState
);
123 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
126 /** Task to set the configuration of the attached device after it has been enumerated, and to print device information
127 * through the serial port.
129 void StillImage_Task(void)
133 switch (USB_HostState
)
135 case HOST_STATE_Addressed
:
136 /* Standard request to set the device configuration to configuration 1 */
137 USB_ControlRequest
= (USB_Request_Header_t
)
139 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_DEVICE
),
140 .bRequest
= REQ_SetConfiguration
,
146 /* Select the control pipe for the request transfer */
147 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
149 /* Send the request, display error and wait for device detach if request fails */
150 if (USB_Host_SendControlRequest(NULL
) != HOST_SENDCONTROL_Successful
)
152 puts_P(PSTR("Control error.\r\n"));
154 /* Indicate error via status LEDs */
155 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
157 /* Wait until USB device disconnected */
158 while (USB_IsConnected
);
162 USB_HostState
= HOST_STATE_Configured
;
164 case HOST_STATE_Configured
:
165 puts_P(PSTR("Getting Config Data.\r\n"));
167 /* Get and process the configuration descriptor data */
168 if ((ErrorCode
= ProcessConfigurationDescriptor()) != SuccessfulConfigRead
)
170 if (ErrorCode
== ControlError
)
171 puts_P(PSTR("Control Error (Get Configuration).\r\n"));
173 puts_P(PSTR("Invalid Device.\r\n"));
175 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
177 /* Indicate error via status LEDs */
178 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
180 /* Wait until USB device disconnected */
181 while (USB_IsConnected
);
185 puts_P(PSTR("Still Image Device Enumerated.\r\n"));
187 USB_HostState
= HOST_STATE_Ready
;
189 case HOST_STATE_Ready
:
190 /* Indicate device busy via the status LEDs */
191 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
193 puts_P(PSTR("Retrieving Device Info...\r\n"));
195 PIMA_SendBlock
= (PIMA_Container_t
)
197 .DataLength
= PIMA_COMMAND_SIZE(0),
198 .Type
= CType_CommandBlock
,
199 .Code
= PIMA_OPERATION_GETDEVICEINFO
,
200 .TransactionID
= 0x00000000,
204 /* Send the GETDEVICEINFO block */
205 SImage_SendBlockHeader();
207 /* Receive the response data block */
208 if ((ErrorCode
= SImage_RecieveBlockHeader()) != PIPE_RWSTREAM_NoError
)
210 ShowCommandError(ErrorCode
, false);
214 /* Calculate the size of the returned device info data structure */
215 uint16_t DeviceInfoSize
= (PIMA_ReceivedBlock
.DataLength
- PIMA_COMMAND_SIZE(0));
217 /* Create a buffer large enough to hold the entire device info */
218 uint8_t DeviceInfo
[DeviceInfoSize
];
220 /* Read in the data block data (containing device info) */
221 SImage_ReadData(DeviceInfo
, DeviceInfoSize
);
223 /* Once all the data has been read, the pipe must be cleared before the response can be sent */
226 /* Create a pointer for walking through the info dataset */
227 uint8_t* DeviceInfoPos
= DeviceInfo
;
229 /* Skip over the data before the unicode device information strings */
230 DeviceInfoPos
+= 8; // Skip to VendorExtensionDesc String
231 DeviceInfoPos
+= ((*DeviceInfoPos
<< 1) + 1); // Skip over VendorExtensionDesc String
232 DeviceInfoPos
+= 2; // Skip over FunctionalMode
233 DeviceInfoPos
+= (4 + (*(uint32_t*)DeviceInfoPos
<< 1)); // Skip over OperationCode Array
234 DeviceInfoPos
+= (4 + (*(uint32_t*)DeviceInfoPos
<< 1)); // Skip over EventCode Array
235 DeviceInfoPos
+= (4 + (*(uint32_t*)DeviceInfoPos
<< 1)); // Skip over DevicePropCode Array
236 DeviceInfoPos
+= (4 + (*(uint32_t*)DeviceInfoPos
<< 1)); // Skip over ObjectFormatCode Array
237 DeviceInfoPos
+= (4 + (*(uint32_t*)DeviceInfoPos
<< 1)); // Skip over ObjectFormatCode Array
239 /* Extract and convert the Manufacturer Unicode string to ASCII and print it through the USART */
240 char Manufacturer
[*DeviceInfoPos
];
241 UnicodeToASCII(DeviceInfoPos
, Manufacturer
);
242 printf_P(PSTR(" Manufacturer: %s\r\n"), Manufacturer
);
244 DeviceInfoPos
+= ((*DeviceInfoPos
<< 1) + 1); // Skip over Manufacturer String
246 /* Extract and convert the Model Unicode string to ASCII and print it through the USART */
247 char Model
[*DeviceInfoPos
];
248 UnicodeToASCII(DeviceInfoPos
, Model
);
249 printf_P(PSTR(" Model: %s\r\n"), Model
);
251 DeviceInfoPos
+= ((*DeviceInfoPos
<< 1) + 1); // Skip over Model String
253 /* Extract and convert the Device Version Unicode string to ASCII and print it through the USART */
254 char DeviceVersion
[*DeviceInfoPos
];
255 UnicodeToASCII(DeviceInfoPos
, DeviceVersion
);
256 printf_P(PSTR(" Device Version: %s\r\n"), DeviceVersion
);
258 /* Receive the final response block from the device */
259 if ((ErrorCode
= SImage_RecieveBlockHeader()) != PIPE_RWSTREAM_NoError
)
261 ShowCommandError(ErrorCode
, false);
265 /* Verify that the command completed successfully */
266 if ((PIMA_ReceivedBlock
.Type
!= CType_ResponseBlock
) || (PIMA_ReceivedBlock
.Code
!= PIMA_RESPONSE_OK
))
268 ShowCommandError(PIMA_ReceivedBlock
.Code
, true);
272 puts_P(PSTR("Opening Session...\r\n"));
274 PIMA_SendBlock
= (PIMA_Container_t
)
276 .DataLength
= PIMA_COMMAND_SIZE(1),
277 .Type
= CType_CommandBlock
,
278 .Code
= PIMA_OPERATION_OPENSESSION
,
279 .TransactionID
= 0x00000000,
280 .Params
= {0x00000001},
283 /* Send the OPENSESSION block, open a session with an ID of 0x0001 */
284 SImage_SendBlockHeader();
286 /* Receive the response block from the device */
287 if ((ErrorCode
= SImage_RecieveBlockHeader()) != PIPE_RWSTREAM_NoError
)
289 ShowCommandError(ErrorCode
, false);
293 /* Verify that the command completed successfully */
294 if ((PIMA_ReceivedBlock
.Type
!= CType_ResponseBlock
) || (PIMA_ReceivedBlock
.Code
!= PIMA_RESPONSE_OK
))
296 ShowCommandError(PIMA_ReceivedBlock
.Code
, true);
300 puts_P(PSTR("Closing Session...\r\n"));
302 PIMA_SendBlock
= (PIMA_Container_t
)
304 .DataLength
= PIMA_COMMAND_SIZE(1),
305 .Type
= CType_CommandBlock
,
306 .Code
= PIMA_OPERATION_CLOSESESSION
,
307 .TransactionID
= 0x00000001,
308 .Params
= {0x00000001},
311 /* Send the CLOSESESSION block, close the session with an ID of 0x0001 */
312 SImage_SendBlockHeader();
314 /* Receive the response block from the device */
315 if ((ErrorCode
= SImage_RecieveBlockHeader()) != PIPE_RWSTREAM_NoError
)
317 ShowCommandError(ErrorCode
, false);
321 /* Verify that the command completed successfully */
322 if ((PIMA_ReceivedBlock
.Type
!= CType_ResponseBlock
) || (PIMA_ReceivedBlock
.Code
!= PIMA_RESPONSE_OK
))
324 ShowCommandError(PIMA_ReceivedBlock
.Code
, true);
328 puts_P(PSTR("Done.\r\n"));
330 /* Indicate device no longer busy */
331 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
333 /* Wait until USB device disconnected */
334 while (USB_IsConnected
);
340 /** Function to convert a given Unicode encoded string to ASCII. This function will only work correctly on Unicode
341 * strings which contain ASCII printable characters only.
343 * \param UnicodeString Pointer to a Unicode encoded input string
344 * \param Buffer Pointer to a buffer where the converted ASCII string should be stored
346 void UnicodeToASCII(uint8_t* UnicodeString
, char* Buffer
)
348 /* Get the number of characters in the string, skip to the start of the string data */
349 uint8_t CharactersRemaining
= *(UnicodeString
++);
351 /* Loop through the entire unicode string */
352 while (CharactersRemaining
--)
354 /* Load in the next unicode character (only the lower byte, only Unicode coded ASCII supported) */
355 *(Buffer
++) = *UnicodeString
;
357 /* Jump to the next unicode character */
361 /* Null terminate the string */
365 /** Displays a PIMA command error via the device's serial port.
367 * \param ErrorCode Error code of the function which failed to complete successfully
368 * \param ResponseCodeError Indicates if the error is due to a command failed indication from the device, or a communication failure
370 void ShowCommandError(uint8_t ErrorCode
, bool ResponseCodeError
)
372 char* FailureType
= ((ResponseCodeError
) ?
PSTR("Response Code != OK") : PSTR("Transaction Fail"));
374 printf_P(PSTR(ESC_BG_RED
"Command Error (%S).\r\n"), FailureType
);
375 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode
);
377 /* Indicate error via status LEDs */
378 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
380 /* Wait until USB device disconnected */
381 while (USB_IsConnected
);