1 /*****************************************************************************
2  * Copyright (c) 2022, Nations Technologies Inc.
3  *
4  * All rights reserved.
5  * ****************************************************************************
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * - Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the disclaimer below.
12  *
13  * Nations' name may not be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
19  * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  * ****************************************************************************/
27 
28 /**
29  * @file usb_regs.c
30  * @author Nations
31  * @version v1.2.0
32  *
33  * @copyright Copyright (c) 2022, Nations Technologies Inc. All rights reserved.
34  */
35 #include "usb_lib.h"
36 
37 /**
38  * @brief Set the CTRL register value.
39  * @param wRegValue new register value.
40  */
USB_SetCtrl(uint16_t wRegValue)41 void USB_SetCtrl(uint16_t wRegValue)
42 {
43     _SetCNTR(wRegValue);
44 }
45 
46 /**
47  * @brief returns the CTRL register value.
48  * @return CTRL register Value.
49  */
USB_GetCtrl(void)50 uint16_t USB_GetCtrl(void)
51 {
52     return (_GetCNTR());
53 }
54 
55 /**
56  * @brief Set the STS register value.
57  * @param wRegValue new register value.
58  */
USB_SetSts(uint16_t wRegValue)59 void USB_SetSts(uint16_t wRegValue)
60 {
61     _SetISTR(wRegValue);
62 }
63 
64 /**
65  * @brief Returns the STS register value.
66  * @return STS register Value
67  */
USB_GetSts(void)68 uint16_t USB_GetSts(void)
69 {
70     return (_GetISTR());
71 }
72 
73 /**
74  * @brief Returns the FN register value.
75  * @return FN register Value
76  */
USB_GetFn(void)77 uint16_t USB_GetFn(void)
78 {
79     return (_GetFNR());
80 }
81 
82 /**
83  * @brief Set the ADDR register value.
84  * @param wRegValue new register value.
85  */
USB_SetAddr(uint16_t wRegValue)86 void USB_SetAddr(uint16_t wRegValue)
87 {
88     _SetDADDR(wRegValue);
89 }
90 
91 /**
92  * @brief Returns the ADDR register value.
93  * @return ADDR register Value
94  */
USB_GetAddr(void)95 uint16_t USB_GetAddr(void)
96 {
97     return (_GetDADDR());
98 }
99 
100 /**
101  * @brief Set the BUFTAB.
102  * @param wRegValue New register value.
103  */
USB_SetBuftab(uint16_t wRegValue)104 void USB_SetBuftab(uint16_t wRegValue)
105 {
106     _SetBTABLE(wRegValue);
107 }
108 
109 /**
110  * @brief Returns the BUFTAB register value.
111  * @return BUFTAB address.
112  */
USB_GetBTABLE(void)113 uint16_t USB_GetBTABLE(void)
114 {
115     return (_GetBTABLE());
116 }
117 
118 /**
119  * @brief Set the Endpoint register value.
120  * @param bEpNum Endpoint Number.
121  * @param wRegValue New register value.
122  */
USB_SetEndPoint(uint8_t bEpNum,uint16_t wRegValue)123 void USB_SetEndPoint(uint8_t bEpNum, uint16_t wRegValue)
124 {
125     _SetENDPOINT(bEpNum, wRegValue);
126 }
127 
128 /**
129  * @brief Return the Endpoint register value.
130  * @param bEpNum Endpoint Number.
131  * @return Endpoint register value.
132  */
USB_GetEndPoint(uint8_t bEpNum)133 uint16_t USB_GetEndPoint(uint8_t bEpNum)
134 {
135     return (_GetENDPOINT(bEpNum));
136 }
137 
138 /**
139  * @brief sets the type in the endpoint register.
140  * @param bEpNum Endpoint Number.
141  * @param wType type definition.
142  */
USB_SetEpType(uint8_t bEpNum,uint16_t wType)143 void USB_SetEpType(uint8_t bEpNum, uint16_t wType)
144 {
145     _SetEPType(bEpNum, wType);
146 }
147 
148 /**
149  * @brief Returns the endpoint type.
150  * @param bEpNum Endpoint Number.
151  * @return Endpoint Type
152  */
USB_GetEpType(uint8_t bEpNum)153 uint16_t USB_GetEpType(uint8_t bEpNum)
154 {
155     return (_GetEPType(bEpNum));
156 }
157 
158 /**
159  * @brief Set the status of Tx endpoint.
160  * @param bEpNum Endpoint Number.
161  * @param wState new state.
162  */
SetEPTxStatus(uint8_t bEpNum,uint16_t wState)163 void SetEPTxStatus(uint8_t bEpNum, uint16_t wState)
164 {
165     _SetEPTxStatus(bEpNum, wState);
166 }
167 
168 /**
169  * @brief Set the status of Rx endpoint.
170  * @param bEpNum Endpoint Number.
171  * @param wState new state.
172  */
SetEPRxStatus(uint8_t bEpNum,uint16_t wState)173 void SetEPRxStatus(uint8_t bEpNum, uint16_t wState)
174 {
175     _SetEPRxStatus(bEpNum, wState);
176 }
177 
178 /**
179  * @brief sets the status for Double Buffer Endpoint to STALL
180  * @param bEpNum Endpoint Number.
181  * @param bDir Endpoint direction.
182  */
USB_SetDouBleBuferEpStall(uint8_t bEpNum,uint8_t bDir)183 void USB_SetDouBleBuferEpStall(uint8_t bEpNum, uint8_t bDir)
184 {
185     uint16_t Endpoint_DTOG_Status;
186     Endpoint_DTOG_Status = USB_GetEndPoint(bEpNum);
187     if (bDir == EP_DBUF_OUT)
188     { /* OUT double buffered endpoint */
189         _SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPRX_DATTOG1);
190     }
191     else if (bDir == EP_DBUF_IN)
192     { /* IN double buffered endpoint */
193         _SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPTX_DATTOG1);
194     }
195 }
196 
197 /**
198  * @brief Returns the endpoint Tx status.
199  * @param bEpNum Endpoint Number.
200  * @return Endpoint TX Status
201  */
USB_GetEpTxSts(uint8_t bEpNum)202 uint16_t USB_GetEpTxSts(uint8_t bEpNum)
203 {
204     return (_GetEPTxStatus(bEpNum));
205 }
206 
207 /**
208  * @brief Returns the endpoint Rx status.
209  * @param bEpNum Endpoint Number.
210  * @return Endpoint RX Status
211  */
USB_GetEpRxSts(uint8_t bEpNum)212 uint16_t USB_GetEpRxSts(uint8_t bEpNum)
213 {
214     return (_GetEPRxStatus(bEpNum));
215 }
216 
217 /**
218  * @brief Valid the endpoint Tx Status.
219  * @param bEpNum Endpoint Number.
220  */
USB_SetEpTxValid(uint8_t bEpNum)221 void USB_SetEpTxValid(uint8_t bEpNum)
222 {
223     _SetEPTxStatus(bEpNum, EP_TX_VALID);
224 }
225 
226 /**
227  * @brief Valid the endpoint Rx Status.
228  * @param bEpNum Endpoint Number.
229  */
USB_SetEpRxValid(uint8_t bEpNum)230 void USB_SetEpRxValid(uint8_t bEpNum)
231 {
232     _SetEPRxStatus(bEpNum, EP_RX_VALID);
233 }
234 
235 /**
236  * @brief Clear the EP_KIND bit.
237  * @param bEpNum Endpoint Number.
238  */
USB_SetEpKind(uint8_t bEpNum)239 void USB_SetEpKind(uint8_t bEpNum)
240 {
241     _SetEP_KIND(bEpNum);
242 }
243 
244 /**
245  * @brief set the  EP_KIND bit.
246  * @param bEpNum Endpoint Number.
247  */
USB_ClrEpKind(uint8_t bEpNum)248 void USB_ClrEpKind(uint8_t bEpNum)
249 {
250     _ClearEP_KIND(bEpNum);
251 }
252 /**
253  * @brief Clear the Status Out of the related Endpoint
254  * @param bEpNum Endpoint Number.
255  */
USB_ClrStsOut(uint8_t bEpNum)256 void USB_ClrStsOut(uint8_t bEpNum)
257 {
258     _ClearEP_KIND(bEpNum);
259 }
260 /**
261  * @brief Set the Status Out of the related Endpoint
262  * @param bEpNum Endpoint Number.
263  */
USB_SetStsOut(uint8_t bEpNum)264 void USB_SetStsOut(uint8_t bEpNum)
265 {
266     _SetEP_KIND(bEpNum);
267 }
268 /**
269  * @brief Enable the double buffer feature for the endpoint.
270  * @param bEpNum Endpoint Number.
271  */
USB_SetEpDoubleBufer(uint8_t bEpNum)272 void USB_SetEpDoubleBufer(uint8_t bEpNum)
273 {
274     _SetEP_KIND(bEpNum);
275 }
276 /**
277  * @brief Disable the double buffer feature for the endpoint.
278  * @param bEpNum Endpoint Number.
279  */
USB_ClrEpDoubleBufer(uint8_t bEpNum)280 void USB_ClrEpDoubleBufer(uint8_t bEpNum)
281 {
282     _ClearEP_KIND(bEpNum);
283 }
284 /**
285  * @brief Returns the Stall status of the Tx endpoint.
286  * @param bEpNum Endpoint Number.
287  * @return Tx Stall status.
288  */
USB_GetTxStallSts(uint8_t bEpNum)289 uint16_t USB_GetTxStallSts(uint8_t bEpNum)
290 {
291     return (_GetTxStallStatus(bEpNum));
292 }
293 /**
294  * @brief Returns the Stall status of the Rx endpoint.
295  * @param bEpNum Endpoint Number.
296  * @return Rx Stall status.
297  */
USB_GetRxStallSts(uint8_t bEpNum)298 uint16_t USB_GetRxStallSts(uint8_t bEpNum)
299 {
300     return (_GetRxStallStatus(bEpNum));
301 }
302 /**
303  * @brief Clear the CTR_RX bit.
304  * @param bEpNum Endpoint Number.
305  */
USB_ClrEpCtrsRx(uint8_t bEpNum)306 void USB_ClrEpCtrsRx(uint8_t bEpNum)
307 {
308     _ClearEP_CTR_RX(bEpNum);
309 }
310 /**
311  * @brief Clear the CTR_TX bit.
312  * @param bEpNum Endpoint Number.
313  */
USB_ClrEpCtrsTx(uint8_t bEpNum)314 void USB_ClrEpCtrsTx(uint8_t bEpNum)
315 {
316     _ClearEP_CTR_TX(bEpNum);
317 }
318 /**
319  * @brief Toggle the DTOG_RX bit.
320  * @param bEpNum Endpoint Number.
321  */
USB_DattogRx(uint8_t bEpNum)322 void USB_DattogRx(uint8_t bEpNum)
323 {
324     _ToggleDTOG_RX(bEpNum);
325 }
326 /**
327  * @brief Toggle the DTOG_TX bit.
328  * @param bEpNum Endpoint Number.
329  */
USB_DattogTx(uint8_t bEpNum)330 void USB_DattogTx(uint8_t bEpNum)
331 {
332     _ToggleDTOG_TX(bEpNum);
333 }
334 /**
335  * @brief Clear the DTOG_RX bit.
336  * @param bEpNum Endpoint Number.
337  */
USB_ClrDattogRx(uint8_t bEpNum)338 void USB_ClrDattogRx(uint8_t bEpNum)
339 {
340     _ClearDTOG_RX(bEpNum);
341 }
342 /**
343  * @brief Clear the DTOG_TX bit.
344  * @param bEpNum Endpoint Number.
345  */
USB_ClrDattogTx(uint8_t bEpNum)346 void USB_ClrDattogTx(uint8_t bEpNum)
347 {
348     _ClearDTOG_TX(bEpNum);
349 }
350 /**
351  * @brief Set the endpoint address.
352  * @param bEpNum Endpoint Number.
353  * @param bAddr New endpoint address.
354  */
USB_SetEpAddress(uint8_t bEpNum,uint8_t bAddr)355 void USB_SetEpAddress(uint8_t bEpNum, uint8_t bAddr)
356 {
357     _SetEPAddress(bEpNum, bAddr);
358 }
359 /**
360  * @brief Get the endpoint address.
361  * @param bEpNum Endpoint Number.
362  * @return Endpoint address.
363  */
USB_GetEpAddress(uint8_t bEpNum)364 uint8_t USB_GetEpAddress(uint8_t bEpNum)
365 {
366     return (_GetEPAddress(bEpNum));
367 }
368 /**
369  * @brief Set the endpoint Tx buffer address.
370  * @param bEpNum Endpoint Number.
371  * @param wAddr new address.
372  */
USB_SetEpTxAddr(uint8_t bEpNum,uint16_t wAddr)373 void USB_SetEpTxAddr(uint8_t bEpNum, uint16_t wAddr)
374 {
375     _SetEPTxAddr(bEpNum, wAddr);
376 }
377 /**
378  * @brief Set the endpoint Rx buffer address.
379  * @param bEpNum Endpoint Number.
380  * @param wAddr new address.
381  */
USB_SetEpRxAddr(uint8_t bEpNum,uint16_t wAddr)382 void USB_SetEpRxAddr(uint8_t bEpNum, uint16_t wAddr)
383 {
384     _SetEPRxAddr(bEpNum, wAddr);
385 }
386 /**
387  * @brief Returns the endpoint Tx buffer address.
388  * @param bEpNum Endpoint Number.
389  * @return Rx buffer address.
390  */
USB_GetEpTxAddr(uint8_t bEpNum)391 uint16_t USB_GetEpTxAddr(uint8_t bEpNum)
392 {
393     return (_GetEPTxAddr(bEpNum));
394 }
395 /**
396  * @brief Returns the endpoint Rx buffer address.
397  * @param bEpNum Endpoint Number.
398  * @return Rx buffer address.
399  */
USB_GetEpRxAddr(uint8_t bEpNum)400 uint16_t USB_GetEpRxAddr(uint8_t bEpNum)
401 {
402     return (_GetEPRxAddr(bEpNum));
403 }
404 /**
405  * @brief Set the Tx count.
406  * @param bEpNum Endpoint Number.
407  * @param wCount new count value.
408  */
USB_SetEpTxCnt(uint8_t bEpNum,uint16_t wCount)409 void USB_SetEpTxCnt(uint8_t bEpNum, uint16_t wCount)
410 {
411     _SetEPTxCount(bEpNum, wCount);
412 }
413 /**
414  * @brief Set the Count Rx Register value.
415  * @param pdwReg point to the register.
416  * @param wCount the new register value.
417  */
USB_SetEpCntRxReg(uint32_t * pdwReg,uint16_t wCount)418 void USB_SetEpCntRxReg(uint32_t* pdwReg, uint16_t wCount)
419 {
420     _SetEPCountRxReg(dwReg, wCount);
421 }
422 /**
423  * @brief Set the Rx count.
424  * @param bEpNum Endpoint Number.
425  * @param wCount the new count value.
426  */
USB_SetEpRxCnt(uint8_t bEpNum,uint16_t wCount)427 void USB_SetEpRxCnt(uint8_t bEpNum, uint16_t wCount)
428 {
429     _SetEPRxCount(bEpNum, wCount);
430 }
431 /**
432  * @brief Get the Tx count.
433  * @param bEpNum Endpoint Number.
434  * @return Tx count value.
435  */
USB_GetEpTxCnt(uint8_t bEpNum)436 uint16_t USB_GetEpTxCnt(uint8_t bEpNum)
437 {
438     return (_GetEPTxCount(bEpNum));
439 }
440 /**
441  * @brief Get the Rx count.
442  * @param bEpNum Endpoint Number.
443  * @return Rx count value.
444  */
USB_GetEpRxCnt(uint8_t bEpNum)445 uint16_t USB_GetEpRxCnt(uint8_t bEpNum)
446 {
447     return (_GetEPRxCount(bEpNum));
448 }
449 /**
450  * @brief Set the addresses of the buffer 0 and 1.
451  * @param bEpNum Endpoint Number.
452  * @param wBuf0Addr new address of buffer 0.
453  * @param wBuf1Addr new address of buffer 1.
454  */
USB_SetEpDblBuferAddr(uint8_t bEpNum,uint16_t wBuf0Addr,uint16_t wBuf1Addr)455 void USB_SetEpDblBuferAddr(uint8_t bEpNum, uint16_t wBuf0Addr, uint16_t wBuf1Addr)
456 {
457     _SetEPDblBuffAddr(bEpNum, wBuf0Addr, wBuf1Addr);
458 }
459 /**
460  * @brief Set the Buffer 1 address.
461  * @param bEpNum Endpoint Number
462  * @param wBuf0Addr new address.
463  */
USB_SetEpDblBuf0Addr(uint8_t bEpNum,uint16_t wBuf0Addr)464 void USB_SetEpDblBuf0Addr(uint8_t bEpNum, uint16_t wBuf0Addr)
465 {
466     _SetEPDblBuf0Addr(bEpNum, wBuf0Addr);
467 }
468 /**
469  * @brief Set the Buffer 1 address.
470  * @param bEpNum Endpoint Number
471  * @param wBuf1Addr new address.
472  */
USB_SetEpDblBuf1Addr(uint8_t bEpNum,uint16_t wBuf1Addr)473 void USB_SetEpDblBuf1Addr(uint8_t bEpNum, uint16_t wBuf1Addr)
474 {
475     _SetEPDblBuf1Addr(bEpNum, wBuf1Addr);
476 }
477 /**
478  * @brief Returns the address of the Buffer 0.
479  * @param bEpNum Endpoint Number.
480  */
USB_GetEpDblBuf0Addr(uint8_t bEpNum)481 uint16_t USB_GetEpDblBuf0Addr(uint8_t bEpNum)
482 {
483     return (_GetEPDblBuf0Addr(bEpNum));
484 }
485 /**
486  * @brief Returns the address of the Buffer 1.
487  * @param bEpNum Endpoint Number.
488  * @return Address of the Buffer 1.
489  */
USB_GetEpDblBuf1Addr(uint8_t bEpNum)490 uint16_t USB_GetEpDblBuf1Addr(uint8_t bEpNum)
491 {
492     return (_GetEPDblBuf1Addr(bEpNum));
493 }
494 /**
495  * @brief Set the number of bytes for a double Buffer endpoint.
496  * @param bEpNum
497  * @param bDir
498  * @param wCount
499  */
USB_SetEpDblBuferCnt(uint8_t bEpNum,uint8_t bDir,uint16_t wCount)500 void USB_SetEpDblBuferCnt(uint8_t bEpNum, uint8_t bDir, uint16_t wCount)
501 {
502     _SetEPDblBuffCount(bEpNum, bDir, wCount);
503 }
504 /**
505  * @brief Set the number of bytes in the buffer 0 of a double Buffer endpoint.
506  * @param bEpNum
507  * @param bDir
508  * @param wCount
509  */
USB_SetEpDblBuf0Cnt(uint8_t bEpNum,uint8_t bDir,uint16_t wCount)510 void USB_SetEpDblBuf0Cnt(uint8_t bEpNum, uint8_t bDir, uint16_t wCount)
511 {
512     _SetEPDblBuf0Count(bEpNum, bDir, wCount);
513 }
514 /**
515  * @brief Set the number of bytes in the buffer 0 of a double Buffer endpoint.
516  * @param bEpNum
517  * @param bDir
518  * @param wCount
519  */
USB_SetEpDblBuf1Cnt(uint8_t bEpNum,uint8_t bDir,uint16_t wCount)520 void USB_SetEpDblBuf1Cnt(uint8_t bEpNum, uint8_t bDir, uint16_t wCount)
521 {
522     _SetEPDblBuf1Count(bEpNum, bDir, wCount);
523 }
524 /**
525  * @brief Returns the number of byte received in the buffer 0 of a double Buffer endpoint.
526  * @param bEpNum Endpoint Number.
527  * @return Endpoint Buffer 0 count
528  */
USB_GetEpDblBuf0Cnt(uint8_t bEpNum)529 uint16_t USB_GetEpDblBuf0Cnt(uint8_t bEpNum)
530 {
531     return (_GetEPDblBuf0Count(bEpNum));
532 }
533 /**
534  * @brief Returns the number of data received in the buffer 1 of a double Buffer endpoint.
535  * @param bEpNum Endpoint Number.
536  * @return Endpoint Buffer 1 count.
537  */
USB_GetEpDblBuf1Cnt(uint8_t bEpNum)538 uint16_t USB_GetEpDblBuf1Cnt(uint8_t bEpNum)
539 {
540     return (_GetEPDblBuf1Count(bEpNum));
541 }
542 /**
543  * @brief gets direction of the double buffered endpoint
544  * @param bEpNum Endpoint Number.
545  * @return EP_DBUF_OUT, EP_DBUF_IN, EP_DBUF_ERR if the endpoint counter not yet programmed.
546  */
GetEPDblBufDir(uint8_t bEpNum)547 EP_DBUF_DIR GetEPDblBufDir(uint8_t bEpNum)
548 {
549     if ((uint16_t)(*_pEPRxCount(bEpNum) & 0xFC00) != 0)
550         return (EP_DBUF_OUT);
551     else if (((uint16_t)(*_pEPTxCount(bEpNum)) & 0x03FF) != 0)
552         return (EP_DBUF_IN);
553     else
554         return (EP_DBUF_ERR);
555 }
556 /**
557  * @brief free buffer used from the application realizing it to the line toggles
558  * bit SW_BUF in the double buffered endpoint register
559  * @param bEpNum
560  * @param bDir
561  */
USB_FreeUserBuf(uint8_t bEpNum,uint8_t bDir)562 void USB_FreeUserBuf(uint8_t bEpNum, uint8_t bDir)
563 {
564     if (bDir == EP_DBUF_OUT)
565     { /* OUT double buffered endpoint */
566         _ToggleDTOG_TX(bEpNum);
567     }
568     else if (bDir == EP_DBUF_IN)
569     { /* IN double buffered endpoint */
570         _ToggleDTOG_RX(bEpNum);
571     }
572 }
573 
574 /**
575  * @brief merge two byte in a word.
576  * @param bh byte high
577  * @param bl bytes low.
578  * @return resulted word.
579  */
USB_ToWord(uint8_t bh,uint8_t bl)580 uint16_t USB_ToWord(uint8_t bh, uint8_t bl)
581 {
582     uint16_t wRet;
583     wRet = (uint16_t)bl | ((uint16_t)bh << 8);
584     return (wRet);
585 }
586 /**
587  * @brief Swap two byte in a word.
588  * @param wSwW word to Swap.
589  * @return resulted word.
590  */
USB_ByteSwap(uint16_t wSwW)591 uint16_t USB_ByteSwap(uint16_t wSwW)
592 {
593     uint8_t bTemp;
594     uint16_t wRet;
595     bTemp = (uint8_t)(wSwW & 0xff);
596     wRet  = (wSwW >> 8) | ((uint16_t)bTemp << 8);
597     return (wRet);
598 }
599