PA2 ADC_CHANNEL_14
PA3 ADC_CHANNEL_15
PA4 ADC_CHANNEL_18
PA5 ADC_CHANNEL_19
PA6 ADC_CHANNEL_3
PA7 ADC_CHANNEL_7
ADC0采样顺序: PA2 -> PA4 -> PA6
ADC1采样顺序: PA3 -> PA5 -> PA7
使用ADC同步DMA模式1,从ADC_SYNCDATA0中读取数据(32bit),其中低16bit为ADC0,高16bit为ADC1
adc_value存储顺序: PA2 -> PA3 -> PA4 -> PA5 -> PA6 -> PA7
main.c
/*!
\file main.c
\brief running LED
\version 2023-03-31, V1.0.0, firmware for GD32H7xx
*/
/*
Copyright (c) 2023, GigaDevice Semiconductor Inc.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
#include "gd32h7xx.h"
#include "systick.h"
#include "string.h"
#include "stdio.h"
__attribute__((aligned(32))) uint16_t adc_value[6];
/*!
\brief enable the CPU Chache
\param[in] none
\param[out] none
\retval none
*/
static void cache_enable(void)
{
/* Enable I-Cache */
SCB_EnableICache();
/* Enable D-Cache */
// SCB_EnableDCache();
}
void led_config()
{
rcu_periph_clock_enable(RCU_GPIOJ);
gpio_mode_set(GPIOJ, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_8);
gpio_output_options_set(GPIOJ, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_8);
gpio_mode_set(GPIOJ, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_9);
gpio_output_options_set(GPIOJ, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_9);
gpio_bit_set(GPIOJ, GPIO_PIN_8);
gpio_bit_set(GPIOJ, GPIO_PIN_9);
}
void usart_config()
{
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_USART0);
gpio_af_set(GPIOB, GPIO_AF_7, GPIO_PIN_6);
gpio_af_set(GPIOB, GPIO_AF_7, GPIO_PIN_7);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_100_220MHZ, GPIO_PIN_6);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_7);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_100_220MHZ, GPIO_PIN_7);
usart_deinit(USART0);
usart_word_length_set(USART0, USART_WL_8BIT);
usart_stop_bit_set(USART0, USART_STB_1BIT);
usart_parity_config(USART0, USART_PM_NONE);
usart_baudrate_set(USART0, 921600U);
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
usart_enable(USART0);
}
void usart_transmit(char* buff, int size)
{
for (int i = 0; i < size; ++i) {
usart_data_transmit(USART0, buff[i]);
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE)) {}
}
}
void adc_config()
{
rcu_periph_clock_enable(RCU_GPIOA);
gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4);
rcu_periph_clock_enable(RCU_ADC0);
rcu_periph_clock_enable(RCU_ADC1);
adc_deinit(ADC0);
adc_clock_config(ADC0, ADC_CLK_SYNC_HCLK_DIV8); //300MHz / 8 = 37.5MHz ADC0/1时钟频率不能超过72MHz
adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
adc_special_function_config(ADC1, ADC_SCAN_MODE, ENABLE);
adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
adc_data_alignment_config(ADC1, ADC_DATAALIGN_RIGHT);
adc_resolution_config(ADC0, ADC_RESOLUTION_12B);
adc_resolution_config(ADC1, ADC_RESOLUTION_12B);
adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 3);
adc_channel_length_config(ADC1, ADC_REGULAR_CHANNEL, 3);
/*
*
* PA2 ADC_CHANNEL_14
* PA3 ADC_CHANNEL_15
* PA4 ADC_CHANNEL_18
* PA5 ADC_CHANNEL_19
* PA6 ADC_CHANNEL_3
* PA7 ADC_CHANNEL_7
*
* ADC0采样顺序: PA2 -> PA4 -> PA6
* ADC1采样顺序: PA3 -> PA5 -> PA7
*
* 使用ADC同步DMA模式1,从ADC_SYNCDATA0中读取数据(32bit),其中低16bit为ADC0,高16bit为ADC1
*
* adc_value存储顺序: PA2 -> PA3 -> PA4 -> PA5 -> PA6 -> PA7
*
*/
adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_14, 240);
adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_18, 240);
adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_3, 240);
adc_regular_channel_config(ADC1, 0, ADC_CHANNEL_15, 240);
adc_regular_channel_config(ADC1, 1, ADC_CHANNEL_19, 240);
adc_regular_channel_config(ADC1, 2, ADC_CHANNEL_7, 240);
//外部触发失能,使用软件触发
adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, EXTERNAL_TRIGGER_DISABLE);
//在同步模式中,当转换配置成外部触发,ADC1的外部触发必须失能
adc_external_trigger_config(ADC1, ADC_REGULAR_CHANNEL, EXTERNAL_TRIGGER_DISABLE);
adc_sync_mode_config(ADC_DAUL_REGULAL_PARALLEL); //同步常规并行模式
adc_sync_dma_config(ADC_SYNC_DMA_MODE1); //一次DMA请求传输2个数据
adc_sync_dma_request_after_last_enable();
adc_enable(ADC0);
adc_enable(ADC1);
delay_1ms(1);
adc_calibration_mode_config(ADC0, ADC_CALIBRATION_OFFSET_MISMATCH);
adc_calibration_mode_config(ADC1, ADC_CALIBRATION_OFFSET_MISMATCH);
adc_calibration_number(ADC0, ADC_CALIBRATION_NUM1);
adc_calibration_number(ADC1, ADC_CALIBRATION_NUM1);
adc_calibration_enable(ADC0);
adc_calibration_enable(ADC1);
}
void dma_config()
{
rcu_periph_clock_enable(RCU_DMA0);
rcu_periph_clock_enable(RCU_DMAMUX);
nvic_irq_enable(DMA0_Channel0_IRQn, 2, 0);
dma_single_data_parameter_struct dma_single_data_parameter;
dma_deinit(DMA0, DMA_CH0);
dma_single_data_parameter.request = DMA_REQUEST_ADC0;
dma_single_data_parameter.periph_addr = (uint32_t)(&ADC_SYNCDATA0);
dma_single_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_single_data_parameter.memory0_addr = (uint32_t)(&adc_value);
dma_single_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_single_data_parameter.periph_memory_width = DMA_PERIPH_WIDTH_32BIT;
dma_single_data_parameter.direction = DMA_PERIPH_TO_MEMORY;
dma_single_data_parameter.number = 3;
dma_single_data_parameter.priority = DMA_PRIORITY_HIGH;
dma_single_data_mode_init(DMA0, DMA_CH0, &dma_single_data_parameter);
dma_circulation_enable(DMA0, DMA_CH0);
dma_interrupt_enable(DMA0, DMA_CH0, DMA_INT_FTF);
dma_channel_enable(DMA0, DMA_CH0);
}
void DMA0_Channel0_IRQHandler()
{
if (RESET != dma_interrupt_flag_get(DMA0, DMA_CH0, DMA_INT_FLAG_FTF)) {
dma_interrupt_flag_clear(DMA0, DMA_CH0, DMA_INT_FLAG_FTF);
char buff[128];
snprintf(buff, 128, "PA2:%d PA3:%d PA4:%d PA5:%d PA6:%d PA7:%d\r\n",
adc_value[0], adc_value[1], adc_value[2], adc_value[3], adc_value[4], adc_value[5]);
usart_transmit(buff, strlen(buff));
gpio_bit_toggle(GPIOJ, GPIO_PIN_8);
}
}
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
/* enable the CPU Cache */
cache_enable();
/* configure systick */
systick_config();
led_config();
usart_config();
adc_config();
dma_config();
char msg[] = "Hello World\r\n";
usart_transmit(msg, strlen(msg));
while(1) {
adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
delay_1ms(1000);
}
}