# HG changeset patch # User Ethan Zonca # Date 2017-09-25 19:40:54 # Node ID af6b7df096c5fe520ae205d30ce0f013310f4cff # Parent 639ea4c6e941c52bab687470b4eb233b4d666259 Try new ADC config diff --git a/inc/adc.h b/inc/adc.h --- a/inc/adc.h +++ b/inc/adc.h @@ -3,8 +3,12 @@ #include "stm32f0xx_hal.h" -extern ADC_HandleTypeDef hadc; void adc_init(void); +void adc_start(void); +void adc_stop(void); +uint8_t adc_get_vbatt(void); +int16_t adc_get_dietemp(void); +ADC_HandleTypeDef* adc_gethandle(void); #endif diff --git a/src/adc.c b/src/adc.c --- a/src/adc.c +++ b/src/adc.c @@ -3,6 +3,10 @@ #include "gpio.h" ADC_HandleTypeDef hadc; +DMA_HandleTypeDef hdma_adc; + +#define ADC_BUF_LEN 3 +uint16_t adc_buffer[ADC_BUF_LEN]; // Initialize ADC void adc_init(void) @@ -17,7 +21,7 @@ void adc_init(void) ADC_ChannelConfTypeDef sConfig; hadc.Instance = ADC1; - hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC; + hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC; // hopefully stops in sleep hadc.Init.Resolution = ADC_RESOLUTION12b; hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; @@ -36,5 +40,79 @@ void adc_init(void) sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; HAL_ADC_ConfigChannel(&hadc, &sConfig); + sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; + sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; + sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; + HAL_ADC_ConfigChannel(&hadc, &sConfig); + + + // TODO: AAH might not want to be running this DMA all the time! Meh. + + __DMA1_CLK_ENABLE(); + HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); + + hdma_adc.Instance = DMA1_Channel1; + hdma_adc.Init.Direction = DMA_PERIPH_TO_MEMORY; + hdma_adc.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_adc.Init.MemInc = DMA_MINC_ENABLE; + hdma_adc.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; + hdma_adc.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; + hdma_adc.Init.Mode = DMA_CIRCULAR; + hdma_adc.Init.Priority = DMA_PRIORITY_LOW; + HAL_DMA_Init(&hdma_adc); + + __HAL_LINKDMA(&hadc,DMA_Handle,hdma_adc); + + + HAL_ADC_Start_DMA(&hadc, adc_buffer, ADC_BUF_LEN); + + +} + +void adc_start(void) +{ + HAL_ADC_Start_DMA(&hadc, adc_buffer, ADC_BUF_LEN); } + +void adc_stop(void) +{ + HAL_ADC_Stop_DMA(&hadc); +} + + +uint8_t adc_get_vbatt(void) +{ + return 33; //adc_buffer[0] / 62.5; // tenths of volts ish +} + + + +//See RM0091 section 13.9 and appendix A.7.16 +//Temperature sensor raw value at 30 degrees C, VDDA=3.3V +#define TEMP30_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7B8)) +//Temperature sensor raw value at 110 degrees C, VDDA=3.3V +#define TEMP110_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7C2)) +#define ADC_TEMPERATURE_OFFSET 0 + +int16_t adc_get_dietemp(void) +{ + int32_t tempraw = adc_buffer[1]; + int32_t temperature; + temperature = tempraw - ((int32_t)*TEMP30_CAL_ADDR); + temperature = temperature * (110L - 30L); + temperature = temperature / (*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR); + temperature = temperature + 30L; + + temperature = temperature + ADC_TEMPERATURE_OFFSET; + + return 14; //temperature; + // TODO: Verify +} + + +ADC_HandleTypeDef* adc_gethandle(void) +{ + return &hadc; +} diff --git a/src/interrupts.c b/src/interrupts.c --- a/src/interrupts.c +++ b/src/interrupts.c @@ -7,6 +7,7 @@ #include "interrupts.h" #include "uart.h" #include "rtc.h" +#include "adc.h" #include "gpio.h" extern TIM_HandleTypeDef htim1; @@ -45,3 +46,9 @@ void HAL_RTC_AlarmAEventCallback(RTC_Han { // Do something awesome or not } + +void DMA1_Channel1_IRQHandler(void) +{ + HAL_DMA_IRQHandler(adc_gethandle()->DMA_Handle); +} + diff --git a/src/main.c b/src/main.c --- a/src/main.c +++ b/src/main.c @@ -61,9 +61,6 @@ int main(void) uint8_t nextwspr_time_valid = 0; uint32_t last_wspr_tx_time = 0; - uint8_t fix_ok = 0; - uint8_t numsats = 0; - uint8_t packet_type = 0; while (1) @@ -158,6 +155,7 @@ int main(void) // TODO: Set wspr countdown timer for this transmission! fix_acq_starttime = 0; state = SYSTEM_WSPRTX; + adc_start(); } // If no decent fix in 3 minutes else if(HAL_GetTick() - fix_acq_starttime > 60000 * 3) @@ -196,12 +194,14 @@ int main(void) packet_type = !packet_type; // alternate packet type last_wspr_tx_time = HAL_GetTick(); state = SYSTEM_IDLE; + adc_stop(); } else { // Window was missed, go back to idle, and try again after time delay last_wspr_tx_time = HAL_GetTick(); state = SYSTEM_IDLE; + adc_stop(); } nextwspr_time_valid = 0; // invalidate wspr time } diff --git a/src/rtc.c b/src/rtc.c --- a/src/rtc.c +++ b/src/rtc.c @@ -10,7 +10,7 @@ RTC_HandleTypeDef hrtc; static void Error_Handler(void) { - volatile crap = 1; + volatile uint8_t crap = 1; } // Initialize RTC diff --git a/src/wspr.c b/src/wspr.c --- a/src/wspr.c +++ b/src/wspr.c @@ -135,20 +135,26 @@ void wspr_transmit(uint8_t* grid_locator //////////////////////////////////////// // Encode value from -50C to 39C => 0-89. TODO: Bounds! - uint8_t temp_enc = 12 + 50; + uint32_t temp_enc = adc_get_dietemp() + 50; + if(temp_enc > 89) + temp_enc = 89; // Encode value from 0-39 with some scalar/offset/etc - uint8_t batt_enc = 16; + uint32_t batt_enc = adc_get_vbatt(); // Hopefully in decivolts + if(batt_enc > 39) + batt_enc = 39; // Encode speed in knots from 0-82 to 0-41 - uint8_t speed_enc = gps_getdata()->speed / 2; + uint32_t speed_enc = gps_getdata()->speed / 2; if(speed_enc > 41) speed_enc = 41; // Encode GPS status - uint8_t gps_status = 0b00; // MSB is valid fix, lsb is sats > 8 + uint32_t gps_status = 0b00; // MSB is valid fix, lsb is sats > 8 + - if(gps_getdata()->fixtype == 2 || gps_getdata()->fixtype == 3) + // We always have a fix if we got to this point; and I think we zero out that we had a fix when turning the GPS off before entering this function +// if(gps_getdata()->fixtype == 2 || gps_getdata()->fixtype == 3) gps_status |= 0b10; if(gps_getdata()->sats_in_solution > 5)