Sergey Pastor / grbl1
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usb_endp.c Source File

usb_endp.c

00001 /******************** (C) COPYRIGHT 2011 STMicroelectronics ********************
00002  * File Name          : usb_endp.c
00003  * Author             : MCD Application Team
00004  * Version            : V3.3.0
00005  * Date               : 21-March-2011
00006  * Description        : Endpoint routines
00007  ********************************************************************************
00008  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00009  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
00010  * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
00011  * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
00012  * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
00013  * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00014  *******************************************************************************/
00015 
00016 #include "usb_lib.h"
00017 #include "usb_desc.h"
00018 #include "usb_mem.h"
00019 #include "hw_config.h"
00020 #include "usb_istr.h"
00021 #include "usb_pwr.h"
00022 #include "serial.h"
00023 uint8_t USB_Rx_Buffer[VIRTUAL_COM_PORT_DATA_SIZE];
00024 
00025 extern uint8_t serial_tx_buffer[];
00026 extern uint8_t serial_tx_buffer_head;
00027 extern volatile uint8_t serial_tx_buffer_tail;
00028 
00029 void EP3_OUT_Callback(void)
00030 {
00031     uint16_t USB_Rx_Cnt;
00032 
00033     /* Get the received data buffer and update the counter */
00034     USB_Rx_Cnt = USB_SIL_Read(EP3_OUT, USB_Rx_Buffer);
00035 
00036     /* USB data will be immediately processed, this allow next USB traffic being
00037     NAKed till the end of the USART Xfer */
00038 
00039     OnUsbDataRx(USB_Rx_Buffer, USB_Rx_Cnt);
00040 
00041     /* Enable the receive of data on EP3 */
00042     SetEPRxValid(ENDP3);
00043 }
00044 void EP1_IN_Callback (void)
00045 {
00046     if (serial_tx_buffer_head != serial_tx_buffer_tail && (_GetEPTxStatus(ENDP1) == EP_TX_NAK))
00047     {
00048         uint16_t USB_Tx_length;
00049 
00050         if (serial_tx_buffer_head > serial_tx_buffer_tail)
00051             USB_Tx_length = serial_tx_buffer_head - serial_tx_buffer_tail;
00052         else
00053         {
00054             USB_Tx_length = TX_BUFFER_SIZE - serial_tx_buffer_tail + serial_tx_buffer_head;
00055         }
00056 
00057         if (USB_Tx_length != 0)
00058         {
00059             if (USB_Tx_length > 64)
00060                 USB_Tx_length = 64;
00061 
00062     //        UserToPMABufferCopy(&serial_tx_buffer[serial_tx_buffer_tail], ENDP1_TXADDR, USB_Tx_length);
00063             {
00064                 uint8_t *pbUsrBuf = serial_tx_buffer + serial_tx_buffer_tail;
00065                 uint32_t n = (USB_Tx_length + 1) >> 1;   /* n = (wNBytes + 1) / 2 */
00066                 uint32_t i, temp1;
00067                 uint16_t *pdwVal;
00068                 pdwVal = (uint16_t *)(ENDP1_TXADDR * 2 + PMAAddr);
00069                 for (i = n; i != 0; i--)
00070                 {
00071                     temp1 = (uint16_t) * pbUsrBuf;
00072                     pbUsrBuf++;
00073                     if (pbUsrBuf - serial_tx_buffer == TX_BUFFER_SIZE)
00074                         pbUsrBuf = serial_tx_buffer;
00075 
00076                     *pdwVal++ = temp1 | (uint16_t) * pbUsrBuf << 8;
00077                     pdwVal++;
00078                     pbUsrBuf++;
00079                     if (pbUsrBuf - serial_tx_buffer == TX_BUFFER_SIZE)
00080                         pbUsrBuf = serial_tx_buffer;
00081                 }
00082             }
00083 
00084             SetEPTxCount(ENDP1, USB_Tx_length);
00085             SetEPTxValid(ENDP1);
00086 
00087             serial_tx_buffer_tail += USB_Tx_length;
00088             if (serial_tx_buffer_tail >= TX_BUFFER_SIZE)
00089                 serial_tx_buffer_tail -= TX_BUFFER_SIZE;
00090         }
00091     }
00092 }
00093 
00094 
00095 /*  \brief Start Of Frame (SOF) callback
00096  */
00097 void SOF_Callback(void)
00098 {
00099     if(bDeviceState == CONFIGURED)
00100     {
00101         /* Check the data to be sent through IN pipe */
00102         EP1_IN_Callback();
00103     }
00104 }
00105 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
00106