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.

Mixing TI driverlib & DRM; TM4C123GH6PM

Genius 3300 points

1. I am relearning TM4C using TM4C launchpad. Earlier i was using DRM only.But now I have shifted to using driverlib, except in some cases where I need fast access like pin toggle or related functions where calling driverlib functions add performance issues.
My code is in two parts: initialzation which is called only once at startup & other is accessed regularly in main code.

2. I have written three codes: GPIO output, GPIO input pull-up , & GPIO input pull-up with interrupt.Below are code for each. I have checked each code on launchpad & they are working.
Is there any other register/bits to be used?

3. Is RED_LED_INVERT function is OK? or it can be shorten ?

4. GPIO output

#define LED_PERIPH    SYSCTL_PERIPH_GPIOF
#define LED_BASE      GPIO_PORTF_AHB_BASE
#define RED_LED       GPIO_PIN_1  

#define RED_LED_ENABLE     HWREG(LED_BASE + (GPIO_O_DATA + (RED_LED << 2))) = RED_LED;   
#define RED_LED_DISABLE()  HWREG(LED_BASE + (GPIO_O_DATA + (RED_LED << 2))) = 0;   
                                                        

static inline void RED_LED_INVERT(void)
{
    uint32_t val;
    val = HWREG(LED_BASE + (GPIO_O_DATA + (RED_LED << 2)));
    HWREG(LED_BASE + (GPIO_O_DATA + (RED_LED << 2))) = (uint8_t)(val ^ RED_LED);
}     

void init(void)
{
    uint32_t ready_count;
    
    SysCtlGPIOAHBEnable(LED_PERIPH);
    SysCtlPeripheralEnable(LED_PERIPH);
    
    ready_count = 2000U;
    while( (!(SysCtlPeripheralReady(LED_PERIPH)))  &&  (--ready_count));
    if(0U == ready_count)   /* if periph not ready take action */
    {
    }    
    
    GPIOPinTypeGPIOOutput(LED_BASE, RED_LED);
    RED_LED_DISABLE();
}

void main(void)
{
    init();
    RED_LED_ENABLE();
    RED_LED_DISABLE();
    RED_LED_INVERT();
    
}

5. GPIO input pull-up

#define BUTTON_PERIPH     SYSCTL_PERIPH_GPIOF
#define BUTTON_BASE       GPIO_PORTF_AHB_BASE
#define BUTTON            GPIO_PIN_4
                                             
static inline uint32_t READ_SW1_VAl(void)
{
    return( HWREG(BUTTON_BASE + (GPIO_O_DATA + (BUTTON << 2))) );
}    


void init(void)
{
    uint32_t ready_count;    
    
    SysCtlGPIOAHBEnable(BUTTON_PERIPH);
    SysCtlPeripheralEnable(BUTTON_PERIPH);
    
    ready_count = 2000U;
    while( (!(SysCtlPeripheralReady(BUTTON_PERIPH)))  &&  (--ready_count));
    if(0U == ready_count)   /* if periph not ready take action */
    {
    }    
    
    GPIOPinTypeGPIOInput(BUTTON_BASE, BUTTON);
    GPIOPadConfigSet(BUTTON_BASE , BUTTON , GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);

}

void main(void)
{
    init();
    while(1)
    {
        if(READ_SW1_VAl())
        {
        }
    }
}

6. GPIO input pull-up with interrupt

#define BUTTON_PERIPH     SYSCTL_PERIPH_GPIOF
#define BUTTON_BASE       GPIO_PORTF_AHB_BASE
#define BUTTON            GPIO_PIN_4
#define BUTTON_INT        GPIO_INT_PIN_4
                                             
static inline uint32_t READ_SW1_VAl(void)
{
    return( HWREG(BUTTON_BASE + (GPIO_O_DATA + (BUTTON << 2))) );
}    

static inline uint32_t READ_BUTTON_INT_STATUS(void)
{
    return( HWREG(BUTTON_BASE + GPIO_O_MIS) );
}    
            
static inline void GPIOF_INT_CLEAR(uint32_t status)
{
    HWREG(GPIO_PORTF_AHB_BASE + GPIO_O_ICR) = status;
}    


void GPIOF_Handler(void)
{
    uint32_t status;

/* get status & clear int flag */    
    status = READ_BUTTON_INT_STATUS();
    GPIOF_INT_CLEAR(status);

    
/* if button int is presses */    
    if((status & BUTTON_INT) == BUTTON_INT)
    {    
        READ_RED_INVERT();
 
        SysCtlDelay(7000000);
    }

}


void init(void)
{
    uint32_t ready_count;    
    
    SysCtlGPIOAHBEnable(BUTTON_PERIPH);
    SysCtlPeripheralEnable(BUTTON_PERIPH);
    
    ready_count = 2000U;
    while( (!(SysCtlPeripheralReady(BUTTON_PERIPH)))  &&  (--ready_count));
    if(0U == ready_count)   /* if periph not ready take action */
    {
    }    
    
    GPIOPinTypeGPIOInput(BUTTON_BASE, BUTTON);
    GPIOPadConfigSet(BUTTON_BASE , BUTTON , GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);

    GPIOIntTypeSet(BUTTON_BASE , BUTTON , GPIO_FALLING_EDGE);
    GPIOIntRegister(BUTTON_BASE , GPIOF_Handler);
    GPIOIntEnable(BUTTON_BASE , BUTTON_INT); 

}

void main(void)
{
    init();
    while(1)
    {
    }
}

  • Hello Vindhyachal,

    The functions done can be replaced by the equivalent GPIOPin functions, why re-invent the wheel?

    Regards
    Amit
  • Hi Amit,

    1. Actually that was for speed. I have defined either macros or inline functions for max speed.
    I will manage them on my owm.

    2. Please check if I had made correct pin toggle., if its ok.
    It is read the pin & then invert the read bot & write it.
    I think there is no direct bit toggle register. So have to do like this.

    static inline void RED_LED_INVERT(void)
    {
    uint32_t val;
    val = HWREG(LED_BASE + (GPIO_O_DATA + (RED_LED << 2)));
    HWREG(LED_BASE + (GPIO_O_DATA + (RED_LED << 2))) = (uint8_t)(val ^ RED_LED);
    }
  • Hello VT,

    Please read the GPIO section on Data Registers. The GPIO Pins are bit banded, so that a write to the PIN_x location (PIN_x << 2) shall only affect the concerned pin state.

    Regards
    Amit
  • Hi Amit,

    1. I was not asking whether other bits will be affected or not. I have tried this code & its working.
    2. I was trying to ask whether this is right method to toggle the pin. First read it & then write the inverted value or I should keep a variable to keep its state. Like

    if(state == 1)
    {
    pin_low();
    state = 0;
    }
    else
    {
    state = 1;
    pin_high();
    }
  • Hello Vindhyachal,

    That should be fine to use the state variable.

    Regards
    Amit
  • Based upon your interest & keenness I'd bet that you'd find any of Joseph Yiu's "Cortex M" books of much value.

    Indeed DRM gains speed - Mr. Yiu's book(s) may suggest further speed-ups...