CS7000: platform: added Vbat, mic and volume level measurement

This commit is contained in:
Silvano Seva 2024-04-30 19:20:19 +02:00
parent 4a76d67184
commit d85287e692
3 changed files with 63 additions and 6 deletions

View File

@ -17,6 +17,8 @@
#include <gpio_shiftReg.h>
#include <spi_bitbang.h>
#include <adc_stm32.h>
#include <hwconfig.h>
#include <pthread.h>
/**
@ -56,6 +58,8 @@ static uint8_t spiSr_func(const void *priv, uint8_t value)
}
static const struct gpioPin shiftRegStrobe = { GPIOEXT_STR };
static pthread_mutex_t adc1Mutex;
SPI_CUSTOM_DEVICE_DEFINE(spiSr, spiSr_func, NULL, NULL)
GPIO_SHIFTREG_DEVICE_DEFINE(extGpio, (const struct spiDevice *) &spiSr, shiftRegStrobe, 24)
ADC_STM32_DEVICE_DEFINE(adc1, ADC1, &adc1Mutex, 3300000)

View File

@ -25,6 +25,16 @@
extern "C" {
#endif
enum AdcChannels
{
ADC_VOL_CH = 15, /* PC5 */
ADC_VBAT_CH = 6, /* PA6 */
ADC_MIC_CH = 3, /* PA3 */
ADC_RSSI_CH = 8, /* PB0 */
ADC_RTX_CH = 7, /* PA7 */
};
extern const struct Adc adc1;
extern const struct spiCustomDevice spiSr;
extern const struct gpioDev extGpio;

View File

@ -19,6 +19,7 @@
#include <peripherals/gpio.h>
#include <gpio_shiftReg.h>
#include <spi_bitbang.h>
#include <adc_stm32.h>
#include <hwconfig.h>
#include <string.h>
@ -37,14 +38,19 @@ static const hwInfo_t hwInfo =
void platform_init()
{
gpio_setMode(PTT_SW, INPUT);
gpio_setMode(PTT_EXT, INPUT);
gpio_setMode(PTT_SW, INPUT);
gpio_setMode(PTT_EXT, INPUT);
gpio_setMode(MAIN_PWR_DET, ANALOG);
gpio_setMode(AIN_MIC, ANALOG);
gpio_setMode(AIN_VOLUME, ANALOG);
gpio_setMode(GPIOEXT_CLK, OUTPUT);
gpio_setMode(GPIOEXT_DAT, OUTPUT);
spi_init((const struct spiDevice *) &spiSr);
gpioShiftReg_init(&extGpio);
adcStm32_init(&adc1);
#ifndef RUNNING_TESTSUITE
gpioDev_set(MAIN_PWR_SW);
@ -53,6 +59,7 @@ void platform_init()
void platform_terminate()
{
adcStm32_terminate(&adc1);
#ifndef RUNNING_TESTSUITE
gpioDev_clear(MAIN_PWR_SW);
@ -63,17 +70,49 @@ void platform_terminate()
uint16_t platform_getVbat()
{
return 7400; // TODO
/*
* Battery voltage is measured through an 1:3.9 voltage divider and
* adc1_getMeasurement returns a value in uV.
*/
uint32_t vbat = adc_getVoltage(&adc1, ADC_VBAT_CH) * 39;
return vbat / 10000;
}
uint8_t platform_getMicLevel()
{
return 0; // TODO
/* Value from ADC is 12 bit wide: shift right by four to get 0 - 255 */
return adc_getRawSample(&adc1, ADC_MIC_CH) >> 4;
}
uint8_t platform_getVolumeLevel()
{
return 0; // TODO
/*
* Volume level corresponds to an analog signal in the range 20 - 2520mV.
* Potentiometer has pseudo-logarithmic law, well described with two straight
* lines with a breakpoint around 410mV.
* Output value has range 0 - 255 with breakpoint at 139.
*/
uint16_t value = adc_getRawSample(&adc1, ADC_VOL_CH);
uint32_t output;
if(value <= 512)
{
// First line: offset zero, slope 0.271
output = value;
output = (output * 271) / 1000;
}
else
{
// Second line: offset 512, slope 0.044
output = value - 512;
output = (output * 44) / 1000;
output += 139;
}
if(output > 255)
output = 255;
return output;
}
int8_t platform_getChSelector()
@ -97,7 +136,11 @@ bool platform_pwrButtonStatus()
* is always a bit of noise in the ADC measurement making the returned
* voltage not to be exactly zero.
*/
return (platform_getVbat() > 1000) ? true : false;
uint16_t vbat = platform_getVbat();
if(vbat < 1000)
return false;
return true;
}
void platform_ledOn(led_t led)