Merge pull request #93 from zyp/master
[fw/stlink] / example / stm32f4 / STM32_USB_Device_Library / Class / cdc / src / usbd_cdc_if_template.c
1 /**
2   ******************************************************************************
3   * @file    usbd_cdc_if_template.c
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    22-July-2011
7   * @brief   Generic media access Layer.
8   ******************************************************************************
9   * @attention
10   *
11   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17   *
18   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
19   ******************************************************************************
20   */ 
21
22 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED 
23 #pragma     data_alignment = 4 
24 #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
25
26 /* Includes ------------------------------------------------------------------*/
27 #include "usbd_cdc_if_template.h"
28 #include  "stm32_eval.h"
29
30 /* Private typedef -----------------------------------------------------------*/
31 /* Private define ------------------------------------------------------------*/
32 /* Private macro -------------------------------------------------------------*/
33 /* Private variables ---------------------------------------------------------*/
34 /* These are external variables imported from CDC core to be used for IN 
35    transfer management. */
36 extern uint8_t  APP_Rx_Buffer []; /* Write CDC received data in this buffer.
37                                      These data will be sent over USB IN endpoint
38                                      in the CDC core functions. */
39 extern uint32_t APP_Rx_ptr_in;    /* Increment this pointer or roll it back to
40                                      start address when writing received data
41                                      in the buffer APP_Rx_Buffer. */
42
43 /* Private function prototypes -----------------------------------------------*/
44 static uint16_t TEMPLATE_Init     (void);
45 static uint16_t TEMPLATE_DeInit   (void);
46 static uint16_t TEMPLATE_Ctrl     (uint32_t Cmd, uint8_t* Buf, uint32_t Len);
47 static uint16_t TEMPLATE_DataTx   (uint8_t* Buf, uint32_t Len);
48 static uint16_t TEMPLATE_DataRx (uint8_t* Buf, uint32_t Len);
49
50 CDC_IF_Prop_TypeDef TEMPLATE_fops = 
51 {
52   TEMPLATE_Init,
53   TEMPLATE_DeInit,
54   TEMPLATE_Ctrl,
55   TEMPLATE_DataTx,
56   TEMPLATE_DataRx
57 };
58
59 /* Private functions ---------------------------------------------------------*/
60
61 /**
62   * @brief  TEMPLATE_Init
63   *         Initializes the CDC media low layer
64   * @param  None
65   * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL
66   */
67 static uint16_t TEMPLATE_Init(void)
68 {
69   /*
70      Add your initialization code here 
71   */  
72   return USBD_OK;
73 }
74
75 /**
76   * @brief  TEMPLATE_DeInit
77   *         DeInitializes the CDC media low layer
78   * @param  None
79   * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL
80   */
81 static uint16_t TEMPLATE_DeInit(void)
82 {
83   /*
84      Add your deinitialization code here 
85   */  
86   return USBD_OK;
87 }
88
89
90 /**
91   * @brief  TEMPLATE_Ctrl
92   *         Manage the CDC class requests
93   * @param  Cmd: Command code            
94   * @param  Buf: Buffer containing command data (request parameters)
95   * @param  Len: Number of data to be sent (in bytes)
96   * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL
97   */
98 static uint16_t TEMPLATE_Ctrl (uint32_t Cmd, uint8_t* Buf, uint32_t Len)
99
100   switch (Cmd)
101   {
102   case SEND_ENCAPSULATED_COMMAND:
103     /* Add your code here */
104     break;
105
106   case GET_ENCAPSULATED_RESPONSE:
107     /* Add your code here */
108     break;
109
110   case SET_COMM_FEATURE:
111     /* Add your code here */
112     break;
113
114   case GET_COMM_FEATURE:
115     /* Add your code here */
116     break;
117
118   case CLEAR_COMM_FEATURE:
119     /* Add your code here */
120     break;
121
122   case SET_LINE_CODING:
123     /* Add your code here */
124     break;
125
126   case GET_LINE_CODING:
127     /* Add your code here */
128     break;
129
130   case SET_CONTROL_LINE_STATE:
131     /* Add your code here */
132     break;
133
134   case SEND_BREAK:
135      /* Add your code here */
136     break;    
137     
138   default:
139     break;
140   }
141
142   return USBD_OK;
143 }
144
145 /**
146   * @brief  TEMPLATE_DataTx
147   *         CDC received data to be send over USB IN endpoint are managed in 
148   *         this function.
149   * @param  Buf: Buffer of data to be sent
150   * @param  Len: Number of data to be sent (in bytes)
151   * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL
152   */
153 static uint16_t TEMPLATE_DataTx (uint8_t* Buf, uint32_t Len)
154 {
155
156   /* Get the data to be sent */
157   for (i = 0; i < Len; i++)
158   {
159     /* APP_Rx_Buffer[APP_Rx_ptr_in] = XXX_ReceiveData(XXX); */
160   }
161
162   /* Increment the in pointer */
163   APP_Rx_ptr_in++;
164   
165   /* To avoid buffer overflow */
166   if(APP_Rx_ptr_in == APP_RX_DATA_SIZE)
167   {
168     APP_Rx_ptr_in = 0;
169   }  
170   
171   return USBD_OK;
172 }
173
174 /**
175   * @brief  TEMPLATE_DataRx
176   *         Data received over USB OUT endpoint are sent over CDC interface 
177   *         through this function.
178   *           
179   *         @note
180   *         This function will block any OUT packet reception on USB endpoint 
181   *         untill exiting this function. If you exit this function before transfer
182   *         is complete on CDC interface (ie. using DMA controller) it will result 
183   *         in receiving more data while previous ones are still not sent.
184   *                 
185   * @param  Buf: Buffer of data to be received
186   * @param  Len: Number of data received (in bytes)
187   * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL
188   */
189 static uint16_t TEMPLATE_DataRx (uint8_t* Buf, uint32_t Len)
190 {
191   uint32_t i;
192   
193   /* Send the received buffer */
194   for (i = 0; i < Len; i++)
195   {
196     /* XXXX_SendData(XXXX, *(Buf + i) ); */
197   } 
198  
199   return USBD_OK;
200 }
201
202 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/