From fe34801106bc179fee5446ee298cb2e66b031562 Mon Sep 17 00:00:00 2001 From: jzlv Date: Mon, 21 Mar 2022 15:00:15 +0800 Subject: [PATCH] [feat][adc_dma] add adc dma driver and demo --- drivers/bl702_driver/hal_drv/inc/hal_adc.h | 10 +- drivers/bl702_driver/hal_drv/src/hal_adc.c | 7 +- examples/adc/adc_dma/CMakeLists.txt | 5 + examples/adc/adc_dma/cdk/adc_dma.cdkproj | 735 +++++++++++++++++++++ examples/adc/adc_dma/main.c | 106 +++ examples/adc/adc_dma/readme.md | 5 + 6 files changed, 866 insertions(+), 2 deletions(-) create mode 100644 examples/adc/adc_dma/CMakeLists.txt create mode 100644 examples/adc/adc_dma/cdk/adc_dma.cdkproj create mode 100644 examples/adc/adc_dma/main.c create mode 100644 examples/adc/adc_dma/readme.md diff --git a/drivers/bl702_driver/hal_drv/inc/hal_adc.h b/drivers/bl702_driver/hal_drv/inc/hal_adc.h index 92ebca2f..d4e84258 100644 --- a/drivers/bl702_driver/hal_drv/inc/hal_adc.h +++ b/drivers/bl702_driver/hal_drv/inc/hal_adc.h @@ -38,6 +38,7 @@ extern "C" { #define DEVICE_CTRL_ADC_VBAT_OFF 0x14 #define DEVICE_CTRL_ADC_TSEN_ON 0x15 #define DEVICE_CTRL_ADC_TSEN_OFF 0x16 +#define DEVICE_CTRL_ADC_DATA_PARSE 0x17 enum adc_index_type { #ifdef BSP_USING_ADC0 @@ -153,6 +154,13 @@ typedef struct { float volt; /*!< ADC voltage result */ } adc_channel_val_t; +typedef struct +{ + uint32_t *input; + adc_channel_val_t *output; + uint32_t num; +} adc_data_parse_t; + typedef struct adc_device { struct device parent; adc_clk_div_t clk_div; /* CLK is not more than 2Mhz */ @@ -162,7 +170,7 @@ typedef struct adc_device { adc_data_width_t data_width; adc_fifo_threshold_t fifo_threshold; adc_pga_gain_t gain; - void* rx_dma; + void *rx_dma; } adc_device_t; #define ADC_DEV(dev) ((adc_device_t *)dev) diff --git a/drivers/bl702_driver/hal_drv/src/hal_adc.c b/drivers/bl702_driver/hal_drv/src/hal_adc.c index 0c7bec78..1ceae33e 100644 --- a/drivers/bl702_driver/hal_drv/src/hal_adc.c +++ b/drivers/bl702_driver/hal_drv/src/hal_adc.c @@ -279,9 +279,14 @@ int adc_control(struct device *dev, int cmd, void *args) case DEVICE_CTRL_ADC_TSEN_OFF: break; - case DEVICE_CTRL_ATTACH_RX_DMA : + case DEVICE_CTRL_ATTACH_RX_DMA: adc_device->rx_dma = (struct device *)args; break; + case DEVICE_CTRL_ADC_DATA_PARSE: { + adc_data_parse_t *parse = (adc_data_parse_t *)args; + ADC_Parse_Result(parse->input, parse->num, (ADC_Result_Type *)parse->output); + } + break; default: break; } diff --git a/examples/adc/adc_dma/CMakeLists.txt b/examples/adc/adc_dma/CMakeLists.txt new file mode 100644 index 00000000..bfd0e557 --- /dev/null +++ b/examples/adc/adc_dma/CMakeLists.txt @@ -0,0 +1,5 @@ +set(mains main.c) +generate_bin() + + + diff --git a/examples/adc/adc_dma/cdk/adc_dma.cdkproj b/examples/adc/adc_dma/cdk/adc_dma.cdkproj new file mode 100644 index 00000000..8370f58f --- /dev/null +++ b/examples/adc/adc_dma/cdk/adc_dma.cdkproj @@ -0,0 +1,735 @@ + + + CPU: RV32IMAFC +Chip: bl70x +Board: bl70x_iot + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 154 + 104 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + no + 0x23000000 + 0x100000 + + + no + 0x22014000 + 0x4000 + + + no + 0x42018000 + 0x8000 + + + no + + + + + no + + + + + + + yes + 0x42020000 + 0xc000 + yes + + + no + + + yes + + + no + + + yes + + + no + + + yes + + + no + + + yes + + + rv32imafc + yes + little + no + no + no + no + + + $(ProjectName) + Executable + no + yes + no + yes + no + yes + + + + no + + + + no + + + + no + $(ProjectPath)../../../../tools/bflb_flash_tool/bflb_mcu_tool.exe --chipname=bl702 --interface=openocd --firmware="$(ProjectPath)/Obj/$(ProjectName).bin" + + + + ARCH_RISCV;BFLB_USE_HAL_DRIVER;BFLB_USE_ROM_DRIVER;bl706_iot + + Optimize more (-O2) + Default (-g) + $(ProjectPath);$(ProjectPath)../;$(ProjectPath)../../../../components/fatfs;$(ProjectPath)../../../../components/freertos/Source/include;$(ProjectPath)../../../../components/shell;$(ProjectPath)../../../../components/usb_stack/class/audio;$(ProjectPath)../../../../components/usb_stack/class/cdc;$(ProjectPath)../../../../components/usb_stack/class/hid;$(ProjectPath)../../../../components/usb_stack/class/msc;$(ProjectPath)../../../../components/usb_stack/class/video;$(ProjectPath)../../../../components/usb_stack/class/webusb;$(ProjectPath)../../../../components/usb_stack/class/winusb;$(ProjectPath)../../../../components/usb_stack/common;$(ProjectPath)../../../../components/usb_stack/core;$(ProjectPath)../../../../bsp/board/bl702;$(ProjectPath)../../../../bsp/bsp_common/platform;$(ProjectPath)../../../../common/device;$(ProjectPath)../../../../common/list;$(ProjectPath)../../../../common/memheap;$(ProjectPath)../../../../common/misc;$(ProjectPath)../../../../common/ring_buffer;$(ProjectPath)../../../../common/soft_crc;$(ProjectPath)../../../../components/shell;$(ProjectPath)../../../../drivers/bl702_driver;$(ProjectPath)../../../../drivers/bl702_driver/hal_drv/default_config;$(ProjectPath)../../../../drivers/bl702_driver/hal_drv/inc;$(ProjectPath)../../../../drivers/bl702_driver/regs;$(ProjectPath)../../../../drivers/bl702_driver/startup;$(ProjectPath)../../../../drivers/bl702_driver/std_drv/inc + -fshort-enums -fno-common -fms-extensions -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -Wall -Wshift-negative-value -Wchar-subscripts -Wformat -Wuninitialized -Winit-self -Wignored-qualifiers -Wunused -Wundef -msmall-data-limit=4 -std=c99 + no + no + no + no + no + no + yes + no + yes + no + + + + + $(ProjectPath);$(ProjectPath)../;$(ProjectPath)../../../../components/fatfs;$(ProjectPath)../../../../components/freertos/Source/include;$(ProjectPath)../../../../components/shell;$(ProjectPath)../../../../components/usb_stack/class/audio;$(ProjectPath)../../../../components/usb_stack/class/cdc;$(ProjectPath)../../../../components/usb_stack/class/hid;$(ProjectPath)../../../../components/usb_stack/class/msc;$(ProjectPath)../../../../components/usb_stack/class/video;$(ProjectPath)../../../../components/usb_stack/class/webusb;$(ProjectPath)../../../../components/usb_stack/class/winusb;$(ProjectPath)../../../../components/usb_stack/common;$(ProjectPath)../../../../components/usb_stack/core;$(ProjectPath)../../../../bsp/board/bl702;$(ProjectPath)../../../../bsp/bsp_common/platform;$(ProjectPath)../../../../common/device;$(ProjectPath)../../../../common/list;$(ProjectPath)../../../../common/memheap;$(ProjectPath)../../../../common/misc;$(ProjectPath)../../../../common/ring_buffer;$(ProjectPath)../../../../common/soft_crc;$(ProjectPath)../../../../components/shell;$(ProjectPath)../../../../drivers/bl702_driver;$(ProjectPath)../../../../drivers/bl702_driver/hal_drv/default_config;$(ProjectPath)../../../../drivers/bl702_driver/hal_drv/inc;$(ProjectPath)../../../../drivers/bl702_driver/regs;$(ProjectPath)../../../../drivers/bl702_driver/startup;$(ProjectPath)../../../../drivers/bl702_driver/std_drv/inc;$(ProjectPath)../../../../drivers/bl602_driver/risc-v/Core/Include;$(ProjectPath)../../../../drivers/bl602_driver/startup;$(ProjectPath)../../../../drivers/bl602_driver/hal_drv/inc;$(ProjectPath)../../../../drivers/bl602_driver/hal_drv/default_config;$(ProjectPath)../../../../drivers/bl602_driver/regs;$(ProjectPath)../../../../drivers/bl602_driver/std_drv/inc + + gdwarf2 + + + yes + yes + $(ProjectPath)../../../../drivers/bl702_driver/bl702_flash.ld + c + + --specs=nano.specs + no + + + + yes + OpenOCD + yes + main + + $(ProjectPath)/../../../../tools/openocd/bl70x_gdb.init + yes + Hard Reset + 23000000 + no + no + $(ProjectPath)/$(ProjectName).cdkcore + + localhost + 1025 + 0 + 2000 + 10 + 50 + yes + no + no + Normal + Hard Reset + 21000000 + Bare Metal + yes + yes + + Local + -arch riscv + + + + + yes + no + no + + + openocd-hifive + no + 4444 + no + 6666 + -f ../../../../tools/openocd/if_rv_dbg_plus.cfg -f ../../../../tools/openocd/tgt_702.cfg + + + + + Erase Sectors + bl70x_flasher.elf + yes + yes + no + Hard Reset + + no + + + + + + + + + no + 0x23000000 + 0x100000 + + + no + 0x22014000 + 0x4000 + + + no + 0x42018000 + 0x8000 + + + no + + + + + no + + + + + + + yes + 0x42020000 + 0xc000 + yes + + + no + + + yes + + + no + + + yes + + + no + + + yes + + + no + + + yes + + + rv32imafc + yes + little + no + no + no + no + + + $(ProjectName) + Executable + no + yes + no + yes + no + yes + + + + no + + + + no + + + + no + $(ProjectPath)../../../../tools/bflb_flash_tool/bflb_mcu_tool.exe --chipname=bl702 --interface=openocd --firmware="$(ProjectPath)/Obj/$(ProjectName).bin" + + + + ARCH_RISCV;BFLB_USE_HAL_DRIVER;BFLB_USE_ROM_DRIVER;bl706_iot + + Optimize more (-O2) + Default (-g) + $(ProjectPath);$(ProjectPath)../;$(ProjectPath)../../../../components/fatfs;$(ProjectPath)../../../../components/freertos/Source/include;$(ProjectPath)../../../../components/shell;$(ProjectPath)../../../../components/usb_stack/class/audio;$(ProjectPath)../../../../components/usb_stack/class/cdc;$(ProjectPath)../../../../components/usb_stack/class/hid;$(ProjectPath)../../../../components/usb_stack/class/msc;$(ProjectPath)../../../../components/usb_stack/class/video;$(ProjectPath)../../../../components/usb_stack/class/webusb;$(ProjectPath)../../../../components/usb_stack/class/winusb;$(ProjectPath)../../../../components/usb_stack/common;$(ProjectPath)../../../../components/usb_stack/core;$(ProjectPath)../../../../bsp/board/bl702;$(ProjectPath)../../../../bsp/bsp_common/platform;$(ProjectPath)../../../../common/device;$(ProjectPath)../../../../common/list;$(ProjectPath)../../../../common/memheap;$(ProjectPath)../../../../common/misc;$(ProjectPath)../../../../common/ring_buffer;$(ProjectPath)../../../../common/soft_crc;$(ProjectPath)../../../../components/shell;$(ProjectPath)../../../../drivers/bl702_driver;$(ProjectPath)../../../../drivers/bl702_driver/hal_drv/default_config;$(ProjectPath)../../../../drivers/bl702_driver/hal_drv/inc;$(ProjectPath)../../../../drivers/bl702_driver/regs;$(ProjectPath)../../../../drivers/bl702_driver/startup;$(ProjectPath)../../../../drivers/bl702_driver/std_drv/inc;$(ProjectPath)../../../../drivers/bl602_driver/risc-v/Core/Include;$(ProjectPath)../../../../drivers/bl602_driver/hal_drv/inc;$(ProjectPath)../../../../drivers/bl602_driver/hal_drv/default_config;$(ProjectPath)../../../../drivers/bl602_driver/regs;$(ProjectPath)../../../../drivers/bl602_driver/std_drv/inc + -fshort-enums -fno-common -fms-extensions -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -Wall -Wshift-negative-value -Wchar-subscripts -Wformat -Wuninitialized -Winit-self -Wignored-qualifiers -Wunused -Wundef -msmall-data-limit=4 -std=c99 + no + no + no + no + no + no + yes + no + yes + no + + + + + $(ProjectPath);$(ProjectPath)../;$(ProjectPath)../../../../components/fatfs;$(ProjectPath)../../../../components/freertos/Source/include;$(ProjectPath)../../../../components/shell;$(ProjectPath)../../../../components/usb_stack/class/audio;$(ProjectPath)../../../../components/usb_stack/class/cdc;$(ProjectPath)../../../../components/usb_stack/class/hid;$(ProjectPath)../../../../components/usb_stack/class/msc;$(ProjectPath)../../../../components/usb_stack/class/video;$(ProjectPath)../../../../components/usb_stack/class/webusb;$(ProjectPath)../../../../components/usb_stack/class/winusb;$(ProjectPath)../../../../components/usb_stack/common;$(ProjectPath)../../../../components/usb_stack/core;$(ProjectPath)../../../../bsp/board/bl702;$(ProjectPath)../../../../bsp/bsp_common/platform;$(ProjectPath)../../../../common/device;$(ProjectPath)../../../../common/list;$(ProjectPath)../../../../common/memheap;$(ProjectPath)../../../../common/misc;$(ProjectPath)../../../../common/ring_buffer;$(ProjectPath)../../../../common/soft_crc;$(ProjectPath)../../../../components/shell;$(ProjectPath)../../../../drivers/bl702_driver;$(ProjectPath)../../../../drivers/bl702_driver/hal_drv/default_config;$(ProjectPath)../../../../drivers/bl702_driver/hal_drv/inc;$(ProjectPath)../../../../drivers/bl702_driver/regs;$(ProjectPath)../../../../drivers/bl702_driver/startup;$(ProjectPath)../../../../drivers/bl702_driver/std_drv/inc;$(ProjectPath)../../../../drivers/bl602_driver/risc-v/Core/Include;$(ProjectPath)../../../../drivers/bl602_driver/startup;$(ProjectPath)../../../../drivers/bl602_driver/hal_drv/inc;$(ProjectPath)../../../../drivers/bl602_driver/hal_drv/default_config;$(ProjectPath)../../../../drivers/bl602_driver/regs;$(ProjectPath)../../../../drivers/bl602_driver/std_drv/inc + + gdwarf2 + + + yes + yes + $(ProjectPath)../../../../drivers/bl702_driver/bl702_flash.ld + c + + --specs=nano.specs + no + + + + yes + ICE + yes + main + + $(ProjectPath)/../../../../tools/openocd/bl70x_gdb.init + yes + Hard Reset + 23000000 + no + no + $(ProjectPath)/$(ProjectName).cdkcore + + localhost + 1025 + 0 + 2000 + 10 + 50 + yes + no + no + Normal + Hard Reset + 21000000 + Bare Metal + yes + yes + + Local + -arch riscv + + + + + yes + no + no + + + openocd-hifive + no + 4444 + no + 6666 + -f ../../../../tools/openocd/if_rv_dbg_plus.cfg -f ../../../../tools/openocd/tgt_702.cfg + + + + + Erase Sectors + bl702_flasher.elf + yes + yes + no + Hard Reset + + no + + + + + + + + ;;; + ;;MHZ; + + glb + uart + + 1 + 1 + + 100:8;100:8;100:8;100:8; + + + + ;;; + ;;MHZ; + + glb + uart + + 1 + 1 + + 100:8;100:8;100:8;100:8; + + diff --git a/examples/adc/adc_dma/main.c b/examples/adc/adc_dma/main.c new file mode 100644 index 00000000..db800867 --- /dev/null +++ b/examples/adc/adc_dma/main.c @@ -0,0 +1,106 @@ +/** + * @file main.c + * @brief + * + * Copyright (c) 2021 Bouffalolab team + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + */ +#include "bflb_platform.h" +#include "hal_adc.h" +#include "hal_dma.h" + +adc_channel_t posChList[] = { ADC_CHANNEL4 }; +adc_channel_t negChList[] = { ADC_CHANNEL_GND }; + +adc_channel_val_t result_val[16]; + +struct device *adc_test; + +uint32_t adc_buffer[16]; + +adc_data_parse_t data_parse; + +void dma_ch0_irq_callback(struct device *dev, void *args, uint32_t size, uint32_t state) +{ + device_control(adc_test, DEVICE_CTRL_ADC_DATA_PARSE, (void*)&data_parse); + for (uint8_t i = 0; i < 16; i++) { + MSG("PosId = %d NegId = %d V= %d mV \r\n", result_val[i].posChan, result_val[i].negChan, (uint32_t)(result_val[i].volt * 1000)); + } +} + +int main(void) +{ + bflb_platform_init(0); + + adc_channel_cfg_t adc_channel_cfg; + + adc_channel_cfg.pos_channel = posChList; + adc_channel_cfg.neg_channel = negChList; + adc_channel_cfg.num = 1; + + adc_register(ADC0_INDEX, "adc"); + + adc_test = device_find("adc"); + + if (adc_test) { + ADC_DEV(adc_test)->continuous_conv_mode = ENABLE; + ADC_DEV(adc_test)->fifo_threshold = ADC_FIFO_THRESHOLD_16BYTE; + device_open(adc_test, DEVICE_OFLAG_DMA_RX); + if (device_control(adc_test, DEVICE_CTRL_ADC_CHANNEL_CONFIG, &adc_channel_cfg) == ERROR) { + MSG("ADC channel config error , Please check the channel corresponding to IO is initial success by board system or Channel is invaild \r\n"); + BL_CASE_FAIL; + while (1) + ; + } + MSG("adc device find success\r\n"); + } + dma_register(DMA0_CH0_INDEX, "dma_ch0"); + struct device *dma_ch0 = device_find("dma_ch0"); + + if (dma_ch0) { + DMA_DEV(dma_ch0)->direction = DMA_PERIPH_TO_MEMORY; + DMA_DEV(dma_ch0)->transfer_mode = DMA_LLI_ONCE_MODE; + DMA_DEV(dma_ch0)->src_req = DMA_REQUEST_ADC0; + DMA_DEV(dma_ch0)->dst_req = DMA_REQUEST_NONE; + DMA_DEV(dma_ch0)->src_addr_inc = DMA_ADDR_INCREMENT_DISABLE; + DMA_DEV(dma_ch0)->dst_addr_inc = DMA_ADDR_INCREMENT_ENABLE; + DMA_DEV(dma_ch0)->src_burst_size = DMA_BURST_1BYTE; + DMA_DEV(dma_ch0)->dst_burst_size = DMA_BURST_1BYTE; + DMA_DEV(dma_ch0)->src_width = DMA_TRANSFER_WIDTH_32BIT; + DMA_DEV(dma_ch0)->dst_width = DMA_TRANSFER_WIDTH_32BIT; + device_open(dma_ch0, 0); + device_set_callback(dma_ch0, dma_ch0_irq_callback); + device_control(dma_ch0, DEVICE_CTRL_SET_INT, NULL); + } + + /* connect dac device and dma device */ + device_control(adc_test, DEVICE_CTRL_ATTACH_RX_DMA, dma_ch0); + + data_parse.input = adc_buffer; + data_parse.output = result_val; + data_parse.num = 16; + + adc_channel_start(adc_test); + while (1) { + if (!dma_channel_check_busy(dma_ch0)) { + device_read(adc_test, 0, adc_buffer, sizeof(adc_buffer) / sizeof(uint8_t));/* size need convert to uint8_t*/ + } + + bflb_platform_delay_ms(500); + } +} \ No newline at end of file diff --git a/examples/adc/adc_dma/readme.md b/examples/adc/adc_dma/readme.md new file mode 100644 index 00000000..272e4750 --- /dev/null +++ b/examples/adc/adc_dma/readme.md @@ -0,0 +1,5 @@ +```bash + +$ make APP=adc_dma BOARD=bl706_iot + +``` \ No newline at end of file