Introduction
This page contains snippets of code used to initialize specific functions of the Texas Instruments TM4C123GH6PM microcontroller. It assumes the presence of the tm4c123gh6pm.h header file for the definition of the constants.
Open-source toolchain Cross compiler: https://launchpad.net/gcc-arm-embedded
Extract to /usr/local/
Flash tools: https://github.com/utzig/lm4tools
Clone github repo and build lm4flash
TI TivaWare: http://www.ti.com/tool/SW-EK-TM4C123GXL
On-board debugging
You need a .AXF with debugging information in it (build sources with -g).
Step 1: start OpenOCD:
/Applications/GNU\ ARM\ Eclipse/OpenOCD/0.10.0-201601101000-dev/bin/openocd -f /Applications/GNU\ ARM\ Eclipse/OpenOCD/0.10.0-201601101000-dev/scripts/board/ek-tm4c123gxl.cfg
Step 2: start GDB:
arm-none-eabi-gdb bin/netradio.axf
where bin/netradio.axf is the .AXF that you want to debug on the board.
Step 3: connect to the board:
(gdb) target extended-remote :3333
Step 4: download the program to the board:
(gdb) load
Step 5: set a breakpoint at main:
(gdb) break main
Step 6: start the program:
(gdb) c
Snippets
GPIO port initialization: digital output
volatile unsigned long delay; SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOx; // activate port x (x = A...F) delay = SYSCTL_RCGC2_R; // allow time for clock to stabilize GPIO_PORTx_DIR_R |= ... ; // set pins as output GPIO_PORTx_AMSEL_R &= ~...; // disable analog functions GPIO_PORTx_AFSEL_R &= ~...; // disable alternative functions GPIO_PORTx_DEN_R |= ... ; // enable digital I/O on selected ports
GPIO port initialization: digital input
volatile unsigned long delay; SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOx; // activate port x (x = A...F) delay = SYSCTL_RCGC2_R; // allow time for clock to stabilize GPIO_PORTx_DIR_R &= ~... ; // set pins as input GPIO_PORTx_AMSEL_R &= ~...; // disable analog functions GPIO_PORTx_AFSEL_R &= ~...; // disable alternative functions GPIO_PORTx_PUR_R |= ...; // enable pull-up resistors for input pins (PDR for pull down) GPIO_PORTx_DEN_R |= ... ; // enable digital I/O on selected ports
GPIO port initialization: analog input (ADC)
The ADC description starts on page 799 of the TM4C123GH6PM datasheet.
Initialization:
volatile unsigned long delay; // Configure PE2 as Ain1 SYSCTL_RCGC2_R |= 0x10; delay = SYSCTL_RCGC2_R; GPIO_PORTE_DIR_R &= ~0x04; // PE2 (Ain1) = input GPIO_PORTE_AFSEL_R |= 0x04; // alt. function for PE2 GPIO_PORTE_DEN_R &= ~0x04; // disable digital I/O on PE2 GPIO_PORTE_AMSEL_R |= 0x04; // analog function for PE2 // Activate & configure ADC0 with Ain1 SYSCTL_RCGC0_R |= 0x00010000; // activate ADC0 SYSCTL_RCGC0_R &= ~0x0300; // configure for 125K ADC0_SSPRI_R = 0x0123; // sequencer 3 is highest priority ADC0_ACTSS_R &= ~0x08; // disable SS3 for config ADC0_EMUX_R &= ~0xF000; // config SS3 as software trigger ADC0_SSMUX3_R = (ADC0_SSMUX3_R & 0xFFFFFFF0) + 1; // use channel Ain1 (PE2) ADC0_SSCTL3_R = 0x06; ADC0_ACTSS_R |= 0x08; // enable SS3
Active wait:
unsigned long result; ADC0_PSSI_R = 0x08; // start SS3 while ((ADC0_RIS_R & 0x08) == 0); // wait until sampling done result = (ADC0_SSFIFO3_R & 0x0FFF); // read result
SysTick initialization and use
With interrupt
Initialization:
NVIC_ST_CTRL_R = 0; NVIC_ST_RELOAD_R = x; // x = reload value NVIC_ST_CURRENT_R = 0; NVIC_SYS_PRI3_R = NVIC_SYS_PRI3_R & 0X00FFFFFF; // priority 0 NVIC_ST_CTRL_R = 0x07; // enable with core clock and interrupts
Interrupt handler:
void SysTick_Handler(void) { ... }
With active wait
Initialization:
NVIC_ST_CTRL_R = 0; NVIC_ST_CTRL_R = 0x05;
Wait:
NVIC_ST_RELOAD_R = x-1; // x = countdown value NVIC_ST_CURRENT_R = 0; while ((NVIC_ST_CTRL_R & 0x00010000) == 0);