From c6563a099ce05d9291a0c27fdbab3ffd9efddc48 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 10 Mar 2023 12:26:33 +0100 Subject: initial merge of #26 --- src/ds.c | 15 +++ src/ds.mk | 3 +- src/main.c | 247 +++++++++++++++++++++++++++++++++++++++-- src/ppu/stm.c | 4 + src/stm32.mk | 30 ++--- src/stm32/setup.c | 197 ++++++++++++++++++++++++++++++++ src/stm32/setup.h | 24 ++++ src/stm32/stm32f0xx_hal_conf.h | 16 +-- 8 files changed, 506 insertions(+), 30 deletions(-) create mode 100644 src/ds.c create mode 100644 src/ppu/stm.c create mode 100644 src/stm32/setup.c create mode 100644 src/stm32/setup.h (limited to 'src') diff --git a/src/ds.c b/src/ds.c new file mode 100644 index 0000000..d6d4357 --- /dev/null +++ b/src/ds.c @@ -0,0 +1,15 @@ +#include "main.h" +#include "ppu/ppu.h" +#include "demo.h" + +void hh_setup() { + hh_ppu_init(); + + hh_demo_setup(); +} + +void hh_exit() { + g_hh_run = false; + + hh_ppu_deinit(); +} diff --git a/src/ds.mk b/src/ds.mk index d4354a2..b5633b3 100644 --- a/src/ds.mk +++ b/src/ds.mk @@ -10,5 +10,6 @@ endif DESKTOP_SRCS += ppusim/sim.c \ ppusim/mem.c \ ppusim/pixel.c \ - ppusim/work.c + ppusim/work.c \ + ds.c diff --git a/src/main.c b/src/main.c index f4de225..ee6125f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,13 +1,98 @@ #include #include "main.h" -#include "ppu/ppu.h" #include "demo.h" bool g_hh_run = true; +static int8_t buttonDPAD[] = {0,0,0,0}; //1left 2right 3down 4up + +struct playerData{ + uint16_t posX; + uint16_t posY; + uint8_t radius; + uint8_t rotation; //45 degrees steps 0 == right 2 == down 4 == left 6 == up + uint8_t directionX; //direction where its looking at in case no input; + int8_t speed; //10 default L/R MODifier + bool inAir; + +}; + +struct playerData player1; + +uint8_t tileMap[30][40] = + { + {1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1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,1,1,1,1 ,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0 ,1,1,1,1,1,1,1,1,1,1 }, + {1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 }, + {1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 }, + {1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 }, + {1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 }, + {1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 }, + {1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 } + + + + }; + +void buttonRead(); +void playerMovement(); +void sendData(uint8_t, uint16_t); + + uint16_t pos_x; //0b0000 0001 0011 0110 + uint16_t pos_y; + + uint8_t left = 0; + uint8_t right = 0; + uint8_t up = 0; + uint8_t down = 0; + + + + uint8_t pos_x_bit[2]; + uint8_t pos_y_bit[2]; + uint8_t data_send[3]; + + int tileX; + int tileY; +// struct playerData player1; + //int buttons[] = {GPIO_PIN_4,GPIO_PIN_5,GPIO_PIN_6,GPIO_PIN_8}; // 1 left // 2 right // 3 up // 4 down + + int main() { hh_setup(); + +/// init struct + player1.posX = 31000; //0b0000 0001 0011 0110 + player1.posY = 21000; + player1.radius = 8; + player1.speed = 1; + player1.directionX = 1; + player1.rotation = 8; + player1.inAir = false; + hh_loop(); hh_exit(); return 0; @@ -17,17 +102,163 @@ void hh_ppu_vblank_interrupt() { static unsigned long frame = 0; frame++; - hh_demo_loop(frame); + buttonRead(); + playerMovement(); + + + // send data via SPI //adjust map size + pos_x = player1.posX / 100; + pos_y = player1.posY / 100; + + sendData(0b01000000,pos_x); + sendData(0b00000000,pos_y); + + // hh_demo_loop(frame); } -void hh_setup() { - hh_ppu_init(); +void sendData(uint8_t address, uint16_t data) +{ + uint8_t bitData[3]; + bitData[2] = data & 0xff; + bitData[1] = (data >> 8); + bitData[0] = address; // first byte is address + + + + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); + HAL_SPI_Transmit(&hspi1, bitData, 3, 100); //2*8 bit data + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET); - hh_demo_setup(); } -void hh_exit() { - g_hh_run = false; +void buttonRead() +{ + //int buttons[] = {GPIO_PIN_4,GPIO_PIN_5,GPIO_PIN_6,GPIO_PIN_8}; // 1 left // 2 right // 3 down // 4 up 8-6-4-5 + uint16_t buttons[] = {0x0100U,0x0040U,0x0010U,0x0020U}; // 1 left // 2 right // 3 down // 4 up + + for(int i = 0; i < 4; i++) + { + if(HAL_GPIO_ReadPin(GPIOB, buttons[i]) == 1) + { + if((i+2) % 2 == 0) + { + buttonDPAD[i] = 1; + } + else + { + buttonDPAD[i] = -1; + } + } + else + { + buttonDPAD[i] = 0; + } + } +} + +void playerMovement() +{ + + int8_t directionX = 0; // -1 = L || 1 == R + int8_t directionY = 0; // -1 = D || 1 == U + + uint8_t i,j; + uint8_t rotation = 0; // 0-7 + //temp var for testing + + + + // direction calc + directionX = buttonDPAD[0] + buttonDPAD[1]; + directionY = buttonDPAD[2] + buttonDPAD[3]; + + //rotation calc + for(i = -1; i < 2;i++) + { + for(j = -1; j < 2; j++) + { + if(directionX == i) + { + if(directionY == j) + { + if(i != 0 && j != 0) //dont update when player idle + { + player1.rotation = rotation; + } + } + } + rotation++; + } + } + //direction calc + if(directionX != 0) //update direction if player is not idle + { + player1.directionX = directionX; + } + //collision map x-axis + + //tile calc including radius and direction for background coliision + + uint16_t tileColX; + uint16_t tileColY = ( player1.posY / 100) / 16; ; + + // remaining space between grid and exact + uint8_t modTileX; + uint8_t modTileY; + + + + if(player1.inAir == false && directionX != 0) + { + if(directionX == 1) + { + tileColX = ( ( player1.posX / 100) + player1.radius ) / 20; + modTileX = ( player1.posX + ( 100 * player1.radius ) ) % 2000; + } + else if(directionX == -1) + { + tileColX = ( ( player1.posX / 100) - player1.radius ) / 20; + modTileX = ( player1.posX - ( 100 * player1.radius ) ) % 2000; + } + + if(tileMap[tileColY][tileColX + directionX] != 1) + { + player1.posX = player1.posX + (directionX * player1.speed); // NEW x set + } + + else if(tileMap[tileColY][tileColX + directionX] == 1) + { + if(modTileX < player1.speed) + { + player1.posX = player1.posX + (directionX * modTileX); // NEW x set + } + else + { + player1.posX = player1.posX + (directionX * player1.speed); // NEW x set + } + } + + } + else //if in air different all borders have to be checked + { + + } + + //collision map floor (y-axis) (falling) + // if falling no jump press (implement) + /* + tileColY = (( player1.posY / 100) + player1.radius) / 16; //bottom of player box + modTileY = 1; + if(tileMap[tileColY+1][tileColX] != 1) //rework after jumping + { + player1.posY = player1.posY + 5 ;// NEW y set //makew var gravity + //playerStat = falling; //for later use of graphics/sound + } + */ + //else if(tileMap[]) + + + + - hh_ppu_deinit(); } diff --git a/src/ppu/stm.c b/src/ppu/stm.c new file mode 100644 index 0000000..fd4a18c --- /dev/null +++ b/src/ppu/stm.c @@ -0,0 +1,4 @@ +#include "ppu/ppu.h" + +void hh_ppu_init() {} +void hh_ppu_deinit() {} diff --git a/src/stm32.mk b/src/stm32.mk index 6926dea..eab34a4 100644 --- a/src/stm32.mk +++ b/src/stm32.mk @@ -43,22 +43,24 @@ STM_SRCS += lib/FreeRTOS-Kernel/croutine.c \ lib/FreeRTOS-Kernel/timers.c \ lib/FreeRTOS-Kernel/portable/GCC/ARM_CM0/port.c \ lib/FreeRTOS-Kernel/portable/MemMang/heap_4.c -STM_SRCS += lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_rcc.c \ - lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_rcc_ex.c \ - lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal.c \ - lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_i2c.c \ - lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_i2c_ex.c \ - lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_gpio.c \ - lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_dma.c \ - lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_cortex.c \ - lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_pwr.c \ - lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_pwr_ex.c \ - lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_flash.c \ - lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_flash_ex.c \ +STM_SRCS += lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal.c \ + lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_spi.c \ + lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_spi_ex.c \ lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_tim.c \ lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_tim_ex.c \ lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_uart.c \ - lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_uart_ex.c + lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_uart_ex.c \ + lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_cortex.c \ + lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_dma.c \ + lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_flash.c \ + lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_flash_ex.c \ + lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_gpio.c \ + lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_pwr.c \ + lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_pwr_ex.c \ + lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_rcc.c \ + lib/STM32-base-STM32Cube/HAL/STM32F0xx/src/stm32f0xx_hal_rcc_ex.c STM_SRCS += stm32/idle_task_static_memory.c \ - stm32/main.c + stm32/main.c \ + stm32/setup.c \ + ppu/stm.c diff --git a/src/stm32/setup.c b/src/stm32/setup.c new file mode 100644 index 0000000..2c3552b --- /dev/null +++ b/src/stm32/setup.c @@ -0,0 +1,197 @@ +#include +#include +#include +#include +#include +#include + +#include "main.h" +#include "setup.h" +#include "ppu/ppu.h" + +UART_HandleTypeDef huart2 = { + .Instance = USART2, + .Init.BaudRate = 115200, + .Init.WordLength = UART_WORDLENGTH_8B, + .Init.StopBits = UART_STOPBITS_1, + .Init.Parity = UART_PARITY_NONE, + .Init.Mode = UART_MODE_TX_RX, + .Init.HwFlowCtl = UART_HWCONTROL_NONE, + .Init.OverSampling = UART_OVERSAMPLING_16, + .Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE, + .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.DataSize = SPI_DATASIZE_8BIT, + .Init.CLKPolarity = SPI_POLARITY_LOW, + .Init.CLKPhase = SPI_PHASE_1EDGE, + .Init.NSS = SPI_NSS_SOFT, + .Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16, + .Init.FirstBit = SPI_FIRSTBIT_MSB, + .Init.TIMode = SPI_TIMODE_DISABLE, + .Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE, + .Init.CRCPolynomial = 7, + .Init.CRCLength = SPI_CRC_LENGTH_DATASIZE, + .Init.NSSPMode = SPI_NSS_PULSE_DISABLE, +}; + +TIM_HandleTypeDef htim3 = { + .Instance = TIM3, + .Init.Prescaler = 7999, + .Init.CounterMode = TIM_COUNTERMODE_UP, + .Init.Period = 65535, + .Init.ClockDivision = TIM_CLOCKDIVISION_DIV1, + .Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE, +}; + +static void hh_io_spi_setup(); +static void hh_io_tim_setup(); +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(); + +void hh_setup() { + HAL_Init(); + + hh_io_clock_setup(); + hh_io_usart2_setup(); + hh_io_gpio_setup(); + hh_io_spi_setup(); + hh_io_tim_setup(); + + hh_ppu_init(); +} + +void hh_exit() { + hh_ppu_deinit(); + + HAL_DeInit(); +} + +void hh_io_clock_setup() { + if (HAL_OK != HAL_RCC_OscConfig(&(RCC_OscInitTypeDef){ + .OscillatorType = RCC_OSCILLATORTYPE_HSI, + .HSIState = RCC_HSI_ON, + .HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT, + .PLL.PLLState = RCC_PLL_ON, + })) return hh_io_setup_error_handler(); + + // cpu, ahb & apb clocks + if (HAL_OK != HAL_RCC_ClockConfig(&(RCC_ClkInitTypeDef){ + .ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1, + .SYSCLKSource = RCC_SYSCLKSOURCE_HSI, + .AHBCLKDivider = RCC_SYSCLK_DIV1, + .APB1CLKDivider = RCC_HCLK_DIV1, + }, FLASH_LATENCY_1)) return hh_io_setup_error_handler(); + + // usart2 clock (usb serial) + if (HAL_RCCEx_PeriphCLKConfig(&(RCC_PeriphCLKInitTypeDef){ + .PeriphClockSelection = RCC_PERIPHCLK_USART2, + .Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1, + .I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI, + }) != HAL_OK) return hh_io_setup_error_handler(); +} + +void hh_io_spi_setup() { + if (HAL_SPI_Init(&hspi1) != HAL_OK) + return hh_io_setup_error_handler(); +} + +void hh_io_usart2_setup() { + if (HAL_UART_Init(&huart2) != HAL_OK) + return hh_io_setup_error_handler(); +} + +void hh_io_tim_setup() { + if (HAL_TIM_Base_Init(&htim3) != HAL_OK) + return hh_io_setup_error_handler(); + + if (HAL_TIM_ConfigClockSource(&htim3, &(TIM_ClockConfigTypeDef) { + .ClockSource = TIM_CLOCKSOURCE_INTERNAL + }) != HAL_OK) return hh_io_setup_error_handler(); + + if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &(TIM_MasterConfigTypeDef) { + .MasterOutputTrigger = TIM_TRGO_RESET, + .MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE, + }) != HAL_OK) return hh_io_setup_error_handler(); +} + +void hh_io_gpio_setup() { + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET); + HAL_GPIO_Init(GPIOA, &(GPIO_InitTypeDef) { + .Pin = GPIO_PIN_9, + .Mode = GPIO_MODE_OUTPUT_PP, + .Pull = GPIO_NOPULL, + .Speed = GPIO_SPEED_FREQ_LOW, + }); + 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, + }); +} + +void HAL_MspInit() { + __HAL_RCC_SYSCFG_CLK_ENABLE(); + __HAL_RCC_PWR_CLK_ENABLE(); +} + +void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) { + if(hspi->Instance != SPI1) return; + + __HAL_RCC_SPI1_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); + + HAL_GPIO_Init(HH_IO_SPI_PORT, &spi_gpio); +} + +void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi) { + if(hspi->Instance != SPI1) return; + + __HAL_RCC_SPI1_CLK_DISABLE(); + + HAL_GPIO_DeInit(HH_IO_SPI_PORT, HH_IO_SPI_PINS); +} + +void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { + 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; + + __HAL_RCC_TIM3_CLK_DISABLE(); +} + +void SysTick_Handler() { + HAL_IncTick(); + if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) + xPortSysTickHandler(); +} + +void HardFault_Handler() { + for(;;); +} + +void hh_io_setup_error_handler() { + __disable_irq(); + while (1); +} diff --git a/src/stm32/setup.h b/src/stm32/setup.h new file mode 100644 index 0000000..66d5ff3 --- /dev/null +++ b/src/stm32/setup.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#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 +extern TIM_HandleTypeDef htim3; // NOLINT + +// required HAL setup functions +void HAL_MspInit(); // NOLINT +void HAL_UART_MspInit(UART_HandleTypeDef *huart); // NOLINT +void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi); // NOLINT +void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi); // NOLINT +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 diff --git a/src/stm32/stm32f0xx_hal_conf.h b/src/stm32/stm32f0xx_hal_conf.h index fc27221..84a3b74 100644 --- a/src/stm32/stm32f0xx_hal_conf.h +++ b/src/stm32/stm32f0xx_hal_conf.h @@ -19,16 +19,18 @@ #define DATA_CACHE_ENABLE 0U #define USE_SPI_CRC 0U -#define HAL_RCC_MODULE_ENABLED #define HAL_MODULE_ENABLED -#define HAL_I2C_MODULE_ENABLED -#define HAL_GPIO_MODULE_ENABLED -#define HAL_DMA_MODULE_ENABLED -#define HAL_CORTEX_MODULE_ENABLED -#define HAL_PWR_MODULE_ENABLED -#define HAL_FLASH_MODULE_ENABLED +#define HAL_SPI_MODULE_ENABLED #define HAL_TIM_MODULE_ENABLED #define HAL_UART_MODULE_ENABLED +#define HAL_CORTEX_MODULE_ENABLED +#define HAL_DMA_MODULE_ENABLED +#define HAL_FLASH_MODULE_ENABLED +#define HAL_GPIO_MODULE_ENABLED +#define HAL_EXTI_MODULE_ENABLED +#define HAL_PWR_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED +// #define HAL_I2C_MODULE_ENABLED #ifdef HAL_RCC_MODULE_ENABLED #include -- cgit v1.2.3