Commit graph

157 commits

Author SHA1 Message Date
Guillaume Gautier 3b50237699 drivers: adc: stm32: use correct macros for dma transfer
Use the correct dedicated macros for enabling DMA transfer for STM32H7 and
U5.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2024-03-12 19:28:54 -04:00
Guillaume Gautier 5d2558bdad drivers: adc: stm32: dma fixes
Add a blank define for the case where DMA channels are defined in ADC node
of the dts but STM32_ADC_DMA is not enabled. Otherwise compilation fails.
Also fix the way the DMA channel is configured by using a standard DT
macro, otherwise it doesn't work for dma-v2bis DMA types.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2024-03-12 19:28:54 -04:00
Guillaume Gautier e91860d8c7 drivers: adc: stm32: disable dma before calibration
For some STM32 series, DMA has to be disabled before starting ADC
calibration.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2024-03-12 10:41:55 +01:00
Alexandre Poizat 0195f774ba drivers: adc: stm32: disable ADC before writing oversampling bits for g0
Added the STM32G0X SOC series to the list of SOC that need to disable the
ADC while setting the oversampling bits to prevent writing over the
CKMODE bits.

Signed-off-by: Alexandre Poizat <apoizat@kalrayinc.com>
2024-03-11 14:21:13 +01:00
Guillaume Gautier 02219189d0 drivers: adc: stm32: add dma support for stm32f4 adc
Add ADC DMA support for STM32F4 (and similar series F2, F7 and L1)

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2024-02-12 12:19:56 +01:00
Guillaume Gautier 62f1105550 drivers: adc: stm32: do not disable adc after measurement
Do not disable the ADC after the end of the measurement to avoid systematic
enabling which is time-consuming in case the configuration is unchanged.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2024-01-30 14:13:28 +00:00
Guillaume Gautier 276fc569d6 drivers: adc: stm32: prevent suspend to ram while measure in progress
Prevents the system to go in Suspend to RAM low power mode while ADC
measurement is in progress.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2024-01-25 08:55:02 +01:00
Guillaume Gautier 14839e80d5 drivers: adc: stm32: add support for two sampling time common channels
For series that have two sampling time common channels, only one was used.
This commit add the support for the second one. The first two different
acquisition time values are used for the sequence and all further values
must match either of them, otherwise generating an error.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2024-01-24 12:44:59 +00:00
Michael R Rosen 193ad777f4 driver: adc: stm32: combine shared and separate irqs
Several STM32 variants include both shared IRQs for some ADCs and
separate IRQs for others (for example, STM32G473 has 5 ADCs, ADC1 and
ADC2 share one IRQ while ADC3, ADC4 and ADC5 each have unique
IRQs). The STM32 ADC driver however previously only supported either
separate IRQ lines for each operational ADC in the devicetree or a
single shared IRQ for all operational ADCs in the devicetree which
prevented all ADCs from being usable at the same time when the variant
contained a mix of both shared and separate ADC IRQ lines (only either
all the shared or all the separate and one of the shared might be used
at most for one application).

To allow for all ADCs in an STM32 variant to be usable in a single
application, generate an ISR and initialization function for each
unique IRQn as defined in the devicetree and give the task of
initialization to the first ADC which connects to that particular
IRQ. Each ISR function will generate code to call the ISR for each ADC
associated with that IRQn as was previously done for
CONFIG_ADC_STM32_SHARED_IRQS, allowing an ISR to be shared for the
ADCs sharing an IRQ while simultaneously providing separate ISRs for
each IRQ. Thus, the only information required to have ADCs either
share an ISR or not is provided by the devicetree.

Signed-off-by: Michael R Rosen <mrrosen@alumni.cmu.edu>
2024-01-23 15:21:55 +00:00
Brian Juel Folkmann 06b57926a2 drivers: adc: stm32: Fix race condition with internal channels
When using one of the internal channels (die_temp, vbat, vref) the
channels are enabled in the individual drivers and disabled again
whenever an adc conversion is complete.

