Add FatFS library to the Webserver project, extend the HTTP server so that it now...
[pub/USBasp.git] / Demos / Host / LowLevel / StillImageHost / Lib / StillImageCommands.c
index 8f05e48..62f3659 100644 (file)
@@ -1,21 +1,21 @@
 /*\r
              LUFA Library\r
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
-  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
-  Permission to use, copy, modify, and distribute this software\r
-  and its documentation for any purpose and without fee is hereby\r
-  granted, provided that the above copyright notice appear in all\r
-  copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting\r
-  documentation, and that the name of the author not be used in\r
-  advertising or publicity pertaining to distribution of the\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
@@ -37,7 +37,6 @@
 \r
 #include "StillImageCommands.h"\r
 \r
 \r
 #include "StillImageCommands.h"\r
 \r
-/* Globals: */\r
 /** PIMA block container for the block to send to the device */\r
 PIMA_Container_t PIMA_SendBlock;\r
 \r
 /** PIMA block container for the block to send to the device */\r
 PIMA_Container_t PIMA_SendBlock;\r
 \r
@@ -79,25 +78,35 @@ void SImage_SendBlockHeader(void)
        Pipe_Freeze();\r
 }\r
 \r
        Pipe_Freeze();\r
 }\r
 \r
-/** Function to receive a PIMA event container from the attached still image device. */\r
-void SImage_RecieveEventHeader(void)\r
+/** Function to receive a PIMA event container from the attached still image device.\r
+ *\r
+ *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum\r
+ */\r
+uint8_t SImage_ReceiveEventHeader(void)\r
 {\r
 {\r
+       uint8_t ErrorCode;\r
+\r
        /* Unfreeze the events pipe */\r
        Pipe_SelectPipe(SIMAGE_EVENTS_PIPE);\r
        Pipe_Unfreeze();\r
        \r
        /* Read in the event data into the global structure */\r
        /* Unfreeze the events pipe */\r
        Pipe_SelectPipe(SIMAGE_EVENTS_PIPE);\r
        Pipe_Unfreeze();\r
        \r
        /* Read in the event data into the global structure */\r
-       Pipe_Read_Stream_LE(&PIMA_EventBlock, sizeof(PIMA_EventBlock));\r
+       ErrorCode = Pipe_Read_Stream_LE(&PIMA_EventBlock, sizeof(PIMA_EventBlock));\r
        \r
        /* Clear the pipe after read complete to prepare for next event */\r
        Pipe_ClearIN();\r
        \r
        /* Freeze the event pipe again after use */\r
        Pipe_Freeze();\r
        \r
        /* Clear the pipe after read complete to prepare for next event */\r
        Pipe_ClearIN();\r
        \r
        /* Freeze the event pipe again after use */\r
        Pipe_Freeze();\r
+       \r
+       return ErrorCode;\r
 }\r
 \r
 }\r
 \r
-/** Function to receive a PIMA response container from the attached still image device. */\r
-uint8_t SImage_RecieveBlockHeader(void)\r
+/** Function to receive a PIMA response container from the attached still image device.\r
+ *\r
+ *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum\r
+ */\r
+uint8_t SImage_ReceiveBlockHeader(void)\r
 {\r
        uint16_t TimeoutMSRem = COMMAND_DATA_TIMEOUT_MS;\r
 \r
 {\r
        uint16_t TimeoutMSRem = COMMAND_DATA_TIMEOUT_MS;\r
 \r
@@ -123,45 +132,39 @@ uint8_t SImage_RecieveBlockHeader(void)
                        }\r
                }\r
                \r
                        }\r
                }\r
                \r
+               Pipe_Freeze();\r
                Pipe_SelectPipe(SIMAGE_DATA_OUT_PIPE);\r
                Pipe_SelectPipe(SIMAGE_DATA_OUT_PIPE);\r
+               Pipe_Unfreeze();\r
 \r
                /* Check if pipe stalled (command failed by device) */\r
                if (Pipe_IsStalled())\r
                {\r
                        /* Clear the stall condition on the OUT pipe */\r
 \r
                /* Check if pipe stalled (command failed by device) */\r
                if (Pipe_IsStalled())\r
                {\r
                        /* Clear the stall condition on the OUT pipe */\r
-                       SImage_ClearPipeStall(SIMAGE_DATA_OUT_PIPE);\r
+                       USB_Host_ClearPipeStall(SIMAGE_DATA_OUT_PIPE);\r
 \r
                        /* Return error code and break out of the loop */\r
                        return PIPE_RWSTREAM_PipeStalled;\r
                }\r
 \r
 \r
                        /* Return error code and break out of the loop */\r
                        return PIPE_RWSTREAM_PipeStalled;\r
                }\r
 \r
+               Pipe_Freeze();\r
                Pipe_SelectPipe(SIMAGE_DATA_IN_PIPE);\r
                Pipe_SelectPipe(SIMAGE_DATA_IN_PIPE);\r
