diff options
Diffstat (limited to 'src/stm32')
-rw-r--r-- | src/stm32/consts.h | 11 | ||||
-rw-r--r-- | src/stm32/main.c | 2 | ||||
-rw-r--r-- | src/stm32/setup.c | 76 | ||||
-rw-r--r-- | src/stm32/setup.h | 4 |
4 files changed, 70 insertions, 23 deletions
diff --git a/src/stm32/consts.h b/src/stm32/consts.h index 447e36e..16d8da2 100644 --- a/src/stm32/consts.h +++ b/src/stm32/consts.h @@ -3,12 +3,19 @@ #include <stm32f091xc.h> // ../../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_MOSI_PORT GPIOA +#define HH_IO_SPI_MOSI_PIN GPIO_PIN_7 +#define HH_IO_SPI_MISO_PORT GPIOA +#define HH_IO_SPI_MISO_PIN GPIO_PIN_6 +#define HH_IO_SPI_CLK_PORT GPIOA +#define HH_IO_SPI_CLK_PIN GPIO_PIN_5 #define HH_IO_SPI_SR_PORT GPIOA #define HH_IO_SPI_SR_PIN GPIO_PIN_9 +#define HH_IO_SPI_PORT GPIOA +#define HH_IO_SPI_PINS (HH_IO_SPI_CLK_PIN | HH_IO_SPI_MOSI_PIN | HH_IO_SPI_MISO_PIN) + #define HH_IO_PPU_HBLANK_PORT GPIOA #define HH_IO_PPU_HBLANK_PIN GPIO_PIN_8 #define HH_IO_PPU_VBLANK_PORT GPIOC diff --git a/src/stm32/main.c b/src/stm32/main.c index b7b4604..181657c 100644 --- a/src/stm32/main.c +++ b/src/stm32/main.c @@ -5,5 +5,5 @@ void hh_ppu_load_tilemap() {} void hh_loop() { - while(1); + while(g_hh_run); } diff --git a/src/stm32/setup.c b/src/stm32/setup.c index dc8d23e..d15c643 100644 --- a/src/stm32/setup.c +++ b/src/stm32/setup.c @@ -11,6 +11,8 @@ #include "ppu/ppu.h" #include "stm32/consts.h" +bool g_hh_first_flush_done = false; + UART_HandleTypeDef huart2 = { .Instance = USART2, .Init.BaudRate = 115200, @@ -24,18 +26,10 @@ UART_HandleTypeDef huart2 = { .AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT, }; -GPIO_InitTypeDef spi_gpio = { - .Pin = HH_IO_SPI_PINS, - .Mode = GPIO_MODE_AF_PP, - .Pull = GPIO_NOPULL, - .Speed = GPIO_SPEED_FREQ_HIGH, - .Alternate = GPIO_AF0_SPI1, -}; - SPI_HandleTypeDef hspi1 = { .Instance = SPI1, .Init.Mode = SPI_MODE_MASTER, - .Init.Direction = SPI_DIRECTION_1LINE, + .Init.Direction = SPI_DIRECTION_2LINES, .Init.DataSize = SPI_DATASIZE_8BIT, .Init.CLKPolarity = SPI_POLARITY_LOW, .Init.CLKPhase = SPI_PHASE_1EDGE, @@ -49,6 +43,17 @@ SPI_HandleTypeDef hspi1 = { .Init.NSSPMode = SPI_NSS_PULSE_DISABLE, }; +DMA_HandleTypeDef hdma_spi1_tx = { + .Instance = DMA1_Channel3, + .Init.Direction = DMA_MEMORY_TO_PERIPH, + .Init.PeriphInc = DMA_PINC_DISABLE, + .Init.MemInc = DMA_MINC_ENABLE, + .Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE, + .Init.MemDataAlignment = DMA_MDATAALIGN_BYTE, + .Init.Mode = DMA_NORMAL, + .Init.Priority = DMA_PRIORITY_HIGH, +}; + TIM_HandleTypeDef htim3 = { .Instance = TIM3, .Init.Prescaler = 7999, @@ -64,6 +69,8 @@ static void hh_io_usart2_setup(); static void hh_io_gpio_setup(); static void hh_io_clock_setup(); static void hh_io_setup_error_handler(); +static void hh_interrupt_setup(); +static void hh_io_dma_setup(); void hh_setup() { HAL_Init(); @@ -71,11 +78,14 @@ void hh_setup() { hh_io_clock_setup(); hh_io_usart2_setup(); hh_io_gpio_setup(); + hh_io_dma_setup(); hh_io_spi_setup(); hh_io_tim_setup(); hh_ppu_init(); hh_demo_setup(); + + hh_ppu_flush(); } void hh_exit() { @@ -84,6 +94,12 @@ void hh_exit() { HAL_DeInit(); } +void hh_io_dma_setup() { + __HAL_RCC_DMA1_CLK_ENABLE(); + HAL_NVIC_SetPriority(DMA1_Ch2_3_DMA2_Ch1_2_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Ch2_3_DMA2_Ch1_2_IRQn); +} + void hh_io_clock_setup() { if (HAL_OK != HAL_RCC_OscConfig(&(RCC_OscInitTypeDef){ .OscillatorType = RCC_OSCILLATORTYPE_HSI, @@ -142,9 +158,9 @@ static void gpio_init(GPIO_TypeDef* port, uint16_t pin, uint32_t mode, uint32_t } void hh_io_gpio_setup() { - __HAL_RCC_GPIOA_CLK_ENABLE(); - __HAL_RCC_GPIOB_CLK_ENABLE(); - __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __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); @@ -173,37 +189,49 @@ void hh_io_gpio_setup() { } void HAL_MspInit() { - __HAL_RCC_SYSCFG_CLK_ENABLE(); - __HAL_RCC_PWR_CLK_ENABLE(); + __HAL_RCC_SYSCFG_CLK_ENABLE(); + __HAL_RCC_PWR_CLK_ENABLE(); HAL_NVIC_SetPriority(PendSV_IRQn, 3, 0); } void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) { - if(hspi->Instance != SPI1) return; + if(hspi->Instance != SPI1) return; __HAL_RCC_SPI1_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); - HAL_GPIO_Init(HH_IO_SPI_PORT, &spi_gpio); + HAL_GPIO_Init(HH_IO_SPI_PORT, &(GPIO_InitTypeDef) { + .Pin = HH_IO_SPI_PINS, + .Mode = GPIO_MODE_AF_PP, + .Pull = GPIO_PULLDOWN, + .Speed = GPIO_SPEED_FREQ_HIGH, + .Alternate = GPIO_AF0_SPI1, + }); + + if (HAL_OK != HAL_DMA_Init(&hdma_spi1_tx)) + return hh_io_setup_error_handler(); + __HAL_DMA1_REMAP(HAL_DMA1_CH3_SPI1_TX); + __HAL_LINKDMA(hspi, hdmatx, hdma_spi1_tx); } void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi) { - if(hspi->Instance != SPI1) return; + if(hspi->Instance != SPI1) return; __HAL_RCC_SPI1_CLK_DISABLE(); __HAL_RCC_GPIOA_CLK_DISABLE(); HAL_GPIO_DeInit(HH_IO_SPI_PORT, HH_IO_SPI_PINS); + HAL_DMA_DeInit(hspi->hdmatx); } void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { - if(htim_base->Instance != TIM3) return; + if(htim_base->Instance != TIM3) return; __HAL_RCC_TIM3_CLK_ENABLE(); } void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base) { - if(htim_base->Instance != TIM3) return; + if(htim_base->Instance != TIM3) return; __HAL_RCC_TIM3_CLK_DISABLE(); } @@ -229,6 +257,7 @@ void EXTI4_15_IRQHandler() { } void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { + if (!g_hh_first_flush_done) return; if (GPIO_Pin == HH_IO_PPU_HBLANK_PIN) { g_hh_hcount++; } @@ -238,3 +267,12 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { hh_ppu_vblank_interrupt(); } } + +void DMA1_Ch2_3_DMA2_Ch1_2_IRQHandler() { + HAL_DMA_IRQHandler(&hdma_spi1_tx); +} + +void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef* hspi) { + if(hspi->Instance != SPI1) return; + g_hh_first_flush_done = true; +} diff --git a/src/stm32/setup.h b/src/stm32/setup.h index b9432dc..f583d20 100644 --- a/src/stm32/setup.h +++ b/src/stm32/setup.h @@ -4,11 +4,12 @@ #include <stm32f0xx_hal_uart.h> #include <stm32f0xx_hal_tim.h> #include <stm32f0xx_hal_gpio.h> +#include <stm32f0xx_hal_dma.h> extern UART_HandleTypeDef huart2; // NOLINT -extern GPIO_InitTypeDef spi_gpio; // NOLINT extern SPI_HandleTypeDef hspi1; // NOLINT extern TIM_HandleTypeDef htim3; // NOLINT +extern DMA_HandleTypeDef hdma_spi1_tx; // NOLINT // required HAL setup functions void HAL_MspInit(); // NOLINT @@ -21,3 +22,4 @@ void HardFault_Handler(); // NOLINT void SysTick_Handler(); // NOLINT void EXTI4_15_IRQHandler(); // NOLINT void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin); // NOLINT +void DMA1_Ch2_3_DMA2_Ch1_2_IRQHandler(); // NOLINT |