mirror of
https://github.com/jhbruhn/eurorack.git
synced 2025-03-15 02:55:49 +00:00
Use HAL drivers for midi2cv
This commit is contained in:
parent
765de98f39
commit
0f0ac98496
16 changed files with 224 additions and 146 deletions
|
@ -1,4 +1,4 @@
|
|||
#include <stm32f37x_conf.h>
|
||||
#include <stm32f3xx_hal.h>
|
||||
|
||||
#include "midi2cv/drivers/eco_display.h"
|
||||
#include "stmlib/system/bootloader_utils.h"
|
||||
|
@ -14,6 +14,42 @@ extern "C" void __cxa_pure_virtual()
|
|||
}
|
||||
|
||||
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()
|
||||
{
|
||||
system_clock.Tick();
|
||||
|
@ -26,6 +62,10 @@ EcoDisplay display;
|
|||
|
||||
int main(void)
|
||||
{
|
||||
HAL_Init();
|
||||
SystemClock_Config();
|
||||
__HAL_RCC_SYSCFG_CLK_ENABLE();
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
SystemInit();
|
||||
|
||||
SysTick_Config(F_CPU / 1000);
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
F_CRYSTAL = 8000000L
|
||||
F_CPU = 72000000L
|
||||
SYSCLOCK = SYSCLK_FREQ_72MHz
|
||||
FAMILY = f37x
|
||||
FAMILY = f3xx
|
||||
MEMORY_MODE = flash
|
||||
U8G2 = enabled
|
||||
# USB = enabled
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
#include "base_display.h"
|
||||
#include "peripherals.h"
|
||||
#include "spi_mode.h"
|
||||
#include <U8x8lib.h>
|
||||
#include <stm32f37x_conf.h>
|
||||
#include <stm32f3xx_hal.h>
|
||||
|
||||
static const uint16_t kPinEnable = GPIO_Pin_2;
|
||||
static const uint16_t kPinReset = GPIO_Pin_0;
|
||||
static const uint16_t kPinDataCommand = GPIO_Pin_9;
|
||||
static const uint16_t kPinEnable = GPIO_PIN_2;
|
||||
static const uint16_t kPinReset = GPIO_PIN_0;
|
||||
static const uint16_t kPinDataCommand = GPIO_PIN_9;
|
||||
|
||||
uint8_t u8x8_stm32_gpio_and_delay(U8X8_UNUSED u8x8_t* u8x8,
|
||||
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,
|
||||
void* arg_ptr)
|
||||
{
|
||||
uint8_t* data;
|
||||
uint8_t* data = (uint8_t*)arg_ptr;
|
||||
switch (msg) {
|
||||
case U8X8_MSG_BYTE_SEND:
|
||||
data = (uint8_t*)arg_ptr;
|
||||
while (arg_int > 0) {
|
||||
while (!(SPI2->SR & SPI_SR_TXE)) {
|
||||
}
|
||||
SPI_SendData8(SPI2, (uint8_t)*data);
|
||||
arg_int--;
|
||||
data++;
|
||||
}
|
||||
|
||||
HAL_SPI_Transmit(&hspi2, data, arg_int, 0);
|
||||
break;
|
||||
case U8X8_MSG_BYTE_INIT:
|
||||
break;
|
||||
case U8X8_MSG_BYTE_SET_DC:
|
||||
if (arg_int)
|
||||
GPIO_WriteBit(GPIOB, kPinDataCommand, Bit_SET);
|
||||
else
|
||||
GPIO_WriteBit(GPIOB, kPinDataCommand, Bit_RESET);
|
||||
HAL_GPIO_WritePin(GPIOB, kPinDataCommand, arg_int ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
||||
break;
|
||||
case U8X8_MSG_BYTE_START_TRANSFER:
|
||||
InitSPI(SPI_MODE_DISPLAY);
|
||||
GPIO_WriteBit(GPIOB, kPinEnable, Bit_RESET);
|
||||
HAL_GPIO_WritePin(GPIOB, kPinEnable, GPIO_PIN_RESET);
|
||||
break;
|
||||
case U8X8_MSG_BYTE_END_TRANSFER:
|
||||
GPIO_WriteBit(GPIOB, kPinEnable, Bit_SET);
|
||||
HAL_GPIO_WritePin(GPIOB, kPinEnable, GPIO_PIN_SET);
|
||||
break;
|
||||
default:
|
||||
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()
|
||||
{
|
||||
// init SS/CS/RST GPIO
|
||||
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
|
||||
GPIO_InitTypeDef gpio_init;
|
||||
gpio_init.GPIO_Mode = GPIO_Mode_OUT;
|
||||
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 = kPinEnable | kPinReset | kPinDataCommand;
|
||||
GPIO_Init(GPIOB, &gpio_init);
|
||||
gpio_init.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
gpio_init.Pull = GPIO_NOPULL;
|
||||
gpio_init.Pin = kPinEnable | kPinReset | kPinDataCommand;
|
||||
HAL_GPIO_Init(GPIOB, &gpio_init);
|
||||
|
||||
GPIO_WriteBit(GPIOB, kPinEnable, Bit_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);
|
||||
HAL_GPIO_WritePin(GPIOB, kPinEnable, GPIO_PIN_SET);
|
||||
|
||||
// init SPI
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
||||
SPI_I2S_DeInit(SPI2);
|
||||
// Initialize SPI
|
||||
InitSPI(SPI_MODE_DISPLAY);
|
||||
GPIO_WriteBit(GPIOB, kPinReset, Bit_RESET);
|
||||
HAL_GPIO_WritePin(GPIOB, kPinReset, GPIO_PIN_RESET);
|
||||
asm("nop");
|
||||
|
||||
GPIO_WriteBit(GPIOB, kPinReset, Bit_SET);
|
||||
HAL_GPIO_WritePin(GPIOB, kPinReset, GPIO_PIN_SET);
|
||||
InitGLib();
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
#include "spi_mode.h"
|
||||
#include "stmlib/system/system_clock.h"
|
||||
#include <U8g2lib.h>
|
||||
#include <stm32f37x_conf.h>
|
||||
#include <u8g2.h>
|
||||
|
||||
using namespace stmlib;
|
||||
|
@ -12,7 +11,6 @@ static uint8_t* default_buf;
|
|||
static uint8_t second_buf[1024];
|
||||
static uint8_t* output_buf;
|
||||
|
||||
|
||||
class U8G2_SH1106_128x64_NONAME_F_SPI : public U8G2 {
|
||||
public:
|
||||
U8G2_SH1106_128x64_NONAME_F_SPI()
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include "stmlib/stmlib.h"
|
||||
#include "base_display.h"
|
||||
#include <U8g2lib.h>
|
||||
#include <stm32f37x_conf.h>
|
||||
#include <stm32f3xx_hal_conf.h>
|
||||
|
||||
#define DISPLAY_WIDTH 128
|
||||
#define DISPLAY_HEIGHT 64
|
||||
|
|
|
@ -27,30 +27,27 @@
|
|||
// Driver for rotary encoder.
|
||||
|
||||
#include "midi2cv/drivers/encoder.h"
|
||||
#include "stm32f3xx_hal_gpio.h"
|
||||
|
||||
|
||||
void Encoder::Init() {
|
||||
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
|
||||
|
||||
void Encoder::Init()
|
||||
{
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
|
||||
GPIO_InitTypeDef gpio_init;
|
||||
|
||||
gpio_init.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
|
||||
gpio_init.GPIO_Speed = GPIO_Speed_10MHz;
|
||||
gpio_init.GPIO_Mode = GPIO_Mode_IN;
|
||||
gpio_init.GPIO_PuPd = GPIO_PuPd_UP;
|
||||
GPIO_Init(GPIOC, &gpio_init);
|
||||
gpio_init.Pin = GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
|
||||
gpio_init.Speed = GPIO_SPEED_FREQ_MEDIUM;
|
||||
gpio_init.Mode = GPIO_MODE_INPUT;
|
||||
gpio_init.Pull = GPIO_PULLUP;
|
||||
HAL_GPIO_Init(GPIOC, &gpio_init);
|
||||
|
||||
switch_state_ = 0xff;
|
||||
quadrature_decoding_state_[0] = quadrature_decoding_state_[1] = 0xff;
|
||||
}
|
||||
|
||||
void Encoder::Debounce() {
|
||||
switch_state_ = (switch_state_ << 1) | \
|
||||
GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_15);
|
||||
quadrature_decoding_state_[0] = (quadrature_decoding_state_[0] << 1) | \
|
||||
GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13);
|
||||
quadrature_decoding_state_[1] = (quadrature_decoding_state_[1] << 1) | \
|
||||
GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_14);
|
||||
void Encoder::Debounce()
|
||||
{
|
||||
switch_state_ = (switch_state_ << 1) | HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_15);
|
||||
quadrature_decoding_state_[0] = (quadrature_decoding_state_[0] << 1) | HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13);
|
||||
quadrature_decoding_state_[1] = (quadrature_decoding_state_[1] << 1) | HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_14);
|
||||
}
|
||||
|
||||
|
|
|
@ -26,10 +26,10 @@
|
|||
//
|
||||
// Driver for rotary encoder.
|
||||
|
||||
#ifndef YARNS_DRIVERS_ENCODER_H_
|
||||
#define YARNS_DRIVERS_ENCODER_H_
|
||||
#pragma once
|
||||
|
||||
#include <stm32f37x_conf.h>
|
||||
#include <stm32f3xx_hal_conf.h>
|
||||
#include "stm32f3xx_hal_gpio.h"
|
||||
#include "stmlib/stmlib.h"
|
||||
|
||||
class Encoder {
|
||||
|
@ -53,7 +53,8 @@ class Encoder {
|
|||
}
|
||||
|
||||
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 {
|
||||
|
@ -79,4 +80,3 @@ class Encoder {
|
|||
|
||||
extern Encoder encoder;
|
||||
|
||||
#endif // YARNS_DRIVERS_ENCODER_H_
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#include "gpio.h"
|
||||
|
||||
#include <stm32f37x_conf.h>
|
||||
|
||||
|
||||
void GPIO::Init() {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
|
||||
#include "stmlib/stmlib.h"
|
||||
#include <stm32f37x_conf.h>
|
||||
#include <stm32f3xx_hal_conf.h>
|
||||
|
||||
#define PORT_RST_OLED GPIOB
|
||||
#define PIN_RST_OLED GPIO_Pin_0
|
||||
|
|
5
midi2cv/drivers/peripherals.c
Normal file
5
midi2cv/drivers/peripherals.c
Normal file
|
@ -0,0 +1,5 @@
|
|||
#include "peripherals.h"
|
||||
|
||||
IWDG_HandleTypeDef hiwdg = { .Instance = IWDG };
|
||||
TIM_HandleTypeDef htim2 = { .Instance = TIM2 };
|
||||
SPI_HandleTypeDef hspi2 = { .Instance = SPI2 };
|
8
midi2cv/drivers/peripherals.h
Normal file
8
midi2cv/drivers/peripherals.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <stm32f3xx_hal.h>
|
||||
|
||||
extern IWDG_HandleTypeDef hiwdg;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern SPI_HandleTypeDef hspi2;
|
||||
|
|
@ -1,38 +1,53 @@
|
|||
#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 InitSPI(uint8_t mode)
|
||||
void InitSPI(SPIMode mode)
|
||||
{
|
||||
if (currentMode != mode) {
|
||||
switch (mode) {
|
||||
case SPI_MODE_DISPLAY:
|
||||
InitSPIDisplay();
|
||||
break;
|
||||
case SPI_MODE_DAC0:
|
||||
case SPI_MODE_DAC1:
|
||||
case SPI_MODE_USB:
|
||||
break;
|
||||
}
|
||||
currentMode = 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) {
|
||||
switch (mode) {
|
||||
case SPI_MODE_DISPLAY:
|
||||
InitSPIDisplay();
|
||||
break;
|
||||
case SPI_MODE_DAC0:
|
||||
case SPI_MODE_DAC1:
|
||||
case SPI_MODE_USB:
|
||||
case SPI_MODE_UNINITIALIZED:
|
||||
|
||||
break;
|
||||
}
|
||||
currentMode = mode;
|
||||
}
|
||||
}
|
||||
|
||||
void InitSPIDisplay(void)
|
||||
{
|
||||
SPI_InitTypeDef spi_init;
|
||||
spi_init.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
||||
spi_init.SPI_Mode = SPI_Mode_Master;
|
||||
spi_init.SPI_DataSize = SPI_DataSize_8b;
|
||||
spi_init.SPI_CPOL = SPI_CPOL_High;
|
||||
spi_init.SPI_CPHA = SPI_CPHA_2Edge;
|
||||
spi_init.SPI_NSS = SPI_NSS_Soft;
|
||||
spi_init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
|
||||
spi_init.SPI_FirstBit = SPI_FirstBit_MSB;
|
||||
spi_init.SPI_CRCPolynomial = 7;
|
||||
SPI_Init(SPI2, &spi_init);
|
||||
SPI_Cmd(SPI2, ENABLE);
|
||||
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
hspi2.Init.Mode = SPI_MODE_MASTER;
|
||||
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi2.Init.NSS = SPI_NSS_SOFT;
|
||||
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
|
||||
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi2.Init.CRCPolynomial = 7;
|
||||
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
HAL_SPI_Init(&hspi2);
|
||||
}
|
||||
|
|
|
@ -1,13 +1,16 @@
|
|||
#ifndef MIDI2CV_DRIVERS_SPI_MODE_H
|
||||
#define MIDI2CV_DRIVERS_SPI_MODE_H
|
||||
|
||||
#include <stm32f37x_conf.h>
|
||||
#include <stm32f3xx_hal.h>
|
||||
|
||||
#define SPI_MODE_DISPLAY 1
|
||||
#define SPI_MODE_DAC0 2
|
||||
#define SPI_MODE_DAC1 3
|
||||
#define SPI_MODE_USB 4
|
||||
enum SPIMode {
|
||||
SPI_MODE_UNINITIALIZED,
|
||||
SPI_MODE_DISPLAY,
|
||||
SPI_MODE_DAC0,
|
||||
SPI_MODE_DAC1,
|
||||
SPI_MODE_USB
|
||||
};
|
||||
|
||||
void InitSPI(uint8_t mode);
|
||||
void InitSPI(SPIMode mode);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
F_CRYSTAL = 8000000L
|
||||
F_CPU = 72000000L
|
||||
SYSCLOCK = SYSCLK_FREQ_72MHz
|
||||
FAMILY = f37x
|
||||
FAMILY = f3xx
|
||||
# USB = enabled
|
||||
U8G2 = enabled
|
||||
PRINTF_FLOATS = enabled
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
#include <stm32f37x_conf.h>
|
||||
#include <stm32f3xx_hal.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "drivers/display.h"
|
||||
#include "drivers/encoder.h"
|
||||
#include "drivers/gpio.h"
|
||||
#include "drivers/peripherals.h"
|
||||
#include "menu/menu.h"
|
||||
#include "menu/menu_items.h"
|
||||
#include "part.h"
|
||||
#include "settings.h"
|
||||
#include "stm32f3xx_hal_cortex.h"
|
||||
#include "stm32f3xx_hal_tim.h"
|
||||
#include "stmlib/system/system_clock.h"
|
||||
#include "ui.h"
|
||||
|
||||
|
@ -27,7 +29,6 @@ extern "C" void __cxa_pure_virtual()
|
|||
|
||||
using namespace stmlib;
|
||||
|
||||
GPIO gpio;
|
||||
Display display;
|
||||
Encoder encoder;
|
||||
|
||||
|
@ -60,29 +61,70 @@ void UsageFault_Handler()
|
|||
while (1)
|
||||
;
|
||||
}
|
||||
void Error_Handler()
|
||||
{
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
void SVC_Handler() {}
|
||||
void DebugMon_Handler() {}
|
||||
void PendSV_Handler() {}
|
||||
|
||||
// called every 1ms
|
||||
void SysTick_Handler()
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
// called every 1ms
|
||||
void SysTick_Handler(void)
|
||||
{
|
||||
HAL_IncTick();
|
||||
ui.Poll();
|
||||
system_clock.Tick();
|
||||
}
|
||||
|
||||
void TIM2_IRQHandler(void)
|
||||
void TIM2_IRQHandler()
|
||||
{
|
||||
// this will get called with 8kHz (foof)
|
||||
if (TIM_GetITStatus(TIM2, TIM_IT_Update) == RESET) {
|
||||
HAL_TIM_IRQHandler(&htim2);
|
||||
}
|
||||
|
||||
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
|
||||
{
|
||||
if (htim != &htim2) {
|
||||
return;
|
||||
}
|
||||
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
||||
|
||||
static uint8_t count = 0;
|
||||
static uint16_t count = 0;
|
||||
count++;
|
||||
if (count % (8000L / 60) == 0) {
|
||||
// refresh display with 60fps
|
||||
if (count % (8000L / 24) == 0) {
|
||||
// refresh display with 24fps
|
||||
ui.Flush();
|
||||
count = 0;
|
||||
}
|
||||
|
@ -96,42 +138,30 @@ void TIM2_IRQHandler(void)
|
|||
|
||||
void InitTimers(void)
|
||||
{
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
||||
TIM_TimeBaseInitTypeDef timer_init;
|
||||
timer_init.TIM_Period = F_CPU / (8000 * 1) - 1;
|
||||
timer_init.TIM_Prescaler = 0;
|
||||
timer_init.TIM_ClockDivision = TIM_CKD_DIV1;
|
||||
timer_init.TIM_CounterMode = TIM_CounterMode_Up;
|
||||
timer_init.TIM_RepetitionCounter = 0;
|
||||
__HAL_RCC_TIM2_CLK_ENABLE();
|
||||
htim2.Init.Period = F_CPU / (8000 * 1) - 1;
|
||||
htim2.Init.Prescaler = 0;
|
||||
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim2.Init.RepetitionCounter = 0;
|
||||
//TIM_InternalClockConfig(TIM2);
|
||||
TIM_TimeBaseInit(TIM2, &timer_init);
|
||||
TIM_Cmd(TIM2, ENABLE);
|
||||
HAL_TIM_Base_Init(&htim2);
|
||||
HAL_TIM_Base_Start_IT(&htim2);
|
||||
|
||||
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // 2.2 priority split.
|
||||
|
||||
// DAC interrupt is given highest priority
|
||||
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);
|
||||
HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2); // 2.2 priority split.
|
||||
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 1);
|
||||
HAL_NVIC_EnableIRQ(TIM2_IRQn);
|
||||
}
|
||||
|
||||
void Init(void)
|
||||
{
|
||||
SystemInit();
|
||||
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x8000);
|
||||
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
|
||||
IWDG_SetPrescaler(IWDG_Prescaler_16);
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
||||
//NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x8000);
|
||||
hiwdg.Init.Prescaler = IWDG_PRESCALER_16;
|
||||
//HAL_IWDG_Init(&hiwdg);
|
||||
|
||||
system_clock.Init();
|
||||
SysTick_Config(F_CPU / 1000);
|
||||
|
||||
//IWDG_Enable();
|
||||
gpio.Init();
|
||||
display.Init();
|
||||
encoder.Init();
|
||||
InitTimers();
|
||||
|
@ -139,6 +169,13 @@ void Init(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();
|
||||
|
||||
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
|
||||
// (next to the interrupts themselves potentially interrupting each other)
|
||||
ui.DoEvents();
|
||||
IWDG_ReloadCounter();
|
||||
HAL_IWDG_Refresh(&hiwdg);
|
||||
}
|
||||
}
|
||||
|
|
2
stmlib
2
stmlib
|
@ -1 +1 @@
|
|||
Subproject commit cb45ab9eca989481e9141f560052993f4d640ad0
|
||||
Subproject commit 688459c60596d95d4973b3d1e85b92c45000d79a
|
Loading…
Reference in a new issue