blob: 4f85be220d10140a510187d64183b3d3c7602483 [file]
/* metadata:
manufacturer: Texas Instruments
*/
#include "bsp/board_api.h"
#include "board.h"
//--------------------------------------------------------------------+
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
void USB0_Handler(void) {
#if CFG_TUH_ENABLED
tuh_int_handler(0, true);
#endif
#if CFG_TUD_ENABLED
tud_int_handler(0);
#endif
}
//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM
//--------------------------------------------------------------------+
static void board_uart_init(void) {
SYSCTL->RCGCUART |= (1 << 0); // Enable the clock to UART0
SYSCTL->RCGCGPIO |= (1 << 0); // Enable the clock to GPIOA
while (!(SYSCTL->PRGPIO & (1 << 0))) {} // Wait for the GPIOA clock to stabilize
while (!(SYSCTL->PRUART & (1 << 0))) {} // Wait for the UART0 clock to stabilize
GPIOA->AFSEL |= (1 << 1) | (1 << 0); // Enable the alternate function on pin PA0 & PA1
GPIOA->PCTL |= (1 << 0) | (1 << 4); // Configure the GPIOPCTL register to select UART0 in PA0 and PA1
GPIOA->DEN |= (1 << 0) | (1 << 1); // Enable the digital functionality in PA0 and PA1
// BAUDRATE = 115200, with SystemCoreClock = 50 Mhz refer manual for calculation
// - BRDI = SystemCoreClock / (16* baud)
// - BRDF = int(fraction*64 + 0.5)
UART0->CTL &= ~(1U << 0); // Disable UART0 by clearing UARTEN bit in the UARTCTL register
UART0->IBRD = 27; // Write the integer portion of the BRD to the UARTIRD register
UART0->FBRD = 8; // Write the fractional portion of the BRD to the UARTFBRD registerer
UART0->LCRH = (0x3 << 5); // 8-bit, no parity, 1 stop bit
UART0->CC = 0x0; // Configure the UART clock source as system clock
UART0->CTL = (1 << 0) | (1 << 8) | (1 << 9); // UART0 Enable, Transmit Enable, Receive Enable
}
static void board_button_init(GPIOA_Type* port, uint8_t PinMsk) {
/* Enable Port Clock */
SYSCTL->RCGCGPIO |= (1 << BTN_PORT_CLK);
/* Let the clock stabilize */
while (!((SYSCTL->PRGPIO) & (1 << BTN_PORT_CLK))) {}
/* Port Digital Enable */
port->DEN |= PinMsk;
/* Set direction */
port->DIR &= ~PinMsk;
/* Enable internal pull so the idle state is deterministic. LaunchPad buttons
* connect the pin to GND when pressed (active-low) and require a pull-up. */
#if BUTTON_STATE_ACTIVE == 0
port->PUR |= PinMsk;
#else
port->PDR |= PinMsk;
#endif
}
static void board_led_init(GPIOA_Type* port, uint8_t PinMsk, uint8_t dirmsk) {
/* Enable Port Clock */
SYSCTL->RCGCGPIO |= (1 << LED_PORT_CLK);
/* Let the clock stabilize */
while (!((SYSCTL->PRGPIO) & (1 << LED_PORT_CLK))) {}
/* Port Digital Enable */
port->DEN |= PinMsk;
/* Set direction */
port->DIR = dirmsk;
}
static void WriteGPIOPin(GPIOA_Type* port, uint8_t PinMsk, bool state) {
if (state) {
port->DATA |= PinMsk;
} else {
port->DATA &= ~(PinMsk);
}
}
static uint32_t ReadGPIOPin(GPIOA_Type* port, uint8_t pinMsk) {
return (port->DATA & pinMsk);
}
void board_init(void) {
#ifdef TM4C123_H
SystemCoreClockUpdate();
#endif
#if CFG_TUSB_OS == OPT_OS_NONE
// 1ms tick timer
SysTick_Config(SystemCoreClock / 1000);
#elif CFG_TUSB_OS == OPT_OS_FREERTOS
// Explicitly disable systick to prevent its ISR from running before scheduler start
SysTick->CTRL &= ~1U;
// If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
#endif
#ifdef TM4C123_H
/* Reset USB */
SYSCTL->SRCR2 |= (1u << 16);
for (volatile uint8_t i = 0; i < 20; i++) {}
SYSCTL->SRCR2 &= ~(1u << 16);
/* Open the USB clock gate */
SYSCTL->RCGCUSB |= (1 << 0);
/* Power-up USB PLL */
SYSCTL->RCC2 &= ~(1u << 14);
/* USB IO Initialization */
SYSCTL->RCGCGPIO |= (1u << 3);
/* Let the clock stabilize */
while (!(SYSCTL->PRGPIO & (1u << 3))) {}
/* USB IOs to Analog Mode */
GPIOD->AFSEL &= ~((1u << 4) | (1u << 5));
GPIOD->DEN &= ~((1u << 4) | (1u << 5));
GPIOD->AMSEL |= ((1u << 4) | (1u << 5));
#else // TM4C129
/* Reset USB */
SYSCTL->SRUSB = 1;
for (volatile uint8_t i = 0; i < 20; i++) {}
SYSCTL->SRUSB = 0;
/* Open the USB clock gate */
SYSCTL->RCGCUSB = 1;
/* Let the clock stabilize */
while(!(SYSCTL->PRUSB & 1)) {}
/* USB IO Initialization */
SYSCTL->RCGCGPIO |= (1u << 10);
/* Let the clock stabilize */
while (!(SYSCTL->PRGPIO & (1u << 10))) {}
/* USB IOs to Analog Mode */
GPIOL->AFSEL &= ~((1u << 6) | (1u << 7));
GPIOL->DEN &= ~((1u << 6) | (1u << 7));
GPIOL->AMSEL |= ((1u << 6) | (1u << 7));
/* USB Clock Configuration */
USB0->CC = 0x207;
#endif
uint8_t leds = 1 << BOARD_LED_PIN;
uint8_t dirmsk = 1 << BOARD_LED_PIN;
/* Configure GPIO for board button */
board_button_init(BOARD_BTN_PORT, BOARD_BTN_Msk);
/* Configure GPIO for board LED */
board_led_init(LED_PORT, leds, dirmsk);
/* Initialize board UART */
board_uart_init();
TU_LOG1_INT(SystemCoreClock);
}
void board_led_write(bool state) {
WriteGPIOPin(LED_PORT, (1 << BOARD_LED_PIN), state);
}
uint32_t board_button_read(void) {
uint32_t gpio_value = ReadGPIOPin(BOARD_BTN_PORT, BOARD_BTN_Msk);
return BUTTON_STATE_ACTIVE ? gpio_value : !gpio_value;
}
size_t board_get_unique_id(uint8_t id[], size_t max_len) {
(void) max_len;
uint8_t const len = 8;
// Note: DID0, DID1 are variant ID, they aer used since TM4C123 does not have unique ID
memcpy(id, (void*)(uintptr_t) &SYSCTL->DID0, len);
return len;
}
int board_uart_write(void const* buf, int len) {
uint8_t const* data = buf;
int count = 0;
while (count < len) {
if ((UART0->FR & (1 << 5)) == 0) { // TX FIFO not full
UART0->DR = data[count];
count++;
} else {
break;
}
}
return count;
}
int board_uart_read(uint8_t* buf, int len) {
(void) buf;
(void) len;
return 0;
}
#if CFG_TUSB_OS == OPT_OS_NONE
volatile uint32_t system_ticks = 0;
void SysTick_Handler(void) {
system_ticks++;
}
uint32_t tusb_time_millis_api(void) {
return system_ticks;
}
#endif