Firmware Skeleton
This step starts the firmware project the robot will run. It sets the clock to 80 MHz and starts the 1 kHz tick. The encoders, the IMU and the balance loop get added to that tick in later steps. It also sets up the buttons and LED, and the menu you'll use to pick each mode.
Hardware: the LaunchPad alone.
What you add
| File | What it does |
|---|---|
clock.c | 80 MHz: crystal, 400 MHz PLL, then DIV400 with divisor 5 |
tick.c | SysTick interrupt at 1 kHz, plus millis() and delay_ms() |
ui.c | RGB LED, SW1 and SW2 (unlocking PF0), 20 ms debounce run in the tick, and ui_menu() |
main.c | Picks a mode from the menu and runs it |
Clone tiva-c-bare as balance-bot and put these files in src/. The Makefile builds every .c file there.
c
// The robot's main program. Each step fills in a mode; for now they only show their colour.
#include "TM4C123.h"
#include "clock.h"
#include "tick.h"
#include "ui.h"
enum { MODE_BALANCE, MODE_DRIVE, MODE_CALIBRATE, MODE_TEST, N_MODES };
static const uint32_t mode_colour[N_MODES] = { LED_GREEN, LED_BLUE, LED_YELLOW, LED_MAGENTA };
static void run(uint32_t mode)
{
// Later steps: balance in place, drive by remote, calibrate the IMU, bench tests.
// Until then a mode "runs" until SW2 is pressed.
(void)mode;
while (!button_pressed(BTN_SW2)) {}
}
int main(void)
{
clock_init();
ui_init();
tick_init();
uint32_t mode = MODE_BALANCE;
while (1)
{
mode = ui_menu(mode_colour, N_MODES, mode);
run(mode);
}
}c
// 80 MHz from the 16 MHz crystal and the 400 MHz PLL (datasheet 5.3, Table 5-6)
#include "TM4C123.h"
#include "clock.h"
#define RCC_MOSCDIS (1U << 0)
#define RCC_XTAL_M (0x1FU << 6)
#define RCC_XTAL_16MHZ (0x15U << 6)
#define RCC2_USERCC2 (1U << 31)
#define RCC2_DIV400 (1U << 30)
#define RCC2_SYSDIV_M (0x7FU << 22) // SYSDIV2 + SYSDIV2LSB
#define RCC2_SYSDIV_80 (4U << 22) // 400 MHz / (4 + 1)
#define RCC2_PWRDN2 (1U << 13)
#define RCC2_BYPASS2 (1U << 11)
#define RCC2_OSCSRC2_M (0x7U << 4) // 0 = main oscillator
#define RIS_PLLLRIS (1U << 6)
void clock_init(void)
{
SYSCTL->RCC2 |= RCC2_USERCC2 | RCC2_BYPASS2; // Run from the raw clock meanwhile
SYSCTL->RCC &= ~RCC_MOSCDIS;
SYSCTL->RCC = (SYSCTL->RCC & ~RCC_XTAL_M) | RCC_XTAL_16MHZ;
SYSCTL->RCC2 &= ~(RCC2_OSCSRC2_M | RCC2_PWRDN2);
SYSCTL->RCC2 |= RCC2_DIV400;
SYSCTL->RCC2 = (SYSCTL->RCC2 & ~RCC2_SYSDIV_M) | RCC2_SYSDIV_80;
while ((SYSCTL->RIS & RIS_PLLLRIS) == 0) {}
SYSCTL->RCC2 &= ~RCC2_BYPASS2;
}c
#pragma once
#include <stdint.h>
#define CPU_HZ 80000000U
void clock_init(void);c
// The 1 kHz system tick. Everything periodic runs from SysTick_Handler:
// buttons now, then the IMU, encoders and balance loop in later steps.
#include "TM4C123.h"
#include "clock.h"
#include "tick.h"
#include "ui.h"
#define STCTRL_ENABLE (1U << 0)
#define STCTRL_INTEN (1U << 1)
#define STCTRL_CLKSRC (1U << 2)
static volatile uint32_t ms;
void tick_init(void)
{
SysTick->CTRL = 0;
SysTick->LOAD = CPU_HZ / TICK_HZ - 1;
SysTick->VAL = 0;
SysTick->CTRL = STCTRL_CLKSRC | STCTRL_INTEN | STCTRL_ENABLE;
}
void SysTick_Handler(void)
{
ms++;
ui_tick();
}
uint32_t millis(void) { return ms; }
void delay_ms(uint32_t n)
{
uint32_t start = ms;
while (ms - start < n) {}
}c
#pragma once
#include <stdint.h>
#define TICK_HZ 1000U
void tick_init(void);
uint32_t millis(void);
void delay_ms(uint32_t ms);c
// LaunchPad buttons and RGB LED: the robot's only user interface
#include "TM4C123.h"
#include "tick.h"
#include "ui.h"
#define RCGC_GPIOF (1U << 5)
#define LEDS LED_WHITE
#define BUTTONS (BTN_SW1 | BTN_SW2)
#define GPIO_KEY 0x4C4F434BU // Unlocks GPIOCR
#define DEBOUNCE_MS 20
#define FLASH_MS 150
static uint8_t held_ms[2]; // SW2, SW1
static volatile uint32_t presses; // Latched press events, one bit per button
void ui_init(void)
{
SYSCTL->RCGCGPIO |= RCGC_GPIOF;
while ((SYSCTL->PRGPIO & RCGC_GPIOF) == 0) {}
GPIOF->LOCK = GPIO_KEY; // PF0 is locked (NMI): unlock it
GPIOF->CR |= BTN_SW2; // before PUR and DEN, or they're dropped
GPIOF->DIR = (GPIOF->DIR | LEDS) & ~BUTTONS;
GPIOF->PUR |= BUTTONS;
GPIOF->DEN |= LEDS | BUTTONS;
led_set(LED_OFF);
}
// Debounce: a press counts once the pin has read low DEBOUNCE_MS in a row
static void debounce(uint32_t button, uint8_t *held, uint32_t down)
{
if (!down)
*held = 0;
else if (*held < DEBOUNCE_MS && ++*held == DEBOUNCE_MS)
presses |= button;
}
void ui_tick(void)
{
uint32_t down = ~GPIOF->DATA & BUTTONS;
debounce(BTN_SW2, &held_ms[0], down & BTN_SW2);
debounce(BTN_SW1, &held_ms[1], down & BTN_SW1);
}
uint32_t button_pressed(uint32_t button)
{
__disable_irq(); // The tick may set another bit mid-update
uint32_t hit = presses & button;
presses &= ~button;
__enable_irq();
return hit != 0;
}
void led_set(uint32_t colour)
{
GPIOF->DATA = (GPIOF->DATA & ~LEDS) | (colour & LEDS);
}
// SW1 steps through n options shown as colours, SW2 picks one: three flashes, then return it
uint32_t ui_menu(const uint32_t *colours, uint32_t n, uint32_t i)
{
button_pressed(BUTTONS); // Forget presses from before the menu
while (1)
{
led_set(colours[i]);
if (button_pressed(BTN_SW1)) i = (i + 1) % n;
if (button_pressed(BTN_SW2)) break;
}
for (uint32_t k = 0; k < 3; k++)
{
led_set(LED_OFF);
delay_ms(FLASH_MS);
led_set(colours[i]);
delay_ms(FLASH_MS);
}
return i;
}c
#pragma once
#include <stdint.h>
// LED colours (PF1 red, PF2 blue, PF3 green)
#define LED_OFF 0U
#define LED_RED (1U << 1)
#define LED_BLUE (1U << 2)
#define LED_GREEN (1U << 3)
#define LED_YELLOW (LED_RED | LED_GREEN)
#define LED_MAGENTA (LED_RED | LED_BLUE)
#define LED_CYAN (LED_BLUE | LED_GREEN)
#define LED_WHITE (LED_RED | LED_BLUE | LED_GREEN)
// Buttons (active low)
#define BTN_SW2 (1U << 0) // PF0: start / back
#define BTN_SW1 (1U << 4) // PF4: next
void ui_init(void);
void ui_tick(void); // Called every 1 ms from the tick
void led_set(uint32_t colour);
uint32_t button_pressed(uint32_t button); // 1 once per press, then 0 until the next
uint32_t ui_menu(const uint32_t *colours, uint32_t n, uint32_t start);Notes
- The clock order is the datasheet's (section 5.3). Set
BYPASS2first, power up the PLL, set the divider, wait forPLLLRIS, then clearBYPASS2. The crystal is off at reset (MOSCDIS = 1), so the PLL can't lock until you clear that bit. - PF0 is locked, because it doubles as the NMI pin. Without writing
LOCKandCRfirst, thePURandDENwrites are dropped silently and SW2 does nothing. PD7 is locked the same way and comes back in Step 5 for the left encoder. - The tick only sets press flags, and main clears them.
button_pressed()clears its flag with interrupts briefly off, so a press the tick records at that moment isn't lost. - The modes are
main.c's table: Balance is green, Drive blue, Calibrate yellow, Test magenta. Later steps fill inrun().
Test
make flash. The LED shows green: the menu, on Balance.- SW1 steps through green, blue, yellow and magenta. Each press moves exactly one mode, even if you hold the button.
- SW2 flashes the colour three times and runs the mode. SW2 again goes back to the menu.
- Clock check: in
main, addwhile (1) { led_set(millis() / 500 % 2 ? LED_RED : LED_OFF); }before the loop. Ten blinks should take 10 s. At the starter's 50 MHz they'd take 16 s. Take the line out again.