This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Difference between a call back function and an interrupt handler?



What is the difference between a Callback function and an Interrupt Handler? When should I write what?

  • @anupsingh.

    You, can get more variety of answers to your question by doing a search in the internet.

    - kel
  • Hello Anupsingh,

    Kel has appropriated the answer very well. You should research the topic a little bit more than posting it on the TM4C forum. if there is an issue with a CallBack not working for one of the examples or the driverlib that you have integrated then bring it up. Just for definition we do "Google"...

    Regards
    Amit
  • hello Amit,

    Following is my code.

    /**************************DOCUMENTATION*******************************/
    /**********************USB CODE*****************************************/
    /*Author PRACHI JOSHI*****/
    /****DATE:17/12/2014********/
    
     /* main.c
     */
    	
    	//TM4C123GH6PMI USB Host functionality
    	// Basic configuration as a host- mass storage programming example
    
    	//The following code shows the basic setup code needed for any application that is using the USB library in host mode.
    //The g_pui8HCDPool array which is passed to the USBHCDInit( ) is used as a heap memory for by the USB library and
    //thus should not be used by the application. In this example, the g_ppsHostClassDrivers array holds the MSC (mass storage class)
    //drivers. The macros provided in the pin_map.h file included with DriverLib can be used to indicate which pin and peripheral
    //to use for a given part. The USBHCDRegisterDrivers( ) call passes in the static array of supported USB host class drivers that
    //are supported by the application. Typically, the reading or writing to a device is left to a file system layer or through direct
    //read and write calls.
    
    #include "inc/tm4c123gh6pm.h"
    #include <stdint.h>
    #include <stdbool.h>
    #include "driverlib/gpio.h"
    #include "inc/hw_gpio.h"
    #include "driverlib/sysctl.h"
    #include "inc/hw_sysctl.h"
    #include "inc/hw_types.h"
    #include "inc/hw_memmap.h"
    #include "driverlib/debug.h"
    #include "driverlib/rom.h"
    #include "driverlib/pin_map.h"
    
    //extras
    //#include "driverlib/fpu.h"
    //#include "driverlib/interrupt.h"
    //#include <string.h>
    #include "driverlib/systick.h"
    #include "driverlib/udma.h"
    //#include "packages/xdc/std.h"
    //#include "packages/xdc/runtime/System.h"
    
    #include "driverlib/usb.h"
    #include "inc/hw_usb.h"
    #include "usblib/host/usbhost.h"      //contains host mode function prototypes and data types
    #include "usblib/host/usbhmsc.h"    //contains Mass Storage Class definitions specific to hosts
    #include "usblib/usblib.h"
    #include "usblib/usbmsc.h"
    
    // USB MSD control
    #define MSD_NOT_CONNECTED 0
    #define MSD_INIT 1
    #define MSD_CONNECTED 2
    
    uint32_t iMSDState= MSD_NOT_CONNECTED; 				//initial value 0
    
    
    
    //********************************************************************************************************************************
    //
    // The number of SysTick ticks per second
    //
    //*********************************************************************************************
    #define TICKS_PER_SECOND 100
    #define MS_PER_SYSTICK (1000 / TICKS_PER_SECOND)
    
    //*************************************************************************************************
    //
    // A counter for system clock ticks, used for simple timing.
    //
    //**********************************************************************************************
    static uint32_t g_ui32SysTickCount;
    
    
    //*****************************************************************************
    //
    // Defines the number of times to call to check if the attached device is
    // ready.
    //
    //*****************************************************************************
    #define USBMSC_DRIVE_RETRY      4
    
    //*****************************************************************************
    
    //
    // Hold the current state for the application.
    //
    //*****************************************************************************
    typedef enum
    {
        //
        // No device is present.
        //
        STATE_NO_DEVICE,
    
        //
        // Mass storage device is being enumerated.
        //
        STATE_DEVICE_ENUM,
    
        //
        // Mass storage device is ready.
        //
        STATE_DEVICE_READY,
    
        //
        // An unsupported device has been attached.
        //
        STATE_UNKNOWN_DEVICE,
    
        //
        // A mass storage device was connected but failed to ever report ready.
        //
        STATE_TIMEOUT_DEVICE,
    
        //
        // A power fault has occurred.
        //
        STATE_POWER_FAULT
    }
    tState;
    
    volatile tState g_eState;
    //volatile tState g_eUIState;
    
    //*****************************************************************************************************************************
    
    void * g_psDMAControlTable;
    extern uint32_t ui32Event;
    uint32_t g_ui32UnknownClass;    // class of the unknown device
    
    //**************************************************************************************************************************
    #define HCD_MEMORY_SIZE 128    //the size of the host controller’s memory size in bytes
    unsigned char g_pui8HCDPool [HCD_MEMORY_SIZE];  //the memory pool to provide to the host controller driver
    
    
    //*****************************************************************************
    //
    // The instance data for the MSC driver.
    //
    //*****************************************************************************
    tUSBHMSCInstance *g_psMSCInstance = 0;
    
    //*****************************************************************************
    
    //Declare the USB events driver interface
    
    DECLARE_EVENT_DRIVER(g_sUSBEventDriver, 0, 0, USBHCDEvents);
    
    static tUSBHostClassDriver const  * const g_ppsHostClassDrivers[] =
    						{
    								&g_sUSBHostMSCClassDriver,
    								&g_sUSBEventDriver                         // why do we add this?
    						};
    	// the global that holds all of the host drivers in use in the application
    
    static const unsigned long g_ui32NumHostClassDrivers =	sizeof ( g_ppsHostClassDrivers ) / sizeof ( tUSBHostClassDriver * );
    //this global holds the number of class drivers in the g_ppsHostClassDrivers list
    
    //*****************************************************************************
    //
    // The control table used by the uDMA controller.  This table must be aligned
    // to a 1024 byte boundary.  In this application uDMA is only used for USB,
    // so only the first 6 channels are needed.
    //
    //*****************************************************************************
    #if defined(ewarm)
    #pragma data_alignment=1024
    tDMAControlTable g_sDMAControlTable[6];
    #elif defined(ccs)
    #pragma DATA_ALIGN(g_sDMAControlTable, 1024)
    tDMAControlTable g_sDMAControlTable[6];
    #else
    tDMAControlTable g_sDMAControlTable[6] __attribute__ ((aligned(1024)));
    #endif
    
    //*****************************************************************************
    //
    // The error routine that is called if the driver library encounters an error.
    //
    //*****************************************************************************
    #ifdef DEBUG
    void
    __error__(char *pcFilename, uint32_t ui32Line)
    {
    }
    #endif
    
    //********************************************************************************************
    //
    //This is the handler for the SysTick interrupt. It simply increments a  counter that is used for timing.
    //
    //**********************************************************************************
    void
    SysTickHandler (void)
    	{
    	g_ui32SysTickCount++;
    	}
    
    
    void USB0HostIntHndler (void)
    	{
    // glow an LED if the pen drive is detected.
    	GPIO_PORTF_DATA_R |= 0x16;
    	//for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++);
    	GPIO_PORTF_DATA_R &= ~(0x16);
    	//for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++);
    }
    
    //************************************************************************************************************
    
    // MSCCallBack
    
    //*************************************************************************************************************
    void MSCCallBack (tUSBHMSCInstance *psMSCInstance, uint32_t ui32Event, void *pvEventData)
    	{
    	switch (ui32Event)
    		{
    			// new pen drive detected
    		case USB_EVENT_CONNECTED:
    			{
    				iMSDState= MSD_INIT;
    				break;
    			}
    
    			//pen drive has been unplugged
    
    		case USB_EVENT_DISCONNECTED:
    			{
    				iMSDState= MSD_NOT_CONNECTED;
    				break;
    			}
    
    		}
    	}
    
    //*****************************************************************************
    //
    // This is the generic callback from host stack.
    //
    // \param pvData is actually a pointer to a tEventInfo structure.
    //
    // This function will be called to inform the application when a USB event has
    // occurred that is outside those related to the mass storage device.  At this
    // point this is used to detect unsupported devices being inserted and removed.
    // It is also used to inform the application when a power fault has occurred.
    // This function is required when the g_USBGenericEventDriver is included in
    // the host controller driver array that is passed in to the
    // USBHCDRegisterDrivers() function.
    //
    // \return None.
    //
    //*****************************************************************************
    
    void
    USBHCDEvents(void *pvEventData)
    {
        tEventInfo *pEventInfo;
    
        //
        // Cast this pointer to its actual type.
        //
        pEventInfo = (tEventInfo *)pvEventData;
    
        switch(pEventInfo->ui32Event)
        {
            //
            // Unknown device detected.
            //
            case USB_EVENT_UNKNOWN_CONNECTED:
            {
                //
                // Save the unknown class.
                //
                g_ui32UnknownClass = pEventInfo->ui32Instance;
    
                //
                // An unknown device was detected.
                //
                g_eState = STATE_UNKNOWN_DEVICE;
    
                break;
            }
    
            //
            // USB Device has been unplugged.
            //
            case USB_EVENT_DISCONNECTED:
            {
                //
                // Unknown device has been removed.
                //
                g_eState = STATE_NO_DEVICE;
    
                break;
            }
    
            case USB_EVENT_POWER_FAULT:
            {
                //
                // No power means no device is present.
                //
                g_eState = STATE_POWER_FAULT;
    
                break;
            }
    
            default:
            {
                break;
            }
        }
    }
    
    
    //*************************************************************************************************************
    
    //Application Start
    
    //**************************************************************************************************************
    
    	void main (void)
    	{
    		uint32_t ui32DriveTimeout;
    
        //
        // Initially wait for device connection.
        //
    	    g_eState = STATE_NO_DEVICE;
    
    	    // Set the clocking to run from the PLL at 50 MHz
    		SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
    
    	// enable clocking
    		SysCtlPeripheralEnable (SYSCTL_PERIPH_USB0);
    		SysCtlUSBPLLEnable();
    
    
    	//Enable the GPIO peripherals used by the USB pins
    		SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOB);
    
    	//enable PB0 and PB1 as VBUS and ID pins
    		GPIOPinTypeUSBAnalog (GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);         // PB0- ID; PB1- VBUS
    
    
    		SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOD);
    	// Enable the D+/ D- as USB pins
    		GPIOPinTypeUSBAnalog (GPIO_PORTD_BASE, GPIO_PIN_4 | GPIO_PIN_5);         // PD4- D+, PD5- D-
    
    		SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOC);
    
    	// configure USBEPEN as a USB pin
    		GPIOPinConfigure (GPIO_PC6_USB0EPEN);
    		GPIOPinTypeUSBDigital (GPIO_PORTC_BASE, GPIO_PIN_6);
    
    	//Configure SysTick for a 100 Hz interrupt
    		SysTickPeriodSet (SysCtlClockGet()/ TICKS_PER_SECOND);
    		SysTickEnable();
    		SysTickIntEnable();
    
    	//Enable the uDMA controller and set up the control table base
    	//The uDMA controller is used by the USB library.
    		SysCtlPeripheralEnable (SYSCTL_PERIPH_UDMA);
    		uDMAEnable ();
    		uDMAControlBaseSet (g_psDMAControlTable);
    
    
    	//Initialize the USB stack for host mode
    		USBStackModeSet(0,eUSBModeHost,0);
    
    
    //*****************************************************************************************************************
    
    // LED initialization
    
    		volatile uint32_t ui32Loop;
    		    SYSCTL_RCGC2_R = SYSCTL_RCGC2_GPIOF;
    		    ui32Loop = SYSCTL_RCGC2_R;
    		    GPIO_PORTF_DIR_R = 0xFF;
    		    GPIO_PORTF_DEN_R = 0xFF;
    
    //*****************************************************************************************************************
    
    	//Register the host class drivers
    		USBHCDRegisterDrivers (0, g_ppsHostClassDrivers, g_ui32NumHostClassDrivers);
    
    	// Initialize the power configuration. Set the power enable signal to be active high and does not enable the power fault.
    	//	USBHCDPowerConfigInit (0, USBHCD_VBUS_AUTO_HIGH | USBHCD_VBUS_FILTER);
    
    
    	// call any open routines here for the application to be ready for a new mass storage device
    		tUSBHMSCInstance *g_psMSCInstance= USBHMSCDriveOpen (0, MSCCallBack);	 // open an instance of the mass storage class driver
    
    		USBHCDInit (0, g_pui8HCDPool, HCD_MEMORY_SIZE);  // initialize the host controller
    
    
    		//LED glow
    		GPIO_PORTF_DATA_R |= 0x16;
    		//for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++);
    		GPIO_PORTF_DATA_R &= ~(0x16);
    		//for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++);
    		// main loop of the application
    		while (1)
    			{
    			switch (iMSDState)
    				{
    				//This state is entered when the pen drive is first detected.
    				case MSD_INIT:
    					{
    					//initialize the newly connected pen drive
    					// wait for the drive to become ready- wait for the value to go to 0 before the host controller attempts to read or write from the device.
    							while ( USBHMSCDriveReady (g_psMSCInstance))
    								{
    								SysCtlDelay(SysCtlClockGet( ) / 3);          //delay given for the device to be ready
    								 ui32DriveTimeout--;
    								 //
    								 // If the timeout is hit then go to the
    								 // STATE_TIMEOUT_DEVICE state.
    								  //
    								   if(ui32DriveTimeout == 0)
    								        {
    								         g_eState = STATE_TIMEOUT_DEVICE;
    								        }
    								}
    						//move it to connected state
    						iMSDState= MSD_CONNECTED;
    						break;
    					}
    				case MSD_CONNECTED:
    					{
    					// glow an LED if the pen drive is detected.
    					GPIO_PORTF_DATA_R |= 0x16;
    					//for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++);
    					GPIO_PORTF_DATA_R &= ~(0x16);
    					//for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++);
    					break;
    					}
    				case MSD_NOT_CONNECTED:
    					{
    					// glow an LED if no device is connected.
    					GPIO_PORTF_DATA_R |= 0x04;
    					//for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++);
    					GPIO_PORTF_DATA_R &= ~(0x04);
    					//for(ui32Loop = 0; ui32Loop < 200000; ui32Loop++);
    					break;
    					}
    				default:
    					{
    					break;
    					}
    				}
    
    			USBHCDMain( );   // Periodic call for the Host Controller Drivers
    
    			}
    
    		}
    
    
    

    I checked it by stepwise debugging. The program control is not going to the call back function.

    Please guide me.

  • FYI: I am trying to only detect the pendrive connected to the board through an OTG cable.!