This creates a race condition if the ADC is used from multiple threads.

This commit moves the disabling of the channels to the individual
drivers.

Signed-off-by: Brian Juel Folkmann <bju@trackunit.com>
2024-01-11 13:53:17 +01:00
HaiLong Yang 640a493c1d drivers: adc: stm32 adc support h7 dual core lines
For STM32H7 dual core lines, M4 can not access to linear calib addr
ADC_LINEAR_CALIB_REG_1_ADDR.

Signed-off-by: HaiLong Yang <hailong.yang@brainco.cn>
2023-12-18 09:29:17 +01:00
Guillaume Gautier 20fd6a10e1 drivers: adc: stm32: prevent pm while measurement in progress
Prevent PM while ADC measurement in progress.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-12-01 14:31:05 +01:00
Francois Ramu de1bfd6a6e drivers: adc: stm32 adc fixing calibration for the stm32F1 serie
Configure the sw trigger just after calibration
So the conversion can start on regular channel on the
software control bit.

Signed-off-by: Francois Ramu <francois.ramu@st.com>
2023-11-30 12:22:46 +01:00
Hein Wessels a257bcb735 drivers: adc: stm32: add dma support for other mcus
This commit adds support for more STM32 CPUs that has
a different DMA interface. This was tested only for
the nucleo_l476rg.

Signed-off-by: Hein Wessels <heinwessels93@gmail.com>
2023-11-23 10:00:50 +01:00
Hein Wessels f27e45473a drivers: adc: stm32: driver now agnostic of actual dma name
Previously the STM32 DMA driver was dependent on a very specific
name for the DMA in the DTS. This hidden requirement has caused
a bit of confusion. This commit changes the driver to instead
always use the first DMA listed in the ADC node's dma property.

Should fix: #65387

Signed-off-by: Hein Wessels <heinwessels93@gmail.com>
2023-11-23 10:00:50 +01:00
Guillaume Gautier 479ba144a7 drivers: adc: stm32: enable PM
Enable PM for STM32 ADC.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-11-08 10:06:32 +01:00
Guillaume Gautier 13ca0dc806 drivers: adc: stm32: refactor calibration
Refactor calibration code in anticipation of PM addition.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-11-08 10:06:32 +01:00
Guillaume Gautier 69fb18a19d drivers: adc: stm32: ADC nocache buffers can be in CONFIG_NOCACHE_MEMORY
CONFIG_NOCACHE_MEMORY is a valid way of declaring buffers in
nocache regions. Consider them valid in the STM32 ADC driver
nocache check.
Copied from commit 818aa2d

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-09-29 16:52:33 +03:00
Guillaume Gautier e8e5d5554c drivers: adc: stm32: reset acquisition time between reads
For all STM32 ADC that use common sampling times, there is a check made to
ensure that all channels of a sequence use the same sampling time.
The value was not reset between reads, resulting in error if two
consecutive sequences used different values.
This commit adds a reset of this value once read is done.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-09-29 09:47:07 +02:00
Guillaume Gautier e09c594711 drivers: adc: stm32: remove channel count limit
Remove CHANNEL_COUNT limit used to check the channel bitmask.
This value was not applicable on STM32L1 where channel can go up to 31.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-09-22 15:30:47 +02:00
Guillaume Gautier c201d21964 drivers: adc: rework stm32 adc sequencer
Add sequencer support for all STM32 series.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-09-22 15:30:47 +02:00
Guillaume Gautier a1adc17b31 drivers: adc: stm32: move internal path setting to sensor drivers
On some STM32 families (such as F4), temperature and Vbat sensor share the
same ADC channel, which can lead to conflict when reading them, and wrong
measurement can follow.

To alleviate this problem, this commit moves the setting of the common
path internal channel to the sensor drivers themselves instead of doing
it in the ADC driver.

The teardown is still done in the ADC driver, systematically, instead of
channel by channel (which has the same result).

By moving this logic in the sensor drivers, the properties temp-channel,
vbat-channel and vref-channel becomes useless and are thus removed.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-09-22 09:21:34 +02:00
Carlo Caione e4a125b6a4 dt: Make zephyr,memory-attr a capabilities bitmask
This is the final step in making the `zephyr,memory-attr` property
actually useful.

The problem with the current implementation is that `zephyr,memory-attr`
is an enum type, this is making very difficult to use that to actually
describe the memory capabilities. The solution proposed in this PR is to
use the `zephyr,memory-attr` property as an OR-ed bitmask of memory
attributes.

With the change proposed in this PR it is possible in the DeviceTree to
mark the memory regions with a bitmask of attributes by using the
`zephyr,memory-attr` property. This property and the related memory
region can then be retrieved at run-time by leveraging a provided helper
library or the usual DT helpers.

The set of general attributes that can be specified in the property are
defined and explained in
`include/zephyr/dt-bindings/memory-attr/memory-attr.h` (the list can be
extended when needed).

For example, to mark a memory region in the DeviceTree as volatile,
non-cacheable, out-of-order:

   mem: memory@10000000 {
       compatible = "mmio-sram";
       reg = <0x10000000 0x1000>;
       zephyr,memory-attr = <( DT_MEM_VOLATILE |
			       DT_MEM_NON_CACHEABLE |
			       DT_MEM_OOO )>;
   };

The `zephyr,memory-attr` property can also be used to set
architecture-specific custom attributes that can be interpreted at run
time. This is leveraged, among other things, to create MPU regions out
of DeviceTree defined memory regions on ARM, for example:

   mem: memory@10000000 {
       compatible = "mmio-sram";
       reg = <0x10000000 0x1000>;
       zephyr,memory-region = "NOCACHE_REGION";
       zephyr,memory-attr = <( DT_ARM_MPU(ATTR_MPU_RAM_NOCACHE) )>;
   };

See `include/zephyr/dt-bindings/memory-attr/memory-attr-mpu.h` to see
how an architecture can define its own special memory attributes (in
this case ARM MPU).

The property can also be used to set custom software-specific
attributes. For example we can think of marking a memory region as
available to be used for memory allocation (not yet implemented):

   mem: memory@10000000 {
       compatible = "mmio-sram";
       reg = <0x10000000 0x1000>;
       zephyr,memory-attr = <( DT_MEM_NON_CACHEABLE |
			       DT_MEM_SW_ALLOCATABLE )>;
   };

Or maybe we can leverage the property to specify some alignment
requirements for the region:

   mem: memory@10000000 {
       compatible = "mmio-sram";
       reg = <0x10000000 0x1000>;
       zephyr,memory-attr = <( DT_MEM_CACHEABLE |
			       DT_MEM_SW_ALIGN(32) )>;
   };

The conventional and recommended way to deal and manage with memory
regions marked with attributes is by using the provided `mem-attr`
helper library by enabling `CONFIG_MEM_ATTR` (or by using the usual DT
helpers).

When this option is enabled the list of memory regions and their
attributes are compiled in a user-accessible array and a set of
functions is made available that can be used to query, probe and act on
regions and attributes, see `include/zephyr/mem_mgmt/mem_attr.h`

Note that the `zephyr,memory-attr` property is only a descriptive
property of the capabilities of the associated memory  region, but it
does not result in any actual setting for the memory to be set. The
user, code or subsystem willing to use this information to do some work
(for example creating an MPU region out of the property) must use either
the provided `mem-attr` library or the usual DeviceTree helpers to
perform the required work / setting.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2023-09-15 12:46:54 +02:00
Gerard Marull-Paretas aaeb0a672e toolchain: only include <zephyr/toolchain.h>
It is wrong to use toolchain-specific header files.

