refact: ADC, switches and keys

Add "hardware_defs" target to dump hardware defines from hal.h per target

Fixed hal.h definitions for switches & ADC inputs

Tools to generate hardware definitions from current hal.h

generator: add support for EXTx and analog switches

WiP

Add generated header to hardware_defs target

Add support for ADC input direction and index per GPIO port

Incredible: it compiles

Removed StdPeriph ADC driver

Works on QX7

Works on TX16S (VBat missing)

Switch driver using generated switches for stm32 targets

Switch driver with generated switches for simu

Use switch names from switch driver

Fixed single conversion handling

Fixed horus VBat reading

Added switch name

Fix warnings about bitfield offsets and GCC 4.4

This is caused by bitfields that cross type boundaries. In this particular case, it happens as the bitfield is not defined the same in `rtc_backup.cpp`.

Switches has already been moved to generic driver

Jitter measurement for each sample, not just for oversampled values

Moved periph clock init to the ADC driver

ADC & GPIOs.

Add switchInit() to init pins

Fix NV14 CMake

Fix taranis target

Fix NV14 (again)

Model audio files: use new interface for switch index

Fix X9-lite

Fix simu headers

Fix getSwitchName()

Add support for ADC switches

Fix switch warning YAML

Test only switches which are defined

Conversions tests for QX7 only on QX7

Fix X12S

Fix function switch names

Enable ADC periph clock before initialising channels

NV14: offset inputs for the sticks & fix switches

WiP: get rid of VSRCRAW and some more

Simu shall use normal ADC driver

fix x7

fix nv14

Fix 9x nav

fix 212x64

fix xlite

fix x9e

fix simu lib

fix unit tests

fix x7 tests

PWM stick values should be written directly into adcValues

... just like FlySky sticks.

fix x12s

fix nv14

Add some support for SPI ADC (only unit tests)

Remove `jq` usage

fix generate_yaml.py for newer libclang

fix pot config

fix stick names

Some YAML generator improvements

Max 16 Pots on COLORLCD

Fix some source names

Use 10 bits for switches to prevent overflow

Removed horus constants for switches & ADC inputs

Fix unit tests

Fix curve edit

Fix libsimulator

Fix function switches (tpro)

Refine ADC & analog API

Moved hardware definitions scripts and templates into own dir

cleanup API refine

Fix unit tests

Mixer: apply calibration only on required inputs

Place ADC switches after all other ADC inputs

Fix X12S

Remove useless TR_POTS_VSRCRAW, TR_SW_VSRCRAW, and TR_VSRCRAW

Cleanup redundant generated YAML parsers

Fixes Boxer rebase

Add boxer to generate-hw-defs.sh

Obsolete LUA exports

Add boxer ADC_DIRECTION

Split generate_hw_defs.py into multiple files

Some cleanup (+ LUA switches)

Fix AXIS definitions

Fixed translation strings declarations

Some rebase fixes

Remove some useless StdPeriph drivers

fix: set pin mode to reset state on usart deinit

fix: use canonical names for calibration data

fix: ADC driver does not care about unconfigured inputs

fix X12S ADC

Add support for ADC conversion chaining

Better X12S ADC implem

Moved X12S ADC driver to HAL/LL

Improve ADC driver separation

Fixed taranis & nv14

Moved PWM gimbals to HAL/LL

Removed useless TR_EXTRA_VSRCRAW

Fixed include guards

Small fixes

Renamed control inputs

Moved internal and external module pulse driver def to boards

Move more things to the generic_stm32 board

Remove STR_VMIXTRIMS

Added analog labels

Generate main control names

Misuse labels / short labels for main controls

Backward compatibility: sliders and legacy names

New keys driver

Let's get rid of StdPeriph headers in simu

Add jinja2 to CI workflows

Use `grep` instead of `sed`

chore: add python jinja2 oto msys setup script

Removed more static definitions and use dynamic ones instead

Fixes X9E

Fix x7 nav too

212 switch display

Fix default sliders

Removed NUM_STICKS

212 switch display working nicely

Surface radio 'stick' replacements

fix: switch, trim & rotary pin init

Remove old key drivers

Removed VKEYS

Removed ETX_FOURCC

Fixed SURFACE_NAME

Removed TR_VSWITCHES & TR_TRIMS_SWITCHES

Fix xpot switches

Store calibrated analogs in physical order

Use getSourceString in drawSource

Implement missing YAML read/write

Fix isSourceAvailableInInputs()

Fix isSourceAvailable()

Some surface fixes

Fix CLI debug

Ported colour UI of analog diagnostics to new ADC API

Small fixes

Better input mapping

Minor tpro fixes

Fix trims available

Remove TX mode choice on surface radio

Rename surface controls

Channel order alignement

Fix throttle warning

Fix checkTrims()

Fix pot config casting issue

Fix IS_SWITCH_FS()

Fixed input mapping

Fix fswitch auto-switch

Fixed storing / reading function switches in YAML

Fix enableVBatBridge() / disableVBatBridge()

Fix interesting mode conversion

Fix sources issues

Fix special funnction list

Proper function switch handling

128 GUI improvements

Fix compilation

Boxer cosmetics

128 hardware screen cosmetic

Fix TX12 switches

More switches fixes

Simplify CPU type handling

Fix flash size

Fixed page-up / page-down in libopenui
This commit is contained in:
Raphael Coeffic
2023-06-25 14:32:39 +02:00
committed by Malte Langermann
parent b6dd3a0ef8
commit 2b100e0eb5
323 changed files with 12812 additions and 22301 deletions
+1 -1
View File
@@ -301,7 +301,7 @@ set(SRC
set(FIRMWARE_TARGET_SRC
${FIRMWARE_TARGET_SRC}
keys_driver.cpp
# keys_driver.cpp
diskio.cpp
# telemetry_driver.cpp
)
@@ -41,6 +41,10 @@ set(FIRMWARE_TARGET_SRC ${FIRMWARE_TARGET_SRC}
../common/arm/stm32/stm32_serial_driver.cpp
../common/arm/stm32/stm32_softserial_driver.cpp
../common/arm/stm32/module_timer_driver.cpp
../common/arm/stm32/stm32_switch_driver.cpp
../common/arm/stm32/stm32_adc.cpp
../common/arm/stm32/stm32_gpio_driver.cpp
../common/arm/stm32/mixer_scheduler_driver.cpp
)
if(IMU)
@@ -0,0 +1,173 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*/
#include "ads79xx.h"
#include "delays_driver.h"
#include "stm32_hal_ll.h"
#define RESETCMD 0x4000
#define MANUAL_MODE 0x1000 // manual mode channel 0
#define MANUAL_MODE_CHANNEL(x) (MANUAL_MODE | ((x) << 7))
static uint16_t ads79xx_rw(SPI_TypeDef* SPIx, uint16_t value)
{
while (!LL_SPI_IsActiveFlag_TXE(SPIx));
LL_SPI_TransmitData16(SPIx, value);
while (!LL_SPI_IsActiveFlag_RXNE(SPIx));
return LL_SPI_ReceiveData16(SPIx);
}
void ads79xx_init(const stm32_spi_adc_t* adc)
{
LL_GPIO_InitTypeDef pinInit;
LL_GPIO_StructInit(&pinInit);
pinInit.Pin = adc->GPIO_Pins;
pinInit.Mode = LL_GPIO_MODE_ALTERNATE;
pinInit.Alternate = adc->GPIO_AF;
LL_GPIO_Init(adc->GPIOx, &pinInit);
pinInit.Pin = adc->GPIO_CS;
pinInit.Mode = LL_GPIO_MODE_OUTPUT;
pinInit.Alternate = LL_GPIO_AF_0;
LL_GPIO_Init(adc->GPIOx, &pinInit);
auto SPIx = adc->SPIx;
LL_SPI_DeInit(SPIx);
LL_SPI_InitTypeDef spiInit;
LL_SPI_StructInit(&spiInit);
spiInit.TransferDirection = LL_SPI_FULL_DUPLEX;
spiInit.Mode = LL_SPI_MODE_MASTER;
spiInit.DataWidth = LL_SPI_DATAWIDTH_16BIT;
spiInit.NSS = LL_SPI_NSS_SOFT;
spiInit.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV8;
LL_SPI_Init(SPIx, &spiInit);
LL_SPI_Enable(SPIx);
LL_SPI_DisableIT_TXE(SPIx);
LL_SPI_DisableIT_RXNE(SPIx);
_SPI_ADC_CS_HIGH(adc);
delay_01us(1);
_SPI_ADC_CS_LOW(adc);
ads79xx_rw(SPIx, RESETCMD);
_SPI_ADC_CS_HIGH(adc);
delay_01us(1);
_SPI_ADC_CS_LOW(adc);
ads79xx_rw(SPIx, MANUAL_MODE);
_SPI_ADC_CS_HIGH(adc);
}
static void ads79xx_dummy_read(const stm32_spi_adc_t* adc, uint16_t start_channel)
{
// A dummy command to get things started
// (because the sampled data is lagging behind for two command cycles)
_SPI_ADC_CS_LOW(adc);
delay_01us(1);
ads79xx_rw(adc->SPIx, MANUAL_MODE_CHANNEL(start_channel));
_SPI_ADC_CS_HIGH(adc);
delay_01us(1);
}
static uint32_t ads79xx_read_next_channel(const stm32_spi_adc_t* adc,
uint8_t index,
const stm32_adc_input_t* inputs,
const uint8_t* chan, uint8_t nconv)
{
uint32_t result = 0;
// This delay is to allow charging of ADC input capacitor
// after the MUX changes from one channel to the other.
// It was determined experimentally. Biggest problem seems to be
// the cross-talk between A4:S1 and A5:MULTIPOS. Changing S1 for one extreme
// to the other resulted in A5 change of:
//
// delay value A5 change Time needed for adcRead()
// 1 16 0.154ms - 0.156ms
// 38 5 0.197ms - 0.199ms
// 62 0 0.225ms - 0.243ms
delay_01us(40);
for (uint8_t i = 0; i < 4; i++) {
_SPI_ADC_CS_LOW(adc);
delay_01us(1);
// command is changed to the next index for the last two readings
// (because the sampled data is lagging behind for two command cycles)
uint8_t chan_idx = (i > 1 ? index + 1 : index) % nconv;
auto input_idx = chan[chan_idx];
auto spi_chan = inputs[input_idx].ADC_Channel;
uint16_t val = (0x0fff & ads79xx_rw(adc->SPIx, MANUAL_MODE_CHANNEL(spi_chan)));
#if defined(JITTER_MEASURE)
if (JITTER_MEASURE_ACTIVE()) {
rawJitter[index].measure(val);
}
#endif
_SPI_ADC_CS_HIGH(adc);
delay_01us(1);
result += val;
}
return result >> 2;
}
bool ads79xx_adc_start_read(const stm32_spi_adc_t* adc, const stm32_adc_input_t* inputs)
{
auto start_channel = inputs[adc->channels[0]].ADC_Channel;
ads79xx_dummy_read(adc, start_channel);
ads79xx_dummy_read(adc, start_channel);
return true;
}
void ads79xx_adc_wait_completion(const stm32_spi_adc_t* adc, const stm32_adc_input_t* inputs)
{
auto spi_channels = adc->n_channels;
auto chans = adc->channels;
// Fetch buffer from generic ADC driver
auto adcValues = getAnalogValues();
// At this point, the first conversion has been started
// in ads79xx_adc_start_read()
// for each SPI channel
for (uint32_t i = 0; i < spi_channels; i++) {
// read SPI channel
auto input_channel = chans[i];
auto adc_value = ads79xx_read_next_channel(adc, i, inputs, chans, spi_channels);
if (inputs[input_channel].inverted)
adcValues[input_channel] = ADC_INVERT_VALUE(adc_value);
else
adcValues[input_channel] = adc_value;
}
}
@@ -0,0 +1,28 @@
/*
* Copyright (C) EdgeTx
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*/
#include "stm32_spi_adc.h"
#include "stm32_adc.h"
void ads79xx_init(const stm32_spi_adc_t* adc);
bool ads79xx_adc_start_read(const stm32_spi_adc_t* adc, const stm32_adc_input_t* inputs);
void ads79xx_adc_wait_completion(const stm32_spi_adc_t* adc, const stm32_adc_input_t* inputs);
@@ -25,16 +25,7 @@
#include <inttypes.h>
#include "cpu_id.h"
#if defined(LUA_EXPORT_GENERATION)
// no includes
#else
#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(SIMU) && !defined(BACKUP)
#if defined(STM32F4)
#include "stm32f4xx.h"
@@ -43,7 +34,6 @@
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_exti.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_adc.h"
#include "stm32f4xx_spi.h"
#include "stm32f4xx_pwr.h"
#include "stm32f4xx_dma.h"
@@ -57,7 +47,6 @@
#include "stm32f2xx_gpio.h"
#include "stm32f2xx_exti.h"
#include "stm32f2xx_tim.h"
#include "stm32f2xx_adc.h"
#include "stm32f2xx_spi.h"
#include "stm32f2xx_pwr.h"
#include "stm32f2xx_dma.h"
@@ -67,11 +56,6 @@
#include "dwt.h" // the old ST library that we use does not define DWT register for STM32F2xx
#endif
#if __clang__
// Restore warnings about registers
#pragma clang diagnostic pop
#endif
#endif
#include "usb_driver.h"
@@ -80,25 +64,6 @@
#include "../simu/simpgmspace.h"
#endif
uint16_t getBatteryVoltage();
// STM32 uses a 25K+25K voltage divider bridge to measure the battery voltage
// Measuring VBAT puts considerable drain (22 µA) on the battery instead of normal drain (~10 nA)
static inline void enableVBatBridge()
{
ADC->CCR |= ADC_CCR_VBATE;
}
static inline void disableVBatBridge()
{
ADC->CCR &= ~ADC_CCR_VBATE;
}
static inline bool isVBatBridgeEnabled()
{
return ADC->CCR & ADC_CCR_VBATE;
}
// Delays driver
#define SYSTEM_TICKS_1MS ((CPU_FREQ + 500) / 1000)
#define SYSTEM_TICKS_1US ((CPU_FREQ + 500000) / 1000000)
@@ -110,10 +75,10 @@ extern "C" {
static inline uint32_t ticksNow()
{
#if defined(SIMU)
return 0;
#else
#if !defined(SIMU) && !defined(BACKUP)
return DWT->CYCCNT;
#else
return 0;
#endif
}
@@ -123,35 +88,4 @@ static inline uint32_t ticksNow()
#include "delays_driver.h"
#define INIT_KEYS_PINS(GPIO) \
GPIO_InitStructure.GPIO_Pin = KEYS_ ## GPIO ## _PINS; \
GPIO_Init(GPIO, &GPIO_InitStructure)
#define SET_KEYS_PINS_HIGH(GPIO) \
GPIO_InitStructure.GPIO_Pin = KEYS_ ## GPIO ## _PINS; \
GPIO_Init(GPIO, &GPIO_InitStructure); \
GPIO_SetBits(GPIO, KEYS_ ## GPIO ## _PINS)
#if defined(ROTARY_ENCODER_NAVIGATION)
typedef int32_t rotenc_t;
extern volatile rotenc_t rotencValue;
#define IS_ROTARY_ENCODER_NAVIGATION_ENABLE() true
#define ROTARY_ENCODER_NAVIGATION_VALUE rotencValue
#define ROTENC_LOWSPEED 1
#define ROTENC_MIDSPEED 5
#define ROTENC_HIGHSPEED 50
#define ROTENC_DELAY_MIDSPEED 32
#define ROTENC_DELAY_HIGHSPEED 16
#elif (defined(RADIO_T8) || defined(RADIO_COMMANDO8)) && defined(__cplusplus)
constexpr uint8_t rotencSpeed = 1;
#endif
#define ROTARY_ENCODER_GRANULARITY (2)
#if defined(PWR_BUTTON_PRESS)
#define pwrOffPressed() pwrPressed()
#else
#define pwrOffPressed() (!pwrPressed())
#endif
#endif
@@ -22,7 +22,7 @@ set(BOOTLOADER_SRC
../../../../../${FATFS_DIR}/ffunicode.c
../../../../../targets/${TARGET_DIR}/${LCD_DRIVER}
../../../../../targets/${TARGET_DIR}/backlight_driver.cpp
../../../../../targets/${TARGET_DIR}/keys_driver.cpp
#../../../../../targets/${TARGET_DIR}/keys_driver.cpp
../../../../../targets/${TARGET_DIR}/diskio.cpp
../../../../../targets/${TARGET_DIR}/board.cpp
../pwr_driver.cpp
@@ -37,6 +37,7 @@ set(BOOTLOADER_SRC
../stm32_exti_driver.cpp
../stm32_serial_driver.cpp
../stm32_usart_driver.cpp
../stm32_gpio_driver.cpp
init.c
boot.cpp
bin_files.cpp
@@ -72,9 +73,9 @@ if(PCB STREQUAL X10 OR PCB STREQUAL X12S OR PCB STREQUAL NV14)
../../../../../fonts/lvgl/lv_font_roboto_bl_16.c
../../../../../thirdparty/libopenui/src/bitmapbuffer.cpp
../../../../../thirdparty/libopenui/thirdparty/lz4/lz4.c
../../../../../targets/${TARGET_DIR}/sdram_driver.c
../../../../../targets/common/arm/stm32/sdio_sd.c
../../../../../targets/common/arm/stm32/rtc_driver.cpp
../../../../../targets/${TARGET_DIR}/sdram_driver.c
../../../../../targets/${TARGET_DIR}/haptic_driver.cpp
)
@@ -83,11 +84,18 @@ if(PCB STREQUAL X10 OR PCB STREQUAL X12S OR PCB STREQUAL NV14)
set(BOOTLOADER_SRC ${BOOTLOADER_SRC} ../../../../../${LVGL_FILE})
endforeach()
if(NOT PCB STREQUAL NV14)
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
../../../../../targets/${TARGET_DIR}/led_driver.cpp
)
endif()
if(USB_CHARGER)
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
../../../../../targets/${TARGET_DIR}/usb_charger_driver.cpp
../../../../../targets/${TARGET_DIR}/led_driver.cpp
)
endif()
@@ -19,6 +19,8 @@
* GNU General Public License for more details.
*/
#include "stm32_hal_ll.h"
#if defined(BLUETOOTH)
#include "bluetooth_driver.h"
#include "stm32_serial_driver.h"
@@ -29,9 +31,13 @@
#include "bin_files.h"
#include "dataconstants.h"
#include "lcd.h"
#include "keys.h"
// #include "keys.h"
#include "debug.h"
#include "watchdog_driver.h"
#include "hal/rotary_encoder.h"
#if defined(DEBUG_SEGGER_RTT)
#include "thirdparty/Segger_RTT/RTT/SEGGER_RTT.h"
#endif
@@ -67,9 +73,10 @@ const uint8_t bootloaderVersion[] __attribute__ ((section(".version"), used)) =
{'B', 'O', 'O', 'T', '1', '0'};
#endif
#if defined(DEBUG) && !defined(SIMU)
#define SOFTRESET_REQUEST 0xCAFEDEAD
volatile tmr10ms_t g_tmr10ms;
#endif
volatile uint8_t tenms = 1;
uint32_t firmwareSize;
uint32_t firmwareAddress = FIRMWARE_ADDRESS;
@@ -80,7 +87,6 @@ uint32_t eepromAddress = 0;
uint32_t eepromWritten = 0;
#endif
volatile uint8_t tenms = 1;
FlashCheckRes valid;
MemoryType memoryType;
uint32_t unlocked = 0;
@@ -88,26 +94,18 @@ uint32_t unlocked = 0;
void interrupt10ms()
{
tenms |= 1u; // 10 mS has passed
#if defined(DEBUG)
g_tmr10ms++;
#endif
uint8_t index = 0;
uint32_t in = readKeys();
for (int i = 0; i < TRM_BASE; i++) {
keys[index++].input(in & (1 << i));
}
keysPollingCycle();
#if defined(ROTARY_ENCODER_NAVIGATION)
static rotenc_t rePreviousValue;
rotenc_t reNewValue = (rotencValue / ROTARY_ENCODER_GRANULARITY);
rotenc_t reNewValue = rotaryEncoderGetValue();
int8_t scrollRE = reNewValue - rePreviousValue;
if (scrollRE) {
rePreviousValue = reNewValue;
pushEvent(scrollRE < 0 ? EVT_KEY_FIRST(KEY_UP) : EVT_KEY_FIRST(KEY_DOWN));
pushEvent(scrollRE < 0 ? EVT_ROTARY_LEFT : EVT_ROTARY_RIGHT);
}
#endif
}
@@ -265,7 +263,7 @@ void bootloaderInitApp()
#if (defined(RADIO_T8) || defined(RADIO_COMMANDO8)) && !defined(RADIOMASTER_RELEASE)
// Bind button not pressed
if ((~KEYS_GPIO_REG_BIND & KEYS_GPIO_PIN_BIND) == false) {
if ((~(KEYS_GPIO_REG_BIND->IDR) & KEYS_GPIO_PIN_BIND) == false) {
#else
// LHR & RHL trims not pressed simultanously
if (readTrims() != BOOTLOADER_KEYS) {
@@ -374,11 +372,11 @@ int bootloaderMain()
bootloaderDrawScreen(state, vpos);
if (event == EVT_KEY_FIRST(KEY_DOWN)) {
if (IS_NEXT_EVENT(event)) {
if (vpos < bootloaderGetMenuItemCount(MAIN_MENU_LEN) - 1) { vpos++; }
continue;
}
else if (event == EVT_KEY_FIRST(KEY_UP)) {
else if (IS_PREVIOUS_EVENT(event)) {
if (vpos > 0) { vpos--; }
continue;
}
@@ -435,7 +433,7 @@ int bootloaderMain()
limit = nameCount;
}
if (event == EVT_KEY_REPT(KEY_DOWN) || event == EVT_KEY_FIRST(KEY_DOWN)) {
if (IS_NEXT_EVENT(event)) {
if (vpos < limit - 1) {
vpos += 1;
}
@@ -446,7 +444,7 @@ int bootloaderMain()
}
}
}
else if (event == EVT_KEY_REPT(KEY_UP) || event == EVT_KEY_FIRST(KEY_UP)) {
else if (IS_PREVIOUS_EVENT(event)) {
if (vpos > 0) {
vpos -= 1;
}
@@ -1,69 +0,0 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*/
#include "extmodule_driver.h"
#include "hal.h"
#include "board.h"
#include "timers_driver.h"
static stm32_pulse_dma_tc_cb_t _ext_timer_DMA_TC_Callback;
const stm32_pulse_timer_t extmoduleTimer = {
.GPIOx = EXTMODULE_TX_GPIO,
.GPIO_Pin = EXTMODULE_TX_GPIO_PIN,
.GPIO_Alternate = EXTMODULE_TIMER_TX_GPIO_AF,
.TIMx = EXTMODULE_TIMER,
.TIM_Freq = EXTMODULE_TIMER_FREQ,
.TIM_Channel = EXTMODULE_TIMER_Channel,
.TIM_IRQn = EXTMODULE_TIMER_IRQn,
.DMAx = EXTMODULE_TIMER_DMA,
.DMA_Stream = EXTMODULE_TIMER_DMA_STREAM_LL,
.DMA_Channel = EXTMODULE_TIMER_DMA_CHANNEL,
.DMA_IRQn = EXTMODULE_TIMER_DMA_STREAM_IRQn,
.DMA_TC_CallbackPtr = &_ext_timer_DMA_TC_Callback,
};
// Make sure the timer channel is supported
static_assert(__STM32_PULSE_IS_TIMER_CHANNEL_SUPPORTED(EXTMODULE_TIMER_Channel),
"Unsupported timer channel");
// Make sure the DMA channel is supported
static_assert(__STM32_PULSE_IS_DMA_STREAM_SUPPORTED(EXTMODULE_TIMER_DMA_STREAM_LL),
"Unsupported DMA stream");
#if !defined(EXTMODULE_TIMER_DMA_IRQHandler)
#error "Missing EXTMODULE_TIMER_DMA_IRQHandler definition"
#endif
extern "C" void EXTMODULE_TIMER_DMA_IRQHandler()
{
stm32_pulse_dma_tc_isr(&extmoduleTimer);
}
#if !defined(EXTMODULE_TIMER_IRQHandler)
#error "Missing EXTMODULE_TIMER_IRQHandler definition"
#endif
extern "C" void EXTMODULE_TIMER_IRQHandler()
{
stm32_pulse_tim_update_isr(&extmoduleTimer);
}
@@ -15,6 +15,7 @@ if(NOT NATIVE_BUILD)
${STM32CUBE_DIR}/Src/stm32f2xx_ll_usart.c
${STM32CUBE_DIR}/Src/stm32f2xx_ll_rcc.c
${STM32CUBE_DIR}/Src/stm32f2xx_ll_exti.c
${STM32CUBE_DIR}/Src/stm32f2xx_ll_adc.c
# HAL drivers
${STM32CUBE_DIR}/Src/stm32f2xx_hal.c
${STM32CUBE_DIR}/Src/stm32f2xx_hal_pwr.c
@@ -50,11 +51,8 @@ add_library(stm32_stdperiph OBJECT EXCLUDE_FROM_ALL
${STM32_STDPERIPH_DIR}/src/stm32f2xx_gpio.c
${STM32_STDPERIPH_DIR}/src/stm32f2xx_dbgmcu.c
${STM32_STDPERIPH_DIR}/src/stm32f2xx_rcc.c
${STM32_STDPERIPH_DIR}/src/stm32f2xx_syscfg.c
${STM32_STDPERIPH_DIR}/src/stm32f2xx_spi.c
${STM32_STDPERIPH_DIR}/src/stm32f2xx_dma.c
${STM32_STDPERIPH_DIR}/src/stm32f2xx_pwr.c
${STM32_STDPERIPH_DIR}/src/stm32f2xx_adc.c
)
set(FIRMWARE_SRC ${FIRMWARE_SRC} $<TARGET_OBJECTS:stm32_stdperiph>)
@@ -8,12 +8,15 @@ if(NOT NATIVE_BUILD)
include_directories(${STM32CUBE_DIR}/Inc)
add_library(stm32cube_ll OBJECT EXCLUDE_FROM_ALL
# LL drivers
${STM32CUBE_DIR}/Src/stm32f4xx_ll_gpio.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_tim.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_dma.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_usart.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_rcc.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_exti.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_adc.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_spi.c
# HAL drivers
${STM32CUBE_DIR}/Src/stm32f4xx_hal.c
${STM32CUBE_DIR}/Src/stm32f4xx_hal_pwr.c
@@ -49,11 +52,8 @@ set(STM32_STDPERIPH_SRC
${STM32_STDPERIPH_DIR}/src/stm32f4xx_gpio.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_dbgmcu.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_rcc.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_syscfg.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_spi.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_dma.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_pwr.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_adc.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_flash.c
)
@@ -66,6 +66,10 @@ if(CPU_TYPE_FULL STREQUAL STM32F429xI)
${STM32_STDPERIPH_DIR}/src/stm32f4xx_dma2d.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_sdio.c
)
elseif (CPU_TYPE_FULL STREQUAL STM32F413xG)
set(STM32_STDPERIPH_SRC ${STM32_STDPERIPH_SRC}
targets/${TARGET_DIR}/startup_stm32f413vgtx.s
)
else()
set(STM32_STDPERIPH_SRC ${STM32_STDPERIPH_SRC}
targets/${TARGET_DIR}/startup_stm32f40_41xxx.s
@@ -151,16 +151,27 @@
/******************************************************************************/
/************************* PLL Parameters *************************************/
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
#define PLL_M 12
#define PLL_N 336
#if defined(STM32F413xx)
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
#define PLL_M 12
#define PLL_N 200
/* SYSCLK = PLL_VCO / PLL_P */
#define PLL_P 2
/* SYSCLK = PLL_VCO / PLL_P */
#define PLL_P 2
/* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
#define PLL_Q 7
/* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
#define PLL_Q 2
#else
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
#define PLL_M 12
#define PLL_N 336
/* SYSCLK = PLL_VCO / PLL_P */
#define PLL_P 2
/* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
#define PLL_Q 7
#endif
/******************************************************************************/
/**
@@ -178,8 +189,11 @@
/** @addtogroup STM32F4xx_System_Private_Variables
* @{
*/
#if defined(STM32F413xx)
uint32_t SystemCoreClock = 100000000;
#else
uint32_t SystemCoreClock = 168000000;
#endif
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
__I uint8_t APBPrescTable[8] = {0, 0, 0, 0, 1, 2, 3, 4};
@@ -20,6 +20,7 @@
*/
#include "board.h"
#include "watchdog_driver.h"
void waitFlashIdle()
{
@@ -22,6 +22,7 @@
#include "flysky_gimbal_driver.h"
#include "stm32_serial_driver.h"
#include "delays_driver.h"
#include "hal/adc_driver.h"
#include "hal.h"
#include "crc.h"
@@ -46,20 +47,8 @@ DEFINE_STM32_SERIAL_PORT(FSGimbal, fsUSART, HALLSTICK_BUFF_SIZE, 0);
static STRUCT_HALL HallProtocol = { 0 };
signed short hall_raw_values[FLYSKY_HALL_CHANNEL_COUNT];
unsigned short hall_adc_values[FLYSKY_HALL_CHANNEL_COUNT];
static void* _fs_usart_ctx = nullptr;
uint16_t get_flysky_hall_adc_value(uint8_t ch)
{
if (ch >= FLYSKY_HALL_CHANNEL_COUNT) {
return 0;
}
return 2 * FLYSKY_OFFSET_VALUE - hall_adc_values[ch];
}
static int _fs_get_byte(uint8_t* data)
{
return STM32SerialDriver.getByte(_fs_usart_ctx, data);
@@ -130,33 +119,31 @@ static volatile bool _fs_gimbal_detected;
static void flysky_gimbal_loop()
{
uint8_t byte;
uint8_t byte;
while(_fs_get_byte(&byte))
{
HallProtocol.index++;
while (_fs_get_byte(&byte)) {
HallProtocol.index++;
_fs_parse(&HallProtocol, byte);
if ( HallProtocol.msg_OK )
{
HallProtocol.msg_OK = 0;
HallProtocol.stickState = HallProtocol.data[HallProtocol.length - 1];
_fs_parse(&HallProtocol, byte);
if (HallProtocol.msg_OK) {
HallProtocol.msg_OK = 0;
HallProtocol.stickState = HallProtocol.data[HallProtocol.length - 1];
switch ( HallProtocol.hallID.hall_Id.receiverID )
{
case TRANSFER_DIR_TXMCU:
if(HallProtocol.hallID.hall_Id.packetID == FLYSKY_HALL_RESP_TYPE_VALUES) {
memcpy(hall_raw_values, HallProtocol.data, sizeof(hall_raw_values));
for ( uint8_t channel = 0; channel < 4; channel++ )
{
hall_adc_values[channel] = FLYSKY_OFFSET_VALUE + hall_raw_values[channel];
}
}
break;
switch (HallProtocol.hallID.hall_Id.receiverID) {
case TRANSFER_DIR_TXMCU:
if (HallProtocol.hallID.hall_Id.packetID ==
FLYSKY_HALL_RESP_TYPE_VALUES) {
int16_t* p_values = (int16_t*)HallProtocol.data;
uint16_t* adcValues = getAnalogValues();
for (uint8_t i = 0; i < 4; i++) {
adcValues[i] = FLYSKY_OFFSET_VALUE - p_values[i];
}
_fs_gimbal_detected = true;
}
}
break;
}
_fs_gimbal_detected = true;
}
}
}
static void flysky_gimbal_deinit()
@@ -19,12 +19,11 @@
* GNU General Public License for more details.
*/
#include "opentx.h"
#include "mixer_scheduler.h"
#if !defined(SIMU)
#include "stm32_hal_ll.h"
#include "FreeRTOSConfig.h"
#include "hal.h"
// Start scheduler with default period
void mixerSchedulerStart()
@@ -81,5 +80,3 @@ extern "C" void MIXER_SCHEDULER_TIMER_IRQHandler(void)
// trigger mixer start
mixerSchedulerISRTrigger();
}
#endif
@@ -20,6 +20,7 @@
*/
#include "board.h"
#include "watchdog_driver.h"
void pwrInit()
{
@@ -107,9 +108,22 @@ bool pwrForcePressed()
bool pwrPressed()
{
#if defined(PWR_EXTRA_SWITCH_GPIO)
return (GPIO_ReadInputDataBit(PWR_SWITCH_GPIO, PWR_SWITCH_GPIO_PIN) == Bit_RESET || GPIO_ReadInputDataBit(PWR_EXTRA_SWITCH_GPIO, PWR_EXTRA_SWITCH_GPIO_PIN) == Bit_RESET);
return (GPIO_ReadInputDataBit(PWR_SWITCH_GPIO, PWR_SWITCH_GPIO_PIN) ==
Bit_RESET ||
GPIO_ReadInputDataBit(PWR_EXTRA_SWITCH_GPIO,
PWR_EXTRA_SWITCH_GPIO_PIN) == Bit_RESET);
#else
return GPIO_ReadInputDataBit(PWR_SWITCH_GPIO, PWR_SWITCH_GPIO_PIN) == Bit_RESET;
return GPIO_ReadInputDataBit(PWR_SWITCH_GPIO, PWR_SWITCH_GPIO_PIN) ==
Bit_RESET;
#endif
}
bool pwrOffPressed()
{
#if defined(PWR_BUTTON_PRESS)
return pwrPressed();
#else
return !pwrPressed();
#endif
}
@@ -21,9 +21,13 @@
#include "stm32_hal_ll.h"
#include "stm32_exti_driver.h"
#include "stm32_gpio_driver.h"
#include "board.h"
#include "hal.h"
#include "hal/key_driver.h"
#include "hal/rotary_encoder.h"
#include "board_common.h"
#if !defined(BOOT)
@@ -43,6 +47,16 @@ volatile uint32_t rotencDt = 0;
(g_eeGeneral.rotEncMode == ROTARY_ENCODER_MODE_INVERT_BOTH ? -2 : 2);
#endif
rotenc_t rotaryEncoderGetValue()
{
return rotencValue / ROTARY_ENCODER_GRANULARITY;
}
rotenc_t rotaryEncoderGetRawValue()
{
return rotencValue;
}
void rotaryEncoderCheck()
{
static uint8_t state = 0;
@@ -131,6 +145,15 @@ void rotaryEncoderStartDelay()
void rotaryEncoderInit()
{
LL_GPIO_InitTypeDef pinInit;
LL_GPIO_StructInit(&pinInit);
pinInit.Mode = LL_GPIO_MODE_INPUT;
pinInit.Pull = LL_GPIO_PULL_UP;
pinInit.Pin = ROTARY_ENCODER_GPIO_PIN_A | ROTARY_ENCODER_GPIO_PIN_B;
stm32_gpio_enable_clock(ROTARY_ENCODER_GPIO);
LL_GPIO_Init(ROTARY_ENCODER_GPIO, &pinInit);
ROTARY_ENCODER_TIMER->ARR = 99; // 100uS
ROTARY_ENCODER_TIMER->PSC = (PERI1_FREQUENCY * TIMER_MULT_APB1) / 1000000 - 1; // 1uS
ROTARY_ENCODER_TIMER->CCER = 0;
@@ -91,8 +91,17 @@ void rtcInit()
rtcGetTime(&utm);
g_rtcTime = gmktime(&utm);
#endif
#if defined(RTC_BACKUP_RAM) && !defined(BOOT)
__HAL_RCC_BKPSRAM_CLK_ENABLE();
HAL_PWREx_EnableBkUpReg();
#endif
}
void rtcDisableBackupReg()
{
#if defined(RTC_BACKUP_RAM)
__HAL_RCC_BKPSRAM_CLK_DISABLE();
HAL_PWREx_DisableBkUpReg();
#endif
}
@@ -21,6 +21,12 @@
#include "sdio_sd.h"
#include "debug.h"
#if defined(STM32F4)
#include "stm32f4xx_sdio.h"
#elif defined(STM32F2)
#include "stm32f2xx_sdio.h"
#endif
#define SDIO_STATIC_FLAGS ((uint32_t)0x000005FF)
#define SDIO_CMD0TIMEOUT ((uint32_t)0x00010000)
@@ -19,25 +19,24 @@
* GNU General Public License for more details.
*/
#include "opentx.h"
#include "hal/adc_driver.h"
#include "stm32_hal_ll.h"
#include "delays_driver.h"
volatile uint32_t pwm_interrupt_count = 0;
volatile uint16_t timer_capture_values[NUM_PWMSTICKS];
#include "hal.h"
#include "dataconstants.h"
void sticksPwmInit()
static volatile uint32_t pwm_interrupt_count;
static void sticksPwmInit()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = PWM_GPIOA_PINS;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(PWM_GPIO, &GPIO_InitStructure);
GPIO_PinAFConfig(PWM_GPIO, GPIO_PinSource0, PWM_GPIO_AF);
GPIO_PinAFConfig(PWM_GPIO, GPIO_PinSource1, PWM_GPIO_AF);
GPIO_PinAFConfig(PWM_GPIO, GPIO_PinSource2, PWM_GPIO_AF);
GPIO_PinAFConfig(PWM_GPIO, GPIO_PinSource3, PWM_GPIO_AF);
LL_GPIO_InitTypeDef pinInit;
pinInit.Pin = PWM_GPIOA_PINS;
pinInit.Mode = LL_GPIO_MODE_ALTERNATE;
pinInit.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
pinInit.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
pinInit.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(PWM_GPIO, &pinInit);
PWM_TIMER->CR1 &= ~TIM_CR1_CEN; // Stop timer
PWM_TIMER->PSC = 80;
@@ -45,14 +44,39 @@ void sticksPwmInit()
PWM_TIMER->CCMR1 = TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_0;
PWM_TIMER->CCMR2 = TIM_CCMR2_CC3S_0 | TIM_CCMR2_CC4S_0;
PWM_TIMER->CCER = TIM_CCER_CC1E | TIM_CCER_CC2E | TIM_CCER_CC3E | TIM_CCER_CC4E;
PWM_TIMER->DIER |= TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4;
PWM_TIMER->DIER |= TIM_DIER_CC1IE | TIM_DIER_CC2IE | TIM_DIER_CC3IE | TIM_DIER_CC4IE;
PWM_TIMER->CR1 = TIM_CR1_CEN; // Start timer
NVIC_EnableIRQ(PWM_IRQn);
NVIC_SetPriority(PWM_IRQn, 10);
}
inline uint32_t TIM_GetCapture_Stick(uint8_t n)
static void sticksPwmDeInit()
{
NVIC_DisableIRQ(PWM_IRQn);
PWM_TIMER->CR1 &= ~TIM_CR1_CEN; // Stop timer
LL_GPIO_InitTypeDef pinInit;
LL_GPIO_StructInit(&pinInit);
pinInit.Pin = PWM_GPIOA_PINS;
pinInit.Mode = LL_GPIO_MODE_INPUT;
LL_GPIO_Init(PWM_GPIO, &pinInit);
}
bool sticksPwmDetect()
{
pwm_interrupt_count = 0;
sticksPwmInit();
delay_ms(20);
if (pwm_interrupt_count < 32) {
sticksPwmDeInit();
return false;
}
return true;
}
static inline uint32_t TIM_GetCapture_Stick(uint8_t n)
{
switch (n) {
case 0:
@@ -68,22 +92,22 @@ inline uint32_t TIM_GetCapture_Stick(uint8_t n)
}
}
inline void TIM_SetPolarityRising(uint8_t n)
static inline void TIM_SetPolarityRising(uint8_t n)
{
PWM_TIMER->CCER &= ~(TIM_CCER_CC1P << (n * 4));
}
inline void TIM_SetPolarityFalling(uint8_t n)
static inline void TIM_SetPolarityFalling(uint8_t n)
{
PWM_TIMER->CCER |= (TIM_CCER_CC1P << (n * 4));
}
inline void TIM_ClearITPendingBit(uint8_t n)
static inline void TIM_ClearITPendingBit(uint8_t n)
{
PWM_TIMER->SR = ~(TIM_IT_CC1 << n);
PWM_TIMER->SR = ~(TIM_SR_CC1IF << n);
}
inline uint32_t diff_with_16bits_overflow(uint32_t a, uint32_t b)
static inline uint32_t diff_with_16bits_overflow(uint32_t a, uint32_t b)
{
if (b > a)
return b - a;
@@ -91,19 +115,45 @@ inline uint32_t diff_with_16bits_overflow(uint32_t a, uint32_t b)
return b + 0xffff - a;
}
#if !defined(STICK_PWM_CHANNEL_0)
#define STICK_PWM_CHANNEL_0 0
#endif
#if !defined(STICK_PWM_CHANNEL_1)
#define STICK_PWM_CHANNEL_1 1
#endif
#if !defined(STICK_PWM_CHANNEL_2)
#define STICK_PWM_CHANNEL_2 2
#endif
#if !defined(STICK_PWM_CHANNEL_3)
#define STICK_PWM_CHANNEL_3 3
#endif
static const uint8_t _channel_map[] = {
STICK_PWM_CHANNEL_0,
STICK_PWM_CHANNEL_1,
STICK_PWM_CHANNEL_2,
STICK_PWM_CHANNEL_3,
};
extern "C" void PWM_IRQHandler(void)
{
static uint8_t timer_capture_states[NUM_PWMSTICKS];
static uint32_t timer_capture_rising_time[NUM_PWMSTICKS];
static uint8_t timer_capture_states[MAX_STICKS];
static uint32_t timer_capture_rising_time[MAX_STICKS];
for (uint8_t i=0; i<NUM_PWMSTICKS; i++) {
if (PWM_TIMER->SR & (TIM_DIER_CC1IE << i)) {
auto adcValues = getAnalogValues();
for (uint8_t i=0; i<MAX_STICKS; i++) {
if (PWM_TIMER->SR & (TIM_SR_CC1IF << i)) {
uint32_t capture = TIM_GetCapture_Stick(i);
pwm_interrupt_count++; // overflow may happen but we only use this to detect PWM / ADC on radio startup
// overflow may happen but we only use this to detect PWM / ADC on radio startup
pwm_interrupt_count++;
if (timer_capture_states[i] != 0) {
uint32_t value = diff_with_16bits_overflow(timer_capture_rising_time[i], capture);
if (value < 10000) {
timer_capture_values[i] = (uint16_t) value;
adcValues[_channel_map[i]] = (uint16_t) value;
}
TIM_SetPolarityRising(i);
timer_capture_states[i] = 0;
@@ -117,21 +167,3 @@ extern "C" void PWM_IRQHandler(void)
}
}
}
#if defined(STICK_CHANNEL_CHANGE)
void sticksPwmRead(uint16_t * values)
{
values[0] = timer_capture_values[STICK_PWM_CHANNEL_0];
values[1] = timer_capture_values[STICK_PWM_CHANNEL_1];
values[2] = timer_capture_values[STICK_PWM_CHANNEL_2];
values[3] = timer_capture_values[STICK_PWM_CHANNEL_3];
}
#else
void sticksPwmRead(uint16_t * values)
{
values[0] = timer_capture_values[0];
values[1] = timer_capture_values[1];
values[2] = timer_capture_values[3];
values[3] = timer_capture_values[2];
}
#endif
@@ -0,0 +1,25 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*/
#pragma once
// returns 'true' if PWM sticks have been detected
bool sticksPwmDetect();
@@ -0,0 +1,582 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*/
#include "stm32_adc.h"
#include "stm32_gpio_driver.h"
#include "opentx.h"
#define ADC_COMMON ((ADC_Common_TypeDef *) ADC_BASE)
#define MAX_ADC_INPUTS 32
#define OVERSAMPLING 4
// Please note that we use the same prio for DMA TC and ADC IRQs
// to avoid issues with preemption between these 2
#define ADC_IRQ_PRIO configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY
// Max 32 inputs supported
static uint32_t _adc_input_mask;
static volatile uint32_t _adc_inhibit_mask;
// DMA buffers
static uint16_t _adc_dma_buffer[MAX_ADC_INPUTS] __DMA;
// ADCs started
static uint8_t _adc_started_mask;
static volatile uint8_t _adc_completed;
static const stm32_adc_t* _adc_ADCs;
static uint8_t _adc_n_ADC;
static const stm32_adc_input_t* _adc_inputs;
static uint8_t _adc_n_inputs;
// Need for oversampling and decimation
static uint8_t _adc_run;
static uint8_t _adc_oversampling_disabled;
static uint16_t _adc_oversampling[MAX_ADC_INPUTS];
// STM32 uses a 25K+25K voltage divider bridge to measure the battery voltage
// Measuring VBAT puts considerable drain (22 µA) on the battery instead of
// normal drain (~10 nA)
void enableVBatBridge()
{
if (adcGetMaxInputs(ADC_INPUT_RTC_BAT) < 1) return;
// Set internal measurement path for vbat sensor
LL_ADC_SetCommonPathInternalCh(ADC_COMMON, LL_ADC_PATH_INTERNAL_VBAT);
auto channel = adcGetInputOffset(ADC_INPUT_RTC_BAT);
_adc_inhibit_mask &= ~(1 << channel);
}
void disableVBatBridge()
{
if (adcGetMaxInputs(ADC_INPUT_RTC_BAT) < 1) return;
auto channel = adcGetInputOffset(ADC_INPUT_RTC_BAT);
_adc_inhibit_mask |= (1 << channel);
// Set internal measurement path to none
LL_ADC_SetCommonPathInternalCh(ADC_COMMON, LL_ADC_PATH_INTERNAL_NONE);
}
bool isVBatBridgeEnabled()
{
// && !(_adc_inhibit_mask & (1 << channel));
return LL_ADC_GetCommonPathInternalCh(ADC_COMMON) == LL_ADC_PATH_INTERNAL_VBAT;
}
static void adc_enable_clock(ADC_TypeDef* ADCx)
{
uint32_t adc_idx = (((uint32_t) ADCx) - ADC1_BASE) / 0x100UL;
uint32_t adc_msk = RCC_APB2ENR_ADC1EN << adc_idx;
LL_APB2_GRP1_EnableClock(adc_msk);
}
static bool adc_disable_dma(DMA_TypeDef* DMAx, uint32_t stream);
static void adc_dma_clear_flags(DMA_TypeDef* DMAx, uint32_t stream);
static void adc_init_pins(const stm32_adc_gpio_t* GPIOs, uint8_t n_GPIO)
{
LL_GPIO_InitTypeDef pinInit;
LL_GPIO_StructInit(&pinInit);
pinInit.Mode = LL_GPIO_MODE_ANALOG;
pinInit.Pull = LL_GPIO_PULL_NO;
const stm32_adc_gpio_t* gpio = GPIOs;
while (n_GPIO > 0) {
pinInit.Pin = 0;
for (uint8_t pin_idx = 0; pin_idx < gpio->n_pins; pin_idx++) {
uint32_t pin = gpio->pins[pin_idx];
uint32_t mode = LL_GPIO_GetPinMode(gpio->GPIOx, pin);
// Output or AF: pin is probably used somewhere else
if (mode != LL_GPIO_MODE_INPUT && mode != LL_GPIO_MODE_ANALOG) continue;
pinInit.Pin |= pin;
}
stm32_gpio_enable_clock(gpio->GPIOx);
LL_GPIO_Init(gpio->GPIOx, &pinInit);
gpio++; n_GPIO--;
}
}
static const uint32_t _seq_length_lookup[] = {
LL_ADC_REG_SEQ_SCAN_DISABLE,
LL_ADC_REG_SEQ_SCAN_ENABLE_2RANKS,
LL_ADC_REG_SEQ_SCAN_ENABLE_3RANKS,
LL_ADC_REG_SEQ_SCAN_ENABLE_4RANKS,
LL_ADC_REG_SEQ_SCAN_ENABLE_5RANKS,
LL_ADC_REG_SEQ_SCAN_ENABLE_6RANKS,
LL_ADC_REG_SEQ_SCAN_ENABLE_7RANKS,
LL_ADC_REG_SEQ_SCAN_ENABLE_8RANKS,
LL_ADC_REG_SEQ_SCAN_ENABLE_9RANKS,
LL_ADC_REG_SEQ_SCAN_ENABLE_10RANKS,
LL_ADC_REG_SEQ_SCAN_ENABLE_11RANKS,
LL_ADC_REG_SEQ_SCAN_ENABLE_12RANKS,
LL_ADC_REG_SEQ_SCAN_ENABLE_13RANKS,
LL_ADC_REG_SEQ_SCAN_ENABLE_14RANKS,
LL_ADC_REG_SEQ_SCAN_ENABLE_15RANKS,
LL_ADC_REG_SEQ_SCAN_ENABLE_16RANKS,
};
static void adc_setup_scan_mode(ADC_TypeDef* ADCx, uint8_t nconv)
{
// ADC must be disabled for the functions used here
LL_ADC_Disable(ADCx);
LL_ADC_InitTypeDef adcInit;
LL_ADC_StructInit(&adcInit);
if (nconv > 1) {
adcInit.SequencersScanMode = LL_ADC_SEQ_SCAN_ENABLE;
} else {
adcInit.SequencersScanMode = LL_ADC_SEQ_SCAN_DISABLE;
}
adcInit.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
LL_ADC_Init(ADCx, &adcInit);
LL_ADC_REG_InitTypeDef adcRegInit;
LL_ADC_REG_StructInit(&adcRegInit);
adcRegInit.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
adcRegInit.ContinuousMode = LL_ADC_REG_CONV_SINGLE;
if (nconv > 1) {
adcRegInit.SequencerLength = _seq_length_lookup[nconv - 1];
adcRegInit.DMATransfer = LL_ADC_REG_DMA_TRANSFER_UNLIMITED;
}
LL_ADC_REG_Init(ADCx, &adcRegInit);
// Enable ADC
// TODO: check ADC_CR2_DDS (normally only for circular DMA
// but the old was using it (no clue why)
LL_ADC_Enable(ADCx);
}
static const uint32_t _rank_lookup[] = {
LL_ADC_REG_RANK_1,
LL_ADC_REG_RANK_2,
LL_ADC_REG_RANK_3,
LL_ADC_REG_RANK_4,
LL_ADC_REG_RANK_5,
LL_ADC_REG_RANK_6,
LL_ADC_REG_RANK_7,
LL_ADC_REG_RANK_8,
LL_ADC_REG_RANK_9,
LL_ADC_REG_RANK_10,
LL_ADC_REG_RANK_11,
LL_ADC_REG_RANK_12,
LL_ADC_REG_RANK_13,
LL_ADC_REG_RANK_14,
LL_ADC_REG_RANK_15,
LL_ADC_REG_RANK_16,
};
static uint8_t adc_init_channels(const stm32_adc_t* adc,
const stm32_adc_input_t* inputs,
const uint8_t* chan,
uint8_t nconv)
{
if (!chan || !nconv) return 0;
uint8_t rank = 0;
uint32_t channel_mask = 0;
while (nconv > 0) {
uint8_t input_idx = *chan;
const stm32_adc_input_t* input = &inputs[input_idx];
// internal channel don't have a GPIO + pin defined
uint32_t mask = (1 << (ADC_CHANNEL_ID_MASK & input->ADC_Channel));
if (!__LL_ADC_IS_CHANNEL_INTERNAL(input->ADC_Channel)) {
uint32_t mode = LL_GPIO_GetPinMode(input->GPIOx, input->GPIO_Pin);
if (mode != LL_GPIO_MODE_ANALOG) {
// skip channel
nconv--; chan++;
continue;
}
} else {
// Internal channels are inhibited until explicitely enabled
_adc_inhibit_mask |= mask;
}
// channel is already used, probably a secondary input
// using the same ADC channel
if (channel_mask & mask) {
nconv--; chan++;
continue;
}
// update mask for used channels
channel_mask |= mask;
// update mask for valid inputs
_adc_input_mask |= (1 << input_idx);
LL_ADC_REG_SetSequencerRanks(adc->ADCx, _rank_lookup[rank],
input->ADC_Channel);
LL_ADC_SetChannelSamplingTime(adc->ADCx, input->ADC_Channel,
adc->sample_time);
nconv--;
rank++;
chan++;
}
return rank;
}
static bool adc_init_dma_stream(ADC_TypeDef* adc, DMA_TypeDef* DMAx,
uint32_t stream, uint32_t channel,
uint16_t* dest, uint8_t nconv)
{
// Disable DMA before continuing (see ref. manual "Stream configuration procedure")
if (!adc_disable_dma(DMAx, stream))
return false;
// Clear Interrupt flags
adc_dma_clear_flags(DMAx, stream);
// setup DMA request
LL_DMA_ConfigAddresses(DMAx, stream, CONVERT_PTR_UINT(&adc->DR),
CONVERT_PTR_UINT(dest),
LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
LL_DMA_SetDataLength(DMAx, stream, nconv);
LL_DMA_SetChannelSelection(DMAx, stream, channel);
// Very high priority, 1 byte transfers, increment memory
LL_DMA_ConfigTransfer(DMAx, stream,
LL_DMA_PRIORITY_VERYHIGH | LL_DMA_MDATAALIGN_HALFWORD |
LL_DMA_PDATAALIGN_HALFWORD | LL_DMA_MEMORY_INCREMENT |
LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
// disable direct mode, half full FIFO
LL_DMA_EnableFifoMode(DMAx, stream);
LL_DMA_SetFIFOThreshold(DMAx, stream, LL_DMA_FIFOTHRESHOLD_1_2);
return true;
}
bool stm32_hal_adc_init(const stm32_adc_t* ADCs, uint8_t n_ADC,
const stm32_adc_input_t* inputs,
const stm32_adc_gpio_t* ADC_GPIOs, uint8_t n_GPIO)
{
_adc_input_mask = 0;
_adc_inhibit_mask = 0;
_adc_oversampling_disabled = 0;
adc_init_pins(ADC_GPIOs, n_GPIO);
// Init common to all ADCs
LL_ADC_CommonInitTypeDef commonInit;
LL_ADC_CommonStructInit(&commonInit);
commonInit.CommonClock = LL_ADC_CLOCK_SYNC_PCLK_DIV2;
LL_ADC_CommonInit(ADC_COMMON, &commonInit);
_adc_input_mask = 0;
// uint16_t* dma_buffer = _adc_dma_buffer;
const stm32_adc_t* adc = ADCs;
while (n_ADC > 0) {
uint8_t nconv = adc->n_channels;
if (nconv > 0) {
// enable periph clock
adc_enable_clock(adc->ADCx);
// configure each channel
const uint8_t* chan = adc->channels;
nconv = adc_init_channels(adc, inputs, chan, nconv);
adc_setup_scan_mode(adc->ADCx, nconv);
if (nconv > 1) {
if (adc->DMAx) {
uint16_t* dma_buffer = _adc_dma_buffer + adc->offset;
if (!adc_init_dma_stream(adc->ADCx, adc->DMAx, adc->DMA_Stream,
adc->DMA_Channel, dma_buffer, nconv))
return false;
NVIC_SetPriority(adc->DMA_Stream_IRQn, ADC_IRQ_PRIO);
NVIC_EnableIRQ(adc->DMA_Stream_IRQn);
} else {
// multiple channels on the same ADC
// without a DMA is NOT supported
return false;
}
} else {
// single conversion
NVIC_SetPriority(ADC_IRQn, ADC_IRQ_PRIO);
NVIC_EnableIRQ(ADC_IRQn);
}
}
// move to next ADC definition
adc++; n_ADC--;
}
return true;
}
#define DMA_Stream0_IT_MASK (uint32_t)(DMA_LISR_FEIF0 | DMA_LISR_DMEIF0 | \
DMA_LISR_TEIF0 | DMA_LISR_HTIF0 | \
DMA_LISR_TCIF0)
#define DMA_Stream4_IT_MASK (uint32_t)(DMA_HISR_FEIF4 | DMA_HISR_DMEIF4 | \
DMA_HISR_TEIF4 | DMA_HISR_HTIF4 | \
DMA_HISR_TCIF4)
static void adc_dma_clear_flags(DMA_TypeDef* DMAx, uint32_t stream)
{
// no other choice, sorry for that...
if (stream == LL_DMA_STREAM_4) {
/* Reset interrupt pending bits for DMA2 Stream4 */
WRITE_REG(DMAx->HIFCR, DMA_Stream4_IT_MASK);
} else if (stream == LL_DMA_STREAM_0) {
/* Reset interrupt pending bits for DMA2 Stream0 */
WRITE_REG(DMAx->LIFCR, DMA_Stream0_IT_MASK);
}
}
static inline DMA_Stream_TypeDef* _dma_get_stream(DMA_TypeDef *DMAx, uint32_t Stream)
{
return ((DMA_Stream_TypeDef*)((uint32_t)((uint32_t)DMAx + STREAM_OFFSET_TAB[Stream])));
}
static void adc_start_dma_conversion(ADC_TypeDef* ADCx, DMA_TypeDef* DMAx, uint32_t stream)
{
// Clear Interrupt flags
adc_dma_clear_flags(DMAx, stream);
// Clear ADC status register & disable ADC IRQ
CLEAR_BIT(ADCx->SR, ADC_SR_EOC | ADC_SR_STRT | ADC_SR_OVR);
CLEAR_BIT(ADCx->CR1, ADC_CR1_EOCIE);
// Enable DMA
auto dma_stream = _dma_get_stream(DMAx, stream);
SET_BIT(dma_stream->CR, DMA_SxCR_TCIE | DMA_SxCR_TEIE | DMA_SxCR_DMEIE);
SET_BIT(dma_stream->CR, DMA_SxCR_EN);
// Trigger ADC start
SET_BIT(ADCx->CR2, ADC_CR2_SWSTART);
}
static bool adc_disable_dma(DMA_TypeDef* DMAx, uint32_t stream)
{
LL_DMA_DisableStream(DMAx, stream);
// wait until DMA EN bit gets cleared by hardware
uint16_t timeout = 1000;
while (LL_DMA_IsEnabledStream(DMAx, stream)) {
if (--timeout == 0) {
// Timeout. Failed to disable DMA
return false;
}
}
return true;
}
static void adc_start_normal_conversion(ADC_TypeDef* ADCx)
{
// clear status flags
CLEAR_BIT(ADCx->SR, ADC_SR_EOC | ADC_SR_STRT | ADC_SR_OVR);
// enble ADC IRQ
SET_BIT(ADCx->CR1, ADC_CR1_EOCIE);
// and start!
SET_BIT(ADCx->CR2, ADC_CR2_SWSTART);
}
static void adc_start_read(const stm32_adc_t* ADCs, uint8_t n_ADC)
{
// Start all ADCs in parallel
uint8_t adc_mask = 1;
_adc_started_mask = 0;
const stm32_adc_t* adc = ADCs;
while (n_ADC > 0) {
// if the ADC has no active channels,
// it has not been enabled at all
auto ADCx = adc->ADCx;
if (!LL_ADC_IsEnabled(ADCx)) {
adc++; n_ADC--; adc_mask <<= 1;
continue;
}
// more than one channel enabled on this ADC
if (LL_ADC_GetSequencersScanMode(ADCx) == LL_ADC_SEQ_SCAN_ENABLE) {
// Disable DMA before continuing (see ref. manual "Stream configuration procedure")
auto DMAx = adc->DMAx;
auto stream = adc->DMA_Stream;
if (DMAx && adc_disable_dma(DMAx, stream)) {
_adc_started_mask |= adc_mask;
adc_start_dma_conversion(ADCx, DMAx, stream);
}
} else {
// only one channel
_adc_started_mask |= adc_mask;
adc_start_normal_conversion(ADCx);
}
// move to next ADC
adc++; n_ADC--; adc_mask <<= 1;
}
}
bool stm32_hal_adc_start_read(const stm32_adc_t* ADCs, uint8_t n_ADC,
const stm32_adc_input_t* inputs, uint8_t n_inputs)
{
_adc_completed = 0;
_adc_run = 0;
_adc_ADCs = ADCs;
_adc_n_ADC = n_ADC;
_adc_inputs = inputs;
_adc_n_inputs = n_inputs;
memclear(_adc_oversampling, sizeof(_adc_oversampling));
adc_start_read(_adc_ADCs, _adc_n_ADC);
return true;
}
static void copy_adc_values(uint16_t* src, const stm32_adc_t* adc,
const stm32_adc_input_t* inputs)
{
for (uint8_t i=0; i < adc->n_channels; i++) {
uint8_t channel = adc->channels[i];
// if input disabled, skip
if (~_adc_input_mask & (1 << channel)) {
continue;
}
// skip sampled but inhibited channels
if (_adc_inhibit_mask & (1 << channel)) {
src++;
continue;
}
// TODO: move inversion to the generic ADC driver?
if (inputs[channel].inverted)
_adc_oversampling[channel] += ADC_INVERT_VALUE(*src);
else
_adc_oversampling[channel] += *src;
#if defined(JITTER_MEASURE)
if (JITTER_MEASURE_ACTIVE()) {
rawJitter[channel].measure(dst[channel]);
}
#endif
src++;
}
}
void stm32_hal_adc_wait_completion(const stm32_adc_t* ADCs, uint8_t n_ADC,
const stm32_adc_input_t* inputs, uint8_t n_inputs)
{
(void)ADCs;
(void)n_ADC;
(void)inputs;
(void)n_inputs;
while(!_adc_completed) {
// busy wait
}
}
void stm32_hal_adc_disable_oversampling()
{
_adc_oversampling_disabled = 1;
}
static void _adc_mark_completed(const stm32_adc_t* adc)
{
uint8_t adc_idx = (adc - _adc_ADCs);
_adc_started_mask &= ~(1 << adc_idx);
}
static void _adc_chain_conversions(const stm32_adc_t* adc)
{
_adc_mark_completed(adc);
if (_adc_started_mask != 0) return;
if (!_adc_oversampling_disabled && (++_adc_run < OVERSAMPLING)) {
adc_start_read(_adc_ADCs, _adc_n_ADC);
return;
}
auto adcValues = getAnalogValues();
for (uint8_t i = 0; i < _adc_n_inputs; i++) {
if (~_adc_input_mask & (1 << i)) continue;
if (_adc_inhibit_mask & (1 << i)) continue;
adcValues[i] = _adc_oversampling[i] / OVERSAMPLING;
}
// we're done!
_adc_completed = 1;
}
void stm32_hal_adc_dma_isr(const stm32_adc_t* adc)
{
// Disable IRQ
auto dma_stream = _dma_get_stream(adc->DMAx, adc->DMA_Stream);
CLEAR_BIT(dma_stream->CR, DMA_SxCR_TCIE | DMA_SxCR_TEIE | DMA_SxCR_DMEIE);
uint16_t* dma_buffer = _adc_dma_buffer + adc->offset;
copy_adc_values(dma_buffer, adc, _adc_inputs);
_adc_chain_conversions(adc);
}
void stm32_hal_adc_isr(const stm32_adc_t* adc)
{
// check if this ADC triggered the IRQ
auto ADCx = adc->ADCx;
if (!LL_ADC_IsActiveFlag_EOCS(ADCx) || !LL_ADC_IsEnabledIT_EOCS(ADCx))
return;
// Disable end-of-conversion IRQ
CLEAR_BIT(ADCx->CR1, ADC_CR1_EOCIE);
uint16_t* dma_buffer = _adc_dma_buffer + adc->offset;
*dma_buffer = adc->ADCx->DR;
copy_adc_values(dma_buffer, adc, _adc_inputs);
_adc_chain_conversions(adc);
}
@@ -0,0 +1,68 @@
/*
* Copyright (C) EdgeTx
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*/
#pragma once
#include "stm32_hal_ll.h"
#include "hal/adc_driver.h"
struct stm32_adc_input_t {
const char* name;
GPIO_TypeDef* GPIOx;
uint32_t GPIO_Pin;
uint32_t ADC_Channel;
uint8_t inverted;
};
struct stm32_adc_t {
ADC_TypeDef* ADCx;
DMA_TypeDef* DMAx;
uint32_t DMA_Channel;
uint32_t DMA_Stream;
IRQn_Type DMA_Stream_IRQn;
const uint8_t* channels;
uint8_t offset;
uint8_t n_channels;
uint8_t sample_time;
};
struct stm32_adc_gpio_t {
GPIO_TypeDef* GPIOx;
const uint32_t* pins;
uint8_t n_pins;
};
bool stm32_hal_adc_init(const stm32_adc_t* ADCs, uint8_t n_ADC,
const stm32_adc_input_t* inputs,
const stm32_adc_gpio_t* ADC_GPIOs, uint8_t n_GPIO);
bool stm32_hal_adc_start_read(const stm32_adc_t* ADCs, uint8_t n_ADC,
const stm32_adc_input_t* inputs, uint8_t n_inputs);
void stm32_hal_adc_wait_completion(const stm32_adc_t* ADCs, uint8_t n_ADC,
const stm32_adc_input_t* inputs, uint8_t n_inputs);
void stm32_hal_adc_disable_oversampling();
void stm32_hal_adc_dma_isr(const stm32_adc_t* adc);
void stm32_hal_adc_isr(const stm32_adc_t* adc);
@@ -0,0 +1,42 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*/
#pragma once
#if defined(__cplusplus)
extern "C" {
#endif
#if defined(STM32F4)
#include "CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h"
#elif defined(STM32F2)
// Workaround to prevent the other CMSIS header to be included
// and fix some missing items in the old one
#include "thirdparty/CMSIS/Include/cmsis_compiler.h"
#define __CORE_CM3_H_GENERIC
#include "CMSIS/Device/ST/STM32F2xx/Include/stm32f2xx.h"
#endif
#if defined(__cplusplus)
}
#endif
@@ -0,0 +1,30 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*/
#include "stm32_gpio_driver.h"
void stm32_gpio_enable_clock(GPIO_TypeDef *GPIOx)
{
uint32_t reg_idx = (((uint32_t) GPIOx) - GPIOA_BASE) / 0x0400UL;
uint32_t reg_msk = RCC_AHB1ENR_GPIOAEN << reg_idx;
LL_AHB1_GRP1_EnableClock(reg_msk);
}
@@ -21,8 +21,6 @@
#pragma once
#include <stdint.h>
#include "stm32_hal_ll.h"
#include "stm32_pulse_driver.h"
extern const stm32_pulse_timer_t extmoduleTimer;
void stm32_gpio_enable_clock(GPIO_TypeDef *GPIOx);
@@ -1,474 +0,0 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*/
#include "opentx.h"
#include "hal/adc_driver.h"
#include "stm32_hal_adc.h"
static bool adc_disable_dma(DMA_Stream_TypeDef * dma_stream);
static void adc_dma_clear_flags(DMA_Stream_TypeDef * dma_stream);
static void adc_init_pins()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
#if defined(ADC_GPIOA_PINS)
#if defined(FLYSKY_GIMBAL)
if (globalData.flyskygimbals)
{
GPIO_InitStructure.GPIO_Pin = ADC_GPIOA_PINS_FS;
}
else
#endif
{
GPIO_InitStructure.GPIO_Pin = ADC_GPIOA_PINS;
}
GPIO_Init(GPIOA, &GPIO_InitStructure);
#endif
#if defined(ADC_GPIOB_PINS)
GPIO_InitStructure.GPIO_Pin = ADC_GPIOB_PINS;
GPIO_Init(GPIOB, &GPIO_InitStructure);
#endif
#if defined(ADC_GPIOC_PINS)
GPIO_InitStructure.GPIO_Pin = ADC_GPIOC_PINS;
GPIO_Init(GPIOC, &GPIO_InitStructure);
#endif
#if defined(ADC_GPIOF_PINS)
GPIO_InitStructure.GPIO_Pin = ADC_GPIOF_PINS;
GPIO_Init(GPIOF, &GPIO_InitStructure);
#endif
}
static void adc_setup_scan_mode(ADC_TypeDef* ADCx, uint8_t nconv)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = ENABLE; // Sets ADC_CR1_SCAN
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // Clears ADC_CR2_CONT
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; // Software trigger
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = nconv; // Channel count
ADC_Init(ADCx, &ADC_InitStructure);
// enable ADC_MAIN
ADC_Cmd(ADCx, ENABLE); // ADC_CR2_ADON
// enable DMA for ADC
ADC_DMACmd(ADCx, ENABLE); // ADC_CR2_DMA
ADC_DMARequestAfterLastTransferCmd(ADCx, ENABLE); // ADC_CR2_DDS
}
static const stm32_hal_adc_channel ADC_MAIN_channels[] = {
{ADC_CHANNEL_STICK_LH, ADC_SAMPTIME},
{ADC_CHANNEL_STICK_LV, ADC_SAMPTIME},
{ADC_CHANNEL_STICK_RV, ADC_SAMPTIME},
{ADC_CHANNEL_STICK_RH, ADC_SAMPTIME},
#if defined(PCBX10)
{ ADC_CHANNEL_POT1, ADC_SAMPTIME },
{ ADC_CHANNEL_POT2, ADC_SAMPTIME },
{ ADC_CHANNEL_POT3, ADC_SAMPTIME },
{ ADC_CHANNEL_EXT1, ADC_SAMPTIME },
{ ADC_CHANNEL_EXT2, ADC_SAMPTIME },
{ ADC_CHANNEL_EXT3, ADC_SAMPTIME },
{ ADC_CHANNEL_EXT4, ADC_SAMPTIME },
{ ADC_CHANNEL_SLIDER1, ADC_SAMPTIME },
{ ADC_CHANNEL_SLIDER2, ADC_SAMPTIME },
{ ADC_CHANNEL_BATT, ADC_SAMPTIME }
#else
#if defined(RADIO_T8) || defined(RADIO_TLITE) || defined(RADIO_COMMANDO8) || defined(RADIO_LR3PRO)
// fake channels to fill unused POT1/POT2
{0, 0},
{0, 0},
#elif defined(PCBX7) || defined(PCBXLITE)
{ADC_CHANNEL_POT1, ADC_SAMPTIME},
{ADC_CHANNEL_POT2, ADC_SAMPTIME},
#if defined(RADIO_BOXER)
{ADC_CHANNEL_POT3, ADC_SAMPTIME},
#endif
#elif defined(PCBX9LITE)
{ADC_CHANNEL_POT1, ADC_SAMPTIME},
#elif defined(PCBX9E)
{ADC_CHANNEL_POT2, ADC_SAMPTIME},
{ADC_CHANNEL_POT3, ADC_SAMPTIME},
{ADC_CHANNEL_POT4, ADC_SAMPTIME},
{ADC_CHANNEL_SLIDER3, ADC_SAMPTIME},
{ADC_CHANNEL_SLIDER4, ADC_SAMPTIME},
#elif defined(PCBX9D) || defined(PCBX9DP)
{ADC_CHANNEL_POT1, ADC_SAMPTIME},
{ADC_CHANNEL_POT2, ADC_SAMPTIME},
{ADC_CHANNEL_POT3, ADC_SAMPTIME},
{ADC_CHANNEL_SLIDER1, ADC_SAMPTIME},
{ADC_CHANNEL_SLIDER2, ADC_SAMPTIME},
#elif defined(PCBNV14)
{ADC_CHANNEL_POT1, ADC_SAMPTIME},
{ADC_CHANNEL_POT2, ADC_SAMPTIME},
{ADC_CHANNEL_SWA, ADC_SAMPTIME},
{ADC_CHANNEL_SWC, ADC_SAMPTIME},
{ADC_CHANNEL_SWE, ADC_SAMPTIME},
{ADC_CHANNEL_SWF, ADC_SAMPTIME},
{ADC_CHANNEL_SWG, ADC_SAMPTIME},
{ADC_CHANNEL_SWH, ADC_SAMPTIME},
#endif
{ADC_CHANNEL_BATT, ADC_SAMPTIME},
{ADC_Channel_Vbat, ADC_SAMPTIME}
#endif
};
static const stm32_hal_adc_channel* ADC_MAIN_get_channels()
{
#if (NUM_PWMSTICKS > 0)
if (STICKS_PWM_ENABLED())
return ADC_MAIN_channels + 4;
#endif
#if defined(FLYSKY_GIMBAL)
if (globalData.flyskygimbals)
{
return ADC_MAIN_channels + FIRST_ANALOG_ADC_FS;
}
else
#endif
{
return ADC_MAIN_channels + FIRST_ANALOG_ADC;
}
}
static uint8_t ADC_MAIN_get_nconv()
{
#if defined(FLYSKY_GIMBAL)
if (globalData.flyskygimbals)
{
return NUM_ANALOGS_ADC_FS;
}
else
#endif
{
return NUM_ANALOGS_ADC; // based on STICKS_PWM_ENABLED()
}
}
#if defined(ADC_EXT)
#if defined(PCBX9E)
static const stm32_hal_adc_channel ADC_EXT_channels[] = {
{ADC_CHANNEL_POT1, ADC_SAMPTIME},
{ADC_CHANNEL_SLIDER1, ADC_SAMPTIME},
{ADC_CHANNEL_SLIDER2, ADC_SAMPTIME}
};
static uint8_t ADC_EXT_get_nconv() { return NUM_ANALOGS_ADC_EXT; }
#elif defined(PCBX10)
static const stm32_hal_adc_channel ADC_EXT_channels[] = {
{ ADC_Channel_Vbat, ADC_SAMPTIME }
};
static uint8_t ADC_EXT_get_nconv() { return 1; }
#elif defined(PCBNV14)
static const stm32_hal_adc_channel ADC_EXT_channels[] = {
{ADC_CHANNEL_SWB, ADC_SAMPTIME},
{ADC_CHANNEL_SWD, ADC_SAMPTIME},
};
static uint8_t ADC_EXT_get_nconv() { return NUM_ANALOGS_ADC_EXT; }
#endif
static const stm32_hal_adc_channel* ADC_EXT_get_channels()
{
return ADC_EXT_channels;
}
#endif
static uint16_t* ADC_MAIN_get_dma_buffer()
{
#if defined(FLYSKY_GIMBAL)
if (globalData.flyskygimbals)
{
return &adcValues[FIRST_ANALOG_ADC_FS];
}
else
#endif
{
return &adcValues[FIRST_ANALOG_ADC];
}
}
#if defined(ADC_EXT) && defined(ADC_EXT_DMA_Stream)
static uint16_t* ADC_EXT_get_dma_buffer()
{
#if defined(FLYSKY_GIMBAL)
if (globalData.flyskygimbals)
{
return adcValues + NUM_ANALOGS_ADC_FS + FIRST_ANALOG_ADC_FS;
}
else
#endif
{
return adcValues + NUM_ANALOGS_ADC + FIRST_ANALOG_ADC;
}
}
#endif
static const stm32_hal_adc ADC_hal_def[] = {
{
ADC_MAIN,
ADC_DMA_Stream, ADC_DMA_Channel, ADC_MAIN_get_dma_buffer,
ADC_MAIN_get_nconv, ADC_MAIN_get_channels
},
#if defined(ADC_EXT)
{
ADC_EXT,
#if defined(ADC_EXT_DMA_Stream)
ADC_EXT_DMA_Stream, ADC_EXT_DMA_Channel, ADC_EXT_get_dma_buffer,
#else
nullptr, 0, nullptr,
#endif
ADC_EXT_get_nconv, ADC_EXT_get_channels
},
#endif
// required to detect end of ADC definitions
{ nullptr, nullptr, 0, nullptr, nullptr, nullptr },
};
static void adc_init_channels(ADC_TypeDef* adc, const stm32_hal_adc_channel* chan,
uint8_t nconv)
{
if (!chan || !nconv) return;
uint8_t rank = 1;
while (nconv > 0) {
ADC_RegularChannelConfig(adc, chan->adc_channel, rank, chan->sample_time);
nconv--; rank++; chan++;
}
}
static bool adc_init_dma_stream(ADC_TypeDef* adc, DMA_Stream_TypeDef * dma_stream,
uint32_t dma_channel, uint16_t* dest, uint8_t nconv)
{
// Disable DMA before continuing (see ref. manual "Stream configuration procedure")
if (!adc_disable_dma(dma_stream))
return false;
// Clear Interrupt flags
adc_dma_clear_flags(dma_stream);
// setup DMA request
dma_stream->PAR = CONVERT_PTR_UINT(&adc->DR);
dma_stream->M0AR = CONVERT_PTR_UINT(dest);
dma_stream->NDTR = nconv;
// Very high priority, 1 byte transfers, increment memory
dma_stream->CR = DMA_SxCR_PL | dma_channel | DMA_SxCR_MSIZE_0 |
DMA_SxCR_PSIZE_0 | DMA_SxCR_MINC;
// disable direct mode, half full FIFO
dma_stream->FCR = DMA_SxFCR_DMDIS | DMA_SxFCR_FTH_0;
return true;
}
static bool stm32_hal_adc_init()
{
adc_init_pins();
// Init common to all ADCs
ADC_CommonInitTypeDef ADC_CommonInitStruct;
ADC_CommonStructInit(&ADC_CommonInitStruct);
ADC_CommonInitStruct.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInit(&ADC_CommonInitStruct);
const stm32_hal_adc* adc_def = ADC_hal_def;
while (adc_def->adc) {
uint8_t nconv = 0;
if (adc_def->get_nconv) {
nconv = adc_def->get_nconv();
if (nconv > 0) {
adc_setup_scan_mode(adc_def->adc, nconv);
// configure each channel
if (adc_def->get_channels) {
const stm32_hal_adc_channel* chan = adc_def->get_channels();
adc_init_channels(adc_def->adc, chan, nconv);
}
if (adc_def->dma_stream && adc_def->get_dma_buffer) {
uint16_t* dma_buffer = adc_def->get_dma_buffer();
if (!adc_init_dma_stream(adc_def->adc, adc_def->dma_stream,
adc_def->dma_channel, dma_buffer, nconv))
return false;
}
}
}
// move to next ADC definition
adc_def++;
}
//TODO: move VBat & PWM sticks somewhere else.
// Enable vbat sensor
ADC_VBATCmd(ENABLE);
#if NUM_PWMSTICKS > 0
if (STICKS_PWM_ENABLED()) {
sticksPwmInit();
}
#endif
return true;
}
#define DMA_Stream0_IT_MASK (uint32_t)(DMA_LISR_FEIF0 | DMA_LISR_DMEIF0 | \
DMA_LISR_TEIF0 | DMA_LISR_HTIF0 | \
DMA_LISR_TCIF0)
#define DMA_Stream4_IT_MASK (uint32_t)(DMA_HISR_FEIF4 | DMA_HISR_DMEIF4 | \
DMA_HISR_TEIF4 | DMA_HISR_HTIF4 | \
DMA_HISR_TCIF4)
static void adc_dma_clear_flags(DMA_Stream_TypeDef * dma_stream)
{
// no other choice, sorry for that...
if (dma_stream == DMA2_Stream4) {
/* Reset interrupt pending bits for DMA2 Stream4 */
DMA2->HIFCR = DMA_Stream4_IT_MASK;
} else if (dma_stream == DMA2_Stream0) {
/* Reset interrupt pending bits for DMA2 Stream0 */
DMA2->LIFCR = DMA_Stream0_IT_MASK;
}
}
static bool adc_start_dma_conversion(ADC_TypeDef* ADCx,
DMA_Stream_TypeDef * dma_stream)
{
// Disable DMA before continuing (see ref. manual "Stream configuration procedure")
if (!adc_disable_dma(dma_stream))
return false;
// Clear Interrupt flags
adc_dma_clear_flags(dma_stream);
// Clear ADC status register
ADCx->SR &= ~(uint32_t)(ADC_SR_EOC | ADC_SR_STRT | ADC_SR_OVR);
// Enable DMA
dma_stream->CR |= DMA_SxCR_EN;
// Trigger ADC start
ADCx->CR2 |= (uint32_t)ADC_CR2_SWSTART;
return true;
}
static bool adc_disable_dma(DMA_Stream_TypeDef* dma_stream)
{
dma_stream->CR &= ~(uint32_t)DMA_SxCR_EN;
// wait until DMA EN bit gets cleared by hardware
uint16_t uiTimeout = 1000;
while (dma_stream->CR & (uint32_t)DMA_SxCR_EN) {
if (--uiTimeout == 0) {
// Timeout. Failed to disable DMA
return false;
}
}
return true;
}
static void adc_start_single_conversion(ADC_TypeDef* ADCx)
{
ADCx->SR &= ~(uint32_t)(ADC_SR_EOC | ADC_SR_STRT | ADC_SR_OVR);
ADCx->CR2 |= (uint32_t)ADC_CR2_SWSTART;
}
static bool stm32_hal_adc_start_read()
{
const stm32_hal_adc* adc_def = ADC_hal_def;
while (adc_def->adc) {
uint8_t nconv = 0;
if (adc_def->get_nconv) {
nconv = adc_def->get_nconv();
if (nconv > 0) {
if (adc_def->dma_stream && adc_def->get_dma_buffer) {
if (!adc_start_dma_conversion(adc_def->adc, adc_def->dma_stream))
return false;
} else if (nconv == 1) {
adc_start_single_conversion(adc_def->adc);
}
}
}
// move to next ADC definition
adc_def++;
}
return true;
}
static void stm32_hal_adc_wait_completion()
{
//TODO:
// - iterate through ADCs
// - replace with IRQ trigger (both)
// - move RTC batt reading somewhere else
#if defined(ADC_EXT) && defined(ADC_EXT_DMA_Stream)
// Wait for all ADCs to complete
for (unsigned int i=0; i<10000; i++) {
if (DMA_GetFlagStatus(ADC_DMA_Stream, ADC_DMA_TC_Flag) != RESET &&
DMA_GetFlagStatus(ADC_EXT_DMA_Stream, ADC_EXT_TC_Flag) != RESET) {
break;
}
}
//TODO: move into completion IRQ trigger
adc_disable_dma(ADC_DMA_Stream);
adc_disable_dma(ADC_EXT_DMA_Stream);
#else
// Wait only for the main ADC, and hope others are done as well
for (unsigned int i = 0; i < 10000; i++) {
if (DMA_GetFlagStatus(ADC_DMA_Stream, ADC_DMA_TC_Flag) != RESET) {
break;
}
}
//TODO: move into completion IRQ trigger
adc_disable_dma(ADC_DMA_Stream);
#if defined(ADC_EXT) && !defined(ADC_EXT_DMA_Stream)
if (isVBatBridgeEnabled()) {
rtcBatteryVoltage = ADC_EXT->DR;
disableVBatBridge();
}
#endif
#endif
}
const etx_hal_adc_driver_t stm32_hal_adc_driver = {
stm32_hal_adc_init,
stm32_hal_adc_start_read,
stm32_hal_adc_wait_completion
};
@@ -21,33 +21,36 @@
#pragma once
#include "stm32_cmsis.h"
#if defined(__cplusplus)
extern "C" {
#define USE_FULL_LL_DRIVER
#if defined(STM32F4)
#include "CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_bus.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_gpio.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_tim.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_dma.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_usart.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_exti.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_system.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_rcc.h"
#elif defined(STM32F2)
#endif
// Workaround to prevent the other CMSIS header to be included
// and fix some missing items in the old one
#include "thirdparty/CMSIS/Include/cmsis_compiler.h"
#define __CORE_CM3_H_GENERIC
#define USE_FULL_LL_DRIVER
#if defined(STM32F4)
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_bus.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_gpio.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_tim.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_dma.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_usart.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_exti.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_system.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_rcc.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_adc.h"
#include "STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_spi.h"
#elif defined(STM32F2)
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_bus.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_gpio.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_tim.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_dma.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_usart.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_exti.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_system.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_rcc.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_adc.h"
#endif
#include "CMSIS/Device/ST/STM32F2xx/Include/stm32f2xx.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_bus.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_gpio.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_tim.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_dma.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_usart.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_exti.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_system.h"
#include "STM32F2xx_HAL_Driver/Inc/stm32f2xx_ll_rcc.h"
#endif
#if defined(__cplusplus)
}
#endif
@@ -21,25 +21,18 @@
#pragma once
struct stm32_hal_adc_channel {
const uint8_t adc_channel;
const uint8_t sample_time;
#include "stm32_hal_ll.h"
struct stm32_spi_adc_t {
SPI_TypeDef* SPIx;
GPIO_TypeDef* GPIOx;
uint32_t GPIO_Pins;
uint32_t GPIO_AF;
uint32_t GPIO_CS;
const uint8_t* channels;
uint8_t n_channels;
};
typedef const stm32_hal_adc_channel* (*stm32_hal_adc_get_channels)();
typedef uint8_t (*stm32_hal_adc_get_nconv)();
typedef uint16_t* (*stm32_hal_adc_get_dma_buffer)();
#define _SPI_ADC_CS_HIGH(adc) LL_GPIO_SetOutputPin(adc->GPIOx, adc->GPIO_CS)
#define _SPI_ADC_CS_LOW(adc) LL_GPIO_ResetOutputPin(adc->GPIOx, adc->GPIO_CS)
struct stm32_hal_adc {
ADC_TypeDef* adc;
DMA_Stream_TypeDef* dma_stream;
uint32_t dma_channel;
const stm32_hal_adc_get_dma_buffer get_dma_buffer;
const stm32_hal_adc_get_nconv get_nconv;
const stm32_hal_adc_get_channels get_channels;
};
// Driver to be passed to adcInit()
extern const etx_hal_adc_driver_t stm32_hal_adc_driver;
@@ -0,0 +1,128 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*/
#include "stm32_switch_driver.h"
#include "hal/adc_driver.h"
bool stm32_switch_get_state(const stm32_switch_t* sw, SwitchHwPos pos)
{
switch (sw->type) {
case SWITCH_HW_2POS:
switch (pos) {
case SWITCH_HW_UP:
if (sw->flags & SWITCH_HW_INVERTED)
return !LL_GPIO_IsInputPinSet(sw->GPIOx_high, sw->Pin_high);
else
return LL_GPIO_IsInputPinSet(sw->GPIOx_high, sw->Pin_high);
case SWITCH_HW_DOWN:
if (sw->flags & SWITCH_HW_INVERTED)
return LL_GPIO_IsInputPinSet(sw->GPIOx_high, sw->Pin_high);
else
return !LL_GPIO_IsInputPinSet(sw->GPIOx_high, sw->Pin_high);
default:
return false;
}
break;
case SWITCH_HW_3POS: {
auto hi = LL_GPIO_IsInputPinSet(sw->GPIOx_high, sw->Pin_high);
auto lo = LL_GPIO_IsInputPinSet(sw->GPIOx_low, sw->Pin_low);
switch (pos) {
case SWITCH_HW_UP:
if (sw->flags & SWITCH_HW_INVERTED)
return !hi && lo;
else
return hi && !lo;
case SWITCH_HW_DOWN:
if (sw->flags & SWITCH_HW_INVERTED)
return hi && !lo;
else
return !hi && lo;
case SWITCH_HW_MID:
return hi && lo;
default:
return false;
}
} break;
case SWITCH_HW_ADC: {
uint16_t value = getAnalogValue(sw->Pin_high);
switch (pos) {
case SWITCH_HW_UP:
return value < 1024;
case SWITCH_HW_DOWN:
return value > 3 * 1024;
case SWITCH_HW_MID:
return (value >= 1024) && (value <= 3 * 1024);
}
} break;
default:
break;
}
return false;
}
SwitchHwPos stm32_switch_get_position(const stm32_switch_t* sw)
{
bool inv = sw->flags & SWITCH_HW_INVERTED;
SwitchHwPos ret = SWITCH_HW_UP;
switch (sw->type) {
case SWITCH_HW_2POS:
if (!LL_GPIO_IsInputPinSet(sw->GPIOx_high, sw->Pin_high))
ret = SWITCH_HW_DOWN;
break;
case SWITCH_HW_3POS: {
auto hi = LL_GPIO_IsInputPinSet(sw->GPIOx_high, sw->Pin_high);
auto lo = LL_GPIO_IsInputPinSet(sw->GPIOx_low, sw->Pin_low);
if (hi && lo)
ret = SWITCH_HW_MID;
else if (!hi && lo)
ret = SWITCH_HW_DOWN;
} break;
case SWITCH_HW_ADC: {
uint16_t value = getAnalogValue(sw->Pin_high);
if (value > 3 * 1024)
ret = SWITCH_HW_DOWN;
else if (value >= 1024)
ret = SWITCH_HW_MID;
} break;
}
if (inv) {
return ret == SWITCH_HW_UP ? SWITCH_HW_DOWN
: ret == SWITCH_HW_DOWN ? SWITCH_HW_UP
: ret;
}
return ret;
}
@@ -0,0 +1,54 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*/
#pragma once
#include "stm32_hal_ll.h"
#include "hal/switch_driver.h"
struct stm32_switch_t
{
const char* name;
GPIO_TypeDef* GPIOx_high;
uint32_t Pin_high;
GPIO_TypeDef* GPIOx_low;
uint32_t Pin_low;
SwitchHwType type;
uint8_t flags;
};
#define SWITCH_HW_DEF_3POS(x, flags) \
{ \
SWITCHES_GPIO_REG_##x##_H, SWITCHES_GPIO_PIN_##x##_H, \
SWITCHES_GPIO_REG_##x##_L, SWITCHES_GPIO_PIN_##x##_L, \
SWITCH_HW_3POS, (flags) \
}
#define SWITCH_HW_DEF_2POS(x, flags) \
{ \
SWITCHES_GPIO_REG_##x, SWITCHES_GPIO_PIN_##x, \
nullptr, 0, SWITCH_HW_2POS, (flags) \
}
SwitchHwPos stm32_switch_get_position(const stm32_switch_t* sw);
bool stm32_switch_get_state(const stm32_switch_t* sw, SwitchHwPos pos);
@@ -318,9 +318,7 @@ void stm32_usart_deinit(const stm32_usart_t* usart)
LL_GPIO_InitTypeDef pinInit;
LL_GPIO_StructInit(&pinInit);
pinInit.Pin = usart->GPIO_Pin;
pinInit.Mode = LL_GPIO_MODE_OUTPUT;
pinInit.Pull = LL_GPIO_PULL_NO;
pinInit.Alternate = LL_GPIO_AF_0;
pinInit.Mode = LL_GPIO_MODE_INPUT;
LL_GPIO_Init(usart->GPIOx, &pinInit);
LL_GPIO_ResetOutputPin(usart->GPIOx, pinInit.Pin);
}
@@ -20,6 +20,7 @@
*/
#include "opentx.h"
#include "watchdog_driver.h"
static volatile uint32_t msTickCount; // Used to get 1 kHz counter
@@ -24,9 +24,9 @@
#if defined(SIMU)
uint16_t getTmr2MHz();
#define watchdogSuspend(timeout)
#else
#else // SIMU
#include "hal.h"
@@ -34,7 +34,7 @@ void init2MhzTimer();
void init1msTimer();
void stop1msTimer();
static inline uint16_t getTmr2MHz() { return TIMER_2MHz_TIMER->CNT; }
#define getTmr2MHz() TIMER_2MHz_TIMER->CNT
void watchdogSuspend(uint32_t timeout);
@@ -39,7 +39,6 @@ extern "C" {
#include "board.h"
#include "debug.h"
#include "debounce.h"
static bool usbDriverStarted = false;
#if defined(BOOT)
@@ -60,8 +59,17 @@ void setSelectedUsbMode(int mode)
int usbPlugged()
{
static PinDebounce debounce;
return debounce.debounce(USB_GPIO, USB_GPIO_PIN_VBUS);
static uint8_t debouncedState = 0;
static uint8_t lastState = 0;
uint8_t state = GPIO_ReadInputDataBit(USB_GPIO, USB_GPIO_PIN_VBUS);
if (state == lastState)
debouncedState = state;
else
lastState = state;
return debouncedState;
}
USB_OTG_CORE_HANDLE USB_OTG_dev;
@@ -22,22 +22,33 @@
#pragma once
#define WDG_DURATION 500 /*ms*/
void watchdogInit(unsigned int duration);
#if defined(SIMU)
#define WAS_RESET_BY_WATCHDOG() (false)
#define WAS_RESET_BY_SOFTWARE() (false)
#define WAS_RESET_BY_WATCHDOG_OR_SOFTWARE() (false)
#define WAS_RESET_BY_WATCHDOG() (false)
#define WAS_RESET_BY_SOFTWARE() (false)
#define WAS_RESET_BY_WATCHDOG_OR_SOFTWARE() (false)
#define WDG_ENABLE(x)
#define WDG_RESET()
#else // SIMU
#if defined(WATCHDOG)
#define WDG_ENABLE(x) watchdogInit(x)
#define WDG_RESET() IWDG->KR = 0xAAAA
#else
#define WDG_ENABLE(x)
#define WDG_RESET()
#else
#if defined(WATCHDOG)
#define WDG_ENABLE(x) watchdogInit(x)
#define WDG_RESET() IWDG->KR = 0xAAAA
#else
#define WDG_ENABLE(x)
#define WDG_RESET()
#endif
#define WAS_RESET_BY_WATCHDOG() (RCC->CSR & (RCC_CSR_WDGRSTF | RCC_CSR_WWDGRSTF))
#define WAS_RESET_BY_SOFTWARE() (RCC->CSR & RCC_CSR_SFTRSTF)
#define WAS_RESET_BY_WATCHDOG_OR_SOFTWARE() (RCC->CSR & (RCC_CSR_WDGRSTF | RCC_CSR_WWDGRSTF | RCC_CSR_SFTRSTF))
#endif
#define WAS_RESET_BY_WATCHDOG() \
(RCC->CSR & (RCC_CSR_WDGRSTF | RCC_CSR_WWDGRSTF))
#define WAS_RESET_BY_SOFTWARE() (RCC->CSR & RCC_CSR_SFTRSTF)
#define WAS_RESET_BY_WATCHDOG_OR_SOFTWARE() \
(RCC->CSR & (RCC_CSR_WDGRSTF | RCC_CSR_WWDGRSTF | RCC_CSR_SFTRSTF))
#endif