Skip to content

Commit

Permalink
enable display and add st7735 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
bhav97 committed Sep 5, 2021
1 parent b368f25 commit 1e488dd
Show file tree
Hide file tree
Showing 12 changed files with 1,081 additions and 147 deletions.
4 changes: 4 additions & 0 deletions ports/stm32h7/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ LD_FILES ?= $(PORT_DIR)/linker/stm32h750xx_flash_CM7.ld
# Source
PORT_SRC_C += \
$(addprefix $(CURRENT_PATH)/, $(wildcard *.c)) \
$(CURRENT_PATH)/components/st7735/st7735.c \
$(CURRENT_PATH)/components/st7735/fonts.c \
$(ST_CMSIS)/Source/Templates/system_stm32h7xx.c \
$(ST_HAL_DRIVER)/Src/stm32h7xx_hal.c \
$(ST_HAL_DRIVER)/Src/stm32h7xx_hal_cortex.c \
Expand All @@ -38,6 +40,7 @@ PORT_SRC_C += \
$(ST_HAL_DRIVER)/Src/stm32h7xx_hal_flash.c \
$(ST_HAL_DRIVER)/Src/stm32h7xx_hal_flash_ex.c \
$(ST_HAL_DRIVER)/Src/stm32h7xx_hal_uart.c \
$(ST_HAL_DRIVER)/Src/stm32h7xx_hal_spi.c \
$(ST_HAL_DRIVER)/Src/stm32h7xx_hal_pwr.c \
$(ST_HAL_DRIVER)/Src/stm32h7xx_hal_pwr_ex.c

Expand All @@ -49,6 +52,7 @@ SRC_C += \
INC += \
$(TOP)/$(BOARD_PATH) \
$(TOP)/$(CMSIS_5)/CMSIS/Core/Include \
$(CURRENT_PATH)/components/st7735/ \
$(TOP)/$(ST_CMSIS)/Include \
$(TOP)/$(ST_HAL_DRIVER)/Inc

Expand Down
73 changes: 73 additions & 0 deletions ports/stm32h7/board_hmi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "board_api.h"

#if (TINYUF2_DISPLAY == 1U)
#include "components/st7735/st7735.h"

SPI_HandleTypeDef _display_spi;
#endif // TINYUF2_DISPLAY == 1U

//--------------------------------------------------------------------+
// Board Display Callouts
//--------------------------------------------------------------------+
#if (TINYUF2_DISPLAY == 1)
void ST7735_Transmit(uint8_t * buffer, uint16_t len, uint32_t timeout)
{
HAL_SPI_Transmit(&_display_spi, buffer, len, timeout);
}

void ST7735_Delay(uint32_t ms)
{
HAL_Delay(ms);
}
#endif

void board_display_init(void)
{
#if (TINYUF2_DISPLAY == 1U)
display_init(&_display_spi);
ST7735_Init();
// Clear previous screen
ST7735_FillScreen(ST7735_BLACK);
#endif // TINYUF2_DISPLAY == 1U
}

// The application draws a complete frame in memory and sends it
// line-by-line to the display
void board_display_draw_line(int y, uint16_t* pixel_color, uint32_t pixel_num)
{
#if (TINYUF2_DISPLAY == 1U)
for (uint32_t x = 0; x < pixel_num; x += 1) {
ST7735_DrawPixel(y, x, pixel_color[x]);
}
#endif // TINYUF2_DISPLAY == 1U
}


//--------------------------------------------------------------------+
// LED pattern
//--------------------------------------------------------------------+
void board_led_write(uint32_t state)
{
#if defined(TINYUF2_LED)
HAL_GPIO_WritePin(LED_PORT, LED_PIN, state ? GPIO_PIN_SET : GPIO_PIN_RESET);
#endif // defined(LED_PIN)
}

//--------------------------------------------------------------------+
// Button
//--------------------------------------------------------------------+
uint32_t board_button_read(void)
{
#if defined(BUTTON_PIN)
return (BUTTON_STATE_ACTIVE == HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) ? 1: 0;
#endif // defined(BUTTON_PIN)
}

//--------------------------------------------------------------------+
// Neopixel color for status
//--------------------------------------------------------------------+
void board_rgb_write(uint8_t const rgb[])
{
// TODO: copy neopixel code from f4 port
(void) rgb;
}
74 changes: 74 additions & 0 deletions ports/stm32h7/board_irq.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "board_api.h"
#include "tusb.h"

#ifndef TINYUF2_SELF_UPDATE

//--------------------------------------------------------------------+
// Core Interrupts
//--------------------------------------------------------------------+

// Forward USB interrupt events to TinyUSB IRQ Handler
// void OTG_HS_EP1_OUT_IRQHandler(void)
// void OTG_HS_EP1_IN_IRQHandler(void)
// void OTG_HS_WKUP_IRQHandler(void)
// void OTG_FS_EP1_OUT_IRQHandler(void)
// void OTG_FS_EP1_IN_IRQHandler(void)
// void OTG_FS_WKUP_IRQHandler(void)

void OTG_FS_IRQHandler(void)
{
tud_int_handler(0);
}

void OTG_HS_IRQHandler(void)
{
tud_int_handler(1);
}
#endif

//--------------------------------------------------------------------+
// Core Exceptions
//--------------------------------------------------------------------+
void SysTick_Handler(void)
{
HAL_IncTick();
board_timer_handler();
}

#define TRAP_EXC
#if defined(TRAP_EXC)

typedef struct __attribute__((packed)) CtxFrame {
uint32_t r0;
uint32_t r1;
uint32_t r2;
uint32_t r3;
uint32_t r12;
uint32_t lr;
uint32_t ret;
uint32_t xpsr;
} sFrame;

#define HARDFAULT_HANDLER(_x) \
asm volatile("tst lr, #4 \n" \
"ite eq \n" \
"mrseq r0, msp \n" \
"mrsne r0, psp \n" \
"b fault_handler \n")

#define R ": %lx"
#define EXC_FMT "r0" R "r1" R "r2" R "r3" R "r12" R "lr" R "ret" R "xpsr" R "\n"
#define EXC_DET(x) x->r0, x->r1, x->r2, x->r3, x->r12, x->lr, x->ret, x->xpsr

__attribute__((optimize("O0")))
void fault_handler(sFrame *fr)
{
printf(EXC_FMT, EXC_DET(fr));
asm("bkpt #0");
}

void HardFault_Handler(void)
{
HARDFAULT_HANDLER();
}
#endif // defined(TRAP_EXC)
133 changes: 11 additions & 122 deletions ports/stm32h7/boards.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "stm32h7xx_hal.h"
#include "board_api.h"
#include "tusb.h"

// This is the UID register
// Change or be met with an exception on Get_Serial requests
Expand All @@ -12,9 +11,6 @@ void board_init(void)
clock_init();
SystemCoreClockUpdate();

// disable systick
board_timer_stop();

// TODO: remove unused clocks
// But some boards might end up using them
__HAL_RCC_GPIOA_CLK_ENABLE();
Expand All @@ -34,8 +30,7 @@ void board_init(void)
#ifdef BUTTON_PIN
GPIO_InitStruct.Pin = BUTTON_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct);
#endif

Expand All @@ -52,6 +47,11 @@ void board_init(void)
// Configure USB for DFU
void board_dfu_init(void)
{
// disable systick
board_timer_stop();
// 1ms systick
board_timer_start(1);

// Not quite sure what an RHPORT is :/
#if BOARD_DEVICE_RHPORT_NUM == 0
GPIO_InitTypeDef GPIO_InitStruct;
Expand Down Expand Up @@ -98,21 +98,16 @@ void board_dfu_complete(void)
NVIC_SystemReset();
}

// void board_millis(void)
// {

// }


bool board_app_valid(void)
{
volatile uint32_t const * app_vector = (volatile uint32_t const*) BOARD_FLASH_APP_START;

// 1st word should be in SRAM region
// 2nd word should be application reset in flash

// 2nd word should be application reset
if (app_vector[1] < BOARD_FLASH_APP_START || app_vector[1] > BOARD_FLASH_APP_START + BOARD_FLASH_SIZE) {
return false;
// reset vector and stack pointer should not be erase
if ((app_vector[0] != 0xFFFFFFFFU) && (app_vector[1] != 0xFFFFFFFFU)) {
return true;
}

// TODO: implement this
Expand Down Expand Up @@ -147,13 +142,6 @@ void board_app_jump(void)

HAL_RCC_DeInit();

SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;

// TODO: protect bootloader region, MPU?
// TODO: maybe I could add secure boot :D

// Setup VTOR to point to application vectors
SCB->VTOR = (uint32_t) BOARD_FLASH_APP_START;

Expand All @@ -165,33 +153,14 @@ void board_app_jump(void)

}

// USB Get_Serial response
uint8_t board_usb_get_serial(uint8_t serial_id[16])
{
uint8_t const len = 12;
memcpy(serial_id, STM32_UUID, len);
return len;
}

//--------------------------------------------------------------------+
// LED pattern
//--------------------------------------------------------------------+

void board_led_write(uint32_t state)
{
HAL_GPIO_WritePin(LED_PORT, LED_PIN, state ? GPIO_PIN_SET : GPIO_PIN_RESET);
}

uint32_t board_button_read(void)
{
return (BUTTON_STATE_ACTIVE == HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) ? 1: 0;
}

void board_rgb_write(uint8_t const rgb[])
{
// TODO: copy neopixel code from f4 port
(void) rgb;
}

//--------------------------------------------------------------------+
// Timer
//--------------------------------------------------------------------+
Expand All @@ -206,95 +175,15 @@ void board_timer_stop(void)
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
}

void SysTick_Handler(void)
{
board_timer_handler();
}

int board_uart_write(void const * buf, int len)
{
(void) buf; (void) len;
return 0;
}

#ifndef TINYUF2_SELF_UPDATE

// Forward USB interrupt events to TinyUSB IRQ Handler
void OTG_HS_EP1_OUT_IRQHandler(void)
{

}

void OTG_HS_EP1_IN_IRQHandler(void)
{

}

void OTG_HS_WKUP_IRQHandler(void)
{

}


void OTG_FS_EP1_OUT_IRQHandler(void)
{

}

void OTG_FS_EP1_IN_IRQHandler(void)
{

}

void OTG_FS_WKUP_IRQHandler(void)
{

}

void OTG_FS_IRQHandler(void)
{
tud_int_handler(0);
}

void OTG_HS_IRQHandler(void)
{
tud_int_handler(1);
}
#endif

// Required by __libc_init_array in startup code if we are compiling using
// -nostdlib/-nostartfiles
void _init(void)
{

}

typedef struct __attribute__((packed)) CtxFrame {
uint32_t r0;
uint32_t r1;
uint32_t r2;
uint32_t r3;
uint32_t r12;
uint32_t lr;
uint32_t return_addr;
uint32_t xpsr;
} sFrame;

#define HARDFAULT_HANDLER(_x) \
asm volatile("tst lr, #4 \n" \
"ite eq \n" \
"mrseq r0, msp \n" \
"mrsne r0, psp \n" \
"b fault_handler \n")

__attribute__((optimize("O0")))
void fault_handler(sFrame *frame)
{
asm("bkpt #0");
}

void HardFault_Handler(void)
{
HARDFAULT_HANDLER();
// asm("bkpt #0");
}
Loading

0 comments on commit 1e488dd

Please sign in to comment.