Skip to content

Commit

Permalink
correct lookup table. adc now returns real world voltages, currents a…
Browse files Browse the repository at this point in the history
…nd resistances
  • Loading branch information
trentgill committed Dec 28, 2023
1 parent 75af028 commit 52eefde
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 28 deletions.
20 changes: 14 additions & 6 deletions ll/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ void ADC_Init(void)
}

// Channel configuration
sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
// sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
sConfig.SamplingTime = ADC_SAMPLETIME_144CYCLES;
sConfig.Offset = 0;

sConfig.Channel = ADC_CHANNEL_0;
Expand Down Expand Up @@ -70,7 +71,6 @@ void ADC_Init(void)
}

static void start_conversion(int offset){
TP_adc_mux_1(mchan);
if( HAL_ADC_Start_DMA( &AdcHandle
, (uint32_t*)&adc_raw[offset*ADC_CHANNELS]
, ADC_CHANNELS
Expand Down Expand Up @@ -145,13 +145,21 @@ void DMA2_Stream4_IRQHandler(void){
HAL_DMA_IRQHandler(AdcHandle.DMA_Handle);
}

// trying this to see if MUX switching & settling time is an issue
static int burn = 0;
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle){
// advance MUX
mchan++;
mchan &= 0x7; // wrap to 8 channels
if(burn){
burn = 0;
} else {
burn = 1;
// advance MUX
mchan++;
mchan %= 8;
TP_adc_mux_1(mchan);
}
start_conversion(mchan);

// start next conversion
start_conversion(mchan);
// printf("adc conv complete\n\r");
}
void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc){
Expand Down
6 changes: 4 additions & 2 deletions ll/dac108.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ void sai_start_transmit(uint16_t* hwords, uint16_t count){
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);
// printf("sai dma err: 0x%0x\n\r", hdma_tx_a.ErrorCode);
printf("sai dma err\n\r");
}
}

Expand Down Expand Up @@ -308,7 +309,8 @@ 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);
// printf("sai error 0x%x\n\r", hsai->ErrorCode);
printf("sai error");
}

static const uint16_t write_through_mode = 0b1001000000000000;
Expand Down
60 changes: 40 additions & 20 deletions ll/tp.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ void TP_jack_direction(int select){
}
}

// ADCMUX output (4): D3, D4, D5, D6
// ADCMUX output (4): D3, D4, D6, D5
static void init_adcmux(void){
g.Mode = GPIO_MODE_OUTPUT_PP;
g.Pull = GPIO_NOPULL;
g.Speed = GPIO_SPEED_FAST;
g.Pin = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6;
g.Pin = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_6 | GPIO_PIN_5;
HAL_GPIO_Init(GPIOD, &g);
}
void TP_adc_mux_1(int chan){
Expand All @@ -262,8 +262,8 @@ void TP_adc_mux_1(int chan){
} else if(chan < 8){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, 1); // enable
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, chan & 0b1);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_5, (chan & 0b10)>>1);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_6, (chan & 0b100)>>2);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_6, (chan & 0b10)>>1);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_5, (chan & 0b100)>>2);
}
}

Expand All @@ -274,35 +274,35 @@ void TP_dac(int chan, float value){

#include "adc.h"
static const int adc_lookup[] = {
5, // adc0 = jack
37, // adc1
32,
4, // adc0 = jack
36, // adc1
31,
26,
21,
17, // 5
12,
7,
2,
37,
32, // 10
27,
22,
18,
13,
8,
8, // 15
3,
38,
33,
28,
23,
23, // 20
19,
14,
9,
4,
39,
39, // 25
34,
29,
24,
20,
15,
10,
5,
40,
35,
30,
25, // adc28
24, // adc28
// TODO allow string lookup for these
0, // 29 = +i_DUT // 1.1107
5, // 30 = -i_DUT // 0.550
Expand All @@ -317,15 +317,35 @@ float TP_adc(int chan){
return -6.66; // BAD CHANNEL
} else if(chan >= 29){ // power channel
float a = ADC_get(adc_lookup[chan]); // lua is 1-based
switch(chan){
case 29: case 30: // i_DUT // 1V = 100mA
a = 100 * 3*(a/4095.0); // convert to real voltage, then 100x to mA
break;
case 31: case 32: // i_PTC -> actually trying to return measured resistance of PTC
a = (3*(a/4095.0) / 3.69697) // calculate real v-drop across PTC
/ (0.1 * 3*((a-2)/4095.0)); // divide it by measured current (in amps)
break;
case 33: case 34: // +/-12V (absolute)
a = 5 * 3*(a/4095.0); // convert to real voltage, then mult by 5
break;
default: break;
}
// TODO convert to human-readable form
return a;
} else { // regular channel
return ADC_get(adc_lookup[chan]); // lua is 1-based
float a = ADC_get(adc_lookup[chan]);
a = 3.0 * a/4095.0; // actual measured voltage on pin
a -= 1.5; // convert to +/- 1v5
a *= -6.666667; // convert to +/- 10V (and invert)
return a; // lua is 1-based
}
}

/* PIN MAPPINGS for ADC buffer
ACTUALLY!! must subtract one from all of these indices
the lookup table above is corrected for this
1 +CURRENT_DUT (internal TP + DUT consumption)
6 -CURRENT_DUT (internal TP + DUT consumption)
11 +CURRENT_PTC (Vdrop across PTC)
Expand Down

0 comments on commit 52eefde

Please sign in to comment.