mirror of
				https://github.com/jhbruhn/eurorack.git
				synced 2025-11-04 05:16:01 +00:00 
			
		
		
		
	tremendous spi performance improvements
This commit is contained in:
		
							parent
							
								
									90fb81270c
								
							
						
					
					
						commit
						04787576fe
					
				
					 4 changed files with 28 additions and 14 deletions
				
			
		| 
						 | 
				
			
			@ -1,9 +1,9 @@
 | 
			
		|||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "peripherals.h"
 | 
			
		||||
#include "stm32f030x8.h"
 | 
			
		||||
#include "stmlib/stmlib.h"
 | 
			
		||||
#include <stm32f0xx_hal.h>
 | 
			
		||||
#include <stm32f0xx_ll_spi.h>
 | 
			
		||||
 | 
			
		||||
namespace stereo_mix {
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -17,6 +17,9 @@ class Dac { // MCP4xx2 dac implementation
 | 
			
		|||
    __HAL_RCC_GPIOB_CLK_ENABLE();
 | 
			
		||||
    __HAL_RCC_SPI1_CLK_ENABLE();
 | 
			
		||||
 | 
			
		||||
    HAL_NVIC_SetPriority(SPI1_IRQn, 0, 0);
 | 
			
		||||
    HAL_NVIC_EnableIRQ(SPI1_IRQn);
 | 
			
		||||
 | 
			
		||||
    ssGpioPort = ssGpioPort_;
 | 
			
		||||
    ssGpioPin = ssGpioPin_;
 | 
			
		||||
    // init SS/CS/RST GPIO
 | 
			
		||||
| 
						 | 
				
			
			@ -46,10 +49,11 @@ class Dac { // MCP4xx2 dac implementation
 | 
			
		|||
    hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
 | 
			
		||||
    hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
 | 
			
		||||
    hspi1.Init.NSS = SPI_NSS_SOFT;
 | 
			
		||||
    hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
 | 
			
		||||
    hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
 | 
			
		||||
    hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
 | 
			
		||||
    hspi1.Init.CRCPolynomial = 7;
 | 
			
		||||
    HAL_SPI_Init(&hspi1);
 | 
			
		||||
    SPI1->CR1 |= SPI_CR1_SPE; // this is required only once
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  void Write16(uint8_t channel, uint16_t value, uint8_t gain, uint8_t buffered)
 | 
			
		||||
| 
						 | 
				
			
			@ -70,13 +74,11 @@ class Dac { // MCP4xx2 dac implementation
 | 
			
		|||
    value |= gain << 13;     // set gain
 | 
			
		||||
    value |= 1 << 12;        // shutdown always set to 1
 | 
			
		||||
 | 
			
		||||
    HAL_GPIO_WritePin(ssGpioPort, ssGpioPin, GPIO_PIN_RESET);
 | 
			
		||||
    HAL_SPI_Transmit(&hspi1, (uint8_t*)&value, 1, HAL_MAX_DELAY);
 | 
			
		||||
    //SPI_I2S_SendData16(SPI1, value); // MSB first, specified in config
 | 
			
		||||
    /*while (HAL_SPI_GetState(&hspi1) & HAL_SPI_STATE_BUSY) {
 | 
			
		||||
      asm("nop");
 | 
			
		||||
    }*/
 | 
			
		||||
    HAL_GPIO_WritePin(ssGpioPort, ssGpioPin, GPIO_PIN_SET);
 | 
			
		||||
    ssGpioPort->BRR |= ssGpioPin;
 | 
			
		||||
    SPI1->DR = value;
 | 
			
		||||
    while ((SPI1->SR & (SPI_SR_TXE | SPI_SR_BSY)) != SPI_SR_TXE)
 | 
			
		||||
      ;
 | 
			
		||||
    ssGpioPort->BSRR |= ssGpioPin;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  void Write16(uint8_t channel, uint16_t value)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -35,7 +35,12 @@ class Leds {
 | 
			
		|||
    pwm_counter &= 0x1ff; // equals to if(pwm_counter > 512) pwm_counter = 0;
 | 
			
		||||
    blink_counter++;
 | 
			
		||||
    if(blink_counter > 0x2000) blink_counter = 0;
 | 
			
		||||
#ifdef DEBUG_PIN
 | 
			
		||||
    for (size_t i = 0; i < kNumChannels - 1; i++) {
 | 
			
		||||
#else
 | 
			
		||||
    for (size_t i = 0; i < kNumChannels; i++) {
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
      bool in_blink_phase = blink_counter > (0x2000 / 2) || !blinking[i];
 | 
			
		||||
      if (intensities[0] && lut_led_gamma[intensities[i]] >= pwm_counter && in_blink_phase) {
 | 
			
		||||
        kGpioPorts[i]->BSRR |= kGpioPins[i];
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,6 +6,10 @@ SPI_HandleTypeDef hspi1 = { .Instance = SPI1 };
 | 
			
		|||
TIM_HandleTypeDef htim3 = { .Instance = TIM3 };
 | 
			
		||||
TIM_HandleTypeDef htim6 = { .Instance = TIM6 };
 | 
			
		||||
 | 
			
		||||
void SPI1_IRQHandler(void)
 | 
			
		||||
{
 | 
			
		||||
  HAL_SPI_IRQHandler(&hspi1);
 | 
			
		||||
}
 | 
			
		||||
void DMA1_Channel1_IRQHandler(void)
 | 
			
		||||
{
 | 
			
		||||
  HAL_DMA_IRQHandler(&hdma1_channel1);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,7 @@
 | 
			
		|||
 | 
			
		||||
#define ENABLE_DEBUG_PIN
 | 
			
		||||
#include "drivers/debug_pin.h"
 | 
			
		||||
 | 
			
		||||
#include "drivers/adc.h"
 | 
			
		||||
#include "drivers/dac.h"
 | 
			
		||||
#include "drivers/leds.h"
 | 
			
		||||
| 
						 | 
				
			
			@ -142,9 +146,9 @@ void Init(void)
 | 
			
		|||
 | 
			
		||||
  HAL_NVIC_SetPriority(TIM6_IRQn, 1, 0);
 | 
			
		||||
  HAL_NVIC_EnableIRQ(TIM6_IRQn);
 | 
			
		||||
  htim6.Init.Prescaler = 192;
 | 
			
		||||
  htim6.Init.Prescaler = 64;
 | 
			
		||||
  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
 | 
			
		||||
  htim6.Init.Period = 512;
 | 
			
		||||
  htim6.Init.Period = 128; //256; //512;
 | 
			
		||||
  htim6.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 | 
			
		||||
  htim6.Init.RepetitionCounter = 0;
 | 
			
		||||
  HAL_TIM_Base_Init(&htim6);
 | 
			
		||||
| 
						 | 
				
			
			@ -155,6 +159,7 @@ void Init(void)
 | 
			
		|||
 | 
			
		||||
void WriteOutputs(void)
 | 
			
		||||
{
 | 
			
		||||
  DEBUG_ON
 | 
			
		||||
  for (int i = 0; i < kNumChannels; i++) {
 | 
			
		||||
    uint16_t out[2];
 | 
			
		||||
    int16_t cvs[2];
 | 
			
		||||
| 
						 | 
				
			
			@ -166,6 +171,7 @@ void WriteOutputs(void)
 | 
			
		|||
    dacs[i].Write16(1, out[1]);
 | 
			
		||||
    dacs[i + 4].Write16(1, out[1]);
 | 
			
		||||
  }
 | 
			
		||||
  DEBUG_OFF
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(void)
 | 
			
		||||
| 
						 | 
				
			
			@ -176,9 +182,6 @@ int main(void)
 | 
			
		|||
  Init();
 | 
			
		||||
 | 
			
		||||
  while (true) {
 | 
			
		||||
 | 
			
		||||
    ui.DoEvents();
 | 
			
		||||
 | 
			
		||||
//    WriteOutputs();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue