Bsongis/adc rtc temperature (#6537)

RTC voltage + CPU temperature + RTC voltage alarm (thanks @schwabe)
This commit is contained in:
Bertrand Songis
2019-07-03 07:58:32 +02:00
committed by GitHub
parent 6ba4b84a45
commit 3a0960c1e3
49 changed files with 568 additions and 447 deletions
+23 -23
View File
@@ -20,7 +20,8 @@
#include "opentx.h"
uint16_t adcValues[NUM_ANALOGS + 1/*RTC*/] __DMA;
uint16_t adcValues[NUM_ANALOGS] __DMA;
uint16_t rtcBatteryVoltage;
#define ADC_CS_HIGH() (ADC_SPI_GPIO->BSRRL = ADC_SPI_PIN_CS)
#define ADC_CS_LOW() (ADC_SPI_GPIO->BSRRH = ADC_SPI_PIN_CS)
@@ -40,8 +41,6 @@ uint16_t adcValues[NUM_ANALOGS + 1/*RTC*/] __DMA;
#define RESETCMD 0x4000
#define MANUAL_MODE 0x1000 // manual mode channel 0
#define SAMPTIME 2 // sample time = 28 cycles
uint16_t SPIx_ReadWriteByte(uint16_t value)
{
while (SPI_I2S_GetFlagStatus(ADC_SPI, SPI_I2S_FLAG_TXE) == RESET);
@@ -113,22 +112,13 @@ void adcInit()
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(ADC_GPIO_MOUSE, &GPIO_InitStructure);
// RTC battery
ADC1->CR1 = ADC_CR1_SCAN;
ADC1->CR2 = ADC_CR2_ADON | ADC_CR2_DMA | ADC_CR2_DDS;
ADC1->SQR1 = (1 - 1) << 20;
ADC1->SQR3 = ADC_CHANNEL_RTC;
ADC1->SMPR1 = SAMPTIME + (SAMPTIME<<3) + (SAMPTIME<<6) + (SAMPTIME<<9) + (SAMPTIME<<12) + (SAMPTIME<<15) + (SAMPTIME<<18) + (SAMPTIME<<21) + (SAMPTIME<<24);
ADC1->SMPR2 = SAMPTIME + (SAMPTIME<<3) + (SAMPTIME<<6) + (SAMPTIME<<9) + (SAMPTIME<<12) + (SAMPTIME<<15) + (SAMPTIME<<18) + (SAMPTIME<<21) + (SAMPTIME<<24) + (SAMPTIME<<27);
// Track pad
ADC3->CR1 = ADC_CR1_SCAN;
ADC3->CR2 = ADC_CR2_ADON | ADC_CR2_DMA | ADC_CR2_DDS;
ADC3->SQR1 = (2 - 1) << 20;
ADC3->SQR2 = 0;
ADC3->SQR3 = ADC_CHANNEL_MOUSE1 + (ADC_CHANNEL_MOUSE2<<5);
ADC3->SMPR1 = 0;
ADC3->SMPR2 = (ADC_SAMPTIME<<(3*ADC_CHANNEL_MOUSE1)) + (ADC_SAMPTIME<<(3*ADC_CHANNEL_MOUSE2));
ADC3->SQR3 = (ADC_CHANNEL_MOUSE1 << 0) + (ADC_CHANNEL_MOUSE2 << 5);
ADC3->SMPR1 = (ADC_SAMPTIME << 0) + (ADC_SAMPTIME << 3) + (ADC_SAMPTIME << 6) + (ADC_SAMPTIME << 9) + (ADC_SAMPTIME << 12) + (ADC_SAMPTIME << 15) + (ADC_SAMPTIME << 18) + (ADC_SAMPTIME << 21) + (ADC_SAMPTIME << 24);
ADC3->SMPR2 = (ADC_SAMPTIME << 0) + (ADC_SAMPTIME << 3) + (ADC_SAMPTIME << 6) + (ADC_SAMPTIME << 9) + (ADC_SAMPTIME << 12) + (ADC_SAMPTIME << 15) + (ADC_SAMPTIME << 18) + (ADC_SAMPTIME << 21) + (ADC_SAMPTIME << 24) + (ADC_SAMPTIME << 27);
ADC->CCR = 0;
// Enable the DMA channel here, DMA2 stream 1, channel 2
@@ -137,6 +127,19 @@ void adcInit()
ADC_DMA_Stream->M0AR = CONVERT_PTR_UINT(&adcValues[MOUSE1]);
ADC_DMA_Stream->NDTR = 2;
ADC_DMA_Stream->FCR = DMA_SxFCR_DMDIS | DMA_SxFCR_FTH_0;
ADC1->CR1 = ADC_CR1_SCAN;
ADC1->CR2 = ADC_CR2_ADON | ADC_CR2_DMA | ADC_CR2_DDS;
ADC1->SQR1 = (1 - 1) << 20;
ADC1->SQR2 = 0;
ADC1->SQR3 = (ADC_Channel_Vbat << 0);
ADC1->SMPR1 = (ADC_SAMPTIME << 0) + (ADC_SAMPTIME << 3) + (ADC_SAMPTIME << 6) + (ADC_SAMPTIME << 9) + (ADC_SAMPTIME << 12) + (ADC_SAMPTIME << 15) + (ADC_SAMPTIME << 18) + (ADC_SAMPTIME << 21) + (ADC_SAMPTIME << 24);
ADC1->SMPR2 = (ADC_SAMPTIME << 0) + (ADC_SAMPTIME << 3) + (ADC_SAMPTIME << 6) + (ADC_SAMPTIME << 9) + (ADC_SAMPTIME << 12) + (ADC_SAMPTIME << 15) + (ADC_SAMPTIME << 18) + (ADC_SAMPTIME << 21) + (ADC_SAMPTIME << 24) + (ADC_SAMPTIME << 27);
}
uint16_t getRTCBatteryVoltage()
{
return rtcBatteryVoltage * 330 / 2048;
}
const uint16_t adcCommands[MOUSE1+2] =
@@ -206,13 +209,14 @@ uint32_t adcReadNextSPIChannel(uint8_t index)
void adcOnChipReadStart()
{
ADC_DMA_Stream->CR &= ~DMA_SxCR_EN; // Disable DMA
ADC1->SR &= ~(uint32_t)(ADC_SR_EOC | ADC_SR_STRT | ADC_SR_OVR);
ADC3->SR &= ~(uint32_t)(ADC_SR_EOC | ADC_SR_STRT | ADC_SR_OVR);
ADC_DMA->LIFCR = DMA_LIFCR_CTCIF0 | DMA_LIFCR_CHTIF0 | DMA_LIFCR_CTEIF0 | DMA_LIFCR_CDMEIF0 | DMA_LIFCR_CFEIF0; // Write ones to clear bits
ADC_DMA_Stream->M0AR = CONVERT_PTR_UINT(&adcValues[MOUSE1]);
ADC_DMA_Stream->NDTR = 2;
ADC_DMA_Stream->CR |= DMA_SxCR_EN; // Enable DMA
ADC3->CR2 |= (uint32_t)ADC_CR2_SWSTART;
ADC1->SR &= ~(uint32_t)(ADC_SR_EOC | ADC_SR_STRT | ADC_SR_OVR);
ADC1->CR2 |= (uint32_t)ADC_CR2_SWSTART;
}
@@ -256,14 +260,10 @@ void adcRead()
for (uint8_t x=0; x<NUM_ANALOGS-MOUSE1; x++) {
adcValues[MOUSE1+x] = temp[x] >> 2;
}
}
uint16_t getRTCBattVoltage()
{
ADC->CCR |= ADC_CCR_VBATE;
adcRead();
ADC->CCR &= ADC_CCR_VBATE;
return ADC1->DR * 330 / 2048;
if (ADC->CCR & ADC_CCR_VBATE) {
rtcBatteryVoltage = ADC1->DR;
}
}
#if !defined(SIMU)
-2
View File
@@ -182,8 +182,6 @@ void boardInit()
#endif
ledBlue();
vbattRTC = getRTCBattVoltage();
#endif
}
+11 -83
View File
@@ -23,63 +23,14 @@
#include "../definitions.h"
#include "../opentx_constants.h"
#include "cpu_id.h"
#if defined(__cplusplus) && !defined(SIMU)
extern "C" {
#endif
#if __clang__
// clang is very picky about the use of "register"
// Tell clang to ignore the warnings for the following files
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-register"
#endif
#if !defined(LUA_EXPORT_GENERATION)
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_rcc.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_syscfg.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_gpio.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_exti.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_spi.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_i2c.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_rtc.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_pwr.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_dma.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_usart.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_flash.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_sdio.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_dbgmcu.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_ltdc.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_fmc.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_tim.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_dma2d.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_adc.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/misc.h"
#endif
#if __clang__
// Restore warnings about registers
#pragma clang diagnostic pop
#endif
#include "usb_driver.h"
#if !defined(SIMU)
#include "usbd_cdc_core.h"
#include "usbd_msc_core.h"
#include "usbd_hid_core.h"
#include "usbd_usr.h"
#include "usbd_desc.h"
#include "usb_conf.h"
#include "usbd_conf.h"
#endif
#include "board_common.h"
#include "hal.h"
#if defined(__cplusplus) && !defined(SIMU)
}
#if !defined(LUA_EXPORT_GENERATION)
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_sdio.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_dma2d.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_ltdc.h"
#include "STM32F4xx_DSP_StdPeriph_Lib_V1.4.0/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_fmc.h"
#endif
#define FLASHSIZE 0x200000
@@ -114,18 +65,6 @@ void boardOff(void);
void init2MhzTimer();
void init5msTimer();
// Delays driver
#ifdef __cplusplus
extern "C" {
#endif
void delaysInit(void);
void delay_01us(uint16_t nb);
void delay_us(uint16_t nb);
void delay_ms(uint32_t ms);
#ifdef __cplusplus
}
#endif
// PCBREV driver
#define IS_HORUS_PROD() GPIO_ReadInputDataBit(PCBREV_GPIO, PCBREV_GPIO_PIN)
#if defined(SIMU) || defined(PCBX10)
@@ -393,18 +332,12 @@ enum Analogs {
POT1 = POT_FIRST,
POT2,
POT3,
POT_LAST = POT3,
SLIDER_FIRST,
SLIDER1 = SLIDER_FIRST,
SLIDER2,
#if defined(PCBX12S)
SLIDER_FRONT_LEFT = SLIDER_FIRST,
SLIDER_FRONT_RIGHT,
SLIDER_REAR_LEFT,
SLIDER_REAR_RIGHT,
#else
SLIDER_REAR_LEFT,
SLIDER_REAR_RIGHT,
#endif
SLIDER_LAST = SLIDER_FIRST + NUM_SLIDERS - 1,
TX_VOLTAGE,
#if defined(PCBX12S)
@@ -415,11 +348,11 @@ enum Analogs {
EXT1,
EXT2,
#endif
NUM_ANALOGS,
TX_RTC = NUM_ANALOGS
NUM_ANALOGS
};
#define POT_LAST (SLIDER_FIRST - 1)
#define SLIDER1 SLIDER_FRONT_LEFT
#define SLIDER2 SLIDER_FRONT_RIGHT
#define DEFAULT_SWITCH_CONFIG (SWITCH_TOGGLE << 14) + (SWITCH_3POS << 12) + (SWITCH_2POS << 10) + (SWITCH_3POS << 8) + (SWITCH_3POS << 6) + (SWITCH_3POS << 4) + (SWITCH_3POS << 2) + (SWITCH_3POS << 0)
#define DEFAULT_POTS_CONFIG (POT_WITH_DETENT << 4) + (POT_MULTIPOS_SWITCH << 2) + (POT_WITHOUT_DETENT << 0)
@@ -449,11 +382,7 @@ enum CalibratedAnalogs {
#define IS_POT(x) ((x)>=POT_FIRST && (x)<=POT_LAST)
#define IS_SLIDER(x) ((x)>=SLIDER_FIRST && (x)<=SLIDER_LAST)
extern uint16_t adcValues[NUM_ANALOGS + 1/*RTC*/];
void adcInit(void);
void adcRead(void);
uint16_t getRTCBattVoltage();
uint16_t getAnalogValue(uint8_t index);
extern uint16_t adcValues[NUM_ANALOGS];
#if defined(PCBX12S)
#define NUM_MOUSE_ANALOGS 2
@@ -483,7 +412,6 @@ extern volatile uint32_t pwm_interrupt_count;
#define BATTERY_MIN 85 // 8.5V
#define BATTERY_MAX 115 // 11.5V
#endif
uint16_t getBatteryVoltage(); // returns current battery voltage in 10mV steps
#if defined(__cplusplus) && !defined(SIMU)
extern "C" {
+2 -4
View File
@@ -214,7 +214,7 @@
#if defined(PCBX12S)
#define ADC_RCC_AHB1Periph (RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_DMA2)
#define ADC_RCC_APB1Periph 0
#define ADC_RCC_APB2Periph (RCC_APB2Periph_SPI4 | RCC_APB2Periph_ADC3)
#define ADC_RCC_APB2Periph (RCC_APB2Periph_SPI4 | RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC3)
#define ADC_SPI SPI4
#define ADC_GPIO_AF GPIO_AF_SPI4
#define ADC_SPI_GPIO GPIOE
@@ -233,11 +233,10 @@
#define ADC_DMA DMA2
#define ADC_DMA_Stream DMA2_Stream0
#define ADC_SAMPTIME 3
#define ADC_CHANNEL_RTC 0 // TODO later ... ADC1_IN18
#elif defined(PCBX10)
#define ADC_RCC_AHB1Periph (RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_DMA2)
#define ADC_RCC_APB1Periph (RCC_APB1Periph_TIM5)
#define ADC_RCC_APB2Periph (RCC_APB2Periph_ADC3)
#define ADC_RCC_APB2Periph (RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC3)
#define ADC_GPIO_PIN_STICK_LH GPIO_Pin_0 // PA.00
#define ADC_GPIO_PIN_STICK_LV GPIO_Pin_1 // PA.01
#define ADC_GPIO_PIN_STICK_RH GPIO_Pin_2 // PA.02
@@ -271,7 +270,6 @@
#define ADC_CHANNEL_BATT ADC_Channel_5 // ADC3_IN5
#define ADC_CHANNEL_EXT1 ADC_Channel_6 // ADC3_IN6
#define ADC_CHANNEL_EXT2 ADC_Channel_7 // ADC3_IN7
#define ADC_CHANNEL_RTC 0 // TODO later ... ADC1_IN18
#define ADC_MAIN ADC3
#define ADC_SAMPTIME 3
#define ADC_DMA DMA2