forked from wanrenqi/PASII
1、修复 当设备主动断开蓝牙DisConnect()时,如果在还没有得到断开蓝牙的回调函数响应时,再次调用断开蓝牙函数DisConnect()时会有概率出现停止软件定时器的问题。
This commit is contained in:
parent
b0a3253626
commit
d3603d1173
|
@ -0,0 +1,38 @@
|
|||
/*************************************************************************************************
|
||||
**Filename: app_adc.c
|
||||
**Version :
|
||||
**Author :
|
||||
**Date :
|
||||
**Description:
|
||||
**
|
||||
*************************************************************************************************/
|
||||
#include "app_adc.h"
|
||||
#include "mcu_bsp.h"
|
||||
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description :
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
void ADC_Config(void)
|
||||
{
|
||||
uint8_t trim = 0;
|
||||
|
||||
trim = GPADC_Get_Calibration(); //获取芯片内部测试阶段写入的校准值
|
||||
|
||||
if(trim==0)
|
||||
{
|
||||
GPADC_Manual_Calibration(0x12); //这里读到芯片内部的校准值为0,表示当前批次没有写入校准值 这里写入0X12即可,0x12是一个比较合理的值
|
||||
}
|
||||
else
|
||||
{
|
||||
GPADC_Manual_Calibration(trim); //如果芯片内部已经有校准值 这里可以字节写入芯片内部的校准值 该值和0x12不会偏差太多
|
||||
}
|
||||
|
||||
GPADC_Init(ADCGP_CH,AVE_MODE); //电池电量采样通道设置
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/*************************************************************************************************
|
||||
**Filename: app_adc.h
|
||||
**Version :
|
||||
**Author :
|
||||
**Date :
|
||||
**Description:
|
||||
**
|
||||
*************************************************************************************************/
|
||||
#ifndef __APP_ADC_H_
|
||||
#define __APP_ADC_H_
|
||||
|
||||
/*********************************************************************
|
||||
* Include (include files)
|
||||
*/
|
||||
#include "gpadc.h"
|
||||
|
||||
/*********************************************************************
|
||||
* Global Function declared ('extern')
|
||||
*/
|
||||
void ADC_Config(void);
|
||||
|
||||
#endif
|
||||
|
||||
/*************************************************************************************************/
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
/*************************************************************************************************
|
||||
**Filename: app_gpio.c
|
||||
**Version :
|
||||
**Author :
|
||||
**Date :
|
||||
**Description:
|
||||
**
|
||||
*************************************************************************************************/
|
||||
#include "app_gpio.h"
|
||||
#include "mcu_bsp.h"
|
||||
|
||||
/*********************************************************************
|
||||
* Extern Function declared ('extern')
|
||||
*/
|
||||
void GPIO_callback(void);
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description :
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
void LED_IO_Config(void)
|
||||
{
|
||||
PIN_Set_GPIO(U32BIT(LED_PIN),PIN_SEL_GPIO); //引脚设置成普通IO口
|
||||
GPIO_Set_Output(U32BIT(LED_PIN)); //设置引脚为输出模式
|
||||
GPIO_Pin_Set(U32BIT(LED_PIN));
|
||||
}
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description :
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
void IIC_IO_Config(void)
|
||||
{
|
||||
i2c_Init(I2C0,U32BIT(I2C0_SCLK),U32BIT(I2C0_SDA)); //lsm6d IIC IO初始化
|
||||
i2c_Init(I2C1,U32BIT(I2C1_SCLK),U32BIT(I2C1_SDA)); //lis2d IIC IO初始化
|
||||
}
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description :
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
void Ctr_IO_Config(void)
|
||||
{
|
||||
//lsm6d 供电使能 IO
|
||||
PIN_Set_GPIO(U32BIT(PEN_PIN),PIN_SEL_GPIO); //引脚设置成普通IO口
|
||||
GPIO_Set_Output(U32BIT(PEN_PIN)); //设置引脚为输出模式
|
||||
GPIO_Pin_Set(U32BIT(PEN_PIN)); //Power Disable
|
||||
|
||||
//lsm6d CS IO口
|
||||
PIN_Set_GPIO(U32BIT(CS_PIN),PIN_SEL_GPIO); //引脚设置成普通IO口
|
||||
GPIO_Set_Output(U32BIT(CS_PIN)); //设置引脚为输出模式
|
||||
GPIO_Pin_Set(U32BIT(CS_PIN)); //CS设置为高,使能IIC通讯
|
||||
|
||||
//lsm6d SA0 IO口
|
||||
PIN_Set_GPIO(U32BIT(SA0_PIN),PIN_SEL_GPIO); //引脚设置成普通IO口
|
||||
GPIO_Set_Output(U32BIT(SA0_PIN)); //设置引脚为输出模式
|
||||
GPIO_Pin_Clear(U32BIT(SA0_PIN)); //SA0设置为低,则设备的地址LSB为0
|
||||
|
||||
//电池采样控制引脚
|
||||
PIN_Set_GPIO(U32BIT(CEN_PIN),PIN_SEL_GPIO); //引脚设置成普通IO口
|
||||
GPIO_Set_Output(U32BIT(CEN_PIN)); //设置引脚为输出模式
|
||||
CEN_ENABLE();
|
||||
|
||||
}
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description :
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
void INT_IO_Config(void)
|
||||
{
|
||||
//lis2dh 中断1 IO口
|
||||
PIN_Set_GPIO(U32BIT(LIS2D_INT1),PIN_SEL_GPIO);
|
||||
GPIO_Set_Input(U32BIT(LIS2D_INT1),0); //设置为输入 不取反
|
||||
PIN_Pullup_Enable(T_QFN_48, U32BIT(LIS2D_INT1)); //关闭上拉输入
|
||||
GPIO_Input_Disable(U32BIT(LIS2D_INT1));
|
||||
io_irq_disable(U32BIT(LIS2D_INT1)); //关闭IO口中断功能
|
||||
|
||||
//lis2dh 中断2 IO口
|
||||
PIN_Set_GPIO(U32BIT(LIS2D_INT2),PIN_SEL_GPIO);
|
||||
GPIO_Set_Input(U32BIT(LIS2D_INT2),U32BIT(LIS2D_INT2)); //设置为输入取反
|
||||
PIN_Pullup_Enable(T_QFN_48, U32BIT(LIS2D_INT2)); //上拉输入
|
||||
GPIO_Input_Enable(U32BIT(LIS2D_INT2));
|
||||
io_irq_enable(U32BIT(LIS2D_INT2),GPIO_callback); //使能IO口中断功能
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/*************************************************************************************************
|
||||
**Filename: app_gpio.h
|
||||
**Version :
|
||||
**Author :
|
||||
**Date :
|
||||
**Description:
|
||||
**
|
||||
*************************************************************************************************/
|
||||
#ifndef __APP_GPIO_H_
|
||||
#define __APP_GPIO_H_
|
||||
|
||||
/*********************************************************************
|
||||
* Include (include files)
|
||||
*/
|
||||
#include "gpio.h"
|
||||
#include "i2c.h"
|
||||
|
||||
/*********************************************************************
|
||||
* Global Function declared ('extern')
|
||||
*/
|
||||
void LED_IO_Config(void);
|
||||
void IIC_IO_Config(void);
|
||||
void Ctr_IO_Config(void);
|
||||
void INT_IO_Config(void);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
/*************************************************************************************************
|
||||
**Filename: app_timer.c
|
||||
**Version :
|
||||
**Author :
|
||||
**Date :
|
||||
**Description:
|
||||
**
|
||||
*************************************************************************************************/
|
||||
#include "app_timer.h"
|
||||
|
||||
/*********************************************************************
|
||||
* Extern Function declared ('extern')
|
||||
*/
|
||||
extern void Timer_2_callback(void);
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description :
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
void Timer_Config(void)
|
||||
{
|
||||
timer_2_disable(); //100ms¶¨Ê±ÈÎÎñ
|
||||
}
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description :
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
void Timer2_Enable(void)
|
||||
{
|
||||
timer_2_enable(3276,Timer_2_callback); //100ms¶¨Ê±ÈÎÎñ
|
||||
}
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description :
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
void Timer2_Disable(void)
|
||||
{
|
||||
timer_2_disable();
|
||||
}
|
||||
/*************************************************************************************************/
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
/*************************************************************************************************
|
||||
**Filename: app_timer.h
|
||||
**Version :
|
||||
**Author :
|
||||
**Date :
|
||||
**Description:
|
||||
**
|
||||
*************************************************************************************************/
|
||||
#ifndef __APP_TIMER_H_
|
||||
#define __APP_TIMER_H_
|
||||
|
||||
/*********************************************************************
|
||||
* Include (include files)
|
||||
*/
|
||||
#include "timer.h"
|
||||
|
||||
/*********************************************************************
|
||||
* Global Function declared ('extern')
|
||||
*/
|
||||
void Timer_Config(void);
|
||||
void Timer2_Enable(void);
|
||||
void Timer2_Disable(void);
|
||||
|
||||
#endif
|
||||
|
||||
/*************************************************************************************************/
|
||||
|
||||
|
|
@ -0,0 +1,164 @@
|
|||
#include"IR_Tx.h"
|
||||
#include"gpio.h"
|
||||
#include"debug.h"
|
||||
#include <math.h>
|
||||
#include "lib.h"
|
||||
|
||||
static IR_Tx_CTRL_TYPE * IR_Tx_CTRL = ((IR_Tx_CTRL_TYPE *) IR_Tx_CTRL_BASE);
|
||||
|
||||
void IR_Tx_Init(void)
|
||||
{
|
||||
uint16_t period, invtime, pwmfomat;
|
||||
uint8_t logicbit;
|
||||
uint8_t clk;
|
||||
|
||||
PIN_CONFIG->PIN_4_SEL = PIN_SEL_IR_OUT;
|
||||
|
||||
//Disable
|
||||
IR_Tx_CTRL->IREN = 0;
|
||||
IR_Tx_CTRL->REPEN = 0;
|
||||
|
||||
//----------------------------CARRIER-------------------------
|
||||
//载波周期:
|
||||
GetMCUClock(&clk);
|
||||
if(clk == SYSTEM_CLOCK_16M_XOSC) clk = SYSTEM_CLOCK_16M_RCOSC;
|
||||
clk = 64/pow(2,clk); //IR_Tx_CLK too
|
||||
dbg_printf("IR_Tx_Init: sys_clk: %dM\r\n", clk);
|
||||
period = clk*1000000/IR_CARR_FREQ; //ir_clk_cycle
|
||||
//载波占空比->1/3
|
||||
invtime = period/3;
|
||||
//载波格式
|
||||
pwmfomat = 0; //0:from high to low, 1:from low to high
|
||||
IR_Tx_CTRL->CARRIER = ((uint32_t)invtime<<11)|((uint32_t)pwmfomat<<22)|period;
|
||||
|
||||
//----------------------------LOGICBIT-------------------------
|
||||
//载波周期26.37362637362637us -> 560us/26.37362637362637us = 21.23
|
||||
logicbit = 560/(1000000.0/IR_CARR_FREQ); //ir_carrier_cycle
|
||||
IR_Tx_CTRL->ONE_FH = logicbit;
|
||||
IR_Tx_CTRL->ONE_LH = logicbit*3;
|
||||
IR_Tx_CTRL->ZERO_FH = logicbit;
|
||||
IR_Tx_CTRL->ZERO_LH = logicbit;
|
||||
|
||||
//----------------------------CTRL----------------------------
|
||||
IR_Tx_CTRL->MODE = 0; //IR mode
|
||||
IR_Tx_CTRL->IRINVOUT = 0; //not invert output level
|
||||
IR_Tx_CTRL->ONEFMT = 0; //mark first,space second
|
||||
IR_Tx_CTRL->ZEROFMT = 0; //mark first,space second
|
||||
IR_Tx_CTRL->IRIE = 1; //interrupt enable when commond CMD FIFO transmit done
|
||||
IR_Tx_CTRL->CMDIE = 0; //interrupt disable when each commond CMD or repeat CMD transmit done
|
||||
//repeat interval:108ms
|
||||
IR_Tx_CTRL->REPTIME = 108000/(1000000.0/IR_CARR_FREQ); //repeat interval,in carrier clock cycles
|
||||
|
||||
//----------------------------FIFOCLR----------------------------
|
||||
IR_Tx_CTRL->ccmdfifoclr = 1; //clear common comand fifo
|
||||
IR_Tx_CTRL->rcmdfifoclr = 1; //clear repeat comand fifo
|
||||
|
||||
//----------------------------INTSTAT----------------------------
|
||||
IR_Tx_CTRL->cmd_done_clr = 1; //clear flag
|
||||
IR_Tx_CTRL->trans_done_clr = 1; //clear flag
|
||||
|
||||
//Enable
|
||||
IR_Tx_CTRL->IREN = 1;
|
||||
IR_Tx_CTRL->REPEN = 0;
|
||||
|
||||
NVIC_EnableIRQ(IR_Tx_IRQn);
|
||||
}
|
||||
|
||||
void IR_Tx_SendData(uint16_t usrCode, uint8_t data)
|
||||
{
|
||||
uint32_t val = 0;
|
||||
uint16_t bit_sequence = 0;
|
||||
uint8_t bit_number = 0;
|
||||
uint16_t first_half_time;
|
||||
uint16_t last_half_time;
|
||||
|
||||
#if 1
|
||||
//引导码 : type1
|
||||
first_half_time = 9000/(1000000.0/IR_CARR_FREQ); /* 9ms载波 */
|
||||
last_half_time = first_half_time/2; /* 4.5ms空闲 */
|
||||
val = (1<<25)|(0<<24)|(last_half_time<<12)|(first_half_time);
|
||||
//dbg_printf("val_1 = 0x%08x\r\n", val);
|
||||
IR_Tx_CTRL->COMCMD = val;
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
//用户码 - 用户码反码 : type0
|
||||
bit_sequence = usrCode;
|
||||
bit_number = 0x0f;
|
||||
val = (0<<25)|(bit_number<<16)|(bit_sequence);
|
||||
//dbg_printf("val_2 = 0x%08x\r\n", val);
|
||||
IR_Tx_CTRL->COMCMD = val;
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
//数据码 - 数据码反码 : type0
|
||||
bit_sequence = (~data<<8)|data;
|
||||
bit_number = 0x0f;
|
||||
val = (0<<25)|(bit_number<<16)|(bit_sequence);
|
||||
//dbg_printf("val_3 = 0x%08x\r\n", val);
|
||||
IR_Tx_CTRL->COMCMD = val;
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
//停止码 : type1
|
||||
first_half_time = 560/(1000000.0/IR_CARR_FREQ); /* 0.56ms载波 */
|
||||
last_half_time = 0; /* NEC格式停止码只有0.56ms载波 */
|
||||
val = (1<<25)|(0<<24)|(last_half_time<<12)|(first_half_time);
|
||||
//dbg_printf("val_4 = 0x%08x\r\n", val);
|
||||
IR_Tx_CTRL->COMCMD = val;
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
//重复码 //NEC: 9ms载波-2.25ms空闲-0.56ms载波
|
||||
|
||||
IR_Tx_CTRL->rcmdfifoclr = 1; //clear repeat comand fifo first
|
||||
|
||||
//停止码 : type1
|
||||
first_half_time = 9000/(1000000.0/IR_CARR_FREQ); /* 9ms载波 */
|
||||
last_half_time = first_half_time/4; /* 2.25ms空闲 */
|
||||
val = (1<<25)|(0<<24)|(last_half_time<<12)|(first_half_time);
|
||||
//dbg_printf("val_5 = 0x%08x\r\n", val);
|
||||
IR_Tx_CTRL->REPCMD = val;
|
||||
|
||||
//停止码 : type1
|
||||
first_half_time = 560/(1000000.0/IR_CARR_FREQ); /* 0.56ms载波 */
|
||||
last_half_time = 0; /* NEC格式停止码只有0.56ms载波 */
|
||||
val = (1<<25)|(0<<24)|(last_half_time<<12)|(first_half_time);
|
||||
//dbg_printf("val_6 = 0x%08x\r\n", val);
|
||||
IR_Tx_CTRL->REPCMD = val;
|
||||
#endif
|
||||
}
|
||||
|
||||
void IR_Tx_IRQHandler(void)
|
||||
{
|
||||
static uint32_t repeat = 0;
|
||||
|
||||
if(IR_Tx_CTRL->trans_done) //Common Cmd FIFO transmit done
|
||||
{
|
||||
dbg_printf("C");
|
||||
IR_Tx_CTRL->trans_done_clr = 1;
|
||||
|
||||
IR_Tx_CTRL->cmd_done_clr = 1; //need
|
||||
IR_Tx_CTRL->CMDIE = 1;
|
||||
IR_Tx_CTRL->REPEN = 1;
|
||||
repeat = 0;
|
||||
}
|
||||
|
||||
if((IR_Tx_CTRL->CMDIE)&&(IR_Tx_CTRL->cmd_done))
|
||||
{
|
||||
IR_Tx_CTRL->cmd_done_clr = 1;
|
||||
dbg_printf("E"); //Each
|
||||
|
||||
//演示通过变量repeat控制重复码发送次数
|
||||
repeat++;
|
||||
if(repeat == 4) //只发送2次重复码(4个重复码)
|
||||
{
|
||||
//stop sending repeat code
|
||||
IR_Tx_CTRL->REPEN = 0;
|
||||
IR_Tx_CTRL->CMDIE = 0;
|
||||
IR_Tx_CTRL->rcmdfifoclr = 1;
|
||||
repeat = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,204 @@
|
|||
#include "atr_decoder.h"
|
||||
#include "debug.h"
|
||||
|
||||
enum {
|
||||
T0 = 0,
|
||||
T1,
|
||||
};
|
||||
|
||||
enum {
|
||||
TA1,
|
||||
TA2,
|
||||
TA15,
|
||||
TC1,
|
||||
TC2,
|
||||
TD1,
|
||||
// For array declaration
|
||||
TOTAL,
|
||||
};
|
||||
|
||||
static uint8_t parsed_data[TOTAL];
|
||||
static uint8_t default_fd = DEFAULT_FD;
|
||||
static uint32_t parsed_bitmask;
|
||||
|
||||
void atr_reset(void)
|
||||
{
|
||||
parsed_bitmask = 0;
|
||||
}
|
||||
|
||||
bool atr_decode(SC_ATR *atr)
|
||||
{
|
||||
int mask, i;
|
||||
int currentProtocol;
|
||||
int index;
|
||||
uint8_t TA15 = 0x07;
|
||||
|
||||
// T0
|
||||
atr->HLength = atr->T0 & 0x0F;
|
||||
mask = atr->T0 & 0xF0;
|
||||
index = 1;
|
||||
|
||||
while (mask != 0)
|
||||
{
|
||||
int T[4];
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
if ((mask >> (i + 4)) & 0x01)
|
||||
{
|
||||
T[i] = (int)atr->T[atr->ILength];
|
||||
atr->ILength++;
|
||||
}
|
||||
else
|
||||
{
|
||||
T[i] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
// TA
|
||||
if (T[0] != -1)
|
||||
{
|
||||
// TA1 - Fi, Di
|
||||
if (index == 1)
|
||||
{
|
||||
parsed_data[TA1] = T[0] & 0xFF;
|
||||
parsed_bitmask |= (1 << TA1);
|
||||
}
|
||||
else if (index == 2)
|
||||
{ // TA2 - PPS Mode
|
||||
parsed_data[TA2] = T[0] & 0xFF;
|
||||
parsed_bitmask |= (1 << TA2);
|
||||
}
|
||||
else if (currentProtocol == 15)
|
||||
{
|
||||
parsed_data[TA15] = T[0] & 0xFF;
|
||||
parsed_bitmask |= (1 << TA15);
|
||||
}
|
||||
}
|
||||
|
||||
// TB - deprecated or T = 1, do not support for now
|
||||
|
||||
// TC
|
||||
if (T[2] != -1)
|
||||
{
|
||||
//TC1 - Extra guard time
|
||||
if (index == 1)
|
||||
{
|
||||
parsed_data[TC1] = T[2] & 0xFF;
|
||||
parsed_bitmask |= (1 << TC1);
|
||||
}
|
||||
else if (index == 2)
|
||||
{
|
||||
parsed_data[TC2] = T[2] & 0xFF;
|
||||
parsed_bitmask |= (1 << TC2);
|
||||
}
|
||||
}
|
||||
|
||||
// TD
|
||||
if (T[3] != -1)
|
||||
{
|
||||
// next TD exist
|
||||
mask = T[3] & 0xF0;
|
||||
currentProtocol = T[3] & 0x0F;
|
||||
// TD1
|
||||
if (index == 1)
|
||||
{
|
||||
parsed_data[TD1] = T[3] & 0xFF;
|
||||
parsed_bitmask |= (1 << TD1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mask = 0;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
// TH
|
||||
atr->Th_Start = atr->ILength;
|
||||
// TCK exists
|
||||
if (atr->ILength + atr->HLength + 2 != atr->TotalLength)
|
||||
{
|
||||
uint8_t check = atr->T0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < atr->TotalLength - 2; i++)
|
||||
{
|
||||
check ^= atr->T[i];
|
||||
}
|
||||
|
||||
atr->Check = check;
|
||||
}
|
||||
|
||||
return (bool)atr->Check;
|
||||
}
|
||||
|
||||
uint8_t atr_decoder_get_extra_guard_time(void)
|
||||
{
|
||||
if (parsed_bitmask & (1 << TC1))
|
||||
return parsed_data[TC1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t atr_decoder_get_waiting_integer(void)
|
||||
{
|
||||
uint8_t wi = 10;
|
||||
if (parsed_bitmask & (1 << TC2))
|
||||
wi = parsed_data[TC2];
|
||||
|
||||
return wi;
|
||||
}
|
||||
|
||||
bool atr_decoder_get_clock_stop(bool *high)
|
||||
{
|
||||
bool en = false;
|
||||
|
||||
*high = false;
|
||||
if ((parsed_bitmask & (1 << TA15)) && (parsed_data[TA15] & 0xC0))
|
||||
{
|
||||
en = true;
|
||||
*high = (bool)((parsed_data[TA15] & 0x80) == 0);
|
||||
}
|
||||
return en;
|
||||
}
|
||||
|
||||
bool atr_decoder_allow_pps(void)
|
||||
{
|
||||
return (bool)((parsed_bitmask & (1 << TA2)) == 0);
|
||||
}
|
||||
|
||||
bool atr_decoder_allow_switch_mode(void)
|
||||
{
|
||||
return (bool)((parsed_bitmask & (1 << TA2)) && (!(parsed_data[TA2] & 0x80))); //bypb add:!
|
||||
}
|
||||
|
||||
uint8_t atr_decoder_get_protocol(void)
|
||||
{
|
||||
// specific mode
|
||||
if (parsed_bitmask & (1 << TA2))
|
||||
{
|
||||
return parsed_data[TA2] & 0x0F;
|
||||
}
|
||||
// first offer
|
||||
else if (parsed_bitmask & (1 << TD1))
|
||||
{
|
||||
return parsed_data[TD1] & 0x0F;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t atr_decoder_get_FD(void)
|
||||
{
|
||||
if (parsed_bitmask & (1 << TA1))
|
||||
{
|
||||
return parsed_data[TA1];
|
||||
}
|
||||
return default_fd;
|
||||
}
|
||||
|
||||
void atr_decoder_config_default_FD(uint8_t fd)
|
||||
{
|
||||
default_fd = fd;
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
串口调试操作文件
|
||||
|
||||
作者:北京盛源达科技有限公司
|
||||
日期:2016/7/8
|
||||
*/
|
||||
#include "debug.h"
|
||||
#include "uart.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef _DEBUG_
|
||||
|
||||
#define MAX_FORMAT_BUFFER_SIZE (128)
|
||||
static uint8_t s_formatBuffer[MAX_FORMAT_BUFFER_SIZE];
|
||||
|
||||
void dbg_init(void)
|
||||
{
|
||||
uart_0_init();
|
||||
}
|
||||
|
||||
void dbg_printf(char *format,...)
|
||||
{
|
||||
uint8_t iWriteNum = 0,i=0;
|
||||
va_list ap;
|
||||
|
||||
if(!format)
|
||||
return;
|
||||
|
||||
va_start(ap,format);
|
||||
|
||||
iWriteNum = vsprintf((char *)s_formatBuffer,format,ap);
|
||||
|
||||
va_end(ap);
|
||||
|
||||
if(iWriteNum > MAX_FORMAT_BUFFER_SIZE)
|
||||
iWriteNum = MAX_FORMAT_BUFFER_SIZE;
|
||||
|
||||
for(i=0;i<iWriteNum;i++){
|
||||
uart_0_write(s_formatBuffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void dbg_hexdump(char *title, uint8_t *buf, uint16_t sz)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
if (title)
|
||||
dbg_printf((title));
|
||||
|
||||
for(i = 0; i < sz; i++)
|
||||
{
|
||||
dbg_printf("%02x ",(uint16_t)buf[i]);
|
||||
}
|
||||
|
||||
dbg_printf("\r\n");
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
延时函数文件
|
||||
|
||||
作者:北京盛源达科技有限公司
|
||||
日期:2016/7/8
|
||||
*/
|
||||
#include "delay.h"
|
||||
/*
|
||||
n毫秒延时函数
|
||||
参数: uint16_t n 要延时的毫秒数 单位:1ms 注意时钟 64M
|
||||
*/
|
||||
void delay_ms(uint16_t n) {
|
||||
uint16_t i,j;
|
||||
for(i=0; i<n; i++)
|
||||
for(j=0; j<8000; j++);
|
||||
}
|
||||
/*
|
||||
n微秒延时函数
|
||||
参数: uint16_t n 要延时的微秒数 单位:1us 注意时钟 64M
|
||||
*/
|
||||
void delay_us(uint16_t n) {
|
||||
uint16_t i,j;
|
||||
for(i=0; i<n; i++)
|
||||
for(j=0; j<8; j++);
|
||||
}
|
|
@ -0,0 +1,221 @@
|
|||
#include "gpadc.h"
|
||||
#include "debug.h"
|
||||
#include "gpio.h"
|
||||
|
||||
GPADC_CTRL_TYPE * GPADC = ((GPADC_CTRL_TYPE *) GPADC_CTRL_BASE);
|
||||
|
||||
uint8_t channel = 0;
|
||||
|
||||
//extern uint8_t adc_state;
|
||||
static void (*adc_callback)(uint16_t adc);
|
||||
/**********************************************************
|
||||
GPIO2 ADCGP_CH[0]
|
||||
GPIO3 ADCGP_CH[1]
|
||||
GPIO4 ADCGP_CH[2]
|
||||
GPIO5 ADCGP_CH[3]
|
||||
GPIO28 ADCGP_CH[4]
|
||||
GPIO29 ADCGP_CH[5]
|
||||
GPIO30 ADCGP_CH[6]
|
||||
GPIO31 ADCGP_CH[7]
|
||||
VBAT ADCGP_CH[8]
|
||||
VDVDD_1.25 ADCGP_CH[9]
|
||||
VDCDC_1.1 ADCGP_CH[10]
|
||||
***********************************************************/
|
||||
void GPADC_Init(ADCGP_CHX ch,GPADC_MODE mode)
|
||||
{
|
||||
//-----------------pin config-------------------
|
||||
switch(ch)
|
||||
{
|
||||
case ADCGP_CH0:
|
||||
//ADCGP_CH[0] Pin Init
|
||||
PIN_CONFIG->PIN_2_SEL = PIN_SEL_GPIO;
|
||||
GPIO_CTRL->GPIO_2_DIR = 1;
|
||||
GPI_CTRL->GPI_2_InE = 0;
|
||||
PIN_CONFIG->PAD_2_INPUT_PULL_UP = 0; //上拉禁止
|
||||
|
||||
PIN_CONFIG->PIN_2_SEL = PIN_SEL_ANALOG;
|
||||
break;
|
||||
case ADCGP_CH1:
|
||||
//ADCGP_CH[1] Pin Init
|
||||
PIN_CONFIG->PIN_3_SEL = PIN_SEL_GPIO;
|
||||
GPIO_CTRL->GPIO_3_DIR = 1;
|
||||
GPI_CTRL->GPI_3_InE = 0;
|
||||
PIN_CONFIG->PAD_3_INPUT_PULL_UP = 0; //上拉禁止
|
||||
|
||||
PIN_CONFIG->PIN_3_SEL = PIN_SEL_ANALOG;
|
||||
break;
|
||||
case ADCGP_CH2:
|
||||
//ADCGP_CH[2] Pin Init
|
||||
PIN_CONFIG->PIN_4_SEL = PIN_SEL_GPIO;
|
||||
GPIO_CTRL->GPIO_4_DIR = 1;
|
||||
GPI_CTRL->GPI_4_InE = 0;
|
||||
PIN_CONFIG->PAD_4_INPUT_PULL_UP = 0; //上拉禁止
|
||||
|
||||
PIN_CONFIG->PIN_4_SEL = PIN_SEL_ANALOG;
|
||||
break;
|
||||
case ADCGP_CH3:
|
||||
//ADCGP_CH[3] Pin Init
|
||||
PIN_CONFIG->PIN_5_SEL = PIN_SEL_GPIO;
|
||||
GPIO_CTRL->GPIO_5_DIR = 1;
|
||||
GPI_CTRL->GPI_5_InE = 0;
|
||||
PIN_CONFIG->PAD_5_INPUT_PULL_UP = 0; //上拉禁止
|
||||
|
||||
PIN_CONFIG->PIN_5_SEL = PIN_SEL_ANALOG;
|
||||
break;
|
||||
case ADCGP_CH4:
|
||||
//ADCGP_CH[4] Pin Init
|
||||
PIN_CONFIG->PIN_28_SEL = PIN_SEL_GPIO;
|
||||
GPIO_CTRL->GPIO_28_DIR = 1;
|
||||
#ifdef _SYD8811_BAND_
|
||||
GPI_CTRL->GPI_28_InE = 0;
|
||||
#else
|
||||
GPI_CTRL->GPI_16_InE = 0;
|
||||
#endif
|
||||
PIN_CONFIG->PAD_36_INPUT_PULL_UP = 0; //上拉禁止
|
||||
|
||||
PIN_CONFIG->PIN_28_SEL = PIN_SEL_ANALOG;
|
||||
break;
|
||||
case ADCGP_CH5:
|
||||
//ADCGP_CH[5] Pin Init
|
||||
PIN_CONFIG->PIN_29_SEL = PIN_SEL_GPIO;
|
||||
GPIO_CTRL->GPIO_29_DIR = 1;
|
||||
GPI_CTRL->GPI_29_InE = 0;
|
||||
PIN_CONFIG->PAD_37_INPUT_PULL_UP = 0; //上拉禁止
|
||||
|
||||
PIN_CONFIG->PIN_29_SEL = PIN_SEL_ANALOG;
|
||||
break;
|
||||
case ADCGP_CH6:
|
||||
//ADCGP_CH[6] Pin Init
|
||||
PIN_CONFIG->PIN_30_SEL = PIN_SEL_GPIO;
|
||||
GPIO_CTRL->GPIO_30_DIR = 1;
|
||||
#ifdef _SYD8811_BAND_
|
||||
GPI_CTRL->GPI_30_InE = 0;
|
||||
#else
|
||||
GPI_CTRL->GPI_20_InE = 0;
|
||||
#endif
|
||||
PIN_CONFIG->PAD_38_INPUT_PULL_UP = 0; //上拉禁止
|
||||
|
||||
PIN_CONFIG->PIN_30_SEL = PIN_SEL_ANALOG;
|
||||
break;
|
||||
case ADCGP_CH7:
|
||||
//ADCGP_CH[7] Pin Init
|
||||
PIN_CONFIG->PIN_31_SEL = PIN_SEL_GPIO;
|
||||
GPIO_CTRL->GPIO_31_DIR = 1;
|
||||
GPI_CTRL->GPI_31_InE = 0;
|
||||
PIN_CONFIG->PAD_39_INPUT_PULL_UP = 0; //上拉禁止
|
||||
|
||||
PIN_CONFIG->PIN_31_SEL = PIN_SEL_ANALOG;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
GPADC->CLKRATE = 31; // 64M/((31+1)*2)= 1M
|
||||
GPADC->DA_GPADC_EN = 1;
|
||||
|
||||
GPADC->CHANNEL_SET_NUM = 1;
|
||||
GPADC->SCAN_COUNT = 1; //只能设置为1
|
||||
GPADC_channel_sel(channel);
|
||||
GPADC->Continue_Scan = 0;
|
||||
|
||||
//set time delay
|
||||
GPADC->START_SETTLE = 24;
|
||||
//GPADC->CHANNEL_SETTLE = 1; //7.52us
|
||||
//GPADC->CHANNEL_SETTLE = 2; //11.29us
|
||||
GPADC->CHANNEL_SETTLE = 4; //18.81us
|
||||
//GPADC->CHANNEL_SETTLE = 63; //240.71us
|
||||
|
||||
if(mode == AVE_MODE)
|
||||
{
|
||||
//一次scan采集data_length+1次数据,然后再取均值
|
||||
GPADC->AVERAGE = 7;
|
||||
GPADC->DATA_LENGTH = 7;
|
||||
}
|
||||
else
|
||||
{
|
||||
GPADC->AVERAGE = 1;
|
||||
GPADC->DATA_LENGTH = 0;
|
||||
}
|
||||
|
||||
GPADC->EVENTS = 0;
|
||||
GPADC->INTENSET = 1; //使能中断,标志位才能置位
|
||||
NVIC_EnableIRQ(GPADC_IRQn);
|
||||
}
|
||||
|
||||
void GPADC_start(void (*p_callback)(uint16_t adc))
|
||||
{
|
||||
if(p_callback) adc_callback=p_callback;
|
||||
GPADC->DA_GPADC_EN = 1;
|
||||
GPADC->TASK_START = 1;
|
||||
}
|
||||
|
||||
/*************************************************************
|
||||
Note:
|
||||
STOP will reset Continue_Scan register
|
||||
**************************************************************/
|
||||
void GPADC_stop(void)
|
||||
{
|
||||
if(GPADC->gpadc_fsm_cs)
|
||||
{
|
||||
GPADC->TASK_STOP = 1;
|
||||
//
|
||||
GPADC->INTENCLR = 1; //disable INT
|
||||
GPADC->EVENTS = 0; //clear INT flag
|
||||
while(GPADC->gpadc_fsm_cs);
|
||||
|
||||
while(GPADC->EVENTS == 0);
|
||||
GPADC->EVENTS = 0; //clr INT flag
|
||||
|
||||
GPADC->INTENSET = 1; //Enable INT
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t GPADC_get_value(void)
|
||||
{
|
||||
return GPADC->adc_data_hclk;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
function: select 1 GPADC channel when GPADC is stopped at oneshot/ave mode
|
||||
para:
|
||||
ch - 0 ~ 10 select channel 0 ~ channel 10
|
||||
note: select GPADC channel when GPADC is stopped
|
||||
*********************************************************************************/
|
||||
void GPADC_channel_sel(uint8_t ch)
|
||||
{
|
||||
if(ch<=10)
|
||||
{
|
||||
GPADC->CHANNEL_SET0 = ch;
|
||||
GPADC->CHANNEL_SEL = ch;
|
||||
}
|
||||
}
|
||||
|
||||
void GPADC_IRQHandler(void)
|
||||
{
|
||||
if(GPADC->EVENTS) //使能ADC中断,标志位才能置位
|
||||
{
|
||||
uint16_t adc = GPADC_get_value();
|
||||
|
||||
GPADC->EVENTS = 0;
|
||||
GPADC->DA_GPADC_EN = 0;
|
||||
if(adc_callback)
|
||||
{
|
||||
adc_callback(adc);
|
||||
}
|
||||
|
||||
// dbg_printf("vol:%4.3f\r\n ",adc);
|
||||
// adc=adc&0xfffe;
|
||||
//
|
||||
// if(channel==8) //VBAT通道
|
||||
// {
|
||||
// vat=(float)adc*3.6/483;
|
||||
// }
|
||||
// else //GPIO通道
|
||||
// {
|
||||
// vat=(float)adc*3.6/1024;
|
||||
// }
|
||||
// dbg_printf("ch:%02d adc : %04x vat: %4.3f\r\n",channel,adc,vat);
|
||||
// adc_state=1;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,267 @@
|
|||
#include "gpio.h"
|
||||
|
||||
//寄存器地址声明
|
||||
PIN_CONFIG_TYPE * PIN_CONFIG = ((PIN_CONFIG_TYPE *) PIN_CONFIG_BASE); //管脚控制寄存器
|
||||
GPIO_CTRL_TYPE * GPIO_CTRL = ((GPIO_CTRL_TYPE *) GPIO_CTRL_BASE); //GPIO控制寄存器
|
||||
GPO_CTRL_TYPE * GPO_CTRL = ((GPO_CTRL_TYPE *) GPO_CTRL_BASE); //GPIO输出控制寄存器
|
||||
GPI_CTRL_TYPE * GPI_CTRL = ((GPI_CTRL_TYPE *) GPI_CTRL_BASE); //GPIO输入控制寄存器
|
||||
GPIO_IRQ_CTRL_TYPE * GPIO_IRQ_CTRL = ((GPIO_IRQ_CTRL_TYPE *) GPIO_IRQ_CTRL_BASE);
|
||||
|
||||
static void (*gpio_callback)(void);
|
||||
|
||||
static uint8_t pinmuxtab48[24] = {
|
||||
0,1,2,3,4,5,6,7,8,9,10,17,18,19,20,21,22,23,24,25,26,27,30,31,
|
||||
};
|
||||
static uint8_t pinmuxtab32[24] = {
|
||||
0,1,2,3,4,5,6,9,10,7,8,17,21,18,22,23,24,19,25,20,26,27,30,31,
|
||||
};
|
||||
|
||||
void PIN_Set_GPIO(uint32_t io,uint8_t fun)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for(i=0; i<32; i++)
|
||||
{
|
||||
if(io & (0x01<<i))
|
||||
{
|
||||
PIN_CONFIG->PINSEL[i] = fun;
|
||||
io &= ~(0x01<<i);
|
||||
if(io == 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PIN_Pullup_Enable(enum _QFN_TYPE_ type, uint32_t io)
|
||||
{
|
||||
uint32_t i;
|
||||
uint32_t *addr32 = (uint32_t *)0x50001080;
|
||||
uint8_t *addr8 = (uint8_t *)0x50001084;
|
||||
uint8_t *p = 0;
|
||||
|
||||
if(type == T_QFN_48)
|
||||
{
|
||||
p = pinmuxtab48;
|
||||
}
|
||||
else if(type == T_QFN_32)
|
||||
{
|
||||
p = pinmuxtab32;
|
||||
}
|
||||
|
||||
if(p!=0)
|
||||
{
|
||||
uint32_t tmp;
|
||||
for(i=0; i<24; i++)
|
||||
{
|
||||
tmp = 0x01<<i;
|
||||
if(io & tmp)
|
||||
{
|
||||
*addr32 |= (0x01 << p[i]);
|
||||
io &= ~tmp;
|
||||
if(io == 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for(i=24; i<32; i++)
|
||||
{
|
||||
tmp = 0x01<<i;
|
||||
if(io & tmp)
|
||||
{
|
||||
*addr8 |= (0x01<<(i-24));
|
||||
io &= ~tmp;
|
||||
if(io == 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PIN_Pullup_Disable(enum _QFN_TYPE_ type, uint32_t io)
|
||||
{
|
||||
uint32_t i;
|
||||
uint32_t *addr32 = (uint32_t *)0x50001080;
|
||||
uint8_t *addr8 = (uint8_t *)0x50001084;
|
||||
uint8_t *p = 0;
|
||||
|
||||
if(type == T_QFN_48)
|
||||
{
|
||||
p = pinmuxtab48;
|
||||
}
|
||||
else if(type == T_QFN_32)
|
||||
{
|
||||
p = pinmuxtab32;
|
||||
}
|
||||
|
||||
if(p!=0)
|
||||
{
|
||||
uint32_t tmp;
|
||||
for(i=0; i<24; i++)
|
||||
{
|
||||
tmp = 0x01<<i;
|
||||
if(io & tmp)
|
||||
{
|
||||
*addr32 &= ~(0x01 << p[i]);
|
||||
io &= ~tmp;
|
||||
if(io == 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for(i=24; i<32; i++)
|
||||
{
|
||||
tmp = 0x01<<i;
|
||||
if(io & tmp)
|
||||
{
|
||||
*addr8 &= ~(0x01<<(i-24));
|
||||
io &= ~tmp;
|
||||
if(io == 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************************
|
||||
功能: GPIO设置为输出(首先要把PIN设为GPIO)
|
||||
参数:
|
||||
uint32_t io 要设置的gpio管脚所在的位
|
||||
举例:GPIO_Set_Output(BIT0|BIT1|BIT2|BIT3); 设置gpio0~gpio3为输出
|
||||
*********************************************************************************/
|
||||
void GPIO_Set_Output(uint32_t io)
|
||||
{
|
||||
GPIO_CTRL->GPIODIR &= ~io;
|
||||
}
|
||||
|
||||
/***************************************************************************************
|
||||
功能: GPIO设置为输入(首先要把PIN设为GPIO)
|
||||
参数:
|
||||
uint32_t io: 要设置的GPIO管脚所在的位
|
||||
uint32_t invert_bits: 该bit所对应的输入是否取反: 0-不取反,1-取反
|
||||
举例:
|
||||
GPIO_Set_Input(BIT0|BIT1|BIT2|BIT3, BIT0|BIT1); //gpio0~3输入,gpio0和gpio1取反
|
||||
***************************************************************************************/
|
||||
void GPIO_Set_Input(uint32_t io, uint32_t invert_bits)
|
||||
{
|
||||
GPIO_CTRL->GPIODIR |= io;
|
||||
GPI_CTRL->GPI_InE |= io;
|
||||
|
||||
GPI_CTRL->GPI_POL &= (~io);
|
||||
GPI_CTRL->GPI_POL |= invert_bits;
|
||||
}
|
||||
|
||||
void GPIO_Set_Input_DIR(uint32_t io)
|
||||
{
|
||||
GPIO_CTRL->GPIODIR |= io;
|
||||
}
|
||||
|
||||
void GPIO_Input_Enable(uint32_t io)
|
||||
{
|
||||
GPI_CTRL->GPI_InE |= io;
|
||||
}
|
||||
|
||||
void GPIO_Input_Disable(uint32_t io)
|
||||
{
|
||||
GPI_CTRL->GPI_InE &= ~io;
|
||||
}
|
||||
|
||||
/*********************************************************************************
|
||||
功能: 管脚输出设置为高电平
|
||||
参数: uint32_t io 要设置的gpio管脚所在的位
|
||||
举例:
|
||||
GPIO_Set_Input(BIT0 | BIT1 | BIT2 | BIT3); 设置gpio0~gpio4输出高电平
|
||||
*********************************************************************************/
|
||||
void GPIO_Pin_Set(uint32_t io)
|
||||
{
|
||||
GPO_CTRL->GPO |= io;
|
||||
}
|
||||
/*********************************************************************************
|
||||
功能: 管脚输出设置为低电平
|
||||
参数: uint32_t io 要设置的gpio管脚所在的位
|
||||
举例:
|
||||
GPIO_Set_Input(BIT0 | BIT1 | BIT2 | BIT3); 设置gpio0~gpio4输出低电平
|
||||
*********************************************************************************/
|
||||
void GPIO_Pin_Clear(uint32_t io)
|
||||
{
|
||||
GPO_CTRL->GPO &= ~io;
|
||||
}
|
||||
|
||||
/*********************************************************************************
|
||||
功能: 设置管脚输出翻转
|
||||
参数: uint32_t io 要设置的gpio管脚所在的位
|
||||
举例:
|
||||
GPIO_Pin_Turn(BIT0 | BIT1 | BIT2 | BIT3); 设置gpio0~gpio4输出取反
|
||||
*/
|
||||
void GPIO_Pin_Turn(uint32_t io)
|
||||
{
|
||||
GPO_CTRL->GPO ^= io;
|
||||
}
|
||||
|
||||
/*********************************************************************************
|
||||
功能: 返回GPIO输入寄存器值
|
||||
参数: 要获取的GPIO所在位
|
||||
返回值:要读回的管脚状态
|
||||
*********************************************************************************/
|
||||
uint32_t GPIO_Pin_Read(uint32_t io)
|
||||
{
|
||||
return GPI_CTRL->GPI & io;
|
||||
}
|
||||
|
||||
/*********************************************************************************
|
||||
功能: GPIO口中断使能函数
|
||||
参数:
|
||||
uint32_t io:要使能的IO口所在的位
|
||||
void * p_callback:GPIO口中断的回调函数
|
||||
*********************************************************************************/
|
||||
void io_irq_enable(uint32_t io, void * p_callback)
|
||||
{
|
||||
// clear interrupt
|
||||
GPIO_IRQ_CTRL->GPIO_INT_CLR = GPIO_IRQ_CTRL->GPIO_INT & io;
|
||||
|
||||
// unmask gpio interrupt
|
||||
GPIO_IRQ_CTRL->GPIO_INT_MSK &= ~io;
|
||||
GPIO_IRQ_CTRL->GPIO_TOP_INT_MSK = 0;
|
||||
gpio_callback = ((void (*)(void))p_callback);
|
||||
|
||||
// enable IRQ
|
||||
NVIC_EnableIRQ(GPIO_IRQn);
|
||||
}
|
||||
|
||||
/*********************************************************************************
|
||||
功能: GPIO口中断禁止函数
|
||||
参数:
|
||||
uint32_t io:要禁止中断的IO口所在的位
|
||||
*********************************************************************************/
|
||||
void io_irq_disable(uint32_t io)
|
||||
{
|
||||
GPIO_IRQ_CTRL->GPIO_INT_MSK |= io;
|
||||
}
|
||||
|
||||
/*********************************************************************************
|
||||
功能: 禁止所有GPIO中断
|
||||
*********************************************************************************/
|
||||
void io_irq_disable_all(void)
|
||||
{
|
||||
NVIC_DisableIRQ(GPIO_IRQn);
|
||||
GPIO_IRQ_CTRL->GPIO_INT_MSK = 0xffffffff;
|
||||
GPIO_IRQ_CTRL->GPIO_TOP_INT_MSK = 1;
|
||||
}
|
||||
|
||||
/********************************************************************************
|
||||
功能: GPIO口中断服务函数
|
||||
备注:调用回调函数
|
||||
*********************************************************************************/
|
||||
void GPIO_IRQHandler(void)
|
||||
{
|
||||
// check interrupt
|
||||
if((GPIO_IRQ_CTRL->GPIO_INT & (~GPIO_IRQ_CTRL->GPIO_INT_MSK)))
|
||||
{
|
||||
if(gpio_callback != 0)
|
||||
(*gpio_callback)();
|
||||
|
||||
// clear interrupt
|
||||
GPIO_IRQ_CTRL->GPIO_INT_CLR = GPIO_IRQ_CTRL->GPIO_INT;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,195 @@
|
|||
#include "i2c.h"
|
||||
#include "gpio.h"
|
||||
#include <string.h>
|
||||
|
||||
//寄存器地址定义
|
||||
static I2C_CTRL_TYPE * I2C_0_CTRL = ((I2C_CTRL_TYPE *) I2C_0_CTRL_BASE);
|
||||
static I2C_TXD_TYPE * I2C_0_TXD = ((I2C_TXD_TYPE *) I2C_0_TXD_BASE);
|
||||
static I2C_RXD_TYPE * I2C_0_RXD = ((I2C_RXD_TYPE *) I2C_0_RXD_BASE);
|
||||
static I2C_CTRL_TYPE * I2C_1_CTRL = ((I2C_CTRL_TYPE *) I2C_1_CTRL_BASE);
|
||||
static I2C_TXD_TYPE * I2C_1_TXD = ((I2C_TXD_TYPE *) I2C_1_TXD_BASE);
|
||||
static I2C_RXD_TYPE * I2C_1_RXD = ((I2C_RXD_TYPE *) I2C_1_RXD_BASE);
|
||||
|
||||
/*****************************************************************************
|
||||
para:
|
||||
i2c : I2C0 or I2C1
|
||||
return : void
|
||||
*****************************************************************************/
|
||||
void i2c_Init(uint8_t i2c,uint32_t clk_io,uint32_t sda_io)
|
||||
{
|
||||
if(i2c == I2C0)
|
||||
{
|
||||
PIN_Set_GPIO(sda_io,PIN_SEL_GPIO);
|
||||
PIN_Pullup_Enable(T_QFN_48, sda_io);
|
||||
|
||||
PIN_Set_GPIO(clk_io,PIN_SEL_I2C_SCL0);
|
||||
PIN_Set_GPIO(sda_io,PIN_SEL_I2C_SDA0);
|
||||
|
||||
I2C_0_CTRL->SLAVE_NACK = 0; // 1:NACK, 0:ACK
|
||||
I2C_0_CTRL->SPEED = 79; //IIC clk = MCU CLK/(2*(speed+1)) SPEED默认值是64
|
||||
}
|
||||
else if(i2c == I2C1)
|
||||
{
|
||||
PIN_Set_GPIO(sda_io,PIN_SEL_GPIO);
|
||||
PIN_Pullup_Enable(T_QFN_48, sda_io);
|
||||
|
||||
PIN_Set_GPIO(clk_io,PIN_SEL_I2C_SCL1);
|
||||
PIN_Set_GPIO(sda_io,PIN_SEL_I2C_SDA1);
|
||||
|
||||
I2C_1_CTRL->SLAVE_NACK = 0; // 1:NACK, 0:ACK
|
||||
I2C_1_CTRL->SPEED = 79; //IIC clk = MCU CLK/(2*(speed+1)) SPEED默认值是64
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************************************************************
|
||||
para:
|
||||
uint8_t id: 7位设备地址
|
||||
uint8_t addr_len: 设备内部存储地址的长度,I2C_2_BYTE_ADDRESS(2字节),I2C_1_BYTE_ADDRESS(1字节)
|
||||
uint8_t * buf: 要发的数据内容
|
||||
uint16_t sz: 要发送的数据长度
|
||||
|
||||
return : SUCCESS or ERROR
|
||||
***************************************************************************************************/
|
||||
ErrorStatus i2c_0_write(uint8_t id, uint8_t addr_len, uint16_t addr, uint8_t * buf, uint16_t sz)
|
||||
{
|
||||
ErrorStatus bret = SUCCESS;
|
||||
|
||||
I2C_0_CTRL->RSTB = 1;
|
||||
I2C_0_CTRL->ID = id;
|
||||
I2C_0_CTRL->ADDR_1 = addr_len;
|
||||
I2C_0_CTRL->ADDRESS = addr;
|
||||
I2C_0_CTRL->DATA_CNT = (sz - 1);
|
||||
|
||||
memcpy((uint8_t *)I2C_0_TXD->TX, buf ,sz);
|
||||
|
||||
I2C_0_CTRL->WRITE = 1;
|
||||
|
||||
while(I2C_0_CTRL->DONE_FLG == 0);
|
||||
I2C_0_CTRL->DONE_FLG = 0;
|
||||
|
||||
if(I2C_0_CTRL->ERR_FLG==1)
|
||||
{
|
||||
I2C_0_CTRL->ERR_FLG = 0;
|
||||
bret = ERROR;
|
||||
}
|
||||
|
||||
I2C_0_CTRL->RSTB = 0; //reset
|
||||
|
||||
return bret;
|
||||
}
|
||||
|
||||
/***************************************************************************************************
|
||||
para:
|
||||
uint8_t id: 7位设备地址
|
||||
uint8_t addr_len: 设备内部存储地址的长度,I2C_2_BYTE_ADDRESS(2字节),I2C_1_BYTE_ADDRESS(1字节)
|
||||
uint8_t * buf: 读取数据的储存区域
|
||||
uint16_t sz: 要读取的数据长度
|
||||
|
||||
return : SUCCESS or ERROR
|
||||
***************************************************************************************************/
|
||||
ErrorStatus i2c_0_read(uint8_t id, uint8_t addr_len, uint16_t addr, uint8_t * buf, uint16_t sz)
|
||||
{
|
||||
ErrorStatus bret = SUCCESS;
|
||||
|
||||
I2C_0_CTRL->RSTB=1;
|
||||
I2C_0_CTRL->ID = id;
|
||||
I2C_0_CTRL->ADDR_1 = addr_len;
|
||||
I2C_0_CTRL->ADDRESS = addr;
|
||||
I2C_0_CTRL->DATA_CNT = (sz - 1);
|
||||
|
||||
I2C_0_CTRL->READ=1;
|
||||
|
||||
while(I2C_0_CTRL->DONE_FLG == 0);
|
||||
I2C_0_CTRL->DONE_FLG = 0;
|
||||
|
||||
if(I2C_0_CTRL->ERR_FLG == 1)
|
||||
{
|
||||
I2C_0_CTRL->ERR_FLG = 0;
|
||||
bret = ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(buf, (uint8_t *)I2C_0_RXD->RX, sz);
|
||||
}
|
||||
|
||||
I2C_0_CTRL->RSTB = 0; //reset
|
||||
|
||||
return bret;
|
||||
}
|
||||
|
||||
/***************************************************************************************************
|
||||
para:
|
||||
uint8_t id: 7位设备地址
|
||||
uint8_t addr_len: 设备内部存储地址的长度,I2C_2_BYTE_ADDRESS(2字节),I2C_1_BYTE_ADDRESS(1字节)
|
||||
uint8_t * buf: 要发的数据内容
|
||||
uint16_t sz: 要发送的数据长度
|
||||
|
||||
return : SUCCESS or ERROR
|
||||
***************************************************************************************************/
|
||||
ErrorStatus i2c_1_write(uint8_t id, uint8_t addr_len, uint16_t addr, uint8_t * buf, uint16_t sz)
|
||||
{
|
||||
ErrorStatus bret = SUCCESS;
|
||||
|
||||
I2C_1_CTRL->RSTB = 1;
|
||||
I2C_1_CTRL->ID = id;
|
||||
I2C_1_CTRL->ADDR_1 = addr_len;
|
||||
I2C_1_CTRL->ADDRESS = addr;
|
||||
I2C_1_CTRL->DATA_CNT = (sz - 1);
|
||||
|
||||
memcpy((uint8_t *)I2C_1_TXD->TX, buf ,sz);
|
||||
|
||||
I2C_1_CTRL->WRITE=1;
|
||||
|
||||
while(I2C_1_CTRL->DONE_FLG == 0);
|
||||
I2C_1_CTRL->DONE_FLG = 0;
|
||||
|
||||
if(I2C_1_CTRL->ERR_FLG==1)
|
||||
{
|
||||
I2C_1_CTRL->ERR_FLG = 0;
|
||||
bret = ERROR;
|
||||
}
|
||||
|
||||
I2C_1_CTRL->RSTB=0; //reset
|
||||
|
||||
return bret;
|
||||
}
|
||||
|
||||
/***************************************************************************************************
|
||||
para:
|
||||
uint8_t id: 7位设备地址
|
||||
uint8_t addr_len: 设备内部存储地址的长度,I2C_2_BYTE_ADDRESS(2字节),I2C_1_BYTE_ADDRESS(1字节)
|
||||
uint8_t * buf: 读取数据的储存区域
|
||||
uint16_t sz: 要读取的数据长度
|
||||
|
||||
return : SUCCESS or ERROR
|
||||
***************************************************************************************************/
|
||||
ErrorStatus i2c_1_read(uint8_t id, uint8_t addr_len, uint16_t addr, uint8_t * buf, uint16_t sz)
|
||||
{
|
||||
ErrorStatus bret = SUCCESS;
|
||||
|
||||
I2C_1_CTRL->RSTB=1;
|
||||
I2C_1_CTRL->ID = id;
|
||||
I2C_1_CTRL->ADDR_1 = addr_len;
|
||||
I2C_1_CTRL->ADDRESS = addr;
|
||||
I2C_1_CTRL->DATA_CNT = (sz - 1);
|
||||
|
||||
I2C_1_CTRL->READ=1;
|
||||
|
||||
while(I2C_1_CTRL->DONE_FLG == 0);
|
||||
I2C_1_CTRL->DONE_FLG = 0;
|
||||
|
||||
if(I2C_1_CTRL->ERR_FLG == 1)
|
||||
{
|
||||
I2C_1_CTRL->ERR_FLG = 0;
|
||||
bret = ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(buf, (uint8_t *)I2C_1_RXD->RX, sz);
|
||||
}
|
||||
|
||||
I2C_1_CTRL->RSTB = 0; //reset
|
||||
|
||||
return bret;
|
||||
}
|
||||
|
|
@ -0,0 +1,307 @@
|
|||
#include "ota.h"
|
||||
#include "lib.h"
|
||||
#include "debug.h"
|
||||
#include <string.h>
|
||||
#include "delay.h"
|
||||
#include "softtimer.h"
|
||||
#include "DebugLog.h"
|
||||
#ifdef _OTA_
|
||||
|
||||
uint8_t ota_state=0;
|
||||
uint8_t ota_writecnt=0;
|
||||
uint16_t ota_section_size=0,ota_receive_size=0,ota_section_check=0,ota_receive_check=0;
|
||||
uint32_t ota_section_offset=0;
|
||||
|
||||
static uint8_t EvtBuf[sizeof(struct hci_evt)];
|
||||
struct hci_evt *pevt = (struct hci_evt *)EvtBuf;
|
||||
__align(4) struct ota_write_info ota_w;
|
||||
#define p_flash_stack ((uint8_t *) 0x2000074a)
|
||||
|
||||
static void EvtCommandComplete(uint8_t opcode, uint8_t *ret_parm, uint8_t ret_len)
|
||||
{
|
||||
pevt->evtcode = EVT_COMMAND_COMPLETE;
|
||||
pevt->evtlen = ret_len + CMD_COMPLETE_HDR_SZ;
|
||||
|
||||
pevt->evtparm.EvtCommandComplete.opcode= opcode;
|
||||
|
||||
memcpy((uint8_t *)&pevt->evtparm.EvtCommandComplete.RetParm, ret_parm, ret_len);
|
||||
}
|
||||
|
||||
void CmdFwErase(void)
|
||||
{
|
||||
struct ret_fw_erase_cmd Ret;
|
||||
|
||||
// DBGPRINTF(("CmdFwErase\r\n"));
|
||||
|
||||
CodeErase();
|
||||
|
||||
Ret.status = ERR_COMMAND_SUCCESS;
|
||||
|
||||
EvtCommandComplete(CMD_FW_ERASE, (uint8_t *)&Ret, sizeof (Ret));
|
||||
}
|
||||
|
||||
static void Cmd4KSETTINGWrite(struct cmd_fw_write *p_cmd)
|
||||
{
|
||||
struct ret_fw_erase_cmd Ret;
|
||||
|
||||
if(p_cmd->offset>4096) return;
|
||||
if((p_cmd->offset+p_cmd->sz)>4096) p_cmd->sz=4096-p_cmd->offset;
|
||||
|
||||
memcpy(p_flash_stack+p_cmd->offset,p_cmd->data,p_cmd->sz);
|
||||
|
||||
DBGPRINTF("cmd4KSETTINGWrite %x %x\r\n",p_cmd->offset,p_cmd->sz);
|
||||
|
||||
Ret.status = ERR_COMMAND_SUCCESS;
|
||||
|
||||
EvtCommandComplete(CMD_FW_WRITE, (uint8_t *)&Ret, sizeof (Ret));
|
||||
}
|
||||
|
||||
static void CmdFwWriteStart(uint8_t status,uint16_t sz,uint16_t checksum)
|
||||
{
|
||||
struct ret_fw_write_start_cmd Ret;
|
||||
Ret.sz=sz;
|
||||
Ret.checksum=checksum;
|
||||
Ret.status=status;
|
||||
|
||||
EvtCommandComplete(CMD_FW_WRITE_START, (uint8_t *)&Ret, sizeof (Ret));
|
||||
}
|
||||
|
||||
static void Cmd4KSETTINGUpgrade(struct cmd_4ksetting_upgrade *p_cmd)
|
||||
{
|
||||
struct ret_fw_erase_cmd Ret;
|
||||
|
||||
uint8_t temp = 0;
|
||||
temp = Setting4kUpdate(p_flash_stack,p_cmd->checksum,p_cmd->Xor);
|
||||
#if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
DBGPRINTF("CodeUpdate=%d %x %x\r\n", temp,p_cmd->checksum,p_cmd->Xor);
|
||||
#endif
|
||||
|
||||
if(temp==0)
|
||||
Ret.status = ERR_COMMAND_FAILED;
|
||||
else
|
||||
{
|
||||
Ret.status = ERR_COMMAND_SUCCESS;
|
||||
}
|
||||
|
||||
EvtCommandComplete(CMD_FW_UPGRADE, (uint8_t *)&Ret, sizeof (Ret));
|
||||
|
||||
}
|
||||
|
||||
static void CmdFwUpgradev20(struct cmd_fw_upgrade_V20 *p_cmd)
|
||||
{
|
||||
struct ret_fw_erase_cmd Ret;
|
||||
uint8_t temp = 0;
|
||||
|
||||
if(ota_w.idx != 0)
|
||||
CodeWrite(ota_w.cnt*32, ota_w.idx, ota_w.buf);
|
||||
|
||||
#if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
DBGPRINTF("ota sz:%x checksum:%x ",p_cmd->sz,p_cmd->checksum);
|
||||
#endif
|
||||
temp = CodeUpdate(NULL, NULL, p_cmd->sz, p_cmd->checksum);
|
||||
#if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
DBGPRINTF("CodeUpdatev20=%d\r\n", temp);
|
||||
#endif
|
||||
|
||||
if(temp==0)
|
||||
Ret.status = ERR_COMMAND_FAILED;
|
||||
else
|
||||
Ret.status = ERR_COMMAND_SUCCESS;
|
||||
|
||||
EvtCommandComplete(CMD_FW_UPGRADE, (uint8_t *)&Ret, sizeof (Ret));
|
||||
}
|
||||
|
||||
static void CmdFlashdataUpgradev30(struct cmd_fw_upgrade_V20 *p_cmd)
|
||||
{
|
||||
struct ret_fw_erase_cmd Ret;
|
||||
uint8_t temp = 0;
|
||||
|
||||
#if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
DBGPRINTF("ota sz:%x checksum:%x ",p_cmd->sz,p_cmd->checksum);
|
||||
#endif
|
||||
temp = FlashDataUpdate(p_cmd->sz, p_cmd->checksum);
|
||||
#if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
DBGPRINTF("CodeUpdatev30=%d\r\n", temp);
|
||||
#endif
|
||||
|
||||
if(temp==0)
|
||||
Ret.status = ERR_COMMAND_FAILED;
|
||||
else
|
||||
Ret.status = ERR_COMMAND_SUCCESS;
|
||||
|
||||
EvtCommandComplete(CMD_FW_UPGRADE, (uint8_t *)&Ret, sizeof (Ret));
|
||||
}
|
||||
|
||||
void ota_cmd(uint8_t *p_cmd, uint8_t sz)
|
||||
{
|
||||
struct hci_cmd *pcmd = (struct hci_cmd*)p_cmd;
|
||||
if((ota_section_size==0) || (ota_state==0))
|
||||
{
|
||||
switch(pcmd->opcode)
|
||||
{
|
||||
case CMD_FW_ERASE:
|
||||
BLSetConnectionUpdate(0); //ota
|
||||
#if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
dbg_printf("CMD_FW_ERASE\r\n");
|
||||
#endif
|
||||
//CmdFwErase();//最好放到main函数while(1)处理
|
||||
Timer_Evt_Start(EVT_1S_OTA);
|
||||
ota_state=1;
|
||||
ota_section_offset=0;
|
||||
break;
|
||||
case CMD_4KSETTING_ERASE:
|
||||
BLSetConnectionUpdate(0); //ota
|
||||
#if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
dbg_printf("4KSETTING_ERASE \r\n");
|
||||
#endif
|
||||
ota_state=4;
|
||||
ota_section_offset=0;
|
||||
break;
|
||||
case CMD_4KSETTING_WRITE:
|
||||
Cmd4KSETTINGWrite(&pcmd->cmdparm.CmdFwWrite);
|
||||
ota_state=5;
|
||||
break;
|
||||
case CMD_4KSETTING_UPGRADE:
|
||||
Cmd4KSETTINGUpgrade(&pcmd->cmdparm.Cmd4ksettingUpgrade);
|
||||
ota_state=6;
|
||||
break;
|
||||
case CMD_FW_UPGRADEV20:
|
||||
CmdFwUpgradev20(&pcmd->cmdparm.CmdFwUpgradeV20);
|
||||
ota_state=3;
|
||||
break;
|
||||
case CMD_FW_WRITE_START:
|
||||
ota_section_size=pcmd->cmdparm.CmdFwWriteStart.sz;
|
||||
ota_section_check=pcmd->cmdparm.CmdFwWriteStart.checksum;
|
||||
ota_section_offset=pcmd->cmdparm.CmdFwWriteStart.offset;
|
||||
ota_receive_size=0;
|
||||
ota_receive_check=0;
|
||||
#if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
dbg_printf("CMD_FW_WRITE_START offset:%x,size:%x checksum:%x\r\n",pcmd->cmdparm.CmdFwWriteStart.offset,ota_section_size,pcmd->cmdparm.CmdFwWriteStart.checksum);
|
||||
#endif
|
||||
break;
|
||||
case CMD_FLASHDATA_ERASE:
|
||||
BLSetConnectionUpdate(0); //ota
|
||||
#if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
dbg_printf("FLASHDATA_ERASE \r\n");
|
||||
#endif
|
||||
//EraseFlashData(0, 30); //假设FLASHDATA区域大小为120KB扇区数目为120KB/4KB=30 这里擦除需要比较长的时间,所以APP那端要延时一下
|
||||
Timer_Evt_Start(EVT_1S_OTA);
|
||||
ota_state=7;
|
||||
ota_section_offset=0;
|
||||
break;
|
||||
case CMD_FLASHDATA_START:
|
||||
ota_section_size=pcmd->cmdparm.CmdFwWriteStart.sz;
|
||||
ota_section_check=pcmd->cmdparm.CmdFwWriteStart.checksum;
|
||||
ota_section_offset=pcmd->cmdparm.CmdFwWriteStart.offset;
|
||||
ota_receive_size=0;
|
||||
ota_receive_check=0;
|
||||
#if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
dbg_printf("FLASHDATA_WRITE_START offset:%x,size:%x checksum:%x\r\n",pcmd->cmdparm.CmdFwWriteStart.offset,ota_section_size,pcmd->cmdparm.CmdFwWriteStart.checksum);
|
||||
#endif
|
||||
break;
|
||||
case CMD_FLASHDATA_UPGRADEV30:
|
||||
CmdFlashdataUpgradev30(&pcmd->cmdparm.CmdFwUpgradeV20);
|
||||
ota_state=3;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
uint16_t idx;
|
||||
ota_writecnt =0;
|
||||
|
||||
for(idx=0; idx < sz ; idx++)
|
||||
ota_receive_check += p_cmd[idx];
|
||||
|
||||
if(((int)p_cmd % 4)!=0)
|
||||
{
|
||||
memcpy(ota_w.buf,p_cmd,sz);
|
||||
if((ota_state==2) || (ota_state==1))
|
||||
{
|
||||
idx=CodeWrite(ota_section_offset+ota_receive_size, sz, ota_w.buf);
|
||||
// #if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
// dbg_printf("CodeWrite noalign:%x %x",ota_section_offset+ota_receive_size,idx);
|
||||
// #endif
|
||||
}
|
||||
else if((ota_state==8) || (ota_state==7))
|
||||
{
|
||||
WriteFlashData(ota_section_offset+ota_receive_size, sz, ota_w.buf);
|
||||
// #if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
// dbg_printf("FlashData noalign ");
|
||||
// #endif
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if((ota_state==2) || (ota_state==1))
|
||||
{
|
||||
idx=CodeWrite(ota_section_offset+ota_receive_size, sz, p_cmd);
|
||||
// #if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
// dbg_printf("CodeWrite align:%x %x",ota_section_offset+ota_receive_size,idx);
|
||||
// #endif
|
||||
}
|
||||
else if((ota_state==8) || (ota_state==7))
|
||||
{
|
||||
WriteFlashData(ota_section_offset+ota_receive_size, sz, p_cmd);
|
||||
// #if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
// dbg_printf("FlashData align ");
|
||||
// #endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// #if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
// dbg_printf("section_offset:%x receive_size:%x section_size:%x\r\n",ota_section_offset,ota_receive_size,ota_section_size);
|
||||
// #endif
|
||||
|
||||
ota_receive_size +=sz;
|
||||
if(ota_receive_size>=ota_section_size)
|
||||
{
|
||||
if(ota_receive_check==ota_section_check)
|
||||
{
|
||||
CmdFwWriteStart(ERR_COMMAND_SUCCESS,ota_receive_size,ota_receive_check);
|
||||
#if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
dbg_printf("section OK! ");
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
CmdFwWriteStart(ERR_COMMAND_FAILED,ota_receive_size,ota_receive_check);
|
||||
#if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
dbg_printf("section faile! ");
|
||||
#endif
|
||||
}
|
||||
#if defined(_DEBUG_) || defined(_SYD_RTT_DEBUG_)
|
||||
dbg_printf("ota_receive_check:%x ota_section_check:%x\r\n",ota_receive_check,ota_section_check);
|
||||
#endif
|
||||
|
||||
ota_variable_clear(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ota_variable_clear(uint8_t all)
|
||||
{
|
||||
if(all)
|
||||
{
|
||||
ota_state=0;
|
||||
ota_writecnt=0;
|
||||
}
|
||||
ota_section_size=0;
|
||||
ota_section_check=0;
|
||||
ota_receive_size=0;
|
||||
ota_receive_check=0;
|
||||
ota_section_offset=0;
|
||||
}
|
||||
|
||||
void ota_rsp(uint8_t *p_rsp, uint8_t *p_sz)
|
||||
{
|
||||
memcpy(p_rsp, EvtBuf, pevt->evtlen + 2);
|
||||
|
||||
*p_sz = pevt->evtlen + 2;
|
||||
|
||||
memset(EvtBuf,0,sizeof(EvtBuf));
|
||||
}
|
||||
#endif
|
||||
|
|
@ -0,0 +1,250 @@
|
|||
#include "pwm.h"
|
||||
#include "gpio.h"
|
||||
#include "debug.h"
|
||||
|
||||
//static PWM_CTRL_TYPE * PWM_0_CTRL = ((PWM_CTRL_TYPE *) PWM_0_CTRL_BASE);
|
||||
//static PWM_CTRL_TYPE * PWM_1_CTRL = ((PWM_CTRL_TYPE *) PWM_1_CTRL_BASE);
|
||||
//static PWM_CTRL_TYPE * PWM_2_CTRL = ((PWM_CTRL_TYPE *) PWM_2_CTRL_BASE);
|
||||
//static HPWM_CTRL_TYPE * HPWM_CTRL = ((HPWM_CTRL_TYPE *) HPWM_CTRL_BASE);
|
||||
|
||||
#define PWM_0_CTRL ((PWM_CTRL_TYPE *)PWM_0_CTRL_BASE)
|
||||
#define PWM_1_CTRL ((PWM_CTRL_TYPE *)PWM_1_CTRL_BASE)
|
||||
#define PWM_2_CTRL ((PWM_CTRL_TYPE *)PWM_2_CTRL_BASE)
|
||||
#define HPWM_CTRL ((HPWM_CTRL_TYPE *)HPWM_CTRL_BASE)
|
||||
|
||||
/*
|
||||
pwm0 初始化函数
|
||||
参数: PWM_x pwm_x 要初始化的pwm模块 参见PWM_MODE枚举
|
||||
PWM_PARAMETER pwm_parameter pwm参数 参见PWM_PARAMETER结构体
|
||||
举例:
|
||||
PWM_PARAMETER pwm_parameter;
|
||||
//Flash mode
|
||||
pwm_parameter.MODE=flash_mode;
|
||||
pwm_parameter.T1=0x10; //16 * 1/32=0.5s 实测0.768563250
|
||||
pwm_parameter.T2=0x20; //32 * 1/32=1s 实测1.585201375
|
||||
pwm_parameter.T3=0x0000;
|
||||
pwm_parameter.N1=0x80; //永久重复,没有T3
|
||||
pwm_parameter.N2=0x80; //永久重复
|
||||
pwm_enable(PWM_0,pwm_parameter);
|
||||
*/
|
||||
void pwm_enable(PWM_x pwm_x,PWM_PARAMETER pwm_parameter)
|
||||
{
|
||||
PWM_CTRL_TYPE * PWM_CTRL = 0;
|
||||
switch(pwm_x)
|
||||
{
|
||||
case PWM_0:
|
||||
PWM_CTRL = PWM_0_CTRL;
|
||||
break;
|
||||
case PWM_1:
|
||||
PWM_CTRL = PWM_1_CTRL;
|
||||
break;
|
||||
default:
|
||||
PWM_CTRL = PWM_2_CTRL;
|
||||
break;
|
||||
}
|
||||
|
||||
PWM_CTRL->MODE= pwm_parameter.MODE;
|
||||
switch(pwm_parameter.MODE)
|
||||
{
|
||||
case pwm_mode:
|
||||
PWM_CTRL->PWM_M = pwm_parameter.PWM_M;
|
||||
PWM_CTRL->PWM_N = pwm_parameter.PWM_N;
|
||||
break;
|
||||
case flash_mode:
|
||||
PWM_CTRL->T1 = pwm_parameter.T1;
|
||||
PWM_CTRL->T2 = pwm_parameter.T2;
|
||||
PWM_CTRL->T3 = pwm_parameter.T3;
|
||||
PWM_CTRL->N1 = pwm_parameter.N1;
|
||||
PWM_CTRL->N2 = pwm_parameter.N2;
|
||||
break;
|
||||
case breath_mode:
|
||||
PWM_CTRL->BR_TH_MAX = pwm_parameter.BR_TH_MAX;
|
||||
PWM_CTRL->BR_TH_MIN = pwm_parameter.BR_TH_MIN;
|
||||
PWM_CTRL->BR_SP = pwm_parameter.BR_SP;
|
||||
PWM_CTRL->T4 = pwm_parameter.T4;
|
||||
|
||||
break;
|
||||
}
|
||||
PWM_CTRL->PAUS= 1;
|
||||
PWM_CTRL->LED_PE= 1;
|
||||
}
|
||||
|
||||
void pwm_disable(PWM_x pwm_x)
|
||||
{
|
||||
switch(pwm_x){
|
||||
case PWM_0 :
|
||||
PWM_0_CTRL->LED_PE= 0;
|
||||
break;
|
||||
case PWM_1 :
|
||||
PWM_1_CTRL->LED_PE= 0;
|
||||
break;
|
||||
case PWM_2 :
|
||||
PWM_2_CTRL->LED_PE= 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
HPWM 初始化函数
|
||||
参数:
|
||||
HPWM_PARAMETER *parameter, 参见HPWM_PARAMETER结构体
|
||||
*/
|
||||
void Hpwm_Init(HPWM_PARAMETER *parameter)
|
||||
{
|
||||
Hpwm_Stop();
|
||||
|
||||
/*设计时钟频率64M*/
|
||||
HPWM_CTRL->PRESCALER = parameter->prescaler;
|
||||
HPWM_CTRL->MODE = parameter->mode;
|
||||
HPWM_CTRL->COUNTER_TOP = parameter->period;
|
||||
|
||||
//dbg_printf("0:POLARITY = 0x%08x\r\n",HPWM_CTRL->POLARITY);
|
||||
switch(parameter->channel)
|
||||
{
|
||||
case HPWM_CH0:
|
||||
//PIN_CONFIG->PIN_3_SEL = PIN_SEL_HPWM_CH0;
|
||||
HPWM_CTRL->CMP_CH0 = parameter->duty;
|
||||
if(parameter->polarity == LOW_FIRST)
|
||||
HPWM_CTRL->POLARITY &= 0x0e;
|
||||
else
|
||||
HPWM_CTRL->POLARITY |= 0x01;
|
||||
|
||||
break;
|
||||
case HPWM_CH1:
|
||||
//PIN_CONFIG->PIN_4_SEL = PIN_SEL_HPWM_CH1;
|
||||
HPWM_CTRL->CMP_CH1 = parameter->duty;
|
||||
if(parameter->polarity == LOW_FIRST)
|
||||
HPWM_CTRL->POLARITY &= 0x0d;
|
||||
else
|
||||
HPWM_CTRL->POLARITY |= 0x02;
|
||||
|
||||
break;
|
||||
case HPWM_CH2:
|
||||
//PIN_CONFIG->PIN_5_SEL = PIN_SEL_HPWM_CH2;
|
||||
HPWM_CTRL->CMP_CH2 = parameter->duty;
|
||||
if(parameter->polarity == LOW_FIRST)
|
||||
HPWM_CTRL->POLARITY &= 0x0b;
|
||||
else
|
||||
HPWM_CTRL->POLARITY |= 0x04;
|
||||
|
||||
break;
|
||||
default:
|
||||
//PIN_CONFIG->PIN_6_SEL = PIN_SEL_HPWM_CH3;
|
||||
HPWM_CTRL->CMP_CH3 = parameter->duty;
|
||||
if(parameter->polarity == LOW_FIRST)
|
||||
HPWM_CTRL->POLARITY &= 0x07;
|
||||
else
|
||||
HPWM_CTRL->POLARITY |= 0x08;
|
||||
|
||||
break;
|
||||
}
|
||||
//dbg_printf("1:POLARITY = 0x%08x\r\n",HPWM_CTRL->POLARITY);
|
||||
#if 0
|
||||
dbg_printf("CMP_CH0 = %d\r\n",HPWM_CTRL->CMP_CH0);
|
||||
dbg_printf("CMP_CH1 = %d\r\n",HPWM_CTRL->CMP_CH1);
|
||||
dbg_printf("CMP_CH2 = %d\r\n",HPWM_CTRL->CMP_CH2);
|
||||
dbg_printf("CMP_CH3 = %d\r\n",HPWM_CTRL->CMP_CH3);
|
||||
#endif
|
||||
|
||||
HPWM_CTRL->EVENTS = HPWM_INT_ALL; //clr INT
|
||||
HPWM_CTRL->INTEN = HPWM_INT_ALL;
|
||||
//HPWM_CTRL->INTEN = HPWM_INT_PERIOD;
|
||||
//HPWM_CTRL->INTEN = HPWM_INT_TASK_STOP;
|
||||
//HPWM_CTRL->INTEN = HPWM_INT_NONE;
|
||||
|
||||
//NVIC_EnableIRQ(HPWM_IRQn); 安装原来的驱动方式,会在HPWM_IRQHandler中断函数中动态修改占空比,这样不方便测试的时候看结果,这里把中断关闭后,显示的波形就是代码设置的波形
|
||||
}
|
||||
|
||||
void Hpwm_Set_duty(HPWM_CHx channels, uint16_t compare)
|
||||
{
|
||||
if(HPWM_CTRL->MODE == UP_MODE)
|
||||
{
|
||||
if(compare >= HPWM_CTRL->COUNTER_TOP) //0 <= compare <= COUNTER_TOP-1
|
||||
{
|
||||
compare = HPWM_CTRL->COUNTER_TOP - 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(compare >= HPWM_CTRL->COUNTER_TOP-1)
|
||||
{
|
||||
compare = HPWM_CTRL->COUNTER_TOP-2; //0 <= compare <= COUNTER_TOP-2
|
||||
}
|
||||
}
|
||||
|
||||
if(channels&HPWM_CH0)
|
||||
{
|
||||
HPWM_CTRL->CMP_CH0 = compare;
|
||||
}
|
||||
if(channels&HPWM_CH1)
|
||||
{
|
||||
HPWM_CTRL->CMP_CH1 = compare;
|
||||
}
|
||||
if(channels&HPWM_CH2)
|
||||
{
|
||||
HPWM_CTRL->CMP_CH2 = compare;
|
||||
}
|
||||
if(channels&HPWM_CH3)
|
||||
{
|
||||
HPWM_CTRL->CMP_CH3 = compare;
|
||||
}
|
||||
}
|
||||
|
||||
//HPWM有4个通道,但这4个通道共用1个周期计数器
|
||||
void Hpwm_Set_period(uint16_t period)
|
||||
{
|
||||
if(period >= HPWM_CTRL->CMP_CH0)
|
||||
{
|
||||
HPWM_CTRL->COUNTER_TOP = period;
|
||||
}
|
||||
}
|
||||
|
||||
void Hpwm_Set_mode(HPWM_mode mode)
|
||||
{
|
||||
HPWM_CTRL->MODE = mode;
|
||||
}
|
||||
|
||||
void Hpwm_Start(void)
|
||||
{
|
||||
HPWM_CTRL->START = 1;
|
||||
}
|
||||
|
||||
void Hpwm_Stop(void)
|
||||
{
|
||||
HPWM_CTRL->STOP = 1;
|
||||
}
|
||||
|
||||
void HPWM_IRQHandler(void)
|
||||
{
|
||||
//动态设置占空比
|
||||
static uint16_t compare = 1;
|
||||
static uint32_t t = 0;
|
||||
|
||||
dbg_printf("HPWM_IRQHandler\r\n");
|
||||
|
||||
if(HPWM_CTRL->EVENTS & HPWM_INT_PERIOD)
|
||||
{
|
||||
compare++;
|
||||
if(compare == HPWM_CTRL->COUNTER_TOP)
|
||||
{
|
||||
compare = 1;
|
||||
|
||||
dbg_printf("COUNTER_TOP Can stop pwm there!\r\n");
|
||||
// t++;
|
||||
// if(t == 4)
|
||||
// {
|
||||
// Hpwm_Stop();
|
||||
// return;
|
||||
// }
|
||||
}
|
||||
Hpwm_Set_duty(HPWM_CHALL, compare);
|
||||
}
|
||||
|
||||
if(HPWM_CTRL->EVENTS & HPWM_INT_TASK_STOP)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
HPWM_CTRL->EVENTS = HPWM_INT_ALL; //clr INT
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
#include "queue.h"
|
||||
|
||||
void queue_init(QUEUE * queue, uint8_t *buf, int size)
|
||||
{
|
||||
queue->head = queue->tail = 0;
|
||||
queue->size = size;
|
||||
queue->data = buf;
|
||||
}
|
||||
|
||||
void queue_reset(QUEUE * queue)
|
||||
{
|
||||
queue->head = queue->tail;
|
||||
}
|
||||
|
||||
int enqueue(QUEUE * queue, uint8_t data)
|
||||
{
|
||||
queue->data[queue->tail++] = data;
|
||||
if (queue->tail >= queue->size)
|
||||
queue->tail = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dequeue(QUEUE * queue, uint8_t * data)
|
||||
{
|
||||
if (queue->head == queue->tail)
|
||||
return 0;
|
||||
|
||||
*data = queue->data[queue->head++];
|
||||
if (queue->head >= queue->size)
|
||||
queue->head = 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int is_queue_empty(QUEUE * queue)
|
||||
{
|
||||
return queue->head == queue->tail;
|
||||
}
|
||||
|
||||
int queue_size(QUEUE * queue)
|
||||
{
|
||||
int extra = 0;
|
||||
if (queue->head > queue->tail) {
|
||||
extra = queue->size;
|
||||
}
|
||||
return (queue->tail + extra - queue->head);
|
||||
}
|
|
@ -0,0 +1,187 @@
|
|||
#include "rtc.h"
|
||||
#include "debug.h"
|
||||
#include "delay.h"
|
||||
|
||||
static RTC_CTRL_TYPE * RTC_CTRL = ((RTC_CTRL_TYPE *) RTC_CTRL_BASE);
|
||||
RTC_IRQ_CALLBACK irq_cb;
|
||||
static bool enabled = false;
|
||||
|
||||
void rtc_int_clear(RTC_INT_TYPE type)
|
||||
{
|
||||
RTC_CTRL->EVENTS_CLR |= type;
|
||||
}
|
||||
|
||||
//可以动态设置RTC中断
|
||||
void rtc_int_enable(RTC_INT_TYPE type)
|
||||
{
|
||||
RTC_CTRL->EVENTS_EN |= type;
|
||||
}
|
||||
|
||||
//可以动态设置RTC中断
|
||||
void rtc_int_disable(RTC_INT_TYPE type)
|
||||
{
|
||||
RTC_CTRL->EVENTS_EN &= ~type;
|
||||
}
|
||||
|
||||
void rtc_start(void)
|
||||
{
|
||||
if(enabled == false)
|
||||
{
|
||||
RTC_CTRL->START = 1;
|
||||
while (RTC_CTRL->START);
|
||||
//RTC_CTRL->LOAD = 1; //load alarm
|
||||
enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* This will reset RTC counter for prescaler*/
|
||||
void rtc_stop(void)
|
||||
{
|
||||
if(enabled == true)
|
||||
{
|
||||
RTC_CTRL->STOP = 1;
|
||||
while (RTC_CTRL->STOP);
|
||||
enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool rtc_status(void)
|
||||
{
|
||||
return enabled;
|
||||
}
|
||||
|
||||
void rtc_clear(void)
|
||||
{
|
||||
/* This clear COUNTER & CALENDAR */
|
||||
RTC_CTRL->CLEAR = 1;
|
||||
while (RTC_CTRL->CLEAR);
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
功能: 设置RTC的tick分频
|
||||
注意:
|
||||
1.必须在停止状态才能设置,即先调用rtc_stop()函数
|
||||
2.必须保证second时间准确,通过SECONDS_BIT
|
||||
************************************************************************/
|
||||
void rtc_set_prescaler(uint32_t tick, uint8_t adjust_seconds_bit)
|
||||
{
|
||||
RTC_CTRL->PRESCALER = (tick - 1) & 0x7FFF;
|
||||
RTC_CTRL->SECONDS_BIT = adjust_seconds_bit & 0x0f;
|
||||
}
|
||||
|
||||
RTC_TIME_TYPE rtc_get_alarm(int id)
|
||||
{
|
||||
__IO uint32_t * compare = id ? &RTC_CTRL->COMPARE1 : &RTC_CTRL->COMPARE0;
|
||||
int i;
|
||||
RTC_TIME_TYPE time;
|
||||
|
||||
time.u32 = *compare;
|
||||
|
||||
// time format to decimal
|
||||
for (i=0 ; i<3; i++)
|
||||
{
|
||||
uint8_t tmp = time.u8[i];
|
||||
time.u8[i] = (tmp >> 4) * 10;
|
||||
time.u8[i] += (tmp & 0xF);
|
||||
}
|
||||
|
||||
return time;
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
功能: 设置RTC闹钟
|
||||
参数:
|
||||
id : 0-选择闹钟0, 1 - 选择闹钟1
|
||||
************************************************************************/
|
||||
void rtc_set_alarm(int id, RTC_TIME_TYPE *time)
|
||||
{
|
||||
__IO uint32_t * compare = id ? &RTC_CTRL->COMPARE1 : &RTC_CTRL->COMPARE0;
|
||||
int i;
|
||||
|
||||
// Decimal to time format
|
||||
for (i=0; i<3; i++)
|
||||
{
|
||||
uint8_t tmp = time->u8[i];
|
||||
time->u8[i] = (tmp / 10) << 4;
|
||||
time->u8[i] |= tmp % 10;
|
||||
}
|
||||
*compare = time->u32;
|
||||
|
||||
delay_us(100); //64M - delay 38us
|
||||
|
||||
RTC_CTRL->LOAD = 1; //load alarm
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
功能: 删除RTC闹钟
|
||||
参数:
|
||||
id : 0-选择闹钟0, 1 - 选择闹钟1
|
||||
*****************************************************************************/
|
||||
void rtc_delete_alarm(int id)
|
||||
{
|
||||
__IO uint32_t * compare = id ? &RTC_CTRL->COMPARE1 : &RTC_CTRL->COMPARE0;
|
||||
|
||||
*compare = 0x00ffffff;
|
||||
RTC_CTRL->LOAD = 1; //load alarm
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
功能: 设置RTC时间,并启动RTC
|
||||
注意:
|
||||
先停止RTC,再设置相关寄存器,再启动RTC
|
||||
************************************************************************/
|
||||
void rtc_set_calendar(RTC_TIME_TYPE *time)
|
||||
{
|
||||
int i;
|
||||
//rtc_stop();
|
||||
|
||||
// Decimal to time format
|
||||
for (i=0; i<3; i++)
|
||||
{
|
||||
uint8_t tmp = time->u8[i];
|
||||
time->u8[i] = (tmp / 10) << 4;
|
||||
time->u8[i] |= tmp % 10;
|
||||
}
|
||||
|
||||
RTC_CTRL->CALENDAR_INI = time->u32;
|
||||
|
||||
//rtc_start();
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
功能: 获取当前的日历时间
|
||||
************************************************************************/
|
||||
RTC_TIME_TYPE rtc_get_calendar(void)
|
||||
{
|
||||
int i;
|
||||
RTC_TIME_TYPE time;
|
||||
|
||||
time.u32 = RTC_CTRL->CALENDAR;
|
||||
|
||||
// time format to decimal
|
||||
for (i = 0 ; i < 3; i++)
|
||||
{
|
||||
uint8_t tmp = time.u8[i];
|
||||
time.u8[i] = (tmp >> 4) * 10;
|
||||
time.u8[i] += (tmp & 0xF);
|
||||
}
|
||||
|
||||
return time;
|
||||
}
|
||||
|
||||
|
||||
void RTC_IRQHandler(void)
|
||||
{
|
||||
RTC_INT_TYPE status = (RTC_INT_TYPE)RTC_CTRL->EVENTS;
|
||||
rtc_int_clear(status);
|
||||
|
||||
if(status)
|
||||
{
|
||||
if(irq_cb)
|
||||
{
|
||||
irq_cb(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
*定时器事件:
|
||||
*SYD8821 2018-4-13
|
||||
*SYD-TEK
|
||||
*
|
||||
*/
|
||||
#define _RTC_HANDLER_C_
|
||||
#include "softtimer.h"
|
||||
|
||||
struct SYD_sysRtc syd_rtc[RTCEVT_NUM] = {0};
|
||||
struct SYD_HISTORY_SETTING syd_history_setting_str;
|
||||
|
||||
#ifdef RTCEVT_185S
|
||||
void RTC_Evt_185s(void)
|
||||
{
|
||||
RTC_EVT|=RTCEVT_185S;
|
||||
}
|
||||
#endif
|
||||
|
||||
void RTC_Evt_List(void)
|
||||
{
|
||||
#ifdef RTCEVT_185S
|
||||
|
||||
RTC_EVT_Creat(RTCEVT_185S,185,RTC_Evt_185s,RTCEVT_DISABLE_MODE); //生成触发事件
|
||||
|
||||
RTC_EVT_Start(RTCEVT_185S);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
|
@ -0,0 +1,859 @@
|
|||
#include <string.h>
|
||||
#include "atr_decoder.h"
|
||||
#include "debug.h"
|
||||
#include "queue.h"
|
||||
#include "sc_reader.h"
|
||||
#include "tpdu.h"
|
||||
#include "gpio.h"
|
||||
#include <math.h>
|
||||
#include "lib.h"
|
||||
|
||||
static SC_READER_CTRL_TYPE * SC_CTRL = ((SC_READER_CTRL_TYPE *)SC_7816_CTRL_BASE);
|
||||
|
||||
// bit mask for deferred task
|
||||
enum SC_DEFERRED_TASK_MASK {
|
||||
CARD_DET_TASK = U32BIT(0), //card insert or remove
|
||||
ATR_DONE_TASK = U32BIT(1),
|
||||
ATR_FAIL_TASK = U32BIT(2),
|
||||
DEACTIVATION_TASK = U32BIT(3),
|
||||
};
|
||||
|
||||
// TPDU status
|
||||
typedef enum {
|
||||
NOT_READY,
|
||||
PPS_REQUESTED,
|
||||
ACTIVATED,
|
||||
} TASK_STATE;
|
||||
|
||||
// Module interrupt mask
|
||||
enum SC_INT_MASK {
|
||||
TX_DONE = U32BIT(0),
|
||||
TX_FIFO_EMPTY = U32BIT(1),
|
||||
TX_FIFO_THRESHOLD = U32BIT(2),
|
||||
TX_PARITY_ERROR = U32BIT(3),
|
||||
TX_RETRY = U32BIT(4),
|
||||
TX_RETRY_OVER_LIMIT = U32BIT(5),
|
||||
RX_FIFO_NOT_EMPTY = U32BIT(6),
|
||||
RX_FIFO_THRESHOLD = U32BIT(7),
|
||||
RX_TIMEOUT = U32BIT(8), //rx fifo empty timeout
|
||||
RX_PARITY_ERROR = U32BIT(9),
|
||||
RX_RETRY = U32BIT(10),
|
||||
RX_RETRY_OVER_LIMIT = U32BIT(11),
|
||||
C2C_WAITING_TIMEOUT = U32BIT(12), //character to character overtime interupt
|
||||
ATR_DONE = U32BIT(13),
|
||||
ATR_FAIL = U32BIT(14),
|
||||
CARD_DETECT = U32BIT(15),
|
||||
CARD_REMOVE = U32BIT(16),
|
||||
DEACTIVATE_DONE = U32BIT(17),
|
||||
SC_INT_ALL = 0x3FFFF,
|
||||
};
|
||||
|
||||
/* Critical section for SC */
|
||||
#define SC_CS_IN() \
|
||||
do{ \
|
||||
NVIC_DisableIRQ(ISO7816_IRQn); \
|
||||
} while(false)
|
||||
#define SC_CS_OUT() \
|
||||
do{ \
|
||||
NVIC_EnableIRQ(ISO7816_IRQn); \
|
||||
} while(false)
|
||||
|
||||
/* Define which interrupts are used for TX/RX */
|
||||
//#define RX_FIFO_INT (RX_FIFO_THRESHOLD | RX_TIMEOUT)
|
||||
#define RX_FIFO_INT (RX_FIFO_THRESHOLD | C2C_WAITING_TIMEOUT | RX_TIMEOUT)
|
||||
#define TX_FIFO_INT (TX_FIFO_EMPTY)
|
||||
|
||||
//表格来自7816-3协议Table 8和Table 9
|
||||
static const uint32_t F_Table[16] = {372, 372, 558, 744, 1116, 1488, 1860, 0, 0, 512, 768, 1024, 1536, 2048};
|
||||
static const uint32_t D_Table[16] = {0, 1, 2, 4, 8, 16, 32, 64, 12, 20};
|
||||
|
||||
static volatile uint32_t task_flag;
|
||||
static volatile bool tx_finished;
|
||||
static bool card_in = false;
|
||||
static SC_ATR ATR;
|
||||
static SC_PPS PPS;
|
||||
static bool card_activated = false;
|
||||
static bool card_clock_stoppable = false;
|
||||
static CALL_BACK callback[CALL_BACK_NUM];
|
||||
static PPS_CALLBACK pps_cb[PPS_CALL_BACK_NUM];
|
||||
static uint8_t buffer[264];
|
||||
static QUEUE sc_queue = {0, 0, sizeof(buffer), buffer};
|
||||
|
||||
__STATIC_INLINE bool get_byte(uint8_t *data);
|
||||
__STATIC_INLINE void sc_reader_enable_retry(void);
|
||||
|
||||
/* Configure module auto activation when card being inserted */
|
||||
void sc_reader_config_auto(bool en)
|
||||
{
|
||||
SC_CTRL->AUTO_ACTIVATION_EN = (en != 0);
|
||||
}
|
||||
|
||||
/* Configure vcc output when activated */
|
||||
void sc_reader_config_vcc_level(bool en)
|
||||
{
|
||||
SC_CTRL->ACTIVE_VCC_LEVEL = (en != 0);
|
||||
}
|
||||
|
||||
/* Configure module output clock */
|
||||
void sc_reader_config_clock_div(uint16_t div)
|
||||
{
|
||||
if (div <= 1)
|
||||
return;
|
||||
/* if div is 3, duty cyle will be 2 : 1, which violates spec */
|
||||
if (div == 4)
|
||||
div = 5;
|
||||
SC_CTRL->SC_CLK_DIV = div - 1;
|
||||
}
|
||||
|
||||
/* FD configuration */
|
||||
static void sc_reader_set_FD(uint8_t fd)
|
||||
{
|
||||
int f = (fd >> 4) & 0x0F;
|
||||
int d = fd & 0x0F;
|
||||
int fi = (atr_decoder_get_FD() >> 4) & 0x0F;
|
||||
uint32_t w;
|
||||
|
||||
if (F_Table[f] == 0 || D_Table[d] == 0)
|
||||
SC_CTRL->BAUD_CLK_DIV = 0x173;
|
||||
else
|
||||
SC_CTRL->BAUD_CLK_DIV = (F_Table[f] / D_Table[d]) - 1;
|
||||
|
||||
// 16 etu for RX FIFO timeout
|
||||
SC_CTRL->RX_TIMEOUT_DELAY = 16 * (SC_CTRL->BAUD_CLK_DIV + 1);
|
||||
|
||||
SC_CTRL->GUARD_TIME = (D_Table[d] == 64) ? 16 : 12;
|
||||
|
||||
w = 960 * atr_decoder_get_waiting_integer() * F_Table[fi];
|
||||
// 960 etu based
|
||||
w /= ((SC_CTRL->BAUD_CLK_DIV + 1) * 960);
|
||||
//SC_CTRL->WAITING_TIME = w;
|
||||
SC_CTRL->ATR_WAIT_TIME = w;
|
||||
}
|
||||
|
||||
/* Reset module configuration */
|
||||
static void sc_reader_spec_default(void)
|
||||
{
|
||||
NVIC_DisableIRQ(ISO7816_IRQn);
|
||||
atr_reset();
|
||||
|
||||
//------------------------------------------------
|
||||
SC_CTRL->EXTRA_GUARD_TIME = 0;
|
||||
sc_reader_enable_retry();
|
||||
|
||||
SC_CTRL->BAUD_CLK_DIV = 371; //default value,加1分频
|
||||
|
||||
/* Set clk to <= 5MHz */
|
||||
{
|
||||
uint8_t clk;
|
||||
|
||||
// source by PCLK
|
||||
GetMCUClock(&clk);
|
||||
if(clk == SYSTEM_CLOCK_16M_XOSC) clk = SYSTEM_CLOCK_16M_RCOSC;
|
||||
clk = 64/pow(2,clk); //SYS_CLK, uint:MHz
|
||||
clk /= 2;
|
||||
sc_reader_config_clock_div((uint16_t)clk); //clk=10 最终时钟速度为4M
|
||||
}
|
||||
|
||||
SC_CTRL->CLK_STOP_EN = false; //卡在激活之前写0打开时钟,中途需要暂停交易的时候写1来停止clk
|
||||
card_clock_stoppable = false;
|
||||
|
||||
/* Reset RX queue for ATR*/
|
||||
queue_reset(&sc_queue);
|
||||
|
||||
/* config SC interrupt */
|
||||
SC_CTRL->INTS_EN = RX_FIFO_INT |
|
||||
ATR_DONE | ATR_FAIL |
|
||||
CARD_DETECT | CARD_REMOVE |
|
||||
DEACTIVATE_DONE |
|
||||
TX_RETRY_OVER_LIMIT | RX_RETRY_OVER_LIMIT;
|
||||
|
||||
/* Clear Pending Interrupt */
|
||||
SC_CTRL->INTS_STATE = SC_INT_ALL;
|
||||
NVIC_EnableIRQ(ISO7816_IRQn);
|
||||
}
|
||||
|
||||
void sc_reader_deactivate(void)
|
||||
{
|
||||
if (card_in == true)
|
||||
{
|
||||
dbg_printf("deactivate!\r\n");
|
||||
SC_CTRL->TRIGGER_DEACTIVATE = true;
|
||||
}
|
||||
}
|
||||
|
||||
void sc_reader_disable(void)
|
||||
{
|
||||
sc_reader_deactivate();
|
||||
//SYS_CTRL->CLK_SC_EN = 0;
|
||||
}
|
||||
|
||||
void sc_reader_activate(void)
|
||||
{
|
||||
if (card_in == true)
|
||||
{
|
||||
dbg_printf("activate!\r\n");
|
||||
GPIO_Pin_Set(BIT3);
|
||||
SC_CTRL->TRIGGER_ACTIVATE = true;
|
||||
}
|
||||
}
|
||||
|
||||
void sc_reader_warm_reset(void)
|
||||
{
|
||||
// if (!card_in || !card_activated)
|
||||
// return;
|
||||
|
||||
sc_reader_spec_default();
|
||||
card_activated = false;
|
||||
SC_CTRL->TRIGGER_WARM_RESET = true;
|
||||
dbg_printf("called sc_reader_warm_reset!\r\n");
|
||||
}
|
||||
|
||||
/* PPS helper function */
|
||||
static void sc_reader_sendPPS(SC_PPS *pps)
|
||||
{
|
||||
uint8_t tmp[6];
|
||||
int idx = 0, i;
|
||||
|
||||
tmp[idx++] = pps->PPSS;
|
||||
tmp[idx++] = pps->PPS[0];
|
||||
pps->PCK = PPS.PPSS ^ PPS.PPS[0];
|
||||
for (i = 1; i <= 3 ; i++) //bypb:改 i<3 为 i<=3
|
||||
{
|
||||
if((pps->PPS[0] >> i) & 0x08)
|
||||
{
|
||||
tmp[idx++] = pps->PPS[i];
|
||||
pps->PCK ^= pps->PPS[i];
|
||||
}
|
||||
}
|
||||
tmp[idx++] = pps->PCK;
|
||||
|
||||
sc_reader_send(tmp, idx);
|
||||
}
|
||||
|
||||
static bool sc_reader_pps_verification(SC_PPS *pps)
|
||||
{
|
||||
int i;
|
||||
uint8_t mask = 0xFF, fd = DEFAULT_FD;
|
||||
bool pass = true;
|
||||
|
||||
// get PPSS
|
||||
if (!get_byte(&pps->R_PPSS))
|
||||
goto PPS_VERIFY_FAIL;
|
||||
pass &= (pps->R_PPSS == 0xFF);
|
||||
|
||||
// get PPS0
|
||||
if (!get_byte(&pps->R_PPS[0]))
|
||||
goto PPS_VERIFY_FAIL;
|
||||
|
||||
// must be T=0
|
||||
pass &= (((pps->R_PPS[0] ^ pps->PPS[0]) & 0x0F) == 0);
|
||||
mask ^= pps->R_PPS[0];
|
||||
|
||||
for (i = 1; i < 3; i++) {
|
||||
if (pps->R_PPS[0] & (0x08 << i)) {
|
||||
// Get data
|
||||
if (!get_byte(&pps->R_PPS[i]))
|
||||
goto PPS_VERIFY_FAIL;
|
||||
mask ^= pps->R_PPS[i];
|
||||
// Must be equal to request
|
||||
pass &= (pps->R_PPS[i] == pps->PPS[i]);
|
||||
|
||||
if (i == 1) { /* PPS1 */
|
||||
fd = pps->R_PPS[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PCK
|
||||
if (!get_byte(&pps->R_PCK))
|
||||
goto PPS_VERIFY_FAIL;
|
||||
mask ^= pps->R_PCK;
|
||||
pass &= (mask == 0);
|
||||
|
||||
sc_reader_set_FD(fd);
|
||||
|
||||
return pass;
|
||||
PPS_VERIFY_FAIL:
|
||||
return false;
|
||||
}
|
||||
|
||||
/* TX helper function */
|
||||
void sc_reader_send(uint8_t *buf, int length)
|
||||
{
|
||||
int i;
|
||||
|
||||
SC_CS_IN();
|
||||
queue_reset(&sc_queue);
|
||||
SC_CS_OUT();
|
||||
|
||||
for (i = 0; i < length; i++)
|
||||
{
|
||||
enqueue(&sc_queue, buf[i]);
|
||||
}
|
||||
|
||||
tx_finished = false;
|
||||
sc_reader_enable_retry();
|
||||
SC_CTRL->CLK_STOP_EN = false; //打开时钟
|
||||
|
||||
//半双工,切换到发送(中断)模式
|
||||
SC_CTRL->INTS_EN &= ~RX_FIFO_INT;
|
||||
SC_CTRL->INTS_EN |= TX_FIFO_INT; //这里会触发TX_FIFO_EMPTY中断
|
||||
|
||||
SC_CTRL->INTS_STATE = C2C_WAITING_TIMEOUT;
|
||||
}
|
||||
|
||||
/* RX helper function */
|
||||
bool sc_reader_get(uint8_t *buf, int length)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (queue_size(&sc_queue) < length)
|
||||
{
|
||||
// Timeout and data not enough => deactivate
|
||||
if (SC_CTRL->INTS_STATE & C2C_WAITING_TIMEOUT)
|
||||
{
|
||||
dbg_printf("RX queue size = %d\r\n", queue_size(&sc_queue));
|
||||
sc_reader_deactivate();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
SC_CS_IN();
|
||||
for (i = 0; i < length; i++, buf++)
|
||||
{
|
||||
dequeue(&sc_queue, buf);
|
||||
}
|
||||
SC_CS_OUT();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void sc_reader_config_clock_stoppable(bool en)
|
||||
{
|
||||
card_clock_stoppable = en;
|
||||
}
|
||||
|
||||
/* Debug Information */
|
||||
void sc_reader_dump_info(void)
|
||||
{
|
||||
int i;
|
||||
uint8_t *buf;
|
||||
|
||||
dbg_printf("card : %s\r\n", card_in ? "in" : "out");
|
||||
dbg_printf("auto : %d\r\n", SC_CTRL->AUTO_ACTIVATION_EN);
|
||||
dbg_printf("vcc : %d\r\n", SC_CTRL->ACTIVE_VCC_LEVEL);
|
||||
dbg_printf("activated : %d\r\n", card_activated);
|
||||
dbg_printf("stoppable : %d\r\n", card_clock_stoppable);
|
||||
|
||||
// ATR sequence
|
||||
dbg_printf("ATR : ");
|
||||
for (i = 0, buf = (uint8_t *)&ATR; i < ATR.TotalLength; i++)
|
||||
dbg_printf("%02X ", *buf++);
|
||||
dbg_printf("\r\n");
|
||||
|
||||
// PPS
|
||||
dbg_printf("PPS : ");
|
||||
for (i = 0, buf = (uint8_t *)&PPS; i < sizeof(PPS); i++)
|
||||
dbg_printf("%02X ", *buf++);
|
||||
dbg_printf("\r\n");
|
||||
}
|
||||
|
||||
/* Callback registration for ACT/DACT */
|
||||
void sc_reader_add_callback(CALL_BACK c, SC_CB type)
|
||||
{
|
||||
if (type >= CALL_BACK_NUM)
|
||||
return;
|
||||
callback[type] = c;
|
||||
}
|
||||
|
||||
/* Callback registration for PPS releated events */
|
||||
void sc_reader_add_PPS_callback(PPS_CALLBACK c, SC_PPS_CB type)
|
||||
{
|
||||
if (type >= PPS_CALL_BACK_NUM)
|
||||
return;
|
||||
pps_cb[type] = c;
|
||||
}
|
||||
|
||||
|
||||
/* Consume all data in queue */
|
||||
__STATIC_INLINE bool get_byte(uint8_t *data)
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
if (!is_queue_empty(&sc_queue))
|
||||
{
|
||||
SC_CS_IN();
|
||||
dequeue(&sc_queue, data);
|
||||
SC_CS_OUT();
|
||||
return true;
|
||||
}
|
||||
else if (SC_CTRL->INTS_STATE & C2C_WAITING_TIMEOUT)
|
||||
{
|
||||
dbg_printf("TIMEOUT\r\n");
|
||||
sc_reader_deactivate();
|
||||
break;
|
||||
}
|
||||
else if (!card_in)
|
||||
{ // card being removed, no timeout will be detected
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
__STATIC_INLINE void sc_reader_enable_retry(void)
|
||||
{
|
||||
SC_CTRL->TX_RETRY_ENABLE = true;
|
||||
SC_CTRL->RX_RETRY_ENABLE = true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform deferred tasks
|
||||
* Do TPDU request & PPS verification
|
||||
*/
|
||||
|
||||
|
||||
//uint8_t Get_Challenge_COM[5] = {0x00, 0x84, 0x00, 0x00,0x04};
|
||||
|
||||
|
||||
void get_challenge_test_handle(TPDU_COMMAND *command)
|
||||
{
|
||||
|
||||
uint8_t i =0;
|
||||
uint8_t *buf;
|
||||
|
||||
dbg_printf("get_challenge_test_handle\r\n");
|
||||
|
||||
dbg_printf("command->header: ");
|
||||
for (i = 0; i < HEADER_SIZE ; i++)
|
||||
dbg_printf("%02X ", command->header[i]);
|
||||
|
||||
dbg_printf("\r\n");
|
||||
|
||||
|
||||
|
||||
dbg_printf("command->writeCommand:%x\r\n",command->writeCommand);
|
||||
|
||||
|
||||
dbg_printf("command->sw: ");
|
||||
for (i = 0; i < SW_SIZE ; i++)
|
||||
dbg_printf("%02X ", command->sw[i]);
|
||||
|
||||
dbg_printf("\r\n");
|
||||
|
||||
if((command->sw[0] == 0x90) &&(command->sw[1] == 0x00))
|
||||
{
|
||||
dbg_printf("command->data: ");
|
||||
for (i = 0; i < command->header[4] ; i++)
|
||||
dbg_printf("%02X ", command->data[i]);
|
||||
|
||||
dbg_printf("\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
dbg_printf("command->sw is error\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TPDU_COMMAND get_challenge_test = {0};
|
||||
|
||||
|
||||
void test_tpdu(void)
|
||||
{
|
||||
|
||||
get_challenge_test.header[0] = 0x00;
|
||||
get_challenge_test.header[1] = 0x84;
|
||||
get_challenge_test.header[2] = 0x00;
|
||||
get_challenge_test.header[3] = 0x00;
|
||||
get_challenge_test.header[4] = 0x04;
|
||||
get_challenge_test.writeCommand = 0;
|
||||
|
||||
tpdu_request(&get_challenge_test,get_challenge_test_handle);
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform deferred tasks
|
||||
* Do TPDU request & PPS verification
|
||||
*/
|
||||
void sc_reader_task(void)
|
||||
{
|
||||
static uint8_t send_test = 0;
|
||||
|
||||
uint32_t flag;
|
||||
static TASK_STATE state;
|
||||
|
||||
SC_CS_IN();
|
||||
flag = task_flag; task_flag = 0;
|
||||
SC_CS_OUT();
|
||||
|
||||
// Handle ISR deferred tasks
|
||||
if (flag)
|
||||
{
|
||||
if (flag & CARD_DET_TASK)
|
||||
{
|
||||
card_activated = false;
|
||||
state = NOT_READY;
|
||||
dbg_printf(card_in ? "CARD DETECTED!\r\n" : "CARD REMOVED!\r\n"); //card_in在中断里设置
|
||||
|
||||
if (card_in == true)
|
||||
{
|
||||
sc_reader_activate();
|
||||
}
|
||||
}
|
||||
|
||||
// ATR Received
|
||||
if (flag & ATR_DONE_TASK)
|
||||
{
|
||||
uint8_t *buf = (uint8_t *)&ATR;
|
||||
|
||||
dbg_printf("ATR DONE!\r\n");
|
||||
|
||||
state = NOT_READY;
|
||||
// Get ATR
|
||||
memset(buf, 0, sizeof(ATR));
|
||||
while(true)
|
||||
{
|
||||
if (!dequeue(&sc_queue, buf++))
|
||||
break;
|
||||
ATR.TotalLength++;
|
||||
}
|
||||
|
||||
if (atr_decode(&ATR)) //check error
|
||||
{
|
||||
sc_reader_deactivate();
|
||||
}
|
||||
else
|
||||
{
|
||||
bool clock_lvl;
|
||||
|
||||
dbg_printf("ATR decode success!\r\n");
|
||||
|
||||
// Timing configuration
|
||||
SC_CTRL->EXTRA_GUARD_TIME = atr_decoder_get_extra_guard_time();
|
||||
|
||||
// Clock stop
|
||||
card_clock_stoppable = atr_decoder_get_clock_stop(&clock_lvl);
|
||||
SC_CTRL->CLK_STOP_VAL = clock_lvl;
|
||||
|
||||
// Setup PPS
|
||||
PPS.PPSS = 0xFF;
|
||||
PPS.PPS[0] = 0x10; //PPS[1] exist, T0 transmission protocol
|
||||
PPS.PPS[1] = atr_decoder_get_FD();
|
||||
|
||||
if (!atr_decoder_allow_pps() && (pps_cb[PPS_REQUEST_CB] == NULL || pps_cb[PPS_REQUEST_CB](&PPS)))
|
||||
{
|
||||
// send pps
|
||||
dbg_printf("send PPS\r\n");
|
||||
sc_reader_sendPPS(&PPS);
|
||||
state = PPS_REQUESTED;
|
||||
card_activated = true;
|
||||
}
|
||||
else if (!atr_decoder_get_protocol()) // T0 and specific mode
|
||||
{
|
||||
// T0 specific mode
|
||||
sc_reader_dump_info();
|
||||
|
||||
// set fd
|
||||
sc_reader_set_FD(DEFAULT_FD); //无需修改 保持默认
|
||||
tpdu_reset();
|
||||
state = ACTIVATED;
|
||||
card_activated = true;
|
||||
}
|
||||
else if (atr_decoder_allow_switch_mode()) // T1 and specific mode but allow to change to negotiable mode
|
||||
{
|
||||
dbg_printf("atr_decoder_allow_switch_mode!\r\n");
|
||||
sc_reader_warm_reset();
|
||||
}
|
||||
else // Not support
|
||||
{
|
||||
dbg_printf("atr_decoder_Not_support_switch_mode!\r\n");
|
||||
sc_reader_deactivate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (flag & ATR_FAIL_TASK)
|
||||
{
|
||||
uint8_t *buf = (uint8_t *)&ATR;
|
||||
state = NOT_READY;
|
||||
card_activated = false; //bypb add
|
||||
// Get ATR
|
||||
memset(buf, 0, sizeof(ATR));
|
||||
while(true)
|
||||
{
|
||||
if (!dequeue(&sc_queue, buf++))
|
||||
break;
|
||||
ATR.TotalLength++;
|
||||
}
|
||||
}
|
||||
|
||||
if (flag & DEACTIVATION_TASK)
|
||||
{
|
||||
card_activated = false;
|
||||
SC_CS_IN();
|
||||
queue_reset(&sc_queue);
|
||||
sc_reader_spec_default();
|
||||
SC_CS_OUT();
|
||||
state = NOT_READY;
|
||||
|
||||
sc_reader_dump_info();
|
||||
|
||||
if (callback[DACT_CB])
|
||||
{
|
||||
callback[DACT_CB]();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* If not in activation, then no TPDU or PPS will be performed */
|
||||
if (!card_in || !card_activated) /*若未插卡或者卡未激活*/
|
||||
{
|
||||
tpdu_reset();
|
||||
return;
|
||||
}
|
||||
|
||||
// SC reader state machine
|
||||
if (state == ACTIVATED)
|
||||
{
|
||||
static TPDU_TASK_STATE tpdu_state;
|
||||
tpdu_state = tpdu_task(tx_finished);
|
||||
if (tpdu_state == TT_IDLE)
|
||||
{
|
||||
if (card_clock_stoppable)
|
||||
{
|
||||
SC_CTRL->CLK_STOP_EN = true;
|
||||
dbg_printf("stop clk!\r\n");
|
||||
}
|
||||
|
||||
if(send_test == 0) {
|
||||
send_test = 1;
|
||||
dbg_printf("state == ACTIVATED,test_tpdu()\r\n");
|
||||
test_tpdu();
|
||||
}
|
||||
|
||||
if (callback[ACT_CB])
|
||||
{
|
||||
callback[ACT_CB]();
|
||||
}
|
||||
}
|
||||
else if (tpdu_state == TT_ERROR)
|
||||
{
|
||||
dbg_printf("TPDU error\r\n");
|
||||
sc_reader_deactivate();
|
||||
state = NOT_READY;
|
||||
}
|
||||
}
|
||||
else if (state == PPS_REQUESTED)
|
||||
{
|
||||
while (!tx_finished);
|
||||
dbg_printf("PPS responsed!\r\n");
|
||||
sc_reader_dump_info();
|
||||
|
||||
if (sc_reader_pps_verification(&PPS))
|
||||
{
|
||||
state = ACTIVATED;
|
||||
dbg_printf("PPS verification success!\r\n");
|
||||
|
||||
//sc_reader_dump_info();
|
||||
|
||||
#if 0
|
||||
sc_reader_warm_reset(); //验证热复位
|
||||
#elif 0
|
||||
sc_reader_deactivate(); //验证去激活
|
||||
#elif 0
|
||||
SC_CTRL->CLK_STOP_EN = true; //验证停止时钟
|
||||
dbg_printf("stop clk!\r\n");
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
if (pps_cb[PPS_FAIL_CB])
|
||||
{
|
||||
pps_cb[PPS_FAIL_CB](&PPS);
|
||||
}
|
||||
sc_reader_deactivate();
|
||||
state = NOT_READY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Configure module settings */
|
||||
void sc_reader_enable(void)
|
||||
{
|
||||
PIN_CONFIG->PIN_27_SEL = PIN_SEL_SC_DETECT;
|
||||
//PIN_CONFIG->PIN_9_SEL = PIN_SEL_SC_VCC;
|
||||
PIN_CONFIG->PIN_17_SEL = PIN_SEL_SC_CLK;
|
||||
PIN_CONFIG->PIN_26_SEL = PIN_SEL_SC_IO;
|
||||
PIN_CONFIG->PIN_4_SEL = PIN_SEL_SC_RSTN;
|
||||
|
||||
SC_CTRL->MODE = 0; //7816
|
||||
|
||||
SC_CTRL->T0T1 = 0; //T0 Protocol - default
|
||||
SC_CTRL->DET_LEVEL = 0; //外部接上拉,下降沿触发
|
||||
PIN_CONFIG->PAD_6_INPUT_PULL_UP = 1; //使能内部上拉
|
||||
|
||||
sc_reader_config_auto(false);
|
||||
|
||||
/* Configure RESET & ACTIVATE timeout to maximum */
|
||||
SC_CTRL->RST_TIME = 400; //100?
|
||||
SC_CTRL->ACT_TIME = 30; //200?
|
||||
|
||||
/* Configure FIFO threshold, FIFO size is 8, set half to inform*/
|
||||
SC_CTRL->RX_FIFO_THRESHOLD = 0x4;
|
||||
SC_CTRL->TX_FIFO_THRESHOLD = 0x4;
|
||||
|
||||
/* Retry configuration */
|
||||
SC_CTRL->TX_RETRY_CNT = 3;
|
||||
SC_CTRL->RX_RETRY_CNT = 3;
|
||||
|
||||
/* Enable RX FIFO Timeout */
|
||||
SC_CTRL->RX_TIMEOUT_DETECT_EN = true;
|
||||
|
||||
sc_reader_spec_default();
|
||||
|
||||
/* Enable TX/RX */
|
||||
SC_CTRL->TX_ENABLE = true;
|
||||
SC_CTRL->RX_ENABLE = true;
|
||||
}
|
||||
|
||||
/* Interrupt Handler */
|
||||
void ISO_7816_IRQHandler(void)
|
||||
#if 0
|
||||
{
|
||||
//uint32_t mask = SC_CTRL->INTS_STATE & SC_CTRL->INTS_EN;
|
||||
|
||||
uint32_t mask = SC_CTRL->INTS_STATE;
|
||||
dbg_printf("0:INTS_STATE = 0x%08x\r\n", mask);
|
||||
|
||||
//SC_CTRL->INTS_STATE = mask; // Clear interrupt
|
||||
SC_CTRL->INTS_STATE = SC_INT_ALL; // Clear interrupt
|
||||
mask = SC_CTRL->INTS_STATE;
|
||||
dbg_printf("1:INTS_STATE = 0x%08x\r\n", mask);
|
||||
}
|
||||
#else
|
||||
{
|
||||
uint32_t mask = SC_CTRL->INTS_STATE & SC_CTRL->INTS_EN;
|
||||
|
||||
// Optimize empty interrupt might occur when access FIFO
|
||||
if (!mask)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (mask & DEACTIVATE_DONE)
|
||||
{
|
||||
task_flag |= DEACTIVATION_TASK;
|
||||
}
|
||||
|
||||
if (mask & (CARD_DETECT | CARD_REMOVE))
|
||||
{
|
||||
card_in = (bool)((mask & CARD_DETECT)? 1:0);
|
||||
task_flag |= CARD_DET_TASK;
|
||||
}
|
||||
|
||||
// RX fifo data available
|
||||
if (mask & RX_FIFO_INT)
|
||||
{
|
||||
while(SC_CTRL->RXFNE)
|
||||
{
|
||||
enqueue(&sc_queue, SC_CTRL->DATA);
|
||||
}
|
||||
}
|
||||
|
||||
// TX fifo available
|
||||
if (mask & TX_FIFO_INT)
|
||||
{
|
||||
uint8_t data;
|
||||
uint8_t i=0;
|
||||
|
||||
// fill data until full or no data
|
||||
while ((i<8) && dequeue(&sc_queue, &data))
|
||||
{
|
||||
SC_CTRL->DATA = data;
|
||||
i++;
|
||||
}
|
||||
|
||||
// TX Done
|
||||
if (is_queue_empty(&sc_queue) && SC_CTRL->TXFE)
|
||||
{
|
||||
// Disable TX FIFO available notification
|
||||
SC_CTRL->INTS_EN &= ~TX_FIFO_INT;
|
||||
SC_CTRL->INTS_EN |= RX_FIFO_INT;
|
||||
//半双工,切换到接收(中断)模式,RX_TIMEOUT中断也需要,因为它指明了FIFO空且等待超时
|
||||
|
||||
tx_finished = true;
|
||||
}
|
||||
}
|
||||
|
||||
// ATR failed
|
||||
if (mask & ATR_FAIL)
|
||||
{
|
||||
// Consumes RX FIFO
|
||||
while(SC_CTRL->RXFNE)
|
||||
{
|
||||
enqueue(&sc_queue, SC_CTRL->DATA);
|
||||
}
|
||||
task_flag |= ATR_FAIL_TASK;
|
||||
//dbg_printf("ATR_FAIL, INT_ST = 0x%X\r\n", SC_CTRL->INTS_STATE);
|
||||
}
|
||||
|
||||
if (mask & ATR_DONE)
|
||||
{
|
||||
// Consume RX FIFO data
|
||||
while(SC_CTRL->RXFNE)
|
||||
{
|
||||
enqueue(&sc_queue, SC_CTRL->DATA);
|
||||
}
|
||||
task_flag |= ATR_DONE_TASK;
|
||||
}
|
||||
|
||||
if (mask & TX_RETRY_OVER_LIMIT)
|
||||
{
|
||||
#if 0
|
||||
if (SC_CTRL->TX_FORCE_PARITY_ERROR)
|
||||
SC_CTRL->TX_FORCE_PARITY_ERROR = false;
|
||||
else
|
||||
SC_CTRL->TX_RETRY_ENABLE = false;
|
||||
#elif 1
|
||||
if (SC_CTRL->TXOVRETYR)
|
||||
{
|
||||
//肯定有
|
||||
SC_CTRL->TXOVRETYR = 1; //写1清除
|
||||
}
|
||||
else
|
||||
{
|
||||
//出错了
|
||||
SC_CTRL->TX_RETRY_ENABLE = false;
|
||||
//dbg_printf("TXOVRETYR Err\r\n"); while(1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (mask & RX_RETRY_OVER_LIMIT)
|
||||
{
|
||||
#if 0
|
||||
if (SC_CTRL->RX_FORCE_PARITY_ERROR)
|
||||
SC_CTRL->RX_FORCE_PARITY_ERROR = false;
|
||||
else
|
||||
SC_CTRL->RX_RETRY_ENABLE = false;
|
||||
#else
|
||||
if (SC_CTRL->RXOVRETRY)
|
||||
{
|
||||
//肯定有
|
||||
SC_CTRL->RXOVRETRY = 1; //写1清除
|
||||
}
|
||||
else
|
||||
{
|
||||
//出错了
|
||||
SC_CTRL->RX_RETRY_ENABLE = false;
|
||||
//dbg_printf("RXOVRETRY Err\r\n"); while(1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Clear interrupt
|
||||
SC_CTRL->INTS_STATE = mask;
|
||||
}
|
||||
#endif
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
#include "sensor_spi.h"
|
||||
#include <string.h>
|
||||
|
||||
static MISC_CTRL_TYPE * MISC_CTRL = ((MISC_CTRL_TYPE *) MISC_BASE);
|
||||
|
||||
void SPIBurstReadSetAddr(uint8_t len, uint8_t* paddr)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
MISC_CTRL->SS_BURST_NUM = len;
|
||||
|
||||
for(i = 0; i < len; i++)
|
||||
MISC_CTRL->SS_ADDR[i] = paddr[i];
|
||||
}
|
||||
|
||||
void SPIBurstRead(uint8_t *pbuf, uint8_t len)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
MISC_CTRL->SS_BURST_READ = 1;
|
||||
|
||||
while(MISC_CTRL->SS_BURST_READ==1);
|
||||
|
||||
for(i = 0; i < len; i++)
|
||||
pbuf[i] = MISC_CTRL->SS_DATA[i];
|
||||
}
|
||||
|
||||
void SPISingleWrite(uint8_t addr, uint8_t data)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
MISC_CTRL->R_write_pol=R_WRIT_POL;
|
||||
|
||||
MISC_CTRL->SS_RDY_CLR = 1;
|
||||
|
||||
MISC_CTRL->SS_BUF[addr] = data;
|
||||
|
||||
while(1)
|
||||
{
|
||||
for(i=0; i< 10; i++)
|
||||
__asm volatile (" NOP");
|
||||
|
||||
if(MISC_CTRL->SS_RDY == 1)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void SPISingleRead(uint8_t addr, uint8_t *p_data)
|
||||
{
|
||||
uint8_t num_tmp = MISC_CTRL->SS_BURST_NUM;
|
||||
uint8_t addr_tmp = MISC_CTRL->SS_ADDR[0];
|
||||
|
||||
MISC_CTRL->R_write_pol=R_WRIT_POL;
|
||||
|
||||
MISC_CTRL->SS_BURST_NUM = 0x01;
|
||||
|
||||
MISC_CTRL->SS_ADDR[0] = addr;
|
||||
|
||||
MISC_CTRL->SS_BURST_READ = 1;
|
||||
|
||||
while(MISC_CTRL->SS_BURST_READ==1);
|
||||
|
||||
*p_data = MISC_CTRL->SS_DATA[0];
|
||||
|
||||
MISC_CTRL->SS_BURST_NUM = num_tmp;
|
||||
|
||||
MISC_CTRL->SS_ADDR[0] = addr_tmp;
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
#include "spi.h"
|
||||
#include "gpio.h"
|
||||
|
||||
static SPI_CTRL_TYPE * SPI_MASTER_CTRL = ((SPI_CTRL_TYPE *) SPI_MASTER_BASE);
|
||||
|
||||
void spi_init(enum spi_interface interface)
|
||||
{
|
||||
PIN_CONFIG->PIN_23_SEL = PIN_SEL_SPIM_CLK; //CLK
|
||||
PIN_CONFIG->PIN_22_SEL = PIN_SEL_SPIM_MISO; // DI
|
||||
PIN_CONFIG->PIN_20_SEL = PIN_SEL_SPIM_CSN; // CS
|
||||
PIN_CONFIG->PIN_24_SEL = PIN_SEL_SPIM_MOSI; // DO
|
||||
|
||||
SPI_MASTER_CTRL->MSB = 1;
|
||||
SPI_MASTER_CTRL->SPEED = 0; //数字越小,速度越快
|
||||
|
||||
#if 1
|
||||
SPI_MASTER_CTRL->CPOL = 0;
|
||||
SPI_MASTER_CTRL->CPHA = 0;
|
||||
#elif 0
|
||||
//untested
|
||||
SPI_MASTER_CTRL->CPOL = 0;
|
||||
SPI_MASTER_CTRL->CPHA = 1;
|
||||
#elif 0
|
||||
//untested
|
||||
SPI_MASTER_CTRL->CPOL = 1;
|
||||
SPI_MASTER_CTRL->CPHA = 0;
|
||||
#else
|
||||
SPI_MASTER_CTRL->CPOL = 1;
|
||||
SPI_MASTER_CTRL->CPHA = 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
void spi_cs_enable(enum spi_interface interface)
|
||||
{
|
||||
SPI_MASTER_CTRL->PE = 1; // CS Low
|
||||
}
|
||||
|
||||
void spi_cs_disable(enum spi_interface interface)
|
||||
{
|
||||
SPI_MASTER_CTRL->PE = 0; // CS High
|
||||
}
|
||||
|
||||
/***********************************************************
|
||||
spi写函数
|
||||
输入参数:uint8_t *p_data 指向要发送的数据
|
||||
uint8_t num 数据的长度
|
||||
***********************************************************/
|
||||
void spi_write(uint8_t *p_data,uint16_t num)
|
||||
{
|
||||
// tx data
|
||||
for(;num>0;num--)
|
||||
{
|
||||
SPI_MASTER_CTRL->OUT = *p_data;
|
||||
SPI_MASTER_CTRL->START = 1;
|
||||
p_data++;
|
||||
while(SPI_MASTER_CTRL->START == 1);
|
||||
SPI_MASTER_CTRL->DONE = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/***********************************************************
|
||||
spi读函数
|
||||
输入参数:uint8_t *p_data 指向要接收数据的位置
|
||||
uint8_t num 数据的长度
|
||||
***********************************************************/
|
||||
void spi_read(uint8_t *p_data,uint16_t num)
|
||||
{
|
||||
// rx data
|
||||
for(;num>0;num--)
|
||||
{
|
||||
SPI_MASTER_CTRL->OUT = 0xFF;
|
||||
SPI_MASTER_CTRL->START = 1;
|
||||
while(SPI_MASTER_CTRL->START == 1);
|
||||
*p_data = SPI_MASTER_CTRL->IN;
|
||||
p_data++;
|
||||
SPI_MASTER_CTRL->DONE = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,131 @@
|
|||
/*************************************CopyRight(c)************************************************
|
||||
** ½Î÷ÂÀ²¼¿Æ¼¼ÓÐÏÞ¹«Ë¾
|
||||
** www.lvbu.wang
|
||||
**************************************************************************************************
|
||||
**Filename:
|
||||
**Version:
|
||||
**Author:
|
||||
**Date:
|
||||
**Description:
|
||||
**
|
||||
**************************************************************************************************
|
||||
**Version:
|
||||
**Modifier:
|
||||
**Datemodified:
|
||||
**Description:
|
||||
**
|
||||
**************************************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* Include
|
||||
*/
|
||||
#include "spi_soft.h"
|
||||
#include "gpio.h"
|
||||
/*********************************************************************
|
||||
* Macros
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Constants
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Typedefs
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Global Variables
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Global Functions
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* External Variables
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* External Functions
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Local Vriables
|
||||
*/
|
||||
static uint32_t csio=0;
|
||||
static uint32_t wrio=0;
|
||||
static uint32_t rdio=0;
|
||||
static uint32_t dataio=0;
|
||||
/*********************************************************************
|
||||
* Local Functions
|
||||
*/
|
||||
void spi_soft_delay_us(uint16_t n) {
|
||||
uint16_t i,j;
|
||||
for(i=0; i<n; i++)
|
||||
for(j=0; j<2; j++);
|
||||
}
|
||||
|
||||
void spi_soft_init(spi_soft_t * p_spis,uint32_t cs_io,uint32_t wr_io, uint32_t rd_io, uint32_t data_io )
|
||||
{
|
||||
|
||||
p_spis->csio = cs_io;
|
||||
p_spis->wrio =wr_io;
|
||||
p_spis->rdio =rd_io;
|
||||
p_spis->dataio =data_io;
|
||||
|
||||
GPIO_Set_Output(U32BIT(p_spis->csio) | U32BIT(p_spis->dataio));
|
||||
GPIO_Pin_Set(U32BIT(p_spis->csio) | U32BIT(p_spis->dataio));
|
||||
|
||||
if(p_spis->wrio != 0)
|
||||
{
|
||||
GPIO_Set_Output(U32BIT(p_spis->wrio));
|
||||
GPIO_Pin_Set(U32BIT(p_spis->wrio));
|
||||
}
|
||||
if(p_spis->rdio != 0)
|
||||
{
|
||||
GPIO_Set_Output(U32BIT(p_spis->rdio) );
|
||||
GPIO_Pin_Set(U32BIT(p_spis->rdio) );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void spi_soft_cs_enable(spi_soft_t * p_spis)
|
||||
{
|
||||
GPIO_Pin_Clear(U32BIT(p_spis->csio));
|
||||
}
|
||||
|
||||
void spi_soft_cs_disenable(spi_soft_t * p_spis)
|
||||
{
|
||||
GPIO_Pin_Set(U32BIT(p_spis->csio));
|
||||
}
|
||||
|
||||
void spi_soft_writebit(spi_soft_t * p_spis,uint8_t Data,uint8_t cnt)
|
||||
{
|
||||
unsigned char i;
|
||||
for (i=0;i<cnt;i++)
|
||||
{
|
||||
GPIO_Pin_Clear(U32BIT(p_spis->wrio));
|
||||
spi_soft_delay_us(5);
|
||||
if((Data&0x80)==0)
|
||||
{
|
||||
GPIO_Pin_Clear(U32BIT(p_spis->dataio));
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_Pin_Set(U32BIT(p_spis->dataio));
|
||||
}
|
||||
spi_soft_delay_us(5);
|
||||
GPIO_Pin_Set(U32BIT(p_spis->wrio));
|
||||
spi_soft_delay_us(5);
|
||||
Data<<=1;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
|
@ -0,0 +1,246 @@
|
|||
#include "timer.h"
|
||||
#include "delay.h"
|
||||
|
||||
static TIMER_CTRL_TYPE * TIMER_CTRL = ((TIMER_CTRL_TYPE *) TIMER_CTRL_BASE);
|
||||
|
||||
//定时器回调函数定义 timer_x的回调函数是timer_callback[x]
|
||||
static void (*timer_callback[3])(void);
|
||||
|
||||
/*
|
||||
定时器0 配置函数
|
||||
参数:
|
||||
uint32_t interval 定时器是递减计数器 interval就是计数器初值
|
||||
void * p_callback 定时器中断的回调函数
|
||||
定时器时钟: 32.768KHz
|
||||
*/
|
||||
void timer_0_enable(uint32_t interval, void * p_callback)
|
||||
{
|
||||
if(TIMER_CTRL->TIMER_0_EN== 1)
|
||||
{
|
||||
TIMER_CTRL->TIMER_0_EN = 0;
|
||||
delay_us(50); //稍微延时 使定时器重启成功
|
||||
}
|
||||
|
||||
TIMER_CTRL->TIMER_0_VAL = interval;
|
||||
TIMER_CTRL->TIMER_0_RGLR = 1; //1:reload enable
|
||||
TIMER_CTRL->TIMER_0_INT_MASK = 0; //1:mask int
|
||||
timer_callback[0] = ((void (*)(void))p_callback);
|
||||
|
||||
TIMER_CTRL->TIMER_0_INT_CLR = 1;
|
||||
NVIC_EnableIRQ(TIMER0_IRQn);
|
||||
|
||||
TIMER_CTRL->TIMER_0_EN = 1;
|
||||
}
|
||||
|
||||
uint32_t timer_0_get_val(void)
|
||||
{
|
||||
return TIMER_CTRL->TIMER_0_CNT;
|
||||
}
|
||||
|
||||
/*
|
||||
定时器0 失能函数 关闭定时器0的中断
|
||||
*/
|
||||
void timer_0_disable(void)
|
||||
{
|
||||
TIMER_CTRL->TIMER_0_EN = 0;
|
||||
NVIC_DisableIRQ(TIMER0_IRQn);
|
||||
TIMER_CTRL->TIMER_0_INT_CLR = 1;
|
||||
}
|
||||
/*
|
||||
定时器0 延时 cnt个32us函数,单位为:32us 总延时 cnt*32 us
|
||||
参数:uint32_t cnt 微秒数
|
||||
*/
|
||||
void timer_0_delay_32us(uint32_t cnt)
|
||||
{
|
||||
TIMER_CTRL->TIMER_0_VAL= cnt;
|
||||
TIMER_CTRL->TIMER_0_INT_MASK = 1;
|
||||
TIMER_CTRL->TIMER_0_INT_CLR = 1;
|
||||
|
||||
TIMER_CTRL->TIMER_0_EN = 1;
|
||||
|
||||
while(TIMER_CTRL->TIMER_0_INT_STATUS==0);
|
||||
|
||||
TIMER_CTRL->TIMER_0_EN = 0;
|
||||
|
||||
TIMER_CTRL->TIMER_0_INT_CLR = 1;
|
||||
}
|
||||
/*
|
||||
定时器0 中断服务函数 这里调用了回调函数
|
||||
*/
|
||||
void TIMER0_IRQHandler(void)
|
||||
{
|
||||
if(TIMER_CTRL->TIMER_0_INT_STATUS == 1)
|
||||
{
|
||||
TIMER_CTRL->TIMER_0_INT_CLR = 1;
|
||||
|
||||
if(timer_callback[0] != 0)
|
||||
(*timer_callback[0])();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
定时器1 配置函数
|
||||
参数:
|
||||
uint32_t interval 定时器是递减计数器 interval就是计数器初值
|
||||
void * p_callback 定时器中断的回调函数
|
||||
定时器时钟: 32.768KHz
|
||||
*/
|
||||
void timer_1_enable(uint32_t interval, void * p_callback)
|
||||
{
|
||||
if(TIMER_CTRL->TIMER_1_EN== 1)
|
||||
{
|
||||
TIMER_CTRL->TIMER_1_EN = 0;
|
||||
delay_us(50);
|
||||
}
|
||||
|
||||
TIMER_CTRL->TIMER_1_VAL= interval;
|
||||
TIMER_CTRL->TIMER_1_RGLR= 1;
|
||||
TIMER_CTRL->TIMER_1_INT_MASK = 0;
|
||||
timer_callback[1] = ((void (*)(void))p_callback);
|
||||
|
||||
TIMER_CTRL->TIMER_1_INT_CLR = 1;
|
||||
NVIC_EnableIRQ(TIMER1_IRQn);
|
||||
|
||||
TIMER_CTRL->TIMER_1_EN = 1;
|
||||
}
|
||||
|
||||
uint32_t timer_1_get_val(void)
|
||||
{
|
||||
return TIMER_CTRL->TIMER_1_CNT;
|
||||
}
|
||||
|
||||
/*
|
||||
定时器1 失能函数 关闭定时器1的中断
|
||||
*/
|
||||
void timer_1_disable(void)
|
||||
{
|
||||
TIMER_CTRL->TIMER_1_EN = 0;
|
||||
NVIC_DisableIRQ(TIMER1_IRQn);
|
||||
TIMER_CTRL->TIMER_1_INT_CLR = 1;
|
||||
}
|
||||
/*
|
||||
定时器1 中断服务函数 这里调用了回调函数
|
||||
*/
|
||||
void TIMER1_IRQHandler(void)
|
||||
{
|
||||
if(TIMER_CTRL->TIMER_1_INT_STATUS == 1)
|
||||
{
|
||||
TIMER_CTRL->TIMER_1_INT_CLR = 1;
|
||||
|
||||
if(timer_callback[1] != 0)
|
||||
(*timer_callback[1])();
|
||||
}
|
||||
}
|
||||
/*
|
||||
定时器2 配置函数
|
||||
参数:
|
||||
uint32_t interval 定时器是递减计数器 interval就是计数器初值
|
||||
void * p_callback 定时器中断的回调函数
|
||||
定时器时钟: 32.768KHz
|
||||
*/
|
||||
void timer_2_enable(uint32_t interval, void * p_callback)
|
||||
{
|
||||
if(TIMER_CTRL->TIMER_2_EN== 1)
|
||||
{
|
||||
TIMER_CTRL->TIMER_2_EN = 0;
|
||||
delay_us(50);
|
||||
}
|
||||
|
||||
TIMER_CTRL->TIMER_2_VAL= interval;
|
||||
TIMER_CTRL->TIMER_2_RGLR= 1;
|
||||
TIMER_CTRL->TIMER_2_INT_MASK = 0;
|
||||
timer_callback[2] = ((void (*)(void))p_callback);
|
||||
|
||||
TIMER_CTRL->TIMER_2_INT_CLR = 1;
|
||||
NVIC_EnableIRQ(TIMER2_IRQn);
|
||||
|
||||
TIMER_CTRL->TIMER_2_EN = 1;
|
||||
}
|
||||
|
||||
uint32_t timer_2_get_val(void)
|
||||
{
|
||||
return TIMER_CTRL->TIMER_2_CNT;
|
||||
}
|
||||
|
||||
/*
|
||||
定时器2 失能函数 关闭定时器2的中断
|
||||
*/
|
||||
void timer_2_disable(void)
|
||||
{
|
||||
TIMER_CTRL->TIMER_2_EN = 0;
|
||||
NVIC_DisableIRQ(TIMER2_IRQn);
|
||||
TIMER_CTRL->TIMER_2_INT_CLR = 1;
|
||||
}
|
||||
/*
|
||||
定时器2 中断服务函数 这里调用了回调函数
|
||||
*/
|
||||
void TIMER2_IRQHandler(void)
|
||||
{
|
||||
if(TIMER_CTRL->TIMER_2_INT_STATUS == 1)
|
||||
{
|
||||
TIMER_CTRL->TIMER_2_INT_CLR = 1;
|
||||
|
||||
if(timer_callback[2] != 0)
|
||||
(*timer_callback[2])();
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************高速定时器***************************************************/
|
||||
static H_TIMER_CTRL_TYPE * H_TIMER_CTRL = ((H_TIMER_CTRL_TYPE *) H_TIMER_CTRL_BASE);
|
||||
static void (*htimer_callback)(void);
|
||||
|
||||
/*************************************************************
|
||||
高速定时器 配置函数
|
||||
参数:
|
||||
uint16_t interval interval就是计数器向上计数的最大值
|
||||
void * p_callback 定时器中断的回调函数
|
||||
定时器时钟: 64M
|
||||
**************************************************************/
|
||||
void Htimer_enable(uint16_t interval, void (*p_callback)(void))
|
||||
{
|
||||
if(H_TIMER_CTRL->BUSY == 1)
|
||||
{
|
||||
H_TIMER_CTRL->STOP = 1;
|
||||
}
|
||||
|
||||
H_TIMER_CTRL->ONE_TIME = 0;
|
||||
H_TIMER_CTRL->COUNTER_TOP = interval;
|
||||
|
||||
H_TIMER_CTRL->PRESCALER = H_TIMER_PERSCALER_1;
|
||||
|
||||
htimer_callback = p_callback;
|
||||
H_TIMER_CTRL->EVENT = 1; //clr Int
|
||||
H_TIMER_CTRL->INTEN = 1;
|
||||
NVIC_EnableIRQ(HTIMER_IRQn);
|
||||
|
||||
H_TIMER_CTRL->START = 1; //set H_TIMER_CTRL->BUSY
|
||||
}
|
||||
|
||||
uint16_t Htimer_get_val(void)
|
||||
{
|
||||
return H_TIMER_CTRL->COUNTER;
|
||||
}
|
||||
|
||||
/*************************************************************
|
||||
高速定时器停止函数
|
||||
**************************************************************/
|
||||
void Htimer_disable(void)
|
||||
{
|
||||
H_TIMER_CTRL->STOP = 1; //clr H_TIMER_CTRL->BUSY
|
||||
while(H_TIMER_CTRL->BUSY);
|
||||
}
|
||||
|
||||
/*************************************************************
|
||||
高速定时器中断服务函数 这里调用了回调函数
|
||||
**************************************************************/
|
||||
void HTIMER_IRQHandler(void)
|
||||
{
|
||||
uint8_t status = (uint8_t)H_TIMER_CTRL->EVENT;
|
||||
H_TIMER_CTRL->EVENT = 1; //clr Int
|
||||
|
||||
if(htimer_callback != 0)
|
||||
{
|
||||
htimer_callback();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,150 @@
|
|||
|
||||
/*************************************CopyRight(c)************************************************
|
||||
** 江西吕布科技有限公司
|
||||
** www.lvbu.wang
|
||||
**************************************************************************************************
|
||||
**Filename:
|
||||
**Version:
|
||||
**Author:
|
||||
**Date:
|
||||
**Description:
|
||||
**
|
||||
**************************************************************************************************
|
||||
**Version:
|
||||
**Modifier:
|
||||
**Datemodified:
|
||||
**Description:
|
||||
**
|
||||
**************************************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* Include
|
||||
*/
|
||||
#include "calender.h"
|
||||
#include "softtimer.h"
|
||||
#include "DebugLog.h"
|
||||
/*********************************************************************
|
||||
* Macros
|
||||
*/
|
||||
#define _TIMER_HANDLER_C_
|
||||
/*********************************************************************
|
||||
* Constants
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Typedefs
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Global Variables
|
||||
*/
|
||||
|
||||
struct SYD_sysTimer syd_sysTimer[EVT_NUM] = {0};
|
||||
/*********************************************************************
|
||||
* Global Functions
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* External Variables
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* External Functions
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Local Vriables
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Local Functions
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Function : 两秒的定时事件
|
||||
* Param in : none
|
||||
* Param out: none
|
||||
* Return : none
|
||||
* Describe :
|
||||
*/
|
||||
#ifdef EVT_2S
|
||||
void Timer_Evt_2s(void)
|
||||
{
|
||||
TIMER_EVT|=EVT_2S;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef EVT_1S_OTA
|
||||
void Timer_EVT_1S_OTA(void)
|
||||
{
|
||||
TIMER_EVT|=EVT_1S_OTA;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef EVT_1S_WORK
|
||||
void Timer_EVT_1S_WORK(void)
|
||||
{
|
||||
TIMER_EVT|=EVT_1S_WORK;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef EVT_100MS_PACK
|
||||
void Timer_EVT_100MS_PCAK(void)
|
||||
{
|
||||
TIMER_EVT|=EVT_100MS_PACK;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef EVT_20S_BATTERY
|
||||
void Timer_EVT_20S_BATTERY(void)
|
||||
{
|
||||
TIMER_EVT|=EVT_20S_BATTERY;
|
||||
}
|
||||
#endif
|
||||
|
||||
void Timer_Evt_List(void)
|
||||
{
|
||||
/* 2s的连接事务处理*/
|
||||
#ifdef EVT_2S
|
||||
Timer_Evt_Creat(EVT_2S,2000,Timer_Evt_2s,EVT_DISABLE_MODE);
|
||||
#endif
|
||||
|
||||
/* 1s中OTA的事件处理*/
|
||||
#ifdef EVT_1S_OTA
|
||||
Timer_Evt_Creat(EVT_1S_OTA,1000,Timer_EVT_1S_OTA,EVT_DISABLE_MODE);
|
||||
|
||||
#endif
|
||||
|
||||
/* 1S的app工作任务*/
|
||||
#ifdef EVT_1S_WORK
|
||||
Timer_Evt_Creat(EVT_1S_WORK,1000,Timer_EVT_1S_WORK,EVT_DISABLE_MODE);
|
||||
#endif
|
||||
|
||||
/* 300ms的打包数据发布*/
|
||||
#ifdef EVT_100MS_PACK
|
||||
Timer_Evt_Creat(EVT_100MS_PACK,100,Timer_EVT_100MS_PCAK,EVT_DISABLE_MODE);
|
||||
#endif
|
||||
|
||||
/* 20S的定时获取电池电量*/
|
||||
#ifdef EVT_20S_BATTERY
|
||||
Timer_Evt_Creat(EVT_20S_BATTERY,20000,Timer_EVT_20S_BATTERY,EVT_DISABLE_MODE);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,117 @@
|
|||
#include "tpdu.h"
|
||||
#include "debug.h"
|
||||
#include "queue.h"
|
||||
#include <string.h>
|
||||
#include "sc_reader.h"
|
||||
|
||||
static int residual_size;
|
||||
static TPDU_TASK_STATE state;
|
||||
static TPDU_CALLBACK notification;
|
||||
static TPDU_COMMAND *command;
|
||||
|
||||
__STATIC_INLINE void tpdu_done(void);
|
||||
|
||||
bool tpdu_request(TPDU_COMMAND *c, TPDU_CALLBACK callback)
|
||||
{
|
||||
// New request can be issue if in IDLE
|
||||
if (state != TT_IDLE || c == NULL)
|
||||
return false;
|
||||
|
||||
command = c;
|
||||
state = TT_SEND_HEADER;
|
||||
notification = callback;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
TPDU_TASK_STATE tpdu_task(bool tx_finished)
|
||||
{
|
||||
static uint8_t *buf;
|
||||
static uint8_t procedure;
|
||||
static uint8_t next_data_size;
|
||||
|
||||
switch (state) {
|
||||
case TT_IDLE:
|
||||
// Nothing to do
|
||||
break;
|
||||
case TT_SEND_HEADER:
|
||||
sc_reader_send(command->header, sizeof(command->header));
|
||||
buf = command->data;
|
||||
next_data_size = 0;
|
||||
|
||||
residual_size = command->header[P3];
|
||||
if (!command->writeCommand && residual_size == 0) // RX special size handle
|
||||
residual_size = 256;
|
||||
// Wait for Header to be sent
|
||||
state = TT_WAIT_FOR_TX_FINISH;
|
||||
break;
|
||||
case TT_WAIT_FOR_TX_FINISH:
|
||||
if (tx_finished) {
|
||||
// TX is done, check procedure bytes
|
||||
state = TT_DETERMINE_PROCEDURE;
|
||||
}
|
||||
break;
|
||||
case TT_DETERMINE_PROCEDURE:
|
||||
if (sc_reader_get(&procedure, 1) && procedure != 0x60) {
|
||||
// Status Bytes
|
||||
uint8_t tmp = procedure & 0xF0;
|
||||
if ((tmp == 0x60) || (tmp == 0x90)) { // SW1
|
||||
command->sw[SW1] = procedure;
|
||||
// polling SW2, should be received already
|
||||
while(!sc_reader_get(&command->sw[SW2], 1));
|
||||
tpdu_done();
|
||||
}
|
||||
else if (residual_size) { // Need data
|
||||
state = TT_DATA;
|
||||
if (procedure == command->header[INS]) { // All data
|
||||
next_data_size = residual_size;
|
||||
}
|
||||
else if (procedure == (command->header[INS] ^ 0xFF)){ // Next byte
|
||||
next_data_size = 1;
|
||||
}
|
||||
else
|
||||
state = TT_ERROR;
|
||||
residual_size -= next_data_size;
|
||||
}
|
||||
else
|
||||
state = TT_ERROR;
|
||||
}
|
||||
break;
|
||||
case TT_DATA:
|
||||
// Process data if command requires data transfer
|
||||
if (next_data_size) {
|
||||
if (command->writeCommand) { // TX
|
||||
sc_reader_send(buf, next_data_size);
|
||||
buf += next_data_size;
|
||||
// Wait for TX finish
|
||||
state = TT_WAIT_FOR_TX_FINISH;
|
||||
}
|
||||
else if (sc_reader_get(buf, next_data_size)) { // RX
|
||||
buf += next_data_size;
|
||||
// All data received, check Status bytes
|
||||
state = TT_DETERMINE_PROCEDURE;
|
||||
}
|
||||
}
|
||||
else {
|
||||
state = TT_ERROR;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
/* Trigger callback for TPDU finish */
|
||||
__STATIC_INLINE void tpdu_done(void)
|
||||
{
|
||||
tpdu_reset();
|
||||
if (notification) {
|
||||
notification(command);
|
||||
}
|
||||
}
|
||||
|
||||
void tpdu_reset(void)
|
||||
{
|
||||
state = TT_IDLE;
|
||||
}
|
|
@ -0,0 +1,233 @@
|
|||
#include "uart.h"
|
||||
#include "gpio.h"
|
||||
|
||||
static UART_CTRL_TYPE * UART_0_CTRL = ((UART_CTRL_TYPE *) UART_0_CTRL_BASE);
|
||||
static UART_CTRL_TYPE * UART_1_CTRL = ((UART_CTRL_TYPE *) UART_1_CTRL_BASE);
|
||||
|
||||
void uartRx_callback(void);
|
||||
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
void uart_0_init(void)
|
||||
{
|
||||
PIN_CONFIG->PIN_15_SEL = PIN_SEL_UART_RXD0;
|
||||
PIN_CONFIG->PIN_16_SEL = PIN_SEL_UART_TXD0;
|
||||
|
||||
|
||||
PIN_CONFIG->PAD_21_INPUT_PULL_UP = 0;
|
||||
PIN_CONFIG->PAD_22_INPUT_PULL_UP = 0;
|
||||
|
||||
UART_0_CTRL->CLK_SEL = 0; /* 1=32M, 0=16M */
|
||||
|
||||
UART_0_CTRL->BAUDSEL = UART_BAUD_115200;
|
||||
UART_0_CTRL->FLOW_EN = UART_RTS_CTS_DISABLE;
|
||||
|
||||
UART_0_CTRL->RX_INT_MASK = 1; /* 1=MASK */
|
||||
UART_0_CTRL->TX_INT_MASK = 1; /* 1=MASK */
|
||||
|
||||
UART_0_CTRL->PAR_FAIL_INT_MASK = 1; /* 1=MASK */
|
||||
UART_0_CTRL->par_rx_even = 0; /* 1=Even, 0=Odd */
|
||||
UART_0_CTRL->par_rx_en = 0; /* 1=Rx Parity check enable */
|
||||
|
||||
UART_0_CTRL->par_tx_even = 0; /* 1=Even, 0=Odd */
|
||||
UART_0_CTRL->par_tx_en = 0; /* 1=Tx Parity check enable */
|
||||
|
||||
//clr Int Flag
|
||||
UART_0_CTRL->RI = 0;
|
||||
UART_0_CTRL->TI = 0;
|
||||
UART_0_CTRL->PAR_FAIL = 1;
|
||||
|
||||
UART_0_CTRL->RX_FLUSH = 1; /* clr rx fifo and set RX_FIFO_EMPTY */
|
||||
|
||||
NVIC_EnableIRQ(UART0_IRQn);
|
||||
|
||||
UART_0_CTRL->UART_EN = 1;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
UART0串口写函数
|
||||
参数: uint8_t data 要发送的数据
|
||||
注意:
|
||||
使用这个函数不可使能Tx中断
|
||||
*****************************************************************************/
|
||||
void uart_0_write(uint8_t data)
|
||||
{
|
||||
UART_0_CTRL->TX_DATA = data;
|
||||
while(UART_0_CTRL->TI == 0);
|
||||
UART_0_CTRL->TI = 0;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
UART0串口读Rx FIFO函数
|
||||
参数:
|
||||
pcnt - 返回读到的字节数
|
||||
pbuf - 指向数据存放的buf
|
||||
注意: FIFO长度为4,要求接收buf大小至少为4字节
|
||||
****************************************************************************/
|
||||
void uart_0_read(uint8_t *pcnt, uint8_t *pbuf)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
volatile uint8_t dly = 0; //如果用64M时钟,必须要加delay
|
||||
|
||||
while(!UART_0_CTRL->RX_FIFO_EMPTY)
|
||||
{
|
||||
*(pbuf+i) = UART_0_CTRL->RX_DATA;
|
||||
i++;
|
||||
|
||||
//延时400ns以上
|
||||
dly++;
|
||||
dly++;
|
||||
dly++;
|
||||
}
|
||||
|
||||
*pcnt = i;
|
||||
}
|
||||
|
||||
void uart_0_close(void)
|
||||
{
|
||||
UART_0_CTRL->UART_EN = 0;
|
||||
NVIC_DisableIRQ(UART0_IRQn);
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
UART0串口中断函数
|
||||
注意:
|
||||
RX_FLUSH只能置位RX_FIFO_EMPTY,不能清除RI
|
||||
*****************************************************************************/
|
||||
void UART0_IRQHandler(void)
|
||||
{
|
||||
if ((UART_0_CTRL->RI) == 1)
|
||||
{
|
||||
//读数据到buf[]中
|
||||
uint8_t len, buf[4];
|
||||
UART_0_CTRL->RI = 0; //先清标志,再读数据
|
||||
uart_0_read(&len, buf);
|
||||
}
|
||||
|
||||
#if 1
|
||||
if ((UART_0_CTRL->TI) == 1)
|
||||
{
|
||||
UART_0_CTRL->TI = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
if((UART_0_CTRL->PAR_FAIL) == 1)
|
||||
{
|
||||
UART_0_CTRL->PAR_FAIL = 1;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
void uart_1_init(void)
|
||||
{
|
||||
// PIN_CONFIG->PIN_0_SEL = PIN_SEL_UART_RXD1;
|
||||
// PIN_CONFIG->PIN_1_SEL = PIN_SEL_UART_TXD1;
|
||||
|
||||
PIN_CONFIG->PIN_12_SEL = PIN_SEL_UART_RXD1;
|
||||
PIN_CONFIG->PIN_13_SEL = PIN_SEL_UART_TXD1;
|
||||
|
||||
PIN_CONFIG->PAD_12_INPUT_PULL_UP = 0;
|
||||
PIN_CONFIG->PAD_13_INPUT_PULL_UP = 0;
|
||||
|
||||
UART_1_CTRL->CLK_SEL = 0; /* 1=32M, 0=16M */
|
||||
|
||||
UART_1_CTRL->BAUDSEL = UART_BAUD_2400;
|
||||
UART_1_CTRL->FLOW_EN = UART_RTS_CTS_DISABLE;
|
||||
|
||||
UART_1_CTRL->RX_INT_MASK = 0; /* 1=MASK */
|
||||
UART_1_CTRL->TX_INT_MASK = 1; /* 1=MASK */
|
||||
|
||||
UART_1_CTRL->PAR_FAIL_INT_MASK = 1; /* 1=MASK */
|
||||
UART_1_CTRL->par_rx_even = 0; /* 1=Even, 0=Odd */
|
||||
UART_1_CTRL->par_rx_en = 0; /* 1=Rx Parity check enable */
|
||||
|
||||
UART_1_CTRL->par_tx_even = 0; /* 1=Even, 0=Odd */
|
||||
UART_1_CTRL->par_tx_en = 0; /* 1=Tx Parity check enable */
|
||||
|
||||
//clr Int Flag
|
||||
UART_1_CTRL->RI = 0;
|
||||
UART_1_CTRL->TI = 0;
|
||||
UART_1_CTRL->PAR_FAIL = 1;
|
||||
|
||||
UART_1_CTRL->RX_FLUSH = 1; /* clr rx fifo and set RX_FIFO_EMPTY */
|
||||
|
||||
NVIC_EnableIRQ(UART1_IRQn);
|
||||
|
||||
UART_1_CTRL->UART_EN = 1;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
UART1串口写函数
|
||||
参数: uint8_t data 要发送的数据
|
||||
注意:
|
||||
使用这个函数不可使能Tx中断
|
||||
****************************************************************************/
|
||||
void uart_1_write(uint8_t data)
|
||||
{
|
||||
UART_1_CTRL->TX_DATA = data;
|
||||
while(UART_1_CTRL->TI == 0);
|
||||
UART_1_CTRL->TI = 0;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
UART1串口读Rx FIFO函数
|
||||
参数:
|
||||
pcnt - 返回读到的字节数
|
||||
pbuf - 指向数据存放的buf
|
||||
注意: FIFO长度为4,要求接收buf大小至少为4字节
|
||||
****************************************************************************/
|
||||
void uart_1_read(uint8_t *pcnt, uint8_t *pbuf)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
volatile uint8_t dly = 0; //如果用64M时钟,必须要加delay延时400ns以上
|
||||
|
||||
while(!UART_1_CTRL->RX_FIFO_EMPTY)
|
||||
{
|
||||
*(pbuf+i) = UART_1_CTRL->RX_DATA;
|
||||
i++;
|
||||
//
|
||||
dly++;
|
||||
dly++;
|
||||
dly++;
|
||||
}
|
||||
|
||||
*pcnt = i;
|
||||
}
|
||||
void uart_1_close(void)
|
||||
{
|
||||
UART_1_CTRL->UART_EN = 0;
|
||||
NVIC_DisableIRQ(UART1_IRQn);
|
||||
}
|
||||
|
||||
void uartRx_callback(void)
|
||||
{
|
||||
uint8_t cnt,temp[4];
|
||||
uart_1_read(&cnt,temp);
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
UART1串口中断函数
|
||||
注意:
|
||||
RX_FLUSH只能置位RX_FIFO_EMPTY,不能清除RI
|
||||
*****************************************************************************/
|
||||
void UART1_IRQHandler(void)
|
||||
{
|
||||
if ((UART_1_CTRL->RI) == 1)
|
||||
{
|
||||
//读数据到buf[]中
|
||||
|
||||
UART_1_CTRL->RI = 0; //先清标志,再读数据
|
||||
|
||||
uartRx_callback();
|
||||
}
|
||||
|
||||
#if 0
|
||||
if ((UART_1_CTRL->TI) == 1)
|
||||
{
|
||||
UART_1_CTRL->TI = 0;
|
||||
}
|
||||
#endif
|
||||
if((UART_1_CTRL->PAR_FAIL) == 1)
|
||||
{
|
||||
UART_1_CTRL->PAR_FAIL = 1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
#include "wdt.h"
|
||||
|
||||
#ifdef _WDT_
|
||||
|
||||
static WDT_CTRL_TYPE * WDT_CTRL = ((WDT_CTRL_TYPE *) WDT_BASE);
|
||||
/************************************************************************************
|
||||
看门狗初始化函数
|
||||
参数: uint16_t count 看门狗计数器初始值 计数器是递减计数器 单位:256/32.768 = 7.8ms
|
||||
*************************************************************************************/
|
||||
void wdt_enable(uint16_t count)
|
||||
{
|
||||
WDT_CTRL->WDT_TIME = count;
|
||||
WDT_CTRL->WDT_EN = 1;
|
||||
}
|
||||
|
||||
/*************************************************************************************
|
||||
清除看门狗函数
|
||||
这里将会把计数器的值设置回初始值
|
||||
**************************************************************************************/
|
||||
void wdt_clear()
|
||||
{
|
||||
WDT_CTRL->WDT_CLR=1;
|
||||
}
|
||||
|
||||
/*************************************************************************************
|
||||
禁用看门狗
|
||||
**************************************************************************************/
|
||||
void wdt_disable()
|
||||
{
|
||||
WDT_CTRL->WDT_EN = 0;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,67 @@
|
|||
/*****************************************************************
|
||||
;Project: Light
|
||||
;MCU:
|
||||
;Date:
|
||||
;File:
|
||||
;Function:
|
||||
******************************************************************/
|
||||
#include "Pow.h"
|
||||
#include "tick.h"
|
||||
#include "gpio.h"
|
||||
/*---------------------------------------------------------------*/
|
||||
//电池使能放电引脚初始化
|
||||
//void Pow_Init(void)
|
||||
//{
|
||||
// PIN_Set_GPIO(U32BIT(Battery_SW_PIN),PIN_SEL_GPIO); //引脚设置成普通IO口
|
||||
// GPIO_Set_Output(U32BIT(Battery_SW_PIN)); //设置引脚为输出模式
|
||||
// GPIO_Pin_Clear(U32BIT(Battery_SW_PIN)); //WS数据IO和使能IO都清零
|
||||
//}
|
||||
/*---------------------------------------------------------------*/
|
||||
//电池放电开关
|
||||
//static void PowOpen(bool Flag)
|
||||
//{
|
||||
// GPIO_Set_Output(U32BIT(Battery_SW_PIN));
|
||||
|
||||
// if(Flag == false)
|
||||
// {
|
||||
// GPIO_Pin_Clear(U32BIT(Battery_SW_PIN));
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// GPIO_Pin_Set(U32BIT(Battery_SW_PIN));
|
||||
// }
|
||||
//}
|
||||
/*---------------------------------------------------------------*/
|
||||
//控制电池放电的延迟开关
|
||||
//void Pow_Ctrl(bool Flag)
|
||||
//{
|
||||
// static unsigned short timer_Pow = 0;
|
||||
// static unsigned char Pow_state=0; //电源目前状态
|
||||
|
||||
// if(Flag == true)
|
||||
// {
|
||||
// switch (Pow_state)
|
||||
// {
|
||||
// case 0:
|
||||
// TimerSet(timer_Pow, 2000); //设置2000ms延时
|
||||
// Pow_state = 1;
|
||||
// break;
|
||||
// case 1: //等待闹钟
|
||||
// if(TimerCheck(timer_Pow,2000))
|
||||
// {
|
||||
// PowOpen(true); //开电
|
||||
// Pow_state = 0;
|
||||
// }
|
||||
// break;
|
||||
// default:
|
||||
// Pow_state = 0;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// PowOpen(false);
|
||||
// Pow_state = 0;
|
||||
// }
|
||||
//}
|
||||
/*---------------------------------------------------------------*/
|
|
@ -0,0 +1,23 @@
|
|||
/*****************************************************************
|
||||
;Project: Light
|
||||
;MCU:
|
||||
;Date:
|
||||
;File:
|
||||
;Function:
|
||||
******************************************************************/
|
||||
#ifndef __POW_H
|
||||
#define __POW_H
|
||||
/*---------------------------------------------------------------*/
|
||||
#include "ARMCM0.h"
|
||||
/*---------------------------------------------------------------*/
|
||||
//º¯ÊýÉùÃ÷
|
||||
//void Pow_Init(void);
|
||||
//void Pow_Ctrl(bool Flag);
|
||||
/*---------------------------------------------------------------*/
|
||||
//ºê¶¨Òå
|
||||
// #define Battery_SW_PIN GPIO_14
|
||||
|
||||
#endif
|
||||
/*---------------------------------------------------------------*/
|
||||
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/*************************************************************************************************
|
||||
**Filename: adc.c
|
||||
**Version :
|
||||
**Author :
|
||||
**Date :
|
||||
**Description:
|
||||
**
|
||||
*************************************************************************************************/
|
||||
#include "adc.h"
|
||||
#include "mcu_bsp.h"
|
||||
#include "gpadc.h"
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description :
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
uint8_t Get_Battery_Value()
|
||||
{
|
||||
GPADC_channel_sel(ADCGP_CH);
|
||||
GPADC_start(cal_battery_value);
|
||||
syd_nosleep((bool)true,SYD_NOSLEEP_ADC);
|
||||
if(connect_flag)
|
||||
{
|
||||
BLE_NotifyBatteryLevel();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
/*************************************************************************************************
|
||||
**Filename: adc.h
|
||||
**Version :
|
||||
**Author :
|
||||
**Date :
|
||||
**Description:
|
||||
**
|
||||
*************************************************************************************************/
|
||||
#ifndef __ADC_H
|
||||
#define __ADC_H
|
||||
|
||||
#include "ARMCM0.h"
|
||||
|
||||
#endif
|
||||
/*---------------------------------------------------------------*/
|
||||
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,293 @@
|
|||
/*
|
||||
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef cJSON__h
|
||||
#define cJSON__h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
|
||||
#define __WINDOWS__
|
||||
#endif
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
|
||||
/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options:
|
||||
|
||||
CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols
|
||||
CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default)
|
||||
CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol
|
||||
|
||||
For *nix builds that support visibility attribute, you can define similar behavior by
|
||||
|
||||
setting default visibility to hidden by adding
|
||||
-fvisibility=hidden (for gcc)
|
||||
or
|
||||
-xldscope=hidden (for sun cc)
|
||||
to CFLAGS
|
||||
|
||||
then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does
|
||||
|
||||
*/
|
||||
|
||||
#define CJSON_CDECL __cdecl
|
||||
#define CJSON_STDCALL __stdcall
|
||||
|
||||
/* export symbols by default, this is necessary for copy pasting the C and header file */
|
||||
#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS)
|
||||
#define CJSON_EXPORT_SYMBOLS
|
||||
#endif
|
||||
|
||||
#if defined(CJSON_HIDE_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) type CJSON_STDCALL
|
||||
#elif defined(CJSON_EXPORT_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL
|
||||
#elif defined(CJSON_IMPORT_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL
|
||||
#endif
|
||||
#else /* !__WINDOWS__ */
|
||||
#define CJSON_CDECL
|
||||
#define CJSON_STDCALL
|
||||
|
||||
#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
|
||||
#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type
|
||||
#else
|
||||
#define CJSON_PUBLIC(type) type
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* project version */
|
||||
#define CJSON_VERSION_MAJOR 1
|
||||
#define CJSON_VERSION_MINOR 7
|
||||
#define CJSON_VERSION_PATCH 14
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/* cJSON Types: */
|
||||
#define cJSON_Invalid (0)
|
||||
#define cJSON_False (1 << 0)
|
||||
#define cJSON_True (1 << 1)
|
||||
#define cJSON_NULL (1 << 2)
|
||||
#define cJSON_Number (1 << 3)
|
||||
#define cJSON_String (1 << 4)
|
||||
#define cJSON_Array (1 << 5)
|
||||
#define cJSON_Object (1 << 6)
|
||||
#define cJSON_Raw (1 << 7) /* raw json */
|
||||
|
||||
#define cJSON_IsReference 256
|
||||
#define cJSON_StringIsConst 512
|
||||
|
||||
/* The cJSON structure: */
|
||||
typedef struct cJSON
|
||||
{
|
||||
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
|
||||
struct cJSON *next;
|
||||
struct cJSON *prev;
|
||||
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
|
||||
struct cJSON *child;
|
||||
|
||||
/* The type of the item, as above. */
|
||||
int type;
|
||||
|
||||
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
|
||||
char *valuestring;
|
||||
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
|
||||
int valueint;
|
||||
/* The item's number, if type==cJSON_Number */
|
||||
double valuedouble;
|
||||
|
||||
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
|
||||
char *string;
|
||||
} cJSON;
|
||||
|
||||
typedef struct cJSON_Hooks
|
||||
{
|
||||
/* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */
|
||||
void *(CJSON_CDECL *malloc_fn)(size_t sz);
|
||||
void (CJSON_CDECL *free_fn)(void *ptr);
|
||||
} cJSON_Hooks;
|
||||
|
||||
typedef int cJSON_bool;
|
||||
|
||||
/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them.
|
||||
* This is to prevent stack overflows. */
|
||||
#ifndef CJSON_NESTING_LIMIT
|
||||
#define CJSON_NESTING_LIMIT 1000
|
||||
#endif
|
||||
|
||||
/* returns the version of cJSON as a string */
|
||||
CJSON_PUBLIC(const char*) cJSON_Version(void);
|
||||
|
||||
/* Supply malloc, realloc and free functions to cJSON */
|
||||
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
|
||||
|
||||
/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
|
||||
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length);
|
||||
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
|
||||
/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated);
|
||||
|
||||
/* Render a cJSON entity to text for transfer/storage. */
|
||||
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
|
||||
/* Render a cJSON entity to text for transfer/storage without any formatting. */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
|
||||
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
|
||||
/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
|
||||
/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
|
||||
/* Delete a cJSON entity and all subentities. */
|
||||
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item);
|
||||
|
||||
/* Returns the number of items in an array (or object). */
|
||||
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
|
||||
/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
|
||||
/* Get item "string" from object. Case insensitive. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
|
||||
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
|
||||
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
|
||||
|
||||
/* Check item type and return its value */
|
||||
CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item);
|
||||
CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item);
|
||||
|
||||
/* These functions check the type of an item */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
|
||||
|
||||
/* These calls create a cJSON item of the appropriate type. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
|
||||
/* raw json */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
|
||||
|
||||
/* Create a string where valuestring references a string so
|
||||
* it will not be freed by cJSON_Delete */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string);
|
||||
/* Create an object/array that only references it's elements so
|
||||
* they will not be freed by cJSON_Delete */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
|
||||
|
||||
/* These utilities create an Array of count items.
|
||||
* The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count);
|
||||
|
||||
/* Append item to the specified array/object. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
|
||||
/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object.
|
||||
* WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before
|
||||
* writing to `item->string` */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
|
||||
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
|
||||
|
||||
/* Remove/Detach items from Arrays/Objects. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
|
||||
|
||||
/* Update array items. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
|
||||
|
||||
/* Duplicate a cJSON item */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
|
||||
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
|
||||
* need to be released. With recurse!=0, it will duplicate any children connected to the item.
|
||||
* The item->next and ->prev pointers are always zero on return from Duplicate. */
|
||||
/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal.
|
||||
* case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
|
||||
|
||||
/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings.
|
||||
* The input pointer json cannot point to a read-only address area, such as a string constant,
|
||||
* but should point to a readable and writable adress area. */
|
||||
CJSON_PUBLIC(void) cJSON_Minify(char *json);
|
||||
|
||||
/* Helper functions for creating and adding items to an object at the same time.
|
||||
* They return the added item or NULL on failure. */
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name);
|
||||
|
||||
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
|
||||
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
|
||||
/* helper for the cJSON_SetNumberValue macro */
|
||||
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
|
||||
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
|
||||
/* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */
|
||||
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring);
|
||||
|
||||
/* Macro for iterating over an array or object */
|
||||
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
|
||||
|
||||
/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */
|
||||
CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
|
||||
CJSON_PUBLIC(void) cJSON_free(void *object);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,48 @@
|
|||
/*************************************************************************************************
|
||||
**Filename:
|
||||
**Version :
|
||||
**Author :
|
||||
**Date :
|
||||
**Description:
|
||||
**
|
||||
*************************************************************************************************/
|
||||
/*********************************************************************
|
||||
* Include (include files)
|
||||
*/
|
||||
#include "common.h"
|
||||
|
||||
/************************************************************************************************
|
||||
* Function : stringLength
|
||||
* Param in : none
|
||||
* Param out: none
|
||||
* Return : none
|
||||
* Describe : ×Ö·û´®³¤¶È
|
||||
*/
|
||||
int stringLength (const char *string)
|
||||
{
|
||||
const char *cptr = string;
|
||||
|
||||
while ( *cptr )
|
||||
++cptr;
|
||||
return cptr - string;
|
||||
}
|
||||
/************************************************************************************************
|
||||
* Function : str_cmp
|
||||
* Param in : none
|
||||
* Param out: none
|
||||
* Return : none
|
||||
* Describe : ×Ö·û´®¿½±´
|
||||
*/
|
||||
unsigned char str_cmp(unsigned char *p1,unsigned char *p2,unsigned char len)
|
||||
{
|
||||
unsigned char i=0;
|
||||
|
||||
while(i<len){
|
||||
if(p1[i]!=p2[i])
|
||||
return 0;
|
||||
i++;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
|
@ -0,0 +1,124 @@
|
|||
/*************************************CopyRight(c)************************************************
|
||||
** ½Î÷ÂÀ²¼¿Æ¼¼ÓÐÏÞ¹«Ë¾
|
||||
** www.lvbu.wang
|
||||
**************************************************************************************************
|
||||
**Filename:
|
||||
**Version:
|
||||
**Author:
|
||||
**Date:
|
||||
**Description:
|
||||
**
|
||||
**************************************************************************************************
|
||||
**Version:
|
||||
**Modifier:
|
||||
**Datemodified:
|
||||
**Description:
|
||||
**
|
||||
**************************************************************************************************/
|
||||
#ifndef LVBU_COMMON_H
|
||||
#define LVBU_COMMON_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* Include
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Macros
|
||||
*/
|
||||
/** The upper 8 bits of a 32 bit value */
|
||||
//lint -emacro(572,MSB_32) // Suppress warning 572 "Excessive shift value"
|
||||
#define MSB_32(a) (((a) & 0xFF000000) >> 24)
|
||||
/** The lower 8 bits (of a 32 bit value) */
|
||||
#define LSB_32(a) ((a) & 0x000000FF)
|
||||
|
||||
/** The upper 8 bits of a 16 bit value */
|
||||
//lint -emacro(572,MSB_16) // Suppress warning 572 "Excessive shift value"
|
||||
#define MSB_16(a) (((a) & 0xFF00) >> 8)
|
||||
/** The lower 8 bits (of a 16 bit value) */
|
||||
#define LSB_16(a) ((a) & 0x00FF)
|
||||
|
||||
/** Leaves the minimum of the two 32-bit arguments */
|
||||
/*lint -emacro(506, MIN) */ /* Suppress "Constant value Boolean */
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
/** Leaves the maximum of the two 32-bit arguments */
|
||||
/*lint -emacro(506, MAX) */ /* Suppress "Constant value Boolean */
|
||||
#define MAX(a, b) ((a) < (b) ? (b) : (a))
|
||||
|
||||
#define STRINGIFY_(val) #val
|
||||
/** Converts a macro argument into a character constant.
|
||||
*/
|
||||
#define STRINGIFY(val) STRINGIFY_(val)
|
||||
|
||||
/** Counts number of elements inside the array
|
||||
*/
|
||||
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
|
||||
|
||||
/**@brief Set a bit in the uint32 word.
|
||||
*
|
||||
* @param[in] W Word whose bit is being set.
|
||||
* @param[in] B Bit number in the word to be set.
|
||||
*/
|
||||
#define SET_BIT(W, B) ((W) |= (uint32_t)(1U << (B)))
|
||||
|
||||
/**@brief Clears a bit in the uint32 word.
|
||||
*
|
||||
* @param[in] W Word whose bit is to be cleared.
|
||||
* @param[in] B Bit number in the word to be cleared.
|
||||
*/
|
||||
#define CLR_BIT(W, B) ((W) &= (~(uint32_t)(1U << (B))))
|
||||
|
||||
/**@brief Checks if a bit is set.
|
||||
*
|
||||
* @param[in] W Word whose bit is to be checked.
|
||||
* @param[in] B Bit number in the word to be checked.
|
||||
*
|
||||
* @retval 1 if bit is set.
|
||||
* @retval 0 if bit is not set.
|
||||
*/
|
||||
#define IS_SET(W, B) (((W) >> (B)) & 1)
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* Constants
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Typedefs
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Global Variables
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Global Functions
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* External Variables
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* External Functions
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Local Vriables
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Local Functions
|
||||
*/
|
||||
int stringLength (const char *string);
|
||||
unsigned char str_cmp(unsigned char *p1,unsigned char *p2,unsigned char len);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* LVBU_COMMON_H */
|
||||
|
|
@ -0,0 +1,136 @@
|
|||
/**
|
||||
* Copyright (c) 2013 - 2020, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, must reproduce the above copyright notice, this list of
|
||||
* conditions and the following disclaimer in the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "crc16.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/* CRC16 implementation acording to CCITT standards */
|
||||
static const unsigned short crc16tab[256]= {
|
||||
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
|
||||
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
|
||||
0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,
|
||||
0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,
|
||||
0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,
|
||||
0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,
|
||||
0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,
|
||||
0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,
|
||||
0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,
|
||||
0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,
|
||||
0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,
|
||||
0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,
|
||||
0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,
|
||||
0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,
|
||||
0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,
|
||||
0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,
|
||||
0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,
|
||||
0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,
|
||||
0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,
|
||||
0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,
|
||||
0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,
|
||||
0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
|
||||
0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,
|
||||
0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,
|
||||
0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,
|
||||
0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,
|
||||
0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,
|
||||
0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,
|
||||
0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,
|
||||
0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,
|
||||
0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,
|
||||
0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
|
||||
};
|
||||
|
||||
|
||||
uint16_t crc16_compute(uint8_t const * p_data, uint32_t size, uint16_t const * p_crc)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
|
||||
uint16_t crc = (p_crc == NULL) ? 0xFFFF : *p_crc;
|
||||
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
crc = (uint8_t)(crc >> 8) | (crc << 8);
|
||||
crc ^= p_data[i];
|
||||
crc ^= (uint8_t)(crc & 0xFF) >> 4;
|
||||
crc ^= (crc << 8) << 4;
|
||||
crc ^= ((crc & 0xFF) << 4) << 1;
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
// modbus crc Êý¾ÝУÑé
|
||||
uint16_t crc16_modbus(uint8_t const *p_data,uint32_t size)
|
||||
{
|
||||
uint32_t i,j;
|
||||
uint16_t crc;
|
||||
crc=0xffff;
|
||||
for(i=0;i<size;i++)
|
||||
{
|
||||
crc^=*(p_data+i);
|
||||
for(j=0;j<8;j++)
|
||||
{
|
||||
if(crc&0x01)
|
||||
{
|
||||
crc>>=1;
|
||||
crc^=0xA001;
|
||||
}
|
||||
else
|
||||
{
|
||||
crc>>=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (crc);
|
||||
}
|
||||
|
||||
|
||||
//CRC16 crc16_ccitt
|
||||
|
||||
unsigned short crc16_ccitt(const unsigned char *buf, int len)
|
||||
{
|
||||
register int counter;
|
||||
register unsigned short crc = 0;
|
||||
for( counter = 0; counter < len; counter++)
|
||||
crc = (crc<<8) ^ crc16tab[((crc>>8) ^ *(char *)buf++)&0x00FF];
|
||||
return crc;
|
||||
}
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
/**
|
||||
* Copyright (c) 2013 - 2020, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, must reproduce the above copyright notice, this list of
|
||||
* conditions and the following disclaimer in the documentation and/or other
|
||||
* materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
/** @file
|
||||
*
|
||||
* @defgroup crc16 CRC16 compute
|
||||
* @{
|
||||
* @ingroup hci_transport
|
||||
*
|
||||
* @brief This module implements CRC-16-CCITT (polynomial 0x1021) with 0xFFFF initial value.
|
||||
* The data can be passed in multiple blocks.
|
||||
*/
|
||||
|
||||
#ifndef CRC16_H__
|
||||
#define CRC16_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**@brief Function for calculating CRC-16 in blocks.
|
||||
*
|
||||
* Feed each consecutive data block into this function, along with the current value of p_crc as
|
||||
* returned by the previous call of this function. The first call of this function should pass NULL
|
||||
* as the initial value of the crc in p_crc.
|
||||
*
|
||||
* @param[in] p_data The input data block for computation.
|
||||
* @param[in] size The size of the input data block in bytes.
|
||||
* @param[in] p_crc The previous calculated CRC-16 value or NULL if first call.
|
||||
*
|
||||
* @return The updated CRC-16 value, based on the input supplied.
|
||||
*/
|
||||
uint16_t crc16_compute(uint8_t const * p_data, uint32_t size, uint16_t const * p_crc);
|
||||
|
||||
uint16_t crc16_modbus(uint8_t const *p_data,uint32_t size);
|
||||
unsigned short crc16_ccitt(const unsigned char *buf, int len);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // CRC16_H__
|
||||
|
||||
/** @} */
|
|
@ -0,0 +1,73 @@
|
|||
/*****************************************************************
|
||||
;Project: Light
|
||||
;MCU:
|
||||
;Date:
|
||||
;File:
|
||||
;Function:
|
||||
******************************************************************/
|
||||
#include "key.h"
|
||||
#include "gpio.h"
|
||||
#include "delay.h"
|
||||
#include "config.h"
|
||||
#include "DebugLog.h"
|
||||
/*---------------------------------------------------------------*/
|
||||
//外部变量调用声明
|
||||
extern TaskFlagUnion_t tTaskFlag;
|
||||
extern uint8_t Adv_State;
|
||||
/*---------------------------------------------------------------*/
|
||||
//函数声明
|
||||
void GPIO_callback(void);
|
||||
/*---------------------------------------------------------------*/
|
||||
//GPIO初始化
|
||||
//把不用到的GPIO口配置成上拉输入
|
||||
void Gpio_Init(void)
|
||||
{
|
||||
PIN_Set_GPIO(0x01FF0F84,PIN_SEL_GPIO);
|
||||
GPIO_Set_Input( 0x01FF0F84,0x01FF0F84);
|
||||
PIN_Pullup_Enable(T_QFN_48,0x01FF0F84);
|
||||
GPIO_Input_Enable(0x01FF0F84);
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//按键初始化
|
||||
void Key_Init(void)
|
||||
{
|
||||
PIN_Set_GPIO(U32BIT(SHOCK_PIN),PIN_SEL_GPIO);
|
||||
GPIO_Set_Input( U32BIT(SHOCK_PIN),0); //设置为输入 不取反
|
||||
PIN_Pullup_Enable(T_QFN_48, U32BIT(SHOCK_PIN));
|
||||
GPIO_Input_Enable(U32BIT(SHOCK_PIN));
|
||||
io_irq_enable(U32BIT(SHOCK_PIN),GPIO_callback);
|
||||
|
||||
//初始化充电状态引脚
|
||||
PIN_Set_GPIO(U32BIT(CHARGE_PIN),PIN_SEL_GPIO);
|
||||
GPIO_Set_Input( U32BIT(CHARGE_PIN),0); //设置为输入 不取反
|
||||
PIN_Pullup_Disable(T_QFN_48, U32BIT(CHARGE_PIN)); //关闭上拉输入
|
||||
GPIO_Input_Enable(U32BIT(CHARGE_PIN));
|
||||
io_irq_disable(U32BIT(CHARGE_PIN)); //关闭IO口中断功能
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//按键初始化
|
||||
void GPIO_callback(void)
|
||||
{
|
||||
unsigned int SW;
|
||||
|
||||
SW=GPIO_IRQ_CTRL->GPIO_INT;
|
||||
|
||||
if(SW&U32BIT(SHOCK_PIN))
|
||||
{
|
||||
delay_ms(1);
|
||||
if((GPIO_Pin_Read(U32BIT(SHOCK_PIN)) & U32BIT(SHOCK_PIN))==0)
|
||||
{
|
||||
tTaskFlag.tFlag.shockFlag = 1;
|
||||
}
|
||||
|
||||
if(Adv_State == 0) //重启广播
|
||||
{
|
||||
StartAdv();
|
||||
Adv_State = 1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
/*****************************************************************
|
||||
;Project: Light
|
||||
;MCU:
|
||||
;Date:
|
||||
;File:
|
||||
;Function:
|
||||
******************************************************************/
|
||||
#ifndef __KEY_H
|
||||
#define __KEY_H
|
||||
/*---------------------------------------------------------------*/
|
||||
#include "ARMCM0.h"
|
||||
/*---------------------------------------------------------------*/
|
||||
//º¯ÊýÉùÃ÷
|
||||
void Key_Init(void);
|
||||
void Gpio_Init(void);
|
||||
/*---------------------------------------------------------------*/
|
||||
//ºê¶¨Òå
|
||||
#define SHOCK_PIN GPIO_25
|
||||
#define CHARGE_PIN GPIO_3
|
||||
|
||||
#endif
|
||||
/*---------------------------------------------------------------*/
|
||||
|
||||
|
|
@ -0,0 +1,176 @@
|
|||
/*************************************************************************************************
|
||||
**Filename: lis2dh.c
|
||||
**Version :
|
||||
**Author :
|
||||
**Date :
|
||||
**Description:
|
||||
**
|
||||
*************************************************************************************************/
|
||||
#include "lis2dh.h"
|
||||
#include "delay.h"
|
||||
#include "gpio.h"
|
||||
#include "i2c.h"
|
||||
#include "mcu_bsp.h"
|
||||
#include "DebugLog.h"
|
||||
|
||||
/*********************************************************************
|
||||
* Global Variables (declared in header file with 'extern')
|
||||
*/
|
||||
stmdev_ctx_t dev_ctx1;
|
||||
/*********************************************************************
|
||||
* Static Functions ('static')
|
||||
*/
|
||||
static void platform1_delay(uint16_t ms);
|
||||
static int32_t platform1_write(uint8_t I2C_addr, uint8_t addr, uint8_t* value, uint16_t len);
|
||||
static int32_t platform1_read(uint8_t I2C_addr,uint8_t regAddress, uint8_t * data, uint16_t len);
|
||||
static int32_t lis2dh12_xyz_axis_enable_set(const stmdev_ctx_t *ctx, uint8_t val);
|
||||
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description : Lis2dh 初始化
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
void Lis2dh_Init(void)
|
||||
{
|
||||
uint8_t temp = 0;
|
||||
lis2dh12_fs_t fs;
|
||||
lis2dh12_ctrl_reg6_t reg6;
|
||||
|
||||
dev_ctx1.write_reg = platform1_write;
|
||||
dev_ctx1.read_reg = platform1_read;
|
||||
dev_ctx1.mdelay = platform1_delay;
|
||||
dev_ctx1.master_addr = LIS2DH_ADDRESS;
|
||||
|
||||
platform1_delay(100);
|
||||
|
||||
lis2dh12_device_id_get(&dev_ctx1, &temp);
|
||||
|
||||
if(temp == LIS2DH12_ID)
|
||||
{
|
||||
//dbg_printf("lis2dh exist\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
// dbg_printf("lis2dh not exist\r\n");
|
||||
}
|
||||
|
||||
//软件复位, 寄存器到默认配值
|
||||
// lis2dh12_boot_set(&dev_ctx1,PROPERTY_ENABLE);
|
||||
//
|
||||
// platform1_delay(100);
|
||||
//
|
||||
// lis2dh12_boot_set(&dev_ctx1,PROPERTY_DISABLE);
|
||||
|
||||
// lis2dh12_full_scale_get(&dev_ctx1,&fs);
|
||||
//
|
||||
// dbg_printf("fs = %d\r\n",fs);
|
||||
//
|
||||
// lis2dh12_full_scale_set(&dev_ctx1, LIS2DH12_4g);
|
||||
//
|
||||
// lis2dh12_full_scale_get(&dev_ctx1,&fs);
|
||||
//
|
||||
// dbg_printf("fs = %d\r\n",fs);
|
||||
|
||||
lis2dh12_full_scale_set(&dev_ctx1, LIS2DH12_2g); //满量程设置
|
||||
|
||||
lis2dh12_data_rate_set(&dev_ctx1,LIS2DH12_ODR_100Hz);//设置输出数据率
|
||||
|
||||
lis2dh12_act_threshold_set(&dev_ctx1, 0x10); //设置活动/非活动阈值
|
||||
|
||||
lis2dh12_act_timeout_set(&dev_ctx1, 0x10); //设置活动/非活动时间超时
|
||||
|
||||
lis2dh12_operating_mode_set(&dev_ctx1, LIS2DH12_LP_8bit); //设置低功耗模式
|
||||
|
||||
lis2dh12_block_data_update_set(&dev_ctx1,PROPERTY_ENABLE);//输出寄存器更新时锁定
|
||||
|
||||
reg6.i2_act = 1;
|
||||
reg6.int_polarity = 0;
|
||||
lis2dh12_pin_int2_config_set(&dev_ctx1,®6); //活动事件信号映射到 INT2 引脚
|
||||
|
||||
lis2dh12_xyz_axis_enable_set(&dev_ctx1,PROPERTY_ENABLE); //使能三轴加速度计
|
||||
}
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description : Lis2dh活动状态判断
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
void Lis2dh_Act_State_Judge(void)
|
||||
{
|
||||
if((GPIO_Pin_Read(U32BIT(LIS2D_INT2)) & U32BIT(LIS2D_INT2))!= 0)
|
||||
{
|
||||
dbg_printf("Act_State\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
dbg_printf("Inact_State\r\n");
|
||||
}
|
||||
}
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description : 使能XYZ的加速度传感器
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
static int32_t lis2dh12_xyz_axis_enable_set(const stmdev_ctx_t *ctx, uint8_t val)
|
||||
{
|
||||
lis2dh12_ctrl_reg1_t ctrl_reg1;
|
||||
int32_t ret;
|
||||
|
||||
ret = lis2dh12_read_reg(ctx, LIS2DH12_CTRL_REG1, (uint8_t *)&ctrl_reg1, 1);
|
||||
|
||||
if (ret == 0) {
|
||||
ctrl_reg1.xen = val;
|
||||
ctrl_reg1.yen = val;
|
||||
ctrl_reg1.zen = val;
|
||||
ret = lis2dh12_write_reg(ctx, LIS2DH12_CTRL_REG1, (uint8_t *)&ctrl_reg1, 1);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description : 寄存器写
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
static int32_t platform1_write(uint8_t I2C_addr, uint8_t addr, uint8_t* value, uint16_t len)
|
||||
{
|
||||
ErrorStatus bret = SUCCESS;
|
||||
|
||||
bret = i2c_1_write(I2C_addr, 1, addr, value, len);
|
||||
|
||||
return (int32_t)(!bret);
|
||||
}
|
||||
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description : 寄存器读
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
static int32_t platform1_read(uint8_t I2C_addr,uint8_t regAddress, uint8_t * data, uint16_t len)
|
||||
{
|
||||
ErrorStatus bret = SUCCESS;
|
||||
|
||||
bret = i2c_1_read(I2C_addr, 1, regAddress, data, len);
|
||||
|
||||
return (int32_t)(!bret);
|
||||
}
|
||||
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description : 延时
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
static void platform1_delay(uint16_t ms)
|
||||
{
|
||||
delay_ms(ms);
|
||||
}
|
||||
|
||||
/*************************************************************************************************/
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
/*************************************************************************************************
|
||||
**Filename: lis2dh.h
|
||||
**Version :
|
||||
**Author :
|
||||
**Date :
|
||||
**Description:
|
||||
**
|
||||
*************************************************************************************************/
|
||||
#ifndef __LIS2D_H_
|
||||
#define __LIS2D_H_
|
||||
|
||||
#include "ARMCM0.h"
|
||||
#include "lis2dh12_reg.h"
|
||||
|
||||
#define LIS2DH_ADDRESS (LIS2DH12_I2C_ADD_H >> 1)
|
||||
|
||||
/*********************************************************************
|
||||
* Global Function declared ('extern')
|
||||
*/
|
||||
void Lis2dh_Init(void);
|
||||
void Lis2dh_Act_State_Judge(void);
|
||||
|
||||
#endif
|
||||
|
||||
/*************************************************************************************************/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,142 @@
|
|||
/*****************************************************************
|
||||
;Project:
|
||||
;MCU:
|
||||
;Date:
|
||||
;File:
|
||||
;Function:
|
||||
******************************************************************/
|
||||
#include "lis2dw.h"
|
||||
#include "gpio.h"
|
||||
#include "i2c.h"
|
||||
#include "lis2dw12_reg.h"
|
||||
#include "delay.h"
|
||||
#include "DebugLog.h"
|
||||
|
||||
stmdev_ctx_t dev_ctx1;
|
||||
|
||||
static void platform1_delay(uint16_t ms);
|
||||
static int32_t platform1_write(uint8_t I2C_addr, uint8_t addr, uint8_t* value, uint16_t len);
|
||||
static int32_t platform1_read(uint8_t I2C_addr,uint8_t regAddress, uint8_t * data, uint16_t len);
|
||||
|
||||
/*---------------------------------------------------------------*/
|
||||
//Lis2dw IO 初始化
|
||||
void Lis2dw_IO_Init(void)
|
||||
{
|
||||
//IIC IO初始化
|
||||
i2c_Init(I2C1,U32BIT(I2C1_SCLK),U32BIT(I2C1_SDA));
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------*/
|
||||
//Lis2dw 初始化
|
||||
void Lis2dw_Init(void)
|
||||
{
|
||||
uint8_t temp = 0;
|
||||
lis2dw12_ctrl4_int1_pad_ctrl_t int_route;
|
||||
|
||||
dev_ctx1.write_reg = platform1_write;
|
||||
dev_ctx1.read_reg = platform1_read;
|
||||
dev_ctx1.mdelay = platform1_delay;
|
||||
dev_ctx1.master_addr = LIS2DW_ADDRESS;
|
||||
|
||||
Lis2dw_IO_Init();
|
||||
|
||||
//获取设置Id
|
||||
lis2dw12_device_id_get(&dev_ctx1, &temp);
|
||||
|
||||
if(temp == LIS2DW_ID)
|
||||
{
|
||||
dbg_printf("device exist\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
dbg_printf("device not exist\r\n");
|
||||
}
|
||||
|
||||
//软件复位, 寄存器到默认配值
|
||||
lis2dw12_reset_set(&dev_ctx1, PROPERTY_ENABLE);
|
||||
|
||||
do {
|
||||
lis2dw12_reset_get(&dev_ctx1, &temp);
|
||||
} while (temp);
|
||||
|
||||
//设置满量程范围
|
||||
lis2dw12_full_scale_set(&dev_ctx1, LIS2DW12_2g);
|
||||
|
||||
//设置低通滤波和滤波器带宽
|
||||
lis2dw12_filter_path_set(&dev_ctx1, LIS2DW12_LPF_ON_OUT);
|
||||
lis2dw12_filter_bandwidth_set(&dev_ctx1,LIS2DW12_ODR_DIV_4);
|
||||
|
||||
//配置电源模式
|
||||
lis2dw12_power_mode_set(&dev_ctx1, LIS2DW12_CONT_LOW_PWR_12bit);
|
||||
|
||||
//设置加速度计的唤醒持续时间
|
||||
lis2dw12_wkup_dur_set(&dev_ctx1,2);
|
||||
|
||||
//设置进入睡眠模式前的持续时间
|
||||
lis2dw12_act_sleep_dur_set(&dev_ctx1,2);
|
||||
|
||||
//设置唤醒加速度计所需的活动阈值
|
||||
lis2dw12_wkup_threshold_set(&dev_ctx1,2);
|
||||
|
||||
lis2dw12_wkup_feed_data_set(&dev_ctx1,LIS2DW12_HP_FEED);
|
||||
|
||||
//设置加速度计检测活动或静止的模式
|
||||
lis2dw12_act_mode_set(&dev_ctx1, LIS2DW12_DETECT_ACT_INACT);
|
||||
|
||||
//使能活动检测中断
|
||||
lis2dw12_pin_int1_route_get(&dev_ctx1, &int_route);
|
||||
int_route.int1_wu = PROPERTY_ENABLE;
|
||||
lis2dw12_pin_int1_route_set(&dev_ctx1, &int_route);
|
||||
|
||||
//设置加速度计的数据输出速率
|
||||
lis2dw12_data_rate_set(&dev_ctx1, LIS2DW12_XL_ODR_200Hz);
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------*/
|
||||
//判断是在静止或者运动状态
|
||||
void Act_State_Judge(void)
|
||||
{
|
||||
lis2dw12_all_sources_t all_source;
|
||||
/* Read status register */
|
||||
lis2dw12_all_sources_get(&dev_ctx1, &all_source);
|
||||
|
||||
/* Check if Activity/Inactivity events */
|
||||
if(all_source.wake_up_src.sleep_state_ia)
|
||||
{
|
||||
dbg_printf("Inactivity Detected\r\n");
|
||||
}
|
||||
|
||||
if(all_source.wake_up_src.wu_ia)
|
||||
{
|
||||
dbg_printf("Activity Detected\r\n");
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//寄存器写
|
||||
static int32_t platform1_write(uint8_t I2C_addr, uint8_t addr, uint8_t* value, uint16_t len)
|
||||
{
|
||||
ErrorStatus bret = SUCCESS;
|
||||
|
||||
bret = i2c_1_write(I2C_addr, 1, addr, value, len);
|
||||
|
||||
return (int32_t)(!bret);
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------*/
|
||||
//寄存器读
|
||||
static int32_t platform1_read(uint8_t I2C_addr,uint8_t regAddress, uint8_t * data, uint16_t len)
|
||||
{
|
||||
ErrorStatus bret = SUCCESS;
|
||||
|
||||
bret = i2c_1_read(I2C_addr, 1, regAddress, data, len);
|
||||
|
||||
return (int32_t)(!bret);
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------*/
|
||||
//延时
|
||||
static void platform1_delay(uint16_t ms)
|
||||
{
|
||||
delay_ms(ms);
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
|
@ -0,0 +1,24 @@
|
|||
/*****************************************************************
|
||||
;Project:
|
||||
;MCU:
|
||||
;Date:
|
||||
;File:
|
||||
;Function:
|
||||
******************************************************************/
|
||||
#ifndef __LIS2DW_H
|
||||
#define __LIS2DW_H
|
||||
/*---------------------------------------------------------------*/
|
||||
#include "ARMCM0.h"
|
||||
|
||||
#define I2C1_SCLK GPIO_3
|
||||
#define I2C1_SDA GPIO_2
|
||||
|
||||
#define LIS2DW_ADDRESS (0x33>>1)
|
||||
|
||||
#define LIS2DW_ID 0x44
|
||||
|
||||
void Lis2dw_Init(void);
|
||||
void Act_State_Judge(void);
|
||||
|
||||
#endif
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,213 @@
|
|||
/*************************************************************************************************
|
||||
**Filename: lsm6d.c
|
||||
**Version :
|
||||
**Author :
|
||||
**Date :
|
||||
**Description:
|
||||
**
|
||||
*************************************************************************************************/
|
||||
#include "lsm6d.h"
|
||||
#include "gpio.h"
|
||||
#include "i2c.h"
|
||||
#include "delay.h"
|
||||
#include "mcu_bsp.h"
|
||||
#include "DebugLog.h"
|
||||
/*********************************************************************
|
||||
* Global Variables (declared in header file with 'extern')
|
||||
*/
|
||||
stmdev_ctx_t dev_ctx;
|
||||
tFilter_Type tFilter;
|
||||
float Angle;
|
||||
PAS_SENSOR_t PAS;
|
||||
/*********************************************************************
|
||||
* Static Variables
|
||||
*/
|
||||
static int16_t data_raw_acc[3];
|
||||
static int16_t data_raw_ang[3];
|
||||
static int16_t data_acc_offset[3];
|
||||
static int16_t data_ang_offset[3];
|
||||
|
||||
/*********************************************************************
|
||||
* Static Functions ('static')
|
||||
*/
|
||||
static void platform0_delay(uint16_t ms);
|
||||
static int32_t platform0_write(uint8_t I2C_addr, uint8_t addr, uint8_t* value, uint16_t len);
|
||||
static int32_t platform0_read(uint8_t I2C_addr,uint8_t regAddress, uint8_t * data, uint16_t len);
|
||||
static void Filter_Complement(float angle_m_cf,float gyro_m_cf);
|
||||
static void Cadence_Stop_Judge(int16_t theta);
|
||||
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description : lsm6dso 初始化
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
void Lsm6dso_Init(void)
|
||||
{
|
||||
uint8_t temp = 0;
|
||||
|
||||
dev_ctx.write_reg = platform0_write;
|
||||
dev_ctx.read_reg = platform0_read;
|
||||
dev_ctx.mdelay = platform0_delay;
|
||||
dev_ctx.master_addr = LSM6DSO_ADDRESS;
|
||||
|
||||
PEN_ENABLE(); //使能lsm6d供电
|
||||
|
||||
platform0_delay(100);
|
||||
|
||||
//获取设置Id
|
||||
lsm6dso_device_id_get(&dev_ctx, &temp);
|
||||
|
||||
if(temp == LSM6DSO_ID)
|
||||
{
|
||||
PAS.exist = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
PAS.exist = 0;
|
||||
}
|
||||
|
||||
//软件复位, 寄存器到默认配值
|
||||
lsm6dso_reset_set(&dev_ctx, PROPERTY_ENABLE);
|
||||
|
||||
do{
|
||||
lsm6dso_reset_get(&dev_ctx, &temp);
|
||||
}while(temp);
|
||||
|
||||
//禁止I3C接口
|
||||
lsm6dso_i3c_disable_set(&dev_ctx, LSM6DSO_I3C_DISABLE);
|
||||
|
||||
//使能BDU
|
||||
lsm6dso_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
|
||||
|
||||
//设置数据输出速率
|
||||
lsm6dso_gy_data_rate_set(&dev_ctx, LSM6DSO_GY_ODR_12Hz5);
|
||||
|
||||
//设置满量程范围
|
||||
lsm6dso_gy_full_scale_set(&dev_ctx, LSM6DSO_1000dps);
|
||||
|
||||
//使能角速度计的滤波功能
|
||||
lsm6dso_gy_lp1_bandwidth_set(&dev_ctx,LSM6DSO_MEDIUM);
|
||||
lsm6dso_gy_filter_lp1_set(&dev_ctx,PROPERTY_ENABLE);
|
||||
}
|
||||
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description : 角度计算 注意:10ms中断计算一次
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
void Lsm6dso_RPM_Cal(void)
|
||||
{
|
||||
uint8_t reg;
|
||||
static float gyro_z;
|
||||
|
||||
lsm6dso_gy_flag_data_ready_get(&dev_ctx, ®);
|
||||
if(reg)
|
||||
{
|
||||
lsm6dso_angular_rate_raw_get(&dev_ctx, data_raw_ang);
|
||||
gyro_z = data_raw_ang[2];
|
||||
}
|
||||
|
||||
gyro_z = (gyro_z - data_ang_offset[2]) + 32768;
|
||||
gyro_z = (32768 - gyro_z)/32.8; //得到的角速度 单位dps(degrees per second)
|
||||
|
||||
Angle = Angle + gyro_z*0.1f*182.04;
|
||||
PAS.angle = Angle;
|
||||
|
||||
Cadence_Stop_Judge(PAS.angle);
|
||||
|
||||
if(PAS.actFlag == 1)
|
||||
{
|
||||
PAS.RPM = (gyro_z*60.0f/360);
|
||||
}
|
||||
else
|
||||
{
|
||||
PAS.RPM = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description : lsm6dso 失能
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
void Lsm6dso_Disable(void)
|
||||
{
|
||||
lsm6dso_reset_set(&dev_ctx, PROPERTY_ENABLE);
|
||||
}
|
||||
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description : 寄存器写
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
static void Cadence_Stop_Judge(int16_t theta)
|
||||
{
|
||||
static uint8_t cntTimes = 0;
|
||||
static int16_t Last_theta = 0,temp;
|
||||
static uint8_t ActFlag = 0;
|
||||
|
||||
cntTimes++;
|
||||
|
||||
if(cntTimes >= 6)
|
||||
{
|
||||
cntTimes = 0;
|
||||
|
||||
temp = theta - Last_theta;
|
||||
|
||||
if(ABS(temp) > 3200)
|
||||
{
|
||||
PAS.actFlag = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
PAS.actFlag = 0;
|
||||
}
|
||||
|
||||
Last_theta = theta;
|
||||
}
|
||||
}
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description : 寄存器写
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
static int32_t platform0_write(uint8_t I2C_addr, uint8_t addr, uint8_t* value, uint16_t len)
|
||||
{
|
||||
ErrorStatus bret = SUCCESS;
|
||||
|
||||
bret = i2c_0_write(I2C_addr, 1, addr, value, len);
|
||||
|
||||
return (int32_t)(!bret);
|
||||
}
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description : 寄存器读
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
static int32_t platform0_read(uint8_t I2C_addr,uint8_t regAddress, uint8_t * data, uint16_t len)
|
||||
{
|
||||
ErrorStatus bret = SUCCESS;
|
||||
|
||||
bret = i2c_0_read(I2C_addr, 1, regAddress, data, len);
|
||||
|
||||
return (int32_t)(!bret);
|
||||
}
|
||||
/*************************************************************************************************
|
||||
* Function Name:
|
||||
* Description : 延时
|
||||
* Arguments :
|
||||
* Return Value :
|
||||
*************************************************************************************************/
|
||||
static void platform0_delay(uint16_t ms)
|
||||
{
|
||||
delay_ms(ms);
|
||||
}
|
||||
/*************************************************************************************************/
|
||||
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
/*************************************************************************************************
|
||||
**Filename: lsm6d.h
|
||||
**Version :
|
||||
**Author :
|
||||
**Date :
|
||||
**Description:
|
||||
**
|
||||
*************************************************************************************************/
|
||||
#ifndef __LSM6D_H_
|
||||
#define __LSM6D_H_
|
||||
|
||||
#include "ARMCM0.h"
|
||||
#include "lsm6dso_reg.h"
|
||||
|
||||
/*********************************************************************
|
||||
* Global Macros ('#define')
|
||||
*/
|
||||
#define LSM6DSO_ADDRESS (LSM6DSO_I2C_ADD_L>>1)
|
||||
#define LSM6DSO_ID 0x6C
|
||||
|
||||
#ifndef ABS
|
||||
#define ABS(x) ((x) < 0 ? -(x) : (x))
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* Struct type definitions('typedef')
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
float C_angle;
|
||||
float bias_cf;
|
||||
float C_angle_dot;
|
||||
|
||||
}tFilter_Type;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t exist;
|
||||
uint8_t batt_level;
|
||||
int16_t RPM;
|
||||
int16_t angle;
|
||||
uint8_t actFlag;
|
||||
uint8_t adjustFlag;
|
||||
uint16_t sleepCnt;
|
||||
uint8_t sleepFlag;
|
||||
uint8_t sleepState;
|
||||
|
||||
} PAS_SENSOR_t;
|
||||
|
||||
/*********************************************************************
|
||||
* Global Variable declared ('extern')
|
||||
*/
|
||||
extern float Angle;
|
||||
extern PAS_SENSOR_t PAS;
|
||||
|
||||
/*********************************************************************
|
||||
* Global Function declared ('extern')
|
||||
*/
|
||||
void Lsm6dso_Init(void);
|
||||
void Lsm6dso_RPM_Cal(void);
|
||||
void Lsm6dso_Correct(void);
|
||||
void Lsm6dso_Disable(void);
|
||||
|
||||
#endif
|
||||
|
||||
/*************************************************************************************************/
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,27 @@
|
|||
/*****************************************************************
|
||||
;Project: Light
|
||||
;MCU:
|
||||
;Date:
|
||||
;File:
|
||||
;Function:
|
||||
******************************************************************/
|
||||
#include "tick.h"
|
||||
#include "timer.h"
|
||||
#include "lsm6d.h"
|
||||
/*---------------------------------------------------------------*/
|
||||
//函数声明
|
||||
static void Timer_2_callback(void);
|
||||
/*---------------------------------------------------------------*/
|
||||
//tick定时初始化
|
||||
void Tick_Init(void)
|
||||
{
|
||||
timer_2_enable(328,Timer_2_callback); //10ms定时任务
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//在定时器的中断服务函数中调用,调用周期: 1ms
|
||||
static void Timer_2_callback(void)
|
||||
{
|
||||
Angle_Cal();
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/*****************************************************************
|
||||
;Project: Light
|
||||
;MCU:
|
||||
;Date:
|
||||
;File:
|
||||
;Function:
|
||||
******************************************************************/
|
||||
#ifndef __TICK_H
|
||||
#define __TICK_H
|
||||
/*---------------------------------------------------------------*/
|
||||
#include "ARMCM0.h"
|
||||
/*---------------------------------------------------------------*/
|
||||
//函数声明
|
||||
void Tick_Init(void);
|
||||
/*---------------------------------------------------------------*/
|
||||
//外部变量调用声明
|
||||
extern volatile unsigned short timer0_tick;
|
||||
/*---------------------------------------------------------------*/
|
||||
//宏定义
|
||||
#define TimerSet(t,val) t = timer0_tick; //记录当前定时器的计数值,设置定时时间 单位ms
|
||||
#define TimerCheck(t,val) ((unsigned short)(timer0_tick - (unsigned short)t) > (unsigned short)val) //检测是否到达定时时间
|
||||
|
||||
|
||||
#define SLEEPTIME 60000 //单位ms
|
||||
|
||||
#endif
|
||||
/*---------------------------------------------------------------*/
|
|
@ -0,0 +1,140 @@
|
|||
/*****************************************************************
|
||||
;Project: Light
|
||||
;MCU:
|
||||
;Date:
|
||||
;File:
|
||||
;Function:
|
||||
******************************************************************/
|
||||
#include "uartdate.h"
|
||||
#include "uart.h"
|
||||
/*---------------------------------------------------------------*/
|
||||
//变量定义
|
||||
//Uart_Rx_Buf_t uart_rx_buf;
|
||||
//static unsigned char Rx_Pack[DATA_MAXSIZE]; //接收数据帧打包
|
||||
/*---------------------------------------------------------------*/
|
||||
//函数声明
|
||||
//static void Command_Execute(unsigned char *Buff);
|
||||
///*---------------------------------------------------------------*/
|
||||
////外部变量调用声明
|
||||
//extern unsigned short cycSpeed; //骑行速度
|
||||
//extern unsigned char cycReSpeedFlag; //骑行速度刷新标志
|
||||
///*---------------------------------------------------------------*/
|
||||
////串口初始化
|
||||
//void Uart_Init(void)
|
||||
//{
|
||||
// uart_1_init();
|
||||
//}
|
||||
///*---------------------------------------------------------------*/
|
||||
////串口接收回调函数
|
||||
//void uartRx_callback(void)
|
||||
//{
|
||||
// uint8_t cnt,i,temp[4];
|
||||
// uart_1_read(&cnt,temp);
|
||||
//
|
||||
// for(i=0;i<cnt;i++)
|
||||
// {
|
||||
// uart_rx_buf.data[uart_rx_buf.header]=temp[i];
|
||||
// uart_rx_buf.header++;
|
||||
// if(uart_rx_buf.header >= MAX_RX_LENGTH)
|
||||
// {
|
||||
// uart_rx_buf.header = 0;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
/*---------------------------------------------------------------*/
|
||||
//发送数据
|
||||
//static void uart_cmd(uint8_t *p_cmd, uint8_t sz)
|
||||
//{
|
||||
// unsigned char i;
|
||||
//
|
||||
// for(i=0; i<sz; i++)
|
||||
// {
|
||||
// uart_1_write(p_cmd[i]);
|
||||
// }
|
||||
//}
|
||||
///*---------------------------------------------------------------*/
|
||||
////发送获取速度命令
|
||||
//void GetSpeedCmd(void)
|
||||
//{
|
||||
// static unsigned char GetSpeedBuff[6] = {0x7e,0x04,0x0c, 0xbf, 0xcf, 0x99};
|
||||
// uart_cmd(GetSpeedBuff,6);
|
||||
//}
|
||||
/*---------------------------------------------------------------*/
|
||||
//串口的接收数据处理
|
||||
//void RxData_Deal(void)
|
||||
//{
|
||||
// static unsigned char Rx_Pack_Index=0;
|
||||
// static unsigned char state=0;
|
||||
// static unsigned char Rx_Pack_Len=0; //需要打包的帧长
|
||||
|
||||
// if(uart_rx_buf.tail != uart_rx_buf.header)
|
||||
// {
|
||||
// switch(state)
|
||||
// {
|
||||
// case 0: //寻找帧头
|
||||
// if(uart_rx_buf.data[uart_rx_buf.tail]==0x7E)
|
||||
// {
|
||||
// Rx_Pack_Index = 0; //每次从帧头开始存
|
||||
// Rx_Pack[Rx_Pack_Index++]= uart_rx_buf.data[uart_rx_buf.tail]; //接收帧头
|
||||
// state = 1;
|
||||
// }
|
||||
// break;
|
||||
//
|
||||
// case 1: //读取帧长度
|
||||
// Rx_Pack_Len = uart_rx_buf.data[uart_rx_buf.tail];
|
||||
// if(Rx_Pack_Len > DATA_MAXSIZE) //判断数据长度是否异常
|
||||
// {
|
||||
// state = 0; //重新寻找帧头
|
||||
// break;
|
||||
// }
|
||||
// Rx_Pack[Rx_Pack_Index++] = uart_rx_buf.data[uart_rx_buf.tail]; //接收帧的剩余长度
|
||||
// state = 3;
|
||||
// break;
|
||||
//
|
||||
// case 3: //判断一帧结束
|
||||
// Rx_Pack[Rx_Pack_Index++] = uart_rx_buf.data[uart_rx_buf.tail];
|
||||
// //--判断是否接收到想要的帧长
|
||||
// if( Rx_Pack_Index >= (Rx_Pack_Len+2) ) //2:1个起始字符和1个数据长度字符
|
||||
// {
|
||||
// state = 0; //失能帧接收标志
|
||||
// Command_Execute(Rx_Pack); //指令执行
|
||||
// }
|
||||
// break;
|
||||
//
|
||||
// default: state = 0;break;
|
||||
//
|
||||
// }
|
||||
//
|
||||
// uart_rx_buf.tail++;
|
||||
// if(uart_rx_buf.tail >= MAX_RX_LENGTH)
|
||||
// {
|
||||
// uart_rx_buf.tail = 0;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//}
|
||||
/*---------------------------------------------------------------*/
|
||||
//解析骑行速度
|
||||
//static void Command_Execute(unsigned char *Buff) // 指令处理
|
||||
//{
|
||||
// static unsigned short LastcycSpeed = 0;
|
||||
//
|
||||
// if(Buff[2] == 0x0C) //0x0C:判断是不是电机的应答帧
|
||||
// {
|
||||
// if(Buff[3]==0x01) //判断是否查询成功
|
||||
// if(Buff[4]==0xBF) //判断是否查询成功
|
||||
// if(Buff[5]==0x02) //判断是否为2个字节的速度
|
||||
// {
|
||||
// //转换成速度
|
||||
// LastcycSpeed = cycSpeed;
|
||||
// cycSpeed = ((unsigned short )Buff[6]<<8) + (unsigned short)Buff[7]; //把这两个字节拼接成速度,eg:(0x12,0x34)->(0x1234)->(4660)
|
||||
// cycSpeed = cycSpeed/10; //过滤小数部分,(4660)->(466.0)
|
||||
// if((LastcycSpeed == 0)&&(cycSpeed > 10))
|
||||
// {
|
||||
// cycSpeed = 1;
|
||||
// }
|
||||
// cycReSpeedFlag = 1;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
/*---------------------------------------------------------------*/
|
|
@ -0,0 +1,32 @@
|
|||
/*****************************************************************
|
||||
;Project: Light
|
||||
;MCU:
|
||||
;Date:
|
||||
;File:
|
||||
;Function:
|
||||
******************************************************************/
|
||||
#ifndef __UARTDATE_H
|
||||
#define __UARTDATE_H
|
||||
/*---------------------------------------------------------------*/
|
||||
#include "ARMCM0.h"
|
||||
/*---------------------------------------------------------------*/
|
||||
//函数声明
|
||||
//void Uart_Init(void);
|
||||
//void RxData_Deal(void);
|
||||
//void GetSpeedCmd(void);
|
||||
///*---------------------------------------------------------------*/
|
||||
////接收数组长度宏定义
|
||||
//#define MAX_RX_LENGTH 100
|
||||
///*---------------------------------------------------------------*/
|
||||
////接收数据缓存结构体
|
||||
//typedef struct{
|
||||
// uint8_t data[MAX_RX_LENGTH];
|
||||
// uint32_t header,tail;
|
||||
//} Uart_Rx_Buf_t;
|
||||
///*---------------------------------------------------------------*/
|
||||
////最大帧长度
|
||||
//#define DATA_MAXSIZE 20
|
||||
|
||||
#endif
|
||||
/*---------------------------------------------------------------*/
|
||||
|
|
@ -0,0 +1,262 @@
|
|||
/*****************************************************************
|
||||
;Project: Light
|
||||
;MCU:
|
||||
;Date:
|
||||
;File:
|
||||
;Function:
|
||||
******************************************************************/
|
||||
#include "ws2812.h"
|
||||
#include "delay.h"
|
||||
#include "gpio.h"
|
||||
/*---------------------------------------------------------------*/
|
||||
//变量定义
|
||||
unsigned char gLedBuffer[LED_NUM][GRB] = {0}; //亮灯数据缓存
|
||||
/*---------------------------------------------------------------*/
|
||||
//函数声明
|
||||
static void WS_Port_Init(void);
|
||||
static void WS_DIN_Clear(void);
|
||||
static void WS_Write_0(void);
|
||||
static void WS_Write_1(void);
|
||||
/*---------------------------------------------------------------*/
|
||||
//WS2812初始化
|
||||
void WS_Init(void)
|
||||
{
|
||||
WS_Port_Init();
|
||||
WS_CloseAll();
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//WS供电使能
|
||||
void WS_Power(FunctionalState NewState)
|
||||
{
|
||||
GPIO_Set_Output(U32BIT(WS_EN_PIN));
|
||||
|
||||
if(NewState != DISABLE)
|
||||
GPIO_Pin_Set(U32BIT(WS_EN_PIN));
|
||||
else
|
||||
GPIO_Pin_Clear(U32BIT(WS_EN_PIN));
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//关闭所有的led
|
||||
void WS_CloseAll(void)
|
||||
{
|
||||
unsigned char led_i, color_i;
|
||||
|
||||
for(led_i=0;led_i<LED_NUM;led_i++)
|
||||
{
|
||||
for(color_i=0;color_i<GRB;color_i++)
|
||||
{
|
||||
gLedBuffer[led_i][color_i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
WS_Show(); //点灯
|
||||
|
||||
//WS_Power(DISABLE); //关闭WS的供电
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//--清除所有ledbuff 缓存
|
||||
void LedBuff_CleanAll(void)
|
||||
{
|
||||
unsigned char led_i,color_i; //用于for循坏
|
||||
|
||||
for(led_i=0;led_i<LED_NUM;led_i++)
|
||||
for(color_i=0;color_i<GRB;color_i++)
|
||||
gLedBuffer[led_i][color_i] = 0;
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//点亮所有灯
|
||||
void WS_LightAll(void)
|
||||
{
|
||||
WS_WriteNum(LED_NUM,0xff0000);
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//指定写某个灯写入color数据,不show灯
|
||||
void LedBuff_WriteOrder(unsigned char ledOrder, unsigned int color)
|
||||
{
|
||||
unsigned char color_i;
|
||||
|
||||
// ledOrder = ledOrder-1;
|
||||
//先把要改变的颜色填入到led的buffer中
|
||||
for(color_i=0;color_i<GRB;color_i++)
|
||||
{
|
||||
if(color&0x800000)
|
||||
{
|
||||
gLedBuffer[ledOrder][color_i] = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
gLedBuffer[ledOrder][color_i] = 0;
|
||||
}
|
||||
color <<= 1;
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//指定写某个灯的color并显示颜色
|
||||
void WS_WriteOrder(unsigned char ledOrder, unsigned int color)
|
||||
{
|
||||
unsigned char color_i;
|
||||
|
||||
// ledOrder = ledOrder-1;
|
||||
//先把要改变的颜色填入到led的buffer中
|
||||
for(color_i=0;color_i<GRB;color_i++)
|
||||
{
|
||||
if(color&0x800000)
|
||||
{
|
||||
gLedBuffer[ledOrder][color_i] = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
gLedBuffer[ledOrder][color_i] = 0;
|
||||
}
|
||||
color <<= 1;
|
||||
}
|
||||
WS_Show(); //点灯
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//-给前num个led写入color,但不show
|
||||
void LedBuff_WriteNum(unsigned char numLed,unsigned int color)
|
||||
{
|
||||
unsigned char led_i,color_i; //用于for循坏
|
||||
unsigned int color_temp;
|
||||
|
||||
for(led_i=0;led_i<numLed;led_i++)
|
||||
{
|
||||
color_temp = color;
|
||||
for(color_i=0;color_i<GRB;color_i++)
|
||||
{
|
||||
if(color_temp&0x800000)
|
||||
{
|
||||
gLedBuffer[led_i][color_i] = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
gLedBuffer[led_i][color_i] = 0;
|
||||
}
|
||||
color_temp <<= 1;
|
||||
}
|
||||
}
|
||||
color_temp <<= 1;
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//--给前num个led写入color 并显示
|
||||
void WS_WriteNum(unsigned char numLed, unsigned int color)
|
||||
{
|
||||
unsigned char led_i,color_i;
|
||||
unsigned int color_temp;
|
||||
|
||||
//先把要改变的颜色填入到led的buffer中
|
||||
for(led_i=0;led_i<numLed;led_i++)
|
||||
{
|
||||
color_temp = color;
|
||||
for(color_i=0;color_i<GRB;color_i++)
|
||||
{
|
||||
if(color_temp&0x800000)
|
||||
{
|
||||
gLedBuffer[led_i][color_i] = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
gLedBuffer[led_i][color_i] = 0;
|
||||
}
|
||||
color_temp <<= 1;
|
||||
}
|
||||
}
|
||||
WS_Show(); //点灯
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//WS驱动IO口初始化
|
||||
static void WS_Port_Init(void)
|
||||
{
|
||||
PIN_Set_GPIO(U32BIT(WS_DIN_PIN) | U32BIT(WS_EN_PIN),PIN_SEL_GPIO); //引脚设置成普通IO口
|
||||
GPIO_Set_Output(U32BIT(WS_DIN_PIN) | U32BIT(WS_EN_PIN)); //设置引脚为输出模式
|
||||
GPIO_Pin_Clear(U32BIT(WS_DIN_PIN)|U32BIT(WS_EN_PIN)); //WS数据IO和使能IO都清零
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//灯带显示函数
|
||||
void WS_Show(void)
|
||||
{
|
||||
unsigned led_i, color_i;
|
||||
|
||||
WS_Power(ENABLE);
|
||||
|
||||
__disable_irq(); //关闭全局中断(防止打断)
|
||||
//依次写10个灯
|
||||
for(led_i=0; led_i<LED_NUM; led_i++)
|
||||
{
|
||||
//依次写入每个灯的24bit时序
|
||||
for(color_i=0;color_i<GRB;color_i++)
|
||||
{
|
||||
if(gLedBuffer[led_i][color_i] != 0) //写高
|
||||
{
|
||||
WS_Write_1();
|
||||
}
|
||||
else //写低
|
||||
{
|
||||
WS_Write_0();
|
||||
}
|
||||
}
|
||||
}
|
||||
WS_DIN_Clear();
|
||||
delay_us(300); //实践证明大于20us就行
|
||||
__enable_irq(); //打开全局中断
|
||||
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------*/
|
||||
//灯带显示函数但是最后不打开总中断
|
||||
void WS_Show_Disable_irq(void)
|
||||
{
|
||||
unsigned led_i, color_i;
|
||||
|
||||
WS_Power(ENABLE);
|
||||
|
||||
__disable_irq(); //关闭全局中断(防止打断)
|
||||
//依次写10个灯
|
||||
for(led_i=0; led_i<LED_NUM; led_i++)
|
||||
{
|
||||
//依次写入每个灯的24bit时序
|
||||
for(color_i=0;color_i<GRB;color_i++)
|
||||
{
|
||||
if(gLedBuffer[led_i][color_i] != 0) //写高
|
||||
{
|
||||
WS_Write_1();
|
||||
}
|
||||
else //写低
|
||||
{
|
||||
WS_Write_0();
|
||||
}
|
||||
}
|
||||
}
|
||||
WS_DIN_Clear();
|
||||
delay_us(300); //实践证明大于20us就行
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//WS的数据清零
|
||||
static void WS_DIN_Clear(void)
|
||||
{
|
||||
GPIO_Set_Output(U32BIT(WS_DIN_PIN));
|
||||
GPIO_Pin_Clear(U32BIT(WS_DIN_PIN));
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//写bit_0
|
||||
static void WS_Write_0(void)
|
||||
{
|
||||
WS_DIN_HIGH;
|
||||
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
|
||||
WS_DIN_LOW;
|
||||
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
|
||||
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
|
||||
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
||||
//写bit_1
|
||||
static void WS_Write_1(void)
|
||||
{
|
||||
WS_DIN_HIGH;
|
||||
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
|
||||
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
|
||||
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
|
||||
WS_DIN_LOW;
|
||||
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
|
||||
}
|
||||
/*---------------------------------------------------------------*/
|
|
@ -0,0 +1,40 @@
|
|||
/*****************************************************************
|
||||
;Project: Light
|
||||
;MCU:
|
||||
;Date:
|
||||
;File:
|
||||
;Function:
|
||||
******************************************************************/
|
||||
#ifndef __WS2812_H
|
||||
#define __WS2812_H
|
||||
/*---------------------------------------------------------------*/
|
||||
#include "ARMCM0.h"
|
||||
/*---------------------------------------------------------------*/
|
||||
//函数声明
|
||||
void WS_Init(void);
|
||||
void WS_Power(FunctionalState NewState);
|
||||
void LedBuff_WriteNum(unsigned char numLed,unsigned int color);
|
||||
void WS_WriteNum(unsigned char numLed, unsigned int color);
|
||||
void LedBuff_WriteOrder(unsigned char ledOrder, unsigned int color);
|
||||
void WS_WriteOrder(unsigned char ledOrder, unsigned int color);
|
||||
void LedBuff_CleanAll(void);
|
||||
void WS_CloseAll(void);
|
||||
void WS_LightAll(void);
|
||||
void WS_Show(void);
|
||||
void WS_Show_Disable_irq(void);
|
||||
/*---------------------------------------------------------------*/
|
||||
//宏定义
|
||||
#define LED_NUM 10
|
||||
#define GRB 24 //24bit
|
||||
|
||||
#define WS_DIN_PIN GPIO_15 //定义SW的数据接口
|
||||
#define WS_EN_PIN GPIO_26 //定义SW的使能接口
|
||||
|
||||
#define WS_HIGH(n) (*(uint32_t *)0x50001014UL |= n)
|
||||
#define WS_LOW(n) (*(uint32_t *)0x50001014UL &= ~n)
|
||||
|
||||
#define WS_DIN_HIGH WS_HIGH(BIT15) //设置SW的数据口为1
|
||||
#define WS_DIN_LOW WS_LOW(BIT15) //设置SW的数据口为0
|
||||
|
||||
#endif
|
||||
/*---------------------------------------------------------------*/
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,13 @@
|
|||
#ifndef _IR_Tx__H__
|
||||
#define _IR_Tx__H__
|
||||
|
||||
#include "ARMCM0.h"
|
||||
#include "syd_type.h"
|
||||
|
||||
#define IR_CARR_FREQ 37910UL
|
||||
|
||||
extern void IR_Tx_Init(void);
|
||||
extern void IR_Tx_SendData(uint16_t usrCode, uint8_t data);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef _ATR_DECODER_H_
|
||||
#define _ATR_DECODER_H_
|
||||
|
||||
#include "armcm0.h"
|
||||
#include "sc_reader.h"
|
||||
|
||||
#define ATR_MAX_T_LENGTH (31)
|
||||
#define DEFAULT_FD (0x11)
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
uint8_t TS;
|
||||
uint8_t T0;
|
||||
uint8_t T[ATR_MAX_T_LENGTH];
|
||||
//
|
||||
uint8_t Check;
|
||||
uint8_t ILength; //Interface characters length
|
||||
uint8_t HLength; //Historical characters length
|
||||
uint8_t TotalLength;
|
||||
uint8_t Th_Start;
|
||||
} SC_ATR;
|
||||
#pragma pack(pop)
|
||||
|
||||
void atr_reset(void);
|
||||
bool atr_decode(SC_ATR *atr);
|
||||
uint8_t atr_decoder_get_extra_guard_time(void);
|
||||
uint8_t atr_decoder_get_waiting_integer(void);
|
||||
bool atr_decoder_get_clock_stop(bool *high);
|
||||
bool atr_decoder_allow_pps(void);
|
||||
bool atr_decoder_allow_switch_mode(void);
|
||||
uint8_t atr_decoder_get_protocol(void);
|
||||
uint8_t atr_decoder_get_FD(void);
|
||||
|
||||
#endif //_ATR_DECODER_H_
|
|
@ -0,0 +1,28 @@
|
|||
#ifndef _DEBUG_H_
|
||||
#define _DEBUG_H_
|
||||
|
||||
#include "ARMCM0.h"
|
||||
#include "config.h"
|
||||
#include "Debug.h"
|
||||
|
||||
//#define _DEBUG_
|
||||
|
||||
#ifdef _DEBUG_
|
||||
#define DBGPRINTF_TIMER(s) dbg_printf s
|
||||
#define DBGPRINTF(...) dbg_printf( __VA_ARGS__)
|
||||
#define DBGHEXDUMP(title,buf,sz) dbg_hexdump(title,buf,sz)
|
||||
|
||||
extern void dbg_init(void);
|
||||
extern void dbg_printf(char *format,...) ;
|
||||
extern void dbg_hexdump(char *title, uint8_t *buf, uint16_t sz);
|
||||
#elif (defined _SYD_RTT_DEBUG_)
|
||||
|
||||
#else
|
||||
#define dbg_printf(...)
|
||||
#define dbg_hexdump(title,buf,sz)
|
||||
|
||||
#define DBGPRINTF(...)
|
||||
#define DBGHEXDUMP(title,buf,sz)
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef _DELAY__H__
|
||||
#define _DELAY__H__
|
||||
|
||||
#include "ARMCM0.h"
|
||||
|
||||
void delay_ms(uint16_t n);
|
||||
void delay_us(uint16_t n);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef _GPADC_H_
|
||||
#define _GPADC_H_
|
||||
|
||||
#include "ARMCM0.h"
|
||||
|
||||
#define GPADC_CH_MAX 11
|
||||
|
||||
typedef enum{
|
||||
ONESHOT_MODE,
|
||||
AVE_MODE,
|
||||
}GPADC_MODE;
|
||||
|
||||
typedef enum _ADCGP_CHX_{
|
||||
ADCGP_CH0 = 0x00,
|
||||
ADCGP_CH1,
|
||||
ADCGP_CH2,
|
||||
ADCGP_CH3,
|
||||
ADCGP_CH4,
|
||||
ADCGP_CH5,
|
||||
ADCGP_CH6,
|
||||
ADCGP_CH7,
|
||||
ADCGP_CH8,
|
||||
ADCGP_CH9,
|
||||
ADCGP_CH10,
|
||||
}ADCGP_CHX;
|
||||
|
||||
extern void GPADC_Init(ADCGP_CHX ch,GPADC_MODE adc_mode);
|
||||
extern void GPADC_start(void (*p_callback)(uint16_t adc));
|
||||
extern void GPADC_stop(void);
|
||||
extern void GPADC_channel_sel(uint8_t ch);
|
||||
extern uint16_t GPADC_get_value(void);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
#ifndef _GPIO__H__
|
||||
#define _GPIO__H__
|
||||
|
||||
#include "ARMCM0.h"
|
||||
#include "lib.h"
|
||||
|
||||
extern PIN_CONFIG_TYPE * PIN_CONFIG;
|
||||
extern GPIO_CTRL_TYPE * GPIO_CTRL; //GPIO控制寄存器
|
||||
extern GPO_CTRL_TYPE * GPO_CTRL; //GPIO输出控制寄存器
|
||||
extern GPI_CTRL_TYPE * GPI_CTRL; //GPIO输入控制寄存器
|
||||
extern GPIO_IRQ_CTRL_TYPE * GPIO_IRQ_CTRL;
|
||||
|
||||
void PIN_Set_GPIO(uint32_t io,uint8_t fun);
|
||||
void PIN_Pullup_Enable(enum _QFN_TYPE_ type, uint32_t io);
|
||||
void PIN_Pullup_Disable(enum _QFN_TYPE_ type, uint32_t io);
|
||||
void GPIO_Set_Output(uint32_t io);
|
||||
void GPIO_Set_Input(uint32_t io, uint32_t invert_bits);
|
||||
void GPIO_Input_Enable(uint32_t io);
|
||||
void GPIO_Input_Disable(uint32_t io);
|
||||
void GPIO_Pin_Set(uint32_t io);
|
||||
void GPIO_Pin_Clear(uint32_t io);
|
||||
void GPIO_Pin_Turn(uint32_t io);
|
||||
uint32_t GPIO_Pin_Read(uint32_t io);
|
||||
void io_irq_enable(uint32_t io, void * p_callback);
|
||||
void io_irq_disable(uint32_t io);
|
||||
void io_irq_disable_all(void);
|
||||
void GPIO_Set_Input_DIR(uint32_t io);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef _I2C_H_
|
||||
#define _I2C_H_
|
||||
|
||||
#include "ARMCM0.h"
|
||||
|
||||
#define I2C0 0x00
|
||||
#define I2C1 0x01
|
||||
|
||||
#define I2C_1_BYTE_ADDRESS 0x01
|
||||
#define I2C_2_BYTE_ADDRESS 0x00
|
||||
|
||||
extern void i2c_Init(uint8_t i2c,uint32_t clk_io,uint32_t sda_io);
|
||||
extern ErrorStatus i2c_0_write(uint8_t id, uint8_t addr_len, uint16_t addr, uint8_t * buf, uint16_t sz);
|
||||
extern ErrorStatus i2c_0_read(uint8_t id, uint8_t addr_len, uint16_t addr, uint8_t * buf, uint16_t sz);
|
||||
extern ErrorStatus i2c_1_write(uint8_t id, uint8_t addr_len, uint16_t addr, uint8_t * buf, uint16_t sz);
|
||||
extern ErrorStatus i2c_1_read(uint8_t id, uint8_t addr_len, uint16_t addr, uint8_t * buf, uint16_t sz);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef _KEY_H_
|
||||
#define _KEY_H_
|
||||
|
||||
#include "ARMCM0.h"
|
||||
|
||||
//#define KEY1 GPIO_12
|
||||
//#define KEY2 GPIO_13
|
||||
//#define KEY3 GPIO_14
|
||||
//#define KEY4 GPIO_24
|
||||
|
||||
//#define UART_DEBUG_RX GPIO_15
|
||||
//#define UART_DEBUG_TX GPIO_16
|
||||
|
||||
#endif
|
|
@ -0,0 +1,133 @@
|
|||
#ifndef _OTA_H_
|
||||
#define _OTA_H_
|
||||
|
||||
#include "ARMCM0.h"
|
||||
#include "config.h"
|
||||
|
||||
#ifdef _OTA_
|
||||
|
||||
#pragma pack(1)
|
||||
|
||||
#define CMD_4KSETTING_ERASE 0x11
|
||||
#define CMD_4KSETTING_WRITE 0x12
|
||||
#define CMD_4KSETTING_UPGRADE 0x13
|
||||
#define CMD_FW_WRITE_START 0x14
|
||||
#define CMD_FW_UPGRADEV20 0x15
|
||||
#define CMD_FW_ERASE 0x16
|
||||
#define CMD_FW_WRITE 0x17
|
||||
#define CMD_FW_UPGRADE 0x18
|
||||
#define CMD_FLASHDATA_ERASE 0x20
|
||||
#define CMD_FLASHDATA_WRITE 0x21
|
||||
#define CMD_FLASHDATA_UPGRADE 0x22
|
||||
#define CMD_FLASHDATA_START 0x23
|
||||
#define CMD_FLASHDATA_UPGRADEV30 0x24
|
||||
|
||||
#define ERR_COMMAND_SUCCESS 0x00
|
||||
#define ERR_COMMAND_FAILED 0x01
|
||||
#define EVT_COMMAND_COMPLETE (0x0E)
|
||||
#define CMD_COMPLETE_HDR_SZ 1
|
||||
#define MAX_FW_WRITE_SZ 15
|
||||
|
||||
struct ret_fw_erase_cmd {
|
||||
uint8_t status;
|
||||
};
|
||||
|
||||
struct cmd_fw_write {
|
||||
uint16_t offset;
|
||||
uint8_t sz;
|
||||
uint8_t data[MAX_FW_WRITE_SZ];
|
||||
};
|
||||
|
||||
|
||||
struct cmd_fw_write_start {
|
||||
uint32_t offset;
|
||||
uint16_t sz;
|
||||
uint16_t checksum;
|
||||
};
|
||||
|
||||
struct ret_fw_write_cmd {
|
||||
uint8_t status;
|
||||
};
|
||||
|
||||
struct ret_fw_write_start_cmd {
|
||||
uint8_t status;
|
||||
uint16_t sz;
|
||||
uint16_t checksum;
|
||||
};
|
||||
|
||||
struct cmd_fw_upgrade {
|
||||
uint16_t sz;
|
||||
uint16_t checksum;
|
||||
};
|
||||
|
||||
struct cmd_4ksetting_upgrade {
|
||||
uint8_t Xor;
|
||||
uint32_t checksum;
|
||||
};
|
||||
|
||||
struct cmd_fw_upgrade_V20 {
|
||||
uint32_t sz;
|
||||
uint16_t checksum;
|
||||
};
|
||||
|
||||
struct ret_fw_upgrade_cmd {
|
||||
uint8_t status;
|
||||
};
|
||||
|
||||
union cmd_parm {
|
||||
struct cmd_fw_write CmdFwWrite;
|
||||
struct cmd_fw_upgrade CmdFwUpgrade;
|
||||
struct cmd_fw_write_start CmdFwWriteStart;
|
||||
struct cmd_fw_upgrade_V20 CmdFwUpgradeV20;
|
||||
struct cmd_4ksetting_upgrade Cmd4ksettingUpgrade;
|
||||
};
|
||||
|
||||
struct hci_cmd {
|
||||
uint8_t opcode;
|
||||
uint8_t len;
|
||||
union cmd_parm cmdparm;
|
||||
};
|
||||
|
||||
union ret_parm {
|
||||
struct ret_fw_erase_cmd RetFwEraseCmd;
|
||||
struct ret_fw_write_cmd RetFwWriteCmd;
|
||||
struct ret_fw_upgrade_cmd RetFwUpgradeCmd;
|
||||
struct ret_fw_write_start_cmd RetFwWriteStartCmd;
|
||||
};
|
||||
|
||||
struct evt_command_complete {
|
||||
uint8_t opcode;
|
||||
union ret_parm RetParm;
|
||||
};
|
||||
|
||||
union evt_parm {
|
||||
struct evt_command_complete EvtCommandComplete;
|
||||
};
|
||||
|
||||
struct hci_evt {
|
||||
uint8_t evtcode;
|
||||
uint8_t evtlen;
|
||||
union evt_parm evtparm;
|
||||
};
|
||||
|
||||
extern uint8_t ota_state,ota_writecnt;
|
||||
|
||||
#pragma pack()
|
||||
|
||||
#pragma pack(4)
|
||||
struct ota_write_info{
|
||||
uint8_t buf[32];
|
||||
uint32_t cnt;
|
||||
uint8_t idx;
|
||||
};
|
||||
#pragma pack()
|
||||
|
||||
extern void ota_cmd(uint8_t *p_cmd, uint8_t sz);
|
||||
extern void ota_rsp(uint8_t *p_rsp, uint8_t *p_sz);
|
||||
extern void CmdFwErase(void);
|
||||
extern void ota_variable_clear(uint8_t all);
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,109 @@
|
|||
#ifndef _PWM_H_
|
||||
#define _PWM_H_
|
||||
|
||||
#include "ARMCM0.h"
|
||||
|
||||
#define HPWM_CLK 64000000UL //设计值
|
||||
|
||||
//PWM模块枚举
|
||||
typedef enum {
|
||||
PWM_0,
|
||||
PWM_1,
|
||||
PWM_2,
|
||||
}PWM_x;
|
||||
|
||||
typedef enum {
|
||||
HPWM_CH0 = 0x01,
|
||||
HPWM_CH1 = 0x02,
|
||||
HPWM_CH2 = 0x04,
|
||||
HPWM_CH3 = 0x08,
|
||||
HPWM_CHALL = 0x0f,
|
||||
}HPWM_CHx;
|
||||
|
||||
//PWM模块工作模式枚举
|
||||
typedef enum {
|
||||
always_on_mode,
|
||||
pwm_mode,
|
||||
flash_mode,
|
||||
breath_mode,
|
||||
}PWM_mode;
|
||||
|
||||
typedef enum {
|
||||
UP_MODE,
|
||||
UP_DOWN_MODE,
|
||||
}HPWM_mode;
|
||||
|
||||
typedef enum {
|
||||
HPWM_INT_NONE = 0,
|
||||
HPWM_INT_PERIOD = 1,
|
||||
HPWM_INT_TASK_STOP = 2,
|
||||
HPWM_INT_ALL = 3,
|
||||
} HPWM_INT_TYPE;
|
||||
|
||||
typedef enum {
|
||||
HPWM_PERSCALER_1 = 0,
|
||||
HPWM_PERSCALER_2 = 1,
|
||||
HPWM_PERSCALER_4 = 2,
|
||||
HPWM_PERSCALER_8 = 3,
|
||||
HPWM_PERSCALER_16 = 4,
|
||||
HPWM_PERSCALER_32 = 5,
|
||||
HPWM_PERSCALER_64 = 6,
|
||||
HPWM_PERSCALER_128 = 7,
|
||||
HPWM_PERSCALER_256 = 8,
|
||||
HPWM_PERSCALER_512 = 9,
|
||||
HPWM_PERSCALER_1024 = 10,
|
||||
HPWM_PERSCALER_2048 = 11,
|
||||
HPWM_PERSCALER_4096 = 12,
|
||||
HPWM_PERSCALER_8192 = 13,
|
||||
HPWM_PERSCALER_16384 = 14,
|
||||
HPWM_PERSCALER_32768 = 15
|
||||
} HPWM_PERSCALER_TYPE;
|
||||
|
||||
typedef enum {
|
||||
LOW_FIRST = 0,
|
||||
HIGH_FIRST
|
||||
} HPWM_POLARITY_TYPE;
|
||||
|
||||
//PWM参数
|
||||
typedef struct{
|
||||
uint8_t module; //模块 使用那个pwm模块
|
||||
|
||||
//----------------------------flash mode------------------------------
|
||||
uint8_t T1; //T1 value(high level time) for flash mode. Unit:1/32 S
|
||||
uint8_t T2; //T2 value(T1 + low level time) for flash mode. Unit:1/32 S
|
||||
uint16_t T3; //T3 value(low level time) for flash mode. Unit:1/32 S
|
||||
uint8_t N1; //repeat "T1-T2" N1 times. If N1 set 0x80 then always repeat "T1-T2" and No T3
|
||||
uint8_t N2; //repeat "flash" N2 times. If N2 set 0x80 then always repeat "flash".
|
||||
//----------------------------breath mode------------------------------
|
||||
uint8_t T4; //breath mode , flash period. Unit:1/2ms
|
||||
uint8_t BR_TH_MAX; //breath mode maxinum briteness threshlod of the LED.Unit:1/2ms
|
||||
uint8_t BR_TH_MIN; //breath mode minnum briteness threshlod of the LED.Unit:1/2ms
|
||||
uint8_t BR_SP; //BR_SP : breath mode speed.indicate the breath speed. Unit:1/32 ms
|
||||
//----------------------------PWM mode------------------------------
|
||||
uint8_t PWM_N; //N value for PWM mode. Unit:1/32 ms
|
||||
uint8_t PWM_M; //M value for PWM mode. Unit:1/32 ms
|
||||
|
||||
uint8_t MODE; // 1 : PWM mode 2 : flash mode. 3 : breath mode.
|
||||
}PWM_PARAMETER;
|
||||
|
||||
//HPWM参数
|
||||
typedef struct{
|
||||
HPWM_CHx channel;
|
||||
HPWM_mode mode;
|
||||
HPWM_PERSCALER_TYPE prescaler;
|
||||
HPWM_POLARITY_TYPE polarity;
|
||||
uint16_t period;
|
||||
uint16_t duty;
|
||||
}HPWM_PARAMETER;
|
||||
|
||||
extern void pwm_enable(PWM_x pwm_x,PWM_PARAMETER pwm_parameter);
|
||||
extern void pwm_disable(PWM_x pwm_x);
|
||||
extern void Hpwm_Init(HPWM_PARAMETER *pwm_parameter);
|
||||
extern void Hpwm_Set_duty(HPWM_CHx channels, uint16_t compare);
|
||||
extern void Hpwm_Set_period(uint16_t period);
|
||||
extern void Hpwm_Set_mode(HPWM_mode mode);
|
||||
extern void Hpwm_Start(void);
|
||||
extern void Hpwm_Stop(void);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
#ifndef _QUEUE_H_
|
||||
#define _QUEUE_H_
|
||||
|
||||
#include "armcm0.h"
|
||||
|
||||
typedef struct {
|
||||
int head;
|
||||
int tail;
|
||||
int size;
|
||||
uint8_t * data;
|
||||
} QUEUE;
|
||||
|
||||
void queue_init(QUEUE * queue, uint8_t * buf, int size);
|
||||
void queue_reset(QUEUE * queue);
|
||||
int enqueue(QUEUE * queue, uint8_t data);
|
||||
int dequeue(QUEUE * queue, uint8_t * data);
|
||||
int is_queue_empty(QUEUE * queue);
|
||||
int is_queue_full(QUEUE * queue);
|
||||
int queue_size(QUEUE * queue);
|
||||
|
||||
#endif //_QUEUE_H_
|
|
@ -0,0 +1,73 @@
|
|||
#ifndef _RTC_H_
|
||||
#define _RTC_H_
|
||||
|
||||
#include "ARMCM0.h"
|
||||
|
||||
#define RTC_CLK 32768UL //Éè¼ÆÖµ
|
||||
|
||||
#define ALARM_0 0
|
||||
#define ALARM_1 1
|
||||
|
||||
typedef enum {
|
||||
RTC_TICK_PRESCALER_1 = RTC_CLK/32768,
|
||||
RTC_TICK_PRESCALER_2 = RTC_CLK/16384,
|
||||
RTC_TICK_PRESCALER_4 = RTC_CLK/8192,
|
||||
RTC_TICK_PRESCALER_8 = RTC_CLK/4096,
|
||||
RTC_TICK_PRESCALER_16 = RTC_CLK/2048,
|
||||
RTC_TICK_PRESCALER_32 = RTC_CLK/1024,
|
||||
RTC_TICK_PRESCALER_64 = RTC_CLK/512,
|
||||
RTC_TICK_PRESCALER_128 = RTC_CLK/256,
|
||||
RTC_TICK_PRESCALER_256 = RTC_CLK/128,
|
||||
RTC_TICK_PRESCALER_512 = RTC_CLK/64,
|
||||
RTC_TICK_PRESCALER_1024 = RTC_CLK/32,
|
||||
RTC_TICK_PRESCALER_2048 = RTC_CLK/16,
|
||||
RTC_TICK_PRESCALER_4096 = RTC_CLK/8,
|
||||
RTC_TICK_PRESCALER_8192 = RTC_CLK/4,
|
||||
RTC_TICK_PRESCALER_16384 = RTC_CLK/2,
|
||||
RTC_TICK_PRESCALER_32768 = RTC_CLK/1
|
||||
} RTC_TICK_PRESCALER;
|
||||
|
||||
typedef enum {
|
||||
RTC_INT_ALARM0 = 0x01,
|
||||
RTC_INT_ALARM1 = 0x02,
|
||||
RTC_INT_TICK = 0x04,
|
||||
RTC_INT_SECOND = 0x08,
|
||||
RTC_INT_ALL = 0x0F
|
||||
} RTC_INT_TYPE;
|
||||
|
||||
typedef void (* RTC_IRQ_CALLBACK) (RTC_INT_TYPE type);
|
||||
|
||||
extern RTC_IRQ_CALLBACK irq_cb;
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef union {
|
||||
struct {
|
||||
uint8_t second;
|
||||
uint8_t minute;
|
||||
uint8_t hour;
|
||||
uint8_t week;
|
||||
} decimal_format;
|
||||
|
||||
uint8_t u8[4];
|
||||
|
||||
uint32_t u32;
|
||||
} RTC_TIME_TYPE;
|
||||
#pragma pack(pop)
|
||||
|
||||
void rtc_int_clear(RTC_INT_TYPE type);
|
||||
void rtc_int_enable(RTC_INT_TYPE type);
|
||||
void rtc_int_disable(RTC_INT_TYPE type);
|
||||
void rtc_start(void);
|
||||
void rtc_stop(void);
|
||||
bool rtc_status(void);
|
||||
void rtc_clear(void);
|
||||
void rtc_set_prescaler(uint32_t tick, uint8_t adjust_seconds_bit);
|
||||
RTC_TIME_TYPE rtc_get_alarm(int id);
|
||||
void rtc_set_alarm(int id, RTC_TIME_TYPE *time);
|
||||
void rtc_delete_alarm(int id);
|
||||
void rtc_set_calendar(RTC_TIME_TYPE *time);
|
||||
RTC_TIME_TYPE rtc_get_calendar(void);
|
||||
void rtc_init(uint32_t tick, uint8_t adjust_seconds_bit, RTC_IRQ_CALLBACK call_back);
|
||||
|
||||
#endif //_RTC_H_
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
#ifndef _SC_READER_H_
|
||||
#define _SC_READER_H_
|
||||
|
||||
#include "armcm0.h"
|
||||
|
||||
typedef enum SC_CALL_BACK_TYPE {
|
||||
ACT_CB,
|
||||
DACT_CB,
|
||||
CALL_BACK_NUM,
|
||||
}SC_CB;
|
||||
|
||||
typedef enum SC_PPS_CALL_BACK_TYPE {
|
||||
PPS_REQUEST_CB,
|
||||
PPS_FAIL_CB,
|
||||
PPS_CALL_BACK_NUM,
|
||||
}SC_PPS_CB;
|
||||
|
||||
typedef struct {
|
||||
uint8_t PPSS;
|
||||
uint8_t PPS[4];
|
||||
uint8_t PCK;
|
||||
uint8_t R_PPSS;
|
||||
uint8_t R_PPS[4];
|
||||
uint8_t R_PCK;
|
||||
} SC_PPS;
|
||||
|
||||
typedef void (*CALL_BACK)(void);
|
||||
typedef bool (*PPS_CALLBACK)(SC_PPS *pps);
|
||||
|
||||
extern void sc_reader_enable(void);
|
||||
extern void sc_reader_disable(void);
|
||||
extern void sc_reader_task(void);
|
||||
extern void sc_reader_config_vcc_level(bool en);
|
||||
extern void sc_reader_add_callback(CALL_BACK c, SC_CB type);
|
||||
extern void sc_reader_add_PPS_callback(PPS_CALLBACK c, SC_PPS_CB type);
|
||||
//
|
||||
extern void sc_reader_send(uint8_t *buf, int length);
|
||||
extern bool sc_reader_get(uint8_t *buf, int length);
|
||||
|
||||
|
||||
extern void sc_reader_warm_reset(void);
|
||||
|
||||
#endif //_SC_READER_H_
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef _SENSOR_SPI_H_
|
||||
#define _SENSOR_SPI_H_
|
||||
|
||||
#include "ARMCM0.h"
|
||||
|
||||
#define R_WRIT_POL 0 /// 0 - 最高读写位是0
|
||||
|
||||
extern void SPIBurstReadSetAddr(uint8_t len, uint8_t* paddr);
|
||||
extern void SPIBurstRead(uint8_t *pbuf, uint8_t len);
|
||||
extern void SPISingleWrite(uint8_t addr, uint8_t data);
|
||||
extern void SPISingleRead(uint8_t addr, uint8_t *p_data);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef _SPI__H__
|
||||
#define _SPI__H__
|
||||
|
||||
#include "ARMCM0.h"
|
||||
|
||||
enum spi_interface{
|
||||
FLASH_SPI,
|
||||
LCD_SPI,
|
||||
};
|
||||
|
||||
extern void spi_cs_enable(enum spi_interface interface);
|
||||
extern void spi_cs_disable(enum spi_interface interface);
|
||||
|
||||
extern void spi_init(enum spi_interface interface);
|
||||
extern void spi_write(uint8_t *p_data,uint16_t num);
|
||||
extern void spi_read(uint8_t *p_data,uint16_t num);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
/*************************************CopyRight(c)************************************************
|
||||
** ½Î÷ÂÀ²¼¿Æ¼¼ÓÐÏÞ¹«Ë¾
|
||||
** www.lvbu.tech
|
||||
**************************************************************************************************
|
||||
**Filename:
|
||||
**Version:
|
||||
**Author:
|
||||
**Date:
|
||||
**Description:
|
||||
**
|
||||
**************************************************************************************************
|
||||
**Version:
|
||||
**Modifier:
|
||||
**Datemodified:
|
||||
**Description:
|
||||
**
|
||||
**************************************************************************************************/
|
||||
#ifndef __SPI_SOFT_H
|
||||
#define __SPI_SOFT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* Include
|
||||
*/
|
||||
#include "ARMCM0.h"
|
||||
/*********************************************************************
|
||||
* Macros
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Constants
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Typedefs
|
||||
*/
|
||||
#define SOFT_SPI_DEF(_name) \
|
||||
static spi_soft_t _name ;
|
||||
|
||||
|
||||
typedef struct spi_soft_s spi_soft_t;
|
||||
|
||||
struct spi_soft_s
|
||||
{
|
||||
uint32_t csio;
|
||||
uint32_t wrio;
|
||||
uint32_t rdio;
|
||||
uint32_t dataio;
|
||||
};
|
||||
/*********************************************************************
|
||||
* Global Variables
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Global Functions
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* External Variables
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* External Functions
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Local Vriables
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Local Functions
|
||||
*/
|
||||
extern void spi_soft_init(spi_soft_t * p_spis, uint32_t cs_io,uint32_t wr_io, uint32_t rd_io, uint32_t data_io );
|
||||
extern void spi_soft_writebit(spi_soft_t * p_spis,uint8_t Data,uint8_t cnt);
|
||||
extern void spi_soft_cs_enable(spi_soft_t * p_spis);
|
||||
extern void spi_soft_cs_disenable(spi_soft_t * p_spis);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __SPI_SOFT_H */
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
#ifndef _TIMER_H_
|
||||
#define _TIMER_H_
|
||||
|
||||
#include "ARMCM0.h"
|
||||
|
||||
extern void timer_0_enable(uint32_t interval, void * p_callback); // 32.768KHz
|
||||
extern void timer_0_disable(void);
|
||||
extern void timer_0_delay_32us(uint32_t cnt);
|
||||
extern uint32_t timer_0_get_val(void);
|
||||
extern void timer_1_enable(uint32_t interval, void * p_callback); // 32.768KHz
|
||||
extern void timer_1_disable(void);
|
||||
extern uint32_t timer_1_get_val(void);
|
||||
extern void timer_2_enable(uint32_t interval, void * p_callback); // 32.768KHz
|
||||
extern void timer_2_disable(void);
|
||||
extern uint32_t timer_2_get_val(void);
|
||||
extern void timer_3_enable(uint32_t interval, void * p_callback); // 32.768KHz
|
||||
extern void timer_3_disable(void);
|
||||
extern uint32_t timer_3_get_val(void);
|
||||
|
||||
typedef enum {
|
||||
H_TIMER_PERSCALER_1 = 0,
|
||||
H_TIMER_PERSCALER_2 = 1,
|
||||
H_TIMER_PERSCALER_4 = 2,
|
||||
H_TIMER_PERSCALER_8 = 3,
|
||||
H_TIMER_PERSCALER_16 = 4,
|
||||
H_TIMER_PERSCALER_32 = 5,
|
||||
H_TIMER_PERSCALER_64 = 6,
|
||||
H_TIMER_PERSCALER_128 = 7,
|
||||
} H_TIMER_PERSCALER_TYPE;
|
||||
|
||||
extern void Htimer_enable(uint16_t interval, void (*p_callback)(void));
|
||||
extern uint16_t Htimer_get_val(void);
|
||||
extern void Htimer_disable(void);
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* TPDU module
|
||||
* TPDU state machine
|
||||
* TPDU request helper
|
||||
*/
|
||||
|
||||
#ifndef _TPDU_H_
|
||||
#define _TPDU_H_
|
||||
|
||||
#include "armcm0.h"
|
||||
|
||||
enum HEADER_INDEX {
|
||||
CLA,
|
||||
INS,
|
||||
P1,
|
||||
P2,
|
||||
P3,
|
||||
HEADER_SIZE
|
||||
};
|
||||
|
||||
enum STATUS_INDEX {
|
||||
SW1,
|
||||
SW2,
|
||||
SW_SIZE
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
uint8_t header[HEADER_SIZE];
|
||||
bool writeCommand;
|
||||
uint8_t data[256];
|
||||
uint8_t sw[SW_SIZE];
|
||||
} TPDU_COMMAND;
|
||||
|
||||
/* TPDU task state definition */
|
||||
typedef enum {
|
||||
TT_IDLE,
|
||||
TT_SEND_HEADER,
|
||||
TT_DETERMINE_PROCEDURE,
|
||||
TT_WAIT_FOR_TX_FINISH,
|
||||
TT_DATA,
|
||||
TT_ERROR,
|
||||
} TPDU_TASK_STATE;
|
||||
|
||||
/* TPDU callback definition */
|
||||
typedef void (*TPDU_CALLBACK)(TPDU_COMMAND *command);
|
||||
|
||||
bool tpdu_request(TPDU_COMMAND *command, TPDU_CALLBACK callback);
|
||||
void tpdu_reset(void);
|
||||
TPDU_TASK_STATE tpdu_task(bool tx_finished);
|
||||
|
||||
#endif //_TPDU_H_
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
#ifndef _UART_H_
|
||||
#define _UART_H_
|
||||
|
||||
#include "ARMCM0.h"
|
||||
|
||||
enum UART_BAUD_SEL {
|
||||
UART_BAUD_1200 = 0x00,
|
||||
UART_BAUD_2400 = 0x01,
|
||||
UART_BAUD_4800 = 0x02,
|
||||
UART_BAUD_9600 = 0x03,
|
||||
UART_BAUD_14400 = 0x04,
|
||||
UART_BAUD_19200 = 0x05,
|
||||
UART_BAUD_38400 = 0x06,
|
||||
UART_BAUD_57600 = 0x07,
|
||||
UART_BAUD_115200 = 0x08,
|
||||
UART_BAUD_230400 = 0x09,
|
||||
UART_BAUD_460800 = 0x0A,
|
||||
UART_BAUD_921600 = 0x0B,
|
||||
};
|
||||
|
||||
enum UART_FLOWCTRL {
|
||||
UART_RTS_CTS_ENABLE = 0x01,
|
||||
UART_RTS_CTS_DISABLE = 0x00,
|
||||
};
|
||||
|
||||
extern void uart_0_init(void);
|
||||
extern void uart_0_write(uint8_t data);
|
||||
extern void uart_0_read(uint8_t *pcnt, uint8_t *pbuf);
|
||||
extern void uart_0_close(void);
|
||||
extern void uart_0_ClrFiFo(void);
|
||||
|
||||
extern void uart_1_init(void);
|
||||
extern void uart_1_write(uint8_t data);
|
||||
extern void uart_1_read(uint8_t *pcnt, uint8_t *pbuf);
|
||||
extern void uart_1_close(void);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,15 @@
|
|||
#ifndef _WDT_H_
|
||||
#define _WDT_H_
|
||||
|
||||
#include "ARMCM0.h"
|
||||
|
||||
#define _WDT_
|
||||
|
||||
#ifdef _WDT_
|
||||
extern void wdt_enable(uint16_t count);
|
||||
extern void wdt_clear(void);
|
||||
extern void wdt_disable(void);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,39 @@
|
|||
#ifndef _ANCS_H_
|
||||
#define _ANCS_H_
|
||||
|
||||
#include "lib.h"
|
||||
|
||||
#define ANCS_MSG_LEN 300
|
||||
#define ANCS_TITLE_LEN 18
|
||||
#define ANCS_APPID_LEN 30
|
||||
|
||||
|
||||
typedef enum
|
||||
{
|
||||
ACTION_ID_POSITIVE = 0, /**< Positive action. */
|
||||
ACTION_ID_NEGATIVE /**< Negative action. */
|
||||
} ancs_action_id_values_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t valid;
|
||||
uint8_t appid_len;
|
||||
uint8_t appid[ANCS_APPID_LEN];
|
||||
uint8_t title_len;
|
||||
uint8_t title[ANCS_TITLE_LEN];
|
||||
uint16_t msg_len;
|
||||
uint8_t msg[ANCS_MSG_LEN];
|
||||
uint8_t ancs_uid[4];
|
||||
} ancs_msg_t;
|
||||
|
||||
extern ancs_msg_t ancs_msg;
|
||||
|
||||
extern void ancs_service_enable(void);
|
||||
extern void clr_ancs_msg(void);
|
||||
extern void ancs_service_att_data(struct attc_ble_evt *p_attc);
|
||||
extern void ancs_result(uint8_t result);
|
||||
|
||||
|
||||
extern void ancs_perform_notification_action(uint8_t *uid, ancs_action_id_values_t action_id);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,122 @@
|
|||
/********************************************************************************
|
||||
* @file calender.h
|
||||
* @author Mingming Liu
|
||||
* @version V1.0.0
|
||||
* @date 12-08-2016
|
||||
* @Emial ming.liu@syd-tek.com
|
||||
* @brief head file for calender.c
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef _CALENDER_H
|
||||
#define _CALENDER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* MACROS
|
||||
*/
|
||||
#define BUILD_UINT16(loByte, hiByte) \
|
||||
((uint16_t)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)))
|
||||
|
||||
#define IsLeapYear(yr) (!((yr) % 400) || (((yr) % 100) && !((yr) % 4)))
|
||||
|
||||
/*********************************************************************
|
||||
* TYPEDEFS
|
||||
*/
|
||||
|
||||
// number of seconds since 0 hrs, 0 minutes, 0 seconds, on the
|
||||
// 1st of January 2000 UTC
|
||||
typedef uint32_t UTCTime;
|
||||
|
||||
// To be used with
|
||||
typedef struct
|
||||
{
|
||||
uint8_t seconds; // 0-59
|
||||
uint8_t minutes; // 0-59
|
||||
uint8_t hour; // 0-23
|
||||
uint8_t day; // 0-30
|
||||
uint8_t month; // 0-11
|
||||
uint16_t year; // 2000+
|
||||
} UTCTimeStruct;
|
||||
|
||||
// To be used with
|
||||
typedef struct
|
||||
{
|
||||
uint8_t RSVD0;
|
||||
uint8_t RSVD1;
|
||||
uint8_t year_low; // *(pData+2)
|
||||
uint8_t month; // *(pData+3)
|
||||
uint8_t day; // *(pData+4)
|
||||
uint8_t hour; // *(pData+5)
|
||||
uint8_t minutes; // *(pData+6)
|
||||
uint8_t seconds; // *(pData+7)
|
||||
uint8_t week; // *(pData+8)
|
||||
} Time_clock_Struct;
|
||||
|
||||
/*********************************************************************
|
||||
* FUNCTIONS
|
||||
*/
|
||||
|
||||
/*
|
||||
* Updates the clock and Timers from the MAC 320us timer tick.
|
||||
*/
|
||||
extern void TimeUpdate( void );
|
||||
|
||||
/*
|
||||
* Set the new time. This will only set the seconds portion
|
||||
* of time and doesn't change the factional second counter.
|
||||
* newTime - number of seconds since 0 hrs, 0 minutes,
|
||||
* 0 seconds, on the 1st of January 2000 UTC
|
||||
*/
|
||||
extern void Set_Clock( UTCTime newTime );
|
||||
|
||||
/*
|
||||
* Gets the current time. This will only return the seconds
|
||||
* portion of time and doesn't include the factional second counter.
|
||||
* returns: number of seconds since 0 hrs, 0 minutes,
|
||||
* 0 seconds, on the 1st of January 2000 UTC
|
||||
*/
|
||||
extern UTCTime Get_Clock( void );
|
||||
|
||||
/*
|
||||
* Converts UTCTime to UTCTimeStruct
|
||||
*
|
||||
* secTime - number of seconds since 0 hrs, 0 minutes,
|
||||
* 0 seconds, on the 1st of January 2000 UTC
|
||||
* tm - pointer to breakdown struct
|
||||
*/
|
||||
extern void ConvertUTCTime( UTCTimeStruct *tm, UTCTime secTime );
|
||||
|
||||
/*
|
||||
* Converts UTCTimeStruct to UTCTime (seconds since 00:00:00 01/01/2000)
|
||||
*
|
||||
* tm - pointer to UTC time struct
|
||||
*/
|
||||
extern UTCTime ConvertUTCSecs( UTCTimeStruct *tm );
|
||||
|
||||
/*
|
||||
* Update/Adjust the clock and timers
|
||||
* Msec - elapsed time in milli seconds
|
||||
*/
|
||||
extern void AdjustTimer( uint32_t Msec );
|
||||
|
||||
extern uint8_t * timeAppClockGet( uint8_t *pData );
|
||||
|
||||
extern void timeAppClockSet( uint8_t *pData );
|
||||
|
||||
extern void timer_calender_handle(void);
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
|
@ -0,0 +1,861 @@
|
|||
#ifndef _BLE_LIB_H_
|
||||
#define _BLE_LIB_H_
|
||||
|
||||
#include "ARMCM0.h"
|
||||
|
||||
enum _RETURN_STATUS_ {
|
||||
_PARAM_ERROR_ = 0x00,
|
||||
_NO_ERROR_ = 0x01,
|
||||
};
|
||||
|
||||
enum _COMPANY_ID_ {
|
||||
COMPANY_ID_SYD = 0x0,
|
||||
};
|
||||
|
||||
enum _QFN_TYPE_ {
|
||||
T_QFN_48 = 0,
|
||||
T_QFN_32 = 1,
|
||||
};
|
||||
|
||||
#ifndef CONFIG_TARGET_LIB
|
||||
|
||||
#pragma pack(1)
|
||||
|
||||
#define MAX_ATT_REPORT_HDL 20
|
||||
#define MAX_ATT_DATA_SZ 23
|
||||
#define BD_ADDR_SZ 6
|
||||
#define MAX_KEY_SZ 16
|
||||
#define MIN_KEY_SZ 7
|
||||
#define MAX_RAND_SZ 8
|
||||
#define MAX_EDIV_SZ 2
|
||||
#define ACCESS_CODE_SZ 4
|
||||
#define MAX_ADV_DATA_SZ 31
|
||||
#define MAX_SCAN_DEV_NUM 8
|
||||
|
||||
#define DIR_IN 0
|
||||
#define DIR_OUT 1
|
||||
|
||||
enum SCAN_TYPE {
|
||||
PASSIVE_SCAN = 0x00,
|
||||
ACTIVE_SCAN = 0x01,
|
||||
};
|
||||
|
||||
enum _BLE_ADDRESS_TYPE_ {
|
||||
PUBLIC_ADDRESS_TYPE = 0x00,
|
||||
RANDOM_ADDRESS_TYPE = 0x01,
|
||||
};
|
||||
|
||||
enum GAP_RET_STATUS {
|
||||
RET_FAIL = 0x00,
|
||||
RET_SUCCESS = 0x01,
|
||||
};
|
||||
|
||||
enum _EVT_TYPE_ {
|
||||
GAP_EVT = 0x00,
|
||||
};
|
||||
|
||||
enum _GAP_EVT_ {
|
||||
GAP_EVT_ADV_END = 0x00000001,
|
||||
GAP_EVT_CONNECTED = 0x00000002,
|
||||
GAP_EVT_DISCONNECTED = 0x00000004,
|
||||
GAP_EVT_PAIRING_COMP = 0x00000008,
|
||||
GAP_EVT_PASSKEY_REQ = 0x00000010,
|
||||
GAP_EVT_SHOW_PASSKEY_REQ = 0x00000020,
|
||||
GAP_EVT_CONNECTION_INTERVAL = 0x00000040,
|
||||
GAP_EVT_CONNECTION_SLEEP = 0x00000080,
|
||||
GAP_EVT_ATT_READ = 0x00000100,
|
||||
GAP_EVT_ATT_WRITE = 0x00000200,
|
||||
GAP_EVT_ATT_PREPARE_WRITE = 0x00000400,
|
||||
GAP_EVT_ATT_EXECUTE_WRITE = 0x00000800,
|
||||
GAP_EVT_ATT_HANDLE_CONFIRMATION = 0x00001000,
|
||||
GAP_EVT_ATT_HANDLE_CONFIGURE = 0x00002000,
|
||||
GAP_EVT_ENC_START = 0x00004000,
|
||||
GAP_EVT_L2CAP_UPDATE_RSP = 0x00008000,
|
||||
GAP_EVT_CONN_UPDATE_COMP = 0x00010000,
|
||||
GAP_EVT_SCAN_REPORT = 0x00020000,
|
||||
GAP_EVT_SCAN_END = 0x00040000,
|
||||
GAP_EVT_PAIRING_START = 0x00080000,
|
||||
};
|
||||
|
||||
enum _ADV_CH_PKT_TYPE_ {
|
||||
ADV_IND = 0x00,
|
||||
ADV_DIRECT_IND = 0x01,
|
||||
ADV_NOCONN_IND = 0x02,
|
||||
SCAN_REQ = 0x03,
|
||||
SCAN_RSP = 0x04,
|
||||
CONNECT_REQ = 0x05,
|
||||
ADV_SCAN_IND = 0x06,
|
||||
};
|
||||
|
||||
enum BLE_SEND_TYPE {
|
||||
BLE_GATT_NOTIFICATION = 0x0001,
|
||||
BLE_GATT_INDICATION = 0x0002,
|
||||
};
|
||||
|
||||
enum WAKEUP_TYPE {
|
||||
SLEEP_WAKEUP = 0,
|
||||
POWERDOWN_WAKEUP = 1,
|
||||
};
|
||||
|
||||
enum SMP_IO_CAPABILITY {
|
||||
IO_DISPLAY_ONLY = 0x00,
|
||||
IO_DISPLAY_YESNO = 0x01,
|
||||
IO_KEYBOARD_ONLY = 0x02,
|
||||
IO_NO_INPUT_OUTPUT = 0x03,
|
||||
IO_KEYBOARD_DISPLAY = 0x04,
|
||||
};
|
||||
|
||||
enum SMP_OOB_FLAG {
|
||||
OOB_AUTH_NOT_PRESENT = 0x00,
|
||||
OOB_AUTH_PRESENT = 0x01,
|
||||
};
|
||||
|
||||
enum SMP_BONDING_FLAGS {
|
||||
AUTHREQ_NO_BONDING = 0x00,
|
||||
AUTHREQ_BONDING = 0x01,
|
||||
};
|
||||
|
||||
enum SMP_KEY_DISTRIBUTION {
|
||||
SMP_KEY_MASTER_IDEN = 0x01,
|
||||
SMP_KEY_ADDR_INFO = 0x02,
|
||||
SMP_KEY_SIGNIN_INFO = 0x04,
|
||||
};
|
||||
|
||||
enum SMP_FAILED_CODE {
|
||||
SMP_RESERVED = 0x00,
|
||||
SMP_PASSKEY_ENTRY_FAILED = 0x01,
|
||||
SMP_OOB_NOT_AVAILABLE = 0x02,
|
||||
SMP_AUTH_REQUIREMENTS = 0x03,
|
||||
SMP_CONFIRM_VALUE_FAILED = 0x04,
|
||||
SMP_PAIRING_NOT_SUPPORTED = 0x05,
|
||||
SMP_ENCTYPTION_KEY_SZ = 0x06,
|
||||
SMP_COMMAND_NOT_SUPPORTED = 0x07,
|
||||
SMP_UNSPECIFIED_REASON = 0x08,
|
||||
SMP_REPEATED_ATTEMPTS = 0x09,
|
||||
SMP_INVALID_PARAMETERS = 0x0A,
|
||||
SMP_FAIL_TIMEOUT = 0xFF,
|
||||
};
|
||||
|
||||
enum _SYSTEM_CLOCK_SEL_ {
|
||||
SYSTEM_CLOCK_64M_RCOSC = 0x00,
|
||||
SYSTEM_CLOCK_32M_RCOSC = 0x01,
|
||||
SYSTEM_CLOCK_16M_RCOSC = 0x02,
|
||||
SYSTEM_CLOCK_8M_RCOSC = 0x03,
|
||||
SYSTEM_CLOCK_4M_RCOSC = 0x04,
|
||||
SYSTEM_CLOCK_2M_RCOSC = 0x05,
|
||||
SYSTEM_CLOCK_1M_RCOSC = 0x06,
|
||||
SYSTEM_CLOCK_500k_RCOSC = 0x07,
|
||||
SYSTEM_CLOCK_32M_XOSC = 0x08,
|
||||
};
|
||||
|
||||
enum _32K_CLOCK_SEL_ {
|
||||
SYSTEM_32K_CLOCK_RCOSC = 0x00,
|
||||
SYSTEM_32K_CLOCK_XOSC = 0x01,
|
||||
SYSTEM_32K_CLOCK_32MXO_DIV = 0x02,
|
||||
};
|
||||
|
||||
enum SOFT_TIMER_CTRL_TYPE {
|
||||
/* 0x0001 ~ 0x0010 internal used */
|
||||
SOFT_TIMER_0 = 0x0020,
|
||||
SOFT_TIMER_1 = 0x0040,
|
||||
SOFT_TIMER_2 = 0x0080,
|
||||
SOFT_TIMER_3 = 0x0100,
|
||||
SOFT_TIMER_4 = 0x0200,
|
||||
};
|
||||
|
||||
typedef void (*sys_timer_cb)(void);
|
||||
|
||||
struct gap_ble_addr {
|
||||
uint8_t type; // \ref _BLE_ADDRESS_TYPE_
|
||||
uint8_t addr[BD_ADDR_SZ];
|
||||
};
|
||||
|
||||
struct gap_key_params {
|
||||
uint8_t ediv[MAX_EDIV_SZ];
|
||||
uint8_t rand[MAX_RAND_SZ];
|
||||
uint8_t ltk[MAX_KEY_SZ];
|
||||
uint8_t iden[MAX_KEY_SZ];
|
||||
};
|
||||
|
||||
struct gap_bond_dev {
|
||||
struct gap_ble_addr addr;
|
||||
struct gap_key_params key;
|
||||
};
|
||||
|
||||
struct gap_adv_params {
|
||||
uint8_t type; // \ref _ADV_CH_PKT_TYPE_
|
||||
struct gap_ble_addr peer_addr;
|
||||
uint8_t policy;
|
||||
uint8_t channel;
|
||||
uint16_t interval; /**< Between 0x0020 and 0x4000 in 0.625 ms units (20ms to 10.24s) */
|
||||
uint16_t timeout; /**< Between 0x0001 and 0x3FFF in seconds, 0x0000 is disable */
|
||||
};
|
||||
|
||||
struct gap_scan_params {
|
||||
uint8_t mode; // \ref SCAN_TYPE
|
||||
uint8_t channel;
|
||||
uint16_t interval;
|
||||
uint16_t window;
|
||||
uint16_t timeout;
|
||||
};
|
||||
|
||||
struct gap_scan_dev {
|
||||
uint8_t adv_type; // \ref _ADV_CH_PKT_TYPE_
|
||||
struct gap_ble_addr peer_addr;
|
||||
};
|
||||
|
||||
struct gap_scan_report_evt {
|
||||
struct gap_scan_dev dev;
|
||||
uint8_t data_len;
|
||||
uint8_t adv_data[MAX_ADV_DATA_SZ];
|
||||
uint8_t rssi;
|
||||
};
|
||||
|
||||
struct gap_scan_end_evt {
|
||||
uint8_t dev_cnt;
|
||||
struct gap_scan_dev *p_dev_list;
|
||||
};
|
||||
|
||||
struct gap_update_params {
|
||||
uint16_t updateitv_min;
|
||||
uint16_t updateitv_max;
|
||||
uint16_t updatelatency;
|
||||
uint16_t updatesvto;
|
||||
};
|
||||
|
||||
struct gap_disconnected_evt {
|
||||
uint8_t reason; // HCI error codes
|
||||
};
|
||||
|
||||
struct gap_pairing_comp_evt {
|
||||
uint8_t status; // \ref GAP_RET_STATUS
|
||||
uint8_t dir; // \ref DIR_IN or DIR_OUT
|
||||
uint8_t reason; // \ref SMP_FAILED_CODE
|
||||
struct gap_key_params enc_key;
|
||||
};
|
||||
|
||||
struct gap_att_read_evt {
|
||||
uint16_t primary;
|
||||
uint16_t uuid;
|
||||
uint16_t hdl;
|
||||
uint16_t offset;
|
||||
};
|
||||
|
||||
struct gap_att_write_evt {
|
||||
uint16_t primary;
|
||||
uint16_t uuid;
|
||||
uint16_t hdl;
|
||||
uint8_t sz;
|
||||
uint8_t data[MAX_ATT_DATA_SZ];
|
||||
};
|
||||
|
||||
struct gap_att_pre_write_evt {
|
||||
uint16_t primary;
|
||||
uint16_t uuid;
|
||||
uint16_t hdl;
|
||||
uint16_t offset;
|
||||
uint8_t sz;
|
||||
uint8_t data[MAX_ATT_DATA_SZ];
|
||||
};
|
||||
|
||||
struct gap_att_exec_write_evt {
|
||||
uint8_t flags;
|
||||
};
|
||||
|
||||
struct gap_att_handle_configure_evt {
|
||||
uint16_t uuid;
|
||||
uint16_t hdl;
|
||||
uint16_t value;
|
||||
};
|
||||
|
||||
enum L2CAP_UPDATE_RSP_RES {
|
||||
CONN_PARAMS_ACCEPTED = 0x0000,
|
||||
CONN_PARAMS_REJECTED = 0x0001,
|
||||
CONN_PARAM_SMART_TIMEROUT = 0x0002, //该类型的事件并不是标准的BLE事件,而是随着smart_update功能而添加的,不使用smart_update功能不会上报该事件
|
||||
CONN_PARAM_SMART_SUCCEED = 0x0003, //该类型的事件并不是标准的BLE事件,而是随着smart_update功能而添加的,不使用smart_update功能不会上报该事件
|
||||
CONN_PARAM_LATENCY_ENABLE = 0x0004, //该类型的事件并不是标准的BLE事件,而是随着smart_update功能而添加的,不使用smart_update功能不会上报该事件
|
||||
CONN_PARAM_LATENCY_DISABLE = 0x0005, //该类型的事件并不是标准的BLE事件,而是随着smart_update功能而添加的,不使用smart_update功能不会上报该事
|
||||
};
|
||||
|
||||
struct gap_l2cap_update_rsp_evt {
|
||||
uint16_t result; // \ref L2CAP_UPDATE_RSP_RES
|
||||
};
|
||||
|
||||
struct gap_link_params {
|
||||
uint16_t interval;
|
||||
uint16_t latency;
|
||||
uint16_t svto;
|
||||
};
|
||||
|
||||
struct gap_ble_evt {
|
||||
uint8_t evt_type; // \ref _EVT_TYPE_
|
||||
uint32_t evt_code; // \ref _GAP_EVT_
|
||||
union
|
||||
{
|
||||
struct gap_disconnected_evt disconn_evt; // \ref GAP_EVT_DISCONNECTED
|
||||
struct gap_ble_addr bond_dev_evt; // \ref GAP_EVT_CONNECTED
|
||||
struct gap_pairing_comp_evt pairing_comp_evt; // \ref GAP_EVT_PAIRING_COMP
|
||||
struct gap_att_read_evt att_read_evt; // \ref GAP_EVT_ATT_READ
|
||||
struct gap_att_write_evt att_write_evt; // \ref GAP_EVT_ATT_WRITE
|
||||
struct gap_att_pre_write_evt att_pre_write_evt; // \ref GAP_EVT_ATT_PREPARE_WRITE
|
||||
struct gap_att_exec_write_evt att_exec_write_evt; // \ref GAP_EVT_ATT_EXECUTE_WRITE
|
||||
struct gap_att_handle_configure_evt att_handle_config_evt; // \ref GAP_EVT_ATT_HANDLE_CONFIGURE
|
||||
struct gap_l2cap_update_rsp_evt l2cap_update_rsp_evt; // \ref GAP_EVT_L2CAP_UPDATE_RSP
|
||||
struct gap_link_params conn_update_complete_evt; // \ref GAP_EVT_CONN_UPDATE_COMP
|
||||
struct gap_scan_report_evt scan_report_evt; // \ref GAP_EVT_SCAN_REPORT
|
||||
struct gap_scan_end_evt scan_end_evt; // \ref GAP_EVT_SCAN_END
|
||||
} evt;
|
||||
};
|
||||
|
||||
struct gap_evt_callback {
|
||||
uint32_t evt_mask;
|
||||
void (*p_callback)(struct gap_ble_evt *p_evt);
|
||||
};
|
||||
|
||||
struct gap_att_report {
|
||||
uint16_t primary;
|
||||
uint16_t uuid;
|
||||
uint16_t hdl;
|
||||
uint16_t config;
|
||||
uint16_t value;
|
||||
};
|
||||
|
||||
struct gap_att_report_handle {
|
||||
uint8_t cnt;
|
||||
struct gap_att_report report[MAX_ATT_REPORT_HDL];
|
||||
};
|
||||
|
||||
struct gap_wakeup_config {
|
||||
uint8_t wakeup_type;
|
||||
bool timer_wakeup_en;
|
||||
bool gpi_wakeup_en;
|
||||
bool wdt_wakeup_en;
|
||||
bool rtc_wakeup_en;
|
||||
bool capdet_wakeup_en;
|
||||
bool ana_wakeup_en;
|
||||
uint32_t gpi_wakeup_cfg;
|
||||
};
|
||||
|
||||
struct smp_pairing_req {
|
||||
uint8_t io;
|
||||
uint8_t oob;
|
||||
uint8_t flags:2;
|
||||
uint8_t mitm:1;
|
||||
uint8_t rsvd:5;
|
||||
uint8_t max_enc_sz;
|
||||
uint8_t init_key;
|
||||
uint8_t rsp_key;
|
||||
};
|
||||
|
||||
|
||||
/* attc */
|
||||
|
||||
enum ATT_CMD_CODE {
|
||||
ATT_ERR_RSP = 0x01,
|
||||
ATT_MTU_REQ = 0x02,
|
||||
ATT_MTU_RSP = 0x03,
|
||||
ATT_FIND_INFO_REQ = 0x04,
|
||||
ATT_FIND_INFO_RSP = 0x05,
|
||||
ATT_FIND_BY_TYPE_VALUE_REQ = 0x06,
|
||||
ATT_FIND_BY_TYPE_VALUE_RSP = 0x07,
|
||||
ATT_READ_BY_TYPE_REQ = 0x08,
|
||||
ATT_READ_BY_TYPE_RSP = 0x09,
|
||||
ATT_READ_REQ = 0x0A,
|
||||
ATT_READ_RSP = 0x0B,
|
||||
ATT_READ_BLOB_REQ = 0x0C,
|
||||
ATT_READ_BLOB_RSP = 0x0D,
|
||||
ATT_READ_MULTIPLE_REQ = 0x0E,
|
||||
ATT_READ_MULTIPLE_RSP = 0x0F,
|
||||
ATT_READ_BY_GROUP_TYPE_REQ = 0x10,
|
||||
ATT_READ_BY_GROUP_TYPE_RSP = 0x11,
|
||||
ATT_WRITE_REQ = 0x12,
|
||||
ATT_WRITE_RSP = 0x13,
|
||||
ATT_WRITE_CMD = 0x52,
|
||||
ATT_PREPARE_WRITE_REQ = 0x16,
|
||||
ATT_PREPARE_WRITE_RSP = 0x17,
|
||||
ATT_EXECUTE_WRITE_REQ = 0x18,
|
||||
ATT_EXECUTE_WRITE_RSP = 0x19,
|
||||
ATT_HANDLE_VAL_NOTIFICATION = 0x1B,
|
||||
ATT_HANDLE_VAL_INDICATION = 0x1D,
|
||||
ATT_HANDLE_VAL_CONFIRMATION = 0x1E,
|
||||
ATT_SIGNED_WRITE_CMD = 0xD2,
|
||||
};
|
||||
|
||||
enum ATT_ERROR_CODE {
|
||||
ATT_INVALID_HANDLE = 0x01,
|
||||
ATT_READ_NOT_PEMITTED = 0x02,
|
||||
ATT_WRITE_NOT_PEMITTED = 0x03,
|
||||
ATT_INVALID_PDU = 0x04,
|
||||
ATT_INSUFFICIENT_AUTHEN = 0x05,
|
||||
ATT_REQ_NOT_SUPPORTED = 0x06,
|
||||
ATT_INVALID_OFFSET = 0x07,
|
||||
ATT_INSUFFICIENT_AUTHOR = 0x08,
|
||||
ATT_PREPARE_QUEUE_FULL = 0x09,
|
||||
ATT_ATTRIBUTE_NOT_FOUND = 0x0A,
|
||||
ATT_ATTRIBUTE_NOT_LONG = 0x0B,
|
||||
ATT_INSUFFICIENT_ENC_KEY_SZ = 0x0C,
|
||||
ATT_INVALID_ATTRIBUTE_VAL_LEN= 0x0D,
|
||||
ATT_UNLIKELY_ERROR = 0x0E,
|
||||
ATT_INSUFFICIENT_ENC = 0x0F,
|
||||
ATT_UNSUPPORTED_GROUP_TYPE = 0x10,
|
||||
ATT_INSUFFICIENT_RESOURCES = 0x11,
|
||||
};
|
||||
|
||||
enum ATT_CHAR_PROPERTY {
|
||||
ATT_CHAR_BROADCAST = 0x01,
|
||||
ATT_CHAR_READ = 0x02,
|
||||
ATT_CHAR_WEIRE_WO_RSP = 0x04,
|
||||
ATT_CHAR_WEIRE = 0x08,
|
||||
ATT_CHAR_NOTIFY = 0x10,
|
||||
ATT_CHAR_INDICATE = 0x20,
|
||||
ATT_CHAR_AUTH_SIGNED_WRITE = 0x40,
|
||||
ATT_CHAR_EXTEND_PROPERTY = 0x80,
|
||||
};
|
||||
|
||||
struct att_err_rsp {
|
||||
uint8_t opcode; // \ref ATT_CMD_CODE
|
||||
uint16_t hdl;
|
||||
uint8_t err; // \ref ATT_ERROR_CODE
|
||||
};
|
||||
|
||||
struct att_mtu_req {
|
||||
uint16_t mtu;
|
||||
};
|
||||
|
||||
struct att_mtu_rsp {
|
||||
uint16_t mtu;
|
||||
};
|
||||
|
||||
struct att_find_info_req {
|
||||
uint16_t start_hdl;
|
||||
uint16_t end_hdl;
|
||||
};
|
||||
|
||||
struct att_find_info_16 {
|
||||
uint16_t hdl;
|
||||
uint8_t uuid[2];
|
||||
};
|
||||
|
||||
struct att_find_info_128 {
|
||||
uint16_t hdl;
|
||||
uint8_t uuid[16];
|
||||
};
|
||||
|
||||
union att_find_info_payload {
|
||||
struct att_find_info_16 uuid16[5];
|
||||
struct att_find_info_128 uuid128;
|
||||
};
|
||||
|
||||
struct att_find_info_rsp {
|
||||
uint8_t format;
|
||||
union att_find_info_payload pair;
|
||||
};
|
||||
|
||||
enum _GATT_FIND_INFO_UUID_TYPE_ {
|
||||
GATT_FIND_INFO_UUID_16 = 0x01,
|
||||
GATT_FIND_INFO_UUID_128 = 0x02,
|
||||
};
|
||||
|
||||
struct att_find_by_type_val_req {
|
||||
uint16_t start_hdl;
|
||||
uint16_t end_hdl;
|
||||
uint16_t att_type;
|
||||
uint8_t att_val[MAX_ATT_DATA_SZ-7];
|
||||
};
|
||||
|
||||
struct att_find_by_type_val_rsp {
|
||||
uint8_t list[MAX_ATT_DATA_SZ-1];
|
||||
};
|
||||
|
||||
struct att_read_by_type_req {
|
||||
uint16_t start_hdl;
|
||||
uint16_t end_hdl;
|
||||
uint8_t att_type[16];
|
||||
};
|
||||
|
||||
|
||||
struct att_read_by_type_service_16 {
|
||||
uint16_t hdl;
|
||||
uint8_t uuid[2];
|
||||
};
|
||||
|
||||
struct att_read_by_type_service_128 {
|
||||
uint16_t hdl;
|
||||
uint8_t uuid[16];
|
||||
};
|
||||
|
||||
union att_read_by_type_service_payload {
|
||||
struct att_read_by_type_service_16 uuid16[3];
|
||||
struct att_read_by_type_service_128 uuid128;
|
||||
};
|
||||
|
||||
struct att_read_by_type_service_rsp {
|
||||
uint8_t length;
|
||||
union att_read_by_type_service_payload pair;
|
||||
};
|
||||
|
||||
struct att_read_by_type_16 {
|
||||
uint16_t hdl;
|
||||
uint8_t property;
|
||||
uint16_t val_hdl;
|
||||
uint8_t char_uuid[2];
|
||||
};
|
||||
|
||||
struct att_read_by_type_128 {
|
||||
uint16_t hdl;
|
||||
uint8_t property;
|
||||
uint16_t val_hdl;
|
||||
uint8_t char_uuid[16];
|
||||
};
|
||||
|
||||
union att_read_by_type_payload {
|
||||
struct att_read_by_type_16 uuid16[3];
|
||||
struct att_read_by_type_128 uuid128;
|
||||
};
|
||||
|
||||
struct att_read_by_type_rsp {
|
||||
uint8_t length;
|
||||
union att_read_by_type_payload pair;
|
||||
};
|
||||
|
||||
struct att_read_by_type_pair_val {
|
||||
uint16_t hdl;
|
||||
uint8_t val[10];
|
||||
};
|
||||
|
||||
struct att_read_by_type_val_rsp {
|
||||
uint8_t length;
|
||||
struct att_read_by_type_pair_val pair[1];
|
||||
};
|
||||
|
||||
struct att_read_by_type_chartextend_rsp {
|
||||
uint8_t length;
|
||||
uint16_t hdl;
|
||||
uint8_t val[MAX_ATT_DATA_SZ-4];
|
||||
};
|
||||
|
||||
struct att_read_by_type_include_rsp {
|
||||
uint8_t length;
|
||||
uint16_t hdl;
|
||||
uint8_t buf[MAX_ATT_DATA_SZ-2];
|
||||
};
|
||||
|
||||
struct att_read_req {
|
||||
uint16_t hdl;
|
||||
};
|
||||
|
||||
struct att_read_rsp {
|
||||
uint8_t buf[MAX_ATT_DATA_SZ-1];
|
||||
};
|
||||
|
||||
struct att_read_blob_req {
|
||||
uint16_t hdl;
|
||||
uint16_t offset;
|
||||
};
|
||||
|
||||
struct att_read_blob_rsp {
|
||||
uint8_t buf[MAX_ATT_DATA_SZ-1];
|
||||
};
|
||||
|
||||
struct att_read_multiple_req {
|
||||
uint16_t hdl[MAX_ATT_DATA_SZ-1];
|
||||
};
|
||||
|
||||
struct att_read_multiple_rsp {
|
||||
uint8_t val[MAX_ATT_DATA_SZ-1];
|
||||
};
|
||||
|
||||
struct att_read_by_group_type_req {
|
||||
uint16_t start_hdl;
|
||||
uint16_t end_hdl;
|
||||
uint8_t att_group_type[16];
|
||||
};
|
||||
|
||||
struct att_read_by_group_type_16 {
|
||||
uint16_t hdl;
|
||||
uint16_t end_hdl;
|
||||
uint8_t uuid[2];
|
||||
};
|
||||
|
||||
struct att_read_by_group_type_128 {
|
||||
uint16_t hdl;
|
||||
uint16_t end_hdl;
|
||||
uint8_t uuid[16];
|
||||
};
|
||||
|
||||
union att_read_by_group_type_payload {
|
||||
struct att_read_by_group_type_16 uuid16[3];
|
||||
struct att_read_by_group_type_128 uuid128;
|
||||
};
|
||||
|
||||
struct att_read_by_group_type_rsp {
|
||||
uint8_t length;
|
||||
union att_read_by_group_type_payload pair;
|
||||
};
|
||||
|
||||
struct att_write_req {
|
||||
uint16_t hdl;
|
||||
uint8_t buf[MAX_ATT_DATA_SZ-3];
|
||||
};
|
||||
|
||||
struct att_write_cmd {
|
||||
uint16_t hdl;
|
||||
uint8_t buf[MAX_ATT_DATA_SZ-3];
|
||||
};
|
||||
|
||||
struct att_prepare_write_req {
|
||||
uint16_t hdl;
|
||||
uint16_t offset;
|
||||
uint8_t buf[MAX_ATT_DATA_SZ-5];
|
||||
};
|
||||
|
||||
struct att_prepare_write_rsp {
|
||||
uint16_t hdl;
|
||||
uint16_t offset;
|
||||
uint8_t buf[MAX_ATT_DATA_SZ-5];
|
||||
};
|
||||
|
||||
struct att_execute_write_req {
|
||||
uint8_t flags;
|
||||
};
|
||||
|
||||
struct att_hdl_val_notifivation {
|
||||
uint16_t hdl;
|
||||
uint8_t buf[MAX_ATT_DATA_SZ-3];
|
||||
};
|
||||
|
||||
struct att_hdl_val_indication {
|
||||
uint16_t hdl;
|
||||
uint8_t buf[MAX_ATT_DATA_SZ-3];
|
||||
};
|
||||
|
||||
struct attc_ble_evt {
|
||||
uint8_t attc_code; // \ref ATT_CMD_CODE
|
||||
uint8_t attc_sz;
|
||||
union
|
||||
{
|
||||
struct att_err_rsp AttErrRsp;
|
||||
struct att_mtu_rsp AttMtuRsp;
|
||||
struct att_find_info_rsp AttFindInfoRsp;
|
||||
struct att_find_by_type_val_rsp AttFindByTypeValRsp;
|
||||
struct att_read_by_type_rsp AttReadByTypeRsp;
|
||||
struct att_read_by_type_include_rsp AttReadByTypeIncludeRsp;
|
||||
struct att_read_by_type_val_rsp AttReadByTypeValRsp;
|
||||
struct att_read_by_type_service_rsp AttReadByTypeServiceRsp;
|
||||
struct att_read_by_type_chartextend_rsp AttReadByTypeChartExtendRsp;
|
||||
struct att_read_rsp AttReadRsp;
|
||||
struct att_read_blob_rsp AttReadBlobRsp;
|
||||
struct att_read_multiple_rsp AttReadMultipleRsp;
|
||||
struct att_read_by_group_type_rsp AttReadByGroupTypeRsp;
|
||||
struct att_hdl_val_notifivation AttHdlValNotification;
|
||||
struct att_hdl_val_indication AttHdlValIndication;
|
||||
} attc;
|
||||
};
|
||||
|
||||
typedef void (*p_attc_callback)(struct attc_ble_evt *p_evt);
|
||||
|
||||
/* attc end*/
|
||||
|
||||
#define CONFIRMATION_EVT_NUM 10
|
||||
#define MAX_UPDATE_ADJ_NUM 4
|
||||
|
||||
enum GAP_SMART_UPDATE_DIRECTION {
|
||||
UPDATE_DIRECTION_UP = 0x01,
|
||||
UPDATE_DIRECTION_DOWN = 0x01,
|
||||
};
|
||||
enum GAP_SMART_UPDATE_STATE {
|
||||
UPDATE_STATE_LATENCY = 0x80,
|
||||
UPDATE_STATE_TIMEROUT = 0x40,
|
||||
UPDATE_STATE_ADJFINISH = 0x20,
|
||||
UPDATE_STATE_WAITANCHOR = 0x10,
|
||||
UPDATE_STATE_START = 0x08,
|
||||
UPDATE_REQ_END = 0x04,
|
||||
UPDATE_LATENCY_STATE = 0x02,
|
||||
UPDATE_STATE_CHANGE = 0x01,
|
||||
};
|
||||
enum GAP_SMART_CONTROL_SET {
|
||||
SMART_CONTROL_LATENCY = 0x80,
|
||||
SMART_CONTROL_UPDATE = 0x40,
|
||||
};
|
||||
struct gap_smart_update_params {
|
||||
uint8_t updatectrl;
|
||||
uint8_t updateadj_num;
|
||||
uint16_t updateitv_target;
|
||||
uint16_t updatelatency;
|
||||
uint16_t updatesvto;
|
||||
};
|
||||
struct GAP_smart_update_ctrol {
|
||||
uint8_t update_state;
|
||||
uint8_t update_direction;
|
||||
uint8_t updateadj_cnt;
|
||||
uint16_t loop_num;
|
||||
uint16_t updateitv_step;
|
||||
struct gap_smart_update_params update_params;
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
BLE_TX_POWER_MINUS_31_DBM = 0,
|
||||
BLE_TX_POWER_MINUS_25_DBM = 1,
|
||||
BLE_TX_POWER_MINUS_19_DBM = 2,
|
||||
BLE_TX_POWER_MINUS_13_DBM = 3,
|
||||
BLE_TX_POWER_MINUS_8_DBM = 4,
|
||||
BLE_TX_POWER_MINUS_3_DBM = 5,
|
||||
BLE_TX_POWER_0_DBM = 6,
|
||||
BLE_TX_POWER_2_DBM = 7,
|
||||
BLE_TX_POWER_4_DBM = 8,
|
||||
} BLE_TX_POWER;
|
||||
#pragma pack()
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef _BLE_LIB_C
|
||||
#else
|
||||
/* ble protocol */
|
||||
extern uint8_t BleInit(void);
|
||||
extern uint8_t DisConnect(void);
|
||||
|
||||
extern uint8_t SetDevAddr(struct gap_ble_addr* p_dev_addr);
|
||||
extern uint8_t GetDevAddr(struct gap_ble_addr* p_dev_addr);
|
||||
extern uint8_t SetLEFeature(uint8_t *p_feature);
|
||||
extern uint8_t SetAdvParams(struct gap_adv_params *p_adv_params);
|
||||
extern uint8_t SetAdvData(uint8_t *p_adv, uint8_t adv_sz, uint8_t *p_scan, uint8_t sacn_sz);
|
||||
extern uint8_t StartAdv(void);
|
||||
extern uint8_t StopAdv(void);
|
||||
|
||||
extern uint8_t SetScanParams(struct gap_scan_params *p_scan_params);
|
||||
extern uint8_t StartScan(void);
|
||||
extern uint8_t StopScan(void);
|
||||
|
||||
extern uint8_t SetSecParams(struct smp_pairing_req *p_sec_params);
|
||||
extern uint8_t SetPasskey(uint32_t passkey);
|
||||
extern uint8_t SecurityReq(uint8_t flag, uint8_t mitm);
|
||||
|
||||
extern uint8_t SetConnectionUpdate(struct gap_update_params *p_update_params);
|
||||
extern uint8_t GetLinkParameters(struct gap_link_params* p_link);
|
||||
extern uint8_t SetWinWideMinusCnt(uint8_t cnt);
|
||||
extern uint8_t GAPConnectionLatencyMode(uint8_t en);
|
||||
extern uint8_t SetEvtCallback(struct gap_evt_callback* p_callback);
|
||||
|
||||
extern uint8_t GetGATTReportHandle(struct gap_att_report_handle** p_hdl);
|
||||
extern uint8_t SetGATTReadRsp(uint8_t len,uint8_t *p_data);
|
||||
extern uint8_t CheckFIFOFull(void);
|
||||
extern uint8_t GATTDataSend(uint8_t type, struct gap_att_report* p_report, uint8_t len, uint8_t *p_data);
|
||||
|
||||
extern uint8_t Rand(void);
|
||||
extern void DelayUS(uint16_t dly);
|
||||
extern void DelayMS(uint32_t dly);
|
||||
extern uint8_t GetCompanyID(void);
|
||||
extern uint8_t GetQFNType(void);
|
||||
extern void RFRead(uint8_t addr, uint8_t * data);
|
||||
extern void RFWrite(uint8_t addr, uint8_t data);
|
||||
|
||||
extern void ble_sched_execute(void);
|
||||
extern bool ble_sched_finish(void);
|
||||
|
||||
|
||||
/* system clock */
|
||||
extern uint8_t LPOCalibration(void);
|
||||
extern uint8_t RCOSCCalibration(void);
|
||||
extern uint8_t MCUClockSwitch(uint8_t sel);
|
||||
extern uint8_t ClockSwitch(uint8_t sel);
|
||||
extern uint8_t GetMCUClock(uint8_t *p_sel);
|
||||
extern uint8_t GetClock(uint8_t *p_sel);
|
||||
|
||||
/* timer */
|
||||
extern uint8_t TimerStart(uint16_t type, uint32_t timecnt_100ms, bool reload, sys_timer_cb pfnCallback);
|
||||
extern void TimerStop(uint16_t type);
|
||||
|
||||
/* sleep & power down */
|
||||
extern uint8_t WakeupConfig(struct gap_wakeup_config *p_cfg);
|
||||
extern uint8_t LLSleep(void);
|
||||
extern uint8_t SystemSleep(void);
|
||||
extern uint8_t SystemPowerDown(void);
|
||||
extern uint8_t SystemReset(void);
|
||||
extern uint8_t RFSleep(void);
|
||||
extern uint8_t RFWakeup(void);
|
||||
extern uint8_t UartEn(uint8_t en);
|
||||
|
||||
/* bond Manager */
|
||||
extern uint8_t SetBondManagerIndex(uint8_t idx);
|
||||
extern uint8_t GetBondDevice(struct gap_bond_dev *p_device);
|
||||
extern uint8_t AddBondDevice(struct gap_bond_dev *p_device);
|
||||
extern uint8_t DelBondDevice(void);
|
||||
extern uint8_t DelAllBondDevice(void);
|
||||
|
||||
/* read&write flash */
|
||||
extern uint8_t ReadProfileData(uint16_t addr, uint16_t len, uint8_t *p_buf);
|
||||
extern uint8_t WriteProfileData(uint16_t addr, uint16_t len, uint8_t *p_buf);
|
||||
|
||||
extern uint8_t EraseFlashData(uint32_t addr, uint8_t sector_num);
|
||||
extern uint8_t ReadFlashData(uint32_t addr, uint16_t len, uint8_t *p_buf);
|
||||
extern uint8_t WriteFlashData(uint32_t addr, uint16_t len, uint8_t *p_buf);
|
||||
|
||||
/* fw upgrade */
|
||||
extern uint8_t CodeErase(void);
|
||||
extern uint8_t CodeWrite(uint32_t offset, uint32_t len, uint8_t *p_buf);
|
||||
extern uint8_t CodeUpdate(uint8_t *p_desc, uint8_t *p_ver, uint32_t sz, uint16_t checksum);
|
||||
/* att client */
|
||||
extern uint8_t ATTCSetCallback(p_attc_callback pfn_callback);
|
||||
extern uint8_t ATTCMTUReq(uint16_t mtu);
|
||||
extern uint8_t ATTCFindInfoReq(uint16_t start_hdl, uint16_t end_hdl);
|
||||
extern uint8_t ATTCFindByTypeValueReq(uint16_t start_hdl, uint16_t end_hdl, uint16_t type, uint8_t val_sz, uint8_t *p_val);
|
||||
extern uint8_t ATTCReadByTypeReq(uint16_t start_hdl, uint16_t end_hdl, uint16_t type_sz, uint8_t *p_type);
|
||||
extern uint8_t ATTCReadReq(uint16_t hdl);
|
||||
extern uint8_t ATTCReadBlobReq(uint16_t hdl, uint16_t offset);
|
||||
extern uint8_t ATTCReadMultipleReq(uint8_t hdl_sz, uint8_t *p_hdl);
|
||||
extern uint8_t ATTCReadByGroupTypeReq(uint16_t start_hdl, uint16_t end_hdl, uint16_t type_sz, uint8_t *p_type);
|
||||
extern uint8_t ATTCWriteReq(uint16_t hdl, uint16_t sz, uint8_t *p_buf);
|
||||
extern uint8_t ATTCWriteCmdReq(uint16_t hdl, uint16_t sz, uint8_t *p_buf);
|
||||
extern uint8_t ATTCPrepareWriteReq(uint16_t hdl, uint16_t offset, uint16_t sz, uint8_t *p_buf);
|
||||
extern uint8_t ATTCExecuteWriteReq(uint8_t flags);
|
||||
extern uint8_t ATTCConfirmation(void);
|
||||
|
||||
extern void GPADC_Manual_Calibration(uint8_t trim);
|
||||
|
||||
extern void BBRFWrite(uint8_t addr, uint8_t data);
|
||||
extern void BBRFRead(uint8_t addr, uint8_t* data);
|
||||
|
||||
extern uint8_t APPtimer_enable(uint8_t id, uint32_t intv, void * p_callback);
|
||||
extern uint8_t APPtimer_disable(uint8_t id);
|
||||
|
||||
extern void GAPBBDelayUS(uint16_t us);
|
||||
extern void GAPBBDelayMS(uint32_t us);
|
||||
extern uint8_t Setting4kUpdate(uint8_t *data, uint32_t checksum, uint8_t Xor);
|
||||
|
||||
extern uint8_t gap_s_smart_update_latency(struct gap_smart_update_params *p_smart_params);
|
||||
|
||||
extern bool GAPGetRFAutoSleep(void);
|
||||
extern void GAPSetRFAutoSleep(bool en);
|
||||
extern bool GAPGetUartEn(void);
|
||||
extern void GAPUartEn(bool en);
|
||||
|
||||
extern uint8_t Setting4kUpdate(uint8_t *data, uint32_t checksum, uint8_t Xor);
|
||||
extern void SetDevAddr_toflash(struct gap_ble_addr* p_dev);
|
||||
|
||||
extern uint8_t flash_peotect_key_confirm(uint32_t key,uint32_t key1);
|
||||
|
||||
extern uint8_t EraseCodeB(uint32_t addr, uint8_t sector_num);
|
||||
extern uint8_t ReadCodeB(uint32_t addr, uint16_t len, uint8_t *p_buf);
|
||||
extern uint8_t WriteCodeB(uint32_t addr, uint16_t len, uint8_t *p_buf);
|
||||
extern void ble_SetTxPower(BLE_TX_POWER value);
|
||||
extern uint8_t FlashDataUpdate( uint32_t sz, uint16_t checksum);
|
||||
extern uint32_t flash_data_size_get(void);
|
||||
extern uint8_t code_idx_get(void);
|
||||
extern uint8_t flash_data_size_set(uint32_t sz);
|
||||
extern uint8_t GPADC_Get_Calibration(void);
|
||||
extern uint8_t Get_RSSI_Last_RX(void);
|
||||
|
||||
extern uint8_t APP_Read_Attribute_Encrypt(uint16_t hdl,uint8_t *enc);
|
||||
extern uint8_t APP_Write_Attribute_Encrypt(uint16_t hdl,uint8_t *enc);
|
||||
extern uint8_t APP_Read_Charact_Permission(uint16_t hdl,uint8_t *permission);
|
||||
extern uint8_t APP_Write_Effect(void);
|
||||
extern void smp_aes_encrypt(uint8_t *k, uint8_t* p, uint8_t* c);
|
||||
extern void syd_set_chip_flash_all_size(uint32_t size);
|
||||
|
||||
extern uint8_t Xtal_trim_get(void);
|
||||
extern uint8_t Xtal_trim_set(uint8_t trim);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -0,0 +1,137 @@
|
|||
#ifndef _TIMER_HANDLER_H_
|
||||
#define _TIMER_HANDLER_H_
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#define TIMER_NUM 5
|
||||
|
||||
typedef void (* TIMER_IRQ_CALLBACK_TYPE)(void);
|
||||
|
||||
typedef enum {
|
||||
TIMER_0 = 0,
|
||||
TIMER_1,
|
||||
TIMER_2,
|
||||
TIMER_3,
|
||||
TIMER_4,
|
||||
} TIMER_ID;
|
||||
|
||||
struct SYD_HISTORY_SETTING {
|
||||
uint32_t SYD_timeSeconds;
|
||||
};
|
||||
|
||||
extern struct SYD_HISTORY_SETTING syd_history_setting_str;
|
||||
|
||||
#define EVT_ENABLE_MODE ((uint8_t) 0x01)
|
||||
#define EVT_DISABLE_MODE ((uint8_t) 0x00)
|
||||
|
||||
|
||||
#define EVT_MAX_NUM ((uint8_t) 0x1F)//定时器事件数
|
||||
|
||||
#define RTCEVT_ENABLE_MODE ((uint8_t) 0x01)
|
||||
#define RTCEVT_DISABLE_MODE ((uint8_t) 0x00)
|
||||
|
||||
|
||||
#define RTCEVT_MAX_NUM ((uint8_t) 0x1F)//定时器事件数
|
||||
|
||||
//0x80000000 作为系统计时事件使用
|
||||
|
||||
struct Array_Node
|
||||
{
|
||||
uint32_t evt_id;
|
||||
};
|
||||
|
||||
struct SYD_sysTimer {
|
||||
struct Array_Node Evt_Array_Node;
|
||||
void (*Timer_Evt_Handle)(void);//void是函数数组元素返回类型,*是数组参数类型
|
||||
uint32_t Evt_Array_Iterval;
|
||||
uint8_t Evt_Array_On_Off_Mode;
|
||||
uint32_t Evt_Array_Trigger_Loop;
|
||||
uint32_t Evt_Loop_Start_Point;
|
||||
};
|
||||
|
||||
extern void SYD_Timer_Init(uint8_t event_num, struct SYD_sysTimer p_sysTimer[]);
|
||||
extern void Timer_Evt_Init(void);
|
||||
extern void Timer_Evt_Start(uint32_t evt_id_para);
|
||||
extern void Timer_Evt_Stop(uint32_t evt_id_para);
|
||||
extern void Timer_Evt_Clr(uint32_t evt_id_para);
|
||||
extern uint8_t Timer_Get_State(uint32_t evt_id_para);
|
||||
extern void Timer_Evt_Creat(uint32_t evt_id_para,uint32_t evt_interval_para,void *evt_handle_para,uint8_t evt_mode_para);
|
||||
|
||||
typedef enum {
|
||||
RTC_INT_CMP0 = 1,
|
||||
RTC_INT_CMP1 = 2,
|
||||
RTC_INT_TICK = 4,
|
||||
RTC_INT_SECOND = 8,
|
||||
RTC_INT_ALL = 0xF,
|
||||
RTC_INT_NUM = 4,
|
||||
} RTC_INT_TYPE;
|
||||
|
||||
typedef void (* RTC_IRQ_CALLBACK) (RTC_INT_TYPE type);
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef union {
|
||||
struct {
|
||||
uint8_t second;
|
||||
uint8_t minute;
|
||||
uint8_t hour;
|
||||
uint8_t day;
|
||||
} decimal_format;
|
||||
uint8_t u8[4];
|
||||
uint32_t u32;
|
||||
} RTC_TIME_TYPE;
|
||||
#pragma pack(pop)
|
||||
|
||||
|
||||
struct RTC_Array_Node
|
||||
{
|
||||
uint32_t RTCEVT_id;
|
||||
};
|
||||
|
||||
struct SYD_sysRtc {
|
||||
struct RTC_Array_Node RTCEVT_Array_Node;
|
||||
void (*RTC_Evt_Handle)(void);//void是函数数组元素返回类型,*是数组参数类型
|
||||
uint32_t RTCEVT_Array_Iterval;
|
||||
uint8_t RTCEVT_Array_On_Off_Mode;
|
||||
uint32_t RTCEVT_Array_Trigger_Loop;
|
||||
uint32_t RTCEVT_Loop_Start_Point;
|
||||
};
|
||||
|
||||
extern uint32_t RTC_RUN(void);
|
||||
extern void RTC_SET(uint32_t Iterval);
|
||||
extern void SYD_RTC_Init(uint8_t event_num, struct SYD_sysRtc p_sysTimer[]);
|
||||
extern void RTC_EVT_Init(void);
|
||||
extern void RTC_EVT_Start(uint32_t RTCEVT_id_para);
|
||||
extern void RTC_EVT_Stop(uint32_t RTCEVT_id_para);
|
||||
extern void RTC_EVT_Clr(uint32_t RTCEVT_id_para);
|
||||
extern uint8_t RTC_Get_State(uint32_t RTCEVT_id_para);
|
||||
extern void RTC_EVT_Creat(uint32_t RTCEVT_id_para,uint32_t RTCEVT_interval_para,void *RTCEVT_handle_para,uint8_t RTCEVT_mode_para);
|
||||
|
||||
extern void rtc_set_interrupt_callback(RTC_IRQ_CALLBACK cb);
|
||||
extern void rtc_int_clear(RTC_INT_TYPE type);
|
||||
extern void rtc_int_enable(RTC_INT_TYPE type);
|
||||
extern void rtc_int_disable(RTC_INT_TYPE type);
|
||||
extern void rtc_start(void);
|
||||
extern void rtc_stop(void);
|
||||
extern void rtc_clear(void);
|
||||
extern void rtc_set_prescaler(uint32_t tick, bool adjust_seconds_bit);
|
||||
extern void rtc_set_seconds_bit(uint32_t order);
|
||||
extern RTC_TIME_TYPE rtc_get_compare(int id);
|
||||
extern void rtc_set_compare(int id, RTC_TIME_TYPE *time);
|
||||
extern RTC_TIME_TYPE rtc_get_calendar(void);
|
||||
extern void rtc_set_calendar(RTC_TIME_TYPE *time);
|
||||
extern bool rtc_status(void);
|
||||
extern uint32_t rtc_interrupt_status(void);
|
||||
extern void rtc_init(uint32_t tick,RTC_IRQ_CALLBACK cb);
|
||||
extern void RTC_EVT_whole_minute_setid(uint32_t RTCEVT_id_para);
|
||||
extern void RTC_EVT_whole_minute_adj(void);
|
||||
|
||||
|
||||
extern struct SYD_sysTimer syd_sysTimer[EVT_NUM];
|
||||
extern void Timer_Evt_List(void);
|
||||
extern struct SYD_sysRtc syd_rtc[RTCEVT_NUM];
|
||||
extern void RTC_Evt_List(void);
|
||||
|
||||
extern uint32_t TIMER_EVT;
|
||||
extern uint32_t RTC_EVT;
|
||||
|
||||
#endif
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,195 @@
|
|||
/*
|
||||
Rtt 头文件
|
||||
|
||||
作者:北京盛源达科技有限公司
|
||||
日期:2018/5/9
|
||||
*/
|
||||
#ifndef __H_DEBUG_LOG_H__
|
||||
#define __H_DEBUG_LOG_H__
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#ifdef _SYD_RTT_DEBUG_
|
||||
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
extern "C"{
|
||||
#endif
|
||||
#endif /* __cplusplus */
|
||||
|
||||
|
||||
//#include "TypeDef.h"
|
||||
//#include "PlatformConfig.h"
|
||||
#if defined(_SYD_RTT_DEBUG_)
|
||||
#define PLATFORM_DEBUG (1)
|
||||
#endif
|
||||
|
||||
|
||||
/* debug log总开关 */
|
||||
#if (defined(PLATFORM_DEBUG) && (1==PLATFORM_DEBUG))
|
||||
#define DEBUG_ENABLE (1)
|
||||
#else
|
||||
#define DEBUG_ENABLE (0)
|
||||
#endif
|
||||
|
||||
|
||||
/* debug log输出方式类型定义 */
|
||||
#define DEBUG_LOG_TYPE_NULL (0)
|
||||
#define DEBUG_LOG_TYPE_UART (1) // 通过uart输出debug log
|
||||
#define DEBUG_LOG_TYPE_RTT (2) // 通过segger rtt打印debug log
|
||||
#define DEBUG_LOG_TYPE_SWO (3) // 通过swo接口打印debug log
|
||||
/* 定义debug log的打印方式 */
|
||||
#define DEBUG_LOG_TYPE (DEBUG_LOG_TYPE_RTT)
|
||||
|
||||
|
||||
/* 定义debug log分类掩码 */
|
||||
#define DEBUG_LOG_MASK_ALL ((uint32_t)0xFFFFFFFF)
|
||||
#define DEBUG_LOG_MASK_NONE ((uint32_t)0x00000000)
|
||||
#define DEBUG_LOG_MASK_ERROR (((uint32_t)1)<<31) // 错误log掩码
|
||||
#define DEBUG_LOG_MASK_WARNING (((uint32_t)1)<<30) // 警告log掩码
|
||||
#define DEBUG_LOG_MASK_NOTE (((uint32_t)1)<<29) // 提醒log掩码
|
||||
#define DEBUG_LOG_MASK_TRACK_STAMP (((uint32_t)1)<<28) // 足迹标记log掩码
|
||||
#define DEBUG_LOG_MASK_ERROR_WARNING (DEBUG_LOG_MASK_ERROR | DEBUG_LOG_MASK_WARNING)
|
||||
#define DEBUG_LOG_MASK_ERROR_NOTE (DEBUG_LOG_MASK_ERROR | DEBUG_LOG_MASK_NOTE)
|
||||
#define DEBUG_LOG_MASK_ERROR_WARNING_NOTE (DEBUG_LOG_MASK_ERROR | DEBUG_LOG_MASK_WARNING | DEBUG_LOG_MASK_NOTE)
|
||||
#define DEBUG_LOG_MASK_WARNING_NOTE (DEBUG_LOG_MASK_WARNING | DEBUG_LOG_MASK_NOTE)
|
||||
|
||||
/* 定义默认debug log输出分类掩码 */
|
||||
#ifndef DEBUG_LOG_MASK_DEFAULT
|
||||
// #define DEBUG_LOG_MASK_DEFAULT (DEBUG_LOG_MASK_NONE)
|
||||
#define DEBUG_LOG_MASK_DEFAULT (DEBUG_LOG_MASK_ERROR_WARNING_NOTE)
|
||||
#endif /* DEBUG_LOG_MASK_DEFAULT */
|
||||
|
||||
|
||||
#if (defined(DEBUG_ENABLE) && (1==DEBUG_ENABLE))
|
||||
#if (DEBUG_LOG_TYPE == DEBUG_LOG_TYPE_UART)
|
||||
/* 通过uart打印debug log */
|
||||
#error "Do Not Support Debug Log Use Uart."
|
||||
// #define CONFIG_UART_ENABLE
|
||||
// #include "debug.h"
|
||||
// #define QPRINTF(...) dbg_printf_sel(0, __VA_ARGS__)
|
||||
#elif (DEBUG_LOG_TYPE == DEBUG_LOG_TYPE_RTT)
|
||||
/* 通过segger rtt打印debug log */
|
||||
#include "SEGGER_RTT.h"
|
||||
#include "SEGGER_RTT_Conf.h"
|
||||
#define DebugLogInit() SEGGER_RTT_Init()
|
||||
#define QPRINTF(...) SEGGER_RTT_printf(0, __VA_ARGS__)
|
||||
#define Qhexdump(title,buf, sz) SEGGER_RTT_hexdump(0,title,buf,sz)
|
||||
#elif (DEBUG_LOG_TYPE == DEBUG_LOG_TYPE_SWO)
|
||||
/* 通过swo接口打印debug log */
|
||||
#error "Do Not Support Debug Log Use SWO."
|
||||
#else
|
||||
#define QPRINTF(...)
|
||||
#endif
|
||||
|
||||
#define LogMask(mask, ...) \
|
||||
do{ \
|
||||
if(mask & DEBUG_LOG_MASK_DEFAULT) \
|
||||
{ \
|
||||
QPRINTF(__VA_ARGS__); \
|
||||
} \
|
||||
}while(0)
|
||||
|
||||
#define LogError(...) \
|
||||
do{ \
|
||||
if(DEBUG_LOG_MASK_ERROR & DEBUG_LOG_MASK_DEFAULT) \
|
||||
{ \
|
||||
QPRINTF("Error:%s:L%d:", __FUNCTION__, __LINE__); \
|
||||
QPRINTF(__VA_ARGS__); \
|
||||
} \
|
||||
}while(0)
|
||||
|
||||
#define LogWarning(...) \
|
||||
do{ \
|
||||
if(DEBUG_LOG_MASK_WARNING & DEBUG_LOG_MASK_DEFAULT) \
|
||||
{ \
|
||||
QPRINTF("Warning:%s:L%d:", __FUNCTION__, __LINE__); \
|
||||
QPRINTF(__VA_ARGS__); \
|
||||
} \
|
||||
}while(0)
|
||||
|
||||
#define LogNote(...) \
|
||||
do{ \
|
||||
if(DEBUG_LOG_MASK_NOTE & DEBUG_LOG_MASK_DEFAULT) \
|
||||
{ \
|
||||
QPRINTF("Note:"); \
|
||||
QPRINTF(__VA_ARGS__); \
|
||||
} \
|
||||
}while(0)
|
||||
|
||||
#define LogLevel(level, ...) \
|
||||
do{ \
|
||||
if(level > 31) \
|
||||
{ \
|
||||
LogError("Debug Log Level Error!!!\n"); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
LogMask(((uint32_t)1)<<level, "[%d]:", level); \
|
||||
LogMask(((uint32_t)1)<<level, __VA_ARGS__); \
|
||||
} \
|
||||
}while(0)
|
||||
|
||||
/* 足迹标记Log */
|
||||
#define LogTrackStamp() \
|
||||
do{ \
|
||||
if(DEBUG_LOG_MASK_TRACK_STAMP & DEBUG_LOG_MASK_DEFAULT) \
|
||||
{ \
|
||||
QPRINTF("Track:%s : %s : L%d\n", __FILE__, __FUNCTION__, __LINE__); \
|
||||
} \
|
||||
}while(0)
|
||||
|
||||
/* 兼容SYD8821 SDK中定义的debug */
|
||||
#undef DBGPRINTF
|
||||
#define DBGPRINTF(...) \
|
||||
do{ \
|
||||
QPRINTF(__VA_ARGS__); \
|
||||
}while(0)
|
||||
|
||||
#undef DBG
|
||||
#define DBG(...) \
|
||||
do{ \
|
||||
QPRINTF(__VA_ARGS__); \
|
||||
}while(0)
|
||||
#undef dbg_printf
|
||||
#define dbg_printf(...) \
|
||||
do{ \
|
||||
QPRINTF(__VA_ARGS__); \
|
||||
}while(0)
|
||||
#undef DBGHEXDUMP
|
||||
#define DBGHEXDUMP(title,buf, sz) \
|
||||
do{ \
|
||||
Qhexdump(title, buf,sz); \
|
||||
}while(0)
|
||||
|
||||
#else
|
||||
/* DEBUG_ENABLE关闭 */
|
||||
#define DebugLogInit()
|
||||
#define QPRINTF(...)
|
||||
#define LogMask(mask, ...)
|
||||
#define LogError(...)
|
||||
#define LogWarning(...)
|
||||
#define LogNote(...)
|
||||
#define LogLevel(level, ...)
|
||||
#define LogTrackStamp()
|
||||
|
||||
/* 兼容SYD8821 SDK中定义的debug */
|
||||
#undef DBGPRINTF
|
||||
#define DBGPRINTF(...)
|
||||
|
||||
#undef DBG
|
||||
#define DBG(...)
|
||||
|
||||
#endif /* #if (defined(DEBUG_ENABLE) && (1==DEBUG_ENABLE)) */
|
||||
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* __cplusplus */
|
||||
|
||||
|
||||
|
||||
#endif /* _SYD_RTT_DEBUG_ */
|
||||
#endif /* ___DEBUG_LOG_H___ */
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue