Changeset - 67d1f9d02048
[Not reviewed]
default
0 5 4
Ethan Zonca (ethanzonca) - 9 years ago 2017-01-04 09:47:53
e@ethanzonca.com
Working pressure sensor and adc vbatt reading
9 files changed with 270 insertions and 2 deletions:
0 comments (0 inline, 0 general)
.cproject
Show inline comments
 
@@ -17,7 +17,7 @@
 
				<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.debug" cleanCommand="${cross_rm} -rf" description="" id="ilg.gnuarmeclipse.managedbuild.cross.config.elf.debug.1412804961" name="Debug" parent="ilg.gnuarmeclipse.managedbuild.cross.config.elf.debug">
 
					<folderInfo id="ilg.gnuarmeclipse.managedbuild.cross.config.elf.debug.1412804961." name="/" resourcePath="">
 
						<toolChain id="ilg.gnuarmeclipse.managedbuild.cross.toolchain.elf.debug.1252985156" name="Cross ARM GCC" superClass="ilg.gnuarmeclipse.managedbuild.cross.toolchain.elf.debug">
 
							<option id="ilg.gnuarmeclipse.managedbuild.cross.option.optimization.level.648941311" name="Optimization Level" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.optimization.level" value="ilg.gnuarmeclipse.managedbuild.cross.option.optimization.level.debug" valueType="enumerated"/>
 
							<option id="ilg.gnuarmeclipse.managedbuild.cross.option.optimization.level.648941311" name="Optimization Level" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.optimization.level" value="ilg.gnuarmeclipse.managedbuild.cross.option.optimization.level.size" valueType="enumerated"/>
 
							<option id="ilg.gnuarmeclipse.managedbuild.cross.option.optimization.messagelength.304740068" name="Message length (-fmessage-length=0)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.optimization.messagelength" value="true" valueType="boolean"/>
 
							<option id="ilg.gnuarmeclipse.managedbuild.cross.option.optimization.signedchar.169298544" name="'char' is signed (-fsigned-char)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.optimization.signedchar" value="true" valueType="boolean"/>
 
							<option id="ilg.gnuarmeclipse.managedbuild.cross.option.optimization.functionsections.706305735" name="Function sections (-ffunction-sections)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.optimization.functionsections" value="true" valueType="boolean"/>
 
@@ -65,6 +65,7 @@
 
									<listOptionValue builtIn="false" value="HSI48_VALUE=48000000"/>
 
									<listOptionValue builtIn="false" value="HSE_VALUE=8000000"/>
 
								</option>
 
								<option id="ilg.gnuarmeclipse.managedbuild.cross.option.assembler.other.1270173496" name="Other assembler flags" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.assembler.other" value="" valueType="string"/>
 
								<inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.assembler.input.959337448" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.assembler.input"/>
 
							</tool>
 
							<tool id="ilg.gnuarmeclipse.managedbuild.cross.tool.c.compiler.772558380" name="Cross ARM C Compiler" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.c.compiler">
 
@@ -80,6 +81,7 @@
 
									<listOptionValue builtIn="false" value="HSI48_VALUE=48000000"/>
 
									<listOptionValue builtIn="false" value="HSE_VALUE=8000000"/>
 
								</option>
 
								<option id="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.other.600165372" name="Other compiler flags" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.c.compiler.other" useByScannerDiscovery="true" value="" valueType="string"/>
 
								<inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.c.compiler.input.1750560740" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.c.compiler.input"/>
 
							</tool>
 
							<tool id="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.compiler.1784562806" name="Cross ARM C++ Compiler" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.compiler">
 
@@ -99,6 +101,7 @@
 
									<listOptionValue builtIn="false" value="HSI48_VALUE=48000000"/>
 
									<listOptionValue builtIn="false" value="HSE_VALUE=8000000"/>
 
								</option>
 
								<option id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.other.1587293044" name="Other compiler flags" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.compiler.other" useByScannerDiscovery="true" value="" valueType="string"/>
 
								<inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.compiler.input.723145048" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.compiler.input"/>
 
							</tool>
 
							<tool id="ilg.gnuarmeclipse.managedbuild.cross.tool.c.linker.813050281" name="Cross ARM C Linker" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.c.linker">
 
@@ -126,6 +129,7 @@
 
								</option>
 
								<option id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.linker.nostart.49019800" name="Do not use standard start files (-nostartfiles)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.linker.nostart" value="false" valueType="boolean"/>
 
								<option id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.linker.usenewlibnano.1900135381" name="Use newlib-nano (--specs=nano.specs)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.linker.usenewlibnano" value="true" valueType="boolean"/>
 
								<option id="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.linker.other.39771167" name="Other linker flags" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.cpp.linker.other" value="" valueType="string"/>
 
								<inputType id="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.linker.input.388623763" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.linker.input">
 
									<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
 
									<additionalInput kind="additionalinput" paths="$(LIBS)"/>
 
@@ -186,6 +190,7 @@
 
							</tool>
 
							<tool id="ilg.gnuarmeclipse.managedbuild.cross.tool.c.linker.1757809268" name="Cross ARM C Linker" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.c.linker.813050281">
 
								<option id="ilg.gnuarmeclipse.managedbuild.cross.option.c.linker.usenewlibnano.1870722667" name="Use newlib-nano (--specs=nano.specs)" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.c.linker.usenewlibnano" value="true" valueType="boolean"/>
 
								<option id="ilg.gnuarmeclipse.managedbuild.cross.option.c.linker.other.800326275" name="Other linker flags" superClass="ilg.gnuarmeclipse.managedbuild.cross.option.c.linker.other" value="" valueType="string"/>
 
							</tool>
 
							<tool id="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.linker.119353502" name="Cross ARM C++ Linker" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.cpp.linker.1583473051"/>
 
							<tool id="ilg.gnuarmeclipse.managedbuild.cross.tool.archiver.1555481861" name="Cross ARM GNU Archiver" superClass="ilg.gnuarmeclipse.managedbuild.cross.tool.archiver.236341817"/>
Include/pressure.h
Show inline comments
 
new file 100644
 
#ifndef PRESSURE_H
 
#define PRESSURE_H
 
 
#include "stm32f0xx_hal.h"
 
 
 
#define PIN_SENSORS_SDA GPIO_PIN_7
 
#define PIN_SENSORS_SCL GPIO_PIN_6
 
#define PORT_SENSORS GPIOB
 
 
#define PRESSURE_ADDRESS 0b10111000
 
#define PRESSURE_PRESS_REGH 0x2A
 
#define PRESSURE_PRESS_REGL 0x29
 
#define PRESSURE_PRESS_REGXL 0x28
 
#define PRESSURE_TEMP_REGH 0x2C
 
#define PRESSURE_TEMP_REGL 0x2B
 
#define PRESSURE_AUTOINC 0b10000000
 
 
#define PRESSURE_CTRL1_1HZ 	  0b00010000
 
#define PRESSURE_CTRL1_7HZ 	  0b00100000
 
#define PRESSURE_CTRL1_12_5HZ 0b00110000
 
#define PRESSURE_CTRL1_25HZ   0b01000000
 
 
#define PRESSURE_CTRL1_PWRUP 0b10000000
 
 
void pressure_init(void);
 
void pressure_read(void);
 
void pressure_updatevalues(void);
 
 
int32_t pressure_gettemp(void);
 
int32_t pressure_getpressure(void);
 
 
I2C_HandleTypeDef* pressure_get_i2c_handle(void);
 
#endif
 
Include/system/adc.h
Show inline comments
 
new file 100644
 
#ifndef SYSTEM_ADC_H_
 
#define SYSTEM_ADC_H_
 
 
void adc_init(void);
 
DMA_HandleTypeDef* adc__hdma_gethandle(void);
 
uint8_t adc_get_vbatt(void);
 
 
 
 
#endif /* SYSTEM_ADC_H_ */
Include/system/stm32f0xx_hal_conf.h
Show inline comments
 
@@ -10,7 +10,7 @@
 
  * @brief This is the list of modules to be used in the HAL driver 
 
  */
 
#define HAL_MODULE_ENABLED  
 
//#define HAL_ADC_MODULE_ENABLED
 
#define HAL_ADC_MODULE_ENABLED
 
//#define HAL_CAN_MODULE_ENABLED   
 
//#define HAL_CEC_MODULE_ENABLED   
 
//#define HAL_COMP_MODULE_ENABLED   
Libraries/aprs/aprs.c
Show inline comments
 
@@ -30,6 +30,7 @@
 
//#include "gps.h"
 
//#include "adc.h"
 
#include "ax25.h"
 
#include "system/adc.h"
 

	
 

	
 
int32_t meters_to_feet(int32_t m)
 
@@ -90,6 +91,9 @@ void aprs_send(void)
 
  snprintf(tmpBuffer, 128, "%u,", gps_getdata()->pdop);
 
  ax25_send_string(tmpBuffer);
 

	
 
  // Vbatt
 
  snprintf(tmpBuffer, 128, "%u,", adc_get_vbatt());
 
  ax25_send_string(tmpBuffer);
 

	
 
  ax25_send_footer();
 
  ax25_flush_frame();
Source/main.c
Show inline comments
 
@@ -9,6 +9,7 @@
 
#include "system/sysclk.h"
 
#include "system/watchdog.h"
 
#include "system/uart.h"
 
#include "system/adc.h"
 
#include "stm32f0xx_hal.h"
 
#include "si446x/si446x.h"
 
#include "aprs/aprs.h"
 
@@ -23,6 +24,7 @@ int main(void)
 
  sysclock_init();
 
  gpio_init();
 
 
  adc_init();
 
 
  afsk_init();
 
  si446x_init();
Source/pressure.c
Show inline comments
 
new file 100644
 
//
 
// Sensors: Reads pressure and e-compass sensors over I2C and stores readings in queryable internal data structures
 
//
 
 
#include "pressure.h"
 
#include "stm32f0xx_hal.h"
 
 
 
#include "config.h"
 
 
 
// Static variables
 
static I2C_HandleTypeDef hi2c1;
 
 
static int32_t pressure_hPa = 0;
 
static int32_t temp_celsius = 0;
 
 
static uint8_t sensors_pressurebuf[8]; // Buffer for i2c rx pressure data
 
static uint8_t sensors_pressure_freshdata = 0; // If pressure sensor has fresh data
 
 
 
// Initialize pressure sensor and E-compass
 
void pressure_init(void)
 
{
 
	GPIO_InitTypeDef GPIO_InitStruct;
 
 
	// I2C Pins
 
    GPIO_InitStruct.Pin = PIN_SENSORS_SCL|PIN_SENSORS_SDA;
 
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
 
    GPIO_InitStruct.Pull = GPIO_PULLUP;
 
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
 
    GPIO_InitStruct.Alternate = GPIO_AF1_I2C1;
 
    HAL_GPIO_Init(PORT_SENSORS, &GPIO_InitStruct);
 
 
    // Initialize I2C peripheral
 
    __I2C1_CLK_ENABLE();
 
    hi2c1.Instance = I2C1;
 
    hi2c1.Init.Timing = 0x00108EFB;
 
    hi2c1.Init.OwnAddress1 = 0x0;
 
    hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
 
    hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
 
    hi2c1.Init.OwnAddress2 = 0;
 
    hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
 
    hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
 
    hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
 
    volatile HAL_StatusTypeDef res = HAL_I2C_Init(&hi2c1);
 
 
    // Configure Analog filter
 
    res =  HAL_I2CEx_AnalogFilter_Config(&hi2c1, I2C_ANALOGFILTER_ENABLED);
 
 
	// Enable I2C interrupt
 
    HAL_NVIC_SetPriority(I2C1_IRQn, 7, 4);
 
    HAL_NVIC_EnableIRQ(I2C1_IRQn);
 
 
    // Blocking initialization of sensors
 
 
	// Power on the pressure sensor and configure //////////////////////////////////////////////////
 
	uint8_t temp[2];
 
	temp[0] = PRESSURE_CTRL1_PWRUP | PRESSURE_CTRL1_7HZ;
 
	HAL_Delay(10);
 
	res = HAL_I2C_Mem_Write(&hi2c1, PRESSURE_ADDRESS, 0x20, 1, temp, 1, 500);
 
}
 
 
 
// Initiate interrupt-based read of engineering sensors
 
void pressure_read(void)
 
{
 
	// Start reading pressure sensor
 
	HAL_Delay(10);
 
	volatile  HAL_StatusTypeDef res = HAL_I2C_Mem_Read(&hi2c1, PRESSURE_ADDRESS, PRESSURE_PRESS_REGXL | PRESSURE_AUTOINC, 1, sensors_pressurebuf, 5, 500);
 
 
	// Consume fresh data flag
 
	sensors_pressure_freshdata = 0;
 
 
	// Convert received bytes to pressure in hPa
 
	pressure_hPa = sensors_pressurebuf[0]<<8 | sensors_pressurebuf[1]<<16 | sensors_pressurebuf[2]<<24;
 
	pressure_hPa >>= 8; // Sign extend
 
	pressure_hPa /= 4096; // convert to hPa
 
 
	// Convert received bytes to temperature in Celsius
 
	int16_t temp_out = (sensors_pressurebuf[3] | sensors_pressurebuf[4]<<8);
 
	if(!temp_out)
 
		temp_celsius = 42;
 
	else
 
		temp_celsius = 42.5 + temp_out / 480.0;
 
 
}
 
 
 
// Get current temperature in Celsius
 
int32_t pressure_gettemp(void)
 
{
 
	return temp_celsius;
 
}
 
 
 
// Get current pressure in hPa
 
int32_t pressure_getpressure(void)
 
{
 
	return pressure_hPa;
 
}
 
 
 
 
inline I2C_HandleTypeDef* pressure_get_i2c_handle(void)
 
{
 
	return &hi2c1;
 
}
 
Source/system/adc.c
Show inline comments
 
new file 100644
 
/*
 
 * FeatherHAB
 
 *
 
 * This file is part of FeatherHAB.
 
 *
 
 * FeatherHab is free software: you can redistribute it and/or modify
 
 * it under the terms of the GNU Affero General Public License as published by
 
 * the Free Software Foundation, either version 3 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * FeatherHab is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU Affero General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU Affero General Public License
 
 * along with FeatherHAB. If not, see <http://www.gnu.org/licenses/>.
 
 *
 
 * Ethan Zonca
 
 *
 
 */
 
 
#include "stm32f0xx_hal.h"
 
#include "system/adc.h"
 
 
ADC_HandleTypeDef hadc;
 
DMA_HandleTypeDef hdma_adc;
 
 
#define ADC_BUF_LEN 1
 
uint16_t adc_buffer[ADC_BUF_LEN];
 
 
 
void adc_init(void)
 
{
 
 
    __ADC1_CLK_ENABLE();
 
 
	GPIO_InitTypeDef GPIO_InitStruct;
 
    GPIO_InitStruct.Pin = GPIO_PIN_6;
 
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 
    GPIO_InitStruct.Pull = GPIO_NOPULL;
 
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
 
 
	ADC_ChannelConfTypeDef sConfig;
 
	hadc.Instance = ADC1;
 
	hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC;
 
	hadc.Init.Resolution = ADC_RESOLUTION12b;
 
	hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
 
	hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
 
	hadc.Init.EOCSelection = EOC_SEQ_CONV;
 
	hadc.Init.LowPowerAutoWait = DISABLE;
 
	hadc.Init.LowPowerAutoPowerOff = DISABLE;
 
	hadc.Init.ContinuousConvMode = ENABLE;
 
	hadc.Init.DiscontinuousConvMode = DISABLE;
 
	hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
 
	hadc.Init.DMAContinuousRequests = ENABLE;
 
	hadc.Init.Overrun = OVR_DATA_OVERWRITTEN;
 
	HAL_ADC_Init(&hadc);
 
 
	sConfig.Channel = ADC_CHANNEL_6;
 
	sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
 
	sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
 
	HAL_ADC_ConfigChannel(&hadc, &sConfig);
 
 
 
	__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);
 
 
 
}
 
 
uint8_t adc_get_vbatt(void)
 
{
 
	return adc_buffer[0] / 62.5; // tenths of volts ish
 
}
 
 
DMA_HandleTypeDef* adc__hdma_gethandle(void)
 
{
 
	return &hdma_adc;
 
}
Source/system/interrupts.c
Show inline comments
 
@@ -9,6 +9,7 @@
 
#include "system/gpio.h"
 
#include "aprs/afsk.h"
 
#include "pressure.h"
 
#include "system/adc.h"
 
 
// Systick
 
void SysTick_Handler(void)
 
@@ -38,3 +39,8 @@ void I2C1_IRQHandler(void)
 
		HAL_I2C_EV_IRQHandler(pressure_get_i2c_handle());
 
	}
 
}
 
 
void DMA1_Channel1_IRQHandler(void)
 
{
 
  HAL_DMA_IRQHandler(adc__hdma_gethandle());
 
}
0 comments (0 inline, 0 general)