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)
{
}
}