diff --git a/meson.build b/meson.build index f4946ade..748a1b3a 100644 --- a/meson.build +++ b/meson.build @@ -415,7 +415,6 @@ dm1801_def += openrtx_def + mk22fn512_def + miosix_cm4f_def ## mod17_src = ['platform/targets/Module17/platform.c', 'platform/drivers/display/SH110x_Mod17.c', - 'platform/drivers/ADC/ADC1_Mod17.c', 'platform/drivers/keyboard/keyboard_Mod17.c', 'platform/drivers/NVM/nvmem_Mod17.c', 'platform/drivers/CPS/cps_io_native_Mod17.c', diff --git a/platform/drivers/ADC/ADC1_Mod17.c b/platform/drivers/ADC/ADC1_Mod17.c deleted file mode 100644 index 0e8ffdd1..00000000 --- a/platform/drivers/ADC/ADC1_Mod17.c +++ /dev/null @@ -1,93 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2020 - 2023 by Silvano Seva IU2KWO * - * and Niccolò Izzo IU2KIN * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - * 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. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, see * - ***************************************************************************/ - -#include -#include -#include -#include -#include "ADC1_Mod17.h" - -static pthread_mutex_t adcMutex; - -void adc1_init() -{ - pthread_mutex_init(&adcMutex, NULL); - - RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; - __DSB(); - - /* - * Configure GPIOs to analog input mode: - */ - gpio_setMode(AIN_HWVER, ANALOG); - - /* - * ADC clock is APB2 frequency divided by 8, giving 10.5MHz. - * We set the sample time of each channel to 84 ADC cycles and we have that - * a conversion takes 12 cycles: total conversion time is then of ~9us. - */ - ADC->CCR |= ADC_CCR_ADCPRE; - ADC1->SMPR2 = ADC_SMPR2_SMP3_2; - - /* - * Convert one channel, no overrun interrupt, 12-bit resolution, - * no analog watchdog, discontinuous mode, no end of conversion interrupts, - * turn on ADC. - */ - ADC1->SQR1 = 0; - ADC1->CR2 = ADC_CR2_ADON; -} - -void adc1_terminate() -{ - pthread_mutex_destroy(&adcMutex); - - ADC1->CR2 &= ~ADC_CR2_ADON; - RCC->APB2ENR &= ~RCC_APB2ENR_ADC1EN; - __DSB(); -} - -uint16_t adc1_getRawSample(uint8_t ch) -{ - if(ch > 15) return 0; - - pthread_mutex_lock(&adcMutex); - - ADC1->SQR3 = ch; - ADC1->CR2 |= ADC_CR2_SWSTART; - while((ADC1->SR & ADC_SR_EOC) == 0) ; - uint16_t value = ADC1->DR; - - pthread_mutex_unlock(&adcMutex); - - return value; -} - -uint16_t adc1_getMeasurement(uint8_t ch) -{ - /* - * To avoid using floats, we convert the raw ADC sample to mV using 16.16 - * fixed point math. The equation for conversion is (sample * 3300)/4096 but, - * since converting the raw ADC sample to 16.16 notation requires a left - * shift by 16 and dividing by 4096 is equivalent to shifting right by 12, - * we just shift left by four and then multiply by 3300. - * With respect to using floats, maximum error is -1mV. - */ - uint32_t sample = (adc1_getRawSample(ch) << 4) * 3300; - return ((uint16_t) (sample >> 16)); -} diff --git a/platform/drivers/ADC/ADC1_Mod17.h b/platform/drivers/ADC/ADC1_Mod17.h deleted file mode 100644 index 1f034bac..00000000 --- a/platform/drivers/ADC/ADC1_Mod17.h +++ /dev/null @@ -1,75 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2020 - 2023 by Silvano Seva IU2KWO * - * and Niccolò Izzo IU2KIN * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - * 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. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, see * - ***************************************************************************/ - -#ifndef ADC1_H -#define ADC1_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * Driver for ADC1, used on Module 17 to sample input voltage - * - * NOTE: values inside the enum are the channel numbers of STM32 ADC1 peripheral. - */ - -enum adcCh -{ - ADC_HWVER_CH = 3, -}; - -/** - * Initialise ADC1. - */ -void adc1_init(); - -/** - * Turn off ADC1. - */ -void adc1_terminate(); - -/** - * Get current measurement of a given channel returning the raw ADC value. - * - * NOTE: the mapping provided in enum adcCh DOES NOT correspond to the physical - * ADC channel mapping! - * - * @param ch: channel number. - * @return current value of the specified channel, in ADC counts. - */ -uint16_t adc1_getRawSample(uint8_t ch); - -/** - * Get current measurement of a given channel. - * - * NOTE: the mapping provided in enum adcCh DOES NOT correspond to the physical - * ADC channel mapping! - * - * @param ch: channel number. - * @return current value of the specified channel in mV. - */ -uint16_t adc1_getMeasurement(uint8_t ch); - -#ifdef __cplusplus -} -#endif - -#endif /* ADC1_H */ diff --git a/platform/targets/Module17/hwconfig.h b/platform/targets/Module17/hwconfig.h index 5f3c1d50..49f128d1 100644 --- a/platform/targets/Module17/hwconfig.h +++ b/platform/targets/Module17/hwconfig.h @@ -26,6 +26,11 @@ #include #include "pinmap.h" +enum AdcChannel +{ + ADC_HWVER_CH = 3, +}; + extern const struct i2cDevice i2c1; /* Screen dimensions */ diff --git a/platform/targets/Module17/platform.c b/platform/targets/Module17/platform.c index 2e6ec2f5..8cafe7bb 100644 --- a/platform/targets/Module17/platform.c +++ b/platform/targets/Module17/platform.c @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2021 - 2023 by Federico Amedeo Izzo IU2NUO, * + * Copyright (C) 2021 - 2024 by Federico Amedeo Izzo IU2NUO, * * Niccolò Izzo IU2KIN, * * Frederik Saraci IU2NRO, * * Silvano Seva IU2KWO * @@ -24,14 +24,15 @@ #include #include #include +#include #include #include -#include #include #include #include +ADC_STM32_DEVICE_DEFINE(adc1, ADC1, NULL, 3300000) I2C_STM32_DEVICE_DEFINE(i2c1, I2C1, NULL) extern mod17Calib_t mod17CalData; @@ -62,10 +63,12 @@ void platform_init() gpio_setMode(PTT_OUT, OUTPUT); gpio_clearPin(PTT_OUT); + gpio_setMode(AIN_HWVER, ANALOG); + /* * Check if external I2C1 pull-ups are present. If they are not, * enable internal pull-ups and slow-down I2C1. - */ + */ gpio_setMode(I2C1_SCL, INPUT_PULL_DOWN); gpio_setMode(I2C1_SDA, INPUT_PULL_DOWN); @@ -95,8 +98,8 @@ void platform_init() DAC->DHR12R1 = 1365; nvm_init(); - adc1_init(); audio_init(); + adcStm32_init(&adc1); mcp4551_init(&i2c1, SOFTPOT_RX); mcp4551_init(&i2c1, SOFTPOT_TX); @@ -112,8 +115,8 @@ void platform_init() * - 0V: rev. 0.1d or lower * - 3.3V: rev 0.1e */ - uint16_t ver = adc1_getMeasurement(ADC_HWVER_CH); - if(ver >= 3000) + uint32_t ver = adc_getVoltage(&adc1, ADC_HWVER_CH); + if(ver >= 3000000) hwInfo.hw_version = 1; /* 100ms blink of sync led to signal device startup */ @@ -129,7 +132,7 @@ void platform_terminate() gpio_clearPin(SYNC_LED); gpio_clearPin(ERR_LED); - adc1_terminate(); + adcStm32_terminate(&adc1); nvm_terminate(); audio_terminate();