Signed-off-by: Gerard Marull-Paretas <gerard@teslabs.com>
2023-09-15 09:27:30 +02:00
Guillaume Gautier 7f8831aa7e drivers: adc: stm32: add support for kernel source clock
Add support for ADC kernel source clock, similar to other STM32 drivers.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-08-29 11:27:07 +01:00
Guillaume Gautier 484028306d drivers: adc: stm32: use dts to set the clock prescaler
Now that everything is in place, the prescaler value picked from dts is
used to properly configure the clock.
The code is moved into its own function.
There are four main cases:
- F1 and F37x don't have prescaler (in ADC register) so nothing is done.
- F0 only has individual registers.
- C0, G0, L0, WB1x and WL have both invidual and common registers. The
  individual one is used to define either synchronous mode with its
  prescaler, or asynchronous mode. The common one is only used to store
  the value of the asynchronous prescaler.
- All others only have a common register where both the mode and the
  prescaler is stored.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-08-29 11:27:07 +01:00
Guillaume Gautier e8c4990639 drivers: adc: stm32: get prescaler value from dts
Get the STM32 ADC prescaler data from dts to create the appropriate LL
macro to use for the ADC clock.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-08-29 11:27:07 +01:00
Carlo Caione 15e84cbfac dts: Move to 'zephyr,memory-attr'
Move to 'zephyr,memory-attr' and use the newly introduced helpers.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2023-07-25 11:22:10 +02:00
Guillaume Gautier d15f3448b5 drivers: adc: Update ADC driver for STM32WBA series
Update ADC driver for STM32WBA series

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-07-12 11:48:10 +02:00
Brett Witherspoon 8f73a479d1 drivers: adc: stm32: use correct device in dma callback
The device passed to the DMA callback is the DMA device. Instead use
the ADC device available in the private data.

Signed-off-by: Brett Witherspoon <brett@witherspoon.engineering>
2023-07-03 13:50:37 +00:00
Guillaume Gautier ec73e7dae5 drivers: adc: stm32: use feature macros instead of series macros
Define two macros for calibration and oversampling for STM32 series that
possess these features, and use these macros instead of a list of series.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-05-25 13:32:08 +00:00
Guillaume Gautier 3ee6ddff29 drivers: adc: rework oversampling for stm32 adc
Rework oversampling by creating a const table containing possible
oversampling values and using this table instead of switch case.
Also disable ADC for all series before setting oversampling.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-05-25 13:32:08 +00:00
Rihards Skuja ecc3315cf8 drivers: adc: stm32: allow to use multiple ADCs with STM32F3 series
STM32F3 have multiple ADCs that share the same IRQ.

Signed-off-by: Rihards Skuja <rihards.s@origin-robotics.com>
2023-05-22 15:26:26 +02:00
Guillaume Gautier b5e750851e drivers: adc: make use of new stm32 adc compatibles
Several sections of the STM32 ADC driver are #ifdef by a combination of the
same SOC defines that share similar IPs. These are F2/F4/F7/L1, and
F1/F37x.
Each of these combinations is now replaced by a specific compatible, which
makes the code a bit lighter and more succinct.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-05-16 18:18:51 +02:00
Guillaume Gautier 649742f47e drivers: adc: add ifdef for stm32f37x adc
Add ifdef around some functions that don't exist for STM32F37x (ADC_V2_5)
like it is done for STM32F1x.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-05-16 18:18:51 +02:00
Guillaume Gautier c1a601397f drivers: adc: cleanup stm32 adc driver
Inverts the logic of some #ifdef, replacing lists of newer series by a
list of NOT older series. This makes it shorter, and more future-proof as
future series are more likely to work out of the box, without need to
manually add a new define in each place.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-05-16 18:18:51 +02:00
Guillaume Gautier bab52fbc81 drivers: adc: clean stm32 adc calibration
Rework and clean some code around STM32 ADC calibration

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-05-16 18:18:51 +02:00
Guillaume Gautier 5180b6fac6 drivers: adc: rewrite stm32 adc enable
Some STM32 families that need to check the ADRDY flag after enabling the
ADC were not doing it. The #ifdef has been updated to fix that.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-05-16 18:18:51 +02:00
Guillaume Gautier 291e4c2561 drivers: adc: add comment to describe different adc versions
Add comment to describe different ADC versions

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-05-16 18:18:51 +02:00
Guillaume Gautier a7b8a5d61f drivers: adc: use dts properties for stm32 adc sampling time
Use the new dts properties for sampling time in the STM32 ADC driver:
- Get the sampling time from dts
- Remove all sampling time macro concatenation.
- Simplify/refactor setting of sampling time
- Check that sampling time is the same for all series using common channel

This also fixes a few cases where wrong sampling times were used for an
ADC instance (like ADC4 on U5, or ADC3 on STM32H723).

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-05-11 10:25:54 +00:00
Cyril Fougeray 1be72d9888 dma: callback with 2 status codes for successful transfers
Make use of positive status values in the DMA callback to pass
info to the DMA client after a successful DMA operation.
A completed DMA transfer uses the status 0 while a reached
water mark uses the status 1.

Signed-off-by: Cyril Fougeray <cyril.fougeray@worldcoin.org>
2023-05-08 09:57:32 +02:00
Guillaume Gautier f3a8279996 drivers: adc: get resolutions from dtsi for stm32 adc
Simplify the STM32 ADC driver code by using the new ADC resolutions
property in dtsi files.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
2023-04-26 12:53:03 +02:00
Brian Juel Folkmann f05b6f412e drivers: adc: stm32h5 adc driver must wait about 1ms after enabling
port commit f7f47dc to stm32h5

Without this fix, ADC driver will hang when reading internal channels.

Signed-off-by: Brian Juel Folkmann <bju@trackunit.com>
2023-04-24 13:34:50 +02:00
Hein Wessels 6427ba861c drivers: adc: stm32h7: remove oversampling workaround
Removes a workaround that was required while there was a bug in
upstream STM32 HAL drivers where the oversampling was not set
correctly in all cases. This has since been fixed, and also
added to the Zephyr STM32 HAL.

The upstream issue:
https://github.com/STMicroelectronics/STM32CubeH7/issues/177

Signed-off-by: Hein Wessels <heinwessels93@gmail.com>
2023-04-13 09:59:42 +02:00
Benjamin Björnsson 05e9387663 drivers: adc: adc_stm32: Add support for C0-series
Add support for ADC on STM32C0-series.

Signed-off-by: Benjamin Björnsson <benjamin.bjornsson@gmail.com>
2023-04-07 08:18:03 +00:00
Pieter De Gendt 6b532ff43e treewide: Update clock control API usage
Replace all (clock_control_subsys_t *) casts with (clock_control_subsys_t)

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
2023-04-05 10:55:46 +02:00
Francois Ramu 3f6a3d6031 drivers: adc stm32adc driver for the new stm32h5 serie
Add the support of the new stm32h5 to the stm32 ADC driver
Note : LL_ADC_CLOCK_ASYNC_DIV6 is the value for getting the
expected results of ADC.

Signed-off-by: Francois Ramu <francois.ramu@st.com>
2023-04-03 09:50:43 +02:00
Francois Ramu f7f47dc437 drivers: adc: stm32 adc driver must wait about 1ms after enabling
After enabling the ADC, the peripheral has a certain delay (about 1ms)
to set its ADC Ready flag in the ADC ISR register.
In between, the ADRDY is still 0 and the ADEN is 1 in the CR.
The ADC can be used for conversion, only when the ADRDY bit is set

Signed-off-by: Francois Ramu <francois.ramu@st.com>
2023-03-28 09:57:28 +02:00
Hein Wessels 36f592ddbc drivers: adc: stm32: only handle OVR flag when supported
STM32 ADC DMA support added in #52965 incorrectly assumed
that all ADC devices have a OVR flag. This commit changes
the driver to only account for it if it's found in the
LL drivers.

Signed-off-by: Hein Wessels <heinwessels93@gmail.com>
2023-03-22 09:34:42 +01:00
Hein Wessels a2dd232410 drivers: adc: stm32: dma support
Sampling multiple adc channels at once using dma

Only verified to be working on nucleo_h743zi

Signed-off-by: Hein Wessels <heinwessels93@gmail.com>
2023-03-20 16:29:24 +01:00