STM32: DMA如何将具有3个通道的ADC的数值以半字的形式保存到缓冲区中?

huangapple go评论52阅读模式
英文:

STM32: how does the DMA save values of an ADC with 3 channels as half word into a buffer?

问题

有一个带有1个ADC和2个通道配置的STM32L151RBT6。ADC配置为“扫描模式”和“连续模式”。ADC的分辨率为12位。ADC值保存在缓冲区中,数组的每个元素大小为32位。DMA将ADC值保存为半字,因此数组的每个元素包含2个ADC值。因此,如果放大到一个元素,它看起来像这样:

MSB 0000 0000 0000 0000 0000 0000 0000 0000 LSB
    |--| |------------| |--| |------------|
    |    |              |    |
    |    |              |    |ADC CH0 Value
    |    |              |
    |    |              |填充位
    |    |
    |    |ADC CH1 Value
    |
    | 填充位

请注意,由于ADC分辨率为12位,每个值仅为12位。

现在来说我的问题:如果我配置3个ADC通道,而不降低分辨率,DMA将如何将ADC值保存到数组中?

这是我的DMA配置:

static uint32_t ADC_ConvertedValue[16];

HAL_ADC_Start_DMA(&hadc, ADC_ConvertedValue, 32);
英文:

I have a STM32L151RBT6 with 1 ADC and 2 channels configured. The ADC is configured in "scan mode" and "continuous mode". The ADC has a resolution of 12 bits. The ADC values are saved into a buffer, where every element of the array is 32 bits in size. The DMA saves the ADC values as half word, so every element of the array contains 2 ADC values. So, if you zoom into an element, it would look like this:

MSB 0000 0000 0000 0000 0000 0000 0000 0000 LSB
    |--| |------------| |--| |------------|
    |    |              |    |
    |    |              |    |ADC CH0 Value
    |    |              |
    |    |              |Filler Bits
    |    |
    |    |ADC CH1 Value
    |
    | Filler Bits

Note that every value is only 12 bits in size, since the ADC resolution is 12 bits.

Now comes my question: How would the DMA save the ADC values into the array, if I would configure 3 ADC channels, without reducing the resolution?

This is my DMA configuration:

static uint32_t ADC_ConvertedValue[16];

HAL_ADC_Start_DMA(&hadc, ADC_ConvertedValue, 32);

答案1

得分: 3

ADC/DMA不关心你将数组定义为uint32_t数组。它只需要一个内存地址(可能在2字节边界上对齐),然后它将写入数据。你可以将其定义为char数组,只要对齐和大小都没问题,它就一样愉快。

由于你正在以12位模式运行ADC,并且它将样本存储为16位值,定义数组为以下形式会更自然:

static uint16_t ADC_ConvertedValue[32];

然后,你可以使用简单的数组索引访问样本,而无需手动从32位值中提取它们。

如果坚持使用uint32_t,并将通道数更改为3,那么第一个数组成员将包含来自通道0和1的样本,然后下一个数组成员将包含来自通道2和0的样本,接下来的将包含1和2的样本,依此类推。

英文:

The ADC/DMA doesn't care that you defined your array to be an array of uint32_t. It just needs a memory address (probably aligned on a 2-byte boundary), and it will write the data in. You could define it as an array of char and it would be equally happy, so long as the alignment and size were ok.

Since you are running the ADC in 12-bit mode, and it is storing samples as 16-bit values, it would be much more natural to define the array as:

static uint16_t ADC_ConvertedValue[32];

Then you could access your samples using simple array indexing, rather than having to manually extract them from 32-bit values.

If you persist in using uint32_t, and changed the number of channels to 3, then the first array member would have samples from channels 0 and 1, then the next array member would have samples from channels 2 and 0, then the next would have 1 and 2, etc.

huangapple
  • 本文由 发表于 2023年3月15日 21:30:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定