Drivers: ADC: added driver for STM32H7 devices
This commit is contained in:
parent
7b35715ee4
commit
f5012e0f63
|
|
@ -284,6 +284,7 @@ stm32h743_src = ['platform/mcu/STM32H7xx/boot/startup.cpp',
|
|||
'platform/mcu/STM32H7xx/drivers/pll.cpp',
|
||||
'platform/mcu/STM32H7xx/drivers/delays.cpp',
|
||||
'platform/drivers/GPIO/gpio_stm32.c',
|
||||
'platform/drivers/ADC/adc_stm32h7.c',
|
||||
'platform/mcu/CMSIS/Device/ST/STM32H7xx/Source/system_stm32h7xx.c']
|
||||
|
||||
stm32h743_inc = ['platform/mcu/CMSIS/Include',
|
||||
|
|
|
|||
|
|
@ -30,9 +30,9 @@ extern "C" {
|
|||
* @param name: instance name.
|
||||
* @param periph: pointer to hardware peripheral.
|
||||
* @param mutx: pointer to mutex for concurrent access, can be NULL.
|
||||
* @param vref: ADC reference voltage, in uV.
|
||||
* @param res: ADC resolution in uV/LSB.
|
||||
*/
|
||||
#define ADC_STM32_DEVICE_DEFINE(name, periph, mutx, vref) \
|
||||
#define ADC_STM32_DEVICE_DEFINE(name, periph, mutx, res) \
|
||||
extern uint16_t adcStm32_sample(const struct Adc *adc, \
|
||||
const uint32_t channel); \
|
||||
const struct Adc name = \
|
||||
|
|
@ -40,7 +40,7 @@ const struct Adc name = \
|
|||
.sample = &adcStm32_sample, \
|
||||
.priv = periph, \
|
||||
.mutex = mutx, \
|
||||
.countsTouV = ADC_COUNTS_TO_UV(vref, 12) \
|
||||
.countsTouV = res \
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,131 @@
|
|||
/***************************************************************************
|
||||
* Copyright (C) 2024 by Silvano Seva IU2KWO *
|
||||
* *
|
||||
* 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 <http://www.gnu.org/licenses/> *
|
||||
***************************************************************************/
|
||||
|
||||
#include <stm32h7xx.h>
|
||||
#include <pthread.h>
|
||||
#include <errno.h>
|
||||
#include "adc_stm32.h"
|
||||
|
||||
int adcStm32_init(const struct Adc *adc)
|
||||
{
|
||||
/*
|
||||
* Configure ADC for synchronous clock mode (clocked by AHB clock), divided
|
||||
* by 4. This gives an ADC clock of 50MHz when AHB clock is 200MHz.
|
||||
*
|
||||
* NOTE: ADC1 and ADC2 share the same clock tree!
|
||||
*/
|
||||
|
||||
switch((uint32_t) adc->priv)
|
||||
{
|
||||
case ADC1_BASE:
|
||||
case ADC2_BASE:
|
||||
RCC->AHB1ENR |= RCC_AHB1ENR_ADC12EN;
|
||||
__DSB();
|
||||
ADC12_COMMON->CCR = ADC_CCR_CKMODE_1
|
||||
| ADC_CCR_CKMODE_0;
|
||||
break;
|
||||
|
||||
case ADC3_BASE:
|
||||
RCC->AHB4ENR |= RCC_AHB4ENR_ADC3EN;
|
||||
__DSB();
|
||||
ADC3_COMMON->CCR = ADC_CCR_CKMODE_1
|
||||
| ADC_CCR_CKMODE_0;
|
||||
break;
|
||||
|
||||
default:
|
||||
return -EINVAL;
|
||||
break;
|
||||
}
|
||||
|
||||
ADC_TypeDef *pAdc = ((ADC_TypeDef *) adc->priv);
|
||||
|
||||
// Enable ADC voltage regulator, enable boost mode. Wait until LDO regulator
|
||||
// is ready.
|
||||
pAdc->CR = ADC_CR_ADVREGEN
|
||||
| ADC_CR_BOOST_1
|
||||
| ADC_CR_BOOST_0;
|
||||
|
||||
while((pAdc->ISR & ADC_ISR_LDORDY) == 0) ;
|
||||
|
||||
// Start calibration, both offset and linearity.
|
||||
pAdc->CR |= ADC_CR_ADCAL
|
||||
| ADC_CR_ADCALLIN;
|
||||
|
||||
while((pAdc->CR & ADC_CR_ADCAL) != 0) ;
|
||||
|
||||
/*
|
||||
* ADC clock is 50MHz. We set the sample time of each channel to 387.5 ADC
|
||||
* cycles, giving a total conversion time of ~7us.
|
||||
*/
|
||||
pAdc->SMPR2 = 0x36DB6DB6;
|
||||
pAdc->SMPR1 = 0x36DB6DB6;
|
||||
|
||||
// Finally,turn on the ADC
|
||||
pAdc->CR |= ADC_CR_ADEN;
|
||||
while((pAdc->ISR & ADC_ISR_ADRDY) != 0) ;
|
||||
|
||||
if(adc->mutex != NULL)
|
||||
pthread_mutex_init((pthread_mutex_t *) adc->mutex, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void adcStm32_terminate(const struct Adc *adc)
|
||||
{
|
||||
// A conversion may be in progress, wait until it finishes
|
||||
if(adc->mutex != NULL)
|
||||
pthread_mutex_lock((pthread_mutex_t *) adc->mutex);
|
||||
|
||||
((ADC_TypeDef *) adc->priv)->CR = 0;
|
||||
|
||||
switch((uint32_t) adc->priv)
|
||||
{
|
||||
case ADC1_BASE:
|
||||
case ADC2_BASE:
|
||||
if((ADC1->CR == 0) && (ADC2->CR == 0))
|
||||
RCC->AHB1ENR &= ~RCC_AHB1ENR_ADC12EN;
|
||||
__DSB();
|
||||
break;
|
||||
|
||||
case ADC3_BASE:
|
||||
RCC->AHB4ENR &= ~RCC_AHB4ENR_ADC3EN;
|
||||
__DSB();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if(adc->mutex != NULL)
|
||||
pthread_mutex_destroy((pthread_mutex_t *) adc->mutex);
|
||||
}
|
||||
|
||||
uint16_t adcStm32_sample(const struct Adc *adc, const uint32_t channel)
|
||||
{
|
||||
if(channel > 16)
|
||||
return 0;
|
||||
|
||||
ADC_TypeDef *pAdc = ((ADC_TypeDef *) adc->priv);
|
||||
|
||||
pAdc->SQR1 = channel << ADC_SQR1_SQ1_Pos;
|
||||
pAdc->PCSEL = 1 << channel;
|
||||
pAdc->CR |= ADC_CR_ADSTART;
|
||||
|
||||
while((pAdc->ISR & ADC_ISR_EOC) == 0) ;
|
||||
|
||||
return pAdc->DR;
|
||||
}
|
||||
|
|
@ -98,7 +98,7 @@ SPI_BITBANG_DEVICE_DEFINE(det_spi, spiDetCfg, NULL)
|
|||
SPI_BITBANG_DEVICE_DEFINE(pll_spi, spiPllCfg, NULL)
|
||||
SPI_STM32_DEVICE_DEFINE(c6000_spi, SPI2, &c6000_mutex)
|
||||
GPIO_SHIFTREG_DEVICE_DEFINE(extGpio, (const struct spiDevice *) &spiSr, shiftRegStrobe, 24)
|
||||
ADC_STM32_DEVICE_DEFINE(adc1, ADC1, &adc1Mutex, 3300000)
|
||||
ADC_STM32_DEVICE_DEFINE(adc1, ADC1, &adc1Mutex, ADC_COUNTS_TO_UV(3300000, 12))
|
||||
|
||||
const struct ak2365a detector =
|
||||
{
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ static pthread_mutex_t adc1Mutex;
|
|||
SPI_STM32_DEVICE_DEFINE(nvm_spi, SPI1, NULL)
|
||||
SPI_BITBANG_DEVICE_DEFINE(pll_spi, spiPllCfg, NULL)
|
||||
SPI_BITBANG_DEVICE_DEFINE(c5000_spi, spiC5000Cfg, NULL)
|
||||
ADC_STM32_DEVICE_DEFINE(adc1, ADC1, &adc1Mutex, 3300000)
|
||||
ADC_STM32_DEVICE_DEFINE(adc1, ADC1, &adc1Mutex, ADC_COUNTS_TO_UV(3300000, 12))
|
||||
|
||||
const struct sky73210 pll =
|
||||
{
|
||||
|
|
|
|||
|
|
@ -27,4 +27,4 @@ static pthread_mutex_t spi2Mutex;
|
|||
static pthread_mutex_t adcMutex;
|
||||
|
||||
SPI_STM32_DEVICE_DEFINE(spi2, SPI2, &spi2Mutex)
|
||||
ADC_STM32_DEVICE_DEFINE(adc1, ADC1, &adcMutex, 3300000)
|
||||
ADC_STM32_DEVICE_DEFINE(adc1, ADC1, &adcMutex, ADC_COUNTS_TO_UV(3300000, 12))
|
||||
|
|
|
|||
|
|
@ -75,4 +75,4 @@ static uint8_t spiC6000_func(const void *priv, uint8_t value)
|
|||
|
||||
SPI_CUSTOM_DEVICE_DEFINE(c6000_spi, spiC6000_func, NULL, &c6000_mutex)
|
||||
SPI_STM32_DEVICE_DEFINE(nvm_spi, SPI1, NULL)
|
||||
ADC_STM32_DEVICE_DEFINE(adc1, ADC1, &adcMutex, 3300000)
|
||||
ADC_STM32_DEVICE_DEFINE(adc1, ADC1, &adcMutex, ADC_COUNTS_TO_UV(3300000, 12))
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@
|
|||
#include <errno.h>
|
||||
|
||||
|
||||
ADC_STM32_DEVICE_DEFINE(adc1, ADC1, NULL, 3300000)
|
||||
ADC_STM32_DEVICE_DEFINE(adc1, ADC1, NULL, ADC_COUNTS_TO_UV(3300000, 12))
|
||||
I2C_STM32_DEVICE_DEFINE(i2c1, I2C1, NULL)
|
||||
I2C_STM32_DEVICE_DEFINE(i2c2, I2C2, NULL)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue