Use HAL drivers for midi2cv

This commit is contained in:
Jan-Henrik 2020-04-13 15:38:56 +02:00
parent 765de98f39
commit 0f0ac98496
16 changed files with 224 additions and 146 deletions

View file

@ -1,4 +1,4 @@
#include <stm32f37x_conf.h> #include <stm32f3xx_hal.h>
#include "midi2cv/drivers/eco_display.h" #include "midi2cv/drivers/eco_display.h"
#include "stmlib/system/bootloader_utils.h" #include "stmlib/system/bootloader_utils.h"
@ -14,6 +14,42 @@ extern "C" void __cxa_pure_virtual()
} }
extern "C" { extern "C" {
void Error_Handler(void)
{
while (1)
;
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
Error_Handler();
}
}
void SysTick_Handler() void SysTick_Handler()
{ {
system_clock.Tick(); system_clock.Tick();
@ -26,6 +62,10 @@ EcoDisplay display;
int main(void) int main(void)
{ {
HAL_Init();
SystemClock_Config();
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_RCC_PWR_CLK_ENABLE();
SystemInit(); SystemInit();
SysTick_Config(F_CPU / 1000); SysTick_Config(F_CPU / 1000);

View file

@ -26,7 +26,7 @@
F_CRYSTAL = 8000000L F_CRYSTAL = 8000000L
F_CPU = 72000000L F_CPU = 72000000L
SYSCLOCK = SYSCLK_FREQ_72MHz SYSCLOCK = SYSCLK_FREQ_72MHz
FAMILY = f37x FAMILY = f3xx
MEMORY_MODE = flash MEMORY_MODE = flash
U8G2 = enabled U8G2 = enabled
# USB = enabled # USB = enabled

View file

@ -1,11 +1,12 @@
#include "base_display.h" #include "base_display.h"
#include "peripherals.h"
#include "spi_mode.h" #include "spi_mode.h"
#include <U8x8lib.h> #include <U8x8lib.h>
#include <stm32f37x_conf.h> #include <stm32f3xx_hal.h>
static const uint16_t kPinEnable = GPIO_Pin_2; static const uint16_t kPinEnable = GPIO_PIN_2;
static const uint16_t kPinReset = GPIO_Pin_0; static const uint16_t kPinReset = GPIO_PIN_0;
static const uint16_t kPinDataCommand = GPIO_Pin_9; static const uint16_t kPinDataCommand = GPIO_PIN_9;
uint8_t u8x8_stm32_gpio_and_delay(U8X8_UNUSED u8x8_t* u8x8, uint8_t u8x8_stm32_gpio_and_delay(U8X8_UNUSED u8x8_t* u8x8,
U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int, U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int,
@ -17,32 +18,23 @@ uint8_t u8x8_stm32_gpio_and_delay(U8X8_UNUSED u8x8_t* u8x8,
uint8_t u8x8_byte_4wire_stm32_spi(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, uint8_t u8x8_byte_4wire_stm32_spi(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int,
void* arg_ptr) void* arg_ptr)
{ {
uint8_t* data; uint8_t* data = (uint8_t*)arg_ptr;
switch (msg) { switch (msg) {
case U8X8_MSG_BYTE_SEND: case U8X8_MSG_BYTE_SEND:
data = (uint8_t*)arg_ptr;
while (arg_int > 0) { HAL_SPI_Transmit(&hspi2, data, arg_int, 0);
while (!(SPI2->SR & SPI_SR_TXE)) {
}
SPI_SendData8(SPI2, (uint8_t)*data);
arg_int--;
data++;
}
break; break;
case U8X8_MSG_BYTE_INIT: case U8X8_MSG_BYTE_INIT:
break; break;
case U8X8_MSG_BYTE_SET_DC: case U8X8_MSG_BYTE_SET_DC:
if (arg_int) HAL_GPIO_WritePin(GPIOB, kPinDataCommand, arg_int ? GPIO_PIN_SET : GPIO_PIN_RESET);
GPIO_WriteBit(GPIOB, kPinDataCommand, Bit_SET);
else
GPIO_WriteBit(GPIOB, kPinDataCommand, Bit_RESET);
break; break;
case U8X8_MSG_BYTE_START_TRANSFER: case U8X8_MSG_BYTE_START_TRANSFER:
InitSPI(SPI_MODE_DISPLAY); InitSPI(SPI_MODE_DISPLAY);
GPIO_WriteBit(GPIOB, kPinEnable, Bit_RESET); HAL_GPIO_WritePin(GPIOB, kPinEnable, GPIO_PIN_RESET);
break; break;
case U8X8_MSG_BYTE_END_TRANSFER: case U8X8_MSG_BYTE_END_TRANSFER:
GPIO_WriteBit(GPIOB, kPinEnable, Bit_SET); HAL_GPIO_WritePin(GPIOB, kPinEnable, GPIO_PIN_SET);
break; break;
default: default:
return 0; return 0;
@ -53,39 +45,23 @@ uint8_t u8x8_byte_4wire_stm32_spi(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int,
void BaseDisplay::Init() void BaseDisplay::Init()
{ {
// init SS/CS/RST GPIO // init SS/CS/RST GPIO
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); __HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitTypeDef gpio_init; GPIO_InitTypeDef gpio_init;
gpio_init.GPIO_Mode = GPIO_Mode_OUT; gpio_init.Mode = GPIO_MODE_OUTPUT_PP;
gpio_init.GPIO_OType = GPIO_OType_PP; gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; gpio_init.Pull = GPIO_NOPULL;
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; gpio_init.Pin = kPinEnable | kPinReset | kPinDataCommand;
gpio_init.GPIO_Pin = kPinEnable | kPinReset | kPinDataCommand; HAL_GPIO_Init(GPIOB, &gpio_init);
GPIO_Init(GPIOB, &gpio_init);
GPIO_WriteBit(GPIOB, kPinEnable, Bit_SET); HAL_GPIO_WritePin(GPIOB, kPinEnable, GPIO_PIN_SET);
// init AF GPIO
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
gpio_init.GPIO_Mode = GPIO_Mode_AF;
gpio_init.GPIO_OType = GPIO_OType_PP;
gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
gpio_init.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_Init(GPIOB, &gpio_init);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_5);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_5);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_5);
// init SPI // init SPI
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
SPI_I2S_DeInit(SPI2);
// Initialize SPI // Initialize SPI
InitSPI(SPI_MODE_DISPLAY); InitSPI(SPI_MODE_DISPLAY);
GPIO_WriteBit(GPIOB, kPinReset, Bit_RESET); HAL_GPIO_WritePin(GPIOB, kPinReset, GPIO_PIN_RESET);
asm("nop"); asm("nop");
GPIO_WriteBit(GPIOB, kPinReset, Bit_SET); HAL_GPIO_WritePin(GPIOB, kPinReset, GPIO_PIN_SET);
InitGLib(); InitGLib();
} }

View file

@ -3,7 +3,6 @@
#include "spi_mode.h" #include "spi_mode.h"
#include "stmlib/system/system_clock.h" #include "stmlib/system/system_clock.h"
#include <U8g2lib.h> #include <U8g2lib.h>
#include <stm32f37x_conf.h>
#include <u8g2.h> #include <u8g2.h>
using namespace stmlib; using namespace stmlib;
@ -12,7 +11,6 @@ static uint8_t* default_buf;
static uint8_t second_buf[1024]; static uint8_t second_buf[1024];
static uint8_t* output_buf; static uint8_t* output_buf;
class U8G2_SH1106_128x64_NONAME_F_SPI : public U8G2 { class U8G2_SH1106_128x64_NONAME_F_SPI : public U8G2 {
public: public:
U8G2_SH1106_128x64_NONAME_F_SPI() U8G2_SH1106_128x64_NONAME_F_SPI()

View file

@ -4,7 +4,7 @@
#include "stmlib/stmlib.h" #include "stmlib/stmlib.h"
#include "base_display.h" #include "base_display.h"
#include <U8g2lib.h> #include <U8g2lib.h>
#include <stm32f37x_conf.h> #include <stm32f3xx_hal_conf.h>
#define DISPLAY_WIDTH 128 #define DISPLAY_WIDTH 128
#define DISPLAY_HEIGHT 64 #define DISPLAY_HEIGHT 64

View file

@ -27,30 +27,27 @@
// Driver for rotary encoder. // Driver for rotary encoder.
#include "midi2cv/drivers/encoder.h" #include "midi2cv/drivers/encoder.h"
#include "stm32f3xx_hal_gpio.h"
void Encoder::Init()
void Encoder::Init() { {
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); __HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitTypeDef gpio_init; GPIO_InitTypeDef gpio_init;
gpio_init.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; gpio_init.Pin = GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
gpio_init.GPIO_Speed = GPIO_Speed_10MHz; gpio_init.Speed = GPIO_SPEED_FREQ_MEDIUM;
gpio_init.GPIO_Mode = GPIO_Mode_IN; gpio_init.Mode = GPIO_MODE_INPUT;
gpio_init.GPIO_PuPd = GPIO_PuPd_UP; gpio_init.Pull = GPIO_PULLUP;
GPIO_Init(GPIOC, &gpio_init); HAL_GPIO_Init(GPIOC, &gpio_init);
switch_state_ = 0xff; switch_state_ = 0xff;
quadrature_decoding_state_[0] = quadrature_decoding_state_[1] = 0xff; quadrature_decoding_state_[0] = quadrature_decoding_state_[1] = 0xff;
} }
void Encoder::Debounce() { void Encoder::Debounce()
switch_state_ = (switch_state_ << 1) | \ {
GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_15); switch_state_ = (switch_state_ << 1) | HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_15);
quadrature_decoding_state_[0] = (quadrature_decoding_state_[0] << 1) | \ quadrature_decoding_state_[0] = (quadrature_decoding_state_[0] << 1) | HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13);
GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13); quadrature_decoding_state_[1] = (quadrature_decoding_state_[1] << 1) | HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_14);
quadrature_decoding_state_[1] = (quadrature_decoding_state_[1] << 1) | \
GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_14);
} }

View file

@ -26,10 +26,10 @@
// //
// Driver for rotary encoder. // Driver for rotary encoder.
#ifndef YARNS_DRIVERS_ENCODER_H_ #pragma once
#define YARNS_DRIVERS_ENCODER_H_
#include <stm32f37x_conf.h> #include <stm32f3xx_hal_conf.h>
#include "stm32f3xx_hal_gpio.h"
#include "stmlib/stmlib.h" #include "stmlib/stmlib.h"
class Encoder { class Encoder {
@ -53,7 +53,8 @@ class Encoder {
} }
inline bool pressed_immediate() const { inline bool pressed_immediate() const {
return !GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_15); return !HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_15);
//return !GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_15);
} }
inline int32_t increment() const { inline int32_t increment() const {
@ -79,4 +80,3 @@ class Encoder {
extern Encoder encoder; extern Encoder encoder;
#endif // YARNS_DRIVERS_ENCODER_H_

View file

@ -1,6 +1,5 @@
#include "gpio.h" #include "gpio.h"
#include <stm32f37x_conf.h>
void GPIO::Init() { void GPIO::Init() {

View file

@ -3,7 +3,7 @@
#include "stmlib/stmlib.h" #include "stmlib/stmlib.h"
#include <stm32f37x_conf.h> #include <stm32f3xx_hal_conf.h>
#define PORT_RST_OLED GPIOB #define PORT_RST_OLED GPIOB
#define PIN_RST_OLED GPIO_Pin_0 #define PIN_RST_OLED GPIO_Pin_0

View file

@ -0,0 +1,5 @@
#include "peripherals.h"
IWDG_HandleTypeDef hiwdg = { .Instance = IWDG };
TIM_HandleTypeDef htim2 = { .Instance = TIM2 };
SPI_HandleTypeDef hspi2 = { .Instance = SPI2 };

View file

@ -0,0 +1,8 @@
#pragma once
#include <stm32f3xx_hal.h>
extern IWDG_HandleTypeDef hiwdg;
extern TIM_HandleTypeDef htim2;
extern SPI_HandleTypeDef hspi2;

View file

@ -1,12 +1,26 @@
#include "spi_mode.h" #include "spi_mode.h"
#include <stm32f37x_conf.h> #include "peripherals.h"
#include "stm32f3xx_hal_spi.h"
#include <stm32f3xx_hal.h>
uint8_t currentMode = 0; SPIMode currentMode = SPI_MODE_UNINITIALIZED;
void InitSPIDisplay(void); void InitSPIDisplay(void);
void InitSPI(uint8_t mode) void InitSPI(SPIMode mode)
{ {
if (currentMode == SPI_MODE_UNINITIALIZED) {
__HAL_RCC_SPI2_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
// init AF GPIO
GPIO_InitTypeDef gpio_init;
gpio_init.Mode = GPIO_MODE_AF_PP;
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
gpio_init.Pull = GPIO_NOPULL;
gpio_init.Pin = GPIO_PIN_8 | GPIO_PIN_14 | GPIO_PIN_15;
gpio_init.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB, &gpio_init);
}
if (currentMode != mode) { if (currentMode != mode) {
switch (mode) { switch (mode) {
case SPI_MODE_DISPLAY: case SPI_MODE_DISPLAY:
@ -15,6 +29,8 @@ void InitSPI(uint8_t mode)
case SPI_MODE_DAC0: case SPI_MODE_DAC0:
case SPI_MODE_DAC1: case SPI_MODE_DAC1:
case SPI_MODE_USB: case SPI_MODE_USB:
case SPI_MODE_UNINITIALIZED:
break; break;
} }
currentMode = mode; currentMode = mode;
@ -23,16 +39,15 @@ void InitSPI(uint8_t mode)
void InitSPIDisplay(void) void InitSPIDisplay(void)
{ {
SPI_InitTypeDef spi_init; hspi2.Init.Direction = SPI_DIRECTION_2LINES;
spi_init.SPI_Direction = SPI_Direction_2Lines_FullDuplex; hspi2.Init.Mode = SPI_MODE_MASTER;
spi_init.SPI_Mode = SPI_Mode_Master; hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
spi_init.SPI_DataSize = SPI_DataSize_8b; hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
spi_init.SPI_CPOL = SPI_CPOL_High; hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
spi_init.SPI_CPHA = SPI_CPHA_2Edge; hspi2.Init.NSS = SPI_NSS_SOFT;
spi_init.SPI_NSS = SPI_NSS_Soft; hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
spi_init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
spi_init.SPI_FirstBit = SPI_FirstBit_MSB; hspi2.Init.CRCPolynomial = 7;
spi_init.SPI_CRCPolynomial = 7; hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
SPI_Init(SPI2, &spi_init); HAL_SPI_Init(&hspi2);
SPI_Cmd(SPI2, ENABLE);
} }

View file

@ -1,13 +1,16 @@
#ifndef MIDI2CV_DRIVERS_SPI_MODE_H #ifndef MIDI2CV_DRIVERS_SPI_MODE_H
#define MIDI2CV_DRIVERS_SPI_MODE_H #define MIDI2CV_DRIVERS_SPI_MODE_H
#include <stm32f37x_conf.h> #include <stm32f3xx_hal.h>
#define SPI_MODE_DISPLAY 1 enum SPIMode {
#define SPI_MODE_DAC0 2 SPI_MODE_UNINITIALIZED,
#define SPI_MODE_DAC1 3 SPI_MODE_DISPLAY,
#define SPI_MODE_USB 4 SPI_MODE_DAC0,
SPI_MODE_DAC1,
SPI_MODE_USB
};
void InitSPI(uint8_t mode); void InitSPI(SPIMode mode);
#endif #endif

View file

@ -27,7 +27,7 @@
F_CRYSTAL = 8000000L F_CRYSTAL = 8000000L
F_CPU = 72000000L F_CPU = 72000000L
SYSCLOCK = SYSCLK_FREQ_72MHz SYSCLOCK = SYSCLK_FREQ_72MHz
FAMILY = f37x FAMILY = f3xx
# USB = enabled # USB = enabled
U8G2 = enabled U8G2 = enabled
PRINTF_FLOATS = enabled PRINTF_FLOATS = enabled

View file

@ -1,13 +1,15 @@
#include <stm32f37x_conf.h> #include <stm32f3xx_hal.h>
#include "config.h" #include "config.h"
#include "drivers/display.h" #include "drivers/display.h"
#include "drivers/encoder.h" #include "drivers/encoder.h"
#include "drivers/gpio.h" #include "drivers/peripherals.h"
#include "menu/menu.h" #include "menu/menu.h"
#include "menu/menu_items.h" #include "menu/menu_items.h"
#include "part.h" #include "part.h"
#include "settings.h" #include "settings.h"
#include "stm32f3xx_hal_cortex.h"
#include "stm32f3xx_hal_tim.h"
#include "stmlib/system/system_clock.h" #include "stmlib/system/system_clock.h"
#include "ui.h" #include "ui.h"
@ -27,7 +29,6 @@ extern "C" void __cxa_pure_virtual()
using namespace stmlib; using namespace stmlib;
GPIO gpio;
Display display; Display display;
Encoder encoder; Encoder encoder;
@ -60,29 +61,70 @@ void UsageFault_Handler()
while (1) while (1)
; ;
} }
void Error_Handler()
{
while (1)
;
}
void SVC_Handler() {} void SVC_Handler() {}
void DebugMon_Handler() {} void DebugMon_Handler() {}
void PendSV_Handler() {} void PendSV_Handler() {}
// called every 1ms void SystemClock_Config(void)
void SysTick_Handler()
{ {
RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
Error_Handler();
}
}
// called every 1ms
void SysTick_Handler(void)
{
HAL_IncTick();
ui.Poll(); ui.Poll();
system_clock.Tick(); system_clock.Tick();
} }
void TIM2_IRQHandler(void) void TIM2_IRQHandler()
{ {
// this will get called with 8kHz (foof) HAL_TIM_IRQHandler(&htim2);
if (TIM_GetITStatus(TIM2, TIM_IT_Update) == RESET) { }
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
{
if (htim != &htim2) {
return; return;
} }
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
static uint8_t count = 0; static uint16_t count = 0;
count++; count++;
if (count % (8000L / 60) == 0) { if (count % (8000L / 24) == 0) {
// refresh display with 60fps // refresh display with 24fps
ui.Flush(); ui.Flush();
count = 0; count = 0;
} }
@ -96,42 +138,30 @@ void TIM2_IRQHandler(void)
void InitTimers(void) void InitTimers(void)
{ {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); __HAL_RCC_TIM2_CLK_ENABLE();
TIM_TimeBaseInitTypeDef timer_init; htim2.Init.Period = F_CPU / (8000 * 1) - 1;
timer_init.TIM_Period = F_CPU / (8000 * 1) - 1; htim2.Init.Prescaler = 0;
timer_init.TIM_Prescaler = 0; htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
timer_init.TIM_ClockDivision = TIM_CKD_DIV1; htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
timer_init.TIM_CounterMode = TIM_CounterMode_Up; htim2.Init.RepetitionCounter = 0;
timer_init.TIM_RepetitionCounter = 0;
//TIM_InternalClockConfig(TIM2); //TIM_InternalClockConfig(TIM2);
TIM_TimeBaseInit(TIM2, &timer_init); HAL_TIM_Base_Init(&htim2);
TIM_Cmd(TIM2, ENABLE); HAL_TIM_Base_Start_IT(&htim2);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // 2.2 priority split. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2); // 2.2 priority split.
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 1);
// DAC interrupt is given highest priority HAL_NVIC_EnableIRQ(TIM2_IRQn);
NVIC_InitTypeDef timer_interrupt;
timer_interrupt.NVIC_IRQChannel = TIM2_IRQn;
timer_interrupt.NVIC_IRQChannelPreemptionPriority = 0;
timer_interrupt.NVIC_IRQChannelSubPriority = 1;
timer_interrupt.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&timer_interrupt);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
} }
void Init(void) void Init(void)
{ {
SystemInit(); //NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x8000);
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x8000); hiwdg.Init.Prescaler = IWDG_PRESCALER_16;
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); //HAL_IWDG_Init(&hiwdg);
IWDG_SetPrescaler(IWDG_Prescaler_16);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
system_clock.Init(); system_clock.Init();
SysTick_Config(F_CPU / 1000);
//IWDG_Enable(); //IWDG_Enable();
gpio.Init();
display.Init(); display.Init();
encoder.Init(); encoder.Init();
InitTimers(); InitTimers();
@ -139,6 +169,13 @@ void Init(void)
int main(void) int main(void)
{ {
SystemInit();
SCB->VTOR = 0x8000;
HAL_DeInit();
HAL_Init();
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_RCC_PWR_CLK_ENABLE();
SystemClock_Config();
Init(); Init();
while (1) { while (1) {
@ -147,6 +184,6 @@ int main(void)
// do we want to call the watchdog here? it's the only part thats getting interrupted after all // do we want to call the watchdog here? it's the only part thats getting interrupted after all
// (next to the interrupts themselves potentially interrupting each other) // (next to the interrupts themselves potentially interrupting each other)
ui.DoEvents(); ui.DoEvents();
IWDG_ReloadCounter(); HAL_IWDG_Refresh(&hiwdg);
} }
} }

2
stmlib

@ -1 +1 @@
Subproject commit cb45ab9eca989481e9141f560052993f4d640ad0 Subproject commit 688459c60596d95d4973b3d1e85b92c45000d79a