Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firmware tidying #748

Merged
merged 13 commits into from
Jan 27, 2022
5 changes: 3 additions & 2 deletions 32blit-stm32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ list(APPEND SOURCES
Src/file.cpp
Src/jpeg.cpp
Src/adc.c
Src/adc.cpp
Src/debug.c
Src/dma.c
Src/hrtim.c
Expand All @@ -91,8 +92,8 @@ list(APPEND SOURCES
Src/sys.c
Src/tim.c
Src/rng.c
Src/32blit/32blit_battery.cpp
Src/32blit/32blit_i2c.cpp
Src/32blit/battery.cpp
Src/32blit/i2c.cpp
Src/SystemMenu/firmware_menu.cpp
Src/SystemMenu/system_menu_controller.cpp
Src/SystemMenu/battery_menu.cpp
Expand Down
5 changes: 0 additions & 5 deletions 32blit-stm32/Inc/32blit.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#define UNUSED(x) ((void)(__typeof__(x))(x)) // suppress "UNUSED" warnings

#include "32blit.hpp"
#include "fatfs.h"
#include "persistence.h"
#include "executable.hpp"

Expand All @@ -16,7 +15,6 @@ void update(uint32_t time);
void render(uint32_t time);

// SD storage
char *get_fr_err_text(FRESULT err);
bool blit_sd_detected();
bool blit_sd_mounted();

Expand Down Expand Up @@ -44,6 +42,3 @@ RawMetadata *blit_get_running_game_metadata();
void blit_menu_update(uint32_t time);
void blit_menu_render(uint32_t time);
void blit_menu();

void blit_enable_ADC();
void blit_disable_ADC();
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@

#pragma once

extern void blit_i2c_tick();
extern void blit_init_accelerometer();
namespace i2c {
void init();
void tick();
}
2 changes: 0 additions & 2 deletions 32blit-stm32/Inc/CDCDataStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

#include "stm32h7xx_hal.h"
#include <cstring>
#include <streambuf>
#include <istream>

class CDCDataStream
{
Expand Down
17 changes: 17 additions & 0 deletions 32blit-stm32/Inc/adc.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

namespace adc {
enum class Value {
joystick_x = 0,
joystick_y,
hack_left,
hack_right,
battery_charge
};

void init();

void stop();

uint16_t get_value(Value v);
}
2 changes: 2 additions & 0 deletions 32blit-stm32/Inc/sound.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "stm32h7xx_hal.h"

#include "audio/audio.hpp"

extern TIM_HandleTypeDef htim6;
Expand Down
126 changes: 18 additions & 108 deletions 32blit-stm32/Src/32blit.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include "string.h"
#include <map>
#include <bitset>
#include <cstring>

#include "32blit.h"
#include "32blit_battery.hpp"
#include "32blit_i2c.hpp"
#include "main.h"
#include "32blit/battery.hpp"
#include "32blit/i2c.hpp"

#include "adc.hpp"
#include "sound.hpp"
#include "display.hpp"
#include "gpio.hpp"
Expand All @@ -16,41 +14,23 @@
#include "multiplayer.hpp"
#include "power.hpp"

#include "adc.h"
#include "tim.h"
#include "rng.h"
#include "spi.h"
#include "i2c.h"
#include "i2c-msa301.h"
#include "i2c-lis3dh.h"
#include "i2c-bq24295.h"
#include "fatfs.h"
#include "quadspi.h"
#include "usbd_core.h"
#include "USBManager.h"
#include "usbd_cdc_if.h"

#include "32blit.hpp"
#include "engine/api_private.hpp"
#include "graphics/color.hpp"
#include "engine/running_average.hpp"
#include "engine/menu.hpp"
#include "engine/version.hpp"

#include "SystemMenu/system_menu_controller.hpp"

#include "stdarg.h"
using namespace blit;
using battery::BatteryChargeStatus;

extern USBD_HandleTypeDef hUsbDeviceHS;
extern USBManager g_usbManager;

#define ADC_BUFFER_SIZE 32

__attribute__((section(".dma_data"))) ALIGN_32BYTES(__IO uint16_t adc1data[ADC_BUFFER_SIZE]);
__attribute__((section(".dma_data"))) ALIGN_32BYTES(__IO uint16_t adc3data[ADC_BUFFER_SIZE]);

FATFS filesystem;
extern Disk_drvTypeDef disk;
static bool fs_mounted = false;
Expand Down Expand Up @@ -206,7 +186,7 @@ void blit_tick() {

do_render();

blit_i2c_tick();
i2c::tick();
blit_process_input();
blit_update_led();
blit_update_vibration();
Expand Down Expand Up @@ -264,11 +244,11 @@ void hook_render(uint32_t time) {
::render(time);

blit::screen.pen = Pen(255, 255, 255);
for(auto i = 0; i < ADC_BUFFER_SIZE; i++) {
/*for(auto i = 0; i < ADC_BUFFER_SIZE; i++) {
int x = i / 8;
int y = i % 8;
blit::screen.text(std::to_string(adc1data[i]), minimal_font, Point(x * 30, y * 10));
}
}*/
}

void blit_update_volume() {
Expand Down Expand Up @@ -354,18 +334,9 @@ void blit_init() {
DWT->CYCCNT = 0;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;

HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc1data, ADC_BUFFER_SIZE);
HAL_ADC_Start_DMA(&hadc3, (uint32_t *)adc3data, ADC_BUFFER_SIZE);

CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CYCCNT = 0;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;

fs_mounted = f_mount(&filesystem, "", 1) == FR_OK; // this shouldn't be necessary here right?

blit_init_accelerometer();

bq24295_init(&hi2c4);
i2c::init();

blit::api.version_major = api_version_major;
blit::api.version_minor = api_version_minor;
Expand Down Expand Up @@ -546,49 +517,6 @@ void blit_update_led() {

// Backlight
__HAL_TIM_SetCompare(&htim15, TIM_CHANNEL_1, (962 - (962 * persist.backlight)) * power::sleep_fade + (1024 * (1.0f - power::sleep_fade)));

// TODO we don't want to do this too often!
switch(battery::get_charge_status()){
case BatteryChargeStatus::NotCharging:
charge_led_r = 1;
charge_led_b = 0;
charge_led_g = 0;
break;
case BatteryChargeStatus::PreCharging:
charge_led_r = 1;
charge_led_b = 1;
charge_led_g = 0;
break;
case BatteryChargeStatus::FastCharging:
charge_led_r = 0;
charge_led_b = 1;
charge_led_g = 0;
break;
case BatteryChargeStatus::ChargingComplete:
charge_led_r = 0;
charge_led_b = 0;
charge_led_g = 1;
break;
}
}

void HAL_ADC_ErrorCallback(ADC_HandleTypeDef* hadc){
}

void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc){
if(hadc->Instance == ADC1) {
SCB_InvalidateDCache_by_Addr((uint32_t *) &adc1data[0], ADC_BUFFER_SIZE);
} else if (hadc->Instance == ADC3) {
SCB_InvalidateDCache_by_Addr((uint32_t *) &adc3data[0], ADC_BUFFER_SIZE);
}
}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc) {
if(hadc->Instance == ADC1) {
SCB_InvalidateDCache_by_Addr((uint32_t *) &adc1data[ADC_BUFFER_SIZE / 2], ADC_BUFFER_SIZE / 2);
} else if (hadc->Instance == ADC3) {
SCB_InvalidateDCache_by_Addr((uint32_t *) &adc3data[ADC_BUFFER_SIZE / 2], ADC_BUFFER_SIZE / 2);
}
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
Expand All @@ -597,7 +525,6 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if(pressed && blit_user_code_running()) { // if button was pressed and we are inside a game, queue the game exit
exit_game = true;
}
HAL_TIM_Base_Stop(&htim2);
HAL_TIM_Base_Stop_IT(&htim2);
}
}
Expand All @@ -614,7 +541,6 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
HAL_NVIC_DisableIRQ(TIM2_IRQn);
__HAL_TIM_SetCounter(&htim2, 0);
__HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, long_press_exit_time * 10); // press-to-reset-time
HAL_TIM_Base_Start(&htim2);
HAL_TIM_Base_Start_IT(&htim2);
__HAL_TIM_CLEAR_FLAG(&htim2, TIM_SR_UIF);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
Expand All @@ -629,25 +555,12 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
}

toggle_menu = true;
HAL_TIM_Base_Stop(&htim2);
HAL_TIM_Base_Stop_IT(&htim2);
__HAL_TIM_SetCounter(&htim2, 0);
}
}
}

