1 /******************** (C) COPYRIGHT 2009 STMicroelectronics ********************
2 * File Name          : otgd_fs_dev.c
3 * Author             : STMicroelectronics
4 * Version            : V3.1.0
5 * Date               : 10/30/2009
6 * Description        : High Layer device mode interface and wrapping layer.
7 ********************************************************************************
8 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
9 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
10 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
11 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
12 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
13 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
14 *******************************************************************************/
15 
16 #ifdef STM32F10X_CL
17 
18 /* Includes ------------------------------------------------------------------*/
19 #include "otgd_fs_dev.h"
20 #include "usb_regs.h"
21 #include "otgd_fs_cal.h"
22 #include "otgd_fs_pcd.h"
23 
24 /* Private typedef -----------------------------------------------------------*/
25 /* Private define ------------------------------------------------------------*/
26 /* Private macro -------------------------------------------------------------*/
27 /* Private variables ---------------------------------------------------------*/
28 /* Extern variables ----------------------------------------------------------*/
29 /* Private function prototypes -----------------------------------------------*/
30 
31 /* Private functions ---------------------------------------------------------*/
32 
33 /*******************************************************************************
34 * Function Name  : OTG_DEV_Init
35 * Description    : Initialize the OTG Device IP and EP0.
36 * Input          : None.
37 * Output         : None.
38 * Return         : None.
39 *******************************************************************************/
OTG_DEV_Init(void)40 void OTG_DEV_Init(void)
41 {
42   EP_DESCRIPTOR ep_descriptor;
43   USB_OTG_EP *ep;
44 
45   /* Set the OTG_USB base registers address */
46   OTGD_FS_SetAddress(USB_OTG_FS1_BASE_ADDR);
47 
48   /* Disable all global interrupts */
49   OTGD_FS_DisableGlobalInt();
50 
51   /*Init the Core (common init.) */
52   OTGD_FS_CoreInit();
53 
54   /* Init Device */
55   OTGD_FS_CoreInitDev();
56 
57   /* Init internal driver structure */
58   OTGD_FS_PCD_Init();
59 
60   /* Configure and open the IN control EP0 */
61   ep_descriptor.bEndpointAddress = 0x80;
62   ep_descriptor.wMaxPacketSize = 64;
63   ep_descriptor.bmAttributes = USB_ENDPOINT_XFER_CONTROL;
64   OTGD_FS_PCD_EP_Open(&ep_descriptor);
65 
66   /* Configure and open the OUT control EP0 */
67   ep_descriptor.bEndpointAddress = 0x00;
68   OTGD_FS_PCD_EP_Open(&ep_descriptor);
69 
70 
71   ep = OTGD_FS_PCD_GetOutEP(0);
72   OTGD_FS_EPStartXfer(ep);
73 
74   /* Enable EP0 to start receiving setup packets */
75   OTGD_FS_PCD_EP0_OutStart();
76 
77   /* Enable USB Global interrupt */
78   OTGD_FS_EnableGlobalInt();
79 }
80 
81 
82 /*******************************************************************************
83 * Function Name  : OTG_DEV_EP_Init
84 * Description    : Initialize the selected endpoint parameters
85 * Input          : - bEpAdd: address of the endpoint (epnum|epdir)
86 *                     expample: EP1 OUT -> 0x01 and EP1 IN 0x81.
87 *                  - bEpType: OTG_DEV_EP_TYPE_CONTROL, OTG_DEV_EP_TYPE_ISOC,
88 *                     OTG_DEV_EP_TYPE_BULK, OTG_DEV_EP_TYPE_INT
89 *                  - wEpMaxPackSize: The EP max packet size.
90 * Output         : None.
91 * Return         : Status: New status to be set for the endpoint:
92 *******************************************************************************/
OTG_DEV_EP_Init(uint8_t bEpAdd,uint8_t bEpType,uint16_t wEpMaxPackSize)93 void OTG_DEV_EP_Init(uint8_t bEpAdd, uint8_t bEpType, uint16_t wEpMaxPackSize)
94 {
95   EP_DESCRIPTOR ep_descriptor;
96   USB_OTG_EP *ep;
97 
98   /* Set the EP parameters in a structure */
99   ep_descriptor.bEndpointAddress = bEpAdd;
100   ep_descriptor.bmAttributes = bEpType;
101   ep_descriptor.wMaxPacketSize = wEpMaxPackSize;
102 
103   OTGD_FS_PCD_EP_Flush(bEpAdd);
104 
105   /* Open the EP with entered parameters */
106   OTGD_FS_PCD_EP_Open(&ep_descriptor);
107 
108   /* Activate the EP if it is an OUT EP */
109   if ((bEpAdd & 0x80) == 0)
110   {
111     ep = OTGD_FS_PCD_GetOutEP(bEpAdd & 0x7F);
112     OTGD_FS_EPStartXfer(ep);
113   }
114   else
115   {
116     ep = OTGD_FS_PCD_GetInEP(bEpAdd & 0x7F);
117     ep->even_odd_frame = 0;
118     OTG_DEV_SetEPTxStatus(bEpAdd, DEV_EP_TX_NAK);
119   }
120 
121 }
122 
123 /*******************************************************************************
124 * Function Name  : OTG_DEV_GetEPTxStatus
125 * Description    : Set the related endpoint status.
126 * Input          : Number of the endpoint.
127 * Output         : None.
128 * Return         : Status: New status to be set for the endpoint:
129 *******************************************************************************/
OTG_DEV_GetEPTxStatus(uint8_t bEpnum)130 uint32_t OTG_DEV_GetEPTxStatus(uint8_t bEpnum)
131 {
132   USB_OTG_EP *ep;
133   uint32_t status = 0;
134 
135   ep = OTGD_FS_PCD_GetInEP(bEpnum & 0x7F);
136 
137   status = OTGD_FS_Dev_GetEPStatus(ep);
138 
139   return status;
140 }
141 
142 /*******************************************************************************
143 * Function Name  : OTG_DEV_GetEPRxStatus
144 * Description    : returns the related endpoint status.
145 * Input          : Number of the endpoint.
146 * Output         : None.
147 * Return         : Status: New status to be set for the endpoint:
148 *******************************************************************************/
OTG_DEV_GetEPRxStatus(uint8_t bEpnum)149 uint32_t OTG_DEV_GetEPRxStatus(uint8_t bEpnum)
150 {
151   USB_OTG_EP *ep;
152   uint32_t status = 0;
153 
154   ep = OTGD_FS_PCD_GetOutEP(bEpnum & 0x7F);
155 
156   status = OTGD_FS_Dev_GetEPStatus(ep);
157 
158   return status;
159 }
160 
161 /*******************************************************************************
162 * Function Name  : OTG_DEV_SetEPTxStatus
163 * Description    : Sets the related endpoint status.
164 * Input          : - bEpnum: Number of the endpoint.
165 *                  - Status: New status to be set for the endpoint. It can be
166 *                    DEV_EP_TX_VALID, DEV_EP_TX_STALL, DEV_EP_TX_NAK or
167 *                    DEV_EP_TX_DISABLE.
168 * Output         : None.
169 * Return         : None.
170 *******************************************************************************/
OTG_DEV_SetEPTxStatus(uint8_t bEpnum,uint32_t Status)171 void OTG_DEV_SetEPTxStatus(uint8_t bEpnum, uint32_t Status)
172 {
173   USB_OTG_EP *ep;
174 
175   ep = OTGD_FS_PCD_GetInEP(bEpnum & 0x7F);
176 
177   if ((bEpnum == 0x80) && (Status == DEV_EP_TX_STALL))
178   {
179     ep->is_in = 1;
180   }
181 
182   OTGD_FS_Dev_SetEPStatus(ep, Status);
183 }
184 
185 /*******************************************************************************
186 * Function Name  : OTG_DEV_SetEPRxStatus
187 * Description    : Sets the related endpoint status.
188 * Input          : - bEpnum: Number of the endpoint.
189 *                  - Status: New status to be set for the endpoint. It can be
190 *                    DEV_EP_RX_VALID, DEV_EP_RX_STALL, DEV_EP_RX_NAK or
191 *                    DEV_EP_RX_DISABLE.
192 * Output         : None.
193 * Return         : None.
194 *******************************************************************************/
OTG_DEV_SetEPRxStatus(uint8_t bEpnum,uint32_t Status)195 void OTG_DEV_SetEPRxStatus(uint8_t bEpnum, uint32_t Status)
196 {
197   USB_OTG_EP *ep;
198 
199   ep = OTGD_FS_PCD_GetOutEP(bEpnum & 0x7F);
200 
201   OTGD_FS_Dev_SetEPStatus(ep, Status);
202 }
203 
204 /*******************************************************************************
205 * Function Name  : USB_DevDisconnect
206 * Description    : Disconnect the Pullup resist.
207 * Input          : bEpNum: Endpoint Number.
208 *                  wState: new state.
209 * Output         : None.
210 * Return         : None.
211 *******************************************************************************/
USB_DevDisconnect(void)212 void USB_DevDisconnect(void)
213 {
214   OTGD_FS_PCD_DevDisconnect();
215 }
216 
217 /*******************************************************************************
218 * Function Name  : USB_DevConnect
219 * Description    : Disconnect the .
220 * Input          : bEpNum: Endpoint Number.
221 *                  wState: new state.
222 * Output         : None.
223 * Return         : None.
224 *******************************************************************************/
USB_DevConnect(void)225 void USB_DevConnect(void)
226 {
227   OTGD_FS_PCD_DevConnect();
228 }
229 
230 /*-*-*-*-*-*-*-*-*-* Replace the usb_regs.h defines -*-*-*-*-*-*-*-*-*-*-*-*-*/
231 
232 /*******************************************************************************
233 * Function Name  : SetEPTxStatus
234 * Description    : Set the status of Tx endpoint.
235 * Input          : bEpNum: Endpoint Number.
236 *                  wState: new state.
237 * Output         : None.
238 * Return         : None.
239 *******************************************************************************/
SetEPTxStatus(uint8_t bEpNum,uint16_t wState)240 void SetEPTxStatus(uint8_t bEpNum, uint16_t wState)
241 {
242   _SetEPTxStatus(bEpNum, wState);
243 }
244 
245 /*******************************************************************************
246 * Function Name  : SetEPRxStatus
247 * Description    : Set the status of Rx endpoint.
248 * Input          : bEpNum: Endpoint Number.
249 *                  wState: new state.
250 * Output         : None.
251 * Return         : None.
252 *******************************************************************************/
SetEPRxStatus(uint8_t bEpNum,uint16_t wState)253 void SetEPRxStatus(uint8_t bEpNum, uint16_t wState)
254 {
255   _SetEPRxStatus(bEpNum, wState);
256 }
257 
258 /*******************************************************************************
259 * Function Name  : GetEPTxStatus
260 * Description    : Returns the endpoint Tx status.
261 * Input          : bEpNum: Endpoint Number.
262 * Output         : None.
263 * Return         : Endpoint TX Status
264 *******************************************************************************/
GetEPTxStatus(uint8_t bEpNum)265 uint16_t GetEPTxStatus(uint8_t bEpNum)
266 {
267   return(_GetEPTxStatus(bEpNum));
268 }
269 
270 /*******************************************************************************
271 * Function Name  : GetEPRxStatus
272 * Description    : Returns the endpoint Rx status.
273 * Input          : bEpNum: Endpoint Number.
274 * Output         : None.
275 * Return         : Endpoint RX Status
276 *******************************************************************************/
GetEPRxStatus(uint8_t bEpNum)277 uint16_t GetEPRxStatus(uint8_t bEpNum)
278 {
279   return(_GetEPRxStatus(bEpNum));
280 }
281 
282 /*******************************************************************************
283 * Function Name  : SetEPTxValid
284 * Description    : Valid the endpoint Tx Status.
285 * Input          : bEpNum: Endpoint Number.
286 * Output         : None.
287 * Return         : None.
288 *******************************************************************************/
SetEPTxValid(uint8_t bEpNum)289 void SetEPTxValid(uint8_t bEpNum)
290 {
291   _SetEPTxStatus(bEpNum, EP_TX_VALID);
292 }
293 
294 /*******************************************************************************
295 * Function Name  : SetEPRxValid
296 * Description    : Valid the endpoint Rx Status.
297 * Input          : bEpNum: Endpoint Number.
298 * Output         : None.
299 * Return         : None.
300 *******************************************************************************/
SetEPRxValid(uint8_t bEpNum)301 void SetEPRxValid(uint8_t bEpNum)
302 {
303   _SetEPRxStatus(bEpNum, EP_RX_VALID);
304 }
305 
306 /*******************************************************************************
307 * Function Name  : GetTxStallStatus
308 * Description    : Returns the Stall status of the Tx endpoint.
309 * Input          : bEpNum: Endpoint Number.
310 * Output         : None.
311 * Return         : Tx Stall status.
312 *******************************************************************************/
GetTxStallStatus(uint8_t bEpNum)313 uint16_t GetTxStallStatus(uint8_t bEpNum)
314 {
315   return(_GetTxStallStatus(bEpNum));
316 }
317 
318 /*******************************************************************************
319 * Function Name  : GetRxStallStatus
320 * Description    : Returns the Stall status of the Rx endpoint.
321 * Input          : bEpNum: Endpoint Number.
322 * Output         : None.
323 * Return         : Rx Stall status.
324 *******************************************************************************/
GetRxStallStatus(uint8_t bEpNum)325 uint16_t GetRxStallStatus(uint8_t bEpNum)
326 {
327   return(_GetRxStallStatus(bEpNum));
328 }
329 
330 /*******************************************************************************
331 * Function Name  : SetEPTxCount.
332 * Description    : Set the Tx count.
333 * Input          : bEpNum: Endpoint Number.
334 *                  wCount: new count value.
335 * Output         : None.
336 * Return         : None.
337 *******************************************************************************/
SetEPTxCount(uint8_t bEpNum,uint16_t wCount)338 void SetEPTxCount(uint8_t bEpNum, uint16_t wCount)
339 {
340 }
341 
342 /*******************************************************************************
343 * Function Name  : SetEPRxCount
344 * Description    : Set the Rx count.
345 * Input          : bEpNum: Endpoint Number.
346 *                  wCount: the new count value.
347 * Output         : None.
348 * Return         : None.
349 *******************************************************************************/
SetEPRxCount(uint8_t bEpNum,uint16_t wCount)350 void SetEPRxCount(uint8_t bEpNum, uint16_t wCount)
351 {
352 }
353 
354 /*******************************************************************************
355 * Function Name  : ToWord
356 * Description    : merge two byte in a word.
357 * Input          : bh: byte high, bl: bytes low.
358 * Output         : None.
359 * Return         : resulted word.
360 *******************************************************************************/
ToWord(uint8_t bh,uint8_t bl)361 uint16_t ToWord(uint8_t bh, uint8_t bl)
362 {
363   uint16_t wRet;
364   wRet = (uint16_t)bl | ((uint16_t)bh << 8);
365   return(wRet);
366 }
367 
368 /*******************************************************************************
369 * Function Name  : ByteSwap
370 * Description    : Swap two byte in a word.
371 * Input          : wSwW: word to Swap.
372 * Output         : None.
373 * Return         : resulted word.
374 *******************************************************************************/
ByteSwap(uint16_t wSwW)375 uint16_t ByteSwap(uint16_t wSwW)
376 {
377   uint8_t bTemp;
378   uint16_t wRet;
379   bTemp = (uint8_t)(wSwW & 0xff);
380   wRet =  (wSwW >> 8) | ((uint16_t)bTemp << 8);
381   return(wRet);
382 }
383 
384 #endif /* STM32F10X_CL */
385 /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
386