Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The memory management on the PowerPC can be used to frighten small children. -- Linus Torvalds


devel / comp.arch.embedded / Re: How to change stm32 m4 adc channel?

SubjectAuthor
* How to change stm32 m4 adc channel?Ed Lee
+- Re: How to change stm32 m4 adc channel?David Brown
`* Re: How to change stm32 m4 adc channel?Michael Kellett
 `* Re: How to change stm32 m4 adc channel?Ed Lee
  `* Re: How to change stm32 m4 adc channel?Michael Kellett
   `* Re: How to change stm32 m4 adc channel?Ed Lee
    `* Re: How to change stm32 m4 adc channel?Michael Kellett
     `* Re: How to change stm32 m4 adc channel?Ed Lee
      `* Re: How to change stm32 m4 adc channel?Richard Damon
       `* Re: How to change stm32 m4 adc channel?Ed Lee
        `- Re: How to change stm32 m4 adc channel?Richard Damon

1
How to change stm32 m4 adc channel?

<7e069d6b-5918-4b8c-b093-6b28beeed223n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=366&group=comp.arch.embedded#366

 copy link   Newsgroups: comp.arch.embedded
X-Received: by 2002:a05:620a:21d9:: with SMTP id h25mr18263271qka.70.1619457920665;
Mon, 26 Apr 2021 10:25:20 -0700 (PDT)
X-Received: by 2002:a25:60c6:: with SMTP id u189mr26625598ybb.300.1619457920483;
Mon, 26 Apr 2021 10:25:20 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch.embedded
Date: Mon, 26 Apr 2021 10:25:20 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=108.213.66.240; posting-account=pjQH5woAAABeN8ToX-2bq3zh9hvCM8sL
NNTP-Posting-Host: 108.213.66.240
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7e069d6b-5918-4b8c-b093-6b28beeed223n@googlegroups.com>
Subject: How to change stm32 m4 adc channel?
From: edward.m...@gmail.com (Ed Lee)
Injection-Date: Mon, 26 Apr 2021 17:25:20 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Ed Lee - Mon, 26 Apr 2021 17:25 UTC

Does anyone know how to do it in register level?

I see many code examples using:

ADC_ChannelConfTypeDef sConfig = {ADC_CHANNEL_0, 1, ADC_SAMPLETIME_28CYCLES};
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
or
ADC_RegularChannelConfig(ADC1, ADC_channel_0, 1, ADC_SampleTime_480Cycles);

but i don't have access to these routines from arm-gcc.

By the way, i am using this to read the adc data:

#include "stm32f407xx.h"
ADC1->CR2 |= ADC_CR2_SWSTART; // Start A2D
while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
data = ADC1->DR;

Re: How to change stm32 m4 adc channel?

<s68eor$f6i$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=367&group=comp.arch.embedded#367

 copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.arch.embedded
Subject: Re: How to change stm32 m4 adc channel?
Date: Tue, 27 Apr 2021 09:33:46 +0200
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <s68eor$f6i$1@dont-email.me>
References: <7e069d6b-5918-4b8c-b093-6b28beeed223n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 27 Apr 2021 07:33:47 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="eeb49757d68ac4236cc60a553138972f";
logging-data="15570"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+pb5Mpwp+MyZl0Y2wwqfz4gUWgn6zE/60="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101
Thunderbird/68.10.0
Cancel-Lock: sha1:M73f4HcfA8joE949d6Oqq8d4pw4=
In-Reply-To: <7e069d6b-5918-4b8c-b093-6b28beeed223n@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Tue, 27 Apr 2021 07:33 UTC

On 26/04/2021 19:25, Ed Lee wrote:
> Does anyone know how to do it in register level?
>
> I see many code examples using:
>
> ADC_ChannelConfTypeDef sConfig = {ADC_CHANNEL_0, 1, ADC_SAMPLETIME_28CYCLES};
> HAL_ADC_ConfigChannel(&hadc1, &sConfig);
> or
> ADC_RegularChannelConfig(ADC1, ADC_channel_0, 1, ADC_SampleTime_480Cycles);
>
> but i don't have access to these routines from arm-gcc.
>

That last statement shows you perhaps don't have a full understanding of
what you are doing here.

However, the most likely cause of your troubles is that the examples you
have are using an SDK from ST, and you either don't have the SDK
installed and set up correctly, or you aren't using it in your project,
or you have simply forgotten to include the appropriate headers at the
start of your C file.

I have not used ST's tools myself, but I would expect that their SDK
comes with complete working example projects for some evaluation boards.
Start with one of these, and adapt it to suit your required changes.

> By the way, i am using this to read the adc data:
>
> #include "stm32f407xx.h"
> ADC1->CR2 |= ADC_CR2_SWSTART; // Start A2D
> while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
> data = ADC1->DR;
>

Re: How to change stm32 m4 adc channel?

<fs-dnUQUYuVumRX9nZ2dnUU78L3NnZ2d@giganews.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=368&group=comp.arch.embedded#368

 copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!border2.nntp.ams1.giganews.com!nntp.giganews.com!buffer2.nntp.ams1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Tue, 27 Apr 2021 07:34:59 -0500
Reply-To: mk@mkesc.co.uk
Subject: Re: How to change stm32 m4 adc channel?
Newsgroups: comp.arch.embedded
References: <7e069d6b-5918-4b8c-b093-6b28beeed223n@googlegroups.com>
From: mk...@mkesc.co.uk (Michael Kellett)
Date: Tue, 27 Apr 2021 13:34:58 +0100
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.10.0
MIME-Version: 1.0
In-Reply-To: <7e069d6b-5918-4b8c-b093-6b28beeed223n@googlegroups.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 7bit
Message-ID: <fs-dnUQUYuVumRX9nZ2dnUU78L3NnZ2d@giganews.com>
Lines: 56
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-6vmSK7DrBxg21Co7Ls7EsdWezzPU48F5xdiHf8oaPg6x6t4jayQbAPuQJLF9lygRWlRBcnsMaknAw/r!QDpPKo9qw0q4vs2cv3oPdC/DOk7rKd3Lzfu1zhQlIsyjoyAD6bqJippTGRZsqpAeu7zXRMKjL8Y=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 2987
 by: Michael Kellett - Tue, 27 Apr 2021 12:34 UTC

On 26/04/2021 18:25, Ed Lee wrote:
> Does anyone know how to do it in register level?
>
> I see many code examples using:
>
> ADC_ChannelConfTypeDef sConfig = {ADC_CHANNEL_0, 1, ADC_SAMPLETIME_28CYCLES};
> HAL_ADC_ConfigChannel(&hadc1, &sConfig);
> or
> ADC_RegularChannelConfig(ADC1, ADC_channel_0, 1, ADC_SampleTime_480Cycles);
>
> but i don't have access to these routines from arm-gcc.
>
> By the way, i am using this to read the adc data:
>
> #include "stm32f407xx.h"
> ADC1->CR2 |= ADC_CR2_SWSTART; // Start A2D
> while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
> data = ADC1->DR;
>
If you download and install ST's Cube etc you will end up with (amongst
a LOT of other stuff) the library code that you could use if you want
functions like:

HAL_ADC_ConfigChannel()

I don't use these - they try to be universal but end up being hard to
understand and slow.

To drive the ADC directly you will need to study the reference manual
and register descriptions and it may well help to look at some ST
examples (from the Cube again).

To address your specific problem:

Assuming a single conversion of one channel and starting after a
hardware reset:

write the channel to be converted into the SQ1 field of ADC_SQR3

make sure the L field of ADC_SQR1 = 0 so the ADC does just one conversion

set the SMP field for the channel in question to the sampling time you
want (ADC_SMPR1 or ADC_SMPR2)

enable the ADC by setting bit 0 in ADC_CR1

start the conversion by setting bit 30 in ADC_CR2

(All the above based on Ref manual for STM446xx - check details for your
own processor.)

You will need to enable the ADC clock but you must have got there already.

MK

Re: How to change stm32 m4 adc channel?

<4d63f91b-4ecf-408f-a5b0-421580e085e6n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=369&group=comp.arch.embedded#369

 copy link   Newsgroups: comp.arch.embedded
X-Received: by 2002:a05:620a:133b:: with SMTP id p27mr24624861qkj.387.1619537943194;
Tue, 27 Apr 2021 08:39:03 -0700 (PDT)
X-Received: by 2002:a25:3287:: with SMTP id y129mr17260647yby.97.1619537942909;
Tue, 27 Apr 2021 08:39:02 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch.embedded
Date: Tue, 27 Apr 2021 08:39:02 -0700 (PDT)
In-Reply-To: <fs-dnUQUYuVumRX9nZ2dnUU78L3NnZ2d@giganews.com>
Injection-Info: google-groups.googlegroups.com; posting-host=108.213.66.240; posting-account=pjQH5woAAABeN8ToX-2bq3zh9hvCM8sL
NNTP-Posting-Host: 108.213.66.240
References: <7e069d6b-5918-4b8c-b093-6b28beeed223n@googlegroups.com> <fs-dnUQUYuVumRX9nZ2dnUU78L3NnZ2d@giganews.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4d63f91b-4ecf-408f-a5b0-421580e085e6n@googlegroups.com>
Subject: Re: How to change stm32 m4 adc channel?
From: edward.m...@gmail.com (Ed Lee)
Injection-Date: Tue, 27 Apr 2021 15:39:03 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Ed Lee - Tue, 27 Apr 2021 15:39 UTC

On Tuesday, April 27, 2021 at 5:35:04 AM UTC-7, Michael Kellett wrote:
> On 26/04/2021 18:25, Ed Lee wrote:
> > Does anyone know how to do it in register level?
> >
> > I see many code examples using:
> >
> > ADC_ChannelConfTypeDef sConfig = {ADC_CHANNEL_0, 1, ADC_SAMPLETIME_28CYCLES};
> > HAL_ADC_ConfigChannel(&hadc1, &sConfig);
> > or
> > ADC_RegularChannelConfig(ADC1, ADC_channel_0, 1, ADC_SampleTime_480Cycles);
> >
> > but i don't have access to these routines from arm-gcc.
> >
> > By the way, i am using this to read the adc data:
> >
> > #include "stm32f407xx.h"
> > ADC1->CR2 |= ADC_CR2_SWSTART; // Start A2D
> > while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
> > data = ADC1->DR;
> >
> If you download and install ST's Cube etc you will end up with (amongst
> a LOT of other stuff) the library code that you could use if you want
> functions like:
>
> HAL_ADC_ConfigChannel()
>
> I don't use these - they try to be universal but end up being hard to
> understand and slow.
>
> To drive the ADC directly you will need to study the reference manual
> and register descriptions and it may well help to look at some ST
> examples (from the Cube again).
>
> To address your specific problem:
>
> Assuming a single conversion of one channel and starting after a
> hardware reset:
>
> write the channel to be converted into the SQ1 field of ADC_SQR3
>
> make sure the L field of ADC_SQR1 = 0 so the ADC does just one conversion
>
> set the SMP field for the channel in question to the sampling time you
> want (ADC_SMPR1 or ADC_SMPR2)
>
> enable the ADC by setting bit 0 in ADC_CR1
>
> start the conversion by setting bit 30 in ADC_CR2
>
> (All the above based on Ref manual for STM446xx - check details for your
> own processor.)
>
> You will need to enable the ADC clock but you must have got there already.
>
> MK

OK, i think this is getting close, but also need to select the Alt-Funct Reg for the port pin. I was hoping to find a complete example of such.

Re: How to change stm32 m4 adc channel?

<WtudnRtPe_VeoRX9nZ2dnUU78Q3NnZ2d@giganews.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=370&group=comp.arch.embedded#370

 copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!border2.nntp.ams1.giganews.com!nntp.giganews.com!buffer2.nntp.ams1.giganews.com!buffer1.nntp.ams1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Tue, 27 Apr 2021 11:33:07 -0500
Reply-To: mk@mkesc.co.uk
Subject: Re: How to change stm32 m4 adc channel?
Newsgroups: comp.arch.embedded
References: <7e069d6b-5918-4b8c-b093-6b28beeed223n@googlegroups.com>
<fs-dnUQUYuVumRX9nZ2dnUU78L3NnZ2d@giganews.com>
<4d63f91b-4ecf-408f-a5b0-421580e085e6n@googlegroups.com>
From: mk...@mkesc.co.uk (Michael Kellett)
Date: Tue, 27 Apr 2021 17:33:06 +0100
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.10.0
MIME-Version: 1.0
In-Reply-To: <4d63f91b-4ecf-408f-a5b0-421580e085e6n@googlegroups.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 7bit
Message-ID: <WtudnRtPe_VeoRX9nZ2dnUU78Q3NnZ2d@giganews.com>
Lines: 75
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-VXFovUoQZ8Bd8vHvay57B1mN8zdgvMx9/Rd1n9zyrNRgq2OuByaS1GP8+9faYYwCNz8SVJSLcgDQcMx!RSkDd697lXnzNdmR7O0GBDDGP5amk9lw8cTkhIGv7aWobZiXmOCY/jdJSdD2zMw3D9YOXMllV5U=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 4087
 by: Michael Kellett - Tue, 27 Apr 2021 16:33 UTC

On 27/04/2021 16:39, Ed Lee wrote:
> On Tuesday, April 27, 2021 at 5:35:04 AM UTC-7, Michael Kellett wrote:
>> On 26/04/2021 18:25, Ed Lee wrote:
>>> Does anyone know how to do it in register level?
>>>
>>> I see many code examples using:
>>>
>>> ADC_ChannelConfTypeDef sConfig = {ADC_CHANNEL_0, 1, ADC_SAMPLETIME_28CYCLES};
>>> HAL_ADC_ConfigChannel(&hadc1, &sConfig);
>>> or
>>> ADC_RegularChannelConfig(ADC1, ADC_channel_0, 1, ADC_SampleTime_480Cycles);
>>>
>>> but i don't have access to these routines from arm-gcc.
>>>
>>> By the way, i am using this to read the adc data:
>>>
>>> #include "stm32f407xx.h"
>>> ADC1->CR2 |= ADC_CR2_SWSTART; // Start A2D
>>> while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
>>> data = ADC1->DR;
>>>
>> If you download and install ST's Cube etc you will end up with (amongst
>> a LOT of other stuff) the library code that you could use if you want
>> functions like:
>>
>> HAL_ADC_ConfigChannel()
>>
>> I don't use these - they try to be universal but end up being hard to
>> understand and slow.
>>
>> To drive the ADC directly you will need to study the reference manual
>> and register descriptions and it may well help to look at some ST
>> examples (from the Cube again).
>>
>> To address your specific problem:
>>
>> Assuming a single conversion of one channel and starting after a
>> hardware reset:
>>
>> write the channel to be converted into the SQ1 field of ADC_SQR3
>>
>> make sure the L field of ADC_SQR1 = 0 so the ADC does just one conversion
>>
>> set the SMP field for the channel in question to the sampling time you
>> want (ADC_SMPR1 or ADC_SMPR2)
>>
>> enable the ADC by setting bit 0 in ADC_CR1
>>
>> start the conversion by setting bit 30 in ADC_CR2
>>
>> (All the above based on Ref manual for STM446xx - check details for your
>> own processor.)
>>
>> You will need to enable the ADC clock but you must have got there already.
>>
>> MK
>
> OK, i think this is getting close, but also need to select the Alt-Funct Reg for the port pin. I was hoping to find a complete example of such.
>
You don't use the GPIO port Alt Function Register for making a pin an
analogue input.
You do it by setting the mode bits for that pin in the GPIO Mode register.
There are two bits for each pin, coded like this:

Bits 2y:2y+1 MODERy[1:0]: Port x configuration bits (y = 0..15)
These bits are written by software to configure the I/O direction mode.
00: Input (reset state)
01: General purpose output mode
10: Alternate function mode
11: Analog mode

I don't have any shareable examples that don't use DMA with scan and
usually continuous mode for the ADC which I think might be distracting.

MK

Re: How to change stm32 m4 adc channel?

<89e41fb7-411b-4c4b-9525-6c155789a587n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=372&group=comp.arch.embedded#372

 copy link   Newsgroups: comp.arch.embedded
X-Received: by 2002:ae9:f310:: with SMTP id p16mr25016833qkg.123.1619556643557; Tue, 27 Apr 2021 13:50:43 -0700 (PDT)
X-Received: by 2002:a25:3287:: with SMTP id y129mr19249261yby.97.1619556643346; Tue, 27 Apr 2021 13:50:43 -0700 (PDT)
Path: i2pn2.org!i2pn.org!news.swapon.de!news.uzoreto.com!tr1.eu1.usenetexpress.com!feeder.usenetexpress.com!tr2.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch.embedded
Date: Tue, 27 Apr 2021 13:50:43 -0700 (PDT)
In-Reply-To: <WtudnRtPe_VeoRX9nZ2dnUU78Q3NnZ2d@giganews.com>
Injection-Info: google-groups.googlegroups.com; posting-host=172.58.30.152; posting-account=pjQH5woAAABeN8ToX-2bq3zh9hvCM8sL
NNTP-Posting-Host: 172.58.30.152
References: <7e069d6b-5918-4b8c-b093-6b28beeed223n@googlegroups.com> <fs-dnUQUYuVumRX9nZ2dnUU78L3NnZ2d@giganews.com> <4d63f91b-4ecf-408f-a5b0-421580e085e6n@googlegroups.com> <WtudnRtPe_VeoRX9nZ2dnUU78Q3NnZ2d@giganews.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <89e41fb7-411b-4c4b-9525-6c155789a587n@googlegroups.com>
Subject: Re: How to change stm32 m4 adc channel?
From: edward.m...@gmail.com (Ed Lee)
Injection-Date: Tue, 27 Apr 2021 20:50:43 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 96
 by: Ed Lee - Tue, 27 Apr 2021 20:50 UTC

On Tuesday, April 27, 2021 at 9:33:12 AM UTC-7, Michael Kellett wrote:
> On 27/04/2021 16:39, Ed Lee wrote:
> > On Tuesday, April 27, 2021 at 5:35:04 AM UTC-7, Michael Kellett wrote:
> >> On 26/04/2021 18:25, Ed Lee wrote:
> >>> Does anyone know how to do it in register level?
> >>>
> >>> I see many code examples using:
> >>>
> >>> ADC_ChannelConfTypeDef sConfig = {ADC_CHANNEL_0, 1, ADC_SAMPLETIME_28CYCLES};
> >>> HAL_ADC_ConfigChannel(&hadc1, &sConfig);
> >>> or
> >>> ADC_RegularChannelConfig(ADC1, ADC_channel_0, 1, ADC_SampleTime_480Cycles);
> >>>
> >>> but i don't have access to these routines from arm-gcc.
> >>>
> >>> By the way, i am using this to read the adc data:
> >>>
> >>> #include "stm32f407xx.h"
> >>> ADC1->CR2 |= ADC_CR2_SWSTART; // Start A2D
> >>> while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
> >>> data = ADC1->DR;
> >>>
> >> If you download and install ST's Cube etc you will end up with (amongst
> >> a LOT of other stuff) the library code that you could use if you want
> >> functions like:
> >>
> >> HAL_ADC_ConfigChannel()
> >>
> >> I don't use these - they try to be universal but end up being hard to
> >> understand and slow.
> >>
> >> To drive the ADC directly you will need to study the reference manual
> >> and register descriptions and it may well help to look at some ST
> >> examples (from the Cube again).
> >>
> >> To address your specific problem:
> >>
> >> Assuming a single conversion of one channel and starting after a
> >> hardware reset:
> >>
> >> write the channel to be converted into the SQ1 field of ADC_SQR3
> >>
> >> make sure the L field of ADC_SQR1 = 0 so the ADC does just one conversion
> >>
> >> set the SMP field for the channel in question to the sampling time you
> >> want (ADC_SMPR1 or ADC_SMPR2)
> >>
> >> enable the ADC by setting bit 0 in ADC_CR1
> >>
> >> start the conversion by setting bit 30 in ADC_CR2
> >>
> >> (All the above based on Ref manual for STM446xx - check details for your
> >> own processor.)
> >>
> >> You will need to enable the ADC clock but you must have got there already.
> >>
> >> MK
> >
> > OK, i think this is getting close, but also need to select the Alt-Funct Reg for the port pin. I was hoping to find a complete example of such.
> >
> You don't use the GPIO port Alt Function Register for making a pin an
> analogue input.
> You do it by setting the mode bits for that pin in the GPIO Mode register.
> There are two bits for each pin, coded like this:
>
> Bits 2y:2y+1 MODERy[1:0]: Port x configuration bits (y = 0..15)
> These bits are written by software to configure the I/O direction mode.
> 00: Input (reset state)
> 01: General purpose output mode
> 10: Alternate function mode
> 11: Analog mode
>
> I don't have any shareable examples that don't use DMA with scan and
> usually continuous mode for the ADC which I think might be distracting.
>
> MK

So, reading from PA0 or PA1 like this?

RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // Enable ADC clock
ADC1->CR2 |= ADC_CR2_ADON; // Enable ADC
ADC1->SQR1 = 0; // bit 23-20 single coversion
ADC->SMPR2 = 2; // 28 cycles

#if PA0
GPIOA->MODER |= 3; // bit 1-0 Analog mode
ADC1->SQR3 = 0; // bit 3-0,0 channel 0
#endif

#if PA1
GPIOA->MODER |= 0xc; // bit 3-2 Analog mode
ADC1->SQR3 = 1; // bit 3-0,0 channel 1
#endif

ADC1->CR2 |= ADC_CR2_SWSTART; // Start ADC
while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
DATA = ADC1->DR; // read data

Re: How to change stm32 m4 adc channel?

<aNmdnbLw99SYhxT9nZ2dnUU78VnNnZ2d@giganews.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=373&group=comp.arch.embedded#373

 copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!border2.nntp.ams1.giganews.com!nntp.giganews.com!buffer2.nntp.ams1.giganews.com!buffer1.nntp.ams1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Wed, 28 Apr 2021 03:17:09 -0500
Reply-To: mk@mkesc.co.uk
Subject: Re: How to change stm32 m4 adc channel?
Newsgroups: comp.arch.embedded
References: <7e069d6b-5918-4b8c-b093-6b28beeed223n@googlegroups.com>
<fs-dnUQUYuVumRX9nZ2dnUU78L3NnZ2d@giganews.com>
<4d63f91b-4ecf-408f-a5b0-421580e085e6n@googlegroups.com>
<WtudnRtPe_VeoRX9nZ2dnUU78Q3NnZ2d@giganews.com>
<89e41fb7-411b-4c4b-9525-6c155789a587n@googlegroups.com>
From: mk...@mkesc.co.uk (Michael Kellett)
Date: Wed, 28 Apr 2021 09:17:08 +0100
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.10.0
MIME-Version: 1.0
In-Reply-To: <89e41fb7-411b-4c4b-9525-6c155789a587n@googlegroups.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-GB
Content-Transfer-Encoding: 7bit
Message-ID: <aNmdnbLw99SYhxT9nZ2dnUU78VnNnZ2d@giganews.com>
Lines: 113
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-jzqxuxUajV6axVpqAWan76dgYvi2eP1KiZeJCBeyqzjJ9v7jPaGhxtIW6IejWGKXM0NFnW1eBwRqqI3!UEsYgoB7t7lMxEF7BS1757LAWb4QtBhSD21bRgi34Q/FamrVLZg1mk2hmtA/MJPuUC1T1yuj3tE=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 5680
 by: Michael Kellett - Wed, 28 Apr 2021 08:17 UTC

On 27/04/2021 21:50, Ed Lee wrote:
> On Tuesday, April 27, 2021 at 9:33:12 AM UTC-7, Michael Kellett wrote:
>> On 27/04/2021 16:39, Ed Lee wrote:
>>> On Tuesday, April 27, 2021 at 5:35:04 AM UTC-7, Michael Kellett wrote:
>>>> On 26/04/2021 18:25, Ed Lee wrote:
>>>>> Does anyone know how to do it in register level?
>>>>>
>>>>> I see many code examples using:
>>>>>
>>>>> ADC_ChannelConfTypeDef sConfig = {ADC_CHANNEL_0, 1, ADC_SAMPLETIME_28CYCLES};
>>>>> HAL_ADC_ConfigChannel(&hadc1, &sConfig);
>>>>> or
>>>>> ADC_RegularChannelConfig(ADC1, ADC_channel_0, 1, ADC_SampleTime_480Cycles);
>>>>>
>>>>> but i don't have access to these routines from arm-gcc.
>>>>>
>>>>> By the way, i am using this to read the adc data:
>>>>>
>>>>> #include "stm32f407xx.h"
>>>>> ADC1->CR2 |= ADC_CR2_SWSTART; // Start A2D
>>>>> while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
>>>>> data = ADC1->DR;
>>>>>
>>>> If you download and install ST's Cube etc you will end up with (amongst
>>>> a LOT of other stuff) the library code that you could use if you want
>>>> functions like:
>>>>
>>>> HAL_ADC_ConfigChannel()
>>>>
>>>> I don't use these - they try to be universal but end up being hard to
>>>> understand and slow.
>>>>
>>>> To drive the ADC directly you will need to study the reference manual
>>>> and register descriptions and it may well help to look at some ST
>>>> examples (from the Cube again).
>>>>
>>>> To address your specific problem:
>>>>
>>>> Assuming a single conversion of one channel and starting after a
>>>> hardware reset:
>>>>
>>>> write the channel to be converted into the SQ1 field of ADC_SQR3
>>>>
>>>> make sure the L field of ADC_SQR1 = 0 so the ADC does just one conversion
>>>>
>>>> set the SMP field for the channel in question to the sampling time you
>>>> want (ADC_SMPR1 or ADC_SMPR2)
>>>>
>>>> enable the ADC by setting bit 0 in ADC_CR1
>>>>
>>>> start the conversion by setting bit 30 in ADC_CR2
>>>>
>>>> (All the above based on Ref manual for STM446xx - check details for your
>>>> own processor.)
>>>>
>>>> You will need to enable the ADC clock but you must have got there already.
>>>>
>>>> MK
>>>
>>> OK, i think this is getting close, but also need to select the Alt-Funct Reg for the port pin. I was hoping to find a complete example of such.
>>>
>> You don't use the GPIO port Alt Function Register for making a pin an
>> analogue input.
>> You do it by setting the mode bits for that pin in the GPIO Mode register.
>> There are two bits for each pin, coded like this:
>>
>> Bits 2y:2y+1 MODERy[1:0]: Port x configuration bits (y = 0..15)
>> These bits are written by software to configure the I/O direction mode.
>> 00: Input (reset state)
>> 01: General purpose output mode
>> 10: Alternate function mode
>> 11: Analog mode
>>
>> I don't have any shareable examples that don't use DMA with scan and
>> usually continuous mode for the ADC which I think might be distracting.
>>
>> MK
>
> So, reading from PA0 or PA1 like this?
>
> RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // Enable ADC clock
> ADC1->CR2 |= ADC_CR2_ADON; // Enable ADC
> ADC1->SQR1 = 0; // bit 23-20 single coversion
> ADC->SMPR2 = 2; // 28 cycles
>
> #if PA0
> GPIOA->MODER |= 3; // bit 1-0 Analog mode
> ADC1->SQR3 = 0; // bit 3-0,0 channel 0
> #endif
>
> #if PA1
> GPIOA->MODER |= 0xc; // bit 3-2 Analog mode
> ADC1->SQR3 = 1; // bit 3-0,0 channel 1
> #endif
>
> ADC1->CR2 |= ADC_CR2_SWSTART; // Start ADC
> while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
> DATA = ADC1->DR; // read data
>

Not quite, entries in SMPR1/2 are for each ADC channel, not each entry
in the sequencer.
So for PA1 you need to set SMP1 bit field to 2,
ADC->SMPR2 = (uint32_t)2 << 3;

Here's a little challenge for you - good practice is to test this code
- and that would mean you should test that the sampling time is actually
what you meant it to be (not that you wrote x bits to y register).
How would you do that ?

MK

Re: How to change stm32 m4 adc channel?

<163471b7-82f9-4aba-9bf2-4902f02797d8n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=374&group=comp.arch.embedded#374

 copy link   Newsgroups: comp.arch.embedded
X-Received: by 2002:a05:620a:21d9:: with SMTP id h25mr28497998qka.70.1619619427293;
Wed, 28 Apr 2021 07:17:07 -0700 (PDT)
X-Received: by 2002:a25:6f85:: with SMTP id k127mr42719615ybc.270.1619619427061;
Wed, 28 Apr 2021 07:17:07 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mixmin.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch.embedded
Date: Wed, 28 Apr 2021 07:17:06 -0700 (PDT)
In-Reply-To: <aNmdnbLw99SYhxT9nZ2dnUU78VnNnZ2d@giganews.com>
Injection-Info: google-groups.googlegroups.com; posting-host=172.58.22.204; posting-account=pjQH5woAAABeN8ToX-2bq3zh9hvCM8sL
NNTP-Posting-Host: 172.58.22.204
References: <7e069d6b-5918-4b8c-b093-6b28beeed223n@googlegroups.com>
<fs-dnUQUYuVumRX9nZ2dnUU78L3NnZ2d@giganews.com> <4d63f91b-4ecf-408f-a5b0-421580e085e6n@googlegroups.com>
<WtudnRtPe_VeoRX9nZ2dnUU78Q3NnZ2d@giganews.com> <89e41fb7-411b-4c4b-9525-6c155789a587n@googlegroups.com>
<aNmdnbLw99SYhxT9nZ2dnUU78VnNnZ2d@giganews.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <163471b7-82f9-4aba-9bf2-4902f02797d8n@googlegroups.com>
Subject: Re: How to change stm32 m4 adc channel?
From: edward.m...@gmail.com (Ed Lee)
Injection-Date: Wed, 28 Apr 2021 14:17:07 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Ed Lee - Wed, 28 Apr 2021 14:17 UTC

On Wednesday, April 28, 2021 at 1:17:13 AM UTC-7, Michael Kellett wrote:
> On 27/04/2021 21:50, Ed Lee wrote:
> > On Tuesday, April 27, 2021 at 9:33:12 AM UTC-7, Michael Kellett wrote:
> >> On 27/04/2021 16:39, Ed Lee wrote:
> >>> On Tuesday, April 27, 2021 at 5:35:04 AM UTC-7, Michael Kellett wrote:
> >>>> On 26/04/2021 18:25, Ed Lee wrote:
> >>>>> Does anyone know how to do it in register level?
> >>>>>
> >>>>> I see many code examples using:
> >>>>>
> >>>>> ADC_ChannelConfTypeDef sConfig = {ADC_CHANNEL_0, 1, ADC_SAMPLETIME_28CYCLES};
> >>>>> HAL_ADC_ConfigChannel(&hadc1, &sConfig);
> >>>>> or
> >>>>> ADC_RegularChannelConfig(ADC1, ADC_channel_0, 1, ADC_SampleTime_480Cycles);
> >>>>>
> >>>>> but i don't have access to these routines from arm-gcc.
> >>>>>
> >>>>> By the way, i am using this to read the adc data:
> >>>>>
> >>>>> #include "stm32f407xx.h"
> >>>>> ADC1->CR2 |= ADC_CR2_SWSTART; // Start A2D
> >>>>> while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
> >>>>> data = ADC1->DR;
> >>>>>
> >>>> If you download and install ST's Cube etc you will end up with (amongst
> >>>> a LOT of other stuff) the library code that you could use if you want
> >>>> functions like:
> >>>>
> >>>> HAL_ADC_ConfigChannel()
> >>>>
> >>>> I don't use these - they try to be universal but end up being hard to
> >>>> understand and slow.
> >>>>
> >>>> To drive the ADC directly you will need to study the reference manual
> >>>> and register descriptions and it may well help to look at some ST
> >>>> examples (from the Cube again).
> >>>>
> >>>> To address your specific problem:
> >>>>
> >>>> Assuming a single conversion of one channel and starting after a
> >>>> hardware reset:
> >>>>
> >>>> write the channel to be converted into the SQ1 field of ADC_SQR3
> >>>>
> >>>> make sure the L field of ADC_SQR1 = 0 so the ADC does just one conversion
> >>>>
> >>>> set the SMP field for the channel in question to the sampling time you
> >>>> want (ADC_SMPR1 or ADC_SMPR2)
> >>>>
> >>>> enable the ADC by setting bit 0 in ADC_CR1
> >>>>
> >>>> start the conversion by setting bit 30 in ADC_CR2
> >>>>
> >>>> (All the above based on Ref manual for STM446xx - check details for your
> >>>> own processor.)
> >>>>
> >>>> You will need to enable the ADC clock but you must have got there already.
> >>>>
> >>>> MK
> >>>
> >>> OK, i think this is getting close, but also need to select the Alt-Funct Reg for the port pin. I was hoping to find a complete example of such.
> >>>
> >> You don't use the GPIO port Alt Function Register for making a pin an
> >> analogue input.
> >> You do it by setting the mode bits for that pin in the GPIO Mode register.
> >> There are two bits for each pin, coded like this:
> >>
> >> Bits 2y:2y+1 MODERy[1:0]: Port x configuration bits (y = 0..15)
> >> These bits are written by software to configure the I/O direction mode.
> >> 00: Input (reset state)
> >> 01: General purpose output mode
> >> 10: Alternate function mode
> >> 11: Analog mode
> >>
> >> I don't have any shareable examples that don't use DMA with scan and
> >> usually continuous mode for the ADC which I think might be distracting.
> >>
> >> MK
> >
> > So, reading from PA0 or PA1 like this?
> >
> > RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // Enable ADC clock
> > ADC1->CR2 |= ADC_CR2_ADON; // Enable ADC
> > ADC1->SQR1 = 0; // bit 23-20 single coversion
> > ADC->SMPR2 = 2; // 28 cycles
> >
> > #if PA0
> > GPIOA->MODER |= 3; // bit 1-0 Analog mode
> > ADC1->SQR3 = 0; // bit 3-0,0 channel 0
> > #endif
> >
> > #if PA1
> > GPIOA->MODER |= 0xc; // bit 3-2 Analog mode
> > ADC1->SQR3 = 1; // bit 3-0,0 channel 1
> > #endif
> >
> > ADC1->CR2 |= ADC_CR2_SWSTART; // Start ADC
> > while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
> > DATA = ADC1->DR; // read data
> >
> Not quite, entries in SMPR1/2 are for each ADC channel, not each entry
> in the sequencer.
> So for PA1 you need to set SMP1 bit field to 2,
> ADC->SMPR2 = (uint32_t)2 << 3;

OK, thanks.

> Here's a little challenge for you - good practice is to test this code
> - and that would mean you should test that the sampling time is actually
> what you meant it to be (not that you wrote x bits to y register).
> How would you do that ?

I guess i can measure the average conversion time from a sampling loop. For 100MHz clock, even 480 cycles are more than enough.

One question remain. How does it ties PA0 with channel 0? If it's hard coded, does it mean only port A can be analog?

Re: How to change stm32 m4 adc channel?

<fomiI.16551$4H2.15318@fx21.iad>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=378&group=comp.arch.embedded#378

 copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!peer02.ams4!peer.am4.highwinds-media.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx21.iad.POSTED!not-for-mail
Subject: Re: How to change stm32 m4 adc channel?
Newsgroups: comp.arch.embedded
References: <7e069d6b-5918-4b8c-b093-6b28beeed223n@googlegroups.com>
<fs-dnUQUYuVumRX9nZ2dnUU78L3NnZ2d@giganews.com>
<4d63f91b-4ecf-408f-a5b0-421580e085e6n@googlegroups.com>
<WtudnRtPe_VeoRX9nZ2dnUU78Q3NnZ2d@giganews.com>
<89e41fb7-411b-4c4b-9525-6c155789a587n@googlegroups.com>
<aNmdnbLw99SYhxT9nZ2dnUU78VnNnZ2d@giganews.com>
<163471b7-82f9-4aba-9bf2-4902f02797d8n@googlegroups.com>
From: Rich...@Damon-Family.org (Richard Damon)
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0)
Gecko/20100101 Thunderbird/78.10.0
MIME-Version: 1.0
In-Reply-To: <163471b7-82f9-4aba-9bf2-4902f02797d8n@googlegroups.com>
Content-Type: text/plain; charset=utf-8
Content-Language: en-US
Content-Transfer-Encoding: 7bit
Lines: 121
Message-ID: <fomiI.16551$4H2.15318@fx21.iad>
X-Complaints-To: abuse@easynews.com
Organization: Forte - www.forteinc.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Wed, 28 Apr 2021 19:40:59 -0400
X-Received-Bytes: 6205
 by: Richard Damon - Wed, 28 Apr 2021 23:40 UTC

On 4/28/21 10:17 AM, Ed Lee wrote:
> On Wednesday, April 28, 2021 at 1:17:13 AM UTC-7, Michael Kellett wrote:
>> On 27/04/2021 21:50, Ed Lee wrote:
>>> On Tuesday, April 27, 2021 at 9:33:12 AM UTC-7, Michael Kellett wrote:
>>>> On 27/04/2021 16:39, Ed Lee wrote:
>>>>> On Tuesday, April 27, 2021 at 5:35:04 AM UTC-7, Michael Kellett wrote:
>>>>>> On 26/04/2021 18:25, Ed Lee wrote:
>>>>>>> Does anyone know how to do it in register level?
>>>>>>>
>>>>>>> I see many code examples using:
>>>>>>>
>>>>>>> ADC_ChannelConfTypeDef sConfig = {ADC_CHANNEL_0, 1, ADC_SAMPLETIME_28CYCLES};
>>>>>>> HAL_ADC_ConfigChannel(&hadc1, &sConfig);
>>>>>>> or
>>>>>>> ADC_RegularChannelConfig(ADC1, ADC_channel_0, 1, ADC_SampleTime_480Cycles);
>>>>>>>
>>>>>>> but i don't have access to these routines from arm-gcc.
>>>>>>>
>>>>>>> By the way, i am using this to read the adc data:
>>>>>>>
>>>>>>> #include "stm32f407xx.h"
>>>>>>> ADC1->CR2 |= ADC_CR2_SWSTART; // Start A2D
>>>>>>> while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
>>>>>>> data = ADC1->DR;
>>>>>>>
>>>>>> If you download and install ST's Cube etc you will end up with (amongst
>>>>>> a LOT of other stuff) the library code that you could use if you want
>>>>>> functions like:
>>>>>>
>>>>>> HAL_ADC_ConfigChannel()
>>>>>>
>>>>>> I don't use these - they try to be universal but end up being hard to
>>>>>> understand and slow.
>>>>>>
>>>>>> To drive the ADC directly you will need to study the reference manual
>>>>>> and register descriptions and it may well help to look at some ST
>>>>>> examples (from the Cube again).
>>>>>>
>>>>>> To address your specific problem:
>>>>>>
>>>>>> Assuming a single conversion of one channel and starting after a
>>>>>> hardware reset:
>>>>>>
>>>>>> write the channel to be converted into the SQ1 field of ADC_SQR3
>>>>>>
>>>>>> make sure the L field of ADC_SQR1 = 0 so the ADC does just one conversion
>>>>>>
>>>>>> set the SMP field for the channel in question to the sampling time you
>>>>>> want (ADC_SMPR1 or ADC_SMPR2)
>>>>>>
>>>>>> enable the ADC by setting bit 0 in ADC_CR1
>>>>>>
>>>>>> start the conversion by setting bit 30 in ADC_CR2
>>>>>>
>>>>>> (All the above based on Ref manual for STM446xx - check details for your
>>>>>> own processor.)
>>>>>>
>>>>>> You will need to enable the ADC clock but you must have got there already.
>>>>>>
>>>>>> MK
>>>>>
>>>>> OK, i think this is getting close, but also need to select the Alt-Funct Reg for the port pin. I was hoping to find a complete example of such.
>>>>>
>>>> You don't use the GPIO port Alt Function Register for making a pin an
>>>> analogue input.
>>>> You do it by setting the mode bits for that pin in the GPIO Mode register.
>>>> There are two bits for each pin, coded like this:
>>>>
>>>> Bits 2y:2y+1 MODERy[1:0]: Port x configuration bits (y = 0..15)
>>>> These bits are written by software to configure the I/O direction mode.
>>>> 00: Input (reset state)
>>>> 01: General purpose output mode
>>>> 10: Alternate function mode
>>>> 11: Analog mode
>>>>
>>>> I don't have any shareable examples that don't use DMA with scan and
>>>> usually continuous mode for the ADC which I think might be distracting.
>>>>
>>>> MK
>>>
>>> So, reading from PA0 or PA1 like this?
>>>
>>> RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // Enable ADC clock
>>> ADC1->CR2 |= ADC_CR2_ADON; // Enable ADC
>>> ADC1->SQR1 = 0; // bit 23-20 single coversion
>>> ADC->SMPR2 = 2; // 28 cycles
>>>
>>> #if PA0
>>> GPIOA->MODER |= 3; // bit 1-0 Analog mode
>>> ADC1->SQR3 = 0; // bit 3-0,0 channel 0
>>> #endif
>>>
>>> #if PA1
>>> GPIOA->MODER |= 0xc; // bit 3-2 Analog mode
>>> ADC1->SQR3 = 1; // bit 3-0,0 channel 1
>>> #endif
>>>
>>> ADC1->CR2 |= ADC_CR2_SWSTART; // Start ADC
>>> while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
>>> DATA = ADC1->DR; // read data
>>>
>> Not quite, entries in SMPR1/2 are for each ADC channel, not each entry
>> in the sequencer.
>> So for PA1 you need to set SMP1 bit field to 2,
>> ADC->SMPR2 = (uint32_t)2 << 3;
>
> OK, thanks.
>
>> Here's a little challenge for you - good practice is to test this code
>> - and that would mean you should test that the sampling time is actually
>> what you meant it to be (not that you wrote x bits to y register).
>> How would you do that ?
>
> I guess i can measure the average conversion time from a sampling loop. For 100MHz clock, even 480 cycles are more than enough.
>
> One question remain. How does it ties PA0 with channel 0? If it's hard coded, does it mean only port A can be analog?
>

Each analog channel is hard tied internally to a given pin. (They are
all port A from my memory, but scattered about a bit). Read the device
reference manual for a listing of the capability of each pin.

Re: How to change stm32 m4 adc channel?

<ddcfccf8-66e0-4be2-847a-07eb427e9ffen@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=380&group=comp.arch.embedded#380

 copy link   Newsgroups: comp.arch.embedded
X-Received: by 2002:ad4:5ca2:: with SMTP id q2mr32880004qvh.21.1619660170831;
Wed, 28 Apr 2021 18:36:10 -0700 (PDT)
X-Received: by 2002:a25:1455:: with SMTP id 82mr2049306ybu.403.1619660170586;
Wed, 28 Apr 2021 18:36:10 -0700 (PDT)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!usenet.pasdenom.info!usenet-fr.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.arch.embedded
Date: Wed, 28 Apr 2021 18:36:10 -0700 (PDT)
In-Reply-To: <fomiI.16551$4H2.15318@fx21.iad>
Injection-Info: google-groups.googlegroups.com; posting-host=108.213.66.240; posting-account=pjQH5woAAABeN8ToX-2bq3zh9hvCM8sL
NNTP-Posting-Host: 108.213.66.240
References: <7e069d6b-5918-4b8c-b093-6b28beeed223n@googlegroups.com>
<fs-dnUQUYuVumRX9nZ2dnUU78L3NnZ2d@giganews.com> <4d63f91b-4ecf-408f-a5b0-421580e085e6n@googlegroups.com>
<WtudnRtPe_VeoRX9nZ2dnUU78Q3NnZ2d@giganews.com> <89e41fb7-411b-4c4b-9525-6c155789a587n@googlegroups.com>
<aNmdnbLw99SYhxT9nZ2dnUU78VnNnZ2d@giganews.com> <163471b7-82f9-4aba-9bf2-4902f02797d8n@googlegroups.com>
<fomiI.16551$4H2.15318@fx21.iad>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ddcfccf8-66e0-4be2-847a-07eb427e9ffen@googlegroups.com>
Subject: Re: How to change stm32 m4 adc channel?
From: edward.m...@gmail.com (Ed Lee)
Injection-Date: Thu, 29 Apr 2021 01:36:10 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Ed Lee - Thu, 29 Apr 2021 01:36 UTC

On Wednesday, April 28, 2021 at 4:41:06 PM UTC-7, Richard Damon wrote:
> On 4/28/21 10:17 AM, Ed Lee wrote:
> > On Wednesday, April 28, 2021 at 1:17:13 AM UTC-7, Michael Kellett wrote:
> >> On 27/04/2021 21:50, Ed Lee wrote:
> >>> On Tuesday, April 27, 2021 at 9:33:12 AM UTC-7, Michael Kellett wrote:
> >>>> On 27/04/2021 16:39, Ed Lee wrote:
> >>>>> On Tuesday, April 27, 2021 at 5:35:04 AM UTC-7, Michael Kellett wrote:
> >>>>>> On 26/04/2021 18:25, Ed Lee wrote:
> >>>>>>> Does anyone know how to do it in register level?
> >>>>>>>
> >>>>>>> I see many code examples using:
> >>>>>>>
> >>>>>>> ADC_ChannelConfTypeDef sConfig = {ADC_CHANNEL_0, 1, ADC_SAMPLETIME_28CYCLES};
> >>>>>>> HAL_ADC_ConfigChannel(&hadc1, &sConfig);
> >>>>>>> or
> >>>>>>> ADC_RegularChannelConfig(ADC1, ADC_channel_0, 1, ADC_SampleTime_480Cycles);
> >>>>>>>
> >>>>>>> but i don't have access to these routines from arm-gcc.
> >>>>>>>
> >>>>>>> By the way, i am using this to read the adc data:
> >>>>>>>
> >>>>>>> #include "stm32f407xx.h"
> >>>>>>> ADC1->CR2 |= ADC_CR2_SWSTART; // Start A2D
> >>>>>>> while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
> >>>>>>> data = ADC1->DR;
> >>>>>>>
> >>>>>> If you download and install ST's Cube etc you will end up with (amongst
> >>>>>> a LOT of other stuff) the library code that you could use if you want
> >>>>>> functions like:
> >>>>>>
> >>>>>> HAL_ADC_ConfigChannel()
> >>>>>>
> >>>>>> I don't use these - they try to be universal but end up being hard to
> >>>>>> understand and slow.
> >>>>>>
> >>>>>> To drive the ADC directly you will need to study the reference manual
> >>>>>> and register descriptions and it may well help to look at some ST
> >>>>>> examples (from the Cube again).
> >>>>>>
> >>>>>> To address your specific problem:
> >>>>>>
> >>>>>> Assuming a single conversion of one channel and starting after a
> >>>>>> hardware reset:
> >>>>>>
> >>>>>> write the channel to be converted into the SQ1 field of ADC_SQR3
> >>>>>>
> >>>>>> make sure the L field of ADC_SQR1 = 0 so the ADC does just one conversion
> >>>>>>
> >>>>>> set the SMP field for the channel in question to the sampling time you
> >>>>>> want (ADC_SMPR1 or ADC_SMPR2)
> >>>>>>
> >>>>>> enable the ADC by setting bit 0 in ADC_CR1
> >>>>>>
> >>>>>> start the conversion by setting bit 30 in ADC_CR2
> >>>>>>
> >>>>>> (All the above based on Ref manual for STM446xx - check details for your
> >>>>>> own processor.)
> >>>>>>
> >>>>>> You will need to enable the ADC clock but you must have got there already.
> >>>>>>
> >>>>>> MK
> >>>>>
> >>>>> OK, i think this is getting close, but also need to select the Alt-Funct Reg for the port pin. I was hoping to find a complete example of such..
> >>>>>
> >>>> You don't use the GPIO port Alt Function Register for making a pin an
> >>>> analogue input.
> >>>> You do it by setting the mode bits for that pin in the GPIO Mode register.
> >>>> There are two bits for each pin, coded like this:
> >>>>
> >>>> Bits 2y:2y+1 MODERy[1:0]: Port x configuration bits (y = 0..15)
> >>>> These bits are written by software to configure the I/O direction mode.
> >>>> 00: Input (reset state)
> >>>> 01: General purpose output mode
> >>>> 10: Alternate function mode
> >>>> 11: Analog mode
> >>>>
> >>>> I don't have any shareable examples that don't use DMA with scan and
> >>>> usually continuous mode for the ADC which I think might be distracting.
> >>>>
> >>>> MK
> >>>
> >>> So, reading from PA0 or PA1 like this?
> >>>
> >>> RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // Enable ADC clock
> >>> ADC1->CR2 |= ADC_CR2_ADON; // Enable ADC
> >>> ADC1->SQR1 = 0; // bit 23-20 single coversion
> >>> ADC->SMPR2 = 2; // 28 cycles
> >>>
> >>> #if PA0
> >>> GPIOA->MODER |= 3; // bit 1-0 Analog mode
> >>> ADC1->SQR3 = 0; // bit 3-0,0 channel 0
> >>> #endif
> >>>
> >>> #if PA1
> >>> GPIOA->MODER |= 0xc; // bit 3-2 Analog mode
> >>> ADC1->SQR3 = 1; // bit 3-0,0 channel 1
> >>> #endif
> >>>
> >>> ADC1->CR2 |= ADC_CR2_SWSTART; // Start ADC
> >>> while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
> >>> DATA = ADC1->DR; // read data
> >>>
> >> Not quite, entries in SMPR1/2 are for each ADC channel, not each entry
> >> in the sequencer.
> >> So for PA1 you need to set SMP1 bit field to 2,
> >> ADC->SMPR2 = (uint32_t)2 << 3;
> >
> > OK, thanks.
> >
> >> Here's a little challenge for you - good practice is to test this code
> >> - and that would mean you should test that the sampling time is actually
> >> what you meant it to be (not that you wrote x bits to y register).
> >> How would you do that ?
> >
> > I guess i can measure the average conversion time from a sampling loop. For 100MHz clock, even 480 cycles are more than enough.
> >
> > One question remain. How does it ties PA0 with channel 0? If it's hard coded, does it mean only port A can be analog?
> >
> Each analog channel is hard tied internally to a given pin. (They are
> all port A from my memory, but scattered about a bit). Read the device
> reference manual for a listing of the capability of each pin.

OK, i will have dig a bit deeper in the reference manual.

This info is critical for someone like me new to the STM chip.

For the (microchip/Atmel) μA SAM world, we have to set them explicitly..

PORT->Group[1].DIRCLR.reg = PORT_PB09;
// Enable the peripheral multiplexer for PB09
PORT->Group[1].PINCFG[9].reg |= PORT_PINCFG_PMUXEN;
// Set PB09 to function B which is analog input.
PORT->Group[1].PMUX[4].reg = PORT_PMUX_PMUXO_B;

Re: How to change stm32 m4 adc channel?

<08piI.296077$nn2.171205@fx48.iad>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=381&group=comp.arch.embedded#381

 copy link   Newsgroups: comp.arch.embedded
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.uzoreto.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!50.7.236.18.MISMATCH!news-out.netnews.com!news.alt.net!fdc3.netnews.com!peer04.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx48.iad.POSTED!not-for-mail
Subject: Re: How to change stm32 m4 adc channel?
Newsgroups: comp.arch.embedded
References: <7e069d6b-5918-4b8c-b093-6b28beeed223n@googlegroups.com>
<fs-dnUQUYuVumRX9nZ2dnUU78L3NnZ2d@giganews.com>
<4d63f91b-4ecf-408f-a5b0-421580e085e6n@googlegroups.com>
<WtudnRtPe_VeoRX9nZ2dnUU78Q3NnZ2d@giganews.com>
<89e41fb7-411b-4c4b-9525-6c155789a587n@googlegroups.com>
<aNmdnbLw99SYhxT9nZ2dnUU78VnNnZ2d@giganews.com>
<163471b7-82f9-4aba-9bf2-4902f02797d8n@googlegroups.com>
<fomiI.16551$4H2.15318@fx21.iad>
<ddcfccf8-66e0-4be2-847a-07eb427e9ffen@googlegroups.com>
From: Rich...@Damon-Family.org (Richard Damon)
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0)
Gecko/20100101 Thunderbird/78.10.0
MIME-Version: 1.0
In-Reply-To: <ddcfccf8-66e0-4be2-847a-07eb427e9ffen@googlegroups.com>
Content-Type: text/plain; charset=utf-8
Content-Language: en-US
Content-Transfer-Encoding: 8bit
Lines: 139
Message-ID: <08piI.296077$nn2.171205@fx48.iad>
X-Complaints-To: abuse@easynews.com
Organization: Forte - www.forteinc.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Wed, 28 Apr 2021 22:48:27 -0400
X-Received-Bytes: 7354
 by: Richard Damon - Thu, 29 Apr 2021 02:48 UTC

On 4/28/21 9:36 PM, Ed Lee wrote:
> On Wednesday, April 28, 2021 at 4:41:06 PM UTC-7, Richard Damon wrote:
>> On 4/28/21 10:17 AM, Ed Lee wrote:
>>> On Wednesday, April 28, 2021 at 1:17:13 AM UTC-7, Michael Kellett wrote:
>>>> On 27/04/2021 21:50, Ed Lee wrote:
>>>>> On Tuesday, April 27, 2021 at 9:33:12 AM UTC-7, Michael Kellett wrote:
>>>>>> On 27/04/2021 16:39, Ed Lee wrote:
>>>>>>> On Tuesday, April 27, 2021 at 5:35:04 AM UTC-7, Michael Kellett wrote:
>>>>>>>> On 26/04/2021 18:25, Ed Lee wrote:
>>>>>>>>> Does anyone know how to do it in register level?
>>>>>>>>>
>>>>>>>>> I see many code examples using:
>>>>>>>>>
>>>>>>>>> ADC_ChannelConfTypeDef sConfig = {ADC_CHANNEL_0, 1, ADC_SAMPLETIME_28CYCLES};
>>>>>>>>> HAL_ADC_ConfigChannel(&hadc1, &sConfig);
>>>>>>>>> or
>>>>>>>>> ADC_RegularChannelConfig(ADC1, ADC_channel_0, 1, ADC_SampleTime_480Cycles);
>>>>>>>>>
>>>>>>>>> but i don't have access to these routines from arm-gcc.
>>>>>>>>>
>>>>>>>>> By the way, i am using this to read the adc data:
>>>>>>>>>
>>>>>>>>> #include "stm32f407xx.h"
>>>>>>>>> ADC1->CR2 |= ADC_CR2_SWSTART; // Start A2D
>>>>>>>>> while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
>>>>>>>>> data = ADC1->DR;
>>>>>>>>>
>>>>>>>> If you download and install ST's Cube etc you will end up with (amongst
>>>>>>>> a LOT of other stuff) the library code that you could use if you want
>>>>>>>> functions like:
>>>>>>>>
>>>>>>>> HAL_ADC_ConfigChannel()
>>>>>>>>
>>>>>>>> I don't use these - they try to be universal but end up being hard to
>>>>>>>> understand and slow.
>>>>>>>>
>>>>>>>> To drive the ADC directly you will need to study the reference manual
>>>>>>>> and register descriptions and it may well help to look at some ST
>>>>>>>> examples (from the Cube again).
>>>>>>>>
>>>>>>>> To address your specific problem:
>>>>>>>>
>>>>>>>> Assuming a single conversion of one channel and starting after a
>>>>>>>> hardware reset:
>>>>>>>>
>>>>>>>> write the channel to be converted into the SQ1 field of ADC_SQR3
>>>>>>>>
>>>>>>>> make sure the L field of ADC_SQR1 = 0 so the ADC does just one conversion
>>>>>>>>
>>>>>>>> set the SMP field for the channel in question to the sampling time you
>>>>>>>> want (ADC_SMPR1 or ADC_SMPR2)
>>>>>>>>
>>>>>>>> enable the ADC by setting bit 0 in ADC_CR1
>>>>>>>>
>>>>>>>> start the conversion by setting bit 30 in ADC_CR2
>>>>>>>>
>>>>>>>> (All the above based on Ref manual for STM446xx - check details for your
>>>>>>>> own processor.)
>>>>>>>>
>>>>>>>> You will need to enable the ADC clock but you must have got there already.
>>>>>>>>
>>>>>>>> MK
>>>>>>>
>>>>>>> OK, i think this is getting close, but also need to select the Alt-Funct Reg for the port pin. I was hoping to find a complete example of such.
>>>>>>>
>>>>>> You don't use the GPIO port Alt Function Register for making a pin an
>>>>>> analogue input.
>>>>>> You do it by setting the mode bits for that pin in the GPIO Mode register.
>>>>>> There are two bits for each pin, coded like this:
>>>>>>
>>>>>> Bits 2y:2y+1 MODERy[1:0]: Port x configuration bits (y = 0..15)
>>>>>> These bits are written by software to configure the I/O direction mode.
>>>>>> 00: Input (reset state)
>>>>>> 01: General purpose output mode
>>>>>> 10: Alternate function mode
>>>>>> 11: Analog mode
>>>>>>
>>>>>> I don't have any shareable examples that don't use DMA with scan and
>>>>>> usually continuous mode for the ADC which I think might be distracting.
>>>>>>
>>>>>> MK
>>>>>
>>>>> So, reading from PA0 or PA1 like this?
>>>>>
>>>>> RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // Enable ADC clock
>>>>> ADC1->CR2 |= ADC_CR2_ADON; // Enable ADC
>>>>> ADC1->SQR1 = 0; // bit 23-20 single coversion
>>>>> ADC->SMPR2 = 2; // 28 cycles
>>>>>
>>>>> #if PA0
>>>>> GPIOA->MODER |= 3; // bit 1-0 Analog mode
>>>>> ADC1->SQR3 = 0; // bit 3-0,0 channel 0
>>>>> #endif
>>>>>
>>>>> #if PA1
>>>>> GPIOA->MODER |= 0xc; // bit 3-2 Analog mode
>>>>> ADC1->SQR3 = 1; // bit 3-0,0 channel 1
>>>>> #endif
>>>>>
>>>>> ADC1->CR2 |= ADC_CR2_SWSTART; // Start ADC
>>>>> while (!(ADC1->SR & ADC_SR_EOC)); // ready wait
>>>>> DATA = ADC1->DR; // read data
>>>>>
>>>> Not quite, entries in SMPR1/2 are for each ADC channel, not each entry
>>>> in the sequencer.
>>>> So for PA1 you need to set SMP1 bit field to 2,
>>>> ADC->SMPR2 = (uint32_t)2 << 3;
>>>
>>> OK, thanks.
>>>
>>>> Here's a little challenge for you - good practice is to test this code
>>>> - and that would mean you should test that the sampling time is actually
>>>> what you meant it to be (not that you wrote x bits to y register).
>>>> How would you do that ?
>>>
>>> I guess i can measure the average conversion time from a sampling loop. For 100MHz clock, even 480 cycles are more than enough.
>>>
>>> One question remain. How does it ties PA0 with channel 0? If it's hard coded, does it mean only port A can be analog?
>>>
>> Each analog channel is hard tied internally to a given pin. (They are
>> all port A from my memory, but scattered about a bit). Read the device
>> reference manual for a listing of the capability of each pin.
>
> OK, i will have dig a bit deeper in the reference manual.
>
> This info is critical for someone like me new to the STM chip.
>
> For the (microchip/Atmel) μA SAM world, we have to set them explicitly.
>
> PORT->Group[1].DIRCLR.reg = PORT_PB09;
> // Enable the peripheral multiplexer for PB09
> PORT->Group[1].PINCFG[9].reg |= PORT_PINCFG_PMUXEN;
> // Set PB09 to function B which is analog input.
> PORT->Group[1].PMUX[4].reg = PORT_PMUX_PMUXO_B;
>

The STM pins also have config registers to configure which special
function (if any) they are set for. There generally is a table listing
every pin and what functions it can connect to.

1
server_pubkey.txt

rocksolid light 0.9.7
clearnet tor