| 
									
										
										
										
											2021-11-08 17:37:21 +07:00
										 |  |  | #include "TM4C123.h"
 | 
					
						
							|  |  |  | #include "bsp/board.h"
 | 
					
						
							|  |  |  | #include "board.h"
 | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // Forward USB interrupt events to TinyUSB IRQ Handler
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | void USB0_Handler(void) | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-02-23 21:49:11 +07:00
										 |  |  | #if CFG_TUH_ENABLED
 | 
					
						
							| 
									
										
										
										
											2021-12-07 16:28:24 +07:00
										 |  |  |   tuh_int_handler(0); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-23 21:46:40 +07:00
										 |  |  | #if CFG_TUD_ENABLED
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   tud_int_handler(0); | 
					
						
							| 
									
										
										
										
											2021-12-07 16:28:24 +07:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // MACRO TYPEDEF CONSTANT ENUM
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  | static void board_uart_init (void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   SYSCTL->RCGCUART |= (1 << 0);                // Enable the clock to UART0
 | 
					
						
							|  |  |  |   SYSCTL->RCGCGPIO |= (1 << 0);                // Enable the clock to GPIOA
 | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   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
 | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-07 16:28:24 +07:00
										 |  |  |   // BAUDRATE = 115200, with SystemCoreClock = 50 Mhz refer manual for calculation
 | 
					
						
							|  |  |  |   //  - BRDI = SystemCoreClock / (16* baud)
 | 
					
						
							|  |  |  |   //  - BRDF = int(fraction*64 + 0.5)
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   UART0->CTL &= ~(1 << 0);                     // Disable UART0 by clearing UARTEN bit in the UARTCTL register
 | 
					
						
							| 
									
										
										
										
											2021-12-07 16:28:24 +07:00
										 |  |  |   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
 | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   UART0->LCRH = (0x3 << 5);                    // 8-bit, no parity, 1 stop bit
 | 
					
						
							|  |  |  |   UART0->CC = 0x0;                             // Configure the UART clock source as system clock
 | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   UART0->CTL = (1 << 0) | (1 << 8) | (1 << 9); // UART0 Enable, Transmit Enable, Recieve Enable
 | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  | static void initialize_board_led (GPIOA_Type *port, uint8_t PinMsk, uint8_t dirmsk) | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   /* Enable PortF Clock */ | 
					
						
							|  |  |  |   SYSCTL->RCGCGPIO |= (1 << 5); | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   /* Let the clock stabilize */ | 
					
						
							| 
									
										
										
										
											2021-12-07 16:28:24 +07:00
										 |  |  |   while ( !((SYSCTL->PRGPIO) & (1 << 5)) ) {} | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   /* Port Digital Enable */ | 
					
						
							|  |  |  |   port->DEN |= PinMsk; | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   /* Set direction */ | 
					
						
							|  |  |  |   port->DIR = dirmsk; | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  | static void board_switch_init (void) | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   GPIOF->DIR &= ~(1 << BOARD_BTN); | 
					
						
							|  |  |  |   GPIOF->PUR |= (1 << BOARD_BTN); | 
					
						
							|  |  |  |   GPIOF->DEN |= (1 << BOARD_BTN); | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  | static void WriteGPIOPin (GPIOA_Type *port, uint8_t PinMsk, bool state) | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   if ( state ) | 
					
						
							| 
									
										
										
										
											2021-12-07 16:28:24 +07:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |     port->DATA |= PinMsk; | 
					
						
							| 
									
										
										
										
											2021-12-07 16:28:24 +07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2021-12-07 16:28:24 +07:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |     port->DATA &= ~(PinMsk); | 
					
						
							| 
									
										
										
										
											2021-12-07 16:28:24 +07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  | static uint32_t ReadGPIOPin (GPIOA_Type *port, uint8_t pinMsk) | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   return (port->DATA & pinMsk); | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  | void board_init (void) | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   SystemCoreClockUpdate(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | #if CFG_TUSB_OS == OPT_OS_NONE
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   // 1ms tick timer
 | 
					
						
							|  |  |  |   SysTick_Config(SystemCoreClock / 1000); | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | #elif CFG_TUSB_OS == OPT_OS_FREERTOS
 | 
					
						
							|  |  |  |     // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
 | 
					
						
							|  |  |  |     NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   /* Reset USB */ | 
					
						
							|  |  |  |   SYSCTL->SRCR2 |= (1u << 16); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for ( volatile uint8_t i = 0; i < 20; i++ ) {} | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   SYSCTL->SRCR2 &= ~(1u << 16); | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   /* Open the USB clock gate */ | 
					
						
							|  |  |  |   SYSCTL->RCGCUSB |= (1 << 0); | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   /* Power-up USB PLL */ | 
					
						
							|  |  |  |   SYSCTL->RCC2 &= ~(1u << 14); | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   /* USB IO Initialization */ | 
					
						
							|  |  |  |   SYSCTL->RCGCGPIO |= (1u << 3); | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   /* Let the clock stabilize */ | 
					
						
							| 
									
										
										
										
											2021-12-07 16:28:24 +07:00
										 |  |  |   while ( !(SYSCTL->PRGPIO & (1u << 3)) ) {} | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   /* USB IOs to Analog Mode */ | 
					
						
							|  |  |  |   GPIOD->AFSEL &= ~((1u << 4) | (1u << 5)); | 
					
						
							|  |  |  |   GPIOD->DEN &= ~((1u << 4) | (1u << 5)); | 
					
						
							|  |  |  |   GPIOD->AMSEL |= ((1u << 4) | (1u << 5)); | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   uint8_t leds = (1 << LED_PIN_RED) | (1 << LED_PIN_BLUE) | (1 << LED_PIN_GREEN); | 
					
						
							|  |  |  |   uint8_t dirmsk = (1 << LED_PIN_RED) | (1 << LED_PIN_BLUE) | (1 << LED_PIN_GREEN); | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   /* Configure GPIO for board LED */ | 
					
						
							|  |  |  |   initialize_board_led(LED_PORT, leds, dirmsk); | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   /* Configure GPIO for board switch */ | 
					
						
							|  |  |  |   board_switch_init(); | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   /* Initialize board UART */ | 
					
						
							|  |  |  |   board_uart_init(); | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-07 16:28:24 +07:00
										 |  |  |   TU_LOG1_INT(SystemCoreClock); | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  | void board_led_write (bool state) | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   WriteGPIOPin(LED_PORT, (1 << LED_PIN_BLUE), state); | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  | uint32_t board_button_read (void) | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   uint32_t gpio_value = ReadGPIOPin(BOARD_BTN_PORT, BOARD_BTN_Msk); | 
					
						
							|  |  |  |   return BUTTON_STATE_ACTIVE ? gpio_value : !gpio_value; | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  | int board_uart_write (void const *buf, int len) | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   uint8_t const * data = buf; | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   for ( int i = 0; i < len; i++ ) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     while ( (UART0->FR & (1 << 5)) != 0 ) {} // Poll until previous data was shofted out
 | 
					
						
							|  |  |  |     UART0->DR = data[i];                     // Write UART0 DATA REGISTER
 | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   return len; | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  | int board_uart_read (uint8_t *buf, int len) | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  |   (void) buf; | 
					
						
							|  |  |  |   (void) len; | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if CFG_TUSB_OS == OPT_OS_NONE
 | 
					
						
							|  |  |  | volatile uint32_t system_ticks = 0; | 
					
						
							|  |  |  | void SysTick_Handler (void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   system_ticks++; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 16:41:08 +07:00
										 |  |  | uint32_t board_millis (void) | 
					
						
							| 
									
										
										
										
											2021-11-07 13:41:32 +07:00
										 |  |  | { | 
					
						
							|  |  |  |   return system_ticks; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 |