MD-9600: using STM32 ADC driver

This commit is contained in:
Silvano Seva 2024-11-10 12:11:25 +01:00
parent 10d7ded93f
commit e1572f8eb5
4 changed files with 36 additions and 19 deletions

View File

@ -26,7 +26,7 @@
#include <interfaces/delays.h> #include <interfaces/delays.h>
#include <interfaces/keyboard.h> #include <interfaces/keyboard.h>
#include <interfaces/platform.h> #include <interfaces/platform.h>
#include <ADC1_MDx.h> #include <adc_stm32.h>
#include "hwconfig.h" #include "hwconfig.h"
/** /**
@ -178,8 +178,8 @@ keyboard_t kbd_getKeys()
*/ */
/* Retrieve row/coloumn voltage measurements. */ /* Retrieve row/coloumn voltage measurements. */
uint16_t row = ((uint16_t) adc1_getMeasurement(ADC_SW2_CH) + 0.5f); uint16_t row = ((uint16_t) adc_getVoltage(&adc1, ADC_SW2_CH) / 1000);
uint16_t col = ((uint16_t) adc1_getMeasurement(ADC_SW1_CH) + 0.5f); uint16_t col = ((uint16_t) adc_getVoltage(&adc1, ADC_SW1_CH) / 1000);
/* Map row voltage to row index. */ /* Map row voltage to row index. */
uint8_t rowIdx = 0xFF; uint8_t rowIdx = 0xFF;

View File

@ -20,8 +20,11 @@
#include <hwconfig.h> #include <hwconfig.h>
#include <spi_stm32.h> #include <spi_stm32.h>
#include <adc_stm32.h>
#include <pthread.h> #include <pthread.h>
static pthread_mutex_t spi2Mutex; static pthread_mutex_t spi2Mutex;
static pthread_mutex_t adcMutex;
SPI_STM32_DEVICE_DEFINE(spi2, SPI2, &spi2Mutex) SPI_STM32_DEVICE_DEFINE(spi2, SPI2, &spi2Mutex)
ADC_STM32_DEVICE_DEFINE(adc1, ADC1, &adcMutex, 3300000)

View File

@ -28,7 +28,20 @@
extern "C" { extern "C" {
#endif #endif
enum adcChannel
{
ADC_VOL_CH = 0,
ADC_VBAT_CH = 1,
ADC_VOX_CH = 3,
ADC_RSSI_CH = 8,
ADC_SW1_CH = 7,
ADC_SW2_CH = 6,
ADC_RSSI2_CH = 9,
ADC_HTEMP_CH = 15
};
extern const struct spiDevice spi2; extern const struct spiDevice spi2;
extern const struct Adc adc1;
/* Device has a working real time clock */ /* Device has a working real time clock */
#define CONFIG_RTC #define CONFIG_RTC

View File

@ -24,7 +24,7 @@
#include <interfaces/delays.h> #include <interfaces/delays.h>
#include <hwconfig.h> #include <hwconfig.h>
#include <string.h> #include <string.h>
#include <ADC1_MDx.h> #include <adc_stm32.h>
#include <calibInfo_MDx.h> #include <calibInfo_MDx.h>
#include <toneGenerator_MDx.h> #include <toneGenerator_MDx.h>
#include <peripherals/rtc.h> #include <peripherals/rtc.h>
@ -53,12 +53,16 @@ void platform_init()
gpio_setMode(PTT_SW, INPUT); gpio_setMode(PTT_SW, INPUT);
/* gpio_setMode(AIN_VBAT, ANALOG);
* Initialise ADC1, for vbat, RSSI, ... gpio_setMode(AIN_MIC, ANALOG);
* Configuration of corresponding GPIOs in analog input mode is done inside gpio_setMode(AIN_RSSI, ANALOG);
* the driver. gpio_setMode(AIN_SW2, ANALOG);
*/ gpio_setMode(AIN_SW1, ANALOG);
adc1_init(); gpio_setMode(AIN_RSSI2, ANALOG);
gpio_setMode(AIN_HTEMP, ANALOG);
/* Initialise ADC1, for vbat, RSSI, ... */
adcStm32_init(&adc1);
/* /*
* Initialise SPI2 for external flash and LCD * Initialise SPI2 for external flash and LCD
@ -78,7 +82,7 @@ void platform_init()
void platform_terminate() void platform_terminate()
{ {
/* Shut down all the modules */ /* Shut down all the modules */
adc1_terminate(); adcStm32_terminate(&adc1);
toneGen_terminate(); toneGen_terminate();
chSelector_terminate(); chSelector_terminate();
audio_terminate(); audio_terminate();
@ -98,19 +102,16 @@ uint16_t platform_getVbat()
{ {
/* /*
* Battery voltage is measured through an 1:5.7 voltage divider and * Battery voltage is measured through an 1:5.7 voltage divider and
* adc1_getMeasurement returns a value in mV. To have effective battery * adc1_getMeasurement returns a value in uV.
* voltage we have to multiply by the ratio: with a simple trick we can do
* it also without using floats and with a maximum error of -1mV.
*/ */
uint32_t vbat = adc_getVoltage(&adc1, ADC_VBAT_CH) * 57;
uint16_t vbat = adc1_getMeasurement(ADC_VBAT_CH); return vbat / 10000;
return (vbat * 6) - ((vbat * 3) / 10);
} }
uint8_t platform_getMicLevel() uint8_t platform_getMicLevel()
{ {
/* Value from ADC is 12 bit wide: shift right by four to get 0 - 255 */ /* Value from ADC is 12 bit wide: shift right by four to get 0 - 255 */
return (adc1_getRawSample(ADC_VOX_CH) >> 4); return adc_getRawSample(&adc1, ADC_VOX_CH) >> 4;
} }
uint8_t platform_getVolumeLevel() uint8_t platform_getVolumeLevel()
@ -120,7 +121,7 @@ uint8_t platform_getVolumeLevel()
* converted to a value in range 0 - 255 using fixed point math: divide by * converted to a value in range 0 - 255 using fixed point math: divide by
* 1600 and then multiply by 256. * 1600 and then multiply by 256.
*/ */
uint16_t value = adc1_getMeasurement(ADC_VOL_CH); uint16_t value = adc_getVoltage(&adc1, ADC_VOL_CH) / 1000;
if(value > 1599) value = 1599; if(value > 1599) value = 1599;
uint32_t level = value << 16; uint32_t level = value << 16;
level /= 1600; level /= 1600;