1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-24 00:35:18 +03:00

Working ADC3 driver for 9XE additional analog inputs (driver source based on Michael Blandford's code)

This commit is contained in:
Damjan Adamic 2014-12-19 18:13:03 +01:00
parent cf87849c5b
commit 6304679582
12 changed files with 164 additions and 44 deletions

View file

@ -90,11 +90,13 @@ const pm_uchar icons[] PROGMEM = {
#define ICON_REBOOT 91, 11
#define ICON_ALTITUDE 102, 9
#define POT_SPACING 5
void drawPotsBars()
{
// Optimization by Mike Blandford
uint8_t x, i, len ; // declare temporary variables
for (x=LCD_W/2-5, i=NUM_STICKS; i<NUM_STICKS+NUM_POTS; x+=5, i++) {
for (x=LCD_W/2-(NUM_POTS/2)*POT_SPACING, i=NUM_STICKS; i<NUM_STICKS+NUM_POTS; x+=POT_SPACING, i++) {
if (IS_POT_AVAILABLE(i)) {
len = ((calibratedStick[i]+RESX)*BAR_HEIGHT/(RESX*2))+1l; // calculate once per loop
V_BAR(x, LCD_H-8, len)
@ -201,11 +203,15 @@ void displayTrims(uint8_t phase)
void displaySliders()
{
for (uint8_t i=NUM_STICKS; i<NUM_STICKS+NUM_POTS; i++) {
if (i == POT3) {
continue;
}
#if defined(REV9E)
if (i < SLIDER1) continue; // TODO change and display more values
coord_t x = ((i==SLIDER1 || i==SLIDER3) ? 3 : LCD_W-5);
int8_t y = (i<SLIDER3 ? LCD_H/2+1 : 1);
#else
if (i == POT3) continue;
coord_t x = ((i==POT1 || i==SLIDER1) ? 3 : LCD_W-5);
int8_t y = (i>=SLIDER1 ? LCD_H/2+1 : 1);
#endif
lcd_vline(x, y, LCD_H/2-2);
lcd_vline(x+1, y, LCD_H/2-2);
y += LCD_H/2-4;

View file

@ -51,10 +51,18 @@ void menuGeneralDiagAna(uint8_t event)
STICK_SCROLL_DISABLE();
for (uint8_t i=0; i<NUM_STICKS+NUM_POTS; i++) {
#if (NUM_STICKS+NUM_POTS) > 9
coord_t y = MENU_TITLE_HEIGHT + 1 + (i/3)*FH;
const uint8_t x_coord[] = {0, 70, 154};
uint8_t x = x_coord[i%3];
lcd_outdezNAtt(x, y, i+1, LEADING0|LEFT, 2);
lcd_putc(x+2*FW-2, y, ':');
#else
coord_t y = MENU_TITLE_HEIGHT + 1 + (i/2)*FH;
uint8_t x = i&1 ? 64+5 : 0;
putsStrIdx(x, y, PSTR("A"), i+1);
lcd_putc(lcdNextPos, y, ':');
#endif
lcd_outhex4(x+3*FW-1, y, anaIn(i));
lcd_outdez8(x+10*FW-1, y, (int16_t)calibratedStick[CONVERT_MODE(i)]*25/256);
}

View file

@ -47,8 +47,11 @@ enum menuGeneralHwItems {
ITEM_SETUP_HW_POT1,
ITEM_SETUP_HW_POT2,
CASE_REVPLUS(ITEM_SETUP_HW_POT3)
CASE_REV9E(ITEM_SETUP_HW_POT4)
ITEM_SETUP_HW_LS,
ITEM_SETUP_HW_RS,
CASE_REV9E(ITEM_SETUP_HW_LS2)
CASE_REV9E(ITEM_SETUP_HW_RS2)
ITEM_SETUP_HW_LABEL_SWITCHES,
ITEM_SETUP_HW_SA,
ITEM_SETUP_HW_SB,
@ -111,6 +114,10 @@ void menuGeneralHardware(uint8_t event)
case ITEM_SETUP_HW_STICK4:
case ITEM_SETUP_HW_LS:
case ITEM_SETUP_HW_RS:
#if defined(REV9E)
case ITEM_SETUP_HW_LS2:
case ITEM_SETUP_HW_RS2:
#endif
{
int idx = (k<=ITEM_SETUP_HW_STICK4 ? k-ITEM_SETUP_HW_STICK1 : k-ITEM_SETUP_HW_LS+7);
lcd_putsiAtt(INDENT_WIDTH, y, STR_VSRCRAW, idx+1, 0);
@ -127,6 +134,9 @@ void menuGeneralHardware(uint8_t event)
case ITEM_SETUP_HW_POT2:
#if defined(REVPLUS)
case ITEM_SETUP_HW_POT3:
#endif
#if defined(REV9E)
case ITEM_SETUP_HW_POT4:
#endif
{
int idx = k - ITEM_SETUP_HW_POT1;

View file

@ -117,8 +117,13 @@
#define MAX_SCRIPTS 7
#define MAX_INPUTS 32
#define NUM_TRAINER 16
#if defined(REV9E)
#define NUM_POTS 8
#define NUM_XPOTS 4
#else
#define NUM_POTS 5
#define NUM_XPOTS 3
#endif
#elif defined(CPUARM)
#define MAX_MODELS 60
#define NUM_CHNOUT 32 // number of real output channels CH1-CH32
@ -343,7 +348,7 @@ enum uartModes {
#define EXTRA_GENERAL_FIELDS \
EXTRA_GENERAL_FIELDS_ARM \
uint8_t uart3Mode; \
uint8_t potsType; \
uint8_t potsType; /*two bits for every pot*/\
uint8_t backlightColor;
#elif defined(CPUARM)
#define EXTRA_GENERAL_FIELDS EXTRA_GENERAL_FIELDS_ARM
@ -391,7 +396,11 @@ PACK(typedef struct {
#define IS_TRAINER_EXTERNAL_MODULE() (g_model.trainerMode == TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE || g_model.trainerMode == TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE)
#define MODELDATA_BITMAP char bitmap[LEN_BITMAP_NAME];
#define MODELDATA_EXTRA uint8_t externalModule; uint8_t trainerMode; ModuleData moduleData[NUM_MODULES+1]; char curveNames[MAX_CURVES][6]; ScriptData scriptsData[MAX_SCRIPTS]; char inputNames[MAX_INPUTS][LEN_INPUT_NAME]; uint8_t nPotsToWarn; int8_t potPosition[NUM_POTS];
#if defined(REV9E)
#define swarnstate_t uint64_t
#else
#define swarnstate_t uint32_t
#endif
#define swarnenable_t uint16_t
#elif defined(PCBSKY9X)
enum ModuleIndex {
@ -1749,9 +1758,18 @@ enum MixSources {
MIXSRC_POT1 = MIXSRC_FIRST_POT, LUA_EXPORT("s1", "Potentiometer 1")
MIXSRC_POT2, LUA_EXPORT("s2", "Potentiometer 2")
MIXSRC_POT3, LUA_EXPORT("s3", "Potentiometer 3")
#if defined(REV9E)
MIXSRC_POT4, LUA_EXPORT("s4", "Potentiometer 4")
#endif
MIXSRC_SLIDER1, LUA_EXPORT("ls", "Left slider")
MIXSRC_SLIDER2, LUA_EXPORT("rs", "Right slider")
#if defined(REV9E)
MIXSRC_SLIDER3, LUA_EXPORT("ls2", "Left center slider")
MIXSRC_SLIDER4, LUA_EXPORT("rs2", "Right center slider")
MIXSRC_LAST_POT = MIXSRC_SLIDER4,
#else
MIXSRC_LAST_POT = MIXSRC_SLIDER2,
#endif
#else
MIXSRC_P1 = MIXSRC_FIRST_POT,
MIXSRC_P2,

View file

@ -1410,7 +1410,7 @@ void getADC()
for (uint32_t i=0; i<4; i++) {
adcRead();
for (uint32_t x=0; x<NUMBER_ANALOG; x++) {
temp[x] += Analog_values[x];
temp[x] += getAnalogValue(x);
}
#if defined(PCBTARANIS)
if (calibrationState) break;

View file

@ -956,9 +956,18 @@ enum Analogs {
POT1,
POT2,
POT3,
#if defined(REV9E)
POT4,
POT_LAST = POT4,
#else
POT_LAST = POT3,
#endif
SLIDER1,
SLIDER2,
#if defined(REV9E)
SLIDER3,
SLIDER4,
#endif
#else
POT1,
POT2,

View file

@ -36,7 +36,7 @@
#include "../opentx.h"
volatile uint16_t Analog_values[NUMBER_ANALOG];
uint16_t Analog_values[NUMBER_ANALOG];
#if defined(FRSKY_STICKS)
const char ana_direction[NUMBER_ANALOG] = {1, 1, 0, 1 ,0 ,1 ,0, 0, 0};
@ -127,3 +127,8 @@ void adcRead()
}
#endif
}
uint16_t getAnalogValue(uint32_t value)
{
return Analog_values[value];
}

View file

@ -235,7 +235,7 @@ void disable_dsm2(uint32_t port);
// ADC driver
void adcInit();
void adcRead(void);
//extern uint16_t Analog_values[];
inline uint16_t getAnalogValue(uint32_t value);
// Buzzer driver
void buzzerSound(uint8_t duration);

View file

@ -56,12 +56,20 @@
#define SLIDE_R 15
#define BATTERY 10
#if defined(REV9E)
#define SLIDER_L2 8 // PIN_FLP_J3 (SLIDER3) = ANC3_IN8
#define SLIDER_R2 7 // PIN_FLP_J4 (SLIDER4) = ADC3_IN7
#define POT_4 6 // PIN_FLP_J5 (POT4) = ADC3_IN6
#endif
// Sample time should exceed 1uS
#define SAMPTIME 2 // sample time = 15 cycles
#define SAMPTIME 2 // sample time = 28 cycles
volatile uint16_t Analog_values[NUMBER_ANALOG];
uint16_t Analog_values[NUMBER_ANALOG];
#if defined(REVPLUS)
#if defined(REV9E)
const int8_t ana_direction[NUMBER_ANALOG] = {1,-1,1,-1, -1,1,-1, -1,1, 1, -1,-1,1};
#elif defined(REVPLUS)
const int8_t ana_direction[NUMBER_ANALOG] = {1,-1,1,-1, -1,1,-1, -1,1, 1};
#elif defined(REV4a)
const int8_t ana_direction[NUMBER_ANALOG] = {1,-1,1,-1, -1,-1,0, -1,1, 1};
@ -69,6 +77,24 @@ volatile uint16_t Analog_values[NUMBER_ANALOG];
const int8_t ana_direction[NUMBER_ANALOG] = {1,-1,1,-1, -1,1,0, -1,1, 1};
#endif
#if defined(REV9E)
#define NUMBER_ANALOG_ADC1 10
#define NUMBER_ANALOG_ADC3 3
// mapping from Analog_values order to enum Analogs
const uint8_t ana_mapping[NUMBER_ANALOG] = { 0 /*STICK1*/, 1 /*STICK2*/, 2 /*STICK3*/, 3 /*STICK4*/,
4 /*POT1*/, 5 /*POT2*/, 6 /*POT3*/, 12 /*POT4*/,
7 /*SLIDER1*/, 8 /*SLIDER2*/, 10 /*SLIDER3*/, 11 /*SLIDER4*/,
9 /*TX_VOLTAGE*/ };
#else
#define NUMBER_ANALOG_ADC1 10
#define NUMBER_ANALOG_ADC3 0
// mapping from Analog_values order to enum Analogs
const uint8_t ana_mapping[NUMBER_ANALOG] = { 0 /*STICK1*/, 1 /*STICK2*/, 2 /*STICK3*/, 3 /*STICK4*/,
4 /*POT1*/, 5 /*POT2*/, 6 /*POT3*/,
7 /*SLIDER1*/, 8 /*SLIDER2*/,
9 /*TX_VOLTAGE*/ };
#endif
void adcInit()
{
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // Enable clock
@ -83,7 +109,9 @@ void adcInit()
PIN_FLP_J1, PIN_ANALOG | PIN_PORTA);
#endif
#if defined(REVPLUS)
#if defined(REV9E)
configure_pins(PIN_FLP_J2 | PIN_FLP_J6, PIN_ANALOG|PIN_PORTB);
#elif defined(REVPLUS)
configure_pins(PIN_FLP_J2 | PIN_FLP_J3, PIN_ANALOG|PIN_PORTB);
#elif !defined(REV3)
configure_pins(PIN_FLP_J2, PIN_ANALOG|PIN_PORTB);
@ -93,7 +121,7 @@ void adcInit()
ADC1->CR1 = ADC_CR1_SCAN;
ADC1->CR2 = ADC_CR2_ADON | ADC_CR2_DMA | ADC_CR2_DDS;
ADC1->SQR1 = (NUMBER_ANALOG-1) << 20 ; // bits 23:20 = number of conversions
ADC1->SQR1 = (NUMBER_ANALOG_ADC1-1) << 20 ; // bits 23:20 = number of conversions
ADC1->SQR2 = (POT_XTRA<<0) + (SLIDE_L<<5) + (SLIDE_R<<10) + (BATTERY<<15); // conversions 7 and more
ADC1->SQR3 = (STICK_LH<<0) + (STICK_LV<<5) + (STICK_RV<<10) + (STICK_RH<<15) + (POT_L<<20) + (POT_R<<25); // conversions 1 to 6
ADC1->SMPR1 = SAMPTIME + (SAMPTIME<<3) + (SAMPTIME<<6) + (SAMPTIME<<9) + (SAMPTIME<<12) + (SAMPTIME<<15) + (SAMPTIME<<18) + (SAMPTIME<<21) + (SAMPTIME<<24);
@ -104,21 +132,45 @@ void adcInit()
DMA2_Stream0->CR = DMA_SxCR_PL | DMA_SxCR_MSIZE_0 | DMA_SxCR_PSIZE_0 | DMA_SxCR_MINC;
DMA2_Stream0->PAR = CONVERT_PTR_UINT(&ADC1->DR);
DMA2_Stream0->M0AR = CONVERT_PTR_UINT(Analog_values);
DMA2_Stream0->NDTR = NUMBER_ANALOG_ADC1;
DMA2_Stream0->FCR = DMA_SxFCR_DMDIS | DMA_SxFCR_FTH_0 ;
#if defined(REV9E)
RCC->APB2ENR |= RCC_APB2ENR_ADC3EN ; // Enable clock
configure_pins( PIN_FLP_J3 | PIN_FLP_J4 | PIN_FLP_J5, PIN_ANALOG | PIN_PORTF ) ;
ADC3->CR1 = ADC_CR1_SCAN ;
ADC3->CR2 = ADC_CR2_ADON | ADC_CR2_DMA | ADC_CR2_DDS ;
ADC3->SQR1 = (NUMBER_ANALOG_ADC3-1) << 20 ; // NUMBER_ANALOG Channels
ADC3->SQR2 = 0;
ADC3->SQR3 = (SLIDER_L2<<0) + (SLIDER_R2<<5) + (POT_4<<10) ; // conversions 1 to 3
ADC3->SMPR1 = SAMPTIME + (SAMPTIME<<3) + (SAMPTIME<<6);
ADC3->SMPR2 = 0;
// Enable the DMA channel here, DMA2 stream 1, channel 2
DMA2_Stream1->CR = DMA_SxCR_PL | DMA_SxCR_CHSEL_1 | DMA_SxCR_MSIZE_0 | DMA_SxCR_PSIZE_0 | DMA_SxCR_MINC;
DMA2_Stream1->PAR = CONVERT_PTR_UINT(&ADC3->DR);
DMA2_Stream1->M0AR = CONVERT_PTR_UINT(Analog_values + NUMBER_ANALOG_ADC1);
DMA2_Stream1->NDTR = NUMBER_ANALOG_ADC3;
DMA2_Stream1->FCR = DMA_SxFCR_DMDIS | DMA_SxFCR_FTH_0 ;
#endif // #if defined(REV9E)
}
void adcRead()
{
uint32_t i ;
DMA2_Stream0->CR &= ~DMA_SxCR_EN ; // Disable DMA
ADC1->SR &= ~(uint32_t) ( ADC_SR_EOC | ADC_SR_STRT | ADC_SR_OVR ) ;
DMA2->LIFCR = DMA_LIFCR_CTCIF0 | DMA_LIFCR_CHTIF0 |DMA_LIFCR_CTEIF0 | DMA_LIFCR_CDMEIF0 | DMA_LIFCR_CFEIF0 ; // Write ones to clear bits
DMA2_Stream0->M0AR = CONVERT_PTR_UINT(Analog_values);
DMA2_Stream0->NDTR = NUMBER_ANALOG ;
DMA2_Stream0->CR |= DMA_SxCR_EN ; // Enable DMA
ADC1->CR2 |= (uint32_t)ADC_CR2_SWSTART ;
for (i=0; i<10000; i++) {
#if defined(REV9E)
DMA2_Stream1->CR &= ~DMA_SxCR_EN ; // Disable DMA
ADC3->SR &= ~(uint32_t) ( ADC_SR_EOC | ADC_SR_STRT | ADC_SR_OVR ) ;
DMA2->LIFCR = DMA_LIFCR_CTCIF1 | DMA_LIFCR_CHTIF1 |DMA_LIFCR_CTEIF1 | DMA_LIFCR_CDMEIF1 | DMA_LIFCR_CFEIF1 ; // Write ones to clear bits
DMA2_Stream1->CR |= DMA_SxCR_EN ; // Enable DMA
ADC3->CR2 |= (uint32_t)ADC_CR2_SWSTART ;
#endif // #if defined(REV9E)
for (unsigned int i=0; i<10000; i++) {
if (DMA2->LISR & DMA_LISR_TCIF0) {
break;
}
@ -127,7 +179,7 @@ void adcRead()
#if !defined(REV3)
// adc direction correct
for (i=0; i<NUMBER_ANALOG; i++) {
for (uint32_t i=0; i<NUMBER_ANALOG; i++) {
if (ana_direction[i] < 0) {
Analog_values[i] = 4096-Analog_values[i];
}
@ -144,3 +196,8 @@ void adcRead()
void adcStop()
{
}
uint16_t getAnalogValue(uint32_t value)
{
return Analog_values[ana_mapping[value]];
}

View file

@ -192,7 +192,8 @@ void watchdogInit(unsigned int duration);
// ADC driver
void adcInit(void);
void adcRead(void);
extern volatile uint16_t Analog_values[];
inline uint16_t getAnalogValue(uint32_t value);
#if defined(REV3)
#define BATT_SCALE 120
#else

View file

@ -218,7 +218,12 @@
#else
#define PIN_FLP_J2 GPIO_Pin_0 // PB.00
#endif
#if defined(REVPLUS)
#if defined(REV9E)
#define PIN_FLP_J3 GPIO_Pin_10 // PF.10
#define PIN_FLP_J4 GPIO_Pin_9 // PF.09
#define PIN_FLP_J5 GPIO_Pin_8 // PF.08
#define PIN_FLP_J6 GPIO_Pin_1 // PB.01
#elif defined(REVPLUS)
#define PIN_FLP_J3 GPIO_Pin_1 // PB.01
#endif
#define PIN_MVOLT GPIO_Pin_0 // PC.00
@ -260,6 +265,7 @@
#define PIN_PORTC 0x0200
#define PIN_PORTD 0x0300
#define PIN_PORTE 0x0400
#define PIN_PORTF 0x0500
#define PIN_PER_1 0x0010
#define PIN_PER_2 0x0020
#define PIN_PER_3 0x0030

View file

@ -428,7 +428,7 @@
#define LEN_VSRCRAW "\004"
#if defined(PCBTARANIS) && defined(REV9E)
#define TR_POTS_VSRCRAW "S1\0 ""S2\0 ""S3\0 ""LS\0 ""RS\0 "
#define TR_POTS_VSRCRAW "S1\0 ""S2\0 ""S3\0 ""S4\0 ""LS\0 ""RS\0 ""LS2\0""RS2\0"
#define TR_SW_VSRCRAW "SA\0 ""SB\0 ""SC\0 ""SD\0 ""SE\0 ""SF\0 ""SG\0 ""SH\0 ""SI\0 ""SJ\0 ""SK\0 ""SL\0 ""SM\0 ""SN\0 ""SO\0 ""SP\0 ""SQ\0 ""SR\0 "
#elif defined(PCBTARANIS)
#define TR_POTS_VSRCRAW "S1\0 ""S2\0 ""S3\0 ""LS\0 ""RS\0 "
@ -471,7 +471,7 @@
#endif
#if defined(PCBTARANIS)
#define TR_6POS_POTS "S11""S12""S13""S14""S15""S16""S21""S22""S23""S24""S25""S26""S31""S32""S33""S34""S35""S36"
#define TR_6POS_POTS "S11""S12""S13""S14""S15""S16""S21""S22""S23""S24""S25""S26""S31""S32""S33""S34""S35""S36""S41""S42""S43""S44""S45""S46"
#define TR_VSWITCHES "---" TR_PHYS_SWITCHES TR_6POS_POTS TR_TRIMS_SWITCHES TR_ROTENC_SWITCHES TR_LOGICALSW "ON\0""One"
#else
#define TR_VSWITCHES "---" TR_9X_3POS_SWITCHES TR_PHYS_SWITCHES TR_TRIMS_SWITCHES TR_ROTENC_SWITCHES TR_LOGICALSW "ON\0""One"