V1.0
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
/*!
|
||||
* \file adc.c
|
||||
*
|
||||
* \brief Target adc implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "adc.h"
|
||||
#include "xc_drv_adc.h"
|
||||
#include "xc_drv_systick.h"
|
||||
#include "main.h"
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief adc_demo
|
||||
* @details Sampling once per second with 3.3v reference voltage
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
|
||||
void adc_demo(void)
|
||||
{
|
||||
DEBUG("__ADC_DEMO__\r");
|
||||
|
||||
uint16_t integer;
|
||||
uint16_t decimal;
|
||||
uint16_t adc_val;
|
||||
uint16_t adc_2v48_param;
|
||||
uint16_t after_calibrat_adc_val;
|
||||
ADC_InitCfg_t adc_cfg ;
|
||||
|
||||
/*----- Special Read the 2.48 v calibration value -----*/
|
||||
/*< If the content of the parameter
|
||||
storage area is invalid, the default assignment is 10000 >*/
|
||||
if(false == xc_adc_2v4_calibrate_read(&adc_2v48_param))
|
||||
{
|
||||
adc_2v48_param = 10000;
|
||||
}
|
||||
|
||||
/*----- Special Pin -----*/
|
||||
/*< The SWD and SWCK pins can also be used as ADC pins.
|
||||
Pay attention to the function remapping of the PIN pins. >*/
|
||||
|
||||
adc_cfg.Freq = ADC_FREQ_2M;
|
||||
adc_cfg.RefVol = ADC_REF_VOL_2_48V;
|
||||
adc_cfg.SampEdge = ADC_SAMPEDGE_RISE;
|
||||
adc_cfg.ExtDataMode = ADC_EXT_DATA_MODE_32BIT;
|
||||
adc_cfg.ExtEdgeSel = ADC_EXT_EDGE_SEL_INTER;
|
||||
adc_cfg.ExtSampleNum = ADC_EXT_SAMPLE_NUM_8;
|
||||
adc_cfg.ExtTriggerSel = ADC_EXT_TRIGGER_SEL_PWM0;
|
||||
|
||||
xc_adc_init(&adc_cfg);
|
||||
|
||||
while(1)
|
||||
{
|
||||
|
||||
xc_adc_start_get_value( ADC_CH5_PIN1 ,&adc_val);
|
||||
|
||||
if(adc_cfg.RefVol == ADC_REF_VOL_3_3V)
|
||||
{
|
||||
integer = ((adc_val)*ADV_REF_VOLT_3_3V*100)/(1.0*4096)/100;
|
||||
decimal = ((uint32_t)((adc_val*ADV_REF_VOLT_3_3V*100)/4096)%100);
|
||||
|
||||
DEBUG("3.3v ref adc_vol=%d.%02d ,adc_val=%d\n", integer, decimal,adc_val);
|
||||
}
|
||||
else if(adc_cfg.RefVol == ADC_REF_VOL_2_48V)
|
||||
{
|
||||
after_calibrat_adc_val = adc_val *adc_2v48_param/10000;
|
||||
integer = ((adc_val)*ADV_REF_VOLT_2_4_8V*100)/(1.0*4096)/100;
|
||||
decimal = ((uint32_t)((adc_val*ADV_REF_VOLT_2_4_8V*100)/4096)%100);
|
||||
DEBUG("2.48v ref adc_vol=%d.%02d ,adc_val=%d\n", integer, decimal,adc_val);
|
||||
|
||||
integer = ((after_calibrat_adc_val)*ADV_REF_VOLT_2_4_8V*100)/(1.0*4096)/100;
|
||||
decimal = ((uint32_t)((after_calibrat_adc_val*ADV_REF_VOLT_2_4_8V*100)/4096)%100);
|
||||
DEBUG("after_calibrat 2.48v ref adc_vol=%d.%02d ,adc_val=%d\n", integer, decimal,after_calibrat_adc_val);
|
||||
}
|
||||
delay_ms(1000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
/*!
|
||||
* \file main.c
|
||||
*
|
||||
* \brief Target main implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
void flash_init()
|
||||
{
|
||||
uint32_t mid = 0;
|
||||
uint32_t flash_size;
|
||||
uint16_t flash_type;
|
||||
xc_fmc_spi_init_oprt( );
|
||||
xc_fmc_spi_flash_wake_up( );
|
||||
|
||||
xc_fmc_spi_flash_rdid((uint8_t*)&mid);
|
||||
printf("Flash RDID: 0x%08x\n", mid);
|
||||
|
||||
uint8_t ruid[16];
|
||||
xc_fmc_spi_flash_ruid(ruid);
|
||||
printf("Flash RUID11: ");
|
||||
for(int i = 0;i < 16;i++)
|
||||
{
|
||||
printf("%02x ", ruid[i]);
|
||||
}
|
||||
printf("\n");
|
||||
#if (PLF_NVDS)
|
||||
if(false == flash_size_and_type_get(&flash_size, &flash_type))
|
||||
{
|
||||
printf("Flash Memory size Get ERROR ! ");
|
||||
while(1);
|
||||
}
|
||||
|
||||
nvds_space_init(flash_size);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
**************************************************************************************
|
||||
* @brief app_uart_init
|
||||
* @details uart init , For debugging
|
||||
* @param void
|
||||
* @retval void
|
||||
***************************************************************************************
|
||||
*/
|
||||
void app_uart_init(void)
|
||||
{
|
||||
GPIO_InitCfg_t gpio_cfg = {0};
|
||||
|
||||
gpio_cfg.Mux = GPIO_Mux0;
|
||||
gpio_cfg.Pull = GPIO_PULLUP;
|
||||
gpio_cfg.Int = NOT_INT;
|
||||
gpio_cfg.FunSel = UART0_TX;
|
||||
|
||||
gpio_cfg.Pin = GPIO_18;
|
||||
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
|
||||
xc_gpio_init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = UART0_RX;
|
||||
gpio_cfg.Pin = GPIO_19;
|
||||
gpio_cfg.Dir = GPIO_DIR_INPUT;
|
||||
xc_gpio_init(&gpio_cfg);
|
||||
|
||||
UART_InitCfg_t uart_cfg = {0};
|
||||
|
||||
uart_cfg.Parity = UART_PARITY_DISABLE;
|
||||
uart_cfg.StopBits = UART_STOP_1_BITS;
|
||||
uart_cfg.WordLength = UART_DATA_8_BITS;
|
||||
uart_cfg.BaudRate = UART_BAUDRATE_115200;
|
||||
uart_cfg.HardwareFlowControl = UART_HWFC_DISABLE;
|
||||
|
||||
xc_uart_init(UART0_IDX, &uart_cfg);
|
||||
}
|
||||
|
||||
/**
|
||||
**************************************************************************************
|
||||
* @brief clock_init
|
||||
* @details Example Initialize the system clock
|
||||
* @param void
|
||||
* @retval void
|
||||
***************************************************************************************
|
||||
*/
|
||||
void clock_init(void)
|
||||
{
|
||||
CLOCK_InitCfg_t clock_cfg;
|
||||
|
||||
clock_cfg.hfclk_src = CLOCK_HFCLK_SRC_XTAL; //Source clock selection
|
||||
clock_cfg.hfclk_in = CLOCK_HFCLK_IN_32M; //system clock selection
|
||||
clock_cfg.lfclk_src = CLOCK_LFCLK_SRC_RC; //Internal 32K clock or external 32K clock
|
||||
|
||||
if (clock_cfg.lfclk_src == CLOCK_LFCLK_SRC_XTAL) {
|
||||
clock_cfg.lfclk_in = CLOCK_LFCLK_IN_32768;
|
||||
} else if (clock_cfg.lfclk_src == CLOCK_LFCLK_SRC_RC) {
|
||||
clock_cfg.lfclk_in = CLOCK_LFCLK_IN_32K;
|
||||
}
|
||||
|
||||
xc_clock_init_cfg(&clock_cfg);
|
||||
|
||||
SysTick_Config(xc_clock_hfclk_in_get() / 100);
|
||||
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* system clock init */
|
||||
clock_init();
|
||||
|
||||
/* uart init */
|
||||
app_uart_init();
|
||||
|
||||
flash_init();
|
||||
|
||||
/* rc32k clock calibration */
|
||||
xc_rc32k_calib_by_hw();
|
||||
|
||||
/* ADC DEMO */
|
||||
adc_demo();
|
||||
|
||||
while (1) {
|
||||
/* main loop */
|
||||
;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user