This commit is contained in:
moyuhai
2026-06-11 13:04:09 +08:00
commit 966451d245
2022 changed files with 513671 additions and 0 deletions
@@ -0,0 +1,232 @@
/*!
* \file system_it_xinc.c
*
* \brief Target system interruption 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 "system_it_xinc.h"
/*------------------------------------------------------------------------------------
Func Prototype
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------------------------------------------------*/
/**
****************************************************************************************
* @brief This function handles NMI exception.
*
* @param[in]
* @param[in]
****************************************************************************************
*/
void NMI_Handler(void)
{
/* USER CODE BEGIN NonMaskableInt_IRQn */
DEBUG("%s\n", __func__);
/* USER CODE END NonMaskableInt_IRQn */
while (1)
{
}
}
/**
****************************************************************************************
* @brief This function handles Hard Fault exception.
*
* @param[in]
* @param[in]
****************************************************************************************
*/
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn */
DEBUG("%s\n", __func__);
/* USER CODE END HardFault_IRQn */
while (1)
{
}
}
/**
****************************************************************************************
* @brief This function handles Memory Manage exception.
*
* @param[in]
* @param[in]
****************************************************************************************
*/
void MemManage_Handler(void)
{
/* USER CODE BEGIN */
DEBUG("%s\n", __func__);
/* USER CODE END */
while (1)
{
}
}
/**
****************************************************************************************
* @brief This function handles Bus Fault exception.
*
* @param[in]
* @param[in]
****************************************************************************************
*/
void BusFault_Handler(void)
{
/* USER CODE BEGIN */
DEBUG("%s\n", __func__);
/* USER CODE END */
while (1)
{
}
}
/**
****************************************************************************************
* @brief This function handles Usage Fault exception.
*
* @param[in]
* @param[in]
****************************************************************************************
*/
void UsageFault_Handler(void)
{
/* USER CODE BEGIN */
DEBUG("%s\n", __func__);
/* USER CODE END */
while (1)
{
}
}
/**
****************************************************************************************
* @brief This function handles SVCall exception.
*
* @param[in]
* @param[in]
****************************************************************************************
*/
void SVC_Handler(void)
{
/* USER CODE BEGIN SVCall_IRQn */
DEBUG("%s\n", __func__);
/* USER CODE END SVCall_IRQn */
while (1)
{
}
}
/**
****************************************************************************************
* @brief This function handles Debug Monitor exception.
*
* @param[in]
* @param[in]
****************************************************************************************
*/
void DebugMon_Handler(void)
{
/* USER CODE BEGIN */
DEBUG("%s\n", __func__);
/* USER CODE END */
while (1)
{
}
}
/**
****************************************************************************************
* @brief This function handles PendSVC exception.
*
* @param[in]
* @param[in]
****************************************************************************************
*/
void PendSV_Handler(void)
{
/* USER CODE BEGIN PendSV_IRQn */
DEBUG("%s\n", __func__);
/* USER CODE END PendSV_IRQn */
while (1)
{
}
}
/****************************** Hard Fault Handler Functions *******************************/
/*------------------------------------------------------------------------------------
Private Functions
-------------------------------------------------------------------------------------*/
void HardFault_Handler_c(unsigned int * HardFault_args)
{
/*栈帧里面内容:*/
unsigned int stack_r0; //压栈的 R0
unsigned int stack_r1; //压栈的 R1
unsigned int stack_r2; //压栈的 R2
unsigned int stack_r3; //压栈的 R3
unsigned int stack_r12; //压栈的 R12
unsigned int stack_lr; //压栈的 lr
unsigned int stack_pc; //压栈的 pc
unsigned int stack_psr; //压栈的 psr
stack_r0 = ((unsigned int)HardFault_args[0]);
stack_r1 = ((unsigned int)HardFault_args[1]);
stack_r2 = ((unsigned int)HardFault_args[2]);
stack_r3 = ((unsigned int)HardFault_args[3]);
stack_r12 = ((unsigned int)HardFault_args[4]);
stack_lr = ((unsigned int)HardFault_args[5]);
stack_pc = ((unsigned int)HardFault_args[6]);
stack_psr = ((unsigned int)HardFault_args[7]);
DEBUG("----%s----\n", __func__);
DEBUG("R0=%x\n",stack_r0);
DEBUG("R1=%x\n",stack_r1);
DEBUG("R2=%x\n",stack_r2);
DEBUG("R3=%x\n",stack_r3);
DEBUG("R12=%x\n",stack_r12);
DEBUG("LR[R14]=%x\n",stack_lr);
DEBUG("PC[R15]=%x\n",stack_pc);
DEBUG("PSR=%x\n",stack_psr);
DEBUG("SCB_SHCSR=%x\n",SCB->SHCSR);
DEBUG("---------------------------\n");
while(1);
}
/*******************************************************************************************/
@@ -0,0 +1,58 @@
/*!
* \file system_it_xinc.h
*
* \brief The header of system_it_xinc.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 __SYSTEM_IT_XINC_H__
#define __SYSTEM_IT_XINC_H__
#ifdef __cplusplus
extern "C" {
#endif
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include "xc6xxx.h"
/*------------------------------------------------------------------------------------
Exported Functions
-------------------------------------------------------------------------------------*/
void NMI_Handler( void );
void HardFault_Handler( void );
void MemManage_Handler( void );
void BusFault_Handler( void );
void UsageFault_Handler( void );
void SVC_Handler( void );
void DebugMon_Handler( void );
void PendSV_Handler( void );
void HardFault_Handler_c(unsigned int * HardFault_args);
#ifdef __cplusplus
}
#endif
#endif /* __SYSTEM_IT_XINC_H__ */
@@ -0,0 +1,117 @@
/**
* Initialize the system
*
* @param none
* @return none
*
* @brief Setup the microcontroller system
*
*/
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include <stdio.h>
#include "xc6xxx.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
#define __DEBUG_OUT_PORT 0
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------------------------------------------------*/
#define VECTOR_NUM 48
void set_vector(void)
{
#if (USE_XIP == 1)
__disable_irq();
for (uint32_t i = 0, *Pvector = (uint32_t *)(0x11001000 + 0),
*_vector_table = (uint32_t *)(0x10000000);
i < VECTOR_NUM; i++) // copy vertor table
{
_vector_table[i] = *Pvector++;
}
*((volatile unsigned int *)(0x4000013C)) =
0x10000001; // inter vertor table remap
__enable_irq();
#endif
}
static void WDT_ResetInit(void)
{
cpr_ctlapbclken_grctl__wdt_pclk_en__setf(ENABLE);
cpr_rstctl_ctlapb_sw__wdt_rstn__setf(RSTCTL_ENABLE);
cpr_rstctl_ctlapb_sw__wdt_rstn__setf(RSTCTL_DISABLE);
cpr_rstctl_wdtrst_mask_set(
(WDT_SYS_RSTN_MASK_DISABLE | WDT_M0_RSTN_MASK_ENABLE));
cpr_lp_ctl__ctl_wdt_tclk_en__setf(ENABLE);
wdt_cr__wdt_en__setf(DISABLE);
}
void SystemInit(void)
{
WDT_ResetInit();
#if (USE_XIP == 1)
set_vector();
#endif
}
__RAM_CODE int sendchar(int c)
{
unsigned int status;
#if (__DEBUG_OUT_PORT == 1)
for (;;) {
status = (*((volatile unsigned *)(0x40011000 + 0x14)));
status &= 0x20;
if (status == 0x20)
break;
}
(*((volatile unsigned *)(0x40011000 + 0x00))) = c;
return (1);
#else
for (;;) {
status = (*((volatile unsigned *)(0x40010000 + 0x14)));
status &= 0x20;
if (status == 0x20)
break;
}
(*((volatile unsigned *)(0x40010000 + 0x00))) = c;
return (1);
#endif
}
struct __FILE
{
int handle; /* Add whatever you need here */
};
FILE __stdout;
__RAM_CODE int fputc(int ch, FILE *f) { return (sendchar(ch)); }
int ferror(FILE *f)
{
/* Your implementation of ferror */
return EOF;
}
void _ttywrch(int ch) { sendchar(ch); }
void _sys_exit(int return_code)
{
label:
goto label; /* endless loop */
}
@@ -0,0 +1,256 @@
;/*****************************************************************************
; * @file: startup_xinc.s
; * @purpose: CMSIS Cortex-M0 Core Device Startup File for the
; * Device xinc.
; *****************************************************************************/
Stack_Size EQU 0x00001000
AREA STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem SPACE Stack_Size
__initial_sp
Heap_Size EQU 0x00000000
AREA HEAP, NOINIT, READWRITE, ALIGN=3
__heap_base
Heap_Mem SPACE Heap_Size
__heap_limit
PRESERVE8
THUMB
; Vector Table Mapped to Address 0 at Reset
AREA RESET, DATA, READONLY
EXPORT __Vectors
EXPORT __Vectors_End
EXPORT __Vectors_Size
__Vectors DCD __initial_sp ; Top of Stack
DCD Reset_Handler ; Reset Handler
DCD NMI_Handler ; NMI Handler
DCD HardFault_Handler ; Hard Fault Handler
DCD MemManage_Handler ; MPU Fault Handler
DCD BusFault_Handler ; Bus Fault Handler
DCD UsageFault_Handler ; Usage Fault Handler
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD SVC_Handler ; SVCall Handler
DCD DebugMon_Handler ; Debug Monitor Handler
DCD 0 ; Reserved
DCD PendSV_Handler ; PendSV Handler
DCD SysTick_Handler ; SysTick Handler
; External Interrupts
; ToDo: Add here the vectors for the device specific external interrupts handler
DCD BLE_Handler ; 0
DCD DMA_Handler ; 1
DCD CPR_Handler ; 2
DCD GPIO_Handler ; 3
DCD RTC_Handler ; 4
DCD TIMER0_Handler ; 5
DCD TIMER1_Handler ; 6
DCD TIMER2_Handler ; 7
DCD TIMER3_Handler ; 8
DCD WDT_Handler ; 9
DCD I2C_Handler ; 10
DCD UART0_Handler ; 11
DCD UART1_Handler ; 12
DCD SPI0_Handler ; 13
DCD SPI1_Handler ; 14
DCD 0 ; 15
DCD 0 ; 16
DCD GADC_Handler ; 17
DCD PWM_Handler ; 18
DCD AES_Handler ; 19
DCD USB_Handler ; 20
DCD AUDIO_Handler ; 21
DCD RF24G_Handler ; 22
DCD SPI2_Handler ; 23
DCD 0 ; 24
DCD UART2_Handler ; 25
DCD I2S_Handler ; 26
DCD AOTIMER0_Handler ; 27
DCD AOTIMER1_Handler ; 28
DCD CMP_Handler ; 29
DCD FMC_Handler ; 30
DCD CAN_Handler ; 31
__Vectors_End
__Vectors_Size EQU __Vectors_End - __Vectors
AREA |.text|, CODE, READONLY
; Reset Handler
Reset_Handler PROC
EXPORT Reset_Handler [WEAK]
IMPORT SystemInit
IMPORT __main
LDR r0, =0x4000013C ; remap
LDR r1, =0x10000001
STR r1, [r0]
LDR R0, =SystemInit
BLX R0
LDR R0, =__main
BX R0
ENDP
; Dummy Exception Handlers (infinite loops which can be modified)
NMI_Handler PROC
EXPORT NMI_Handler [WEAK]
B .
ENDP
HardFault_Handler\
PROC
IMPORT HardFault_Handler_c ;函数声明
movs r0, #4 ;判断主栈指针还是进程栈指针
mov r1, lr
tst r0, r1
beq hf_used_msp ;如果是主栈指针
mrs r0, psp ;否则是进程栈指针,把进程栈指针地址付给 R0
ldr r1, =HardFault_Handler_c ;跳转到 HardFault 中断程序
bx r1
hf_used_msp
mrs r0, msp ;把主栈指针地址赋给 R0
ldr r1, =HardFault_Handler_c
bx r1
;EXPORT HardFault_Handler [WEAK]
;B .
ENDP
MemManage_Handler\
PROC
EXPORT MemManage_Handler [WEAK]
B .
ENDP
BusFault_Handler\
PROC
EXPORT BusFault_Handler [WEAK]
B .
ENDP
UsageFault_Handler\
PROC
EXPORT UsageFault_Handler [WEAK]
B .
ENDP
SVC_Handler PROC
EXPORT SVC_Handler [WEAK]
B .
ENDP
DebugMon_Handler\
PROC
EXPORT DebugMon_Handler [WEAK]
B .
ENDP
Default_Handler PROC
EXPORT BLE_Handler [WEAK]
EXPORT DMA_Handler [WEAK]
EXPORT CPR_Handler [WEAK]
EXPORT GPIO_Handler [WEAK]
EXPORT RTC_Handler [WEAK]
EXPORT TIMER0_Handler [WEAK]
EXPORT TIMER1_Handler [WEAK]
EXPORT TIMER2_Handler [WEAK]
EXPORT TIMER3_Handler [WEAK]
EXPORT WDT_Handler [WEAK]
EXPORT I2C_Handler [WEAK]
EXPORT UART0_Handler [WEAK]
EXPORT UART1_Handler [WEAK]
EXPORT SPI0_Handler [WEAK]
EXPORT SPI1_Handler [WEAK]
;EXPORT KBS_Handler [WEAK]
;EXPORT QDEC_Handler [WEAK]
EXPORT GADC_Handler [WEAK]
EXPORT PWM_Handler [WEAK]
EXPORT AES_Handler [WEAK]
EXPORT PendSV_Handler [WEAK]
EXPORT SysTick_Handler [WEAK]
EXPORT USB_Handler [WEAK];20
EXPORT AUDIO_Handler [WEAK];21
EXPORT RF24G_Handler [WEAK];22
EXPORT SPI2_Handler [WEAK];23
;EXPORT MPU_Handler [WEAK];24
EXPORT UART2_Handler [WEAK];25
EXPORT I2S_Handler [WEAK];26
EXPORT AOTIMER0_Handler [WEAK];27
EXPORT AOTIMER1_Handler [WEAK];28
EXPORT CMP_Handler [WEAK];29
EXPORT FMC_Handler [WEAK];30
EXPORT CAN_Handler [WEAK];31
PendSV_Handler
SysTick_Handler
BLE_Handler
RF24G_Handler
DMA_Handler
CPR_Handler
GPIO_Handler
RTC_Handler
TIMER0_Handler
TIMER1_Handler
TIMER2_Handler
TIMER3_Handler
WDT_Handler
I2C_Handler
I2S_Handler
UART0_Handler
UART1_Handler
UART2_Handler
SPI0_Handler
SPI1_Handler
SPI2_Handler
;KBS_Handler
;QDEC_Handler
GADC_Handler
PWM_Handler
AUDIO_Handler
;SIM_Handler
AES_Handler
AOTIMER0_Handler
AOTIMER1_Handler
CMP_Handler
FMC_Handler
CAN_Handler
USB_Handler
B .
ENDP
ALIGN
; User Initial Stack & Heap
IMPORT __use_two_region_memory
EXPORT __user_initial_stackheap
__user_initial_stackheap
LDR R0, = Heap_Mem
LDR R1, = (Stack_Mem + Stack_Size)
LDR R2, = (Heap_Mem + Heap_Size)
LDR R3, = Stack_Mem
BX LR
ALIGN
END
@@ -0,0 +1,68 @@
/*!
* \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 <stdio.h>
#include <string.h>
#include <stdint.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,63 @@
/*!
* \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 <stdio.h>
#include <string.h>
#include <stdint.h>
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
TypeDef
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Exported Functions
-------------------------------------------------------------------------------------*/
void clock_init(void);
#ifdef __cplusplus
}
#endif
#endif /* __CLOCK_H__ */
@@ -0,0 +1,84 @@
/*!
* \file key.h
*
* \brief The head file of key.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 __KEY_H__
#define __KEY_H__
#ifdef __cplusplus
extern "C"
{
#endif
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
#define KEY_DOWN_TIMER 10
#define KEY_LONG_TIMER 20
#define KEY_LOH_TIMER 20
#define KEY_UP_TIMER 10
// KEY NUM 0- 255
#define KEY_NUM1 0X0001
#define KEY_NUM2 0X0002
#define KEY_SHORT_DOWN 0X0000
#define KEY_SHORT_UP 0X0100
#define KEY_LONG_DOWN 0X0200
#define KEY_LONG_UP 0X0400
#define NO_KEY 0
#define REC_KEY KEY_NUM1
#define REC_KEY_SHORT_DOWN (REC_KEY | KEY_SHORT_DOWN)
#define REC_KEY_SHORT_UP (REC_KEY | KEY_SHORT_UP)
#define REC_KEY_LONG_DOWN (REC_KEY | KEY_LONG_DOWN)
#define REC_KEY_LONG_UP (REC_KEY | KEY_LONG_UP)
/*------------------------------------------------------------------------------------
TypeDef
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Exported Functions
-------------------------------------------------------------------------------------*/
void app_key_init(void);
void app_key_scan(void);
#ifdef __cplusplus
}
#endif
#endif /* __KEY_H__ */
@@ -0,0 +1,66 @@
/*!
* \file main.h
*
* \brief The head file of main.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 __MAIN_H__
#define __MAIN_H__
#ifdef __cplusplus
extern "C" {
#endif
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "xc6xxx.h"
#include "clock.h"
#include "adc.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
TypeDef
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Exported Functions
-------------------------------------------------------------------------------------*/
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H__ */
@@ -0,0 +1,64 @@
/*!
* \file key_message.h
*
* \brief The head file of key_message.c
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip )
*
* \author ( XinChip )
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __KEY_MESSAGE_H__
#define __KEY_MESSAGE_H__
#ifdef __cplusplus
extern "C"
{
#endif
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
TypeDef
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Exported Functions
-------------------------------------------------------------------------------------*/
void app_key_message_send(uint16_t msg);
uint16_t app_key_message_get(void);
void app_key_message_clr(void);
#ifdef __cplusplus
}
#endif
#endif /* __KEY_MESSAGE_H__ */
@@ -0,0 +1,156 @@
/*!
* \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_AUDIO_ADC_H_
#define __BSP_AUDIO_ADC_H_
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include "stdint.h"
#include "xc60xx.h"
#include "xc_drv_adc.h"
#include "xc_drv_pga.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
#define PGA_MIC_RECORD_DEMO_EN 1
#define PGA_MIC_GET_VAL_DEMO_EN 1
#define DMA_ENABLE 1
#define DMA_MULTIPLE_TRANS_ENABLE 1
#define ADC_PWM_MODE 1
#define MIC_SAMPLING_DEBUG 0 ///
#define USER_KEY_EN 1// KEY
#define MIC_GAIN_1_EN 1 // The first-level gain is enabled 20DB
#define MIC_GAIN_2_LEVEN 0X08 // Second-stage gain 1.6DB * N( 0 - 0X0E )
#define ADC_CLK_DIV_0 (0x00UL)
#define ADC_CLK_DIV_2 (0x01UL)
#define ADC_CLK_DIV_4 (0x03UL)
#define ADC_CLK_DIV_8 (0x07UL)
#define ADC_CLK_DIV_16 (0x0fUL)
#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_CLOCK_FREQ_2M // ADC CLOCK select (ADC_CLOCK_FREQ_1M - ADC_CLOCK_FREQ_4M )
#define MIC_SAMPLING_8K 0
#define MIC_SAMPLING_16K 1
#define SAMPLING_TYPE MIC_SAMPLING_16K
#define MIC_SAMPLING_MAX_LEN 480//Recording cache buff length
#define MIC_SAMPLING_BUFF_MAX 2//
#define PCM_DATA_DEBUG_EN 1 // uart DEBUG pcm data
/*------------------------------------------------------------------------------------
TypeDef
-------------------------------------------------------------------------------------*/
#if PGA_MIC_RECORD_DEMO_EN
typedef struct
{
int16_t data_buff[MIC_SAMPLING_BUFF_MAX][MIC_SAMPLING_MAX_LEN]; //audio data to the buff
uint16_t data_rx_index; //data_buff index
uint16_t data_rx_len; //data_buff len
uint8_t save_buf_index; //double mic buff 1 indexdata_buff[save_buf_index] []
uint8_t get_buf_index; //double mic buff 1 indexdata_buff[save_buf_index] []
uint8_t save_data_buff_count; // mic buff SAVE index CON
uint8_t buf_full_flag;
uint8_t sampling_wait_count; // Audio sampling data interval The sampling frequency is calculated based on the sampling speed of the ADC For example, 16K sampling
uint8_t sampling_count; // Calculate the audio sampling points
int16_t pga_offset_val;
} mic_audio_t;
typedef struct
{
#if (MIC_SAMPLING_DEBUG)
uint32_t audio_adc_rx_con;
uint8_t audio_rx_stop_flag;
#endif // MIC_SAMPLING_DEBUG
#if (MIC_SAMPLING_DEBUG)
uint16_t rec_time;
uint16_t rec_time_con;
#endif //( MIC_SAMPLING_DEBUG)
uint8_t state;
} mic_record_t;
enum
{
REC_INIT,
REC_START,
REC_STOP,
};
#if (ADC_PWM_MODE)
enum adc_fs {
adc_fs_8k = 0,
adc_fs_16k,
};
#endif
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
extern mic_audio_t mic_audio;
extern mic_record_t record;
/*------------------------------------------------------------------------------------
Exported Functions
-------------------------------------------------------------------------------------*/
void audio_adc_rx_dma_event_handler(void);
void audio_adc_rx_event_handler(uint16_t pga_adc_val);
void audio_adc_start_get_value(ADC_ChannelTypeDef_t Channel, uint16_t *adc_val);
void app_audio_record_init(void);
void app_audio_record_start(void);
void app_audio_record_stop(void);
uint8_t app_get_record_state(void);
uint8_t app_get_record_buff_full_state(void);
void app_clr_record_buff_full_state(void);
void app_record_process(void);
void mic_pga_adc_demo(void);
#endif // PGA_MIC_RECORD_DEMO_EN
#if (PGA_MIC_GET_VAL_DEMO_EN)
void pga_mic_adc_get_val_demo(void);
#endif // (PGA_MIC_GET_VAL_DEMO_EN)
void app_pga_init(void);
#endif //__BSP_AUDIO_ADC_H_
@@ -0,0 +1,212 @@
/*!
* \file key.c
*
* \brief Target key 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 "xc_drv_gpio.h"
#include "xc60xx.h"
#include "key.h"
#include "message.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
#define IS_KEY_DOWN(n) (!(xc_gpio_read_pin(n)))
#define KEY_GPIO GPIO_10
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
static uint16_t last_key_val = NO_KEY;
static uint16_t key_down_count = 0;
static uint16_t key_up_count = 0;
/*------------------------------------------------------------------------------------
Func Prototype
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------------------------------------------------*/
/**
* @brief app_key_hold_detection
* @details
* @param curren_key_val
* @retval key_num
*/
__RAM_CODE uint16_t app_key_hold_detection(uint16_t curren_key_val)
{
uint16_t key_num = NO_KEY;
if (key_down_count < 0xff)
key_down_count++;
if (key_down_count == KEY_DOWN_TIMER)
{
key_num = (curren_key_val | KEY_SHORT_DOWN); // shor key
}
else if(key_down_count == KEY_LONG_TIMER)
{
key_num = (curren_key_val | KEY_LONG_DOWN); // loog key
}
key_up_count = 0;
return key_num;
}
/**
* @brief app_new_key_detection
* @details key up or new key detection
* @param curren_key_val
* @retval key_num
*/
__RAM_CODE uint16_t app_new_key_detection(uint16_t curren_key_val)
{
uint16_t key_num = NO_KEY;
key_up_count++;
if (key_up_count >= KEY_UP_TIMER)
{
if (key_down_count >= KEY_LONG_TIMER)
{
key_num = (last_key_val | KEY_LONG_UP); // long key up
last_key_val = 0;
}
else if (key_down_count >= KEY_DOWN_TIMER && key_down_count < KEY_LONG_TIMER)
{
key_num = (last_key_val | KEY_SHORT_UP); // shor key up
last_key_val = 0;
}
key_down_count = 0;
key_up_count = 0;
}
if ((curren_key_val && curren_key_val != last_key_val))
{
if (key_down_count >= KEY_LONG_TIMER)
{
key_num = (last_key_val | KEY_LONG_UP); // long key up
}
else if (key_down_count >= KEY_DOWN_TIMER && key_down_count < KEY_LONG_TIMER)
{
key_num = (last_key_val | KEY_SHORT_UP); // shor key up
}
last_key_val = curren_key_val;
key_down_count = 0;
key_up_count = 0;
}
return key_num;
}
/**
* @brief key_check_process
* @details
* @param curren_key_val
* @retval key_num
*/
__RAM_CODE uint16_t key_check_process(uint16_t curren_key_val)
{
uint16_t key_num = NO_KEY;
if ( curren_key_val == last_key_val && last_key_val != NO_KEY )
{
key_num = app_key_hold_detection(curren_key_val);
}
else
{
key_num = app_new_key_detection(curren_key_val);
}
return key_num;
}
/**
* @brief gpio_key_check
* @details
* @param void
* @retval key_num
*/
uint16_t gpio_key_check(void)
{
uint16_t key_val = NO_KEY;
if ( IS_KEY_DOWN(KEY_GPIO) )
{
key_val = REC_KEY;
}
return key_val;
}
/**
* @brief key_check_process
* @details key scan and send
* @param void
* @retval void
*/
void app_key_scan(void)
{
uint16_t key_num = NO_KEY;
uint16_t key_val = NO_KEY;
key_val = gpio_key_check();
key_num = key_check_process(key_val);
if (key_num != NO_KEY)
{
app_key_message_send( key_num );
}
}
/**
* @brief app_key_init
* @details key init
* @param void
* @retval void
*/
void app_key_init(void)
{
GPIO_InitCfg_t GPIO_InitCfg = {0};
GPIO_InitCfg.Mux = GPIO_Mux0;
GPIO_InitCfg.FunSel = GPIO_Dx;
GPIO_InitCfg.Pull = GPIO_PULLUP;
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
GPIO_InitCfg.Int = NOT_INT;
GPIO_InitCfg.Pin = KEY_GPIO;
xc_gpio_init(&GPIO_InitCfg);
}
@@ -0,0 +1,145 @@
/*!
* \file main.c
*
* \brief Target main implementation
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip ) Alex-J
*
* \author ( XinChip )
*/
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include "main.h"
#include "pga_adc.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Func Prototype
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------------------------------------------------*/
void app_uart_init(void)
{
GPIO_InitCfg_t gpio_cfg = {0};
gpio_cfg.Mux = GPIO_Mux0;
gpio_cfg.Pull = GPIO_PULLUP;
gpio_cfg.Int = NOT_INT;
gpio_cfg.FunSel = UART0_TX;
gpio_cfg.Pin = GPIO_3;
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
xc_gpio_init(&gpio_cfg);
gpio_cfg.FunSel = UART0_RX;
gpio_cfg.Pin = GPIO_19;
gpio_cfg.Dir = GPIO_DIR_INPUT;
xc_gpio_init(&gpio_cfg);
UART_InitCfg_t uart_cfg = {0};
uart_cfg.Parity = UART_PARITY_DISABLE;
uart_cfg.StopBits = UART_STOP_1_BITS;
uart_cfg.WordLength = UART_DATA_8_BITS;
uart_cfg.BaudRate = UART_BAUDRATE_115200;
uart_cfg.HardwareFlowControl = UART_HWFC_DISABLE;
xc_uart_init(UART0_IDX, &uart_cfg);
}
int app_io_simulate_time_period(void)
{
GPIO_InitCfg_t GPIO_InitCfg = { 0 };
GPIO_InitCfg.Mux = GPIO_Mux0;
GPIO_InitCfg.FunSel = GPIO_Dx;
GPIO_InitCfg.Pull = GPIO_PULLUP;
GPIO_InitCfg.Dir = GPIO_DIR_OUTPUT;
GPIO_InitCfg.Int = NOT_INT;
GPIO_InitCfg.Pin = 17;
xc_gpio_init(&GPIO_InitCfg);
GPIO_InitCfg.Pin = 20;
xc_gpio_init(&GPIO_InitCfg);
GPIO_InitCfg.Pin = 1;
xc_gpio_init(&GPIO_InitCfg);
xc_gpio_write_pin(GPIO_17, GPIO_PIN_RESET);
xc_gpio_write_pin(GPIO_20, GPIO_PIN_RESET);
xc_gpio_write_pin(GPIO_1, GPIO_PIN_RESET);
return 0;
}
void clock_init(void)
{
CLOCK_InitCfg_t clock_cfg;
clock_cfg.hfclk_src = CLOCK_HFCLK_SRC_XTAL;
clock_cfg.hfclk_in = CLOCK_HFCLK_IN_32M;
clock_cfg.lfclk_src = CLOCK_LFCLK_SRC_RC;
if (clock_cfg.lfclk_src == CLOCK_LFCLK_SRC_XTAL) {
clock_cfg.lfclk_in = CLOCK_LFCLK_IN_32768;
} else if (clock_cfg.lfclk_src == CLOCK_LFCLK_SRC_RC) {
clock_cfg.lfclk_in = CLOCK_LFCLK_IN_32K;
}
xc_clock_init_cfg(&clock_cfg);
SysTick_Config(xc_clock_hfclk_in_get() / 100);
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
}
int main(void)
{
// Clock Initialization
clock_init();
app_uart_init();
app_io_simulate_time_period();
app_pga_init();
delay_ms(1);//Wait for the pga to stabilize
DEBUG("__START__\n");
#if (PGA_MIC_RECORD_DEMO_EN)
/*----- mic pga adc demo ----- */
mic_pga_adc_demo();
#endif //(PGA_MIC_RECORD_DEMO_EN)
#if (PGA_MIC_GET_VAL_DEMO_EN)
/*----- mic pga get adc value demo ----- */
pga_mic_adc_get_val_demo();
#endif //(PGA_MIC_GET_VAL_DEMO_EN)
while(1)
{
/* main loop */
;
}
}
@@ -0,0 +1,123 @@
/*!
* \file key.c
*
* \brief Target key 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 "message.h"
#include "xc6xxx.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
#define MESSAGE_MAX 40
#define MSG_ADD_MAX(msg) (msg >= MESSAGE_MAX) ? (MESSAGE_MAX) : (msg+1)
#define MSG_REDUCE_MIN(msg) (msg) ? (msg - 1) : 0
#define MSG_LOOP_MAX_MIN(msg) (msg >= MESSAGE_MAX) ? (0) : (msg)
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
uint16_t message_buff[MESSAGE_MAX] = {0};
volatile uint16_t message_in_count = 0;
volatile uint16_t message_out_index = 0;
/*------------------------------------------------------------------------------------
Func Prototype
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------------------------------------------------*/
/**
* @brief app_key_message_send
* @details key msg send
* @param void
* @retval void
*/
void app_key_message_send(uint16_t msg)
{
if (message_in_count >= MESSAGE_MAX )
{
return;
}
for(uint8_t i = 0; i < MESSAGE_MAX ; i++)
{
if (message_buff[i] == 0)
{
message_buff[i] = msg;
message_in_count++;
break;
}
}
}
/**
* @brief app_key_message_get
* @details get key msg
* @param void
* @retval void
*/
uint16_t app_key_message_get(void)
{
uint16_t msg = 0;
if (!message_in_count)
{
message_out_index = 0;
return msg;
}
for(uint8_t i = 0; i < MESSAGE_MAX ; i++)
{
if (message_buff[message_out_index])
{
msg = message_buff[message_out_index];
message_buff[message_out_index] = 0;
message_out_index = MSG_ADD_MAX(message_out_index);
message_out_index = MSG_LOOP_MAX_MIN(message_out_index);
message_in_count = MSG_REDUCE_MIN(message_in_count);
break;
}
message_out_index = MSG_ADD_MAX(message_out_index);
message_out_index = MSG_LOOP_MAX_MIN(message_out_index);
}
return msg;
}
/**
* @brief app_key_message_clr
* @details clear key msg
* @param void
* @retval void
*/
void app_key_message_clr(void)
{
memset(message_buff,0x00,MESSAGE_MAX);
message_in_count = 0;
}
File diff suppressed because it is too large Load Diff