STM32F USB interrupt analysis

Publisher:anluranLatest update time:2018-05-30 Source: eefocusKeywords:STM32F  USB Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Sometimes I always wonder, what is the best way to learn? Just like learning USB, is it enough to just learn how to apply it, or do I have to go deep into the protocol content and the underlying driver? I often tell others that I know something, but in fact I only know a little bit, and I only know some applications to fool others. So I always comfort myself: I just need to make some applications!!!

The following introduces the two files usb_endp.c and usb_istr.c of the STM32 USB project.

First is usb_endp.c. This file is very simple. It just defines several endpoint input and output functions. My project only has it.

uint8_t USB_Receive_Buffer[REPORT_COUNT]; //Endpoint receives data buffer REPORT_COUNT=64

uint8_t USB_Send_Buffer[REPORT_COUNT]; //Endpoint sends data buffer

volatile uint8_t USB_Received_Flag=0; //USB received data flag

 

/****************************************************** ****************************

* Function Name: EP1_IN_Callback.

* Description: Endpoint 1 input callback function

* Input : None.

* Output : None.

* Return : None.

*************************************************** *******************************/

void EP1_IN_Callback(void)

{

 

}

/****************************************************** ****************************

* Function Name: EP1_OUT_Callback.

* Description: Endpoint 1 output callback function

* Input : None.

* Output : None.

* Return : None.

*************************************************** *******************************/

void EP1_OUT_Callback(void)

{

        USB_SIL_Read(EP1_OUT,USB_Receive_Buffer); //Read the data of output endpoint

        USB_Received_Flag=1; //Flag of received data

}

/****************************************************** ****************************

* Function Name: EP2_IN_Callback.

* Description : Endpoint input callback function.

* Input : None.

* Output : None.

* Return : None.

*************************************************** *******************************/

void EP2_IN_Callback(void)

{

 

}

Next, we will divide usb_istr.c. This c file mainly registers some endpoint response functions, such as the endpoint input and output callback function above, and the interrupt processing of the ISTR interrupt status register, as follows:

__IO uint16_t wIstr; /* Used to save the value read from the ISRT register*/

__IO uint8_t bIntPackSOF = 0; /* Number of start of frame packets (SOFs) received between two consecutive data packets */

 

 

/*Define an array of function pointers pointing to pointers. The function pointers point to 7 endpoint input service programs respectively*/

void (*pEpInt_IN[7])(void) =

  {

    EP1_IN_Callback,

    EP2_IN_Callback,

    EP3_IN_Callback,

    EP4_IN_Callback,

    EP5_IN_Callback,

    EP6_IN_Callback,

    EP7_IN_Callback,

  };

 

/*Define an array of function pointers pointing to pointers. The function pointers point to 7 endpoint output service programs respectively*/

void (*pEpInt_OUT[7])(void) =

  {

    EP1_OUT_Callback,

    EP2_OUT_Callback,

    EP3_OUT_Callback,

    EP4_OUT_Callback,

    EP5_OUT_Callback,

    EP6_OUT_Callback,

    EP7_OUT_Callback,

  };

 

#ifndef STM32F10X_CL

 

/****************************************************** ****************************

* Function Name : USB_Istr

* Description: ISTR interrupt event interrupt service routine

* Input : None.

* Output : None.

* Return : None.

*************************************************** *******************************/

void USB_Istr(void)

{

 

  wIstr = _GetISTR(); //Read the value of the interrupt status register (ISTR)

 

#if (IMR_MSK & ISTR_CTR) //Correct transmission interrupt CTR flag

  if (wIstr & ISTR_CTR &wInterrupt_Mask)

  {

    /* servicing of the endpoint correct transfer interrupt */

    /* clear of the CTR flag into the sub */

    CTR_LP(); //Call the correct transmission interrupt service routine

#ifdef CTR_CALLBACK

    CTR_Callback(); //When CTR_CALLBACK is defined, CTR_Callback is called, like a hook function, to do something when a CRT interrupt occurs

#endif

  }

#endif 

  /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-*-*-*-*-*-*-*-*-*/

#if (IMR_MSK & ISTR_RESET) //Reset (RESET) interrupt flag

  if (wIstr & ISTR_RESET & wInterrupt_Mask) //The interrupt flag read is the RESET interrupt flag, and the RESET interrupt is enabled

  {

    _SetISTR((uint16_t)CLR_RESET); //Clear RESET interrupt flag

    Device_Property.Reset(); //Call the reset function

#ifdef RESET_CALLBACK

    RESET_CALLBACK(); //When RESET_CALLBACK is defined, call RESET_CALLBACK, like a hook function, to do something when a RESET interrupt occurs

#endif

  }

#endif

  /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-*-*-*-*-*-*-*-*-*/

#if (IMR_MSK & ISTR_DOVR) //Packet buffer overflow (DOVR) interrupt flag

  if (wIstr & ISTR_DOVR & wInterrupt_Mask) //The interrupt flag read is the DOVR interrupt flag, and the DOVR interrupt is enabled

  {

    _SetISTR((uint16_t)CLR_DOVR); //Clear DOVR interrupt flag

#ifdef DOVR_CALLBACK

    DOVR_Callback(); //When DOVR_CALLBACK is defined, DOVR_Callback is called, just like a hook function, to do something when a DOVR interrupt occurs

#endif

  }

#endif

  /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-*-*-*-*-*-*-*-*-*/

#if (IMR_MSK & ISTR_ERR) //Error (ERR) interrupt flag

  if (wIstr & ISTR_ERR & wInterrupt_Mask) //The interrupt flag read is the ERR interrupt flag, and the ERR interrupt is enabled

  {

    _SetISTR((uint16_t)CLR_ERR); //Clear ERR interrupt flag

#ifdef ERR_CALLBACK

    ERR_Callback(); //When ERR_CALLBACK is defined, ERR_Callback is called, like a hook function, to do something when an ERR interrupt occurs

#endif

  }

#endif

  /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-*-*-*-*-*-*-*-*-*/

#if (IMR_MSK & ISTR_WKUP) //Wake-up (WKUP) interrupt flag

  if (wIstr & ISTR_WKUP & wInterrupt_Mask) //The interrupt flag read is the WKUP interrupt flag, and the WKUP interrupt is enabled

  {

    _SetISTR((uint16_t)CLR_WKUP); //Clear ERR interrupt flag

    Resume(RESUME_EXTERNAL); //Call the wake-up function

#ifdef WKUP_CALLBACK

    WKUP_Callback(); //When WKUP_CALLBACK is defined, WKUP_Callback is called, just like a hook function, to do something when a WKUP interrupt occurs

#endif

  }

#endif

  /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-*-*-*-*-*-*-*-*-*/

#if (IMR_MSK & ISTR_SUSP) //Suspend (SUSP) interrupt flag

  if (wIstr & ISTR_SUSP & wInterrupt_Mask) //The interrupt flag read is the SUSP interrupt flag, and the SUSP interrupt is enabled

  {

 

    /* check if SUSPEND is possible */

    if (fSuspendEnabled) // Check if it can be suspended

    {

      Suspend(); //If it is possible to suspend, call the suspend function

    }

    else

    {

      /* if not possible then resume after xx ms */

      Resume(RESUME_LATER); //If suspend is not allowed, resume after xx ms

    }

    /* clear of the ISTR bit must be done after setting ofCNTR_FSUSP */

    _SetISTR((uint16_t)CLR_SUSP); //Clear SUSP interrupt flag

#ifdef SUSP_CALLBACK

    SUSP_Callback(); //When SUSP_CALLBACK is defined, SUSP_Callback is called, just like a hook function, to do something when a SUSP interrupt occurs

#endif

  }

#endif

  /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-*-*-*-*-*-*-*-*-*/

#if (IMR_MSK & ISTR_SOF) //Start of frame (SOF) interrupt flag

  if (wIstr & ISTR_SOF & wInterrupt_Mask) //The interrupt flag read is the SOF interrupt flag, and the SOF interrupt is enabled

  {

    _SetISTR((uint16_t)CLR_SOF); //Clear SOF interrupt flag

    bIntPackSOF++; //Count the total number of SOFs received

 

#ifdef SOF_CALLBACK

    SOF_Callback(); //When SOF_CALLBACK is defined, SOF_Callback is called, like a hook function, to do something when a SOF interrupt occurs

#endif

  }

#endif

  /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* -*-*-*-*-*-*-*-*-*-*/

#if (IMR_MSK & ISTR_ESOF) //Expected frame start (ESOF) interrupt flag, when the expected SOF frame start is not received, the interrupt is triggered

  if (wIstr & ISTR_ESOF & wInterrupt_Mask) //The interrupt flag read is the SOF interrupt flag, and the ESOF interrupt is enabled

  {

    _SetISTR((uint16_t)CLR_ESOF); //Clear ESOF interrupt flag

    /* resume handling timing is made with ESOFs */ 

        /* request without change of the machine state */

    Resume(RESUME_ESOF); //Resume ESOF processing time

 

#ifdef ESOF_CALLBACK

    ESOF_Callback(); //When ESOF_CALLBACK is defined, ESOF_Callback is called like a hook function to do something when an ESOF interrupt occurs

#endif

  }

#endif

} /* USB_Istr */


