mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-11-16 01:05:15 +08:00
[bsp][bouffalo] Validate ADC DMA sample channels (#12003)
* fix(bouffalo): validate ADC DMA channel indexes * fix(bouffalo): validate configured ADC channels * fix(bouffalo): keep ADC channel validation warning-free
This commit is contained in:
1 parent
adbf7de5d1
commit
b3a0eb5e12
1 file changed
+48
-2
@@ -23,7 +23,9 @@
|
||||
#endif
|
||||
|
||||
static struct bflb_dma_channel_lli_pool_s lli[1];
|
||||
static volatile uint32_t raw_data[16 + 10];
|
||||
#define BL_ADC_DMA_CHANNEL_OFFSET (10U)
|
||||
#define BL_ADC_DMA_CHANNEL_COUNT (16U)
|
||||
static volatile uint32_t raw_data[BL_ADC_DMA_CHANNEL_COUNT + BL_ADC_DMA_CHANNEL_OFFSET];
|
||||
static struct bflb_adc_result_s adc_value[10];
|
||||
|
||||
static struct bflb_adc_config_s adc_config =
|
||||
@@ -110,6 +112,38 @@ static struct _adc_channel_cfg chan[] = {
|
||||
#endif
|
||||
};
|
||||
|
||||
static rt_bool_t bl_adc_channel_is_valid(rt_int8_t channel)
|
||||
{
|
||||
if (channel < 0)
|
||||
{
|
||||
return RT_FALSE;
|
||||
}
|
||||
|
||||
if ((rt_size_t)channel >= BL_ADC_DMA_CHANNEL_COUNT)
|
||||
{
|
||||
return RT_FALSE;
|
||||
}
|
||||
|
||||
#if defined(BSP_USING_ADC_CH0) || defined(BSP_USING_ADC_CH1) || \
|
||||
defined(BSP_USING_ADC_CH2) || defined(BSP_USING_ADC_CH3) || \
|
||||
defined(BSP_USING_ADC_CH4) || defined(BSP_USING_ADC_CH5) || \
|
||||
defined(BSP_USING_ADC_CH6) || defined(BSP_USING_ADC_CH7) || \
|
||||
defined(BSP_USING_ADC_CH8) || defined(BSP_USING_ADC_CH9) || \
|
||||
defined(BSP_USING_ADC_CH10)
|
||||
rt_size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(chan) / sizeof(chan[0]); i++)
|
||||
{
|
||||
if (chan[i].chan.pos_chan == (rt_uint8_t)channel)
|
||||
{
|
||||
return RT_TRUE;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return RT_FALSE;
|
||||
}
|
||||
|
||||
static void bl_adc_pin_init(void)
|
||||
{
|
||||
struct bflb_device_s *gpio;
|
||||
@@ -127,6 +161,11 @@ static rt_err_t bl_adc_enabled(struct rt_adc_device *device, rt_int8_t channel,
|
||||
struct bl_adc *_adc;
|
||||
struct bflb_dma_channel_lli_transfer_s transfers[1];
|
||||
_adc = (struct bl_adc *)device->parent.user_data;
|
||||
if (!bl_adc_channel_is_valid(channel))
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
struct bflb_adc_channel_s chan;
|
||||
chan.pos_chan = channel;
|
||||
chan.neg_chan = ADC_CHANNEL_GND;
|
||||
@@ -155,15 +194,22 @@ static rt_err_t bl_adc_enabled(struct rt_adc_device *device, rt_int8_t channel,
|
||||
static rt_err_t bl_adc_get_value(struct rt_adc_device *device, rt_int8_t channel, rt_uint32_t *value)
|
||||
{
|
||||
struct bl_adc *_adc;
|
||||
rt_size_t sample_index;
|
||||
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
RT_ASSERT(value != RT_NULL);
|
||||
|
||||
if (!bl_adc_channel_is_valid(channel))
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
_adc = device->parent.user_data;
|
||||
sample_index = BL_ADC_DMA_CHANNEL_OFFSET + (rt_size_t)channel;
|
||||
|
||||
rt_sem_take(_adc->sem, RT_WAITING_FOREVER);
|
||||
/* get ADC value */
|
||||
*value = (rt_uint32_t)raw_data[10 + channel];
|
||||
*value = (rt_uint32_t)raw_data[sample_index];
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user