Improve switiching between spi modes

This commit is contained in:
Jan-Henrik 2019-09-19 22:50:51 +02:00
parent 8c0ad0007a
commit e59fff8665
4 changed files with 71 additions and 16 deletions

View file

@ -4,6 +4,7 @@
#include "stmlib/utils/random.h"
#include <stm32f37x_conf.h>
#include <u8g2.h>
#include "spi_mode.h"
using namespace stmlib;
@ -21,6 +22,8 @@ u8g2_t* Display::u8g2()
return &u8g2_;
}
void InitSPI(void);
void Display::Init()
{
// init SS/CS/RST GPIO
@ -53,19 +56,7 @@ void Display::Init()
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
SPI_I2S_DeInit(SPI2);
// Initialize SPI
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);
InitSPI(SPI_MODE_DISPLAY);
GPIO_WriteBit(GPIOB, kPinReset, Bit_RESET);
asm("nop");
@ -73,6 +64,7 @@ void Display::Init()
InitGLib();
}
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 void* arg_ptr)
@ -104,6 +96,7 @@ uint8_t u8x8_byte_4wire_hw_spi(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int,
GPIO_WriteBit(GPIOB, kPinDataCommand, Bit_RESET);
break;
case U8X8_MSG_BYTE_START_TRANSFER:
InitSPI(SPI_MODE_DISPLAY);
GPIO_WriteBit(GPIOB, kPinEnable, Bit_RESET);
break;
case U8X8_MSG_BYTE_END_TRANSFER:
@ -120,7 +113,7 @@ void Display::InitGLib()
u8g2_Setup_sh1106_128x64_noname_f(&u8g2_, U8G2_R0, u8x8_byte_4wire_hw_spi, u8x8_stm32_gpio_and_delay);
output_buf = default_buf = u8g2_.tile_buf_ptr;
u8g2_InitDisplay(&u8g2_);
u8g2_SetContrast(&u8g2_, 127);
u8g2_SetContrast(&u8g2_, 255);
u8g2_SetPowerSave(&u8g2_, 0);
Random::Seed(42);

View file

@ -0,0 +1,38 @@
#include "spi_mode.h"
#include <stm32f37x_conf.h>
uint8_t currentMode = 0;
void InitSPIDisplay(void);
void InitSPI(uint8_t 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;
}
}
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);
}

View file

@ -0,0 +1,13 @@
#ifndef MIDI2CV_DRIVERS_SPI_MODE_H
#define MIDI2CV_DRIVERS_SPI_MODE_H
#include <stm32f37x_conf.h>
#define SPI_MODE_DISPLAY 1
#define SPI_MODE_DAC0 2
#define SPI_MODE_DAC1 3
#define SPI_MODE_USB 4
void InitSPI(uint8_t mode);
#endif

View file

@ -52,7 +52,7 @@ void TIM2_IRQHandler(void)
return;
}
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
// this will get called with 192 kHz (foof)
// we want to reduce the amount of times the ui gets polled to 1kHz
// which still is a lot (60fps would be enough tbh)
@ -63,6 +63,15 @@ void TIM2_IRQHandler(void)
ui.Flush();
count = 0;
}
if (count % 48 == 0) { // write audiodac1
}
if (count % 48 == 1) { // write audiodac2
}
if (count % 48 == 2) { // write audiodac3
}
if (count % 48 == 3) { // write audiodac4
}
}
}
@ -103,7 +112,7 @@ void Init(void)
SysTick_Config(F_CPU / 8000);
IWDG_Enable();
gpio.Init();
asm("bkpt #0");
// asm("bkpt #0");
display.Init();
InitTimers();
}
@ -112,6 +121,8 @@ int main(void)
{
Init();
while (1) {
// In this loop we do things that dont depend on any timing, but that have to be done.
// you should not write on spi here because that should happen in the TIM2 interrupt
ui.Update();
}
}