mirror of
https://github.com/jhbruhn/eurorack.git
synced 2025-03-15 02:55:49 +00:00
Improve switiching between spi modes
This commit is contained in:
parent
8c0ad0007a
commit
e59fff8665
4 changed files with 71 additions and 16 deletions
|
@ -4,6 +4,7 @@
|
||||||
#include "stmlib/utils/random.h"
|
#include "stmlib/utils/random.h"
|
||||||
#include <stm32f37x_conf.h>
|
#include <stm32f37x_conf.h>
|
||||||
#include <u8g2.h>
|
#include <u8g2.h>
|
||||||
|
#include "spi_mode.h"
|
||||||
|
|
||||||
using namespace stmlib;
|
using namespace stmlib;
|
||||||
|
|
||||||
|
@ -21,6 +22,8 @@ u8g2_t* Display::u8g2()
|
||||||
return &u8g2_;
|
return &u8g2_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void InitSPI(void);
|
||||||
|
|
||||||
void Display::Init()
|
void Display::Init()
|
||||||
{
|
{
|
||||||
// init SS/CS/RST GPIO
|
// init SS/CS/RST GPIO
|
||||||
|
@ -53,19 +56,7 @@ void Display::Init()
|
||||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
||||||
SPI_I2S_DeInit(SPI2);
|
SPI_I2S_DeInit(SPI2);
|
||||||
// Initialize SPI
|
// Initialize SPI
|
||||||
SPI_InitTypeDef spi_init;
|
InitSPI(SPI_MODE_DISPLAY);
|
||||||
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);
|
|
||||||
|
|
||||||
GPIO_WriteBit(GPIOB, kPinReset, Bit_RESET);
|
GPIO_WriteBit(GPIOB, kPinReset, Bit_RESET);
|
||||||
asm("nop");
|
asm("nop");
|
||||||
|
|
||||||
|
@ -73,6 +64,7 @@ void Display::Init()
|
||||||
InitGLib();
|
InitGLib();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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,
|
||||||
U8X8_UNUSED void* arg_ptr)
|
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);
|
GPIO_WriteBit(GPIOB, kPinDataCommand, Bit_RESET);
|
||||||
break;
|
break;
|
||||||
case U8X8_MSG_BYTE_START_TRANSFER:
|
case U8X8_MSG_BYTE_START_TRANSFER:
|
||||||
|
InitSPI(SPI_MODE_DISPLAY);
|
||||||
GPIO_WriteBit(GPIOB, kPinEnable, Bit_RESET);
|
GPIO_WriteBit(GPIOB, kPinEnable, Bit_RESET);
|
||||||
break;
|
break;
|
||||||
case U8X8_MSG_BYTE_END_TRANSFER:
|
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);
|
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;
|
output_buf = default_buf = u8g2_.tile_buf_ptr;
|
||||||
u8g2_InitDisplay(&u8g2_);
|
u8g2_InitDisplay(&u8g2_);
|
||||||
u8g2_SetContrast(&u8g2_, 127);
|
u8g2_SetContrast(&u8g2_, 255);
|
||||||
u8g2_SetPowerSave(&u8g2_, 0);
|
u8g2_SetPowerSave(&u8g2_, 0);
|
||||||
|
|
||||||
Random::Seed(42);
|
Random::Seed(42);
|
||||||
|
|
38
midi2cv/drivers/spi_mode.cc
Normal file
38
midi2cv/drivers/spi_mode.cc
Normal 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);
|
||||||
|
}
|
13
midi2cv/drivers/spi_mode.h
Normal file
13
midi2cv/drivers/spi_mode.h
Normal 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
|
|
@ -63,6 +63,15 @@ void TIM2_IRQHandler(void)
|
||||||
ui.Flush();
|
ui.Flush();
|
||||||
count = 0;
|
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);
|
SysTick_Config(F_CPU / 8000);
|
||||||
IWDG_Enable();
|
IWDG_Enable();
|
||||||
gpio.Init();
|
gpio.Init();
|
||||||
asm("bkpt #0");
|
// asm("bkpt #0");
|
||||||
display.Init();
|
display.Init();
|
||||||
InitTimers();
|
InitTimers();
|
||||||
}
|
}
|
||||||
|
@ -112,6 +121,8 @@ int main(void)
|
||||||
{
|
{
|
||||||
Init();
|
Init();
|
||||||
while (1) {
|
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();
|
ui.Update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue