Skip to content

Commit

Permalink
non functional dac setup
Browse files Browse the repository at this point in the history
  • Loading branch information
trentgill committed Dec 27, 2023
1 parent fc0538a commit 52197f2
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 468 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ SRC = main.c \
$(HALS)/stm32f7xx_hal_pwr.c \
$(HALS)/stm32f7xx_hal_pwr_ex.c \
$(HALS)/stm32f7xx_hal_rng.c \
$(HALS)/stm32f7xx_hal_sai.c \
$(HALS)/stm32f7xx_hal_spi.c \
$(HALS)/stm32f7xx_hal_tim.c \
$(HALS)/stm32f7xx_hal_tim_ex.c \
Expand Down
18 changes: 9 additions & 9 deletions lib/flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

#include "stm32f7xx.h"

// 16kB calibration
#define CALIBRATION_LOCATION 0x0800C000
#define CALIBRATION_SECTOR FLASH_SECTOR_3
#define CALIBRATION_SIZE (0x4000 - 4)

// 64kB user script
#define USER_SCRIPT_LOCATION 0x08010000
#define USER_SCRIPT_SECTOR FLASH_SECTOR_4
// 16kB calibration -> using 256kB
#define CALIBRATION_LOCATION 0x08180000
#define CALIBRATION_SECTOR FLASH_SECTOR_10 // UNUSED
#define CALIBRATION_SIZE (0x40000 - 4)

// 64kB user script -> using 256kB
#define USER_SCRIPT_LOCATION 0x081C0000
#define USER_SCRIPT_SECTOR FLASH_SECTOR_11
//#define USER_SCRIPT_SIZE (0x10000 - 4)
// #define USER_SCRIPT_SIZE (0x2000 - 4) // 8kB up to v2.1
#define USER_SCRIPT_SIZE (0x4000 - 4) // 16kB v3.0+
#define USER_SCRIPT_SIZE (0x40000 - 4) // 16kB v3.0+

typedef enum { FLASH_Status_Init = 0
, FLASH_Status_Saved = 1
Expand Down
2 changes: 1 addition & 1 deletion lib/slopes.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ typedef struct{
float shaped; // current shaped output voltage
} Slope_t;

#define SLOPE_CHANNELS 4
#define SLOPE_CHANNELS 8

// refactor for dynamic SLOPE_CHANNELS
// refactor for dynamic SAMPLE_RATE
Expand Down
46 changes: 24 additions & 22 deletions ll/adda.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

#include "debug_pin.h"
#include "ads131.h"
#include "dac8565.h"
// #include "dac8565.h"
#include "dac108.h"

#include "../lib/flash.h" // FLASH_*_t
#include "cal_ll.h" // CAL_LL_Init(),
Expand All @@ -28,15 +29,15 @@ static void CAL_ReadFlash( void );

uint16_t ADDA_Init( int adc_timer_ix )
{
ADC_Init( ADDA_BLOCK_SIZE
, ADDA_ADC_CHAN_COUNT
, adc_timer_ix
);
// ADC_Init( ADDA_BLOCK_SIZE
// , ADDA_ADC_CHAN_COUNT
// , adc_timer_ix
// );
DAC_Init( ADDA_BLOCK_SIZE
, ADDA_DAC_CHAN_COUNT
);
CAL_LL_Init();
CAL_ReadFlash();
// CAL_LL_Init();
// CAL_ReadFlash();
return ADDA_BLOCK_SIZE;
}

Expand All @@ -45,12 +46,12 @@ void ADDA_Start( void )
DAC_Start();
}

void ADDA_BlockProcess( uint32_t* dac_pickle_ptr )
static IO_block_t b = { .size = ADDA_BLOCK_SIZE };
void ADDA_BlockProcess( uint16_t* dac_pickle_ptr )
{
IO_block_t b = { .size = ADDA_BLOCK_SIZE };
ADC_UnpickleBlock( b.in[0]
, ADDA_BLOCK_SIZE
);
// ADC_UnpickleBlock( b.in[0]
// , ADDA_BLOCK_SIZE
// );
IO_BlockProcess( &b );
DAC_PickleBlock( dac_pickle_ptr
, b.out[0]
Expand All @@ -60,16 +61,16 @@ void ADDA_BlockProcess( uint32_t* dac_pickle_ptr )

float ADDA_GetADCValue( uint8_t channel )
{
return ADC_GetValue( channel );
return 0.0;
// return ADC_GetValue( channel );
}

__weak IO_block_t* IO_BlockProcess( IO_block_t* b )
{
for( uint16_t i=0; i<(b->size); i++ ){
b->out[0][i] = b->in[0][i];
b->out[1][i] = b->in[1][i];
b->out[2][i] = 2.0;
b->out[3][i] = 3.0;
for(int j=0; j<8; j++){
b->out[j][i] = 0.0;
}
}
return b;
}
Expand Down Expand Up @@ -98,10 +99,10 @@ void CAL_Set( int chan, CAL_Param_t param, float val )
if(chan >= 1 && chan <= 2){ // adc
if(param == CAL_Offset){
cal.adc[chan-1].shift = val;
ADC_CalibrateShift(chan-1, val);
// ADC_CalibrateShift(chan-1, val);
} else {
cal.adc[chan-1].scale = val;
ADC_CalibrateScalar(chan-1, val);
// ADC_CalibrateScalar(chan-1, val);
}
} else if( chan >= 3 && chan <= 6){ // dac
if(param == CAL_Offset){
Expand Down Expand Up @@ -145,15 +146,16 @@ static void CAL_ReadFlash( void )
, sizeof(CAL_chan_t) * (2+4)
) ){
for( int j=0; j<2; j++ ){
ADC_CalibrateShift( j, cal.adc[j].shift );
ADC_CalibrateScalar( j, cal.adc[j].scale );
;;
// ADC_CalibrateShift( j, cal.adc[j].shift );
// ADC_CalibrateScalar( j, cal.adc[j].scale );
}
for( int j=0; j<4; j++ ){
DAC_CalibrateOffset( j, cal.dac[j].shift );
DAC_CalibrateScalar( j, cal.dac[j].scale );
}
} else {
printf("not yet calibrated\n");
CAL_Defaults();
// CAL_Defaults();
}
}
49 changes: 36 additions & 13 deletions ll/dac108.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ uint16_t* samples = NULL;

#define DAC_ZERO_VOLTS ((uint16_t)(((uint32_t)0xFFFF * 2)/3))
#define DAC_V_TO_U16 ((float)(65535.0 / 15.0))
#define DAC_CHANNELSS 4
#define DAC_CHANNELSS 8
float dac_calibrated_offset[DAC_CHANNELSS];
float dac_calibrated_scalar[DAC_CHANNELSS];

Expand All @@ -26,15 +26,16 @@ void DAC_Init(uint16_t bsize, uint8_t chan_count){
sai_init();

// Create the sample buffer for DMA transfer
samp_count = DAC_BUFFER_COUNT * bsize * chan_count;
samples = malloc( sizeof(uint16_t) * samp_count );
for( int i=0; i<samp_count; i++ ){ samples[i] = 0; } // unnecessary
samp_count = DAC_BUFFER_COUNT * bsize * chan_count; // 512
printf("samp_count %x\n\r", samp_count);
samples = malloc( sizeof(uint16_t) * samp_count ); // 1k
if(samples == NULL){ printf("!DAC_buffer\n"); }
for( int i=0; i<samp_count; i++ ){ samples[i] = 0; } // unnecessary

for( int j=0; j<DAC_CHANNELSS; j++ ){
dac_calibrated_offset[j] = 0.0;
dac_calibrated_scalar[j] = DAC_V_TO_U16;
}
// for( int j=0; j<DAC_CHANNELSS; j++ ){
// dac_calibrated_offset[j] = 0.0;
// dac_calibrated_scalar[j] = DAC_V_TO_U16;
// }
}

void DAC_Start(void){
Expand Down Expand Up @@ -132,7 +133,7 @@ static void sai_init(void){
hsai_a.Init.FirstBit = SAI_FIRSTBIT_MSB;
hsai_a.Init.ClockStrobing = SAI_CLOCKSTROBING_RISINGEDGE; // CONFIRM

hsai_a.FrameInit.FrameLength = 8; // was 33 for 32 vals
hsai_a.FrameInit.FrameLength = 17; // was 33 for 32 vals
hsai_a.FrameInit.ActiveFrameLength = 1; // 1 ?
hsai_a.FrameInit.FSDefinition = SAI_FS_STARTFRAME;
hsai_a.FrameInit.FSPolarity = SAI_FS_ACTIVE_HIGH;
Expand All @@ -141,9 +142,13 @@ static void sai_init(void){
hsai_a.SlotInit.FirstBitOffset = 0;
hsai_a.SlotInit.SlotSize = SAI_SLOTSIZE_16B;
hsai_a.SlotInit.SlotNumber = 8;
hsai_a.SlotInit.SlotActive = SAI_SLOTACTIVE_ALL;
hsai_a.SlotInit.SlotActive = SAI_SLOTACTIVE_0 | SAI_SLOTACTIVE_1
| SAI_SLOTACTIVE_2 | SAI_SLOTACTIVE_3
| SAI_SLOTACTIVE_4 | SAI_SLOTACTIVE_5
| SAI_SLOTACTIVE_6 | SAI_SLOTACTIVE_7;

if(HAL_SAI_Init(&hsai_a)){
printf("sai init failed\n\r");
return;
}

Expand Down Expand Up @@ -182,7 +187,7 @@ void HAL_SAI_MspInit(SAI_HandleTypeDef *hsai)
hdma_tx_a.Init.Mode = DMA_CIRCULAR;
hdma_tx_a.Init.Priority = DMA_PRIORITY_HIGH;
hdma_tx_a.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_tx_a.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_tx_a.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_1QUARTERFULL;
hdma_tx_a.Init.MemBurst = DMA_MBURST_SINGLE;
hdma_tx_a.Init.PeriphBurst = DMA_PBURST_SINGLE;

Expand All @@ -199,11 +204,18 @@ void HAL_SAI_MspInit(SAI_HandleTypeDef *hsai)
// debug(&debug, "HAL_DMA_Init failed");
return;
}

// Codec request triggers transfer & new frame calc
HAL_NVIC_SetPriority( DMA2_Stream3_IRQn
, DAC_IRQPriority
, 1
);
HAL_NVIC_EnableIRQ( DMA2_Stream3_IRQn );
}

void sai_start_transmit(uint16_t* hwords, uint16_t count){
if(HAL_SAI_Transmit_DMA(&hsai_a, (uint8_t*)hwords, count)){
;
printf("sai transmit fail.\n\r");
}
}

Expand All @@ -212,6 +224,9 @@ void sai_start_transmit(uint16_t* hwords, uint16_t count){
// DMA triggered by codec requesting more ADC!
void DMA2_Stream3_IRQHandler(void){
HAL_DMA_IRQHandler(&hdma_tx_a);
if(hdma_tx_a.ErrorCode != HAL_OK){
printf("sai dma err: 0x%0x\n\r", hdma_tx_a.ErrorCode);
}
}

static void callback(int offset){
Expand All @@ -222,7 +237,13 @@ static void callback(int offset){
// float out[b_size2];
// codec_to_floats( in, &in[b_size], &inBuff[offset2], b_size );
// FIXME ONLY ONE OF THE NEXT 2 FUNS

ADDA_BlockProcess( samples );





// (*block_process_fn)( out
// , in // treat ins as zeroes (unused)
// , b_size
Expand All @@ -233,5 +254,7 @@ static void callback(int offset){

void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai){ callback(0); }
void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai){ callback(1); }

void HAL_SAI_ErrorCallback(SAI_HandleTypeDef *hsai){
printf("sai error 0x%x\n\r", hsai->ErrorCode);
}

55 changes: 1 addition & 54 deletions ll/dac108.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,6 @@
#include <stm32f7xx.h>
#include "interrupts.h" // DAC_IRQPriority

// Defn for I2S
#define I2Sx SPI2
#define I2Sx_CLK_ENABLE() __HAL_RCC_SPI2_CLK_ENABLE()
#define I2Sx_DMAx_CLK_ENABLE() __HAL_RCC_DMA1_CLK_ENABLE()
#define I2Sx_NSS_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define I2Sx_SCK_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define I2Sx_MOSI_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define I2Sx_NRST_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()

#define I2Sx_FORCE_RESET() __HAL_RCC_SPI2_FORCE_RESET()
#define I2Sx_RELEASE_RESET() __HAL_RCC_SPI2_RELEASE_RESET()

// Definition for I2Sx Pins
#define I2Sx_NSS_PIN GPIO_PIN_12
#define I2Sx_NSS_GPIO_PORT GPIOB
#define I2Sx_NSS_AF GPIO_AF5_SPI2
#define I2Sx_SCK_PIN GPIO_PIN_13
#define I2Sx_SCK_GPIO_PORT GPIOB
#define I2Sx_SCK_AF GPIO_AF5_SPI2
#define I2Sx_MOSI_PIN GPIO_PIN_15
#define I2Sx_MOSI_GPIO_PORT GPIOB
#define I2Sx_MOSI_AF GPIO_AF5_SPI2
#define I2Sx_NRST_PIN GPIO_PIN_14
#define I2Sx_NRST_GPIO_PORT GPIOB

// Definition for SPId's DMA
#define I2Sx_TX_DMA_CHANNEL DMA_CHANNEL_0
#define I2Sx_TX_DMA_STREAM DMA1_Stream4

// Definition for SPId's NVIC
#define I2Sx_IRQn SPI2_IRQn
#define I2Sx_IRQHandler SPI2_IRQHandler
#define I2Sx_DMA_TX_IRQn DMA1_Stream4_IRQn
#define I2Sx_DMA_TX_IRQHandler DMA1_Stream4_IRQHandler

#define I2Sx_IRQPriority DAC_IRQPriority
#define I2Sx_IRQSubPriority 2
#define I2Sx_DMA_TX_IRQPriority DAC_IRQPriority
#define I2Sx_DMA_TX_IRQSubPriority 1

// dac8565 defines
#define DAC8565_SET_ONE ((uint8_t)0b00010000)
#define DAC8565_PREP_ONE ((uint8_t)0x0)
#define DAC8565_SET_AND_UPDATE ((uint8_t)0b00100000)
#define DAC8565_SET_ALL ((uint8_t)0b00110100)
#define DAC8565_REFRESH_ALL ((uint8_t)0b00110000)

void DAC_Init( uint16_t bsize, uint8_t chan_count );
void DAC_Start(void);

Expand All @@ -60,13 +13,6 @@ void DAC_PickleBlock( uint16_t* dac_pickle_ptr
, uint16_t bsize
);

void I2Sx_DMA_TX_IRQHandler(void);
void I2Sx_IRQHandler(void);

void HAL_I2S_TxCpltCallback( I2S_HandleTypeDef *hi2s );
void HAL_I2S_TxHalfCpltCallback( I2S_HandleTypeDef *hi2s );
void HAL_I2S_ErrorCallback( I2S_HandleTypeDef *hi2s );

void DAC_I2S_MspInit(I2S_HandleTypeDef *hi2s);
void DAC_I2S_MspDeInit(I2S_HandleTypeDef *hi2s);

Expand All @@ -78,3 +24,4 @@ void HAL_SAI_MspInit(SAI_HandleTypeDef *hsai);
void DMA2_Stream3_IRQHandler(void);
void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai);
void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai);
void HAL_SAI_ErrorCallback(SAI_HandleTypeDef *hsai);
Loading

0 comments on commit 52197f2

Please sign in to comment.