AT32F435_437_Firmware_Library/project/at_start_f435/examples/adc/combine_mode_ordinary_smlt_.../src/main.c

265 lines
9.2 KiB
C

/**
**************************************************************************
* @file main.c
* @brief main program
**************************************************************************
* Copyright notice & Disclaimer
*
* The software Board Support Package (BSP) that is made available to
* download from Artery official website is the copyrighted work of Artery.
* Artery authorizes customers to use, copy, and distribute the BSP
* software and its related documentation for the purpose of design and
* development in conjunction with Artery microcontrollers. Use of the
* software is governed by this copyright notice and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
*
**************************************************************************
*/
#include "at32f435_437_board.h"
#include "at32f435_437_clock.h"
/** @addtogroup AT32F435_periph_examples
* @{
*/
/** @addtogroup 435_ADC_combine_mode_ordinary_smlt_oneslave_edma ADC_combine_mode_ordinary_smlt_oneslave_edma
* @{
*/
__IO uint32_t adccom_ordinary_valuetab[5][3];
__IO uint32_t edma_trans_complete_flag = 0;
__IO uint32_t adc1_overflow_flag = 0;
__IO uint32_t adc2_overflow_flag = 0;
static void gpio_config(void);
static void edma_config(void);
static void adc_config(void);
/**
* @brief gpio configuration.
* @param none
* @retval none
*/
static void gpio_config(void)
{
gpio_init_type gpio_initstructure;
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_initstructure);
/* config adc pin as analog input mode */
gpio_initstructure.gpio_mode = GPIO_MODE_ANALOG;
gpio_initstructure.gpio_pins = GPIO_PINS_4 | GPIO_PINS_5 | GPIO_PINS_6 | GPIO_PINS_7;
gpio_init(GPIOA, &gpio_initstructure);
gpio_initstructure.gpio_mode = GPIO_MODE_ANALOG;
gpio_initstructure.gpio_pins = GPIO_PINS_0 | GPIO_PINS_1;
gpio_init(GPIOB, &gpio_initstructure);
}
/**
* @brief edma configuration.
* @param none
* @retval none
*/
static void edma_config(void)
{
edma_init_type edma_init_struct;
crm_periph_clock_enable(CRM_EDMA_PERIPH_CLOCK, TRUE);
nvic_irq_enable(EDMA_Stream1_IRQn, 0, 0);
edma_reset(EDMA_STREAM1);
edma_default_para_init(&edma_init_struct);
edma_init_struct.buffer_size = 15;
edma_init_struct.direction = EDMA_DIR_PERIPHERAL_TO_MEMORY;
edma_init_struct.memory0_base_addr = (uint32_t)adccom_ordinary_valuetab;
edma_init_struct.memory_burst_mode = EDMA_MEMORY_SINGLE;
edma_init_struct.memory_data_width = EDMA_MEMORY_DATA_WIDTH_WORD;
edma_init_struct.memory_inc_enable = TRUE;
edma_init_struct.peripheral_base_addr = (uint32_t)&(ADCCOM->codt);
edma_init_struct.peripheral_burst_mode = EDMA_PERIPHERAL_SINGLE;
edma_init_struct.peripheral_data_width = EDMA_PERIPHERAL_DATA_WIDTH_WORD;
edma_init_struct.peripheral_inc_enable = FALSE;
edma_init_struct.priority = EDMA_PRIORITY_VERY_HIGH;
edma_init_struct.loop_mode_enable = FALSE;
edma_init_struct.fifo_threshold = EDMA_FIFO_THRESHOLD_1QUARTER;
edma_init_struct.fifo_mode_enable = FALSE;
edma_init(EDMA_STREAM1, &edma_init_struct);
/* edmamux init and enable */
edmamux_enable(TRUE);
edmamux_init(EDMAMUX_CHANNEL1, EDMAMUX_DMAREQ_ID_ADC1);
/* enable edma full data transfer interrupt */
edma_interrupt_enable(EDMA_STREAM1, EDMA_FDT_INT, TRUE);
edma_stream_enable(EDMA_STREAM1, TRUE);
}
/**
* @brief adc configuration.
* @param none
* @retval none
*/
static void adc_config(void)
{
adc_common_config_type adc_common_struct;
adc_base_config_type adc_base_struct;
crm_periph_clock_enable(CRM_ADC1_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_ADC2_PERIPH_CLOCK, TRUE);
nvic_irq_enable(ADC1_2_3_IRQn, 0, 0);
adc_common_default_para_init(&adc_common_struct);
/* config combine mode */
adc_common_struct.combine_mode = ADC_ORDINARY_SMLT_ONLY_ONESLAVE_MODE;
/* config division,adcclk is division by hclk */
adc_common_struct.div = ADC_HCLK_DIV_4;
/* config common dma mode,it's useful for ordinary group in combine mode */
adc_common_struct.common_dma_mode = ADC_COMMON_DMAMODE_2;
/* config common dma request repeat */
adc_common_struct.common_dma_request_repeat_state = FALSE;
/* config adjacent adc sampling interval,it's useful for ordinary shifting mode */
adc_common_struct.sampling_interval = ADC_SAMPLING_INTERVAL_5CYCLES;
/* config inner temperature sensor and vintrv */
adc_common_struct.tempervintrv_state = FALSE;
/* config voltage battery */
adc_common_struct.vbat_state = FALSE;
adc_common_config(&adc_common_struct);
adc_base_default_para_init(&adc_base_struct);
adc_base_struct.sequence_mode = TRUE;
adc_base_struct.repeat_mode = FALSE;
adc_base_struct.data_align = ADC_RIGHT_ALIGNMENT;
adc_base_struct.ordinary_channel_length = 3;
adc_base_config(ADC1, &adc_base_struct);
adc_resolution_set(ADC1, ADC_RESOLUTION_12B);
/* config ordinary channel */
adc_ordinary_channel_set(ADC1, ADC_CHANNEL_4, 1, ADC_SAMPLETIME_640_5);
adc_ordinary_channel_set(ADC1, ADC_CHANNEL_5, 2, ADC_SAMPLETIME_640_5);
adc_ordinary_channel_set(ADC1, ADC_CHANNEL_6, 3, ADC_SAMPLETIME_640_5);
/* config ordinary trigger source and trigger edge */
adc_ordinary_conversion_trigger_set(ADC1, ADC_ORDINARY_TRIG_TMR1CH1, ADC_ORDINARY_TRIG_EDGE_NONE);
/* config dma mode,it's not useful when common dma mode is use */
adc_dma_mode_enable(ADC1, FALSE);
/* config dma request repeat,it's not useful when common dma mode is use */
adc_dma_request_repeat_enable(ADC1, FALSE);
/* enable adc overflow interrupt */
adc_interrupt_enable(ADC1, ADC_OCCO_INT, TRUE);
adc_base_config(ADC2, &adc_base_struct);
adc_resolution_set(ADC2, ADC_RESOLUTION_12B);
adc_ordinary_channel_set(ADC2, ADC_CHANNEL_7, 1, ADC_SAMPLETIME_640_5);
adc_ordinary_channel_set(ADC2, ADC_CHANNEL_8, 2, ADC_SAMPLETIME_640_5);
adc_ordinary_channel_set(ADC2, ADC_CHANNEL_9, 3, ADC_SAMPLETIME_640_5);
adc_ordinary_conversion_trigger_set(ADC2, ADC_ORDINARY_TRIG_TMR1CH1, ADC_ORDINARY_TRIG_EDGE_NONE);
adc_dma_mode_enable(ADC2, FALSE);
adc_dma_request_repeat_enable(ADC2, FALSE);
adc_interrupt_enable(ADC2, ADC_OCCO_INT, TRUE);
/* adc enable */
adc_enable(ADC1, TRUE);
adc_enable(ADC2, TRUE);
while(adc_flag_get(ADC1, ADC_RDY_FLAG) == RESET);
while(adc_flag_get(ADC2, ADC_RDY_FLAG) == RESET);
/* adc calibration */
adc_calibration_init(ADC1);
while(adc_calibration_init_status_get(ADC1));
adc_calibration_start(ADC1);
while(adc_calibration_status_get(ADC1));
adc_calibration_init(ADC2);
while(adc_calibration_init_status_get(ADC2));
adc_calibration_start(ADC2);
while(adc_calibration_status_get(ADC2));
}
/**
* @brief main function.
* @param none
* @retval none
*/
int main(void)
{
__IO uint32_t index = 0;
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
/* config the system clock */
system_clock_config();
/* init at start board */
at32_board_init();
at32_led_off(LED2);
at32_led_off(LED3);
at32_led_off(LED4);
uart_print_init(115200);
gpio_config();
edma_config();
adc_config();
printf("combine_mode_ordinary_simult \r\n");
/* adc1 software trigger start conversion */
for(index = 0; index < 5; index++)
{
adc_ordinary_software_trigger_enable(ADC1, TRUE);
delay_ms(100);
}
if((edma_trans_complete_flag == 0) || (adc1_overflow_flag != 0) || (adc2_overflow_flag != 0))
{
/* printf flag when error occur */
at32_led_on(LED3);
at32_led_on(LED4);
printf("error occur\r\n");
printf("edma_trans_complete_flag = %d\r\n",edma_trans_complete_flag);
printf("adc1_overflow_flag = %d\r\n",adc1_overflow_flag);
printf("adc2_overflow_flag = %d\r\n",adc2_overflow_flag);
}
else
{
/* printf data when conversion end without error */
printf("conversion end without error\r\n");
for(index = 0; index < 5; index++)
{
printf("adc1_ordinary_channel4[%d] = 0x%x\r\n",index, adccom_ordinary_valuetab[index][0] & 0xFFFF);
printf("adc1_ordinary_channel5[%d] = 0x%x\r\n",index, adccom_ordinary_valuetab[index][1] & 0xFFFF);
printf("adc1_ordinary_channel6[%d] = 0x%x\r\n",index, adccom_ordinary_valuetab[index][2] & 0xFFFF);
printf("adc2_ordinary_channel7[%d] = 0x%x\r\n",index, (adccom_ordinary_valuetab[index][0] >> 16) & 0xFFFF);
printf("adc2_ordinary_channel8[%d] = 0x%x\r\n",index, (adccom_ordinary_valuetab[index][1] >> 16) & 0xFFFF);
printf("adc2_ordinary_channel9[%d] = 0x%x\r\n",index, (adccom_ordinary_valuetab[index][2] >> 16) & 0xFFFF);
printf("\r\n");
}
}
at32_led_on(LED2);
while(1)
{
}
}
/**
* @}
*/
/**
* @}
*/