mirror of
https://github.com/jhbruhn/eurorack.git
synced 2025-03-15 02:55:49 +00:00
Fixes for stereo_mix hal drivers
This commit is contained in:
parent
0f0ac98496
commit
07991c2cf4
6 changed files with 72 additions and 76 deletions
|
@ -1,8 +1,5 @@
|
||||||
#include "stereo_mix/drivers/adc.h"
|
#include "stereo_mix/drivers/adc.h"
|
||||||
#include "stm32f030x8.h"
|
#include "peripherals.h"
|
||||||
#include "stm32f0xx_hal_dma.h"
|
|
||||||
#include "stm32f0xx_hal_dma_ex.h"
|
|
||||||
|
|
||||||
#include <stm32f0xx_hal.h>
|
#include <stm32f0xx_hal.h>
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
@ -11,7 +8,7 @@ void DMA_TransferComplete(DMA_HandleTypeDef* dma); // declared in stereo_mix.cc
|
||||||
|
|
||||||
namespace stereo_mix {
|
namespace stereo_mix {
|
||||||
|
|
||||||
void Adc::Init()
|
Adc::Adc()
|
||||||
{
|
{
|
||||||
__HAL_RCC_DMA1_CLK_ENABLE();
|
__HAL_RCC_DMA1_CLK_ENABLE();
|
||||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||||
|
@ -38,16 +35,15 @@ void Adc::Init()
|
||||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14, GPIO_PIN_RESET);
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14, GPIO_PIN_RESET);
|
||||||
this->mux_index_ = 0;
|
this->mux_index_ = 0;
|
||||||
|
|
||||||
adc.Init.Resolution = ADC_RESOLUTION_12B;
|
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
|
||||||
adc.Init.ContinuousConvMode = ENABLE;
|
hadc1.Init.ContinuousConvMode = ENABLE;
|
||||||
adc.Init.DiscontinuousConvMode = DISABLE;
|
hadc1.Init.DiscontinuousConvMode = DISABLE;
|
||||||
adc.Init.DataAlign = ADC_DATAALIGN_LEFT;
|
hadc1.Init.DataAlign = ADC_DATAALIGN_LEFT;
|
||||||
adc.Init.ScanConvMode = ADC_SCAN_ENABLE;
|
hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
|
||||||
adc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
||||||
adc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
|
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
|
||||||
adc.Init.DMAContinuousRequests = DISABLE;
|
hadc1.Init.DMAContinuousRequests = DISABLE;
|
||||||
adc.Instance = ADC1;
|
HAL_ADC_Init(&hadc1);
|
||||||
HAL_ADC_Init(&adc);
|
|
||||||
HAL_NVIC_SetPriority(ADC1_IRQn, 0, 0);
|
HAL_NVIC_SetPriority(ADC1_IRQn, 0, 0);
|
||||||
HAL_NVIC_EnableIRQ(ADC1_IRQn);
|
HAL_NVIC_EnableIRQ(ADC1_IRQn);
|
||||||
|
|
||||||
|
@ -55,44 +51,43 @@ void Adc::Init()
|
||||||
sConfig.Channel = ADC_CHANNEL_0;
|
sConfig.Channel = ADC_CHANNEL_0;
|
||||||
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
|
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
|
||||||
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
|
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
|
||||||
HAL_ADC_ConfigChannel(&adc, &sConfig);
|
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
|
||||||
sConfig.Channel = ADC_CHANNEL_1;
|
sConfig.Channel = ADC_CHANNEL_1;
|
||||||
HAL_ADC_ConfigChannel(&adc, &sConfig);
|
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
|
||||||
sConfig.Channel = ADC_CHANNEL_2;
|
sConfig.Channel = ADC_CHANNEL_2;
|
||||||
HAL_ADC_ConfigChannel(&adc, &sConfig);
|
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
|
||||||
sConfig.Channel = ADC_CHANNEL_3;
|
sConfig.Channel = ADC_CHANNEL_3;
|
||||||
HAL_ADC_ConfigChannel(&adc, &sConfig);
|
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
|
||||||
sConfig.Channel = ADC_CHANNEL_4;
|
sConfig.Channel = ADC_CHANNEL_4;
|
||||||
HAL_ADC_ConfigChannel(&adc, &sConfig);
|
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
|
||||||
sConfig.Channel = ADC_CHANNEL_5;
|
sConfig.Channel = ADC_CHANNEL_5;
|
||||||
HAL_ADC_ConfigChannel(&adc, &sConfig);
|
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
|
||||||
sConfig.Channel = ADC_CHANNEL_6;
|
sConfig.Channel = ADC_CHANNEL_6;
|
||||||
HAL_ADC_ConfigChannel(&adc, &sConfig);
|
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
|
||||||
sConfig.Channel = ADC_CHANNEL_7;
|
sConfig.Channel = ADC_CHANNEL_7;
|
||||||
HAL_ADC_ConfigChannel(&adc, &sConfig);
|
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
|
||||||
sConfig.Channel = ADC_CHANNEL_8;
|
sConfig.Channel = ADC_CHANNEL_8;
|
||||||
HAL_ADC_ConfigChannel(&adc, &sConfig);
|
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
|
||||||
|
|
||||||
dma.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
hdma1_channel1.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
||||||
dma.Init.PeriphInc = DMA_PINC_DISABLE;
|
hdma1_channel1.Init.PeriphInc = DMA_PINC_DISABLE;
|
||||||
dma.Init.MemInc = DMA_MINC_ENABLE;
|
hdma1_channel1.Init.MemInc = DMA_MINC_ENABLE;
|
||||||
dma.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
|
hdma1_channel1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
|
||||||
dma.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
|
hdma1_channel1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
|
||||||
dma.Init.Mode = DMA_CIRCULAR;
|
hdma1_channel1.Init.Mode = DMA_CIRCULAR;
|
||||||
dma.Init.Priority = DMA_PRIORITY_LOW;
|
hdma1_channel1.Init.Priority = DMA_PRIORITY_LOW;
|
||||||
dma.Instance = DMA1_Channel1;
|
HAL_DMA_Init(&hdma1_channel1);
|
||||||
HAL_DMA_Init(&dma);
|
__HAL_LINKDMA(&hadc1, DMA_Handle, hdma1_channel1);
|
||||||
__HAL_LINKDMA(&adc, DMA_Handle, dma);
|
|
||||||
|
|
||||||
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
|
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
|
||||||
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
|
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
|
||||||
|
|
||||||
HAL_ADC_Start_DMA(&adc, (uint32_t*)values_, ADC_CHANNEL_LAST);
|
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)values_, ADC_CHANNEL_LAST);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Adc::DeInit()
|
Adc::~Adc()
|
||||||
{
|
{
|
||||||
HAL_ADC_DeInit(&adc);
|
HAL_ADC_DeInit(&hadc1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Adc::OnDMATransferComplete()
|
void Adc::OnDMATransferComplete()
|
||||||
|
@ -106,7 +101,7 @@ void Adc::OnDMATransferComplete()
|
||||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, static_cast<GPIO_PinState>(address & 1));
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, static_cast<GPIO_PinState>(address & 1));
|
||||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, static_cast<GPIO_PinState>(address & 2));
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, static_cast<GPIO_PinState>(address & 2));
|
||||||
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, static_cast<GPIO_PinState>(address & 4));
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, static_cast<GPIO_PinState>(address & 4));
|
||||||
HAL_ADC_Start_DMA(&adc, (uint32_t*)values_, ADC_CHANNEL_LAST);
|
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)values_, ADC_CHANNEL_LAST);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace rings
|
} // namespace rings
|
||||||
|
|
|
@ -30,11 +30,9 @@ enum AdcGroup {
|
||||||
|
|
||||||
class Adc {
|
class Adc {
|
||||||
public:
|
public:
|
||||||
Adc() {}
|
Adc();
|
||||||
~Adc() {}
|
~Adc();
|
||||||
|
|
||||||
void Init();
|
|
||||||
void DeInit();
|
|
||||||
void OnDMATransferComplete();
|
void OnDMATransferComplete();
|
||||||
inline const uint16_t* values() { return &values_[0]; }
|
inline const uint16_t* values() { return &values_[0]; }
|
||||||
inline int32_t value(int32_t channel) const
|
inline int32_t value(int32_t channel) const
|
||||||
|
@ -45,8 +43,6 @@ class Adc {
|
||||||
{
|
{
|
||||||
return static_cast<float>(values_[index]) / 65536.0f;
|
return static_cast<float>(values_[index]) / 65536.0f;
|
||||||
}
|
}
|
||||||
DMA_HandleTypeDef dma;
|
|
||||||
ADC_HandleTypeDef adc;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint16_t values_[ADC_CHANNEL_COUNT];
|
uint16_t values_[ADC_CHANNEL_COUNT];
|
||||||
|
|
|
@ -1,14 +1,17 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "peripherals.h"
|
||||||
|
#include "stm32f030x8.h"
|
||||||
#include "stmlib/stmlib.h"
|
#include "stmlib/stmlib.h"
|
||||||
#include <stm32f0xx_hal.h>
|
#include <stm32f0xx_hal.h>
|
||||||
|
|
||||||
namespace stereo_mix {
|
namespace stereo_mix {
|
||||||
|
|
||||||
|
|
||||||
class Dac { // MCP4xx2 dac implementation
|
class Dac { // MCP4xx2 dac implementation
|
||||||
public:
|
public:
|
||||||
void Init(GPIO_TypeDef* ssGpioPort_, uint16_t ssGpioPin_)
|
Dac(GPIO_TypeDef* ssGpioPort_, uint16_t ssGpioPin_)
|
||||||
|
: ssGpioPort(ssGpioPort_)
|
||||||
|
, ssGpioPin(ssGpioPin_)
|
||||||
{
|
{
|
||||||
ssGpioPort = ssGpioPort_;
|
ssGpioPort = ssGpioPort_;
|
||||||
ssGpioPin = ssGpioPin_;
|
ssGpioPin = ssGpioPin_;
|
||||||
|
@ -36,20 +39,19 @@ class Dac { // MCP4xx2 dac implementation
|
||||||
|
|
||||||
// init SPI
|
// init SPI
|
||||||
__HAL_RCC_SPI1_CLK_ENABLE();
|
__HAL_RCC_SPI1_CLK_ENABLE();
|
||||||
// HAL_SPI_DeInit(&spi);
|
// HAL_SPI_DeInit(&spi);
|
||||||
|
|
||||||
// Initialize SPI TODO: check which config we need
|
// Initialize SPI TODO: check which config we need
|
||||||
spi.Init.Direction = SPI_DIRECTION_2LINES;
|
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
|
||||||
spi.Init.Mode = SPI_MODE_MASTER;
|
hspi1.Init.Mode = SPI_MODE_MASTER;
|
||||||
spi.Init.DataSize = SPI_DATASIZE_16BIT;
|
hspi1.Init.DataSize = SPI_DATASIZE_16BIT;
|
||||||
spi.Init.CLKPolarity = SPI_POLARITY_HIGH;
|
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
|
||||||
spi.Init.CLKPhase = SPI_PHASE_1EDGE;
|
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||||
spi.Init.NSS = SPI_NSS_SOFT;
|
hspi1.Init.NSS = SPI_NSS_SOFT;
|
||||||
spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
|
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
|
||||||
spi.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||||
spi.Init.CRCPolynomial = 7;
|
hspi1.Init.CRCPolynomial = 7;
|
||||||
spi.Instance = SPI1;
|
HAL_SPI_Init(&hspi1);
|
||||||
HAL_SPI_Init(&spi);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void Write16(uint8_t channel, uint16_t value, uint8_t gain, uint8_t buffered)
|
void Write16(uint8_t channel, uint16_t value, uint8_t gain, uint8_t buffered)
|
||||||
|
@ -71,9 +73,9 @@ class Dac { // MCP4xx2 dac implementation
|
||||||
value |= 1 << 12; // shutdown always set to 1
|
value |= 1 << 12; // shutdown always set to 1
|
||||||
|
|
||||||
HAL_GPIO_WritePin(ssGpioPort, ssGpioPin, GPIO_PIN_RESET);
|
HAL_GPIO_WritePin(ssGpioPort, ssGpioPin, GPIO_PIN_RESET);
|
||||||
HAL_SPI_Transmit(&spi, (uint8_t*) &value, 1, 1000);
|
HAL_SPI_Transmit(&hspi1, (uint8_t*)&value, 1, 1000);
|
||||||
//SPI_I2S_SendData16(SPI1, value); // MSB first, specified in config
|
//SPI_I2S_SendData16(SPI1, value); // MSB first, specified in config
|
||||||
while (HAL_SPI_GetState(&spi) == HAL_SPI_STATE_BUSY) {
|
while (HAL_SPI_GetState(&hspi1) & HAL_SPI_STATE_BUSY) {
|
||||||
asm("nop");
|
asm("nop");
|
||||||
}
|
}
|
||||||
HAL_GPIO_WritePin(ssGpioPort, ssGpioPin, GPIO_PIN_SET);
|
HAL_GPIO_WritePin(ssGpioPort, ssGpioPin, GPIO_PIN_SET);
|
||||||
|
@ -85,7 +87,6 @@ class Dac { // MCP4xx2 dac implementation
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SPI_HandleTypeDef spi;
|
|
||||||
GPIO_TypeDef* ssGpioPort;
|
GPIO_TypeDef* ssGpioPort;
|
||||||
uint16_t ssGpioPin;
|
uint16_t ssGpioPin;
|
||||||
};
|
};
|
||||||
|
|
5
stereo_mix/drivers/peripherals.c
Normal file
5
stereo_mix/drivers/peripherals.c
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
#include "peripherals.h"
|
||||||
|
|
||||||
|
ADC_HandleTypeDef hadc1 = { .Instance = ADC1 };
|
||||||
|
DMA_HandleTypeDef hdma1_channel1 = { .Instance = DMA1_Channel1 };
|
||||||
|
SPI_HandleTypeDef hspi1 = { .Instance = SPI1 };
|
7
stereo_mix/drivers/peripherals.h
Normal file
7
stereo_mix/drivers/peripherals.h
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stm32f0xx_hal.h>
|
||||||
|
|
||||||
|
extern ADC_HandleTypeDef hadc1;
|
||||||
|
extern DMA_HandleTypeDef hdma1_channel1;
|
||||||
|
extern SPI_HandleTypeDef hspi1;
|
|
@ -1,13 +1,15 @@
|
||||||
#include "drivers/adc.h"
|
#include "drivers/adc.h"
|
||||||
#include "drivers/dac.h"
|
#include "drivers/dac.h"
|
||||||
|
#include "drivers/peripherals.h"
|
||||||
#include "resources.h"
|
#include "resources.h"
|
||||||
#include "stm32f030x8.h"
|
|
||||||
#include "stm32f0xx_hal_adc.h"
|
|
||||||
#include <stm32f0xx_hal.h>
|
#include <stm32f0xx_hal.h>
|
||||||
|
|
||||||
using namespace stereo_mix;
|
using namespace stereo_mix;
|
||||||
|
|
||||||
Dac dacs[8];
|
Dac dacs[8] = {
|
||||||
|
{ GPIOB, GPIO_PIN_8 }, { GPIOB, GPIO_PIN_9 }, { GPIOB, GPIO_PIN_10 }, { GPIOB, GPIO_PIN_11 },
|
||||||
|
{ GPIOA, GPIO_PIN_8 }, { GPIOA, GPIO_PIN_9 }, { GPIOA, GPIO_PIN_10 }, { GPIOA, GPIO_PIN_11 }
|
||||||
|
};
|
||||||
Adc adc;
|
Adc adc;
|
||||||
|
|
||||||
// Default interrupt handlers.
|
// Default interrupt handlers.
|
||||||
|
@ -80,11 +82,11 @@ void SystemClock_Config(void)
|
||||||
}
|
}
|
||||||
void DMA1_Channel1_IRQHandler(void)
|
void DMA1_Channel1_IRQHandler(void)
|
||||||
{
|
{
|
||||||
HAL_DMA_IRQHandler(&adc.dma);
|
HAL_DMA_IRQHandler(&hdma1_channel1);
|
||||||
}
|
}
|
||||||
void ADC1_IRQHandler(void)
|
void ADC1_IRQHandler(void)
|
||||||
{
|
{
|
||||||
HAL_ADC_IRQHandler(&adc.adc);
|
HAL_ADC_IRQHandler(&hadc1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
|
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
|
||||||
|
@ -100,16 +102,6 @@ int main(void)
|
||||||
__HAL_RCC_SYSCFG_CLK_ENABLE();
|
__HAL_RCC_SYSCFG_CLK_ENABLE();
|
||||||
__HAL_RCC_PWR_CLK_ENABLE();
|
__HAL_RCC_PWR_CLK_ENABLE();
|
||||||
|
|
||||||
dacs[0].Init(GPIOB, GPIO_PIN_8);
|
|
||||||
dacs[1].Init(GPIOB, GPIO_PIN_9);
|
|
||||||
dacs[2].Init(GPIOB, GPIO_PIN_10);
|
|
||||||
dacs[3].Init(GPIOB, GPIO_PIN_11);
|
|
||||||
dacs[4].Init(GPIOA, GPIO_PIN_8);
|
|
||||||
dacs[5].Init(GPIOA, GPIO_PIN_9);
|
|
||||||
dacs[6].Init(GPIOA, GPIO_PIN_10);
|
|
||||||
dacs[7].Init(GPIOA, GPIO_PIN_11);
|
|
||||||
|
|
||||||
adc.Init();
|
|
||||||
while (true) {
|
while (true) {
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
uint32_t value_l;
|
uint32_t value_l;
|
||||||
|
|
Loading…
Reference in a new issue