3 Copyright (C) Dean Camera, 2021.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2021 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 disclaims 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 * enters a loop to run the application tasks in sequence.
46 puts_P(PSTR(ESC_FG_CYAN
"Still Image Host Demo running.\r\n" ESC_FG_WHITE
));
48 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
49 GlobalInterruptEnable();
53 StillImageHost_Task();
59 /** Configures the board hardware and chip peripherals for the demo's functionality. */
60 void SetupHardware(void)
62 #if (ARCH == ARCH_AVR8)
63 /* Disable watchdog if enabled by bootloader/fuses */
64 MCUSR
&= ~(1 << WDRF
);
67 /* Disable clock division */
68 clock_prescale_set(clock_div_1
);
71 /* Hardware Initialization */
72 Serial_Init(9600, false);
76 /* Create a stdio stream for the serial port for stdin and stdout */
77 Serial_CreateStream(NULL
);
80 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
81 * starts the library USB task to begin the enumeration and USB management process.
83 void EVENT_USB_Host_DeviceAttached(void)
85 puts_P(PSTR(ESC_FG_GREEN
"Device Attached.\r\n" ESC_FG_WHITE
));
86 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
89 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
90 * stops the library USB task management process.
92 void EVENT_USB_Host_DeviceUnattached(void)
94 puts_P(PSTR(ESC_FG_GREEN
"\r\nDevice Unattached.\r\n" ESC_FG_WHITE
));
95 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
98 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
99 * enumerated by the host and is now ready to be used by the application.
101 void EVENT_USB_Host_DeviceEnumerationComplete(void)
103 puts_P(PSTR("Getting Config Data.\r\n"));
107 /* Get and process the configuration descriptor data */
108 if ((ErrorCode
= ProcessConfigurationDescriptor()) != SuccessfulConfigRead
)
110 if (ErrorCode
== ControlError
)
111 puts_P(PSTR(ESC_FG_RED
"Control Error (Get Configuration).\r\n"));
113 puts_P(PSTR(ESC_FG_RED
"Invalid Device.\r\n"));
115 printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE
), ErrorCode
);
117 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
121 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
122 if ((ErrorCode
= USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful
)
124 printf_P(PSTR(ESC_FG_RED
"Control Error (Set Configuration).\r\n"
125 " -- Error Code: %d\r\n" ESC_FG_WHITE
), ErrorCode
);
127 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
131 puts_P(PSTR("Still Image Device Enumerated.\r\n"));
132 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
135 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
136 void EVENT_USB_Host_HostError(const uint8_t ErrorCode
)
140 printf_P(PSTR(ESC_FG_RED
"Host Mode Error\r\n"
141 " -- Error Code %d\r\n" ESC_FG_WHITE
), ErrorCode
);
143 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
147 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
148 * enumerating an attached USB device.
150 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode
,
151 const uint8_t SubErrorCode
)
153 printf_P(PSTR(ESC_FG_RED
"Dev Enum Error\r\n"
154 " -- Error Code %d\r\n"
155 " -- Sub Error Code %d\r\n"
156 " -- In State %d\r\n" ESC_FG_WHITE
), ErrorCode
, SubErrorCode
, USB_HostState
);
158 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
161 /** Task to print device information through the serial port, and open/close a test PIMA session with the
162 * attached Still Image device.
164 void StillImageHost_Task(void)
166 if (USB_HostState
!= HOST_STATE_Configured
)
171 /* Indicate device busy via the status LEDs */
172 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
174 puts_P(PSTR("Retrieving Device Info...\r\n"));
176 PIMA_SendBlock
= (PIMA_Container_t
)
178 .DataLength
= PIMA_COMMAND_SIZE(0),
179 .Type
= PIMA_CONTAINER_CommandBlock
,
180 .Code
= PIMA_OPERATION_GETDEVICEINFO
,
181 .TransactionID
= 0x00000000,
185 /* Send the GETDEVICEINFO block */
186 SImage_SendBlockHeader();
188 /* Receive the response data block */
189 if ((ErrorCode
= SImage_ReceiveBlockHeader()) != PIPE_RWSTREAM_NoError
)
191 ShowCommandError(ErrorCode
, false);
192 USB_Host_SetDeviceConfiguration(0);
196 /* Calculate the size of the returned device info data structure */
197 uint16_t DeviceInfoSize
= (PIMA_ReceivedBlock
.DataLength
- PIMA_COMMAND_SIZE(0));
199 /* Create a buffer large enough to hold the entire device info */
200 uint8_t DeviceInfo
[DeviceInfoSize
];
202 /* Read in the data block data (containing device info) */
203 SImage_ReadData(DeviceInfo
, DeviceInfoSize
);
205 /* Once all the data has been read, the pipe must be cleared before the response can be sent */
208 /* Create a pointer for walking through the info dataset */
209 uint8_t* DeviceInfoPos
= DeviceInfo
;
211 /* Skip over the data before the unicode device information strings */
212 DeviceInfoPos
+= 8; // Skip to VendorExtensionDesc String
213 DeviceInfoPos
+= (1 + UNICODE_STRING_LENGTH(*DeviceInfoPos
)); // Skip over VendorExtensionDesc String
214 DeviceInfoPos
+= 2; // Skip over FunctionalMode
215 DeviceInfoPos
+= (4 + (*(uint32_t*)DeviceInfoPos
<< 1)); // Skip over Supported Operations Array
216 DeviceInfoPos
+= (4 + (*(uint32_t*)DeviceInfoPos
<< 1)); // Skip over Supported Events Array
217 DeviceInfoPos
+= (4 + (*(uint32_t*)DeviceInfoPos
<< 1)); // Skip over Supported Device Properties Array
218 DeviceInfoPos
+= (4 + (*(uint32_t*)DeviceInfoPos
<< 1)); // Skip over Capture Formats Array
219 DeviceInfoPos
+= (4 + (*(uint32_t*)DeviceInfoPos
<< 1)); // Skip over Image Formats Array
221 /* Extract and convert the Manufacturer Unicode string to ASCII and print it through the USART */
222 char Manufacturer
[*DeviceInfoPos
];
223 UnicodeToASCII(DeviceInfoPos
, Manufacturer
);
224 printf_P(PSTR(" Manufacturer: %s\r\n"), Manufacturer
);
226 DeviceInfoPos
+= 1 + UNICODE_STRING_LENGTH(*DeviceInfoPos
); // Skip over Manufacturer String
228 /* Extract and convert the Model Unicode string to ASCII and print it through the USART */
229 char Model
[*DeviceInfoPos
];
230 UnicodeToASCII(DeviceInfoPos
, Model
);
231 printf_P(PSTR(" Model: %s\r\n"), Model
);
233 DeviceInfoPos
+= 1 + UNICODE_STRING_LENGTH(*DeviceInfoPos
); // Skip over Model String
235 /* Extract and convert the Device Version Unicode string to ASCII and print it through the USART */
236 char DeviceVersion
[*DeviceInfoPos
];
237 UnicodeToASCII(DeviceInfoPos
, DeviceVersion
);
238 printf_P(PSTR(" Device Version: %s\r\n"), DeviceVersion
);
240 /* Receive the final response block from the device */
241 if ((ErrorCode
= SImage_ReceiveBlockHeader()) != PIPE_RWSTREAM_NoError
)
243 ShowCommandError(ErrorCode
, false);
244 USB_Host_SetDeviceConfiguration(0);
248 /* Verify that the command completed successfully */
249 if ((PIMA_ReceivedBlock
.Type
!= PIMA_CONTAINER_ResponseBlock
) || (PIMA_ReceivedBlock
.Code
!= PIMA_RESPONSE_OK
))
251 ShowCommandError(PIMA_ReceivedBlock
.Code
, true);
252 USB_Host_SetDeviceConfiguration(0);
256 puts_P(PSTR("Opening Session...\r\n"));
258 PIMA_SendBlock
= (PIMA_Container_t
)
260 .DataLength
= PIMA_COMMAND_SIZE(1),
261 .Type
= PIMA_CONTAINER_CommandBlock
,
262 .Code
= PIMA_OPERATION_OPENSESSION
,
263 .TransactionID
= 0x00000000,
264 .Params
= {0x00000001},
267 /* Send the OPENSESSION block, open a session with an ID of 0x0001 */
268 SImage_SendBlockHeader();
270 /* Receive the response block from the device */
271 if ((ErrorCode
= SImage_ReceiveBlockHeader()) != PIPE_RWSTREAM_NoError
)
273 ShowCommandError(ErrorCode
, false);
274 USB_Host_SetDeviceConfiguration(0);
278 /* Verify that the command completed successfully */
279 if ((PIMA_ReceivedBlock
.Type
!= PIMA_CONTAINER_ResponseBlock
) || (PIMA_ReceivedBlock
.Code
!= PIMA_RESPONSE_OK
))
281 ShowCommandError(PIMA_ReceivedBlock
.Code
, true);
282 USB_Host_SetDeviceConfiguration(0);
286 puts_P(PSTR("Closing Session...\r\n"));
288 PIMA_SendBlock
= (PIMA_Container_t
)
290 .DataLength
= PIMA_COMMAND_SIZE(1),
291 .Type
= PIMA_CONTAINER_CommandBlock
,
292 .Code
= PIMA_OPERATION_CLOSESESSION
,
293 .TransactionID
= 0x00000001,
294 .Params
= {0x00000001},
297 /* Send the CLOSESESSION block, close the session with an ID of 0x0001 */
298 SImage_SendBlockHeader();
300 /* Receive the response block from the device */
301 if ((ErrorCode
= SImage_ReceiveBlockHeader()) != PIPE_RWSTREAM_NoError
)
303 ShowCommandError(ErrorCode
, false);
304 USB_Host_SetDeviceConfiguration(0);
308 /* Verify that the command completed successfully */
309 if ((PIMA_ReceivedBlock
.Type
!= PIMA_CONTAINER_ResponseBlock
) || (PIMA_ReceivedBlock
.Code
!= PIMA_RESPONSE_OK
))
311 ShowCommandError(PIMA_ReceivedBlock
.Code
, true);
312 USB_Host_SetDeviceConfiguration(0);
316 puts_P(PSTR("Done.\r\n"));
318 /* Indicate device no longer busy */
319 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
320 USB_Host_SetDeviceConfiguration(0);
323 /** Function to convert a given Unicode encoded string to ASCII. This function will only work correctly on Unicode
324 * strings which contain ASCII printable characters only.
326 * \param[in] UnicodeString Pointer to a Unicode encoded input string
327 * \param[out] Buffer Pointer to a buffer where the converted ASCII string should be stored
329 void UnicodeToASCII(uint8_t* UnicodeString
,
332 /* Get the number of characters in the string, skip to the start of the string data */
333 uint8_t CharactersRemaining
= *(UnicodeString
++);
335 /* Loop through the entire unicode string */
336 while (CharactersRemaining
--)
338 /* Load in the next unicode character (only the lower byte, as only Unicode coded ASCII is supported) */
339 *(Buffer
++) = *UnicodeString
;
341 /* Jump to the next unicode character */
345 /* Null terminate the string */
349 /** Displays a PIMA command error via the device's serial port.
351 * \param[in] ErrorCode Error code of the function which failed to complete successfully
352 * \param[in] ResponseCodeError Indicates if the error is due to a command failed indication from the device, or a communication failure
354 void ShowCommandError(uint8_t ErrorCode
,
355 bool ResponseCodeError
)
357 const char* FailureType
= ((ResponseCodeError
) ?
PSTR("Response Code != OK") : PSTR("Transaction Fail"));
359 printf_P(PSTR(ESC_FG_RED
"Command Error (%S).\r\n"
360 " -- Error Code %d\r\n" ESC_FG_WHITE
), FailureType
, ErrorCode
);
362 /* Indicate error via status LEDs */
363 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);