Analysis of STM32 usb_mem.c and usb_sil.c files

Publisher:SerendipitySoulLatest update time:2016-10-10 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
These two c files are quite simple. Let's talk about the file usb_men.c first. From the file name, we can know that it is related to memory. This file mainly defines two functions, one is to read the data of the double buffer PMA, PMAToUserBufferCopy(), and the other is to write data to the double buffer PMA, UserToPMABufferCopy. If, when your USB device receives data, of course the data is stored in PMA, we need to use the PMAToUserBufferCopy() function to read the data. If we want to send data to the USB host, we need to copy the data you want to send to the PMA buffer so that it can be sent out. The principle is similar to the serial port.

/******************************************************************************* * Function Name : UserToPMABufferCopy * Description : Copy data from the user memory area to PMA (data packet memory area) * Input : pbUsrBuf: points to the user's memory area * wPMABufAddr: the address of wPMABufAddr to be copied to PMA * wNBytes: the length of the data to be copied (unit: word) * Output : None. * Return : None . *******************************************************************************/ void UserToPMABufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes) { uint32_t n = (wNBytes + 1) >> 1; //n = (wNBytes + 1) / 2, indicating how many words uint32_t i, temp1, temp2; uint16_t *pdwVal; pdwVal = (uint16_t *)(wPMABufAddr * 2 + PMAAddr); //pdwVal stores the address where the data is to be stored for (i = n; i != 0; i--) //Start testing the data { temp1 = (uint16_t) * pbUsrBuf; pbUsrBuf++; temp2 = temp1 | (uint16_t) * pbUsrBuf << 8; //Integrate 2 16-bit data *pdwVal++ = temp2; //Copy the integrated data to PMA pdwVal++; //The destination pointer points to the next address pbUsrBuf++; //The source pointer points to the next address } } /******************************************************************************* * Function Name : PMAToUserBufferCopy * Description : Copy a buffer from user memory area to packet memory area (PMA) * Input : pbUsrBuf points to the user's memory area * wPMABufAddr PAM address * wNBytes number of bytes to be copied * Output : None. * Return : None. ******************************************************************************/ void PMAToUserBufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes) { uint32_t n = (wNBytes + 1) >> 1; //n = (wNBytes + 1) / 2, indicating how many words uint32_t i; uint32_t *pdwVal; pdwVal = (uint32_t *)(wPMABufAddr * 2 + PMAAddr); //Read 1 word of data from the PAM area for (i = n; i != 0; i--) { *(uint16_t*)pbUsrBuf++ = *pdwVal++; //Copy the user's memory area pbUsrBuf++; } }

 
Next is the usb_sil.c file, which is mainly used for the initialization of the simple interface layer and the read and write operation functions of the endpoints. There are three functions in total: USB_SIL_Init(); USB_SIL_Write(); USB_SIL_Read().
The USB_SIL_Init() function initializes the IP and endpoints of the USB device. This function is called in CustomHID_init() in usb_prop.c. In short, it is initialized.

/*******************************************************************************
* Function Name : USB_SIL_Init
* Description : Initialize USB device IP and endpoints
* Input : None.
* Output : None.
* Return : Status.
*******************************************************************************/
uint32_t USB_SIL_Init(void)
{
#ifndef STM32F10X_CL

/* USB interrupts initialization */
/* clear pending interrupts */
_SetISTR(0); //Disable all interrupts
wInterrupt_Mask = IMR_MSK;
/* set interrupts mask */
_SetCNTR(wInterrupt_Mask); //Enable some interrupts

#else

/* Perform OTG Device initialization procedure (including EP0 init) */
OTG_DEV_Init(); //Execute initialization procedure OTG device (including EP0 initialization)

#endif /* STM32F10X_CL */

return 0;
}

 
There are two endpoint read and write data functions. The endpoint write function USB_SIL_Write() has three parameters: uint8_t bEpAddr, uint8_t* pBufferPointer, uint32_t wBufferSize. So when calling the write function, you need to specify the endpoint number, the address of the data to be written, and the length of the data to be written. The read function USB_SIL_Read() has only two parameters: the endpoint number and the address of the data area to be saved.

/*******************************************************************************
* Function Name : USB_SIL_Write
* Description : Write data to the selected endpoint
* Input : bEpAddr: address of non-control endpoint
* pBufferPointer: points to the buffer data to be written to the endpoint
* wBufferSize: length of data to be written (unit: byte)
* Output : None.
* Return : Status.
*******************************************************************************/
uint32_t USB_SIL_Write(uint8_t bEpAddr, uint8_t* pBufferPointer, uint32_t wBufferSize)
{
#ifndef STM32F10X_CL

UserToPMABufferCopy(pBufferPointer, GetEPTxAddr(bEpAddr & 0x7F), wBufferSize); //Copy user data to PMA

SetEPTxCount((bEpAddr & 0x7F), wBufferSize); //Update the control register of data length

#else

PCD_EP_Write (bEpAddr, pBufferPointer, wBufferSize); //Use the PCD interface layer function to write to the selected endpoint

#endif /* STM32F10X_CL */

return 0;
}

/*******************************************************************************
* Function Name : USB_SIL_Read
* Description : Read data from the selected endpoint
* Input : bEpAddr: address of non-control endpoint
* pBufferPointer: point to the address of the data area to be saved
* Output : None.
* Return : Return the length of the read data (unit: byte)
*******************************************************************************/
uint32_t USB_SIL_Read(uint8_t bEpAddr, uint8_t* pBufferPointer)
{
uint32_t DataLength = 0;

#ifndef STM32F10X_CL

DataLength = GetEPRxCount(bEpAddr & 0x7F); //Get the received data length from the selected endpointPMAToUserBufferCopy

(pBufferPointer, GetEPRxAddr(bEpAddr & 0x7F), DataLength); //Copy data from PMA to user area

#else

USB_OTG_EP *ep;

ep = PCD_GetOutEP(bEpAddr); //Get the structure pointer of the selected endpointDataLength

= ep->xfer_len; //Get the received data lengthPCD_EP_Read

(bEpAddr, pBufferPointer, DataLength); //Use PCD interface layer function to read the selected port

#endif /* STM32F10X_CL */

return DataLength; //Return the received data length
}

 

Keywords:STM32 Reference address:Analysis of STM32 usb_mem.c and usb_sil.c files

Previous article:STM32 interrupt priority understanding and preemptive priority and slave priority
Next article:Production of STM32 upgrade files

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号