Skip to content

Commit 4aa0163

Browse files
authored
Merge pull request #1293 from fpistm/ADC_channel_bank
Adc channel bank support
2 parents 8160331 + 813edb9 commit 4aa0163

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

cores/arduino/stm32/PinNamesTypes.h

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
1-
/* *******************************************************************************
2-
* Copyright (c) 2016, STMicroelectronics
1+
/*
2+
*******************************************************************************
3+
* Copyright (c) 2016-2021, STMicroelectronics
34
* All rights reserved.
45
*
5-
* Redistribution and use in source and binary forms, with or without
6-
* modification, are permitted provided that the following conditions are met:
7-
*
8-
* 1. Redistributions of source code must retain the above copyright notice,
9-
* this list of conditions and the following disclaimer.
10-
* 2. Redistributions in binary form must reproduce the above copyright notice,
11-
* this list of conditions and the following disclaimer in the documentation
12-
* and/or other materials provided with the distribution.
13-
* 3. Neither the name of STMicroelectronics nor the names of its contributors
14-
* may be used to endorse or promote products derived from this software
15-
* without specific prior written permission.
6+
* This software component is licensed by ST under BSD 3-Clause license,
7+
* the "License"; You may not use this file except in compliance with the
8+
* License. You may obtain a copy of the License at:
9+
* opensource.org/licenses/BSD-3-Clause
1610
*
17-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20-
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23-
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24-
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25-
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2711
*******************************************************************************
2812
*/
2913
#ifndef _PINNAMESTYPES_H
@@ -42,7 +26,8 @@ extern "C" {
4226
* [19:15] Channel (Analog/Timer specific)
4327
* [20] Inverted (Analog/Timer specific)
4428
* [21] Analog ADC control - Only valid for specific families
45-
* [32:22] Reserved
29+
* [22] Analog channel bank B - Only valid for specific families
30+
* [32:23] Reserved
4631
*/
4732

4833
#define STM_PIN_FUNCTION_MASK 0x07
@@ -77,16 +62,21 @@ extern "C" {
7762
#define STM_PIN_AN_CTRL_SHIFT 21
7863
#define STM_PIN_ANALOG_CONTROL_BIT (STM_PIN_AN_CTRL_MASK << STM_PIN_AN_CTRL_SHIFT)
7964

80-
#define STM_PIN_FUNCTION(X) (((X) >> STM_PIN_FUNCTION_SHIFT) & STM_PIN_FUNCTION_MASK)
81-
#define STM_PIN_OD(X) (((X) >> STM_PIN_OD_SHIFT) & STM_PIN_OD_MASK)
82-
#define STM_PIN_PUPD(X) (((X) >> STM_PIN_PUPD_SHIFT) & STM_PIN_PUPD_MASK)
83-
#define STM_PIN_SPEED(X) (((X) >> STM_PIN_SPEED_SHIFT) & STM_PIN_SPEED_MASK)
84-
#define STM_PIN_AFNUM(X) (((X) >> STM_PIN_AFNUM_SHIFT) & STM_PIN_AFNUM_MASK)
85-
#define STM_PIN_CHANNEL(X) (((X) >> STM_PIN_CHAN_SHIFT) & STM_PIN_CHAN_MASK)
86-
#define STM_PIN_INVERTED(X) (((X) >> STM_PIN_INV_SHIFT) & STM_PIN_INV_MASK)
87-
#define STM_PIN_ANALOG_CONTROL(X) (((X) >> STM_PIN_AN_CTRL_SHIFT) & STM_PIN_AN_CTRL_MASK)
88-
#define STM_PIN_MODE(X) ((STM_PIN_OD((X)) << 4) | \
89-
(STM_PIN_FUNCTION((X)) & (~STM_PIN_OD_BITS)))
65+
#define STM_PIN_AN_CHAN_BANK_B_MASK 0x01
66+
#define STM_PIN_AN_CHAN_BANK_B_SHIFT 22
67+
#define STM_PIN_ANALOG_CHAN_BANK_B_BIT (STM_PIN_AN_CHAN_BANK_B_MASK << STM_PIN_AN_CHAN_BANK_B_SHIFT)
68+
69+
#define STM_PIN_FUNCTION(X) (((X) >> STM_PIN_FUNCTION_SHIFT) & STM_PIN_FUNCTION_MASK)
70+
#define STM_PIN_OD(X) (((X) >> STM_PIN_OD_SHIFT) & STM_PIN_OD_MASK)
71+
#define STM_PIN_PUPD(X) (((X) >> STM_PIN_PUPD_SHIFT) & STM_PIN_PUPD_MASK)
72+
#define STM_PIN_SPEED(X) (((X) >> STM_PIN_SPEED_SHIFT) & STM_PIN_SPEED_MASK)
73+
#define STM_PIN_AFNUM(X) (((X) >> STM_PIN_AFNUM_SHIFT) & STM_PIN_AFNUM_MASK)
74+
#define STM_PIN_CHANNEL(X) (((X) >> STM_PIN_CHAN_SHIFT) & STM_PIN_CHAN_MASK)
75+
#define STM_PIN_INVERTED(X) (((X) >> STM_PIN_INV_SHIFT) & STM_PIN_INV_MASK)
76+
#define STM_PIN_ANALOG_CONTROL(X) (((X) >> STM_PIN_AN_CTRL_SHIFT) & STM_PIN_AN_CTRL_MASK)
77+
#define STM_PIN_ANALOG_CHANNEL_BANK_B(X) (((X) >> STM_PIN_AN_CHAN_BANK_B_SHIFT) & STM_PIN_AN_CHAN_BANK_B_MASK)
78+
#define STM_PIN_MODE(X) ((STM_PIN_OD((X)) << 4) | \
79+
(STM_PIN_FUNCTION((X)) & (~STM_PIN_OD_BITS)))
9080

9181
#define STM_PIN_DEFINE(FUNC_OD, PUPD, AFNUM) ((int)(FUNC_OD) |\
9282
((PUPD & STM_PIN_PUPD_MASK) << STM_PIN_PUPD_SHIFT) |\
@@ -116,13 +106,14 @@ typedef enum {
116106
STM_PIN_ANALOG = 3,
117107
} StmPinFunction;
118108

119-
#define STM_MODE_INPUT (STM_PIN_INPUT)
120-
#define STM_MODE_OUTPUT_PP (STM_PIN_OUTPUT)
121-
#define STM_MODE_OUTPUT_OD (STM_PIN_OUTPUT | STM_PIN_OD_BITS)
122-
#define STM_MODE_AF_PP (STM_PIN_ALTERNATE)
123-
#define STM_MODE_AF_OD (STM_PIN_ALTERNATE | STM_PIN_OD_BITS)
124-
#define STM_MODE_ANALOG (STM_PIN_ANALOG)
125-
#define STM_MODE_ANALOG_ADC_CONTROL (STM_PIN_ANALOG | STM_PIN_ANALOG_CONTROL_BIT)
109+
#define STM_MODE_INPUT (STM_PIN_INPUT)
110+
#define STM_MODE_OUTPUT_PP (STM_PIN_OUTPUT)
111+
#define STM_MODE_OUTPUT_OD (STM_PIN_OUTPUT | STM_PIN_OD_BITS)
112+
#define STM_MODE_AF_PP (STM_PIN_ALTERNATE)
113+
#define STM_MODE_AF_OD (STM_PIN_ALTERNATE | STM_PIN_OD_BITS)
114+
#define STM_MODE_ANALOG (STM_PIN_ANALOG)
115+
#define STM_MODE_ANALOG_ADC_CONTROL (STM_PIN_ANALOG | STM_PIN_ANALOG_CONTROL_BIT)
116+
#define STM_MODE_ANALOG_ADC_CHANNEL_BANK_B (STM_PIN_ANALOG | STM_PIN_ANALOG_CHAN_BANK_B_BIT)
126117

127118
// High nibble = port number (FirstPort <= PortName <= LastPort)
128119
// Low nibble = pin number

libraries/SrcWrapper/src/stm32/analog.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static PinName g_current_pin = NC;
9090
#endif
9191

9292
/* Private Functions */
93-
static uint32_t get_adc_channel(PinName pin)
93+
static uint32_t get_adc_channel(PinName pin, uint32_t *bank)
9494
{
9595
uint32_t function = pinmap_function(pin, PinMap_ADC);
9696
uint32_t channel = 0;
@@ -207,6 +207,15 @@ static uint32_t get_adc_channel(PinName pin)
207207
channel = 0;
208208
break;
209209
}
210+
#ifdef ADC_CHANNELS_BANK_B
211+
if (STM_PIN_ANALOG_CHANNEL_BANK_B(function)) {
212+
*bank = ADC_CHANNELS_BANK_B;
213+
} else {
214+
*bank = ADC_CHANNELS_BANK_A;
215+
}
216+
#else
217+
UNUSED(bank);
218+
#endif
210219
return channel;
211220
}
212221

@@ -746,6 +755,7 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution)
746755
__IO uint16_t uhADCxConvertedValue = 0;
747756
uint32_t samplingTime = ADC_SAMPLINGTIME;
748757
uint32_t channel = 0;
758+
uint32_t bank = 0;
749759

750760
if ((pin & PADC_BASE) && (pin < ANA_START)) {
751761
#if defined(STM32H7xx)
@@ -762,7 +772,7 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution)
762772
samplingTime = ADC_SAMPLINGTIME_INTERNAL;
763773
} else {
764774
AdcHandle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC);
765-
channel = get_adc_channel(pin);
775+
channel = get_adc_channel(pin, &bank);
766776
}
767777

768778
if (AdcHandle.Instance == NP) {
@@ -824,7 +834,9 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution)
824834
!defined(STM32MP1xx) && !defined(STM32WBxx)
825835
AdcHandle.Init.LowPowerAutoPowerOff = DISABLE; /* ADC automatically powers-off after a conversion and automatically wakes-up when a new conversion is triggered */
826836
#endif
827-
#ifdef ADC_CHANNELS_BANK_A
837+
#ifdef ADC_CHANNELS_BANK_B
838+
AdcHandle.Init.ChannelsBank = bank;
839+
#elif defined(ADC_CHANNELS_BANK_A)
828840
AdcHandle.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
829841
#endif
830842
AdcHandle.Init.ContinuousConvMode = DISABLE; /* Continuous mode disabled to have only 1 conversion at each conversion trig */

0 commit comments

Comments
 (0)