From 9c0a8622c26743583966b653704d5bfc8b4c0c7d Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 4 Apr 2023 16:24:10 +0200 Subject: full ppu IO in stm code (h/vblank interrupts) --- src/main.c | 2 ++ src/main.h | 3 +++ src/ppu/stm.c | 2 ++ src/stm32/consts.h | 42 +++++++++++++++++++++++++++++++++++ src/stm32/input.c | 18 +++++++++++---- src/stm32/main.c | 7 +----- src/stm32/setup.c | 65 +++++++++++++++++++++++++++++++++++++++++++----------- src/stm32/setup.h | 5 ++--- 8 files changed, 118 insertions(+), 26 deletions(-) create mode 100644 src/stm32/consts.h (limited to 'src') diff --git a/src/main.c b/src/main.c index a3a4351..67490ab 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,8 @@ #include "ppu/internals.h" bool g_hh_run = true; +volatile unsigned long g_hh_vcount; +volatile unsigned short g_hh_hcount; void hh_ppu_vblank_interrupt() { static unsigned long frame = 0; diff --git a/src/main.h b/src/main.h index 8e94f18..b2003d3 100644 --- a/src/main.h +++ b/src/main.h @@ -2,6 +2,9 @@ #include +extern volatile unsigned long g_hh_vcount; +extern volatile unsigned short g_hh_hcount; + extern bool g_hh_run; /** @brief exec entrypoint */ diff --git a/src/ppu/stm.c b/src/ppu/stm.c index 72e46ae..e1d7e5d 100644 --- a/src/ppu/stm.c +++ b/src/ppu/stm.c @@ -18,6 +18,8 @@ void hh_ppu_vram_dwrite(uint8_t* data, size_t size) { HAL_SPI_Transmit(&hspi1, (uint8_t[4]){ 0xff }, 4, HAL_MAX_DELAY); // reset SPI HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET); + // HAL_Delay(100); // TODO: remove HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); + // HAL_Delay(100); // TODO: remove } diff --git a/src/stm32/consts.h b/src/stm32/consts.h new file mode 100644 index 0000000..447e36e --- /dev/null +++ b/src/stm32/consts.h @@ -0,0 +1,42 @@ +#pragma once + +#include + +// ../../docs/hardware/pinout.md +#define HH_IO_SPI_PORT GPIOA +#define HH_IO_SPI_PINS (GPIO_PIN_5 | GPIO_PIN_7) + +#define HH_IO_SPI_SR_PORT GPIOA +#define HH_IO_SPI_SR_PIN GPIO_PIN_9 + +#define HH_IO_PPU_HBLANK_PORT GPIOA +#define HH_IO_PPU_HBLANK_PIN GPIO_PIN_8 +#define HH_IO_PPU_VBLANK_PORT GPIOC +#define HH_IO_PPU_VBLANK_PIN GPIO_PIN_7 + +#define HH_IO_GP1_UP_PORT GPIOB +#define HH_IO_GP1_UP_PIN GPIO_PIN_5 +#define HH_IO_GP1_DOWN_PORT GPIOA +#define HH_IO_GP1_DOWN_PIN GPIO_PIN_3 +#define HH_IO_GP1_LEFT_PORT GPIOB +#define HH_IO_GP1_LEFT_PIN GPIO_PIN_3 +#define HH_IO_GP1_RIGHT_PORT GPIOB +#define HH_IO_GP1_RIGHT_PIN GPIO_PIN_4 +#define HH_IO_GP1_PRIMARY_PORT GPIOA +#define HH_IO_GP1_PRIMARY_PIN GPIO_PIN_10 +#define HH_IO_GP1_SECONDARY_PORT GPIOA +#define HH_IO_GP1_SECONDARY_PIN GPIO_PIN_2 + +#define HH_IO_GP2_UP_PORT GPIOC +#define HH_IO_GP2_UP_PIN GPIO_PIN_1 +#define HH_IO_GP2_DOWN_PORT GPIOA +#define HH_IO_GP2_DOWN_PIN GPIO_PIN_0 +#define HH_IO_GP2_LEFT_PORT GPIOB +#define HH_IO_GP2_LEFT_PIN GPIO_PIN_0 +#define HH_IO_GP2_PRIMARY_PORT GPIOC +#define HH_IO_GP2_PRIMARY_PIN GPIO_PIN_0 +#define HH_IO_GP2_RIGHT_PORT GPIOA +#define HH_IO_GP2_RIGHT_PIN GPIO_PIN_4 +#define HH_IO_GP2_SECONDARY_PORT GPIOA +#define HH_IO_GP2_SECONDARY_PIN GPIO_PIN_1 + diff --git a/src/stm32/input.c b/src/stm32/input.c index e2d07cb..ee1a262 100644 --- a/src/stm32/input.c +++ b/src/stm32/input.c @@ -1,13 +1,23 @@ #include #include "input.h" +#include "stm32/consts.h" hh_s_gamepad g_hh_controller_p1 = { 0 }; hh_s_gamepad g_hh_controller_p2 = { 0 }; void hh_input_read() { - g_hh_controller_p1.dpad_left = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4); - g_hh_controller_p1.dpad_right = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5); - g_hh_controller_p1.dpad_down = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6); - g_hh_controller_p1.dpad_up = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8); + g_hh_controller_p1.dpad_up = HAL_GPIO_ReadPin(HH_IO_GP1_UP_PORT, HH_IO_GP1_UP_PIN); + g_hh_controller_p1.dpad_down = HAL_GPIO_ReadPin(HH_IO_GP1_DOWN_PORT, HH_IO_GP1_DOWN_PIN); + g_hh_controller_p1.dpad_left = HAL_GPIO_ReadPin(HH_IO_GP1_LEFT_PORT, HH_IO_GP1_LEFT_PIN); + g_hh_controller_p1.dpad_right = HAL_GPIO_ReadPin(HH_IO_GP1_RIGHT_PORT, HH_IO_GP1_RIGHT_PIN); + g_hh_controller_p1.button_primary = HAL_GPIO_ReadPin(HH_IO_GP1_PRIMARY_PORT, HH_IO_GP1_PRIMARY_PIN); + g_hh_controller_p1.button_secondary = HAL_GPIO_ReadPin(HH_IO_GP1_SECONDARY_PORT, HH_IO_GP1_SECONDARY_PIN); + + g_hh_controller_p2.dpad_up = HAL_GPIO_ReadPin(HH_IO_GP2_UP_PORT, HH_IO_GP2_UP_PIN); + g_hh_controller_p2.dpad_down = HAL_GPIO_ReadPin(HH_IO_GP2_DOWN_PORT, HH_IO_GP2_DOWN_PIN); + g_hh_controller_p2.dpad_left = HAL_GPIO_ReadPin(HH_IO_GP2_LEFT_PORT, HH_IO_GP2_LEFT_PIN); + g_hh_controller_p2.dpad_right = HAL_GPIO_ReadPin(HH_IO_GP2_RIGHT_PORT, HH_IO_GP2_RIGHT_PIN); + g_hh_controller_p2.button_primary = HAL_GPIO_ReadPin(HH_IO_GP2_PRIMARY_PORT, HH_IO_GP2_PRIMARY_PIN); + g_hh_controller_p2.button_secondary = HAL_GPIO_ReadPin(HH_IO_GP2_SECONDARY_PORT, HH_IO_GP2_SECONDARY_PIN); } diff --git a/src/stm32/main.c b/src/stm32/main.c index fb548d8..b7b4604 100644 --- a/src/stm32/main.c +++ b/src/stm32/main.c @@ -1,14 +1,9 @@ #include #include "main.h" -#include "ppu/ppu.h" void hh_ppu_load_tilemap() {} void hh_loop() { - // TODO: call hh_ppu_vblank_interrupt on interrupt instead of manually - while (1) { - hh_ppu_vblank_interrupt(); - HAL_Delay(16); - } + while(1); } diff --git a/src/stm32/setup.c b/src/stm32/setup.c index 9de1f48..f692159 100644 --- a/src/stm32/setup.c +++ b/src/stm32/setup.c @@ -9,6 +9,7 @@ #include "setup.h" #include "demo.h" #include "ppu/ppu.h" +#include "stm32/consts.h" UART_HandleTypeDef huart2 = { .Instance = USART2, @@ -131,22 +132,44 @@ void hh_io_tim_setup() { }) != HAL_OK) return hh_io_setup_error_handler(); } +static void gpio_init(GPIO_TypeDef* port, uint16_t pin, uint32_t mode, uint32_t pull) { + HAL_GPIO_Init(port, &(GPIO_InitTypeDef) { + .Pin = pin, + .Mode = mode, + .Pull = pull, + .Speed = GPIO_SPEED_FREQ_HIGH, + }); +} + void hh_io_gpio_setup() { __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); - - HAL_GPIO_Init(GPIOA, &(GPIO_InitTypeDef) { - .Pin = GPIO_PIN_9, - .Mode = GPIO_MODE_OUTPUT_PP, - .Pull = GPIO_NOPULL, - .Speed = GPIO_SPEED_FREQ_HIGH, - }); - HAL_GPIO_Init(GPIOA, &(GPIO_InitTypeDef) { - .Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_8, - .Mode = GPIO_MODE_INPUT, - .Pull = GPIO_PULLDOWN, - }); - HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); + __HAL_RCC_GPIOC_CLK_ENABLE(); + + // SPI reset line + gpio_init(HH_IO_SPI_SR_PORT, HH_IO_SPI_SR_PIN, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL); + + // PPU HBLANK/VBLANK + gpio_init(HH_IO_PPU_HBLANK_PORT, HH_IO_PPU_HBLANK_PIN, GPIO_MODE_IT_RISING, GPIO_NOPULL); + gpio_init(HH_IO_PPU_VBLANK_PORT, HH_IO_PPU_VBLANK_PIN, GPIO_MODE_IT_RISING, GPIO_NOPULL); + HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(EXTI4_15_IRQn); + + // gamepad 1 + gpio_init(HH_IO_GP1_UP_PORT, HH_IO_GP1_UP_PIN, GPIO_MODE_INPUT, GPIO_PULLDOWN); + gpio_init(HH_IO_GP1_DOWN_PORT, HH_IO_GP1_DOWN_PIN, GPIO_MODE_INPUT, GPIO_PULLDOWN); + gpio_init(HH_IO_GP1_LEFT_PORT, HH_IO_GP1_LEFT_PIN, GPIO_MODE_INPUT, GPIO_PULLDOWN); + gpio_init(HH_IO_GP1_RIGHT_PORT, HH_IO_GP1_RIGHT_PIN, GPIO_MODE_INPUT, GPIO_PULLDOWN); + gpio_init(HH_IO_GP1_PRIMARY_PORT, HH_IO_GP1_PRIMARY_PIN, GPIO_MODE_INPUT, GPIO_PULLDOWN); + gpio_init(HH_IO_GP1_SECONDARY_PORT, HH_IO_GP1_SECONDARY_PIN, GPIO_MODE_INPUT, GPIO_PULLDOWN); + + // gamepad 2 + gpio_init(HH_IO_GP2_UP_PORT, HH_IO_GP2_UP_PIN, GPIO_MODE_INPUT, GPIO_PULLDOWN); + gpio_init(HH_IO_GP2_DOWN_PORT, HH_IO_GP2_DOWN_PIN, GPIO_MODE_INPUT, GPIO_PULLDOWN); + gpio_init(HH_IO_GP2_LEFT_PORT, HH_IO_GP2_LEFT_PIN, GPIO_MODE_INPUT, GPIO_PULLDOWN); + gpio_init(HH_IO_GP2_RIGHT_PORT, HH_IO_GP2_RIGHT_PIN, GPIO_MODE_INPUT, GPIO_PULLDOWN); + gpio_init(HH_IO_GP2_PRIMARY_PORT, HH_IO_GP2_PRIMARY_PIN, GPIO_MODE_INPUT, GPIO_PULLDOWN); + gpio_init(HH_IO_GP2_SECONDARY_PORT, HH_IO_GP2_SECONDARY_PIN, GPIO_MODE_INPUT, GPIO_PULLDOWN); } void HAL_MspInit() { @@ -199,3 +222,19 @@ void hh_io_setup_error_handler() { __disable_irq(); while (1); } + +void EXTI4_15_IRQHandler() { + HAL_GPIO_EXTI_IRQHandler(HH_IO_PPU_HBLANK_PIN); + HAL_GPIO_EXTI_IRQHandler(HH_IO_PPU_VBLANK_PIN); +} + +void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { + if (GPIO_Pin == HH_IO_PPU_HBLANK_PIN) { + g_hh_hcount++; + } + if (GPIO_Pin == HH_IO_PPU_VBLANK_PIN) { + g_hh_hcount = 0; + g_hh_vcount++; + hh_ppu_vblank_interrupt(); + } +} diff --git a/src/stm32/setup.h b/src/stm32/setup.h index 66d5ff3..b9432dc 100644 --- a/src/stm32/setup.h +++ b/src/stm32/setup.h @@ -5,9 +5,6 @@ #include #include -#define HH_IO_SPI_PINS (GPIO_PIN_5 | GPIO_PIN_7) -#define HH_IO_SPI_PORT GPIOA - extern UART_HandleTypeDef huart2; // NOLINT extern GPIO_InitTypeDef spi_gpio; // NOLINT extern SPI_HandleTypeDef hspi1; // NOLINT @@ -22,3 +19,5 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base); // NOLINT void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base); // NOLINT void HardFault_Handler(); // NOLINT void SysTick_Handler(); // NOLINT +void EXTI4_15_IRQHandler(); // NOLINT +void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin); // NOLINT -- cgit v1.2.3