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 puts_P(PSTR("Getting Config Data.\r\n"));
138 /* Get and process the configuration descriptor data */
139 if ((ErrorCode
= ProcessConfigurationDescriptor()) != SuccessfulConfigRead
)
141 if (ErrorCode
== ControlError
)
142 puts_P(PSTR("Control Error (Get Configuration).\r\n"));
144 puts_P(PSTR("Invalid Device.\r\n"));
146 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
148 /* Indicate error via status LEDs */
149 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
151 /* Wait until USB device disconnected */
152 while (USB_IsConnected
);
156 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
157 if ((ErrorCode
= USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful
)
159 puts_P(PSTR("Control error.\r\n"));
161 /* Indicate error via status LEDs */
162 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
164 /* Wait until USB device disconnected */
165 while (USB_IsConnected
);
169 USB_HostState
= HOST_STATE_Configured
;
171 case HOST_STATE_Configured
:
172 puts_P(PSTR("Still Image Device Enumerated.\r\n"));
174 USB_HostState
= HOST_STATE_Ready
;
176 case HOST_STATE_Ready
:
177 /* Indicate device busy via the status LEDs */
178 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
180 puts_P(PSTR("Retrieving Device Info...\r\n"));
182 PIMA_SendBlock
= (PIMA_Container_t
)
184 .DataLength
= PIMA_COMMAND_SIZE(0),
185 .Type
= CType_CommandBlock
,
186 .Code
= PIMA_OPERATION_GETDEVICEINFO
,
187 .TransactionID
= 0x00000000,
191 /* Send the GETDEVICEINFO block */
192 SImage_SendBlockHeader();
194 /* Receive the response data block */
195 if ((ErrorCode
= SImage_RecieveBlockHeader()) != PIPE_RWSTREAM_NoError
)
197 ShowCommandError(ErrorCode
, false);
201 /* Calculate the size of the returned device info data structure */
202 uint16_t DeviceInfoSize
= (PIMA_ReceivedBlock
.DataLength
- PIMA_COMMAND_SIZE(0));
204 /* Create a buffer large enough to hold the entire device info */
205 uint8_t DeviceInfo
[DeviceInfoSize
];
207 /* Read in the data block data (containing device info) */
208 SImage_ReadData(DeviceInfo
, DeviceInfoSize
);
210 /* Once all the data has been read, the pipe must be cleared before the response can be sent */
213 /* Create a pointer for walking through the info dataset */
214 uint8_t* DeviceInfoPos
= DeviceInfo
;
216 /* Skip over the data before the unicode device information strings */
217 DeviceInfoPos
+= 8; // Skip to VendorExtensionDesc String
218 DeviceInfoPos
+= ((*DeviceInfoPos
<< 1) + 1); // Skip over VendorExtensionDesc String
219 DeviceInfoPos
+= 2; // Skip over FunctionalMode
220 DeviceInfoPos
+= (4 + (*(uint32_t*)DeviceInfoPos
<< 1)); // Skip over OperationCode Array
221 DeviceInfoPos
+= (4 + (*(uint32_t*)DeviceInfoPos
<< 1)); // Skip over EventCode Array
222 DeviceInfoPos
+= (4 + (*(uint32_t*)DeviceInfoPos
<< 1)); // Skip over DevicePropCode Array
223 DeviceInfoPos
+= (4 + (*(uint32_t*)DeviceInfoPos
<< 1)); // Skip over ObjectFormatCode Array
224 DeviceInfoPos
+= (4 + (*(uint32_t*)DeviceInfoPos
<< 1)); // Skip over ObjectFormatCode Array
226 /* Extract and convert the Manufacturer Unicode string to ASCII and print it through the USART */
227 char Manufacturer
[*DeviceInfoPos
];
228 UnicodeToASCII(DeviceInfoPos
, Manufacturer
);
229 printf_P(PSTR(" Manufacturer: %s\r\n"), Manufacturer
);
231 DeviceInfoPos
+= ((*DeviceInfoPos
<< 1) + 1); // Skip over Manufacturer String
233 /* Extract and convert the Model Unicode string to ASCII and print it through the USART */
234 char Model
[*DeviceInfoPos
];
235 UnicodeToASCII(DeviceInfoPos
, Model
);
236 printf_P(PSTR(" Model: %s\r\n"), Model
);
238 DeviceInfoPos
+= ((*DeviceInfoPos
<< 1) + 1); // Skip over Model String
240 /* Extract and convert the Device Version Unicode string to ASCII and print it through the USART */
241 char DeviceVersion
[*DeviceInfoPos
];
242 UnicodeToASCII(DeviceInfoPos
, DeviceVersion
);
243 printf_P(PSTR(" Device Version: %s\r\n"), DeviceVersion
);
245 /* Receive the final response block from the device */
246 if ((ErrorCode
= SImage_RecieveBlockHeader()) != PIPE_RWSTREAM_NoError
)
248 ShowCommandError(ErrorCode
, false);
252 /* Verify that the command completed successfully */
253 if ((PIMA_ReceivedBlock
.Type
!= CType_ResponseBlock
) || (PIMA_ReceivedBlock
.Code
!= PIMA_RESPONSE_OK
))
255 ShowCommandError(PIMA_ReceivedBlock
.Code
, true);
259 puts_P(PSTR("Opening Session...\r\n"));
261 PIMA_SendBlock
= (PIMA_Container_t
)
263 .DataLength
= PIMA_COMMAND_SIZE(1),
264 .Type
= CType_CommandBlock
,
265 .Code
= PIMA_OPERATION_OPENSESSION
,
266 .TransactionID
= 0x00000000,
267 .Params
= {0x00000001},
270 /* Send the OPENSESSION block, open a session with an ID of 0x0001 */
271 SImage_SendBlockHeader();
273 /* Receive the response block from the device */
274 if ((ErrorCode
= SImage_RecieveBlockHeader()) != PIPE_RWSTREAM_NoError
)
276 ShowCommandError(ErrorCode
, false);
280 /* Verify that the command completed successfully */
281 if ((PIMA_ReceivedBlock
.Type
!= CType_ResponseBlock
) || (PIMA_ReceivedBlock
.Code
!= PIMA_RESPONSE_OK
))
283 ShowCommandError(PIMA_ReceivedBlock
.Code
, true);
287 puts_P(PSTR("Closing Session...\r\n"));
289 PIMA_SendBlock
= (PIMA_Container_t
)
291 .DataLength
= PIMA_COMMAND_SIZE(1),
292 .Type
= CType_CommandBlock
,
293 .Code
= PIMA_OPERATION_CLOSESESSION
,
294 .TransactionID
= 0x00000001,
295 .Params
= {0x00000001},
298 /* Send the CLOSESESSION block, close the session with an ID of 0x0001 */
299 SImage_SendBlockHeader();
301 /* Receive the response block from the device */
302 if ((ErrorCode
= SImage_RecieveBlockHeader()) != PIPE_RWSTREAM_NoError
)
304 ShowCommandError(ErrorCode
, false);
308 /* Verify that the command completed successfully */
309 if ((PIMA_ReceivedBlock
.Type
!= CType_ResponseBlock
) || (PIMA_ReceivedBlock
.Code
!= PIMA_RESPONSE_OK
))
311 ShowCommandError(PIMA_ReceivedBlock
.Code
, true);
315 puts_P(PSTR("Done.\r\n"));
317 /* Indicate device no longer busy */
318 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
320 /* Wait until USB device disconnected */
321 while (USB_IsConnected
);
327 /** Function to convert a given Unicode encoded string to ASCII. This function will only work correctly on Unicode
328 * strings which contain ASCII printable characters only.
330 * \param UnicodeString Pointer to a Unicode encoded input string
331 * \param Buffer Pointer to a buffer where the converted ASCII string should be stored
333 void UnicodeToASCII(uint8_t* UnicodeString
, char* Buffer
)
335 /* Get the number of characters in the string, skip to the start of the string data */
336 uint8_t CharactersRemaining
= *(UnicodeString
++);
338 /* Loop through the entire unicode string */
339 while (CharactersRemaining
--)
341 /* Load in the next unicode character (only the lower byte, only Unicode coded ASCII supported) */
342 *(Buffer
++) = *UnicodeString
;
344 /* Jump to the next unicode character */
348 /* Null terminate the string */
352 /** Displays a PIMA command error via the device's serial port.
354 * \param ErrorCode Error code of the function which failed to complete successfully
355 * \param ResponseCodeError Indicates if the error is due to a command failed indication from the device, or a communication failure
357 void ShowCommandError(uint8_t ErrorCode
, bool ResponseCodeError
)
359 char* FailureType
= ((ResponseCodeError
) ?
PSTR("Response Code != OK") : PSTR("Transaction Fail"));
361 printf_P(PSTR(ESC_BG_RED
"Command Error (%S).\r\n"), FailureType
);
362 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode
);
364 /* Indicate error via status LEDs */
365 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
367 /* Wait until USB device disconnected */
368 while (USB_IsConnected
);