Changeset - af6b7df096c5
[Not reviewed]
default
0 6 0
Ethan Zonca - 8 years ago 2017-09-25 19:40:54
ez@ethanzonca.com
Try new ADC config
6 files changed with 106 insertions and 11 deletions:
0 comments (0 inline, 0 general)
inc/adc.h
Show inline comments
 
#ifndef __adc_H
 
#define __adc_H
 
 
#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
src/adc.c
Show inline comments
 
#include "stm32f0xx_hal.h"
 
#include "adc.h"
 
#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)
 
{
 
    __ADC1_CLK_ENABLE();
 
 
    GPIO_InitTypeDef GPIO_InitStruct;
 
    GPIO_InitStruct.Pin = VBATT_SENSE_Pin;
 
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 
    GPIO_InitStruct.Pull = GPIO_NOPULL;
 
    HAL_GPIO_Init(VBATT_SENSE_GPIO_Port, &GPIO_InitStruct);
 
 
    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;
 
    hadc.Init.EOCSelection = EOC_SINGLE_CONV;
 
    hadc.Init.LowPowerAutoWait = DISABLE;
 
    hadc.Init.LowPowerAutoPowerOff = DISABLE;
 
    hadc.Init.ContinuousConvMode = DISABLE;
 
    hadc.Init.DiscontinuousConvMode = DISABLE;
 
    hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
 
    hadc.Init.DMAContinuousRequests = DISABLE;
 
    hadc.Init.Overrun = OVR_DATA_PRESERVED;
 
    HAL_ADC_Init(&hadc);
 
 
    sConfig.Channel = ADC_CHANNEL_6;
 
    sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
 
    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;
 
}
src/interrupts.c
Show inline comments
 
//
 
// Interrupts: all global ISRs
 
//
 
 
#include "stm32f0xx_hal.h"
 
#include "stm32f0xx.h"
 
#include "interrupts.h"
 
#include "uart.h"
 
#include "rtc.h"
 
#include "adc.h"
 
#include "gpio.h"
 
 
extern TIM_HandleTypeDef htim1;
 
extern volatile uint8_t proceed;
 
 
void SysTick_Handler(void)
 
{
 
    HAL_IncTick();
 
    HAL_SYSTICK_IRQHandler();
 
}
 
 
void DMA1_Channel2_3_IRQHandler(void)
 
{
 
    HAL_DMA_IRQHandler(uart_get_txdma_handle());
 
    HAL_DMA_IRQHandler(uart_get_txdma_handle());
 
}
 
 
void USART1_IRQHandler(void)
 
{
 
    HAL_UART_IRQHandler(uart_gethandle());
 
}
 
 
void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
 
{
 
    proceed = 1;
 
    HAL_TIM_IRQHandler(&htim1);
 
}
 
 
void RTC_IRQHandler(void)
 
{
 
  HAL_RTC_AlarmIRQHandler(rtc_gethandle());
 
}
 
 
 
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
 
{
 
	// Do something awesome or not
 
}
 
 
void DMA1_Channel1_IRQHandler(void)
 
{
 
    HAL_DMA_IRQHandler(adc_gethandle()->DMA_Handle);
 
}
 
src/main.c
Show inline comments
 
@@ -40,51 +40,48 @@ uint32_t statled_ontime = 0;
 
int main(void)
 