+               Pipe_Unfreeze();\r
 \r
                /* Check if pipe stalled (command failed by device) */\r
                if (Pipe_IsStalled())\r
                {\r
                        /* Clear the stall condition on the IN pipe */\r
 \r
                /* Check if pipe stalled (command failed by device) */\r
                if (Pipe_IsStalled())\r
                {\r
                        /* Clear the stall condition on the IN pipe */\r
-                       SImage_ClearPipeStall(SIMAGE_DATA_IN_PIPE);\r
+                       USB_Host_ClearPipeStall(SIMAGE_DATA_IN_PIPE);\r
 \r
                        /* Return error code */\r
                        return PIPE_RWSTREAM_PipeStalled;\r
                }\r
                  \r
                /* Check to see if the device was disconnected, if so exit function */\r
 \r
                        /* Return error code */\r
                        return PIPE_RWSTREAM_PipeStalled;\r
                }\r
                  \r
                /* Check to see if the device was disconnected, if so exit function */\r
-               if (!(USB_IsConnected))\r
-               {\r
-                       /* Return error code */\r
-                       return PIPE_RWSTREAM_DeviceDisconnected;\r
-               }\r
-       };\r
-       \r
-       /* Freeze OUT pipe after use */\r
-       Pipe_SelectPipe(SIMAGE_DATA_OUT_PIPE);\r
-       Pipe_Freeze();\r
-\r
-       /* Select the IN data pipe for data reception */\r
-       Pipe_SelectPipe(SIMAGE_DATA_IN_PIPE);\r
-       \r
+               if (USB_HostState == HOST_STATE_Unattached)\r
+                 return PIPE_RWSTREAM_DeviceDisconnected;\r
+       }\r
+               \r
        /* Load in the response from the attached device */\r
        Pipe_Read_Stream_LE(&PIMA_ReceivedBlock, PIMA_COMMAND_SIZE(0));\r
        \r
        /* Load in the response from the attached device */\r
        Pipe_Read_Stream_LE(&PIMA_ReceivedBlock, PIMA_COMMAND_SIZE(0));\r
        \r
@@ -190,29 +193,35 @@ uint8_t SImage_RecieveBlockHeader(void)
 \r
 /** Function to send the given data to the device, after a command block has been issued.\r
  *\r
 \r
 /** Function to send the given data to the device, after a command block has been issued.\r
  *\r
- *  \param Buffer  Source data buffer to send to the device\r
- *  \param Bytes   Number of bytes to send\r
+ *  \param[in] Buffer  Source data buffer to send to the device\r
+ *  \param[in] Bytes   Number of bytes to send\r
+ *\r
+ *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum\r
  */\r
  */\r
-void SImage_SendData(void* Buffer, uint16_t Bytes)\r
+uint8_t SImage_SendData(void* Buffer, uint16_t Bytes)\r
 {\r
 {\r
+       uint8_t ErrorCode;\r
+\r
        /* Unfreeze the data OUT pipe */\r
        Pipe_SelectPipe(SIMAGE_DATA_OUT_PIPE);\r
        Pipe_Unfreeze();\r
        \r
        /* Write the data contents to the pipe */\r
        /* Unfreeze the data OUT pipe */\r
        Pipe_SelectPipe(SIMAGE_DATA_OUT_PIPE);\r
        Pipe_Unfreeze();\r
        \r
        /* Write the data contents to the pipe */\r
-       Pipe_Write_Stream_LE(Buffer, Bytes);\r
+       ErrorCode = Pipe_Write_Stream_LE(Buffer, Bytes);\r
 \r
        /* Send the last packet to the attached device */\r
        Pipe_ClearOUT();\r
 \r
        /* Freeze the pipe again after use */\r
        Pipe_Freeze();\r
 \r
        /* Send the last packet to the attached device */\r
        Pipe_ClearOUT();\r
 \r
        /* Freeze the pipe again after use */\r
        Pipe_Freeze();\r
+       \r
+       return ErrorCode;\r
 }\r
 \r
 /** Function to receive the given data to the device, after a response block has been received.\r
  *\r
 }\r
 \r
 /** Function to receive the given data to the device, after a response block has been received.\r
  *\r
- *  \param Buffer  Destination data buffer to put read bytes from the device\r
- *  \param Bytes   Number of bytes to receive\r
+ *  \param[out] Buffer  Destination data buffer to put read bytes from the device\r
+ *  \param[in] Bytes    Number of bytes to receive\r
  *\r
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum\r
  */\r
  *\r
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum\r
  */\r
@@ -254,26 +263,3 @@ bool SImage_IsEventReceived(void)
        \r
        return IsEventReceived;\r
 }\r
        \r
        return IsEventReceived;\r
 }\r
-\r
-/** Clears the stall condition in the attached device on the nominated endpoint number.\r
- *\r
- *  \param EndpointNum  Endpoint number in the attached device whose stall condition is to be cleared\r
- *\r
- *  \return A value from the USB_Host_SendControlErrorCodes_t enum\r
- */\r
-uint8_t SImage_ClearPipeStall(const uint8_t EndpointNum)\r
-{\r
-       USB_ControlRequest = (USB_Request_Header_t)\r
-               {\r
-                       .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT),\r
-                       .bRequest      = REQ_ClearFeature,\r
-                       .wValue        = FEATURE_ENDPOINT_HALT,\r
-                       .wIndex        = EndpointNum,\r
-                       .wLength       = 0,\r
-               };\r
-       \r
-       /* Select the control pipe for the request transfer */\r
-       Pipe_SelectPipe(PIPE_CONTROLPIPE);\r
-\r
-       return USB_Host_SendControlRequest(NULL);\r
-}\r