GPIO Port Configuration - glennlopez/EmbeddedSystems.Playground GitHub Wiki

[WIKI ENTRY IS WORK IN PROGRESS]

Address definitions

// System Control Legacy base address: 0x400F.E000 (Datasheet pg. 234)
#define SYSCTL_RCGC2_R          (*((volatile unsigned long *)0x400FE108))

// PortF(APB) base address: 0x40025000 (Datasheet pg. 657)
#define GPIO_PORTF_DATA_R       (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTF_DEN_R        (*((volatile unsigned long *)0x4002551C))
#define GPIO_PORTF_DIR_R        (*((volatile unsigned long *)0x40025400))
#define GPIO_PORTF_AMSEL_R      (*((volatile unsigned long *)0x40025528))
#define GPIO_PORTF_AFSEL_R      (*((volatile unsigned long *)0x40025420))
#define GPIO_PORTF_PCTL_R       (*((volatile unsigned long *)0x4002552C))

// Prototypes
void initPortF(void);
void delay(unsigned int param);

Port Initialization

void initPortF(void){    unsigned long volatile delay;

    // Port Clock Control
    SYSCTL_RCGC2_R          |=      0x00000020;
    delay  /*Wait Ready*/    =      SYSCTL_RCGC2_R;

    // GPIO Digital Control
    GPIO_PORTF_DEN_R        |=      0x0E;
    GPIO_PORTF_DIR_R        |=      0x0E;

    // GPIO Alternate function control
    GPIO_PORTF_AMSEL_R      &=      ~0x0E;
    GPIO_PORTF_AFSEL_R      &=      ~0x0E;
    GPIO_PORTF_PCTL_R       &=      ~0x0000FFF0;
}

Main Function Routine

void main(void) {
    initPortF();                     // Initialize PortF

    while(1){
        GPIO_PORTF_DATA_R ^= 0x04;   // Toggle LED on/off
        delay(100);                  // Up/Down time
    }
	
}

// Busy-wait delay 
void delay(unsigned int param){ unsigned int i, j;

    for(j = 0; j < param; j++){
        for(i = 0; i < 15; i++){
            // do nothing
        }
    }
}

Adding: Bit-specific Addressing

Add the register definitions below to the ones in the begining of this wiki.

// PortF Bit-specific Address: (7|200, 6|100, 5|80, 4|40, 3|20, 2|10, 1|08, 0|04)
#define LED_B                   (*((volatile unsigned long *)0x40025010))
#define LED_R                   (*((volatile unsigned long *)0x40025008))
#define LED_G                   (*((volatile unsigned long *)0x40025020))
while(1){
   LED_G ^= ON;
   delay(param);
}


Adding: Inputs (SW1 & SW2)

Add the register definitions below to the ones in the begining of this wiki.

// PortF Input configuration registers
#define GPIO_PORTF_PUR_R        (*((volatile unsigned long *)0x40025510))
#define GPIO_PORTF_PDR_R        (*((volatile unsigned long *)0x40025514))

// PortF Unlock registers (Only needed for PF0 PD7)
#define GPIO_PORTF_LOCK_R       (*((volatile unsigned long *)0x40025520))
#define GPIO_PORTF_CR_R         (*((volatile unsigned long *)0x40025524))

// PortF Bit-specific Address
#define SW1                     (*((volatile unsigned long *)0x40025040))
#define SW2                     (*((volatile unsigned long *)0x40025004))

The Initialization routine is as follows...

// Initialize PortF Input (SW1 & SW2)
void initPortF_in(void){

    //UNLOCK PF0
    GPIO_PORTF_LOCK_R        =      0x4C4F434B;
    GPIO_PORTF_CR_R         |=      0x01;

    // GPIO Digital Control
    GPIO_PORTF_DEN_R        |=      0x11;
    GPIO_PORTF_DIR_R        &=      ~0x11;
    GPIO_PORTF_PUR_R        |=       0x11;

    // GPIO Alternate function control
    GPIO_PORTF_AMSEL_R       =      0;
    GPIO_PORTF_AFSEL_R      &=      ~0x11;
    GPIO_PORTF_PCTL_R       &=      ~0x000F000F;
}