hpw422移植新的sdk
This commit is contained in:
@@ -0,0 +1,440 @@
|
||||
/*!
|
||||
* \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 "xc6xxx_hal_adc.h"
|
||||
#include "xc6xxx_hal_adc_dmac.h"
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
uint8_t adc_ch_buff[1000];
|
||||
extern uint32_t adc_val_buff[500];
|
||||
uint16_t adc_con = 0;
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief adc_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
|
||||
void adc_demo(void)
|
||||
{
|
||||
DEBUG("__ADC_DEMO__\r");
|
||||
DEBUG("XC_ADC->RF_CTL:%x\n", XC_ADC->RF_CTL);
|
||||
|
||||
uint16_t integer;
|
||||
uint16_t decimal;
|
||||
uint16_t adc_val = 0;
|
||||
adc_cfg adc_cfg;
|
||||
|
||||
/*----- Special Pin -----*/
|
||||
/*< SWD 和 SWCK 引脚也可以作为 ADC 使用,
|
||||
注意 PIN 脚的功能重映射 >*/
|
||||
// GPIO_MuxCtl(12,1);
|
||||
// GPIO_MuxCtl(13,1);
|
||||
/*-----------------------*/
|
||||
|
||||
// *((volatile unsigned *)(0x4002F000 + 0x03C)) &= ~(0x1<<14);
|
||||
|
||||
adc_cfg.Freq = ADC_FREQ_2M;
|
||||
adc_cfg.RefVol = ADC_REF_VOL_3_3V;
|
||||
adc_cfg.SampEdge = ADC_SAMPEDGE_FALL;
|
||||
adc_cfg.ExtDataMode = ADC_DATA_MODE_32BIT;
|
||||
adc_cfg.ExtEdgeSel = ADC_EXT_EDGE_SEL_INTER;
|
||||
adc_cfg.ExtSampleNum = ADC_SAMPLE_NUM_8;
|
||||
adc_cfg.ExtTriggerSel = ADC_TRIGGER_SEL_PWM0;
|
||||
|
||||
ADC_Init(XC_ADC, &adc_cfg);
|
||||
|
||||
while (1) {
|
||||
DEBUG("XC_ADC->RF_CTL22:%x\n", XC_ADC->RF_CTL);
|
||||
ADC_StartGetValue(XC_ADC, ADC_CHANNEL4_PIN0, &adc_val);
|
||||
|
||||
if (adc_cfg.RefVol == ADC_REF_VOL_3_3V) {
|
||||
integer = ((adc_val) * 3.3 * 100) / (1.0 * 4096) / 100;
|
||||
decimal = ((uint32_t)((adc_val * 3.3 * 100) / 4096) % 100);
|
||||
|
||||
DEBUG("3.3v ref adc_vol=%d.%d ,adc_val=%d\n", integer, decimal,
|
||||
adc_val);
|
||||
} else if (adc_cfg.RefVol == ADC_REFVOL_2_33V) {
|
||||
integer = ((adc_val) * 2.33 * 100) / (1.0 * 4096) / 100;
|
||||
decimal = ((uint32_t)((adc_val * 2.33 * 100) / 4096) % 100);
|
||||
DEBUG("2.33v ref adc_vol=%d.%d ,adc_val=%d\n", integer, decimal,
|
||||
adc_val);
|
||||
}
|
||||
Delay_Ms(1000);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief adc_channel_sw_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
|
||||
void adc_channelswitch_sw_demo(void)
|
||||
{
|
||||
DEBUG("ADC channelswitch sw demo\n");
|
||||
|
||||
uint16_t integer;
|
||||
uint16_t decimal;
|
||||
adc_cfg adc_cfg;
|
||||
Delay_Ms(500);
|
||||
|
||||
*((volatile unsigned int *)(0x400001BC)) &=
|
||||
~(1 << 12); // 防止GPIO6被接到运放,导致GADC\上拉无效问题
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Pull = GPIO_NOPULL;
|
||||
// Board Led1 Initialization
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitCfg.Int = NOT_INT;
|
||||
GPIO_InitCfg.Pin = 20;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
|
||||
GPIO_InitCfg.Pin = 18;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
|
||||
GPIO_InitCfg.Pin = 19;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
|
||||
GPIO_InitCfg.Pin = 0;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
|
||||
GPIO_FunSel(3, UART0_TX); // 更换打印口
|
||||
GPIO_FunSel(2, UART0_RX);
|
||||
|
||||
// *((volatile unsigned *)(0x4002F000 + 0x03C)) &= ~(0x1<<14);
|
||||
|
||||
adc_cfg.Freq = ADC_FREQ_4M;
|
||||
adc_cfg.RefVol = ADC_REF_VOL_3_3V;
|
||||
adc_cfg.SampEdge = ADC_SAMPEDGE_FALL;
|
||||
adc_cfg.ExtDataMode = ADC_DATA_MODE_32BIT;
|
||||
adc_cfg.ExtEdgeSel =
|
||||
ADC_EXT_EDGE_SEL_INTER; // ADC_EXT_EDGE_SEL_EXTERN_RISE //
|
||||
// ADC_EXT_EDGE_SEL_EXTERN_BOTH
|
||||
adc_cfg.ExtSampleNum = ADC_SAMPLE_NUM_4;
|
||||
adc_cfg.ExtTriggerSel = ADC_TRIGGER_SEL_PWM0;
|
||||
|
||||
ADC_Init(XC_ADC, &adc_cfg);
|
||||
|
||||
__ADC_ChannelSet(XC_ADC, ADC_CHANNEL4_PIN0);
|
||||
|
||||
//==================中断设置=========================
|
||||
ADC_Enable_IT(XC_ADC, ADC_INT_EN_READ_REQ_EN_ENABLE |
|
||||
ADC_INT_EN_FIFO_ERROR_EN_ENABLE);
|
||||
NVIC_EnableIRQ(GADC_IRQn);
|
||||
//===========================================
|
||||
|
||||
__ADC_FIFO_ReqIntLenthSet(XC_ADC);
|
||||
|
||||
__ADC_FIFO_Flush(XC_ADC);
|
||||
|
||||
__ADC_Clear_IT_Flag(XC_ADC);
|
||||
|
||||
// ADC DMA enable
|
||||
// XC_ADC->MAIN_CTL &= ~ADC_MAIN_CTL_DMAS_ON_ENABLE;
|
||||
// XC_ADC->MAIN_CTL |= ADC_MAIN_CTL_DMAS_ON_ENABLE;
|
||||
// ADC_DMA_Init();
|
||||
|
||||
__ADC_Enable(XC_ADC);
|
||||
|
||||
uint32_t val;
|
||||
ADC_FIFO_TypeDef *fifo_val = NULL;
|
||||
|
||||
while (1) {
|
||||
|
||||
if (adc_con >= 500 && adc_con != 0xffff) {
|
||||
fifo_val = (ADC_FIFO_TypeDef *)&val;
|
||||
|
||||
for (uint16_t i = 0; i < 500; i++) {
|
||||
val = adc_val_buff[i];
|
||||
|
||||
integer = ((uint32_t)(fifo_val->value_1) * (3.3 * 100)) /
|
||||
(1.0 * 4095) / 100;
|
||||
decimal =
|
||||
((uint32_t)((fifo_val->value_1 * (3.3 * 100)) / 4095) %
|
||||
100);
|
||||
DEBUG("i_dex1: %4d ch : %d val: %d adc_vol=%d.%02dV\n", i,
|
||||
fifo_val->chanel_1, fifo_val->value_1, integer, decimal);
|
||||
|
||||
integer = ((uint32_t)(fifo_val->value_2) * (3.3 * 100)) /
|
||||
(1.0 * 4095) / 100;
|
||||
decimal =
|
||||
((uint32_t)((fifo_val->value_2 * (3.3 * 100)) / 4095) %
|
||||
100);
|
||||
DEBUG("i_dex2: %4d ch : %d val: %d adc_vol=%d.%02dV\n", i,
|
||||
fifo_val->chanel_2, fifo_val->value_2, integer, decimal);
|
||||
|
||||
if ((i % 2))
|
||||
DEBUG("\r\n\r\n");
|
||||
// DEBUG("i_dex: %4d ch : %d val: %d\n",i,
|
||||
// adc_ch_buff[i],adc_val_buff[i]);
|
||||
}
|
||||
adc_con = 0xffff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief adc_channel_sw_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void adc_channelswitch_hw_demo(void)
|
||||
{
|
||||
DEBUG("ADC channelswitch hw demo\n");
|
||||
|
||||
uint16_t integer;
|
||||
uint16_t decimal;
|
||||
adc_cfg adc_cfg;
|
||||
Delay_Ms(500);
|
||||
|
||||
*((volatile unsigned int *)(0x400001BC)) &=
|
||||
~(1 << 12); // 防止GPIO6被接到运放,导致GADC\上拉无效问题
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Pull = GPIO_NOPULL;
|
||||
// Board Led1 Initialization
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitCfg.Int = NOT_INT;
|
||||
GPIO_InitCfg.Pin = 20;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
|
||||
GPIO_InitCfg.Pin = 18;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
|
||||
GPIO_InitCfg.Pin = 19;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
|
||||
GPIO_InitCfg.Pin = 0;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
|
||||
GPIO_FunSel(3, UART0_TX); // 更换打印口
|
||||
GPIO_FunSel(2, UART0_RX);
|
||||
|
||||
// *((volatile unsigned *)(0x4002F000 + 0x03C)) &= ~(0x1<<14);
|
||||
|
||||
adc_cfg.Freq = ADC_FREQ_4M;
|
||||
adc_cfg.RefVol = ADC_REF_VOL_3_3V;
|
||||
adc_cfg.SampEdge = ADC_SAMPEDGE_FALL;
|
||||
adc_cfg.ExtDataMode = ADC_DATA_MODE_32BIT;
|
||||
adc_cfg.ExtEdgeSel =
|
||||
ADC_EXT_EDGE_SEL_INTER; // ADC_EXT_EDGE_SEL_EXTERN_RISE //
|
||||
// ADC_EXT_EDGE_SEL_EXTERN_BOTH
|
||||
adc_cfg.ExtSampleNum = ADC_SAMPLE_NUM_4;
|
||||
adc_cfg.ExtTriggerSel = ADC_TRIGGER_SEL_PWM0;
|
||||
|
||||
ADC_Init(XC_ADC, &adc_cfg);
|
||||
|
||||
XC_ADC->TIMER1 = 62;
|
||||
XC_ADC->TIMER0 = 1;
|
||||
|
||||
//=============设置需要切换的通道(1---4通道,1个bit表示一个通道)=============
|
||||
XC_ADC->CHAN_CTL &= ~ADC_CHAN_CTL_CHAN_AUTO_Msk;
|
||||
XC_ADC->CHAN_CTL |= 0xf << 8;
|
||||
|
||||
// 使能ADC自动切换
|
||||
XC_ADC->MAIN_CTL &= ~ADC_MAIN_CTL_AUTO_SW_Msk;
|
||||
XC_ADC->MAIN_CTL |=
|
||||
ADC_MAIN_CTL_AUTO_SW_ENABLE; //(ADC_MAIN_CTL_AUTO_SW_ENABLE<<ADC_MAIN_CTL_AUTO_SW_Pos);
|
||||
|
||||
//==================中断设置=========================
|
||||
ADC_Enable_IT(XC_ADC, ADC_INT_EN_READ_REQ_EN_ENABLE |
|
||||
ADC_INT_EN_FIFO_ERROR_EN_ENABLE);
|
||||
NVIC_EnableIRQ(GADC_IRQn);
|
||||
//===========================================
|
||||
|
||||
__ADC_FIFO_ReqIntLenthSet(XC_ADC);
|
||||
|
||||
__ADC_FIFO_Flush(XC_ADC);
|
||||
|
||||
__ADC_Clear_IT_Flag(XC_ADC);
|
||||
|
||||
// ADC DMA enable
|
||||
// XC_ADC->MAIN_CTL &= ~ADC_MAIN_CTL_DMAS_ON_ENABLE;
|
||||
// XC_ADC->MAIN_CTL |= ADC_MAIN_CTL_DMAS_ON_ENABLE;
|
||||
// ADC_DMA_Init();
|
||||
|
||||
__ADC_Enable(XC_ADC);
|
||||
|
||||
uint32_t val;
|
||||
ADC_FIFO_TypeDef *fifo_val = NULL;
|
||||
|
||||
while (1) {
|
||||
|
||||
if (adc_con >= 500 && adc_con != 0xffff) {
|
||||
fifo_val = (ADC_FIFO_TypeDef *)&val;
|
||||
|
||||
for (uint16_t i = 0; i < 500; i++) {
|
||||
val = adc_val_buff[i];
|
||||
|
||||
integer = ((uint32_t)(fifo_val->value_1) * (3.3 * 100)) /
|
||||
(1.0 * 4095) / 100;
|
||||
decimal =
|
||||
((uint32_t)((fifo_val->value_1 * (3.3 * 100)) / 4095) %
|
||||
100);
|
||||
DEBUG("i_dex1: %4d ch : %d val: %d adc_vol=%d.%02dV\n", i,
|
||||
fifo_val->chanel_1, fifo_val->value_1, integer, decimal);
|
||||
|
||||
integer = ((uint32_t)(fifo_val->value_2) * (3.3 * 100)) /
|
||||
(1.0 * 4095) / 100;
|
||||
decimal =
|
||||
((uint32_t)((fifo_val->value_2 * (3.3 * 100)) / 4095) %
|
||||
100);
|
||||
DEBUG("i_dex2: %4d ch : %d val: %d adc_vol=%d.%02dV\n", i,
|
||||
fifo_val->chanel_2, fifo_val->value_2, integer, decimal);
|
||||
|
||||
if ((i % 2))
|
||||
DEBUG("\r\n\r\n");
|
||||
// DEBUG("i_dex: %4d ch : %d val: %d\n",i,
|
||||
// adc_ch_buff[i],adc_val_buff[i]);
|
||||
}
|
||||
adc_con = 0xffff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ADC_channelswitch_sw_Intr_Callback(void)
|
||||
{
|
||||
uint32_t adc_int;
|
||||
static uint8_t adc_start = 0;
|
||||
|
||||
adc_int = XC_ADC->INT;
|
||||
|
||||
if (!(adc_int & (ADC_INT_READ_REQ_INT_Msk | ADC_INT_FIFO_ERROR_INT_Msk))) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (adc_con >= 500) {
|
||||
//__ADC_Clear_IT_Flag(XC_ADC);
|
||||
return;
|
||||
}
|
||||
//__ADC_Disable(XC_ADC);
|
||||
|
||||
__ADC_Clear_IT_Flag(XC_ADC);
|
||||
|
||||
if (++adc_start > 3)
|
||||
adc_start = 0;
|
||||
|
||||
switch (adc_start) {
|
||||
case 0:
|
||||
__ADC_ChannelSet(XC_ADC, ADC_CHANNEL1_PIN20);
|
||||
|
||||
GPIO_Output_Low(1);
|
||||
GPIO_Output_High(1);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
__ADC_ChannelSet(XC_ADC, ADC_CHANNEL11_PIN2);
|
||||
// XC_ADC->MAIN_CTL &= ~ADC_MAIN_CTL_AUTO_SW_Msk;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
__ADC_ChannelSet(XC_ADC, ADC_CHANNEL4_PIN0);
|
||||
// XC_ADC->MAIN_CTL &= ~ADC_MAIN_CTL_AUTO_SW_Msk;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
__ADC_ChannelSet(XC_ADC, ADC_CHANNEL6_PIN4);
|
||||
// XC_ADC->MAIN_CTL &= ~ADC_MAIN_CTL_AUTO_SW_Msk;
|
||||
break;
|
||||
|
||||
// case 4:
|
||||
// __ADC_ChannelSet(XC_ADC, ADC_CHANNEL7_PIN5);
|
||||
// //XC_ADC->MAIN_CTL &= ~ADC_MAIN_CTL_AUTO_SW_Msk;
|
||||
// break;
|
||||
//
|
||||
// case 5:
|
||||
// __ADC_ChannelSet(XC_ADC, ADC_CHANNEL2_PIN19);
|
||||
// //XC_ADC->MAIN_CTL &= ~ADC_MAIN_CTL_AUTO_SW_Msk;
|
||||
// break;
|
||||
//
|
||||
// case 6:
|
||||
// __ADC_ChannelSet(XC_ADC, ADC_CHANNEL3_PIN18);
|
||||
// //XC_ADC->MAIN_CTL &= ~ADC_MAIN_CTL_AUTO_SW_Msk;
|
||||
// break;
|
||||
//
|
||||
// case 7:
|
||||
// __ADC_ChannelSet(XC_ADC, ADC_CHANNEL4_PIN0);
|
||||
// //XC_ADC->MAIN_CTL &= ~ADC_MAIN_CTL_AUTO_SW_Msk;
|
||||
// break;
|
||||
}
|
||||
|
||||
for (int t = 0; t < ADC_FIFO_CTL_READ_REQ_THRESH_LEN_2; t++) {
|
||||
// val = XC_ADC->FIFO;
|
||||
adc_val_buff[adc_con] = XC_ADC->FIFO;
|
||||
adc_con++;
|
||||
}
|
||||
|
||||
//__ADC_Enable(XC_ADC);
|
||||
//__ADC_FIFO_Flush(XC_ADC);
|
||||
//__ADC_Clear_IT_Flag(XC_ADC);
|
||||
}
|
||||
|
||||
void ADC_channelswitch_hw_Intr_Callback(void)
|
||||
{
|
||||
uint32_t adc_int;
|
||||
|
||||
adc_int = XC_ADC->INT;
|
||||
|
||||
if (!(adc_int & (ADC_INT_READ_REQ_INT_Msk | ADC_INT_FIFO_ERROR_INT_Msk))) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (adc_con >= 500) {
|
||||
return;
|
||||
}
|
||||
//__ADC_Disable(XC_ADC);
|
||||
|
||||
__ADC_Clear_IT_Flag(XC_ADC);
|
||||
|
||||
for (int t = 0; t < ADC_FIFO_CTL_READ_REQ_THRESH_LEN_2; t++) {
|
||||
adc_val_buff[adc_con] = XC_ADC->FIFO;
|
||||
adc_con++;
|
||||
}
|
||||
}
|
||||
|
||||
// void ADC_Intr_Callback(void)
|
||||
//{
|
||||
// ADC_channelswitch_hw_Intr_Callback(); //adc硬件切换
|
||||
//// ADC_channelswitch_sw_Intr_Callback();//adc软件切换
|
||||
//// PGA_ADC_Intr_Callback(); //录音pga
|
||||
//}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*!
|
||||
* \file adc.h
|
||||
*
|
||||
* \brief The head file of adc.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __ADC_H__
|
||||
#define __ADC_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void adc_demo(void);
|
||||
void adc_channelswitch_sw_demo(void);
|
||||
void adc_channelswitch_hw_demo(void);
|
||||
void mic_pga_adc_demo(void);
|
||||
void ADC_channelswitch_hw_Intr_Callback(void);
|
||||
void ADC_channelswitch_sw_Intr_Callback(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __ADC_H__ */
|
||||
@@ -0,0 +1,610 @@
|
||||
/*!
|
||||
* \file pga_adc.c
|
||||
*
|
||||
* \brief Target pga 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 "pga_adc.h"
|
||||
#include "adc.h"
|
||||
#include "ringbuffer.h"
|
||||
#include "xc6xxx_hal_adc.h"
|
||||
#include "xc6xxx_hal_uart.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
#define USER_NOISE_EN 0
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
Mic_sampling_cb m_sampling_cb;
|
||||
extern void UART_SendByte(UART_TypeDef *UARTx, uint8_t Byte);
|
||||
extern uint8_t adc_ch_buff[1000];
|
||||
extern uint32_t adc_val_buff[500];
|
||||
extern uint16_t adc_con;
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
****************************************************************************************
|
||||
* @brief
|
||||
*
|
||||
* @param[in]
|
||||
* @param[in]
|
||||
|
||||
* @return[out]
|
||||
*
|
||||
****************************************************************************************
|
||||
*/
|
||||
void adc_uart_init(void)
|
||||
{
|
||||
|
||||
// uint8_t rxbuff[26];
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
GPIO_InitStruct.Mux = GPIO_Mux0;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
GPIO_InitStruct.Int = NOT_INT;
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_3;
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_OUTPUT;
|
||||
GPIO_InitStruct.FunSel = UART1_TX;
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_2;
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitStruct.FunSel = UART1_RX;
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
UART_InitTypeDef UART_InitStruct = {0};
|
||||
|
||||
UART_InitStruct.Parity = UART_PARITY_DISABLE;
|
||||
UART_InitStruct.StopBits = UART_STOP_1_BITS;
|
||||
UART_InitStruct.WordLength = UART_UARTx_TCR_CLS_8BITS;
|
||||
UART_InitStruct.BaudRate = UART_BAUDRATE_1M;
|
||||
UART_InitStruct.HardwareFlowControl = UART_UARTx_MCR_AFCE_DISABLE;
|
||||
|
||||
UART_Init(XC_UART1, &UART_InitStruct);
|
||||
|
||||
UART_Enable_RxIT(XC_UART1);
|
||||
NVIC_EnableIRQ(UART1_IRQn);
|
||||
}
|
||||
|
||||
void uart_seed_data(uint8_t data) { UART_SendByte(XC_UART1, data); }
|
||||
|
||||
void uart_seed_buff(uint8_t *Data, uint16_t len)
|
||||
{
|
||||
UART_SendData(XC_UART1, Data, len);
|
||||
}
|
||||
|
||||
// extern unsigned int filter_data(unsigned int data);
|
||||
// 对采样到的ADC值进行处理
|
||||
void pga_adc_rx_event_handler(uint16_t pga_adc_val)
|
||||
{
|
||||
#if USER_NOISE_EN
|
||||
static uint8_t delay_con = 0;
|
||||
static uint32_t voice_data_bk = 0;
|
||||
#endif
|
||||
static uint8_t freq_con = 0;
|
||||
|
||||
short voice_data = 0;
|
||||
|
||||
freq_con++;
|
||||
if (freq_con < m_sampling_cb.freq_con)
|
||||
return;
|
||||
freq_con = 0;
|
||||
|
||||
#if SAMPLING_DEBUG
|
||||
if (m_sampling_cb.rx_flag)
|
||||
return;
|
||||
|
||||
m_sampling_cb.pga_adc_rx_con++;
|
||||
#endif
|
||||
|
||||
#if REC_TEST_EN || SAMPLING_DEBUG
|
||||
if (!m_sampling_cb.rec_len_time) {
|
||||
#if SAMPLING_DEBUG
|
||||
m_sampling_cb.rx_flag = 1;
|
||||
#endif // SAMPLING_DEBUG
|
||||
return;
|
||||
}
|
||||
#endif // REC_TEST_EN || SAMPLING_DEBUG
|
||||
|
||||
// voice_data = pga_adc_val;
|
||||
// if (pga_adc_val > 50) pga_adc_val = pga_adc_val - 50;//平移
|
||||
|
||||
voice_data = pga_adc_val;
|
||||
|
||||
#if USER_NOISE_EN
|
||||
if (pga_adc_val > 2100 || pga_adc_val < 1900) {
|
||||
delay_con = 0;
|
||||
} else {
|
||||
delay_con++;
|
||||
if (delay_con >= 50) {
|
||||
delay_con = 50;
|
||||
pga_adc_val = (voice_data_bk + pga_adc_val) / 2;
|
||||
pga_adc_val = 1024 + pga_adc_val / 2;
|
||||
}
|
||||
}
|
||||
|
||||
voice_data_bk = voice_data = pga_adc_val;
|
||||
#endif // USER_NOISE_EN
|
||||
|
||||
if (voice_data > 2047) {
|
||||
voice_data = (((uint32_t)voice_data - 2047) * 0x7fff / 2048);
|
||||
voice_data &= ~0x8000;
|
||||
} else {
|
||||
voice_data = (((uint32_t)2047 - voice_data) * 0x7fff / 2048);
|
||||
voice_data = 0x7fff - voice_data;
|
||||
voice_data |= 0x8000;
|
||||
}
|
||||
|
||||
m_sampling_cb.mic_adc_buff[m_sampling_cb.save_buf_index]
|
||||
[m_sampling_cb.pga_adc_rx_index] = voice_data;
|
||||
|
||||
m_sampling_cb.pga_adc_rx_index++;
|
||||
|
||||
if (m_sampling_cb.pga_adc_rx_index >= MIC_SAMPLING_MAX_LEN) {
|
||||
m_sampling_cb.pga_adc_rx_index = 0;
|
||||
m_sampling_cb.save_buf_index = !m_sampling_cb.save_buf_index;
|
||||
m_sampling_cb.buf_full_flag = 1;
|
||||
}
|
||||
}
|
||||
|
||||
#if (USE_XIP == 1)
|
||||
/**
|
||||
* @brief mic_pga_adc_sampling_set
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
|
||||
void mic_pga_adc_sampling_set(void)
|
||||
{
|
||||
CLOCK_HighFreq_In_Typedef uclock_freq = CLOCK_HighFreq_Get();
|
||||
#if (SAMPLING_TYPE == MIC_SAMPLING_8K)
|
||||
|
||||
#if (ADC_FREQ_CLOCK == ADC_CLK_DIV_2)
|
||||
if (uclock_freq == CLOCK_HFCLK_IN_96M) {
|
||||
m_sampling_cb.freq_con = 32;
|
||||
} else if (uclock_freq == CLOCK_HFCLK_IN_64M) {
|
||||
m_sampling_cb.freq_con = 24;
|
||||
} else {
|
||||
m_sampling_cb.freq_con = 19;
|
||||
}
|
||||
#else
|
||||
//===========ADC时钟为2/4M的时候
|
||||
if (uclock_freq == CLOCK_HFCLK_IN_96M) {
|
||||
m_sampling_cb.freq_con = 32;
|
||||
} else if (uclock_freq == CLOCK_HFCLK_IN_64M) {
|
||||
m_sampling_cb.freq_con = 21;
|
||||
} else {
|
||||
m_sampling_cb.freq_con = 15;
|
||||
}
|
||||
#endif //(ADC_FREQ_CLOCK == ADC_CLK_DIV_2)
|
||||
|
||||
#else // 16k采样
|
||||
|
||||
#if (ADC_FREQ_CLOCK == ADC_CLK_DIV_2)
|
||||
if (uclock_freq == CLOCK_HFCLK_IN_96M) {
|
||||
m_sampling_cb.freq_con = 18;
|
||||
} else if (uclock_freq == CLOCK_HFCLK_IN_64M) {
|
||||
m_sampling_cb.freq_con = 12;
|
||||
} else {
|
||||
m_sampling_cb.freq_con = 9;
|
||||
}
|
||||
#elif (ADC_FREQ_CLOCK == ADC_CLK_DIV_4)
|
||||
if (uclock_freq == CLOCK_HFCLK_IN_96M) {
|
||||
m_sampling_cb.freq_con = 13;
|
||||
} else if (uclock_freq == CLOCK_HFCLK_IN_64M) {
|
||||
m_sampling_cb.freq_con = 8;
|
||||
} else {
|
||||
m_sampling_cb.freq_con = 7;
|
||||
}
|
||||
#else
|
||||
//===========ADC时钟为2的时候
|
||||
if (uclock_freq == CLOCK_HFCLK_IN_96M) {
|
||||
m_sampling_cb.freq_con = 16;
|
||||
} else if (uclock_freq == CLOCK_HFCLK_IN_64M) {
|
||||
m_sampling_cb.freq_con = 9;
|
||||
} else {
|
||||
m_sampling_cb.freq_con = 5;
|
||||
}
|
||||
#endif //(ADC_FREQ_CLOCK == ADC_CLK_DIV_2)
|
||||
|
||||
#endif //(SAMPLING_TYPE == MIC_SAMPLING_8K)
|
||||
}
|
||||
|
||||
#else
|
||||
/**
|
||||
* @brief mic_pga_adc_sampling_set
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void mic_pga_adc_sampling_set(void)
|
||||
{
|
||||
CLOCK_HighFreq_In_Typedef uclock_freq = CLOCK_HighFreq_Get();
|
||||
#if (SAMPLING_TYPE == MIC_SAMPLING_8K)
|
||||
|
||||
if (uclock_freq == CLOCK_HFCLK_IN_96M) {
|
||||
m_sampling_cb.freq_con = 32;
|
||||
} else if (uclock_freq == CLOCK_HFCLK_IN_64M) {
|
||||
m_sampling_cb.freq_con = 24;
|
||||
} else {
|
||||
m_sampling_cb.freq_con = 20;
|
||||
}
|
||||
|
||||
//===========ADC采样率为2/4M的时候
|
||||
if (uclock_freq == CLOCK_HFCLK_IN_96M)
|
||||
// {
|
||||
// m_sampling_cb.freq_con = 32;
|
||||
// }
|
||||
// else if (uclock_freq == CLOCK_HFCLK_IN_64M)
|
||||
// {
|
||||
// m_sampling_cb.freq_con = 21;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// m_sampling_cb.freq_con = 20;
|
||||
// }
|
||||
|
||||
#else
|
||||
if (uclock_freq == CLOCK_HFCLK_IN_96M) {
|
||||
m_sampling_cb.freq_con = 16;
|
||||
} else if (uclock_freq == CLOCK_HFCLK_IN_64M) {
|
||||
m_sampling_cb.freq_con = 12;
|
||||
} else {
|
||||
m_sampling_cb.freq_con = 10;
|
||||
}
|
||||
|
||||
//===========ADC采样率为2/4M的时候
|
||||
// if (uclock_freq == CLOCK_HFCLK_IN_96M)
|
||||
// {
|
||||
// m_sampling_cb.freq_con = 16;
|
||||
// }
|
||||
// else if (uclock_freq == CLOCK_HFCLK_IN_64M)
|
||||
// {
|
||||
// m_sampling_cb.freq_con = 9;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// m_sampling_cb.freq_con = 7;
|
||||
// }
|
||||
|
||||
#endif // (SAMPLING_TYPE == MIC_SAMPLING_8K)
|
||||
}
|
||||
|
||||
#endif // USE_XIP == 1
|
||||
|
||||
/**
|
||||
* @brief mic_pga_adc_sampling_set
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
|
||||
uint32_t pga_adc_data_uart_byte_delay(void)
|
||||
{
|
||||
uint32_t delay = 0;
|
||||
CLOCK_HighFreq_In_Typedef uclock_freq = CLOCK_HighFreq_Get();
|
||||
|
||||
#if (ADC_FREQ_CLOCK == ADC_CLK_DIV_2)
|
||||
if (uclock_freq == CLOCK_HFCLK_IN_64M) {
|
||||
delay = 210;
|
||||
} else {
|
||||
delay = 75;
|
||||
}
|
||||
#else
|
||||
if (uclock_freq == CLOCK_HFCLK_IN_64M) {
|
||||
delay = 210;
|
||||
} else {
|
||||
delay = 86;
|
||||
}
|
||||
#endif
|
||||
return delay;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief pga_init
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
|
||||
void pga_init(void)
|
||||
{
|
||||
uint32_t adc_reg = 0;
|
||||
|
||||
DEBUG("audio_adc_ctrl_reg0 = %x\n",
|
||||
*((volatile unsigned int *)(0x400001BC)));
|
||||
// for(int i=0; i<0x100000; i++); //delay
|
||||
|
||||
adc_reg = *((volatile unsigned int *)(0x400001BC));
|
||||
|
||||
adc_reg &= ~(1 << 28);
|
||||
adc_reg |= (1 << 28); // opa第1级增益 +20DB
|
||||
adc_reg &= ~(0x0f << 24);
|
||||
adc_reg |= (0x0a << 24); // opa第2级增益 +16DB //主要修改增益处(<24-27>)
|
||||
|
||||
adc_reg &= ~(0x7 << 21);
|
||||
adc_reg |= (0x7 << 21); // BIT21-23电流大小控制,最大为000,最小为111
|
||||
|
||||
adc_reg &= ~(0x0F << 0); // bias寄存器 控制电流
|
||||
|
||||
adc_reg &= ~(0x1 << 4); // opa pd信号
|
||||
|
||||
adc_reg &= ~(1 << 12); // open pdbias
|
||||
|
||||
adc_reg |= (0x1 << 5); // 差分
|
||||
|
||||
adc_reg &= ~(1 << 7); // open pdbias pga第1级增益,
|
||||
|
||||
*((volatile unsigned int *)(0x400001BC)) = adc_reg;
|
||||
DEBUG("after audio_adc_ctrl_reg0 = %x\n",
|
||||
*((volatile unsigned int *)(0x400001BC)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief mic_pga_adc_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
|
||||
void mic_pga_adc_demo(void)
|
||||
{
|
||||
// DEBUG("__MIC_PGA_ADC_DEMO__\r");
|
||||
adc_uart_init();
|
||||
|
||||
// uint16_t adc_val = 0;
|
||||
adc_cfg adc_cfg;
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Pull = GPIO_NOPULL;
|
||||
// Board Led1 Initialization
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitCfg.Int = NOT_INT;
|
||||
GPIO_InitCfg.Pin = 6;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
|
||||
// GPIO_InitCfg.Pin = 8;
|
||||
// GPIO_Init(&GPIO_InitCfg);
|
||||
//
|
||||
// GPIO_InitCfg.Pin = 9;
|
||||
// GPIO_Init(&GPIO_InitCfg);
|
||||
|
||||
#if SAMPLING_DEBUG || REC_TEST_EN
|
||||
TIMER_BaseInitTypeDef Timer_Init;
|
||||
|
||||
Timer_Init.src_clk = TIMER_CLK_SRC_32M_DIV;
|
||||
Timer_Init.mode = TIMER_MODE_CYCLE_COUNT;
|
||||
Timer_Init.div_clk = TIMER_DIV_CLK_1MHzOr1K;
|
||||
// Timer0 Config & Start
|
||||
TIMER_Base_Init(XC_TIMER1, &Timer_Init);
|
||||
TIMER_SetUs(XC_TIMER1,
|
||||
1000000); // 测试采样到的时候改成1秒,录音测试改成5秒或者其他
|
||||
#endif
|
||||
|
||||
// pga初始化
|
||||
pga_init();
|
||||
|
||||
/*----- Special Pin -----*/
|
||||
/*< SWD 和 SWCK 引脚也可以作为 ADC 使用,
|
||||
注意 PIN 脚的功能重映射 >*/
|
||||
// GPIO_MuxCtl(12,GPIO_Mux1);
|
||||
// GPIO_MuxCtl(13,GPIO_Mux1);
|
||||
/*-----------------------*/
|
||||
|
||||
adc_cfg.Freq = ADC_FREQ_4M;
|
||||
adc_cfg.RefVol = ADC_REF_VOL_3_3V;
|
||||
adc_cfg.SampEdge = ADC_SAMPEDGE_FALL;
|
||||
adc_cfg.ExtDataMode = ADC_DATA_MODE_32BIT;
|
||||
adc_cfg.ExtEdgeSel =
|
||||
ADC_EXT_EDGE_SEL_INTER; // ADC_EXT_EDGE_SEL_EXTERN_RISE //
|
||||
// ADC_EXT_EDGE_SEL_EXTERN_BOTH
|
||||
adc_cfg.ExtSampleNum = ADC_SAMPLE_NUM_16;
|
||||
adc_cfg.ExtTriggerSel = ADC_TRIGGER_SEL_PWM0;
|
||||
|
||||
ADC_Init(XC_ADC, &adc_cfg);
|
||||
|
||||
// 采样率设置
|
||||
mic_pga_adc_sampling_set(); // ADC改动跟着改动
|
||||
|
||||
__ADC_ChannelSet(XC_ADC, ADC_CHANNEL13_PIN6);
|
||||
|
||||
__ADC_FIFO_ReqIntLenthSet(XC_ADC);
|
||||
|
||||
__ADC_FIFO_Flush(XC_ADC);
|
||||
|
||||
__ADC_Clear_IT_Flag(XC_ADC);
|
||||
|
||||
//=============中断=======================
|
||||
ADC_Enable_IT(XC_ADC, ADC_INT_EN_READ_REQ_EN_ENABLE |
|
||||
ADC_INT_EN_FIFO_ERROR_EN_ENABLE);
|
||||
NVIC_EnableIRQ(GADC_IRQn);
|
||||
|
||||
__ADC_Enable(XC_ADC);
|
||||
|
||||
#if SAMPLING_DEBUG || REC_TEST_EN
|
||||
TIMER_Start_IT(XC_TIMER1);
|
||||
m_sampling_cb.rec_len_time = 5; // 秒
|
||||
|
||||
uint8_t rec_time;
|
||||
rec_time = m_sampling_cb.rec_len_time;
|
||||
#endif
|
||||
|
||||
DEBUG("start adc\n");
|
||||
|
||||
short voice_data = 0;
|
||||
uint16_t voice_adc_rx_index = 0;
|
||||
uint32_t uart_byte_delay = pga_adc_data_uart_byte_delay();
|
||||
|
||||
while (1) {
|
||||
#if SAMPLING_DEBUG
|
||||
if (m_sampling_cb.rx_flag) {
|
||||
Delay_Ms(500);
|
||||
DEBUG("sampling: %d.%dKhz\n",
|
||||
(m_sampling_cb.pga_adc_rx_con / rec_time) / 1000,
|
||||
(m_sampling_cb.pga_adc_rx_con / rec_time) % 1000);
|
||||
}
|
||||
#endif
|
||||
// ADC_MIC_StartGetValue(XC_ADC, ADC_CHANNEL13_PIN6,&adc_val); //轮询
|
||||
if (m_sampling_cb.buf_full_flag) {
|
||||
m_sampling_cb.buf_full_flag = 0;
|
||||
|
||||
voice_adc_rx_index = 0;
|
||||
while (voice_adc_rx_index < MIC_SAMPLING_MAX_LEN) {
|
||||
voice_data =
|
||||
m_sampling_cb.mic_adc_buff[!m_sampling_cb.save_buf_index]
|
||||
[voice_adc_rx_index];
|
||||
// DEBUG("%d\n",voice_data);
|
||||
uart_seed_data(voice_data & 0xff);
|
||||
for (uint16_t i = 0; i < uart_byte_delay; i++)
|
||||
; // 64M延时
|
||||
uart_seed_data((voice_data >> 8) & 0xff);
|
||||
for (uint16_t i = 0; i < uart_byte_delay; i++)
|
||||
; // 64M延时
|
||||
voice_adc_rx_index++;
|
||||
}
|
||||
// m_sampling_cb.save_buf_index = !m_sampling_cb.save_buf_index;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Timer1_Callback(void *context)
|
||||
{
|
||||
(void)context;
|
||||
#if SAMPLING_DEBUG
|
||||
// m_sampling_cb.rx_flag = 1;
|
||||
#endif
|
||||
#if REC_TEST_EN
|
||||
if (m_sampling_cb.rec_len_time) {
|
||||
// DEBUG("m_sampling_cb.rec_len_time\n");
|
||||
m_sampling_cb.rec_len_time--;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//=========================================PGA+ADC相关的=========================================
|
||||
void ADC_MIC_StartGetValue(ADC_TypeDef *ADCx, ADC_ChannelTypeDef Channel,
|
||||
uint16_t *adc_val)
|
||||
{
|
||||
uint32_t val;
|
||||
ADC_FIFO_TypeDef *fifo_val = NULL;
|
||||
|
||||
if (__ADC_ChannelGet(XC_ADC) != Channel) {
|
||||
__ADC_Disable(ADCx);
|
||||
|
||||
__ADC_ChannelSet(ADCx, Channel);
|
||||
|
||||
__ADC_FIFO_ReqIntLenthSet(ADCx);
|
||||
|
||||
__ADC_FIFO_Flush(ADCx);
|
||||
|
||||
__ADC_Clear_IT_Flag(ADCx);
|
||||
|
||||
__ADC_Enable(ADCx);
|
||||
}
|
||||
|
||||
val = ADCx->INT_RAW;
|
||||
if (((val & ADC_INT_READ_REQ_INT_Msk) != ADC_INT_READ_REQ_INT_Msk)) {
|
||||
return;
|
||||
}
|
||||
|
||||
__ADC_Clear_IT_Flag(ADCx);
|
||||
|
||||
fifo_val = (ADC_FIFO_TypeDef *)&val;
|
||||
|
||||
for (int t = 0; t < ADC_FIFO_CTL_READ_REQ_THRESH_LEN_8; t++) {
|
||||
val = ADCx->FIFO;
|
||||
|
||||
if (fifo_val->chanel_1 != fifo_val->chanel_2) {
|
||||
// DEBUG("channel error :%d\n",t);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fifo_val->chanel_1 != Channel) {
|
||||
// DEBUG("channel error :%d\n",t);
|
||||
continue;
|
||||
}
|
||||
|
||||
pga_adc_rx_event_handler(fifo_val->value_1);
|
||||
pga_adc_rx_event_handler(fifo_val->value_2);
|
||||
}
|
||||
|
||||
*adc_val = fifo_val->value_1;
|
||||
|
||||
__ADC_FIFO_Flush(ADCx);
|
||||
// __ADC_Disable(ADCx);
|
||||
// __ADC_ChannelSet(ADCx, Channel);
|
||||
// __ADC_Clear_IT_Flag(XC_ADC);
|
||||
// __ADC_Enable(XC_ADC);
|
||||
}
|
||||
//=================================================================================
|
||||
|
||||
void PGA_ADC_Intr_Callback(void)
|
||||
{
|
||||
uint32_t adc_int;
|
||||
uint32_t val;
|
||||
|
||||
ADC_FIFO_TypeDef *fifo_val = NULL;
|
||||
|
||||
adc_int = XC_ADC->INT;
|
||||
|
||||
if (!(adc_int & (ADC_INT_READ_REQ_INT_Msk | ADC_INT_FIFO_ERROR_INT_Msk))) {
|
||||
return;
|
||||
}
|
||||
// 开关ADC会影响采样率,需要重新调整
|
||||
//__ADC_Disable(XC_ADC);
|
||||
fifo_val = (ADC_FIFO_TypeDef *)&val;
|
||||
|
||||
for (int t = 0; t < ADC_FIFO_CTL_READ_REQ_THRESH_LEN_8; t++) {
|
||||
val = XC_ADC->FIFO;
|
||||
|
||||
if (fifo_val->chanel_1 == ADC_CHANNEL13_PIN6) {
|
||||
pga_adc_rx_event_handler(fifo_val->value_1);
|
||||
}
|
||||
if (fifo_val->chanel_2 == ADC_CHANNEL13_PIN6) {
|
||||
pga_adc_rx_event_handler(fifo_val->value_2);
|
||||
}
|
||||
// if (t==7)
|
||||
// __ADC_Enable(XC_ADC);
|
||||
}
|
||||
// __ADC_Enable(XC_ADC);
|
||||
__ADC_FIFO_Flush(XC_ADC);
|
||||
}
|
||||
|
||||
void ADC_Intr_Callback(void)
|
||||
{
|
||||
ADC_channelswitch_hw_Intr_Callback(); // adc硬件切换
|
||||
// ADC_channelswitch_sw_Intr_Callback();//adc软件切换
|
||||
// PGA_ADC_Intr_Callback(); //录音pga
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*!
|
||||
* \file pga_adc.h
|
||||
*
|
||||
* \brief The head file of pga_adc.h
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __BSP_PGA_ADC_H_
|
||||
#define __BSP_PGA_ADC_H_
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "stdint.h"
|
||||
#include "xc6xxx.h"
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
#define SAMPLING_DEBUG 1
|
||||
#define REC_TEST_EN 1 //录音测试,定时录音
|
||||
|
||||
#define ADC_CLOCK_FREQ_8M ADC_CLK_DIV_0 // ADC_FREQ_8M
|
||||
#define ADC_CLOCK_FREQ_4M ADC_CLK_DIV_2 // ADC_FREQ_4M
|
||||
#define ADC_CLOCK_FREQ_2M ADC_CLK_DIV_4 // ADC_FREQ_2M
|
||||
#define ADC_CLOCK_FREQ_1M ADC_CLK_DIV_8 // ADC_FREQ_1M
|
||||
#define ADC_CLOCK_FREQ_500K ADC_CLK_DIV_16 // ADC_FREQ_500K
|
||||
|
||||
#define ADC_FREQ_CLOCK ADC_CLK_DIV_4 // ADC时钟选择
|
||||
|
||||
#define MIC_SAMPLING_8K 0
|
||||
#define MIC_SAMPLING_16K 1
|
||||
#define SAMPLING_TYPE MIC_SAMPLING_16K
|
||||
|
||||
#define MIC_SAMPLING_MAX_LEN 1 * 256 //缓冲长度,根据需求调节
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
typedef struct {
|
||||
short mic_adc_buff[2][MIC_SAMPLING_MAX_LEN]; //双缓存buff,方便读取以及采样
|
||||
uint16_t pga_adc_rx_index; //缓存buff下标
|
||||
uint8_t
|
||||
save_buf_index; //双缓存buff下标,存放第几个mic_adc_buff[save_buf_index][]
|
||||
uint8_t buf_full_flag;
|
||||
|
||||
uint8_t freq_con; //采样率计算
|
||||
|
||||
#if SAMPLING_DEBUG
|
||||
uint32_t pga_adc_rx_con;
|
||||
uint8_t rx_flag;
|
||||
#endif
|
||||
|
||||
#if REC_TEST_EN || SAMPLING_DEBUG
|
||||
uint8_t rec_len_time;
|
||||
#endif
|
||||
|
||||
} Mic_sampling_cb;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
extern Mic_sampling_cb m_sampling_cb;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void audio_adc_rx_dma_event_handler(void);
|
||||
void pga_adc_rx_event_handler(uint16_t pga_adc_val);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,322 @@
|
||||
/*!
|
||||
* \file can.c
|
||||
*
|
||||
* \brief Target xc60xx can 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 "can.h"
|
||||
#include "xc6xxx_hal_can.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
#define CAN_TX_MODE 0U
|
||||
#define CAN_RX_MODE 1U
|
||||
#define CAN_WORK_MODE CAN_TX_MODE
|
||||
|
||||
#define CAN_ID_MODE CAN_ID_STD // CAN_ID_STD or CAN_ID_EXT
|
||||
#define CAN_FILTER_MODE \
|
||||
CAN_FILTER_SINGLE // CAN_FILTER_SINGLE or CAN_FILTER_DUAL
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
#if (CAN_RX_MODE == CAN_WORK_MODE)
|
||||
uint8_t Can_recv_flag = false;
|
||||
CAN_MsgTypeDef Can_Rx_Msg;
|
||||
|
||||
#endif
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief can_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*
|
||||
* ##### How to use this driver #####
|
||||
* ======================================
|
||||
* 125kbs config:
|
||||
* tclk = 1/(16 Mhz), Prescaler = 4
|
||||
* TQ = tclk * 2 * Prescaler = 1/(16 Mhz) * 8 = 1 / (2 Mhz)
|
||||
* T(syncseg) = 1 * TQ
|
||||
* T(tseg1) = CAN_BUS_TIME_TSEG1_8TQ;
|
||||
* T(tseg2) = CAN_BUS_TIME_TSEG2_7TQ;
|
||||
* T(bit) = T(syncseg) + T(tseg1) + T(tseg2) = 16 TQ = 16 * 1/ (4 Mhz) = 4 / (1
|
||||
* Mhz)
|
||||
*
|
||||
* 1Mbs config:
|
||||
* tclk = 1/(16 Mhz), Prescaler = 1
|
||||
* TQ = tclk * 2 * Prescaler = 1/(16 Mhz) * 2 = 1 / (8 Mhz)
|
||||
* T(syncseg) = 1 * TQ
|
||||
* T(tseg1) = CAN_BUS_TIME_TSEG1_4TQ;
|
||||
* T(tseg2) = CAN_BUS_TIME_TSEG2_3TQ;
|
||||
* T(bit) = T(syncseg) + T(tseg1) + T(tseg2) = 8 TQ = 8 * 1/ (8 Mhz) = 1 / (1
|
||||
* Mhz)
|
||||
* ======================================
|
||||
*/
|
||||
void can_demo(void) {
|
||||
DEBUG("__CAN_DEMO__\r");
|
||||
|
||||
// uint8_t FilterID = CAN_ID_MODE;//CAN_ID_EXT;
|
||||
// uint8_t FilterMode = CAN_FILTER_MODE;//CAN_FILTER_DUAL;
|
||||
|
||||
CAN_InitTypeDef CAN_InitStruct;
|
||||
CAN_FilterTypeDef CAN_FilterCfg;
|
||||
|
||||
CAN_InitStruct.Prescaler = 1;
|
||||
CAN_InitStruct.SyncJumpWidth = CAN_BUS_TIME_SJW_1TQ;
|
||||
CAN_InitStruct.TimeSeg1 = CAN_BUS_TIME_TSEG1_4TQ;
|
||||
CAN_InitStruct.TimeSeg2 = CAN_BUS_TIME_TSEG2_3TQ;
|
||||
CAN_InitStruct.SleepMode = CAN_MODE_SLEEP_NOT_SLEEP;
|
||||
|
||||
CAN_Init(XC_CAN, &CAN_InitStruct);
|
||||
|
||||
CAN_FilterCfg.Id_Mode = CAN_ID_MODE;
|
||||
CAN_FilterCfg.Mode = CAN_FILTER_MODE;
|
||||
|
||||
if (CAN_FilterCfg.Id_Mode == CAN_ID_STD) {
|
||||
if (CAN_FilterCfg.Mode == CAN_FILTER_SINGLE) {
|
||||
// mask, 0 means that the corresponding bit is not filtered.
|
||||
CAN_FilterCfg.Std_Single.Std_Id = 0x0706;
|
||||
CAN_FilterCfg.Std_Single.Std_Id_Mask = 0x0000;
|
||||
CAN_FilterCfg.Std_Single.RTR = 0x00;
|
||||
CAN_FilterCfg.Std_Single.RTR_Mask = 0x00;
|
||||
CAN_FilterCfg.Std_Single.Data_Filter.Data[0] = 0x08;
|
||||
CAN_FilterCfg.Std_Single.Data_Filter.Data[1] = 0x00;
|
||||
CAN_FilterCfg.Std_Single.Data_Filter.Data_Mask[0] = 0x00;
|
||||
CAN_FilterCfg.Std_Single.Data_Filter.Data_Mask[1] = 0x00;
|
||||
} else if (CAN_FilterCfg.Mode == CAN_FILTER_DUAL) {
|
||||
// mask, 0 means that the corresponding bit is not filtered.
|
||||
CAN_FilterCfg.Std_Dual.Filter1.Std_Id = 0x0706;
|
||||
CAN_FilterCfg.Std_Dual.Filter1.Std_Id_Mask = 0x0000;
|
||||
CAN_FilterCfg.Std_Dual.Filter1.RTR = 0x00;
|
||||
CAN_FilterCfg.Std_Dual.Filter1.RTR_Mask = 0x00;
|
||||
CAN_FilterCfg.Std_Dual.Filter1.Data_Filter.Data[0] = 0x0;
|
||||
CAN_FilterCfg.Std_Dual.Filter1.Data_Filter.Data[1] = 0x0;
|
||||
CAN_FilterCfg.Std_Dual.Filter1.Data_Filter.Data_Mask[0] = 0x0;
|
||||
CAN_FilterCfg.Std_Dual.Filter1.Data_Filter.Data_Mask[1] = 0x0;
|
||||
|
||||
CAN_FilterCfg.Std_Dual.Filter2.Std_Id = 0x0706;
|
||||
CAN_FilterCfg.Std_Dual.Filter2.Std_Id_Mask = 0x0000;
|
||||
CAN_FilterCfg.Std_Dual.Filter2.RTR = 0x00;
|
||||
CAN_FilterCfg.Std_Dual.Filter2.RTR_Mask = 0x00;
|
||||
}
|
||||
} else if (CAN_FilterCfg.Id_Mode == CAN_ID_EXT) {
|
||||
if (CAN_FilterCfg.Mode == CAN_FILTER_SINGLE) {
|
||||
// mask, 0 means that the corresponding bit is not filtered.
|
||||
CAN_FilterCfg.Ext_Single.Ext_Id = 0x12345678;
|
||||
CAN_FilterCfg.Ext_Single.Ext_Id_Mask = 0x00000000;
|
||||
CAN_FilterCfg.Ext_Single.RTR = 0x00;
|
||||
CAN_FilterCfg.Ext_Single.RTR_Mask = 0x00;
|
||||
} else if (CAN_FilterCfg.Mode == CAN_FILTER_DUAL) {
|
||||
// mask, 0 means that the corresponding bit is not filtered.
|
||||
CAN_FilterCfg.Ext_Dual.Filter1.Ext_Id = 0x12345678;
|
||||
CAN_FilterCfg.Ext_Dual.Filter1.Ext_Id_Mask = 0x00000000;
|
||||
|
||||
CAN_FilterCfg.Ext_Dual.Filter2.Ext_Id = 0x12345678;
|
||||
CAN_FilterCfg.Ext_Dual.Filter2.Ext_Id_Mask = 0x00000000;
|
||||
}
|
||||
}
|
||||
|
||||
CAN_Filter_Config(XC_CAN, &CAN_FilterCfg);
|
||||
|
||||
CAN_Enable_IT(XC_CAN, CAN_INTERRUPT_RECEIVE_ENABLE);
|
||||
NVIC_EnableIRQ(CAN_IRQn);
|
||||
|
||||
#if 0
|
||||
if(FilterID == CAN_ID_STD)
|
||||
{
|
||||
if(FilterMode == CAN_FILTER_SINGLE)
|
||||
{
|
||||
//mask, 0 means that the corresponding bit is not filtered.
|
||||
CAN_FilterConfig.FilterIdHigh16bit = 0x0000;
|
||||
CAN_FilterConfig.FilterIdLow16bit = 0x0070;
|
||||
CAN_FilterConfig.FilterMaskIdHigh16bit = 0x0000;
|
||||
CAN_FilterConfig.FilterMaskIdLow16bit = 0x0000;
|
||||
CAN_FilterConfig.FilterRTR = 0x0000;
|
||||
CAN_FilterConfig.FilterMaskRTR = 0x0000;
|
||||
CAN_FilterConfig.StandardFrameFilter.Data[0]= 0x08;
|
||||
CAN_FilterConfig.StandardFrameFilter.Data[1]= 0x00;
|
||||
CAN_FilterConfig.StandardFrameFilter.MaskData[0]= 0x00;
|
||||
CAN_FilterConfig.StandardFrameFilter.MaskData[1]= 0x00;
|
||||
|
||||
CAN_StandardFrameConfigSingleFilter(XC_CAN, &CAN_FilterConfig);
|
||||
}
|
||||
else if(FilterMode == CAN_FILTER_DUAL)
|
||||
{
|
||||
//mask, 0 means that the corresponding bit is not filtered.
|
||||
//DualFilter1 Only filter one byte of data
|
||||
CAN_FilterConfig.FilterIdHigh16bit = 0x0070;
|
||||
CAN_FilterConfig.FilterIdLow16bit = 0x0070;
|
||||
CAN_FilterConfig.FilterMaskIdHigh16bit = 0x0000;
|
||||
CAN_FilterConfig.FilterMaskIdLow16bit = 0x0000;
|
||||
CAN_FilterConfig.FilterRTR = 0x0000;
|
||||
CAN_FilterConfig.FilterMaskRTR = 0x0000;
|
||||
|
||||
|
||||
CAN_StandardFrameConfigDualFilter1(XC_CAN, &CAN_FilterConfig);
|
||||
|
||||
//DualFilter2 do not filter data, only filter ID and RTR
|
||||
CAN_FilterConfig.FilterIdHigh16bit = 0x0000;
|
||||
CAN_FilterConfig.FilterIdLow16bit = 0x000F;
|
||||
CAN_FilterConfig.FilterMaskIdHigh16bit = 0x0000;
|
||||
CAN_FilterConfig.FilterMaskIdLow16bit = 0x0000;
|
||||
CAN_FilterConfig.FilterRTR = 0x0000;
|
||||
CAN_FilterConfig.FilterMaskRTR = 0x0000;
|
||||
|
||||
CAN_StandardFrameConfigDualFilter2(XC_CAN, &CAN_FilterConfig);
|
||||
}
|
||||
}
|
||||
else if(FilterID == CAN_ID_EXT)
|
||||
{
|
||||
if(FilterMode == CAN_FILTER_SINGLE)
|
||||
{
|
||||
//mask, 0 means that the corresponding bit is not filtered.
|
||||
CAN_FilterConfig.FilterIdHigh16bit = 0x0070;
|
||||
CAN_FilterConfig.FilterIdLow16bit = 0x0070;
|
||||
CAN_FilterConfig.FilterMaskIdHigh16bit = 0x0000;
|
||||
CAN_FilterConfig.FilterMaskIdLow16bit = 0x0000;
|
||||
CAN_FilterConfig.FilterRTR = 0x0000;
|
||||
CAN_FilterConfig.FilterMaskRTR = 0x0000;
|
||||
|
||||
CAN_ExtendedFrameConfigSingleFilter(XC_CAN, &CAN_FilterConfig);
|
||||
}
|
||||
else if(FilterMode == CAN_FILTER_DUAL)
|
||||
{
|
||||
//mask, 1 means that the corresponding bit is not filtered.
|
||||
CAN_FilterConfig.FilterIdHigh16bit = 0x0070;
|
||||
CAN_FilterConfig.FilterIdLow16bit = 0x0070;
|
||||
CAN_FilterConfig.FilterMaskIdHigh16bit = 0x0000;
|
||||
CAN_FilterConfig.FilterMaskIdLow16bit = 0x0000;
|
||||
CAN_FilterConfig.FilterRTR = 0x0000;
|
||||
CAN_FilterConfig.FilterMaskRTR = 0x0000;
|
||||
|
||||
CAN_ExtendedFrameConfigDualFilter1(XC_CAN, &CAN_FilterConfig);
|
||||
|
||||
CAN_FilterConfig.FilterIdHigh16bit = 0x0080;
|
||||
CAN_FilterConfig.FilterIdLow16bit = 0x0080;
|
||||
CAN_FilterConfig.FilterMaskIdHigh16bit = 0x0000;
|
||||
CAN_FilterConfig.FilterMaskIdLow16bit = 0x0000;
|
||||
CAN_FilterConfig.FilterRTR = 0x0000;
|
||||
CAN_FilterConfig.FilterMaskRTR = 0x0000;
|
||||
|
||||
CAN_ExtendedFrameConfigDualFilter2(XC_CAN, &CAN_FilterConfig);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if (CAN_TX_MODE == CAN_WORK_MODE)
|
||||
DEBUG("__CAN_TX_MODE__\r");
|
||||
CAN_MsgTypeDef CAN_Msg;
|
||||
|
||||
CAN_Msg.TxHeader.StdId = 0x706; // 11 bits
|
||||
CAN_Msg.TxHeader.ExtId = 0x12345678; // 29 bits
|
||||
CAN_Msg.TxHeader.FrameFormat = CAN_ID_MODE; // CAN_ID_STD or CAN_ID_EXT
|
||||
CAN_Msg.TxHeader.RTR = CAN_RTR_DATA;
|
||||
CAN_Msg.TxHeader.DLC = 8; // 0~8 bytes
|
||||
|
||||
for (uint8_t i = 0; i < CAN_Msg.TxHeader.DLC; i++) {
|
||||
CAN_Msg.Tx_Msg[i] = i;
|
||||
}
|
||||
CAN_Transmit(XC_CAN, &CAN_Msg);
|
||||
#else
|
||||
DEBUG("__CAN_RX_MODE__\r");
|
||||
#endif
|
||||
|
||||
while (1) {
|
||||
#if (CAN_TX_MODE == CAN_WORK_MODE)
|
||||
Delay_Ms(1000);
|
||||
#else
|
||||
if (Can_recv_flag == true) {
|
||||
if (Can_Rx_Msg.RxHeader.FrameFormat == CAN_ID_STD)
|
||||
DEBUG("CAN STD ID: 0x%04x\n", Can_Rx_Msg.RxHeader.StdId);
|
||||
else if (Can_Rx_Msg.RxHeader.FrameFormat == CAN_ID_EXT)
|
||||
DEBUG("CAN EXT ID: 0x%08x\n", Can_Rx_Msg.RxHeader.ExtId);
|
||||
else
|
||||
DEBUG("CAN ID Mode Error!!!\n");
|
||||
DEBUG("CAN DLC: %d\n", Can_Rx_Msg.RxHeader.DLC);
|
||||
for (uint8_t n = 0; n < Can_Rx_Msg.RxHeader.DLC; n++) {
|
||||
PRINT("%x-", Can_Rx_Msg.Rx_Msg[n]);
|
||||
}
|
||||
PRINT("\r");
|
||||
|
||||
Can_recv_flag = false;
|
||||
}
|
||||
|
||||
// if(Can_ext_recv_flag == true)
|
||||
// {
|
||||
// DEBUG("CAN Recv %d\n", recv);
|
||||
// for(uint8_t n=0; n<recv; n++)
|
||||
// {
|
||||
// DEBUG("%x ", Recv_buffer[n]);
|
||||
// }
|
||||
// DEBUG("\r");
|
||||
//
|
||||
// Can_ext_recv_flag = false;
|
||||
// }
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CAN_Intr_Callback
|
||||
* @details CAN interrupt callback function
|
||||
* @param void *context
|
||||
* @retval void
|
||||
*/
|
||||
__RAM_CODE void CAN_Intr_Callback(CAN_MsgTypeDef *can_msg) {
|
||||
#if (CAN_RX_MODE == CAN_WORK_MODE)
|
||||
Can_Rx_Msg.RxHeader.FrameFormat = can_msg->RxHeader.FrameFormat;
|
||||
Can_Rx_Msg.RxHeader.StdId = can_msg->RxHeader.StdId;
|
||||
Can_Rx_Msg.RxHeader.ExtId = can_msg->RxHeader.ExtId;
|
||||
Can_Rx_Msg.RxHeader.DLC = can_msg->RxHeader.DLC;
|
||||
memcpy(Can_Rx_Msg.Rx_Msg, can_msg->Rx_Msg, Can_Rx_Msg.RxHeader.DLC);
|
||||
// DEBUG("CB_FrameFormat=0x%x, RTR=0x%x, SID=0x%x, EID=0x%x, len=0x%x\n",
|
||||
// Can_Rx_Msg.RxHeader.FrameFormat,
|
||||
// Can_Rx_Msg.RxHeader.RTR , Can_Rx_Msg.RxHeader.StdId,
|
||||
// Can_Rx_Msg.RxHeader.ExtId, Can_Rx_Msg.RxHeader.DLC);
|
||||
Can_recv_flag = true;
|
||||
#endif
|
||||
}
|
||||
|
||||
///**
|
||||
// * @brief CAN_Ext_ID_Intr_Callback
|
||||
// * @details CAN extended id interrupt callback function
|
||||
// * @param void *context
|
||||
// * @retval void
|
||||
// */
|
||||
//__RAM_CODE void CAN_Ext_ID_Intr_Callback(CAN_EFF_TypeDef can_eff, uint8_t
|
||||
//can_dlc)
|
||||
//{
|
||||
//#if (CAN_RX_MODE == CAN_WORK_MODE)
|
||||
// memcpy(Recv_buffer, can_eff.DATA, can_dlc);
|
||||
// Can_ext_recv_flag = false;
|
||||
//#endif
|
||||
//}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* \file can.h
|
||||
*
|
||||
* \brief The head file of can.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __CAN_H__
|
||||
#define __CAN_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void can_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __CAN_H__ */
|
||||
@@ -0,0 +1,71 @@
|
||||
/*!
|
||||
* \file clock.c
|
||||
*
|
||||
* \brief Target clock 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 "clock.h"
|
||||
#include "xc6xxx_hal_clock.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief clock_init
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void clock_init(void) {
|
||||
CLOCK_CtrlBlock_Typedef clock_cb;
|
||||
|
||||
clock_cb.hfclk_src = CLOCK_HFCLK_SRC_XTAL;
|
||||
clock_cb.hfclk_in = CLOCK_HFCLK_IN_32M;
|
||||
|
||||
clock_cb.lfclk_src = CLOCK_LFCLK_SRC_RC;
|
||||
|
||||
if (clock_cb.lfclk_src == CLOCK_LFCLK_SRC_XTAL) {
|
||||
clock_cb.lfclk_in = CLOCK_XTAL_LFCLK_IN_32K;
|
||||
} else if (clock_cb.lfclk_src == CLOCK_LFCLK_SRC_RC) {
|
||||
clock_cb.lfclk_in = CLOCK_RC_LFCLK_IN_32K;
|
||||
}
|
||||
// Clock Initialization
|
||||
CLOCK_Init(clock_cb);
|
||||
// Systick Configuration
|
||||
SysTick_Config(CLOCK_HighFreq_Get() / 100);
|
||||
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* \file clock.h
|
||||
*
|
||||
* \brief The head file of clock.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __CLOCK_H__
|
||||
#define __CLOCK_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void clock_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __CLOCK_H__ */
|
||||
@@ -0,0 +1,229 @@
|
||||
/*!
|
||||
* \file fmc_spi.c
|
||||
*
|
||||
* \brief Target fmc spi 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 "fmc_spi.h"
|
||||
#include "xc6xxx_fmc_spi.h"
|
||||
#include "xc6xxx_fmc_spi_dma.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
****************************************************************************************
|
||||
* @brief
|
||||
*
|
||||
* @param[in]
|
||||
* @param[in]
|
||||
****************************************************************************************
|
||||
*/
|
||||
__RAM_CODE void fmc_spi_demo(void) {
|
||||
FMC_SPI_Init_Oprt();
|
||||
FMC_SPI_Flash_WakeUp();
|
||||
|
||||
uint32_t mid = 0;
|
||||
FMC_SPI_Flash_RDID((uint8_t *)&mid);
|
||||
DEBUG("Flash RDID: 0x%08x\n", mid);
|
||||
|
||||
uint8_t ruid[16];
|
||||
FMC_SPI_Flash_RUID(ruid);
|
||||
DEBUG("Flash RUID: ");
|
||||
for (int i = 0; i < 16; i++) {
|
||||
DEBUG("%02x ", ruid[i]);
|
||||
}
|
||||
DEBUG("\n");
|
||||
|
||||
#if 1
|
||||
uint8_t data[FLASH_PAGE_SIZE];
|
||||
FMC_SPI_Flash_Erase_Sector(0x19000);
|
||||
DEBUG("Flash_Erase\n");
|
||||
FMC_SPI_Flash_ReadPage(0x19000, data, FLASH_PAGE_SIZE);
|
||||
DEBUG("Flash Read data: \n");
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
DEBUG("%02x ", data[i]);
|
||||
}
|
||||
DEBUG("\n");
|
||||
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
data[i] = i;
|
||||
}
|
||||
data[0] = 88;
|
||||
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
FMC_SPI_Flash_WritePage(0x19000 + i * FLASH_PAGE_SIZE, data,
|
||||
FLASH_PAGE_SIZE);
|
||||
data[0]++;
|
||||
}
|
||||
DEBUG("Flash Write finished!\n");
|
||||
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
data[i] = 0xff;
|
||||
}
|
||||
|
||||
FMC_SPI_Flash_ReadPage(0x19000 + i * FLASH_PAGE_SIZE, data,
|
||||
FLASH_PAGE_SIZE);
|
||||
DEBUG("Flash Read finished%d: \n", i);
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
DEBUG("%02x ", data[i]);
|
||||
}
|
||||
DEBUG("\n");
|
||||
}
|
||||
|
||||
uint8_t w_buff[716];
|
||||
FMC_SPI_Flash_Erase_Sector(0x19000);
|
||||
for (uint16_t i = 0; i < sizeof(w_buff); i++)
|
||||
w_buff[i] = 0x33;
|
||||
FMC_SPI_FlashWrite(0x19000 + 57, w_buff, sizeof(w_buff));
|
||||
DEBUG("Flash Write Any finish\n");
|
||||
|
||||
uint8_t r_buff[1024];
|
||||
FMC_SPI_FlashRead(0x19000, r_buff, sizeof(r_buff));
|
||||
DEBUG("Flash Read Any addr: \n");
|
||||
for (uint16_t i = 0; i < sizeof(r_buff); i++)
|
||||
DEBUG("%02x ", r_buff[i]);
|
||||
DEBUG("\n");
|
||||
|
||||
FMC_SPI_Flash_Erase_Sector(0x19000);
|
||||
for (uint16_t i = 0; i < sizeof(w_buff); i++)
|
||||
w_buff[i] = 0x66;
|
||||
FMC_SPI_FlashWrite(0x19000 + 176, w_buff, 120);
|
||||
DEBUG("Flash Write Any finish2\n");
|
||||
|
||||
FMC_SPI_FlashRead(0x19000 + 176, r_buff, sizeof(r_buff));
|
||||
DEBUG("Flash Read Any addr2: \n");
|
||||
for (uint16_t i = 0; i < sizeof(r_buff); i++)
|
||||
DEBUG("%02x ", r_buff[i]);
|
||||
DEBUG("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
****************************************************************************************
|
||||
* @brief
|
||||
*
|
||||
* @param[in]
|
||||
* @param[in]
|
||||
|
||||
* @return[out]
|
||||
*
|
||||
****************************************************************************************
|
||||
*/
|
||||
__RAM_CODE void fmc_spi_dma_demo(void) {
|
||||
DEBUG("__FMC_SPI_DEMO__\n");
|
||||
|
||||
FMC_SPI_Init();
|
||||
// DEBUG("__FMC_SPI_Init_End__\n");
|
||||
#if (DMAC_NVIC_ENABLE == 1)
|
||||
NVIC_EnableIRQ(DMA_IRQn);
|
||||
#endif
|
||||
// DEBUG("__DMA_IRQ_Enable__\n");
|
||||
FMC_SPI_DMA_Flash_WakeUp(XC_FMC);
|
||||
// DEBUG("__FMC_SPI_FLash_Wakeup__\n");
|
||||
|
||||
uint32_t mid = 0;
|
||||
// FMC_SPI_Flash_Read_RDID(XC_FMC, (uint8_t*)&mid);
|
||||
FMC_SPI_DMA_Flash_RDID(XC_FMC, (uint8_t *)&mid);
|
||||
// FMC_QSPI_DMA_Flash_QREMS(XC_FMC, (uint8_t*)&mid);
|
||||
DEBUG("Flash RDID: 0x%08x\n", mid);
|
||||
|
||||
uint8_t ruid[16];
|
||||
FMC_SPI_DMA_Flash_RUID(XC_FMC, ruid);
|
||||
DEBUG("Flash RUID: ");
|
||||
for (int i = 0; i < 16; i++) {
|
||||
PRINT("%02x ", ruid[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
uint8_t data[FLASH_PAGE_SIZE];
|
||||
FMC_SPI_DMA_Flash_Erase_Sector(XC_FMC, 0x19000);
|
||||
DEBUG("Flash_Erase\n");
|
||||
FMC_SPI_DMA_Flash_ReadPage(XC_FMC, 0x19000, data, FLASH_PAGE_SIZE);
|
||||
DEBUG("Flash Read data: \n");
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
PRINT("%02x ", data[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
data[i] = i;
|
||||
}
|
||||
data[0] = 88;
|
||||
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
FMC_SPI_DMA_Flash_WritePage(XC_FMC, 0x19000 + i * FLASH_PAGE_SIZE, data,
|
||||
FLASH_PAGE_SIZE);
|
||||
data[0]++;
|
||||
}
|
||||
DEBUG("Flash Write finished!\n");
|
||||
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
#if (USE_XIP == 1)
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
data[i] = 0xff;
|
||||
}
|
||||
#else
|
||||
memset(data, 0xff, FLASH_PAGE_SIZE);
|
||||
#endif
|
||||
FMC_SPI_DMA_Flash_ReadPage(XC_FMC, 0x19000 + i * FLASH_PAGE_SIZE, data,
|
||||
FLASH_PAGE_SIZE);
|
||||
DEBUG("Flash Read finished%d: \n", i);
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
PRINT("%02x ", data[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
}
|
||||
|
||||
uint8_t w_buff[716];
|
||||
memset(w_buff, 0x33, sizeof(w_buff));
|
||||
FMC_SPI_DMA_FlashWrite(XC_FMC, 0x19000 + 57, w_buff, sizeof(w_buff));
|
||||
DEBUG("Flash Write Any finish\n");
|
||||
|
||||
uint8_t r_buff[1024];
|
||||
FMC_SPI_DMA_FlashRead(XC_FMC, 0x19000, r_buff, sizeof(r_buff));
|
||||
DEBUG("Flash Read Any addr: \n");
|
||||
for (uint16_t i = 0; i < sizeof(r_buff); i++)
|
||||
PRINT("%02x ", r_buff[i]);
|
||||
PRINT("\n");
|
||||
|
||||
memset(w_buff, 0x66, sizeof(w_buff));
|
||||
FMC_SPI_DMA_FlashWrite(XC_FMC, 0x19000 + 176, w_buff, 120);
|
||||
DEBUG("Flash Write Any finish2\n");
|
||||
|
||||
FMC_SPI_DMA_FlashRead(XC_FMC, 0x19000, r_buff, sizeof(r_buff));
|
||||
DEBUG("Flash Read Any addr2: \n");
|
||||
for (uint16_t i = 0; i < sizeof(r_buff); i++)
|
||||
PRINT("%02x ", r_buff[i]);
|
||||
PRINT("\n");
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*!
|
||||
* \file fmc_spi.h
|
||||
*
|
||||
* \brief The head file of fmc_spi.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __FMC_SPI_H__
|
||||
#define __FMC_SPI_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void fmc_spi_demo(void);
|
||||
void fmc_spi_dma_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __FMC_SPI_H__ */
|
||||
@@ -0,0 +1,225 @@
|
||||
/*!
|
||||
* \file gpio.c
|
||||
*
|
||||
* \brief Target gpio 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 "gpio.h"
|
||||
#include "xc6xxx_hal_gpio.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
#define LED1_PIN GPIO_25
|
||||
#define LED2_PIN GPIO_24
|
||||
#define KEY1_PIN GPIO_6
|
||||
#define KEY2_PIN GPIO_8
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
uint8_t gpio_intr_flag = false;
|
||||
uint32_t intr_val = 0;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief gpio_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void gpio_demo(void) {
|
||||
DEBUG("__GPIO_DEMO__\r");
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Pull = GPIO_PULLUP;
|
||||
// Board Led1 Initialization
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_OUTPUT;
|
||||
GPIO_InitCfg.Int = NOT_INT;
|
||||
GPIO_InitCfg.Pin = LED1_PIN;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
// Board Led2 Initialization
|
||||
GPIO_InitCfg.Pin = LED2_PIN;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
// Board Key1 Initialization
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitCfg.Int = FAIL_EDGE_INT;
|
||||
GPIO_InitCfg.Pin = KEY1_PIN;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
// Board Key2 Initialization
|
||||
GPIO_InitCfg.Pin = KEY2_PIN;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
// Gpio NVIC Enable
|
||||
NVIC_EnableIRQ(GPIO_IRQn);
|
||||
|
||||
GPIO_Output_High(LED1_PIN);
|
||||
GPIO_Output_High(LED2_PIN);
|
||||
|
||||
while (1) {
|
||||
if (gpio_intr_flag == true) {
|
||||
DEBUG("Intr Status Val: 0x%08x\n", intr_val);
|
||||
gpio_intr_flag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gpio_Intr_Callback
|
||||
* @details Gpio interrupt callback function
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
__RAM_CODE void Gpio_Intr_Callback(uint32_t intr_sta) {
|
||||
intr_val = intr_sta;
|
||||
DEBUG("intr_sta: %08x\n", intr_sta);
|
||||
// Determine the interrupt of Key1
|
||||
if ((intr_sta & (1 << KEY1_PIN)) != 0) {
|
||||
GPIO_Output_Toggle(LED1_PIN);
|
||||
gpio_intr_flag = true;
|
||||
}
|
||||
// Determine the interrupt of Key=2
|
||||
if ((intr_sta & (1 << KEY2_PIN)) != 0) {
|
||||
GPIO_Output_Toggle(LED2_PIN);
|
||||
gpio_intr_flag = true;
|
||||
}
|
||||
}
|
||||
|
||||
void gpio_pulldown_input_test(void) {
|
||||
// input test
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitCfg.Int = NOT_INT;
|
||||
GPIO_InitCfg.Pull = GPIO_PULLDOWN;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
GPIO_InitCfg.Pin = i;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
}
|
||||
while (1) {
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
DEBUG("gaio [%d] Val: %d\r\n", i, GPIO_ValRead(i));
|
||||
Delay_Ms(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void gpio_pullup_input_test(void) {
|
||||
// input test
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitCfg.Int = NOT_INT;
|
||||
GPIO_InitCfg.Pull = GPIO_PULLUP;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
GPIO_InitCfg.Pin = i;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
}
|
||||
while (1) {
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
DEBUG("gaio [%d] Val: %d\r\n", i, GPIO_ValRead(i));
|
||||
Delay_Ms(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void gpio_nopull_input_test(void) {
|
||||
// input test
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitCfg.Int = NOT_INT;
|
||||
GPIO_InitCfg.Pull = GPIO_NOPULL;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
GPIO_InitCfg.Pin = i;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
}
|
||||
while (1) {
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
DEBUG("gaio [%d] Val: %d\r\n", i, GPIO_ValRead(i));
|
||||
Delay_Ms(50);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void gpio_pulldown_input_inter_test(void) {
|
||||
// input test
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitCfg.Int = RIS_EDGE_INT;
|
||||
GPIO_InitCfg.Pull = GPIO_PULLDOWN;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
GPIO_InitCfg.Pin = i;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
}
|
||||
// Gpio NVIC Enable
|
||||
NVIC_EnableIRQ(GPIO_IRQn);
|
||||
}
|
||||
|
||||
void gpio_pullup_input_inter_test(void) {
|
||||
// input test
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitCfg.Int = FAIL_EDGE_INT;
|
||||
GPIO_InitCfg.Pull = GPIO_PULLUP;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
GPIO_InitCfg.Pin = i;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
}
|
||||
// Gpio NVIC Enable
|
||||
NVIC_EnableIRQ(GPIO_IRQn);
|
||||
}
|
||||
|
||||
void gpio_output_test(void) {
|
||||
GPIO_InitTypeDef GPIO_InitCfg = {0};
|
||||
GPIO_InitCfg.Mux = GPIO_Mux0;
|
||||
GPIO_InitCfg.FunSel = GPIO_Dx;
|
||||
GPIO_InitCfg.Pull = GPIO_PULLDOWN;
|
||||
GPIO_InitCfg.Dir = GPIO_DIR_OUTPUT;
|
||||
GPIO_InitCfg.Int = NOT_INT;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
GPIO_InitCfg.Pin = i;
|
||||
GPIO_Init(&GPIO_InitCfg);
|
||||
GPIO_Output_Low(i);
|
||||
}
|
||||
Delay_Ms(100);
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
GPIO_Output_High(i);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* \file gpio.h
|
||||
*
|
||||
* \brief The head file of gpio.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __GPIO_H__
|
||||
#define __GPIO_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void gpio_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __GPIO_H__ */
|
||||
@@ -0,0 +1,94 @@
|
||||
/*!
|
||||
* \file i2c_eeprom.c
|
||||
*
|
||||
* \brief Target i2c eeprom 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 "i2c_eeprom.h"
|
||||
#include "xc6xxx_hal_i2c.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief i2c_eeprom_test_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void i2c_eeprom_demo(void) {
|
||||
uint8_t txbuff[512] = {"XC66xx I2C TEST \n"};
|
||||
uint8_t rxbuff[sizeof(txbuff)] = {0};
|
||||
|
||||
I2C_InitTypeDef I2C_InitStruct = {0};
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
GPIO_InitStruct.Mux = GPIO_Mux0;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
GPIO_InitStruct.Int = NOT_INT;
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_3;
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_OUTPUT;
|
||||
GPIO_InitStruct.FunSel = I2C_SCL;
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_2;
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitStruct.FunSel = I2C_SDA;
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
I2C_InitStruct.I2C_ClockSpeed = I2C_ClockSpeed_1M;
|
||||
I2C_InitStruct.I2C_Mode = I2C_Mode_MASTER;
|
||||
I2C_InitStruct.I2C_OwnAddress1 = 0x50;
|
||||
|
||||
I2C_Init(XC_I2C, &I2C_InitStruct);
|
||||
|
||||
for (int i = 0; i < sizeof(txbuff) / 2; i++) {
|
||||
txbuff[i] = i;
|
||||
}
|
||||
|
||||
for (int i = sizeof(txbuff) / 2; i < sizeof(txbuff); i++) {
|
||||
txbuff[i] = sizeof(txbuff) - i - 1;
|
||||
}
|
||||
|
||||
DEBUG("Start Write 24Cxx....\r\n");
|
||||
AT24CXX_Write(0, (uint8_t *)txbuff, sizeof(txbuff));
|
||||
DEBUG("24Cxx Write Sucess!\r\n");
|
||||
|
||||
DEBUG("Start Read 24Cxx....\r\n");
|
||||
AT24CXX_Read(0, rxbuff, sizeof(rxbuff));
|
||||
DEBUG("The Data Readed Is: \r\n");
|
||||
|
||||
for (int i = 0; i < sizeof(rxbuff); i++) {
|
||||
DEBUG("[%d-%d] ", i, rxbuff[i]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* \file i2c_eeprom.h
|
||||
*
|
||||
* \brief The head file of i2c_eeprom.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __I2C_EEPROM_H__
|
||||
#define __I2C_EEPROM_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void i2c_eeprom_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __I2C_EEPROM_H__ */
|
||||
@@ -0,0 +1,86 @@
|
||||
/*!
|
||||
* \file i2c_master.c
|
||||
*
|
||||
* \brief Target i2c master 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 "i2c_master.h"
|
||||
#include "xc6xxx_hal_i2c.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief i2c_master_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void i2c_master_demo(void) {
|
||||
DEBUG("i2c_master_test \n");
|
||||
uint8_t txbuff[32];
|
||||
uint8_t rxbuff[32];
|
||||
I2C_InitTypeDef I2C_InitStruct = {0};
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
GPIO_InitStruct.Mux = GPIO_Mux0;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
GPIO_InitStruct.Int = NOT_INT;
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_5;
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_OUTPUT;
|
||||
GPIO_InitStruct.FunSel = I2C_SCL;
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_4;
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitStruct.FunSel = I2C_SDA;
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
I2C_InitStruct.I2C_ClockSpeed = I2C_ClockSpeed_400K;
|
||||
I2C_InitStruct.I2C_Mode = I2C_Mode_MASTER;
|
||||
I2C_InitStruct.I2C_OwnAddress1 = 0xA0 >> 1;
|
||||
|
||||
I2C_Init(XC_I2C, &I2C_InitStruct);
|
||||
|
||||
for (int i = 0; i < 32; i++) {
|
||||
txbuff[i] = i;
|
||||
}
|
||||
|
||||
I2C_Master_Write(XC_I2C, (uint8_t *)txbuff, sizeof(txbuff));
|
||||
I2C_Master_Read(XC_I2C, (uint8_t *)rxbuff, sizeof(rxbuff));
|
||||
|
||||
DEBUG("reve:");
|
||||
for (int i = 0; i < sizeof(rxbuff); i++) {
|
||||
DEBUG("%d-", rxbuff[i]);
|
||||
}
|
||||
DEBUG("\n");
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*!
|
||||
* \file i2C_master.h
|
||||
*
|
||||
* \brief The head file of i2C_master.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __I2C_MASTER_TEST_H__
|
||||
#define __I2C_MASTER_TEST_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void i2c_master_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _I2C_MASTER_TEST_H_ */
|
||||
@@ -0,0 +1,90 @@
|
||||
/*!
|
||||
* \file i2c_slave.c
|
||||
*
|
||||
* \brief Target i2c slave 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 "i2c_slave.h"
|
||||
#include "xc6xxx_hal_i2c.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief i2c_slave_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void i2c_slave_demo(void) {
|
||||
DEBUG("i2c_slave_test \n");
|
||||
I2C_InitTypeDef I2C_InitStruct = {0};
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
GPIO_InitStruct.Mux = GPIO_Mux0;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
GPIO_InitStruct.Int = NOT_INT;
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_5;
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_OUTPUT;
|
||||
GPIO_InitStruct.FunSel = I2C_SCL;
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_4;
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitStruct.FunSel = I2C_SDA;
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
I2C_InitStruct.I2C_ClockSpeed = I2C_ClockSpeed_400K;
|
||||
I2C_InitStruct.I2C_Mode = I2C_Mode_SLAVE;
|
||||
I2C_InitStruct.I2C_OwnAddress1 = 0xA0 >> 1;
|
||||
|
||||
I2C_Init(XC_I2C, &I2C_InitStruct);
|
||||
|
||||
I2C_Enable_IT(XC_I2C, I2C_INTR_STAT_RX_DONE_GENERATED |
|
||||
I2C_INTR_STAT_STOP_DET_GENERATED |
|
||||
I2C_INTR_STAT_RD_REQ_GENERATED |
|
||||
I2C_INTR_STAT_RX_FULL_GENERATED);
|
||||
|
||||
I2C_FIFO_IT_RXTL_Set(XC_I2C, I2C_RX_TL_FIFO_1);
|
||||
I2C_FIFO_IT_TXTL_Set(XC_I2C, I2C_TX_TL_FIFO_0);
|
||||
|
||||
NVIC_EnableIRQ(I2C_IRQn);
|
||||
|
||||
while (1) {
|
||||
if (i2c_slave_rx_flag == 1) {
|
||||
for (int i = 0; i < 32; i++) {
|
||||
DEBUG("test_buff[%d]=%d\n", i, i2c_slave_rx_buff[i]);
|
||||
}
|
||||
i2c_slave_rx_flag = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* \file i2C_slave.h
|
||||
*
|
||||
* \brief The head file of i2C_slave.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __I2C_SLAVE_TEST_H__
|
||||
#define __I2C_SLAVE_TEST_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void i2c_slave_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __I2C_SLAVE_TEST_H__ */
|
||||
@@ -0,0 +1,73 @@
|
||||
/*!
|
||||
* \file i2c_software.c
|
||||
*
|
||||
* \brief Target i2c software 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 "i2c_software.h"
|
||||
#include "xc6xxx_sw_i2c.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief i2c_software_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void i2c_software_demo(void) {
|
||||
uint8_t txbuff[512] = {"XINCHIP AT24C08 IIC TEST 20190507 . . . . . . . ."};
|
||||
uint8_t rxbuff[sizeof(txbuff)];
|
||||
I2C_SoftWareInit();
|
||||
|
||||
for (int i = 0; i < sizeof(txbuff) / 2; i++) {
|
||||
txbuff[i] = i;
|
||||
}
|
||||
|
||||
for (int i = sizeof(txbuff) / 2; i < sizeof(txbuff); i++) {
|
||||
txbuff[i] = sizeof(txbuff) - i - 1;
|
||||
}
|
||||
DEBUG("AT24CXX_Write\n");
|
||||
for (int i = 0; i < sizeof(txbuff); i++) {
|
||||
DEBUG("[%d-%d] ", i, txbuff[i]);
|
||||
}
|
||||
|
||||
AT24CXX_WriteLenByte(0, (uint8_t *)txbuff, sizeof(txbuff));
|
||||
|
||||
DEBUG("AT24CXX_Read\n");
|
||||
AT24CXX_ReadLenByte(0, rxbuff, sizeof(txbuff));
|
||||
|
||||
for (int i = 0; i < sizeof(rxbuff); i++) {
|
||||
DEBUG("[%d-%d] ", i, rxbuff[i]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* \file i2c_software.h
|
||||
*
|
||||
* \brief The header of i2c_software.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __I2C_SOFTWARE_H__
|
||||
#define __I2C_SOFTWARE_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void i2c_software_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _I2C_SOFTWARE_H_ */
|
||||
@@ -0,0 +1,468 @@
|
||||
/*!
|
||||
* \file pwm.c
|
||||
*
|
||||
* \brief Target pwm 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 "pwm.h"
|
||||
#include "xc6xxx_hal_pwm.h"
|
||||
#include "xc6xxx_hal_adc.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Local Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @brief pwm_test_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
|
||||
void pwm_output_demo()
|
||||
{
|
||||
DEBUG("PWM_OUTPUT_DEMO\r");
|
||||
|
||||
// PWM0 and PWM1 can be mapped to other pins, PWM0 and PWM1 have inverted output.
|
||||
// PWM(HZ) = PWMCLK(16000000)/(DutyCycleAcc*(period+1))/2
|
||||
PWM_InitTypeDef PWM_InitStruct;
|
||||
|
||||
PWM_InitStruct.DutyCycleAcc = 256;
|
||||
PWM_InitStruct.DutyCycle = 100;
|
||||
PWM_InitStruct.Period = 0;
|
||||
PWM_InitStruct.InvertDelay = 0x17;
|
||||
PWM_InitStruct.InvertEnable = true;
|
||||
PWM_InitStruct.OutputInvertPin = GPIO_2;
|
||||
PWM_InitStruct.OutputPin = GPIO_3;
|
||||
PWM_InitStruct.SrcClk = PWM_CLK_SRC_32M_DIV;
|
||||
PWM_InitStruct.SrcEnable = PWM_EN_SEL_ALL;
|
||||
PWM_Init(XC_PWM0, &PWM_InitStruct);
|
||||
|
||||
PWM_InitStruct.OutputInvertPin = GPIO_7;
|
||||
PWM_InitStruct.OutputPin = GPIO_8;
|
||||
PWM_Init(XC_PWM1, &PWM_InitStruct);
|
||||
|
||||
PWM_InitStruct.InvertEnable = false;
|
||||
PWM_InitStruct.OutputPin = GPIO_0;
|
||||
PWM_Init(XC_PWM2, &PWM_InitStruct);
|
||||
|
||||
PWM_InitStruct.OutputPin = GPIO_1;
|
||||
PWM_Init(XC_PWM3, &PWM_InitStruct);
|
||||
|
||||
PWM_InitStruct.OutputPin = GPIO_12;
|
||||
PWM_Init(XC_PWM4, &PWM_InitStruct);
|
||||
|
||||
PWM_InitStruct.OutputPin = GPIO_13;
|
||||
PWM_Init(XC_PWM5, &PWM_InitStruct);
|
||||
|
||||
PWM_Start_All();
|
||||
|
||||
// Delay_Ms(1000);
|
||||
//
|
||||
// PWM_InitStruct.DutyCycleAcc = 7000;
|
||||
// PWM_InitStruct.DutyCycle = 6000;
|
||||
// PWM_DutyCycle_Set(XC_PWM0,PWM_InitStruct.DutyCycleAcc , PWM_InitStruct.DutyCycle);
|
||||
//
|
||||
// PWM_InitStruct.DutyCycleAcc = 7000;
|
||||
// PWM_InitStruct.DutyCycle = 6000;
|
||||
// PWM_DutyCycle_Set(XC_PWM1,PWM_InitStruct.DutyCycleAcc , PWM_InitStruct.DutyCycle);
|
||||
//
|
||||
// PWM_InitStruct.DutyCycleAcc = 7000;
|
||||
// PWM_InitStruct.DutyCycle = 6000;
|
||||
// PWM_DutyCycle_Set(XC_PWM2,PWM_InitStruct.DutyCycleAcc , PWM_InitStruct.DutyCycle);
|
||||
|
||||
// PWM_InitStruct.DutyCycleAcc = 7000;
|
||||
// PWM_InitStruct.DutyCycle = 6000;
|
||||
// PWM_DutyCycle_Set(XC_PWM3,PWM_InitStruct.DutyCycleAcc , PWM_InitStruct.DutyCycle);
|
||||
//
|
||||
// PWM_InitStruct.DutyCycleAcc = 7000;
|
||||
// PWM_InitStruct.DutyCycle = 6000;
|
||||
// PWM_DutyCycle_Set(XC_PWM4,PWM_InitStruct.DutyCycleAcc , PWM_InitStruct.DutyCycle);
|
||||
//
|
||||
// PWM_InitStruct.DutyCycleAcc = 7000;
|
||||
// PWM_InitStruct.DutyCycle = 6000;
|
||||
// PWM_DutyCycle_Set(XC_PWM5,PWM_InitStruct.DutyCycleAcc , PWM_InitStruct.DutyCycle);
|
||||
|
||||
}
|
||||
|
||||
void pwm_brake_demo()
|
||||
{
|
||||
DEBUG("PWM_BRAKE_DEMO\r");
|
||||
// PWM0 and PWM1 can be mapped to other pins, PWM0 and PWM1 have inverted output.
|
||||
// PWM(HZ) = PWMCLK(16000000)/(DutyCycleAcc*(period+1))/2
|
||||
PWM_InitTypeDef PWM_InitStruct;
|
||||
PWM_InitStruct.DutyCycleAcc = 60000;
|
||||
PWM_InitStruct.DutyCycle = 30000;
|
||||
PWM_InitStruct.Period = 99;
|
||||
PWM_InitStruct.InvertDelay = 0;
|
||||
PWM_InitStruct.InvertEnable = false;
|
||||
PWM_InitStruct.OutputInvertPin = GPIO_2;
|
||||
PWM_InitStruct.OutputPin = GPIO_1;
|
||||
PWM_InitStruct.SrcClk = PWM_CLK_SRC_32M_DIV;
|
||||
PWM_InitStruct.SrcEnable = PWM_EN_SEL_SELF;
|
||||
|
||||
PWM_Init(XC_PWM0, &PWM_InitStruct);
|
||||
PWM_Start(XC_PWM0);
|
||||
|
||||
GPIO_FunSel(PWM_SIGNAL_BRK0_GPIO4,GPIO_Dx);
|
||||
GPIO_MuxCtl(PWM_SIGNAL_BRK0_GPIO4,GPIO_Mux1);
|
||||
GPIO_PullConfig(PWM_SIGNAL_BRK0_GPIO4,GPIO_PULLUP);
|
||||
|
||||
GPIO_FunSel(PWM_SIGNAL_BRK1_GPIO5,GPIO_Dx);
|
||||
GPIO_MuxCtl(PWM_SIGNAL_BRK1_GPIO5,GPIO_Mux1);
|
||||
GPIO_PullConfig(PWM_SIGNAL_BRK1_GPIO5,GPIO_PULLUP);
|
||||
|
||||
GPIO_FunSel(PWM_SIGNAL_BRK2_GPIO6,GPIO_Dx);
|
||||
GPIO_MuxCtl(PWM_SIGNAL_BRK2_GPIO6,GPIO_Mux1);
|
||||
XC_CPR->OPA_CTRL_REG &= ~(1<<12);
|
||||
GPIO_PullConfig(PWM_SIGNAL_BRK2_GPIO6,GPIO_PULLUP);
|
||||
|
||||
PWM_Brake_Enable(XC_PWM0);
|
||||
PWM_BrakeSignal_Mask_Config(XC_PWM0,PWM_BRK0_MASK_DISABLE | PWM_BRK1_MASK_DISABLE | PWM_BRK2_MASK_DISABLE);
|
||||
PWM_BrakeSignal_Invert_Config(XC_PWM0,PWM_BRK0_LOW_LEVEL | PWM_BRK1_LOW_LEVEL | PWM_BRK2_LOW_LEVEL);
|
||||
PWM_BrakeRecoveryMode_Config(XC_PWM0,PWM_BRK_MODE_HARDWARE);
|
||||
PWM_BrakeDebounce_Config(XC_PWM0,PWM_BRK_DBC_EN_ENABLE,PWM_BRK_DBC_STEP2);
|
||||
|
||||
while(1)
|
||||
{
|
||||
if( XC_PWM0->PWM_BREAK_CTL & PWM_BRK_MODE_SOFTWARE )
|
||||
{
|
||||
if((XC_PWM0->PWM_BREAK_CTL & PWM_BRK_SYNC_INVALID) == 0)
|
||||
{
|
||||
XC_PWM0->PWM_BREAK_CTL |= 0x01;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void pwm_capture_demo()
|
||||
{
|
||||
DEBUG("PWM_CAPTURE_DEMO\r");
|
||||
|
||||
// PWM0 and PWM1 can be mapped to other pins, PWM0 and PWM1 have inverted output.
|
||||
// DutyCycleAccLevel 为2时,为中心对齐模式
|
||||
// PWM(HZ) = PWMCLK(16000000)/(DutyCycleAcc*(period+1))/2
|
||||
PWM_InitTypeDef PWM_InitStruct;
|
||||
PWM_InitStruct.DutyCycleAcc = 60000;
|
||||
PWM_InitStruct.DutyCycle = 30000;
|
||||
PWM_InitStruct.Period = 99;
|
||||
PWM_InitStruct.InvertDelay = 0;
|
||||
PWM_InitStruct.InvertEnable = false;
|
||||
PWM_InitStruct.OutputInvertPin = GPIO_2;
|
||||
PWM_InitStruct.OutputPin = GPIO_1;
|
||||
PWM_InitStruct.SrcClk = PWM_CLK_SRC_32M_DIV;
|
||||
PWM_InitStruct.SrcEnable = PWM_EN_SEL_SELF;
|
||||
|
||||
PWM_Init(XC_PWM0, &PWM_InitStruct);
|
||||
PWM_Start(XC_PWM0);
|
||||
|
||||
GPIO_FunSel(PWM_SIGNAL_BRK0_GPIO4,GPIO_Dx);
|
||||
GPIO_MuxCtl(PWM_SIGNAL_BRK0_GPIO4,GPIO_Mux1);
|
||||
GPIO_PullConfig(PWM_SIGNAL_BRK0_GPIO4,GPIO_PULLDOWN);
|
||||
|
||||
GPIO_FunSel(PWM_SIGNAL_CAPTURE0_GPIO3,GPIO_Dx);
|
||||
GPIO_MuxCtl(PWM_SIGNAL_CAPTURE0_GPIO3,GPIO_Mux1);
|
||||
|
||||
GPIO_FunSel(PWM_SIGNAL_CAPTURE1_GPIO8,GPIO_Dx);
|
||||
GPIO_MuxCtl(PWM_SIGNAL_CAPTURE1_GPIO8,GPIO_Mux1);
|
||||
|
||||
GPIO_FunSel(PWM_SIGNAL_CAPTURE2_GPIO9,GPIO_Dx);
|
||||
GPIO_MuxCtl(PWM_SIGNAL_CAPTURE2_GPIO9,GPIO_Mux1);
|
||||
|
||||
PWM_CaptureCounter_Enable(PWM_IC_CH0);
|
||||
PWM_CapturePsc_Config(PWM_IC_CH0, PWM_CAPTURE_PSC_1);
|
||||
PWM_CaptureEdge_Config(PWM_IC_CH0,PWM_CAPTURE_MODE_RISE);
|
||||
PWM_CaptureDebounce_Enable(PWM_IC_CH0,PWM_CAPTURE_DBC_STEP1);
|
||||
PWM_Capture_Signal_Config(PWM_IC_CH0,PWM_ICSIG_CAPTURE0);
|
||||
PWM_Capture_Enable_IT(PWM_IC_CH0);
|
||||
PWM_Capture_Enable(PWM_IC_CH0);
|
||||
|
||||
PWM_CaptureCounter_Enable(PWM_IC_CH1);
|
||||
PWM_CapturePsc_Config(PWM_IC_CH1, PWM_CAPTURE_PSC_1);
|
||||
PWM_CaptureEdge_Config(PWM_IC_CH1,PWM_CAPTURE_MODE_RISE);
|
||||
PWM_CaptureDebounce_Enable(PWM_IC_CH1,PWM_CAPTURE_DBC_STEP1);
|
||||
PWM_Capture_Signal_Config(PWM_IC_CH1,PWM_ICSIG_CAPTURE1);
|
||||
PWM_Capture_Enable_IT(PWM_IC_CH1);
|
||||
PWM_Capture_Enable(PWM_IC_CH1);
|
||||
|
||||
PWM_CaptureCounter_Enable(PWM_IC_CH2);
|
||||
PWM_CapturePsc_Config(PWM_IC_CH2, PWM_CAPTURE_PSC_1);
|
||||
PWM_CaptureEdge_Config(PWM_IC_CH2,PWM_CAPTURE_MODE_RISE);
|
||||
PWM_CaptureDebounce_Enable(PWM_IC_CH2,PWM_CAPTURE_DBC_STEP1);
|
||||
PWM_Capture_Signal_Config(PWM_IC_CH2,PWM_ICSIG_CAPTURE2);
|
||||
PWM_Capture_Enable_IT(PWM_IC_CH2);
|
||||
PWM_Capture_Enable(PWM_IC_CH2);
|
||||
|
||||
NVIC_EnableIRQ(PWM_IRQn);
|
||||
}
|
||||
|
||||
void pwm_enable_adc_demo(void)
|
||||
{
|
||||
DEBUG("PWM_ENABLE_ADC_DEMO\r");
|
||||
adc_cfg adc_cfg ;
|
||||
uint16_t adc_val,integer,decimal;
|
||||
// pwm 使能后触发 adc中断
|
||||
// PWM0 and PWM1 can be mapped to other pins, PWM0 and PWM1 have inverted output.
|
||||
// DutyCycleAccLevel 为2时,为中心对齐模式
|
||||
// PWM(HZ) = PWMCLK(16000000)/(DutyCycleAcc*(period+1))/2
|
||||
PWM_InitTypeDef PWM_InitStruct;
|
||||
PWM_InitStruct.DutyCycleAcc = 60000;
|
||||
PWM_InitStruct.DutyCycle = 30000;
|
||||
PWM_InitStruct.Period = 99;
|
||||
PWM_InitStruct.InvertDelay = 0;
|
||||
PWM_InitStruct.InvertEnable = false;
|
||||
PWM_InitStruct.OutputInvertPin = GPIO_2;
|
||||
PWM_InitStruct.OutputPin = GPIO_1;
|
||||
PWM_InitStruct.SrcClk = PWM_CLK_SRC_32M_DIV;
|
||||
PWM_InitStruct.SrcEnable = PWM_EN_SEL_SELF;
|
||||
|
||||
PWM_Init(XC_PWM0, &PWM_InitStruct);
|
||||
PWM_Start(XC_PWM0);
|
||||
|
||||
|
||||
adc_cfg.Freq = ADC_FREQ_1M;
|
||||
adc_cfg.RefVol = ADC_REF_VOL_3_3V;
|
||||
adc_cfg.SampEdge = ADC_SAMPEDGE_FALL;
|
||||
adc_cfg.ExtDataMode = ADC_DATA_MODE_32BIT;
|
||||
adc_cfg.ExtEdgeSel = ADC_EXT_EDGE_SEL_EXTERN_RISE;
|
||||
adc_cfg.ExtSampleNum = ADC_SAMPLE_NUM_8;
|
||||
adc_cfg.ExtTriggerSel = ADC_TRIGGER_SEL_PWM0;
|
||||
|
||||
ADC_Init(XC_ADC, &adc_cfg);
|
||||
|
||||
__ADC_ChannelSet(XC_ADC, ADC_CHANNEL7_PIN5);
|
||||
|
||||
__ADC_FIFO_ReqIntLenthSet(XC_ADC);
|
||||
|
||||
__ADC_FIFO_Flush(XC_ADC);
|
||||
|
||||
__ADC_Clear_IT_Flag(XC_ADC);
|
||||
|
||||
ADC_Enable_IT(XC_ADC, ADC_INT_EN_READ_REQ_EN_ENABLE | ADC_INT_EN_FIFO_ERROR_EN_ENABLE);
|
||||
|
||||
__ADC_Enable(XC_ADC);
|
||||
|
||||
NVIC_EnableIRQ(GADC_IRQn);
|
||||
|
||||
while(1)
|
||||
{
|
||||
|
||||
adc_val = ADC_IT_CollectVal_Get();
|
||||
|
||||
if(adc_cfg.RefVol == ADC_REF_VOL_3_3V)
|
||||
{
|
||||
integer = ((adc_val)*3.3*100)/(1.0*4096)/100;
|
||||
decimal = ((uint32_t)((adc_val*3.3*100)/4096)%100);
|
||||
|
||||
DEBUG("3.3v ref adc_vol=%d.%d ,adc_val=%d\n", integer, decimal,adc_val);
|
||||
}
|
||||
else if(adc_cfg.RefVol == ADC_REFVOL_2_33V)
|
||||
{
|
||||
integer = ((adc_val)*2.33*100)/(1.0*4096)/100;
|
||||
decimal = ((uint32_t)((adc_val*2.33*100)/4096)%100);
|
||||
DEBUG("2.33v ref adc_vol=%d.%d ,adc_val=%d\n", integer, decimal,adc_val);
|
||||
}
|
||||
Delay_Ms(1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void pwm_lowpower_demo(void)
|
||||
{
|
||||
DEBUG("PWM_LOWPOWER_DEMO\r");
|
||||
// PWM0 and PWM1 can be mapped to other pins, PWM0 and PWM1 have inverted output.
|
||||
// DutyCycleAccLevel 为2时,为中心对齐模式
|
||||
// PWM(HZ) = PWMCLK(16000000)/(DutyCycleAcc*(period+1))/2
|
||||
PWM_InitTypeDef PWM_InitStruct;
|
||||
PWM_InitStruct.DutyCycleAcc = 10000;
|
||||
PWM_InitStruct.DutyCycle = 5000;
|
||||
PWM_InitStruct.Period = 99;
|
||||
PWM_InitStruct.InvertDelay = 0;
|
||||
PWM_InitStruct.InvertEnable = false;
|
||||
PWM_InitStruct.OutputInvertPin = GPIO_2;
|
||||
PWM_InitStruct.OutputPin = GPIO_1;
|
||||
PWM_InitStruct.SrcClk = PWM_CLK_SRC_32M_DIV;
|
||||
PWM_InitStruct.SrcEnable = PWM_EN_SEL_SELF;
|
||||
|
||||
PWM_Init(XC_PWM0, &PWM_InitStruct);
|
||||
PWM_Start(XC_PWM0);
|
||||
PWM_LowPower_Enable(XC_PWM0);
|
||||
PWM_LowPower_CountDirection_Set(XC_PWM0,PWM_SLEEP_COUNT_DIRECTION_UP);
|
||||
|
||||
PWR_InitTypeDef PWR_InitStruct = {0};
|
||||
PWR_InitStruct.PWR_WakeITSrc = GPIO_IRQn_WAKE | RTC_IRQn_WAKE ;
|
||||
PWR_InitStruct.PWR_SleepMode = LIGHT_SLEEP_MODE;//LIGHT_SLEEP_MODE;//DEEP_SLEEP_MODE;
|
||||
|
||||
FMC_SPI_Init( );
|
||||
PWR_GPIO_SleepConfig(PWR_InitStruct.PWR_SleepMode);
|
||||
|
||||
if(PWR_InitStruct.PWR_SleepMode == LIGHT_SLEEP_MODE)
|
||||
{
|
||||
PWR_GPIO_LightSleepWakeConfig(GPIO_4, RIS_EDGE_INT);
|
||||
PWR_GPIO_LightSleepWakeConfig(GPIO_5, FAIL_EDGE_INT);
|
||||
}
|
||||
else if(PWR_InitStruct.PWR_SleepMode == DEEP_SLEEP_MODE)
|
||||
{
|
||||
// PWRKEY Initialization is required after deep sleep wakeup
|
||||
PWR_PWRKEY_Init();
|
||||
PWR_PWRKEY_DeepSleepWakeConfig(GPIO_4, DEEP_SLEEP_GPIO_WAKE_HIGH_LEVEL);
|
||||
PWR_PWRKEY_DeepSleepWakeConfig(GPIO_5, DEEP_SLEEP_GPIO_WAKE_LOW_LEVEL);
|
||||
}
|
||||
|
||||
PWR_SleepInit(&PWR_InitStruct);
|
||||
|
||||
while(1)
|
||||
{
|
||||
// DEBUG("opa %x\n", *((volatile unsigned *)(0x40000000 + 0x1bc)) ); //OPA
|
||||
// DEBUG("CMP %x\n", *((volatile unsigned *)(0x40000000 + 0x260)) ); //CMP
|
||||
Delay_Ms(100);
|
||||
PWR_CPU_SleepEnter( );
|
||||
DEBUG("wakeup\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
uint8_t CaptureCh0_Callback(void)
|
||||
{
|
||||
PWM_CaptureVal_Get(PWM_IC_CH0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t CaptureCh1_Callback(void)
|
||||
{
|
||||
PWM_CaptureVal_Get(PWM_IC_CH1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t CaptureCh2_Callback(void)
|
||||
{
|
||||
PWM_CaptureVal_Get(PWM_IC_CH2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
*-----------------------------------------------------------------------------------------------
|
||||
* PWM Timer
|
||||
*-----------------------------------------------------------------------------------------------
|
||||
*/
|
||||
void pwm_timer_demo(void)
|
||||
{
|
||||
DEBUG("PWM_TIMER_DEMO\r");
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
GPIO_InitStruct.Mux = GPIO_Mux1;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
GPIO_InitStruct.Int = NOT_INT;
|
||||
GPIO_InitStruct.FunSel = GPIO_Dx;
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_OUTPUT;
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_29;
|
||||
GPIO_Init( &GPIO_InitStruct );
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_30;
|
||||
GPIO_Init( &GPIO_InitStruct );
|
||||
|
||||
// GPIO_InitStruct.Pin = GPIO_35;
|
||||
// GPIO_Init( &GPIO_InitStruct );
|
||||
|
||||
GPIO_MuxCtl(GPIO_35, GPIO_Mux1);
|
||||
GPIO_MuxCtl(GPIO_36, GPIO_Mux1);
|
||||
GPIO_MuxCtl(GPIO_37, GPIO_Mux1);
|
||||
GPIO_MuxCtl(GPIO_38, GPIO_Mux1);
|
||||
|
||||
PWM_Timer_Ctl_Cfg T_Ctrl_Cfg;
|
||||
T_Ctrl_Cfg.Src_Clk = PWM_CLK_SRC_32M_DIV;
|
||||
T_Ctrl_Cfg.Clk_Div = PWM_CLK_DIV0;
|
||||
T_Ctrl_Cfg.Timer_Num = PWM_ALL_TIMER; //PWM_TIMER_1 | PWM_TIMER_2 | PWM_TIMER_3;
|
||||
T_Ctrl_Cfg.Timer_Mode = 0x01;
|
||||
T_Ctrl_Cfg.Timer_Int_Mask_En = DISABLE;
|
||||
T_Ctrl_Cfg.Timer_PWM_En = ENABLE;
|
||||
T_Ctrl_Cfg.Timer_0N100_PWM_En = ENABLE;
|
||||
PWM_Timer_Init(XC_PWM_TIMER, &T_Ctrl_Cfg);
|
||||
|
||||
PWM_Timer_Load_Cnt T_Load;
|
||||
T_Load.Timer_Num = PWM_ALL_TIMER; //PWM_TIMER_1 | PWM_TIMER_2 | PWM_TIMER_3;
|
||||
T_Load.Load_Cnt = 0x2ff;
|
||||
T_Load.Load_Cnt2 = 0xd00;
|
||||
PWM_Timer_SetLoad_Cnt(XC_PWM_TIMER, &T_Load);
|
||||
PWM_Timer_Start(XC_PWM_TIMER, T_Load.Timer_Num);
|
||||
}
|
||||
|
||||
#define PWM_TIMER_DEMO 1
|
||||
#define PWM_OUTPUT_DEMO 0
|
||||
#define PWM_CAPTURE_DEMO 0
|
||||
#define PWM_BRAKE_DEMO 0
|
||||
#define PWM_ENABLE_ADC_DEMO 0
|
||||
#define PWM_LOWPOWER_DEMO 0
|
||||
|
||||
void pwm_demo()
|
||||
{
|
||||
#if PWM_TIMER_DEMO
|
||||
pwm_timer_demo();
|
||||
#endif
|
||||
|
||||
#if PWM_OUTPUT_DEMO
|
||||
pwm_output_demo();
|
||||
#endif
|
||||
|
||||
#if PWM_CAPTURE_DEMO
|
||||
pwm_capture_demo();
|
||||
#endif
|
||||
|
||||
#if PWM_BRAKE_DEMO
|
||||
pwm_brake_demo();
|
||||
#endif
|
||||
|
||||
#if PWM_ENABLE_ADC_DEMO
|
||||
pwm_enable_adc_demo();
|
||||
#endif
|
||||
|
||||
#if PWM_LOWPOWER_DEMO
|
||||
pwm_lowpower_demo();
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* \file pwm_test.h
|
||||
*
|
||||
* \brief The head file of pwm_test.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __PWM_TEST_H__
|
||||
#define __PWM_TEST_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void pwm_test_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __PWM_TEST_H__ */
|
||||
@@ -0,0 +1,168 @@
|
||||
/*!
|
||||
* \file pwr.c
|
||||
*
|
||||
* \brief Target pwr 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 "pwr.h"
|
||||
#include "xc6xxx_hal_pwr.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief pwr_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
|
||||
void wakeup_timer_set(uint32_t time) {
|
||||
XC_CPR_AO->ALWAYS_ON_REG1 &= (~(1 << 19)); // deep sleep set
|
||||
TIMER_BaseInitTypeDef Timer_Init;
|
||||
Timer_Init.src_clk = TIMER_CLK_SRC_32K;
|
||||
Timer_Init.mode = TIMER_MODE_SINGLE_COUNT;
|
||||
Timer_Init.div_clk = TIMER_DIV_CLK_32000Hz;
|
||||
// Timer0 Config & Start
|
||||
TIMER_Base_Init(XC_AOTIMER0, &Timer_Init);
|
||||
TIMER_SetUs(XC_AOTIMER0, time);
|
||||
TIMER_Start_IT(XC_AOTIMER0);
|
||||
}
|
||||
|
||||
void system_sleep_init() {
|
||||
uint32_t WakeITSrc;
|
||||
#if (USE_XIP == 1)
|
||||
FMC_SPI_Init();
|
||||
#endif //(USE_XIP == 1)
|
||||
|
||||
// PWR_InitTypeDef PWR_InitStruct = {0};
|
||||
#if (USE_XIP != 1)
|
||||
SPI_InitCfg_t spi_cfg = {0};
|
||||
spi_cfg.Mode = SPI_MODE_MASTER;
|
||||
spi_cfg.DataSize = SSI_CTRL0_DFS_LEN_8BIT;
|
||||
spi_cfg.Direction = SSI_CTRL0_TMOD_WR;
|
||||
spi_cfg.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
|
||||
spi_cfg.CLKPolarity = SSI_CTRL0_SCPOL_LOW;
|
||||
spi_cfg.CLKPhase = SPI_CPHA_LEAD;
|
||||
spi_cfg.FirstBit = SPI_FirstBit_MSB;
|
||||
|
||||
xc_spi_init(XC_SPI0, &spi_cfg);
|
||||
SPI_Flash_PowerDown(XC_SPI0);
|
||||
#endif //(USE_XIP!=1)
|
||||
GPIO_SleepConfig();
|
||||
WakeITSrc =
|
||||
GPIO_IRQn_WAKE | RTC_IRQn_WAKE | TIMER_AO0_IRQn_WAKE | TIMER0_IRQn_WAKE;
|
||||
__PWR_WakeIT_Set(WakeITSrc);
|
||||
}
|
||||
|
||||
void system_lightsleep_cfg() {
|
||||
#if (USE_XIP != 1)
|
||||
cprao_aon_puctrl1_set(0x4);
|
||||
// XC_CPR_AO->PU_CTRLx[0] = 0x4; // 0x4; /* puctrl1= 0x4 , SSI0RX must
|
||||
// pulldown*/
|
||||
#endif //(USE_XIP!=1)
|
||||
|
||||
XC_CPR_AO->SYS_TIME = (RST_READY_TIME << 12) | (OSC32_STABLE_TIME);
|
||||
__PWR_PD_LightSleep_Set();
|
||||
__PWR_SleepSrcMask_Set(0x1e001e);
|
||||
__PWR_OSC_Off();
|
||||
}
|
||||
|
||||
void system_deepsleep_cfg() {
|
||||
#if (USE_XIP != 1)
|
||||
XC_CPR_AO->PU_CTRLx[0] = 0xf; // 0xF;
|
||||
#endif //(USE_XIP!=1)
|
||||
|
||||
XC_CPR_AO->SYS_TIME = (RST_READY_TIME << 12) | (OSC32_STABLE_TIME);
|
||||
__PWR_PD_DeepSleep_Set();
|
||||
Delay_Us(100);
|
||||
__PWR_SleepSrcMask_Set(0x1e001e);
|
||||
__PWR_OSC_Off();
|
||||
}
|
||||
|
||||
void Sleep_Demo() {
|
||||
|
||||
system_sleep_init();
|
||||
PWR_GPIO_LightSleepWakeConfig(GPIO_3, RIS_EDGE_INT);
|
||||
PWR_GPIO_LightSleepWakeConfig(GPIO_2, FAIL_EDGE_INT);
|
||||
system_lightsleep_cfg();
|
||||
|
||||
while (1) {
|
||||
|
||||
Sleep();
|
||||
DEBUG("wakeup\n");
|
||||
}
|
||||
}
|
||||
|
||||
void DeepSleep_Demo() {
|
||||
|
||||
system_sleep_init();
|
||||
// PWRKEY Initialization is required after deep sleep wakeup
|
||||
PWR_PWRKEY_Init();
|
||||
PWR_PWRKEY_DeepSleepWakeConfig(GPIO_2, DEEP_SLEEP_GPIO_WAKE_HIGH_LEVEL);
|
||||
PWR_PWRKEY_DeepSleepWakeConfig(GPIO_3, DEEP_SLEEP_GPIO_WAKE_LOW_LEVEL);
|
||||
system_deepsleep_cfg();
|
||||
|
||||
while (1) {
|
||||
DeepSleep();
|
||||
}
|
||||
}
|
||||
|
||||
void DeepSleep_Timerwakeup_Demo() {
|
||||
system_sleep_init();
|
||||
// PWRKEY Initialization is required after deep sleep wakeup
|
||||
PWR_PWRKEY_Init();
|
||||
system_deepsleep_cfg();
|
||||
|
||||
while (1) {
|
||||
wakeup_timer_set(100 * 1000);
|
||||
DeepSleep();
|
||||
}
|
||||
}
|
||||
|
||||
#define SLEEP_DEMO 1
|
||||
#define DEEPSLEEP_DEMO 0
|
||||
#define DEEPSLEEP_TIMERWAKEUP_DEMO 0
|
||||
|
||||
void pwr_demo() {
|
||||
#if SLEEP_DEMO
|
||||
Sleep_Demo();
|
||||
#endif
|
||||
|
||||
#if DEEPSLEEP_DEMO
|
||||
DeepSleep_Demo();
|
||||
#endif
|
||||
|
||||
#if DEEPSLEEP_TIMERWAKEUP_DEMO
|
||||
DeepSleep_Timerwakeup_Demo();
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*!
|
||||
* \file pwr.h
|
||||
*
|
||||
* \brief The head file of pwr.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __PWR_H__
|
||||
#define __PWR_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void pwr_demo(void);
|
||||
void wakeup_timer_set(uint32_t time);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __PWR_H__ */
|
||||
@@ -0,0 +1,68 @@
|
||||
/*!
|
||||
* \file qdec.c
|
||||
*
|
||||
* \brief Target qdec 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 "qdec.h"
|
||||
#include "xc6xxx_hal_qdec.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
****************************************************************************************
|
||||
* @brief
|
||||
*
|
||||
* @param[in]
|
||||
* @param[in]
|
||||
****************************************************************************************
|
||||
*/
|
||||
void qdec_demo(void) {
|
||||
DEBUG("__QDEC_DEMO__\n");
|
||||
|
||||
DEBUG("0_XC_QDEC->QDEC_CTL[0x%08x]: %08x\n", &XC_QDEC->QDEC_CTL,
|
||||
XC_QDEC->QDEC_CTL);
|
||||
QDEC_InitCfg qdec_config;
|
||||
qdec_config.Auto_Clr_En = DISABLE;
|
||||
qdec_config.Single_Sample_Rst_En = DISABLE;
|
||||
qdec_config.DB_Filter_En = ENABLE;
|
||||
qdec_config.Acc_Sample_Div = QDEC_DIVIDE_DIV_1024;
|
||||
qdec_config.Pts_Sampled = QDEC_PTS_NUM_80;
|
||||
qdec_config.DB_Sample_Div = QDEC_DB_SAMP_DIV_32;
|
||||
|
||||
QDEC_Init(XC_QDEC, &qdec_config);
|
||||
// XC_QDEC->QDEC_CTL = 0x01;
|
||||
DEBUG("1_XC_QDEC->QDEC_CTL[0x%08x]: %08x\n", &XC_QDEC->QDEC_CTL,
|
||||
XC_QDEC->QDEC_CTL);
|
||||
DEBUG("XC_QDEC->SAMP_CTL[0x%08x]: %08x\n", &XC_QDEC->SAMP_CTL,
|
||||
XC_QDEC->SAMP_CTL);
|
||||
// NVIC_EnableIRQ(QDEC_IRQn);
|
||||
|
||||
__QDEC_Enable();
|
||||
__QDEC_Start_Count();
|
||||
|
||||
DEBUG("2_XC_QDEC->QDEC_CTL[0x%08x]: %08x\n", &XC_QDEC->QDEC_CTL,
|
||||
XC_QDEC->QDEC_CTL);
|
||||
NVIC_EnableIRQ(QDEC_IRQn);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* \file qdec.h
|
||||
*
|
||||
* \brief The head file of qdec.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __QDEC_H__
|
||||
#define __QDEC_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void qdec_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __QDEC_H__ */
|
||||
@@ -0,0 +1,213 @@
|
||||
/*!
|
||||
* \file rtc.c
|
||||
*
|
||||
* \brief Target rtc 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 "rtc.h"
|
||||
#include "xc6xxx_hal_rtc.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
//#define LED1_PIN GPIO_26
|
||||
//#define LED2_PIN GPIO_27
|
||||
//#define KEY1_PIN GPIO_1
|
||||
//#define KEY2_PIN GPIO_5
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
Rtc_Intr_Sta_t RTC_Intr_Sta = {.T1_CMR = false,
|
||||
.T2_CMR = false,
|
||||
.T3_CMR = false,
|
||||
.Day = false,
|
||||
.Hour = false,
|
||||
.Min = false,
|
||||
.Sec = false};
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief rtc_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void rtc_demo(void) {
|
||||
DEBUG("__RTC_DEMO__\r");
|
||||
|
||||
RTC_InitTypeDef rtc_cfg = {0};
|
||||
Rtc_DateTypeDef Date = {0};
|
||||
|
||||
rtc_cfg.Date.Sec = 0;
|
||||
rtc_cfg.Date.Min = 0;
|
||||
rtc_cfg.Date.Hour = 18;
|
||||
rtc_cfg.Date.Day = 154;
|
||||
rtc_cfg.Date.Week = 6;
|
||||
|
||||
rtc_cfg.DateLimit.SecLimit = 60;
|
||||
rtc_cfg.DateLimit.MinLimit = 60;
|
||||
rtc_cfg.DateLimit.HourLimit = 24;
|
||||
|
||||
rtc_cfg.MatchTime.ch = RTC_MATCH_TIME_1;
|
||||
rtc_cfg.MatchTime.Sec = 5;
|
||||
rtc_cfg.MatchTime.Min = 02;
|
||||
rtc_cfg.MatchTime.Hour = 18;
|
||||
rtc_cfg.MatchTime.Week = RTC_WEEK_MATCH_SATURDAY;
|
||||
rtc_cfg.MatchTimeEnable = true;
|
||||
|
||||
rtc_cfg.SecIT_Enable = true;
|
||||
rtc_cfg.MinIT_Enable = true;
|
||||
rtc_cfg.HourIT_Enable = true;
|
||||
rtc_cfg.DayIT_Enable = false;
|
||||
|
||||
RTC_Init(XC_RTC, &rtc_cfg);
|
||||
RTC_Start(XC_RTC);
|
||||
NVIC_EnableIRQ(RTC_IRQn);
|
||||
|
||||
while (1) {
|
||||
if (RTC_Intr_Sta.T1_CMR == true) {
|
||||
DEBUG("RTC T1 CMR Irq\n");
|
||||
RTC_Intr_Sta.T1_CMR = false;
|
||||
}
|
||||
|
||||
if (RTC_Intr_Sta.T2_CMR == true) {
|
||||
DEBUG("RTC T2 CMR Irq\n");
|
||||
RTC_Intr_Sta.T2_CMR = false;
|
||||
}
|
||||
|
||||
if (RTC_Intr_Sta.T3_CMR == true) {
|
||||
DEBUG("RTC T3 CMR Irq\n");
|
||||
RTC_Intr_Sta.T3_CMR = false;
|
||||
}
|
||||
|
||||
if (RTC_Intr_Sta.Sec == true) {
|
||||
RTC_DateGet(XC_RTC, &Date);
|
||||
DEBUG("%02d:%02d:%02d\n", Date.Hour, Date.Min, Date.Sec);
|
||||
RTC_Intr_Sta.Sec = false;
|
||||
}
|
||||
|
||||
if (RTC_Intr_Sta.Min == true) {
|
||||
RTC_Intr_Sta.Min = false;
|
||||
}
|
||||
|
||||
if (RTC_Intr_Sta.Hour == true) {
|
||||
RTC_Intr_Sta.Hour = false;
|
||||
}
|
||||
|
||||
if (RTC_Intr_Sta.Day == true) {
|
||||
RTC_Intr_Sta.Day = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief T1_CMR_Callback
|
||||
* @details RTC compare timer1 callback function
|
||||
* @param void
|
||||
* @retval uint8_t
|
||||
*/
|
||||
__RAM_CODE uint8_t T1_CMR_Callback(void) {
|
||||
RTC_Intr_Sta.T1_CMR = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief T2_CMR_Callback
|
||||
* @details RTC compare timer2 callback function
|
||||
* @param void
|
||||
* @retval uint8_t
|
||||
*/
|
||||
__RAM_CODE uint8_t T2_CMR_Callback(void) {
|
||||
RTC_Intr_Sta.T2_CMR = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief T3_CMR_Callback
|
||||
* @details RTC compare timer3 callback function
|
||||
* @param void
|
||||
* @retval uint8_t
|
||||
*/
|
||||
__RAM_CODE uint8_t T3_CMR_Callback(void) {
|
||||
RTC_Intr_Sta.T3_CMR = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sec_Callback
|
||||
* @details RTC second callback function
|
||||
* @param void
|
||||
* @retval uint8_t
|
||||
*/
|
||||
__RAM_CODE uint8_t Sec_Callback(void) {
|
||||
RTC_Intr_Sta.Sec = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Min_Callback
|
||||
* @details RTC minute callback function
|
||||
* @param void
|
||||
* @retval uint8_t
|
||||
*/
|
||||
__RAM_CODE uint8_t Min_Callback(void) {
|
||||
RTC_Intr_Sta.Min = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Hour_Callback
|
||||
* @details RTC hour callback function
|
||||
* @param void
|
||||
* @retval uint8_t
|
||||
*/
|
||||
__RAM_CODE uint8_t Hour_Callback(void) {
|
||||
RTC_Intr_Sta.Hour = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Day_Callback
|
||||
* @details RTC Day callback function
|
||||
* @param void
|
||||
* @retval uint8_t
|
||||
*/
|
||||
__RAM_CODE uint8_t Day_Callback(void) {
|
||||
RTC_Intr_Sta.Day = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*!
|
||||
* \file rtc.h
|
||||
*
|
||||
* \brief The head file of rtc.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __RTC_H__
|
||||
#define __RTC_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
typedef struct Rtc_Intr_Status_s {
|
||||
uint8_t T1_CMR;
|
||||
uint8_t T2_CMR;
|
||||
uint8_t T3_CMR;
|
||||
uint8_t Day;
|
||||
uint8_t Hour;
|
||||
uint8_t Min;
|
||||
uint8_t Sec;
|
||||
} Rtc_Intr_Sta_t;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void rtc_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __RTC_H__ */
|
||||
@@ -0,0 +1,87 @@
|
||||
/*!
|
||||
* \file pwr_test.c
|
||||
*
|
||||
* \brief Target pwr test 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 "pwr_test.h"
|
||||
#include "xc60xx_pwr.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief pwr_test_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
|
||||
void pwr_test_demo() {
|
||||
SPI_InitCfg_t spi_cfg = {0};
|
||||
PWR_InitTypeDef PWR_InitStruct = {0};
|
||||
|
||||
spi_cfg.Mode = SPI_MODE_MASTER;
|
||||
spi_cfg.DataSize = SSI_CTRL0_DFS_LEN_8BIT;
|
||||
spi_cfg.Direction = SSI_CTRL0_TMOD_WR;
|
||||
spi_cfg.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
|
||||
spi_cfg.CLKPolarity = SSI_CTRL0_SCPOL_LOW;
|
||||
spi_cfg.CLKPhase = SPI_CPHA_LEAD;
|
||||
spi_cfg.FirstBit = SPI_FirstBit_MSB;
|
||||
|
||||
xc_spi_init(XC_SPI0, &spi_cfg);
|
||||
SPI_Flash_PowerDown(XC_SPI0);
|
||||
|
||||
PWR_InitStruct.PWR_WakeITSrc = GPIO_IRQn_WAKE | RTC_IRQn_WAKE;
|
||||
PWR_InitStruct.PWR_SleepMode = DEEP_SLEEP_MODE;
|
||||
|
||||
PWR_GPIO_SleepConfig(PWR_InitStruct.PWR_SleepMode);
|
||||
|
||||
if (PWR_InitStruct.PWR_SleepMode == LIGHT_SLEEP_MODE) {
|
||||
PWR_GPIO_LightSleepWakeConfig(GPIO_3, RIS_EDGE_INT);
|
||||
PWR_GPIO_LightSleepWakeConfig(GPIO_22, FAIL_EDGE_INT);
|
||||
} else if (PWR_InitStruct.PWR_SleepMode == DEEP_SLEEP_MODE) {
|
||||
// PWRKEY Initialization is required after deep sleep wakeup
|
||||
PWR_PWRKEY_Init();
|
||||
PWR_PWRKEY_DeepSleepWakeConfig(GPIO_22, DEEP_SLEEP_GPIO_WAKE_HIGH_LEVEL);
|
||||
PWR_PWRKEY_DeepSleepWakeConfig(GPIO_7, DEEP_SLEEP_GPIO_WAKE_LOW_LEVEL);
|
||||
}
|
||||
|
||||
PWR_SleepInit(&PWR_InitStruct);
|
||||
|
||||
while (1) {
|
||||
Delay_Ms(100);
|
||||
DEBUG("sleep\n");
|
||||
PWR_CPU_SleepEnter(PWR_InitStruct.PWR_SleepMode);
|
||||
DEBUG("wakeup\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* \file pwr_test.h
|
||||
*
|
||||
* \brief The head file of pwr_test.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __PWR_TEST_H__
|
||||
#define __PWR_TEST_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void pwr_test_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __PWR_TEST_H__ */
|
||||
@@ -0,0 +1,459 @@
|
||||
/*!
|
||||
* \file spi_flash.c
|
||||
*
|
||||
* \brief Target spi flash 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 "spi_flash.h"
|
||||
#include "xc6xxx_hal_spi.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
#define FLASH_MAX_SIZE 256 * 1024
|
||||
#define FLASH_MAX_PAGE_NUM (FLASH_MAX_SIZE / FLASH_PAGE_SIZE)
|
||||
|
||||
#define TEST_BASE_ADDR 0x6400
|
||||
#define TEST_START_PAGE_IDX (TEST_BASE_ADDR / FLASH_PAGE_SIZE)
|
||||
|
||||
#define TEST_CYCLE_NUM 4u
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief spi_flash_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void spi_flash_demo(void) {
|
||||
DEBUG("__SPI_FLASH_DEMO__\r");
|
||||
|
||||
SPI_TypeDef *spi_idx = XC_SPI0; // XC_SPI0;//XC_SPI1;//XC_SPI2;
|
||||
|
||||
if (spi_idx == XC_SPI1) {
|
||||
GPIO_InitTypeDef gpio_cfg = {0};
|
||||
|
||||
gpio_cfg.Mux = GPIO_Mux0;
|
||||
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
|
||||
gpio_cfg.FunSel = SSI1_CLK;
|
||||
gpio_cfg.Int = NOT_INT;
|
||||
gpio_cfg.Pull = GPIO_PULLDOWN;
|
||||
gpio_cfg.Pin = GPIO_14;
|
||||
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_SSN;
|
||||
gpio_cfg.Pin = GPIO_15;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_TX;
|
||||
gpio_cfg.Pin = GPIO_16;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_RX;
|
||||
gpio_cfg.Pin = GPIO_17;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
} else if (spi_idx == XC_SPI2) {
|
||||
GPIO_InitTypeDef gpio_cfg = {0};
|
||||
|
||||
gpio_cfg.Mux = GPIO_Mux2;
|
||||
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
|
||||
gpio_cfg.FunSel = GPIO_Dx;
|
||||
gpio_cfg.Int = NOT_INT;
|
||||
gpio_cfg.Pull = GPIO_PULLDOWN;
|
||||
gpio_cfg.Pin = GPIO_14; // clk
|
||||
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = GPIO_Dx;
|
||||
gpio_cfg.Pin = GPIO_15; // ssn
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = GPIO_Dx;
|
||||
gpio_cfg.Pin = GPIO_16;
|
||||
GPIO_Init(&gpio_cfg); // mosi
|
||||
|
||||
gpio_cfg.FunSel = GPIO_Dx;
|
||||
gpio_cfg.Pin = GPIO_17;
|
||||
GPIO_Init(&gpio_cfg); // miso
|
||||
}
|
||||
|
||||
SPI_InitCfg_t spi_cfg = {0};
|
||||
|
||||
spi_cfg.Mode = SPI_MODE_MASTER;
|
||||
spi_cfg.DataSize = SSI_CTRL0_DFS_LEN_8BIT;
|
||||
spi_cfg.Direction = SSI_CTRL0_TMOD_WR;
|
||||
spi_cfg.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
|
||||
spi_cfg.CLKPolarity = SSI_CTRL0_SCPOL_LOW;
|
||||
spi_cfg.CLKPhase = SPI_CPHA_LEAD;
|
||||
spi_cfg.FirstBit = SPI_FirstBit_MSB;
|
||||
|
||||
xc_spi_init(spi_idx, &spi_cfg);
|
||||
|
||||
DEBUG("SPI Start\n");
|
||||
|
||||
xc_spi_flash_wake_up(spi_idx);
|
||||
|
||||
uint32_t mid = 0;
|
||||
spi_flash_rdid(spi_idx, (uint8_t *)&mid);
|
||||
DEBUG("Flash RDID: 0x%08x\n", mid);
|
||||
|
||||
uint8_t ruid[16];
|
||||
spi_flash_read_128bit_uid(spi_idx, ruid);
|
||||
DEBUG("Flash RUID: ");
|
||||
for (int i = 0; i < 16; i++) {
|
||||
PRINT("%02x ", ruid[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
uint8_t data[FLASH_PAGE_SIZE];
|
||||
#if 1
|
||||
uint8_t rd_data[FLASH_PAGE_SIZE];
|
||||
uint8_t cycle_num = 1;
|
||||
// xc_spi_flash_erase_sector(spi_idx, 0x3F000);
|
||||
|
||||
while (cycle_num <= TEST_CYCLE_NUM) {
|
||||
DEBUG("---------------- %d Cycle Start ----------------\n", cycle_num);
|
||||
for (uint16_t n = TEST_START_PAGE_IDX; n < FLASH_MAX_PAGE_NUM; n++) {
|
||||
memset(data, 0x55, FLASH_PAGE_SIZE);
|
||||
spi_flash_erase_page(spi_idx, n * FLASH_PAGE_SIZE);
|
||||
DEBUG("Page 0x%x Erased.\n", n * FLASH_PAGE_SIZE);
|
||||
xc_spi_flash_write_page(spi_idx, n * FLASH_PAGE_SIZE, data);
|
||||
DEBUG("Page 0x%x has been Written.\n", n * FLASH_PAGE_SIZE);
|
||||
xc_spi_flash_read_page(spi_idx, n * FLASH_PAGE_SIZE, rd_data);
|
||||
DEBUG("Page 0x%x Read: ", n * FLASH_PAGE_SIZE);
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
if (rd_data[i] != 0x55)
|
||||
DEBUG("Err: %02x\n", rd_data[i]);
|
||||
}
|
||||
memset(rd_data, 0, FLASH_PAGE_SIZE);
|
||||
DEBUG("--- Page 0x%x Check Complete ---\n", n * FLASH_PAGE_SIZE);
|
||||
}
|
||||
DEBUG("********** Code 0x55 Test End **********\n");
|
||||
|
||||
for (uint16_t n = TEST_START_PAGE_IDX; n < FLASH_MAX_PAGE_NUM; n++) {
|
||||
memset(data, 0xaa, FLASH_PAGE_SIZE);
|
||||
spi_flash_erase_page(spi_idx, n * FLASH_PAGE_SIZE);
|
||||
DEBUG("Page 0x%x Erased.\n", n * FLASH_PAGE_SIZE);
|
||||
xc_spi_flash_write_page(spi_idx, n * FLASH_PAGE_SIZE, data);
|
||||
DEBUG("Page 0x%x has been Written.\n", n * FLASH_PAGE_SIZE);
|
||||
xc_spi_flash_read_page(spi_idx, n * FLASH_PAGE_SIZE, rd_data);
|
||||
DEBUG("Page 0x%x Read: ", n * FLASH_PAGE_SIZE);
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
if (rd_data[i] != 0xaa)
|
||||
DEBUG("Err: %02x\n", rd_data[i]);
|
||||
}
|
||||
memset(rd_data, 0, FLASH_PAGE_SIZE);
|
||||
DEBUG("--- Page 0x%x Check Complete ---\n", n * FLASH_PAGE_SIZE);
|
||||
}
|
||||
DEBUG("********** Code 0xAA Test End **********\n");
|
||||
|
||||
for (uint16_t n = TEST_START_PAGE_IDX; n < FLASH_MAX_PAGE_NUM; n++) {
|
||||
memset(data, 0x00, FLASH_PAGE_SIZE);
|
||||
spi_flash_erase_page(spi_idx, n * FLASH_PAGE_SIZE);
|
||||
DEBUG("Page 0x%x Erased.\n", n * FLASH_PAGE_SIZE);
|
||||
xc_spi_flash_write_page(spi_idx, n * FLASH_PAGE_SIZE, data);
|
||||
DEBUG("Page 0x%x has been Written.\n", n * FLASH_PAGE_SIZE);
|
||||
xc_spi_flash_read_page(spi_idx, n * FLASH_PAGE_SIZE, rd_data);
|
||||
DEBUG("Page 0x%x Read: ", n * FLASH_PAGE_SIZE);
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
if (rd_data[i] != 0x00)
|
||||
DEBUG("Err: %02x\n", rd_data[i]);
|
||||
}
|
||||
memset(rd_data, 0, FLASH_PAGE_SIZE);
|
||||
DEBUG("--- Page 0x%x Check Complete ---\n", n * FLASH_PAGE_SIZE);
|
||||
}
|
||||
DEBUG("********** Code 0x00 Test End **********\n");
|
||||
|
||||
DEBUG("----------------- %d Cycle End -----------------\n", cycle_num);
|
||||
cycle_num++;
|
||||
}
|
||||
#else
|
||||
xc_spi_flash_erase_sector(spi_idx, 0x3F000);
|
||||
xc_spi_flash_read_page(spi_idx, 0x3F000, data);
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
PRINT("%02x ", data[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
data[i] = i;
|
||||
}
|
||||
// data[0] = 110;
|
||||
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
xc_spi_flash_write_page(spi_idx, 0x3F000 + i * FLASH_PAGE_SIZE, data);
|
||||
data[0]++;
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
memset(data, 0xff, FLASH_PAGE_SIZE);
|
||||
xc_spi_flash_read_page(spi_idx, 0x3F000 + i * FLASH_PAGE_SIZE, data);
|
||||
DEBUG("Flash Read finished%d: \n", i);
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
PRINT("%02x ", data[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
}
|
||||
|
||||
uint8_t w_buff[716];
|
||||
memset(w_buff, 0x11, sizeof(w_buff));
|
||||
spi_write_bytes(spi_idx, 0x3F000 + 57, w_buff, sizeof(w_buff));
|
||||
DEBUG("Flash Write Any finish\n");
|
||||
|
||||
uint8_t r_buff[1024];
|
||||
spi_flash_read(spi_idx, 0x3F000, r_buff, sizeof(r_buff));
|
||||
DEBUG("Flash Read Any addr: \n");
|
||||
for (uint16_t i = 0; i < sizeof(r_buff); i++)
|
||||
PRINT("%02x ", r_buff[i]);
|
||||
PRINT("\n");
|
||||
|
||||
memset(w_buff, 0x22, sizeof(w_buff));
|
||||
spi_write_bytes(spi_idx, 0x3F000 + 196, w_buff, 115);
|
||||
DEBUG("Flash Write Any finish2\n");
|
||||
|
||||
spi_flash_read(spi_idx, 0x3F000, r_buff, sizeof(r_buff));
|
||||
DEBUG("Flash Read Any addr2: \n");
|
||||
for (uint16_t i = 0; i < sizeof(r_buff); i++)
|
||||
PRINT("%02x ", r_buff[i]);
|
||||
PRINT("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief spi_flash_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void spi_dma_flash_demo(void) {
|
||||
DEBUG("__SPI_DMA_FLASH_DEMO__\n");
|
||||
|
||||
SPI_TypeDef *spi_idx = XC_SPI0; // XC_SPI0;//XC_SPI1;//XC_SPI2;
|
||||
|
||||
if (spi_idx == XC_SPI1) {
|
||||
GPIO_InitTypeDef gpio_cfg = {0};
|
||||
|
||||
gpio_cfg.Mux = GPIO_Mux0;
|
||||
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
|
||||
gpio_cfg.FunSel = SSI1_CLK;
|
||||
gpio_cfg.Int = NOT_INT;
|
||||
gpio_cfg.Pull = GPIO_PULLDOWN;
|
||||
gpio_cfg.Pin = GPIO_14;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_SSN;
|
||||
gpio_cfg.Pin = GPIO_15; // GPIO_15;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_TX;
|
||||
gpio_cfg.Pin = GPIO_16;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_RX;
|
||||
gpio_cfg.Pin = GPIO_17;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
} else if (spi_idx == XC_SPI2) {
|
||||
GPIO_InitTypeDef gpio_cfg = {0};
|
||||
|
||||
gpio_cfg.Mux = GPIO_Mux2;
|
||||
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
|
||||
gpio_cfg.FunSel = GPIO_Dx;
|
||||
gpio_cfg.Int = NOT_INT;
|
||||
gpio_cfg.Pull = GPIO_PULLDOWN;
|
||||
gpio_cfg.Pin = GPIO_14; // clk
|
||||
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = GPIO_Dx;
|
||||
gpio_cfg.Pin = GPIO_15; // ssn
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = GPIO_Dx;
|
||||
gpio_cfg.Pin = GPIO_16;
|
||||
GPIO_Init(&gpio_cfg); // mosi
|
||||
|
||||
gpio_cfg.FunSel = GPIO_Dx;
|
||||
gpio_cfg.Pin = GPIO_17;
|
||||
GPIO_Init(&gpio_cfg); // miso
|
||||
}
|
||||
|
||||
SPI_InitCfg_t spi_cfg = {0};
|
||||
|
||||
spi_cfg.Mode = SPI_MODE_MASTER;
|
||||
spi_cfg.DataSize = SSI_CTRL0_DFS_LEN_8BIT;
|
||||
spi_cfg.Direction = SSI_CTRL0_TMOD_WR;
|
||||
spi_cfg.BaudRatePrescaler =
|
||||
SPI_BAUDRATEPRESCALER_2; // SPI_BAUDRATEPRESCALER_2;
|
||||
spi_cfg.CLKPolarity = SSI_CTRL0_SCPOL_LOW;
|
||||
spi_cfg.CLKPhase = SPI_CPHA_LEAD;
|
||||
spi_cfg.FirstBit = SPI_FirstBit_MSB;
|
||||
|
||||
xc_spi_init(spi_idx, &spi_cfg);
|
||||
NVIC_EnableIRQ(DMA_IRQn);
|
||||
|
||||
SPI_DMA_Flash_WakeUp(spi_idx);
|
||||
|
||||
uint32_t mid = 0;
|
||||
SPI_DMA_Flash_RDID(spi_idx, (uint8_t *)&mid);
|
||||
// spi_flash_rdid(spi_idx, (uint8_t*)&mid);
|
||||
DEBUG("Flash RDID: 0x%08x\n", mid);
|
||||
|
||||
uint8_t ruid[16];
|
||||
SPI_DMA_Flash_RUID(spi_idx, ruid);
|
||||
// spi_flash_read_128bit_uid(spi_idx, ruid);
|
||||
DEBUG("Flash RUID: ");
|
||||
for (int i = 0; i < 16; i++) {
|
||||
PRINT("%02x ", ruid[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
uint8_t data[FLASH_PAGE_SIZE];
|
||||
#if 1
|
||||
// test
|
||||
uint8_t rd_data[FLASH_PAGE_SIZE];
|
||||
uint8_t cycle_num = 1;
|
||||
|
||||
while (cycle_num <= TEST_CYCLE_NUM) {
|
||||
DEBUG("---------------- %d Cycle Start ----------------\n", cycle_num);
|
||||
for (uint16_t n = TEST_START_PAGE_IDX; n < FLASH_MAX_PAGE_NUM; n++) {
|
||||
memset(data, 0x55, FLASH_PAGE_SIZE);
|
||||
SPI_DMA_Flash_Erase_Page(spi_idx, n * FLASH_PAGE_SIZE);
|
||||
DEBUG("Page 0x%x Erased.\n", n * FLASH_PAGE_SIZE);
|
||||
SPI_DMA_Flash_WritePage(spi_idx, n * FLASH_PAGE_SIZE, data,
|
||||
FLASH_PAGE_SIZE);
|
||||
DEBUG("Page 0x%x has been Written.\n", n * FLASH_PAGE_SIZE);
|
||||
SPI_DMA_Flash_ReadPage(spi_idx, n * FLASH_PAGE_SIZE, rd_data,
|
||||
FLASH_PAGE_SIZE);
|
||||
DEBUG("Page 0x%x Read: ", n * FLASH_PAGE_SIZE);
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
if (rd_data[i] != 0x55)
|
||||
DEBUG("Err: %02x\n", rd_data[i]);
|
||||
}
|
||||
memset(rd_data, 0, FLASH_PAGE_SIZE);
|
||||
DEBUG("--- Page 0x%x Check Complete ---\n", n * FLASH_PAGE_SIZE);
|
||||
}
|
||||
DEBUG("********** Code 0x55 Test End **********\n");
|
||||
|
||||
for (uint16_t n = TEST_START_PAGE_IDX; n < FLASH_MAX_PAGE_NUM; n++) {
|
||||
memset(data, 0xaa, FLASH_PAGE_SIZE);
|
||||
SPI_DMA_Flash_Erase_Page(spi_idx, n * FLASH_PAGE_SIZE);
|
||||
DEBUG("Page 0x%x Erased.\n", n * FLASH_PAGE_SIZE);
|
||||
SPI_DMA_Flash_WritePage(spi_idx, n * FLASH_PAGE_SIZE, data,
|
||||
FLASH_PAGE_SIZE);
|
||||
DEBUG("Page 0x%x has been Written.\n", n * FLASH_PAGE_SIZE);
|
||||
SPI_DMA_Flash_ReadPage(spi_idx, n * FLASH_PAGE_SIZE, rd_data,
|
||||
FLASH_PAGE_SIZE);
|
||||
DEBUG("Page 0x%x Read: ", n * FLASH_PAGE_SIZE);
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
if (rd_data[i] != 0xaa)
|
||||
DEBUG("Err: %02x\n", rd_data[i]);
|
||||
}
|
||||
memset(rd_data, 0, FLASH_PAGE_SIZE);
|
||||
DEBUG("--- Page 0x%x Check Complete ---\n", n * FLASH_PAGE_SIZE);
|
||||
}
|
||||
DEBUG("********** Code 0xAA Test End **********\n");
|
||||
|
||||
for (uint16_t n = TEST_START_PAGE_IDX; n < FLASH_MAX_PAGE_NUM; n++) {
|
||||
memset(data, 0x00, FLASH_PAGE_SIZE);
|
||||
SPI_DMA_Flash_Erase_Page(spi_idx, n * FLASH_PAGE_SIZE);
|
||||
DEBUG("Page 0x%x Erased.\n", n * FLASH_PAGE_SIZE);
|
||||
SPI_DMA_Flash_WritePage(spi_idx, n * FLASH_PAGE_SIZE, data,
|
||||
FLASH_PAGE_SIZE);
|
||||
DEBUG("Page 0x%x has been Written.\n", n * FLASH_PAGE_SIZE);
|
||||
SPI_DMA_Flash_ReadPage(spi_idx, n * FLASH_PAGE_SIZE, rd_data,
|
||||
FLASH_PAGE_SIZE);
|
||||
DEBUG("Page 0x%x Read: ", n * FLASH_PAGE_SIZE);
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
if (rd_data[i] != 0x00)
|
||||
DEBUG("Err: %02x\n", rd_data[i]);
|
||||
}
|
||||
memset(rd_data, 0, FLASH_PAGE_SIZE);
|
||||
DEBUG("--- Page 0x%x Check Complete ---\n", n * FLASH_PAGE_SIZE);
|
||||
}
|
||||
DEBUG("********** Code 0x00 Test End **********\n");
|
||||
|
||||
DEBUG("----------------- %d Cycle End -----------------\n", cycle_num);
|
||||
cycle_num++;
|
||||
}
|
||||
|
||||
#else
|
||||
SPI_DMA_Flash_Erase_Sector(spi_idx, 0x5000);
|
||||
SPI_DMA_Flash_ReadPage(spi_idx, 0x5000, data, FLASH_PAGE_SIZE);
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
PRINT("%02x ", data[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
data[i] = i;
|
||||
}
|
||||
// data[0] = 110;
|
||||
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
SPI_DMA_Flash_WritePage(spi_idx, 0x5000 + i * FLASH_PAGE_SIZE, data,
|
||||
FLASH_PAGE_SIZE);
|
||||
data[0]++;
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
memset(data, 0xff, FLASH_PAGE_SIZE);
|
||||
SPI_DMA_Flash_ReadPage(spi_idx, 0x5000 + i * FLASH_PAGE_SIZE, data,
|
||||
FLASH_PAGE_SIZE);
|
||||
DEBUG("Flash Read finished%d: \n", i);
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
PRINT("%02x ", data[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
}
|
||||
|
||||
uint8_t w_buff[716];
|
||||
memset(w_buff, 0x11, sizeof(w_buff));
|
||||
SPI_DMA_FlashWrite(spi_idx, 0x5000 + 57, w_buff, sizeof(w_buff));
|
||||
DEBUG("Flash Write Any finish\n");
|
||||
|
||||
uint8_t r_buff[1024];
|
||||
SPI_DMA_FlashRead(spi_idx, 0x5000, r_buff, sizeof(r_buff));
|
||||
DEBUG("Flash Read Any addr: \n");
|
||||
for (uint16_t i = 0; i < sizeof(r_buff); i++)
|
||||
PRINT("%02x ", r_buff[i]);
|
||||
PRINT("\n");
|
||||
|
||||
memset(w_buff, 0x22, sizeof(w_buff));
|
||||
SPI_DMA_FlashWrite(spi_idx, 0x5000 + 196, w_buff, 115);
|
||||
DEBUG("Flash Write Any finish2\n");
|
||||
|
||||
SPI_DMA_FlashRead(spi_idx, 0x5000, r_buff, sizeof(r_buff));
|
||||
DEBUG("Flash Read Any addr2: \n");
|
||||
for (uint16_t i = 0; i < sizeof(r_buff); i++)
|
||||
PRINT("%02x ", r_buff[i]);
|
||||
PRINT("\n");
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*!
|
||||
* \file spi_flash.h
|
||||
*
|
||||
* \brief The head file of spi_flash.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __SPI_FLASH_H__
|
||||
#define __SPI_FLASH_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void spi_flash_demo(void);
|
||||
void spi_dma_flash_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __SPI_FLASH_H__ */
|
||||
@@ -0,0 +1,208 @@
|
||||
/*!
|
||||
* \file spi_master.c
|
||||
*
|
||||
* \brief Target spi master device 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 "spi_master.h"
|
||||
#include "xc6xxx_hal_spi.h"
|
||||
#include "xc6xxx_hal_spi_dmac.h"
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
#define MS_BUFFER_LEN 64U
|
||||
#define MS_NODMA_BUFFER_LEN 16U
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
volatile uint8_t m_wr_buff[MS_BUFFER_LEN] = {0};
|
||||
volatile uint8_t m_rd_buff[MS_BUFFER_LEN] = {0};
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
****************************************************************************************
|
||||
* @brief
|
||||
*
|
||||
* @param[in]
|
||||
* @param[in]
|
||||
|
||||
* @return[out]
|
||||
*
|
||||
****************************************************************************************
|
||||
*/
|
||||
void spi_dma_master_demo(void) {
|
||||
DEBUG("__SPI_DMA_MASTER_DEMO__\n");
|
||||
|
||||
GPIO_InitTypeDef gpio_cfg = {0};
|
||||
|
||||
gpio_cfg.Mux = GPIO_Mux0;
|
||||
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
|
||||
gpio_cfg.FunSel = SSI1_CLK;
|
||||
gpio_cfg.Int = NOT_INT;
|
||||
gpio_cfg.Pull = GPIO_PULLDOWN;
|
||||
gpio_cfg.Pin = GPIO_5; // GPIO_24; //GPIO_14;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_SSN;
|
||||
gpio_cfg.Pin = GPIO_15; // GPIO_15;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_TX;
|
||||
gpio_cfg.Pin = GPIO_16;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_RX;
|
||||
gpio_cfg.Pin = GPIO_17;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
SPI_TypeDef *spi_idx = XC_SPI1; // XC_SPI0;//XC_SPI1;
|
||||
SPI_InitCfg_t spi_cfg = {0};
|
||||
|
||||
spi_cfg.Mode = SPI_MODE_MASTER;
|
||||
spi_cfg.DataSize = SSI_CTRL0_DFS_LEN_8BIT;
|
||||
spi_cfg.Direction = SSI_CTRL0_TMOD_WR;
|
||||
spi_cfg.BaudRatePrescaler =
|
||||
SPI_BAUDRATEPRESCALER_4; // SPI_BAUDRATEPRESCALER_2;
|
||||
spi_cfg.CLKPolarity = SSI_CTRL0_SCPOL_LOW;
|
||||
spi_cfg.CLKPhase = SSI_CTRL0_SCPHA_TRAIL;
|
||||
spi_cfg.FirstBit = SPI_FirstBit_MSB;
|
||||
|
||||
xc_spi_init(spi_idx, &spi_cfg);
|
||||
NVIC_EnableIRQ(DMA_IRQn);
|
||||
|
||||
memset((uint8_t *)m_wr_buff, 0x55, sizeof(m_wr_buff));
|
||||
DEBUG("Write buffer: \n");
|
||||
for (uint16_t i = 0; i < sizeof(m_wr_buff); i++) {
|
||||
PRINT("%02x ", m_wr_buff[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
SPI_DMA_WriteAndReadData(spi_idx, (uint8_t *)m_wr_buff, sizeof(m_wr_buff),
|
||||
(uint8_t *)m_rd_buff, sizeof(m_rd_buff));
|
||||
DEBUG("__SPI_READ_END__\n");
|
||||
for (uint16_t i = 0; i < sizeof(m_rd_buff); i++) {
|
||||
PRINT("%02x ", m_rd_buff[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
memset((uint8_t *)m_wr_buff, 0xaa, sizeof(m_wr_buff));
|
||||
SPI_DMA_WriteAndReadData(spi_idx, (uint8_t *)m_wr_buff, sizeof(m_wr_buff),
|
||||
(uint8_t *)m_rd_buff, sizeof(m_rd_buff));
|
||||
DEBUG("__SPI_READ_END2__\n");
|
||||
for (uint16_t i = 0; i < sizeof(m_rd_buff); i++) {
|
||||
PRINT("%02x ", m_rd_buff[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief spi_master_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void spi_master_demo(void) {
|
||||
DEBUG("__SPI_MASTER_DEMO__\r");
|
||||
|
||||
SPI_InitCfg_t spi_cfg = {0};
|
||||
GPIO_InitTypeDef gpio_cfg = {0};
|
||||
|
||||
gpio_cfg.Mux = GPIO_Mux0;
|
||||
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
|
||||
gpio_cfg.FunSel = SSI1_CLK;
|
||||
gpio_cfg.Int = NOT_INT;
|
||||
gpio_cfg.Pull = GPIO_PULLDOWN;
|
||||
gpio_cfg.Pin = GPIO_5;
|
||||
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_SSN;
|
||||
gpio_cfg.Pin = GPIO_15;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_TX;
|
||||
gpio_cfg.Pin = GPIO_16;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_RX;
|
||||
gpio_cfg.Pin = GPIO_17;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
spi_cfg.Mode = SPI_MODE_MASTER;
|
||||
spi_cfg.DataSize = SSI_CTRL0_DFS_LEN_8BIT;
|
||||
spi_cfg.Direction = SSI_CTRL0_TMOD_WR;
|
||||
spi_cfg.BaudRatePrescaler = SSI_BAUD_DIV_8;
|
||||
spi_cfg.CLKPolarity = SSI_CTRL0_SCPOL_LOW;
|
||||
spi_cfg.CLKPhase = SSI_CTRL0_SCPHA_TRAIL;
|
||||
spi_cfg.FirstBit = SPI_FirstBit_MSB;
|
||||
|
||||
SPI_TypeDef *spi_handle = XC_SPI1;
|
||||
|
||||
xc_spi_init(spi_handle, &spi_cfg);
|
||||
|
||||
uint8_t txbuff[MS_NODMA_BUFFER_LEN];
|
||||
uint8_t rxbuff[MS_NODMA_BUFFER_LEN];
|
||||
|
||||
SPI_RXFTL_Set(XC_SPI1, 0x07);
|
||||
SPI_IE_Enable(XC_SPI1, SSI_IE_RXFIE_ENABLE);
|
||||
|
||||
DEBUG("Write buffer: \n");
|
||||
memset((uint8_t *)txbuff, 0xaa, sizeof(txbuff));
|
||||
for (int i = 0; i < sizeof(txbuff); i++) {
|
||||
PRINT("%02x ", txbuff[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
spi_write_and_read_data(spi_handle, (uint8_t *)txbuff, sizeof(txbuff),
|
||||
(uint8_t *)rxbuff, sizeof(rxbuff));
|
||||
|
||||
DEBUG("Read buffer:\n");
|
||||
for (int i = 0; i < sizeof(rxbuff); i++) {
|
||||
PRINT("%02x ", rxbuff[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
DEBUG("__SPI_END__\n");
|
||||
|
||||
DEBUG("Write buffer: \n");
|
||||
memset((uint8_t *)txbuff, 0x55, sizeof(txbuff));
|
||||
for (int i = 0; i < sizeof(txbuff); i++) {
|
||||
PRINT("%02x ", txbuff[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
spi_write_and_read_data(spi_handle, (uint8_t *)txbuff, sizeof(txbuff),
|
||||
(uint8_t *)rxbuff, sizeof(rxbuff));
|
||||
|
||||
DEBUG("Read buffer:\n");
|
||||
for (int i = 0; i < sizeof(rxbuff); i++) {
|
||||
PRINT("%02x ", rxbuff[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
DEBUG("__SPI_END2__\n");
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*!
|
||||
* \file spi_master.h
|
||||
*
|
||||
* \brief The head file of spi_master.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __SPI_MASTER_H__
|
||||
#define __SPI_MASTER_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void spi_dma_master_demo(void);
|
||||
void spi_master_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __SPI_MASTER_H__ */
|
||||
@@ -0,0 +1,202 @@
|
||||
/*!
|
||||
* \file spi_slave.c
|
||||
*
|
||||
* \brief Target spi slave device 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 "spi_slave.h"
|
||||
#include "xc6xxx_hal_spi.h"
|
||||
#include "xc6xxx_hal_spi_dmac.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
#define SL_BUFFER_LEN 64U
|
||||
#define SL_NODMA_BUFFER_LEN 16U
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
volatile uint8_t s_rd_buff[SL_BUFFER_LEN] = {0};
|
||||
volatile uint8_t s_wr_buff[SL_BUFFER_LEN] = {0};
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief spi_dma_slave_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void spi_dma_slave_demo(void) {
|
||||
DEBUG("__SPI_DMA_SLAVE_DEMO__\n");
|
||||
|
||||
GPIO_InitTypeDef gpio_cfg = {0};
|
||||
|
||||
gpio_cfg.Mux = GPIO_Mux0;
|
||||
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
|
||||
gpio_cfg.FunSel = SSI1_CLK;
|
||||
gpio_cfg.Int = NOT_INT;
|
||||
gpio_cfg.Pull = GPIO_PULLDOWN;
|
||||
gpio_cfg.Pin = GPIO_5;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_SSN;
|
||||
gpio_cfg.Pin = GPIO_15; // GPIO_15;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_TX;
|
||||
gpio_cfg.Pin = GPIO_16;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_RX;
|
||||
gpio_cfg.Pin = GPIO_17;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
SPI_TypeDef *spi_idx = XC_SPI1; // XC_SPI0;//XC_SPI1;
|
||||
SPI_InitCfg_t spi_cfg = {0};
|
||||
|
||||
spi_cfg.Mode = SPI_MODE_SLAVE;
|
||||
spi_cfg.DataSize = SSI_CTRL0_DFS_LEN_8BIT;
|
||||
spi_cfg.Direction = SSI_CTRL0_TMOD_WR;
|
||||
spi_cfg.BaudRatePrescaler =
|
||||
SPI_BAUDRATEPRESCALER_2; // SPI_BAUDRATEPRESCALER_2;
|
||||
spi_cfg.CLKPolarity = SSI_CTRL0_SCPOL_LOW;
|
||||
spi_cfg.CLKPhase = SSI_CTRL0_SCPHA_TRAIL;
|
||||
spi_cfg.FirstBit = SPI_FirstBit_MSB;
|
||||
|
||||
xc_spi_init(spi_idx, &spi_cfg);
|
||||
NVIC_EnableIRQ(DMA_IRQn);
|
||||
|
||||
DEBUG("Write buffer: \n");
|
||||
for (uint16_t i = 0; i < sizeof(s_wr_buff); i++) {
|
||||
s_wr_buff[i] = i;
|
||||
PRINT("%02x ", s_wr_buff[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
SPI_DMA_ReadAndWriteData(spi_idx, (uint8_t *)s_wr_buff, sizeof(s_wr_buff),
|
||||
(uint8_t *)s_rd_buff, sizeof(s_rd_buff));
|
||||
DEBUG("__SPI_READ_END__\n");
|
||||
for (uint16_t i = 0; i < sizeof(s_rd_buff); i++) {
|
||||
PRINT("%02x ", s_rd_buff[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
s_wr_buff[0] = 0xff;
|
||||
SPI_DMA_ReadAndWriteData(spi_idx, (uint8_t *)s_wr_buff, sizeof(s_wr_buff),
|
||||
(uint8_t *)s_rd_buff, sizeof(s_rd_buff));
|
||||
DEBUG("__SPI_READ_END2__\n");
|
||||
for (uint16_t i = 0; i < sizeof(s_rd_buff); i++) {
|
||||
PRINT("%02x ", s_rd_buff[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief spi_slave_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void spi_slave_demo(void) {
|
||||
DEBUG("__SPI_SLAVE_DEMO__\r");
|
||||
|
||||
SPI_InitCfg_t spi_cfg = {0};
|
||||
GPIO_InitTypeDef gpio_cfg = {0};
|
||||
|
||||
gpio_cfg.Mux = GPIO_Mux0;
|
||||
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
|
||||
gpio_cfg.FunSel = SSI1_CLK;
|
||||
gpio_cfg.Int = NOT_INT;
|
||||
gpio_cfg.Pull = GPIO_PULLDOWN;
|
||||
gpio_cfg.Pin = GPIO_5;
|
||||
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_SSN;
|
||||
gpio_cfg.Pin = GPIO_15;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_TX;
|
||||
gpio_cfg.Pin = GPIO_16;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_RX;
|
||||
gpio_cfg.Pin = GPIO_17;
|
||||
GPIO_Init(&gpio_cfg);
|
||||
|
||||
spi_cfg.Mode = SPI_MODE_SLAVE;
|
||||
spi_cfg.DataSize = SSI_CTRL0_DFS_LEN_8BIT;
|
||||
spi_cfg.Direction = SSI_CTRL0_TMOD_WR;
|
||||
spi_cfg.BaudRatePrescaler = SSI_BAUD_DIV_8;
|
||||
spi_cfg.CLKPolarity = SSI_CTRL0_SCPOL_LOW;
|
||||
spi_cfg.CLKPhase = SSI_CTRL0_SCPHA_TRAIL;
|
||||
spi_cfg.FirstBit = SPI_FirstBit_MSB;
|
||||
|
||||
SPI_TypeDef *spi_handle = XC_SPI1;
|
||||
|
||||
xc_spi_init(spi_handle, &spi_cfg);
|
||||
|
||||
uint8_t txbuff[SL_NODMA_BUFFER_LEN];
|
||||
uint8_t rxbuff[SL_NODMA_BUFFER_LEN];
|
||||
|
||||
SPI_RXFTL_Set(XC_SPI1, 0x07);
|
||||
SPI_IE_Enable(XC_SPI1, SSI_IE_RXFIE_ENABLE);
|
||||
// while(1)
|
||||
//{
|
||||
DEBUG("Write buffer: \n");
|
||||
for (int i = 0; i < sizeof(txbuff); i++) {
|
||||
txbuff[i] = i + 1;
|
||||
PRINT("%02x ", txbuff[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
spi_write_and_read_data(spi_handle, (uint8_t *)txbuff, sizeof(txbuff),
|
||||
(uint8_t *)rxbuff, sizeof(rxbuff));
|
||||
|
||||
DEBUG("Read buffer: \n");
|
||||
for (int i = 0; i < sizeof(rxbuff); i++) {
|
||||
PRINT("%02x ", rxbuff[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
DEBUG("__SPI_END__\n");
|
||||
|
||||
DEBUG("Write buffer: \n");
|
||||
memset((uint8_t *)txbuff, 0xbb, sizeof(txbuff));
|
||||
for (int i = 0; i < sizeof(txbuff); i++) {
|
||||
PRINT("%02x ", txbuff[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
spi_write_and_read_data(spi_handle, (uint8_t *)txbuff, sizeof(txbuff),
|
||||
(uint8_t *)rxbuff, sizeof(rxbuff));
|
||||
|
||||
DEBUG("Read buffer: \n");
|
||||
for (int i = 0; i < sizeof(rxbuff); i++) {
|
||||
PRINT("%02x ", rxbuff[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
DEBUG("__SPI_END2__\n");
|
||||
//}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*!
|
||||
* \file spi_slave.h
|
||||
*
|
||||
* \brief The head file of spi_slave.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __SPI_SLAVE_H__
|
||||
#define __SPI_SLAVE_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void spi_dma_slave_demo(void);
|
||||
void spi_slave_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __SPI_SLAVE_H__ */
|
||||
@@ -0,0 +1,181 @@
|
||||
/*!
|
||||
* \file timer.c
|
||||
*
|
||||
* \brief Target timer 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 "timer.h"
|
||||
#include "xc6xxx_hal_timer.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
TimerStatus_t TimerStat = {
|
||||
.T0_Flag = false,
|
||||
.T1_Flag = false,
|
||||
.T2_Flag = false,
|
||||
.T3_Flag = false,
|
||||
.AO_T0_Flag = false,
|
||||
.AO_T1_Flag = false,
|
||||
|
||||
};
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief timer_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void timer_demo(void) {
|
||||
DEBUG("__TIMER_DEMO__\r");
|
||||
|
||||
uint32_t us = 1000 * 1000;
|
||||
TIMER_BaseInitTypeDef Timer_Init;
|
||||
|
||||
Timer_Init.src_clk = TIMER_CLK_SRC_32M_DIV;
|
||||
Timer_Init.mode = TIMER_MODE_CYCLE_COUNT;
|
||||
Timer_Init.div_clk = TIMER_DIV_CLK_16MHzOr16K;
|
||||
// Timer0 Config & Start
|
||||
TIMER_Base_Init(XC_TIMER0, &Timer_Init);
|
||||
TIMER_SetUs(XC_TIMER0, us);
|
||||
TIMER_Start_IT(XC_TIMER0);
|
||||
// Timer1 Config & Start
|
||||
TIMER_Base_Init(XC_TIMER1, &Timer_Init);
|
||||
TIMER_SetUs(XC_TIMER1, us + 1000);
|
||||
TIMER_Start_IT(XC_TIMER1);
|
||||
// Timer2 Config & Start
|
||||
TIMER_Base_Init(XC_TIMER2, &Timer_Init);
|
||||
TIMER_SetUs(XC_TIMER2, us + 2000);
|
||||
TIMER_Start_IT(XC_TIMER2);
|
||||
// Timer3 Config & Start
|
||||
TIMER_Base_Init(XC_TIMER3, &Timer_Init);
|
||||
TIMER_SetUs(XC_TIMER3, us + 3000);
|
||||
TIMER_Start_IT(XC_TIMER3);
|
||||
|
||||
// AOTimer0 Config & Start
|
||||
Timer_Init.src_clk = TIMER_CLK_SRC_32K;
|
||||
TIMER_Base_Init(XC_AOTIMER0, &Timer_Init);
|
||||
TIMER_SetUs(XC_AOTIMER0, us + 4000);
|
||||
TIMER_Start_IT(XC_AOTIMER0);
|
||||
|
||||
// AOTimer1 Config & Start
|
||||
Timer_Init.src_clk = TIMER_CLK_SRC_32K;
|
||||
TIMER_Base_Init(XC_AOTIMER1, &Timer_Init);
|
||||
TIMER_SetUs(XC_AOTIMER1, us + 5000);
|
||||
TIMER_Start_IT(XC_AOTIMER1);
|
||||
|
||||
while (1) {
|
||||
if (TimerStat.T0_Flag == true) {
|
||||
DEBUG("__TIMER0__\n");
|
||||
TimerStat.T0_Flag = false;
|
||||
}
|
||||
|
||||
if (TimerStat.T1_Flag == true) {
|
||||
DEBUG("__TIMER1__\n");
|
||||
TimerStat.T1_Flag = false;
|
||||
}
|
||||
|
||||
if (TimerStat.T2_Flag == true) {
|
||||
DEBUG("__TIMER2__\n");
|
||||
TimerStat.T2_Flag = false;
|
||||
}
|
||||
|
||||
if (TimerStat.T3_Flag == true) {
|
||||
DEBUG("__TIMER3__\n");
|
||||
TimerStat.T3_Flag = false;
|
||||
}
|
||||
if (TimerStat.AO_T0_Flag == true) {
|
||||
DEBUG("__AOTIMER0__\n");
|
||||
TimerStat.AO_T0_Flag = false;
|
||||
}
|
||||
|
||||
if (TimerStat.AO_T1_Flag == true) {
|
||||
DEBUG("__AOTIMER1__\n");
|
||||
TimerStat.AO_T1_Flag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Timer0_Callback
|
||||
* @details Timer0 handler callback function
|
||||
* @param void *context
|
||||
* @retval void
|
||||
*/
|
||||
__RAM_CODE void Timer0_Callback(void *context) { TimerStat.T0_Flag = true; }
|
||||
|
||||
/**
|
||||
* @brief Timer1_Callback
|
||||
* @details Timer1 handler callback function
|
||||
* @param void *context
|
||||
* @retval void
|
||||
*/
|
||||
__RAM_CODE void Timer1_Callback(void *context) { TimerStat.T1_Flag = true; }
|
||||
|
||||
/**
|
||||
* @brief Timer2_Callback
|
||||
* @details Timer2 handler callback function
|
||||
* @param void *context
|
||||
* @retval void
|
||||
*/
|
||||
__RAM_CODE void Timer2_Callback(void *context) { TimerStat.T2_Flag = true; }
|
||||
|
||||
/**
|
||||
* @brief Timer3_Callback
|
||||
* @details Timer3 handler callback function
|
||||
* @param void *context
|
||||
* @retval void
|
||||
*/
|
||||
__RAM_CODE void Timer3_Callback(void *context) { TimerStat.T3_Flag = true; }
|
||||
|
||||
/**
|
||||
* @brief AOTimer0_Callback
|
||||
* @details AOTimer0 handler callback function
|
||||
* @param void *context
|
||||
* @retval void
|
||||
*/
|
||||
__RAM_CODE void AOTimer0_Callback(void *context) {
|
||||
TimerStat.AO_T0_Flag = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief AOTimer1_Callback
|
||||
* @details AOTimer1 handler callback function
|
||||
* @param void *context
|
||||
* @retval void
|
||||
*/
|
||||
__RAM_CODE void AOTimer1_Callback(void *context) {
|
||||
TimerStat.AO_T1_Flag = true;
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*!
|
||||
* \file timer.h
|
||||
*
|
||||
* \brief The head file of timer.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __TIMER_H__
|
||||
#define __TIMER_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
typedef struct TimerStatus_s {
|
||||
uint8_t T0_Flag;
|
||||
uint8_t T1_Flag;
|
||||
uint8_t T2_Flag;
|
||||
uint8_t T3_Flag;
|
||||
uint8_t AO_T0_Flag;
|
||||
uint8_t AO_T1_Flag;
|
||||
|
||||
} TimerStatus_t;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void timer_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TIMER_H__ */
|
||||
@@ -0,0 +1,133 @@
|
||||
/*!
|
||||
* \file uart.c
|
||||
*
|
||||
* \brief Target uart 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 "uart.h"
|
||||
#include "xc6xxx_hal_uart.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief uart_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void uart_demo(void)
|
||||
{
|
||||
DEBUG("__UART_DEMO__\n");
|
||||
|
||||
uint8_t rxbuff[26];
|
||||
UART_TypeDef *uart_handle = XC_UART2; // XC_UART0;//XC_UART1;//XC_UART2;
|
||||
|
||||
if (uart_handle == XC_UART1) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
GPIO_InitStruct.Mux = GPIO_Mux0;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
GPIO_InitStruct.Int = NOT_INT;
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_3;
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_OUTPUT;
|
||||
GPIO_InitStruct.FunSel = UART1_TX;
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_2;
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitStruct.FunSel = UART1_RX;
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
UART_InitTypeDef UART_InitStruct = {0};
|
||||
|
||||
UART_InitStruct.Parity = UART_PARITY_DISABLE;
|
||||
UART_InitStruct.StopBits = UART_STOP_1_BITS;
|
||||
UART_InitStruct.WordLength = UART_DATA_8_BITS;
|
||||
UART_InitStruct.BaudRate = UART_BAUDRATE_115200;
|
||||
UART_InitStruct.HardwareFlowControl = UART_HWFC_DISABLE;
|
||||
|
||||
UART_Init(uart_handle, &UART_InitStruct);
|
||||
} else if (uart_handle == XC_UART2) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
GPIO_InitStruct.Mux = GPIO_Mux1;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
GPIO_InitStruct.Int = NOT_INT;
|
||||
GPIO_InitStruct.FunSel = GPIO_Dx;
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_32; // uart2 tx
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_OUTPUT;
|
||||
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_31; // uart2 rx
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
UART_InitTypeDef UART_InitStruct = {0};
|
||||
|
||||
UART_InitStruct.Parity = UART_PARITY_DISABLE;
|
||||
UART_InitStruct.StopBits = UART_STOP_1_BITS;
|
||||
UART_InitStruct.WordLength = UART_DATA_8_BITS;
|
||||
UART_InitStruct.BaudRate = UART_BAUDRATE_115200;
|
||||
UART_InitStruct.HardwareFlowControl = UART_HWFC_DISABLE;
|
||||
|
||||
UART_Init(uart_handle, &UART_InitStruct);
|
||||
}
|
||||
|
||||
UART_Enable_RxIT(uart_handle);
|
||||
|
||||
if (uart_handle == XC_UART0) {
|
||||
NVIC_EnableIRQ(UART0_IRQn);
|
||||
} else if (uart_handle == XC_UART1) {
|
||||
NVIC_EnableIRQ(UART1_IRQn);
|
||||
} else if (uart_handle == XC_UART2) {
|
||||
NVIC_EnableIRQ(UART2_IRQn);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 26; i++) {
|
||||
rxbuff[i] = i + 'A';
|
||||
}
|
||||
UART_SendData(uart_handle, rxbuff, 26);
|
||||
|
||||
uint16_t data_len = 0;
|
||||
|
||||
while (1) {
|
||||
data_len = UART_ReceiveData(uart_handle, rxbuff);
|
||||
if (data_len) {
|
||||
UART_SendData(uart_handle, rxbuff, data_len);
|
||||
data_len = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* \file uart.h
|
||||
*
|
||||
* \brief The head file of uart.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __UART_H__
|
||||
#define __UART_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void uart_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __UART_H__ */
|
||||
@@ -0,0 +1,103 @@
|
||||
/*!
|
||||
* \file uart_dma_test.c
|
||||
*
|
||||
* \brief Target uart test 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 "uart_dma.h"
|
||||
#include "xc60xx_uart.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief uart_dma_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void uart_dma_demo(void) {
|
||||
DEBUG("__UART_DMA_DEMO__\n");
|
||||
|
||||
uint8_t __attribute__((aligned(4))) rxbuff[26];
|
||||
UART_TypeDef *uart_handle = XC_UART0;
|
||||
|
||||
UART_InitTypeDef UART_InitStruct = {0};
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
GPIO_InitStruct.Mux = GPIO_Mux0;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
GPIO_InitStruct.Int = NOT_INT;
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_3;
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_OUTPUT;
|
||||
GPIO_InitStruct.FunSel = UART0_TX;
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_2;
|
||||
GPIO_InitStruct.Dir = GPIO_DIR_INPUT;
|
||||
GPIO_InitStruct.FunSel = UART0_RX;
|
||||
GPIO_Init(&GPIO_InitStruct);
|
||||
|
||||
UART_InitStruct.Parity = UART_PARITY_DISABLE;
|
||||
UART_InitStruct.StopBits = UART_STOP_1_BITS;
|
||||
UART_InitStruct.WordLength = UART_UARTx_TCR_CLS_8BITS;
|
||||
UART_InitStruct.BaudRate = UART_BAUDRATE_115200;
|
||||
UART_InitStruct.HardwareFlowControl = UART_UARTx_MCR_AFCE_DISABLE;
|
||||
|
||||
UART_Init(uart_handle, &UART_InitStruct);
|
||||
|
||||
UART_DMA_IT_Register_TxCallback(uart_handle);
|
||||
UART_DMA_IT_Register_RxCallback(uart_handle);
|
||||
|
||||
if (uart_handle == XC_UART0) {
|
||||
UART_DMA_IT_Enable(XC_UART0,
|
||||
1 << (DMA_CH_RCV_UART0 + 16) | 1 << DMA_CH_SEND_UART0);
|
||||
} else if (uart_handle == XC_UART1) {
|
||||
UART_DMA_IT_Enable(XC_UART1,
|
||||
1 << (DMA_CH_RCV_UART1 + 16) | 1 << DMA_CH_SEND_UART1);
|
||||
}
|
||||
|
||||
NVIC_EnableIRQ(DMAS_IRQn);
|
||||
|
||||
UARTx_DMA_Receive_Config(uart_handle);
|
||||
|
||||
for (int i = 0; i < 26; i++) {
|
||||
uart_txdmabuff[i] = i + 'A';
|
||||
}
|
||||
|
||||
while (1) {
|
||||
if (UART_DMA_ReceiveData(uart_handle, rxbuff)) {
|
||||
UART_DMA_SendData(uart_handle, rxbuff, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* \file xc60xx_uart.h
|
||||
*
|
||||
* \brief The head file of xc60xx_uart.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __UART_DMA_H__
|
||||
#define __UART_DMA_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void uart_dma_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __UART_DMA_H__ */
|
||||
@@ -0,0 +1,60 @@
|
||||
/*!
|
||||
* \file wdt.c
|
||||
*
|
||||
* \brief Target wdt(watch dog) 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 "wdt.h"
|
||||
#include "xc6xxx_hal_wdt.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief wdt_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void wdt_demo() {
|
||||
DEBUG("__WDT_TEST_DEMO__\r");
|
||||
|
||||
WDT_InitTypeDef WDT_InitStruct;
|
||||
|
||||
NVIC_EnableIRQ(WDT_IRQn);
|
||||
|
||||
WDT_InitStruct.WorkMode = WDT_WORK_MODE1;
|
||||
WDT_InitStruct.ReloadValue = WDT_ReloadValue_0x7FFFFF;
|
||||
|
||||
WDT_Init(XC_WDT, &WDT_InitStruct);
|
||||
WDT_Start(XC_WDT);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*!
|
||||
* \file wdt.h
|
||||
*
|
||||
* \brief The head file of wdt.c
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __WDT_H__
|
||||
#define __WDT_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
TypeDef
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void wdt_demo(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __WDT_H__ */
|
||||
Reference in New Issue
Block a user