Keywords:STM32F  USB Reference address:STM32F USB interrupt analysis

Previous article:stm32NVIC and external interrupt
Next article:Introduction to STM32 interrupts

Recommended ReadingLatest update time:2024-11-17 02:54

Performance indicators and application characteristics of 4444 four-channel USB isolation oscilloscope
The PicoScope4444 is not only suitable for differential measurements, it is more than a general-purpose oscilloscope with superior differential measurements. PicoScope software provides many free functions such as serial bus decoding, mask measurement, tolerance test channel calculation, mask measurement, tolerance te
[Test Measurement]
USB custom HID device implementation - LPC1768
First, modify the device descriptor based on the previous mouse #include "usbdesc.h"   //USB standard device descriptor const U8 USB_DeviceDescriptor = {       USB_DEVICE_DESC_SIZE, //bLength field. The length of the device descriptor is 18 (0x12) bytes     USB_DEVICE_DESCRIPTOR_TYPE, //bDescriptorType field. The devi
[Microcontroller]
i.MX6Q (TQIMX6Q/TQE9) study notes - USB HOST transplantation of the new version of BSP
USB HOST drivers are generally maintained by chip manufacturers, so we still only need to configure DTS to complete the porting of the USB HOST driver. DTS Configuration Refer to the DTS related to sabresd and add the following content to our DTS: / {   ... regulators { compatible = "simple-bus";   reg_usb
[Microcontroller]
Fast charging is becoming more and more popular, ZHIYUAN Electronics releases USB-PD fast charging test solution
USB-PD (Power Delivery) is a power supply standard based on USB Type-C, with a maximum power of 100W. Although USB-PD fast charging is becoming more and more popular, there is no test tool for fast charging in the industry. ZLG Zhiyuan Electronics officially released the USB-PD test solution and provides free on-site
[Test Measurement]
Fast charging is becoming more and more popular, ZHIYUAN Electronics releases USB-PD fast charging test solution
Design and implementation of infrared data transmission system based on USB2.0
1 Introduction With the development and application of test technology and wireless communication technology, test instruments are developing towards miniaturization and low power consumption. Infrared data transmission is low-cost, simple and easy to use, and is widely used in many small devices. In order to a
[Test Measurement]
Design and implementation of infrared data transmission system based on USB2.0
STM32-USB virtual serial port-learning notes
USB It is used to standardize the connection and communication between computers and external devices. It is an interface technology used in the PC field. USB interface supports plug-and-play and hot-swap functions of devices There are already multiple versions of USB1.0/1.1/2.0/3.0. Currently, USB1.1 and USB2.0 ar
[Microcontroller]
OK6410 USB device driver
usb.c source code: // Reference: drivers/hid/usbhid/usbmouse.c #include "linux/kernel.h" #include "linux/slab.h" #include "linux/module.h" #include "linux/init.h" #include "linux/usb/input.h" #include "linux/hid.h" #include "linux/input.h"  static struct input_dev *mk_dev; static int len; static char *buf; static dma_
[Microcontroller]
STM32F103 Programming-9-USB to TTL Serial Port (Transmit and Receive)
This is modified based on the previous routine USB to TTL serial port (printf). The main modifications are as follows: 1. Since printf is not used anymore, remove the definition of PUTCHAR_PROTOTYPE. 2. Find the code for sending and receiving serial data from the official website routines, and integrate it into the ne
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号