mirror of
https://github.com/jhbruhn/eurorack.git
synced 2025-03-14 18:55:48 +00:00
Improve dac performance
This commit is contained in:
parent
6e2b61c298
commit
7d2acb8a96
2 changed files with 47 additions and 37 deletions
|
@ -7,11 +7,15 @@
|
|||
|
||||
namespace stereo_mix {
|
||||
|
||||
class Dac { // MCP4xx2 dac implementation
|
||||
GPIO_TypeDef* kMDacPorts[] = { GPIOB, GPIOB, GPIOB, GPIOB };
|
||||
const uint16_t kMDacPins[] = { GPIO_PIN_8, GPIO_PIN_9, GPIO_PIN_10, GPIO_PIN_11 };
|
||||
|
||||
GPIO_TypeDef* kOffsetDacPorts[] = { GPIOA, GPIOA, GPIOA, GPIOA };
|
||||
const uint16_t kOffsetDacPins[] = { GPIO_PIN_8, GPIO_PIN_9, GPIO_PIN_10, GPIO_PIN_11 };
|
||||
|
||||
class Dacs { // MCP4xx2 dac implementation
|
||||
public:
|
||||
Dac(GPIO_TypeDef* ssGpioPort_, uint16_t ssGpioPin_)
|
||||
: ssGpioPort(ssGpioPort_)
|
||||
, ssGpioPin(ssGpioPin_)
|
||||
Dacs()
|
||||
{
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
|
@ -20,18 +24,27 @@ class Dac { // MCP4xx2 dac implementation
|
|||
HAL_NVIC_SetPriority(SPI1_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(SPI1_IRQn);
|
||||
|
||||
ssGpioPort = ssGpioPort_;
|
||||
ssGpioPin = ssGpioPin_;
|
||||
// init SS/CS/RST GPIO
|
||||
|
||||
GPIO_InitTypeDef gpio_init;
|
||||
gpio_init.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
gpio_init.Pull = GPIO_NOPULL;
|
||||
gpio_init.Pin = ssGpioPin;
|
||||
HAL_GPIO_Init(ssGpioPort, &gpio_init);
|
||||
|
||||
HAL_GPIO_WritePin(ssGpioPort, ssGpioPin, GPIO_PIN_SET);
|
||||
for (size_t i = 0; i < 8; i++) {
|
||||
// init SS/CS/RST GPIO
|
||||
uint16_t ssGpioPin;
|
||||
GPIO_TypeDef* ssGpioPort;
|
||||
if (i < 4) {
|
||||
ssGpioPin = kMDacPins[i];
|
||||
ssGpioPort = kMDacPorts[i];
|
||||
} else {
|
||||
ssGpioPin = kOffsetDacPins[i - 4];
|
||||
ssGpioPort = kOffsetDacPorts[i - 4];
|
||||
}
|
||||
gpio_init.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
gpio_init.Pull = GPIO_NOPULL;
|
||||
gpio_init.Pin = ssGpioPin;
|
||||
HAL_GPIO_Init(ssGpioPort, &gpio_init);
|
||||
|
||||
HAL_GPIO_WritePin(ssGpioPort, ssGpioPin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
// init AF GPIO
|
||||
|
||||
|
@ -56,7 +69,7 @@ class Dac { // MCP4xx2 dac implementation
|
|||
SPI1->CR1 |= SPI_CR1_SPE; // this is required only once
|
||||
};
|
||||
|
||||
void Write16(uint8_t channel, uint16_t value, uint8_t gain, uint8_t buffered)
|
||||
void Write16(uint8_t device, uint8_t channel, uint16_t value, uint8_t gain, uint8_t buffered)
|
||||
{
|
||||
if (channel > 1)
|
||||
return; // only 2 channels available
|
||||
|
@ -74,21 +87,19 @@ class Dac { // MCP4xx2 dac implementation
|
|||
value |= gain << 13; // set gain
|
||||
value |= 1 << 12; // shutdown always set to 1
|
||||
|
||||
ssGpioPort->BRR |= ssGpioPin;
|
||||
kMDacPorts[device]->BRR |= kMDacPins[device];
|
||||
kOffsetDacPorts[device]->BRR |= kOffsetDacPins[device];
|
||||
SPI1->DR = value;
|
||||
while ((SPI1->SR & (SPI_SR_TXE | SPI_SR_BSY)) != SPI_SR_TXE)
|
||||
;
|
||||
ssGpioPort->BSRR |= ssGpioPin;
|
||||
kMDacPorts[device]->BSRR |= kMDacPins[device];
|
||||
kOffsetDacPorts[device]->BSRR |= kOffsetDacPins[device];
|
||||
};
|
||||
|
||||
void Write16(uint8_t channel, uint16_t value)
|
||||
void Write16(uint8_t device, uint8_t channel, uint16_t value)
|
||||
{
|
||||
Write16(channel, value, 1, 0);
|
||||
Write16(device, channel, value, 1, 0);
|
||||
};
|
||||
|
||||
private:
|
||||
GPIO_TypeDef* ssGpioPort;
|
||||
uint16_t ssGpioPin;
|
||||
};
|
||||
|
||||
}
|
|
@ -3,7 +3,7 @@
|
|||
#include "drivers/debug_pin.h"
|
||||
|
||||
#include "drivers/adc.h"
|
||||
#include "drivers/dac.h"
|
||||
#include "drivers/dacs.h"
|
||||
#include "drivers/leds.h"
|
||||
#include "drivers/peripherals.h"
|
||||
#include "drivers/switches.h"
|
||||
|
@ -16,10 +16,7 @@
|
|||
using namespace stereo_mix;
|
||||
using namespace stmlib;
|
||||
|
||||
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 }
|
||||
};
|
||||
Dacs dacs;
|
||||
|
||||
Adc adc;
|
||||
Leds leds;
|
||||
|
@ -148,7 +145,7 @@ void Init(void)
|
|||
HAL_NVIC_EnableIRQ(TIM6_IRQn);
|
||||
htim6.Init.Prescaler = 64;
|
||||
htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim6.Init.Period = 128; //256; //512;
|
||||
htim6.Init.Period = 96; //128; //256; //512;
|
||||
htim6.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim6.Init.RepetitionCounter = 0;
|
||||
HAL_TIM_Base_Init(&htim6);
|
||||
|
@ -159,19 +156,21 @@ void Init(void)
|
|||
|
||||
void WriteOutputs(void)
|
||||
{
|
||||
DEBUG_ON
|
||||
uint16_t out[kNumChannels][2];
|
||||
DEBUG_ON
|
||||
for (int i = 0; i < kNumChannels; i++) {
|
||||
uint16_t out[2];
|
||||
int16_t cvs[2];
|
||||
cvs[0] = 65535 - adc.cv_value(AdcChannel(ADC_CHANNEL_PAN_1 + i));
|
||||
cvs[1] = adc.cv_value(AdcChannel(ADC_CHANNEL_VOL_1 + i));
|
||||
processors[i].Process(cvs, out);
|
||||
dacs[i].Write16(0, out[0]);
|
||||
dacs[i + 4].Write16(0, out[0]);
|
||||
dacs[i].Write16(1, out[1]);
|
||||
dacs[i + 4].Write16(1, out[1]);
|
||||
processors[i].Process(cvs, out[i]);
|
||||
}
|
||||
DEBUG_OFF
|
||||
for (int i = 0; i < kNumChannels; i++) {
|
||||
dacs.Write16(i, 0, out[i][0]);
|
||||
//dacs.Write16(0, out[i][0]);
|
||||
dacs.Write16(i, 1, out[i][1]);
|
||||
//dacs[i + 4].Write16(1, out[i][1]);
|
||||
}
|
||||
DEBUG_OFF
|
||||
}
|
||||
|
||||
int main(void)
|
||||
|
|
Loading…
Reference in a new issue