void blit_disable_ADC()
{
// TODO: Flesh this out if it's still necessary in interrupt-driven ADC mode
return;
}

void blit_enable_ADC()
{
// TODO: Flesh this out if it's still necessary in interrupt-driven ADC mode
return;
}

void blit_process_input() {
// Read buttons
blit::buttons =
Expand All @@ -667,7 +580,7 @@ void blit_process_input() {
power::update_active();

// Process ADC readings
int joystick_x = (adc1data[0] >> 1) - 16384;
int joystick_x = (adc::get_value(adc::Value::joystick_x) >> 1) - 16384;
joystick_x = std::max(-8192, std::min(8192, joystick_x));
if(joystick_x < -1024) {
joystick_x += 1024;
Expand All @@ -679,7 +592,7 @@ void blit_process_input() {
}
blit::joystick.x = joystick_x / 7168.0f;

int joystick_y = (adc1data[1] >> 1) - 16384;
int joystick_y = (adc::get_value(adc::Value::joystick_y) >> 1) - 16384;
joystick_y = std::max(-8192, std::min(8192, joystick_y));
if(joystick_y < -1024) {
joystick_y += 1024;
Expand All @@ -694,19 +607,17 @@ void blit_process_input() {
if(blit::joystick.length() > 0.01f)
power::update_active();

blit::hack_left = (adc3data[0] >> 1) / 32768.0f;
blit::hack_right = (adc3data[1] >> 1) / 32768.0f;
blit::hack_left = (adc::get_value(adc::Value::hack_left) >> 1) / 32768.0f;
blit::hack_right = (adc::get_value(adc::Value::hack_right) >> 1) / 32768.0f;

battery::update_charge(6.6f * adc3data[2] / 65535.0f);
battery::update_charge(6.6f * adc::get_value(adc::Value::battery_charge) / 65535.0f);
}

// blit_switch_execution
//
// Switches execution to new location defined by EXTERNAL_LOAD_ADDRESS
// EXTERNAL_LOAD_ADDRESS is the start of the Vector Table location
// Attempts to init a new game and switch render/tick to run it.
// Can also be used to exit back to the firmware.
//
typedef void (*pFunction)(void);
pFunction JumpToApplication;

bool blit_switch_execution(uint32_t address, bool force_game)
{
Expand Down Expand Up @@ -788,9 +699,7 @@ bool blit_switch_execution(uint32_t address, bool force_game)
// old-style soft-reset to app with linked HAL
// left for compatibility/testing

// Stop the ADC DMA
HAL_ADC_Stop_DMA(&hadc1);
HAL_ADC_Stop_DMA(&hadc3);
adc::stop();

// Stop the audio
HAL_TIM_Base_Stop_IT(&htim6);
Expand Down Expand Up @@ -822,8 +731,9 @@ bool blit_switch_execution(uint32_t address, bool force_game)
/* Disable Systick interrupt */
SysTick->CTRL = 0;

typedef void (*pFunction)(void);
/* Initialize user application's Stack Pointer & Jump to user application */
JumpToApplication = (pFunction) (*(__IO uint32_t*) (EXTERNAL_LOAD_ADDRESS + 4));
auto JumpToApplication = (pFunction) (*(__IO uint32_t*) (EXTERNAL_LOAD_ADDRESS + 4));
__set_MSP(*(__IO uint32_t*) EXTERNAL_LOAD_ADDRESS);
JumpToApplication();

Expand Down
Loading