{
 
    HAL_Init();
 
    HAL_Delay(1000); // startup delay before infinisleep
 
 
    sysclk_init();
 
    rtc_init();
 
    gpio_init();
 
    adc_init();
 
    wspr_init();
 
 
    uint32_t led_timer = HAL_GetTick();
 
 
    led_blink(4);
 
 
    uint16_t blink_rate = BLINK_FAST;
 
    uint8_t state = SYSTEM_GPSACQ;
 
 
    uint32_t gps_polltimer = 0;
 
    uint32_t fix_acq_starttime = 0;
 
    uint32_t nextwspr_time = 0;
 
    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)
 
    {
 
 
    	// Every 10 minutes, wake up and try to wspr
 
    	if(state == SYSTEM_IDLE && (HAL_GetTick() - last_wspr_tx_time > 60000 * 10))
 
    	{
 
    		state = SYSTEM_GPSACQ;
 
    	}
 
 
        // Update fix status every 2 seconds
 
        if(HAL_GetTick() - gps_polltimer > 2000)
 
        {
 
            if(gps_ison())
 
            {
 
            	gps_update_data();
 
 
            	// If odd minute
 
            	if(gps_getdata()->minute % 2)
 
            	{
 
            		// Wait until even minute plus one second, coming soon
 
            		nextwspr_time = HAL_GetTick() + (60000 - (gps_getdata()->second * 1000));
 
                    nextwspr_time_valid = 1;
 
@@ -137,92 +134,95 @@ int main(void)
 
                HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
                HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
                HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
 
 
                if(!gps_ison())
 
                {
 
                	fix_acq_starttime = HAL_GetTick();
 
                    gps_poweron(); // power on and initialize GPS module
 
                }
 
 
                // If 3d fix with a decent enough precision
 
                if( ((gps_getdata()->fixtype == 2) || (gps_getdata()->fixtype == 3)) && gps_getdata()->pdop < 10 && nextwspr_time_valid == 1)
 
                {
 
                    // Disable GPS module
 
                    gps_poweroff();
 
 
                    // TODO: Set RTC from GPS time
 
 
                    // TODO: Set RTC for countdown to next transmission timeslot!
 
 
                    // 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)
 
                {
 
                	// Flash error code and go to idle, try again next time
 
                	led_blink(4);
 
                    gps_poweroff();
 
                    fix_acq_starttime = 0;
 
                    last_wspr_tx_time = HAL_GetTick(); // repeat acq/tx cycle after big time delay
 
                	state = SYSTEM_IDLE;
 
                }
 
            } break;
 
 
            
 
            // Wait for wspr timeslot and start transmitting
 
            case SYSTEM_WSPRTX:
 
            {
 
            	blink_rate = BLINK_MED;
 
                // Wait for wspr countdown timer to expire and go to tx
 
//                if(timeout_expired)
 
//                {
 
 
            	// If we're after the minute but not more than 2s after the minute, start tx
 
            	if(HAL_GetTick() >= nextwspr_time)
 
            	{
 
            		if(HAL_GetTick() < nextwspr_time + 2000)
 
            		{
 
            			volatile double latitude_flt = (double)gps_getdata()->latitude / 10000000.0;
 
            			volatile double longitude_flt = (double)gps_getdata()->longitude / 10000000.0;
 
            			volatile uint8_t grid_locator[7];
 
 
            			__calc_gridloc(grid_locator, latitude_flt, longitude_flt);
 
 
                        // TODO: Switch between alternate and standard packet
 
						wspr_transmit(grid_locator, packet_type);
 
                        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
 
                }
 
            	else
 
            	{
 
                    HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
                    HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
                    HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
                    HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
            	}
 
 
                // Schedule next wakeup (maybe 2mins prior ot timeslot if no osc trim)
 
                // Next wakeup should enter SYSTEM_GPSACQ state...
 
 
            } break;
 
 
        }
 
 
 
        if(HAL_GetTick() - led_timer > blink_rate)
 
        {
 
            ledpulse();
 
            led_timer = HAL_GetTick();
 
        }
src/rtc.c
Show inline comments
 
//
 
// RTC: configure real-time clock
 
//
 
 
#include "stm32f0xx_hal.h"
 
#include "rtc.h"
 
 
 
RTC_HandleTypeDef hrtc;
 
 
static void Error_Handler(void)
 
{
 
	volatile crap = 1;
 
	volatile uint8_t crap = 1;
 
}
 
 
// Initialize RTC
 
void rtc_init(void)
 
{
 
	__HAL_RCC_RTC_ENABLE();
 
 
    
 
  RTC_TimeTypeDef sTime;
 
  RTC_DateTypeDef sDate;
 
  RTC_AlarmTypeDef sAlarm;
 
 
  hrtc.Instance = RTC;
 
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
 
  hrtc.Init.AsynchPrediv = 124;
 
  hrtc.Init.SynchPrediv = 322; // if this has enough bits should be 1.0018Hz based on 40kHz LSI
 
  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
 
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
 
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
 
 
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
 
  {
 
    Error_Handler();
 
  }
src/wspr.c
Show inline comments
 
@@ -114,62 +114,68 @@ void wspr_transmit(uint8_t* grid_locator
 
        chunk = (subalt / 26 / 26);
 

	
 
        call[3] = 'A' + chunk;
 

	
 
        // Subtract off previous portion
 
        subalt -= (chunk * 26 * 26);
 

	
 
        // Mask off following 1 26bit values
 
        chunk = (subalt / 26);
 

	
 
        call[4] = 'A' + chunk;
 

	
 
        // Subtract off previous portion
 
        subalt -= (chunk * 26);
 

	
 
        // Remainder is the last call char
 
        call[5] = 'A' + subalt;
 
 
 

	
 
        ////////////////////////////////////////
 
        // Composite temp/batt/speed/gps 
 
        ////////////////////////////////////////
 

	
 
        // 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)
 
            gps_status |= 0b01;
 

	
 
        uint32_t engdata = gps_status + 2 * (speed_enc + 42 * (batt_enc + 40 * temp_enc));
 

	
 
        ////////////////////////////////////////////
 
        // Encode temp/batt/speed/gps
 
        ////////////////////////////////////////////
 

	
 
        // Mask off fields
 
        chunk = engdata / 18 / 10 / 10 / 19;  
 

	
 
        // Encode to grid locator
 
        loc[0] = 'A' + chunk;
 

	
 
        // Subtract off previous portion 
 
        engdata -= (chunk * 18 * 10 * 10 * 19);  
 

	
 
        // Mask of fields
 
        chunk = engdata / 10 / 10 / 19;  
 

	
 
        // Encode to grid locator
0 comments (0 inline, 0 general)