251 lines
7.2 KiB
C
251 lines
7.2 KiB
C
#if 1
|
|
#include <stdio.h>
|
|
#include "ql_rtos.h"
|
|
#include "ql_application.h"
|
|
#include "ql_audio.h"
|
|
#include "ql_iic.h"
|
|
#include "ql_gpio.h"
|
|
#include "app_inc.h"
|
|
#include "ql_fs.h"
|
|
|
|
extern int fota_start;
|
|
extern int audio_packet_type;
|
|
extern volatile int tts_playing;
|
|
extern int g_testmode;
|
|
|
|
static ql_sem_t nfc_irq_sem = NULL;
|
|
#define DP1312EA_PIN_IRQ GPIO_PIN_NO_85
|
|
|
|
// NFC 任务状态
|
|
typedef enum {
|
|
NFC_STATE_LPCD_WAIT, // 低功耗等待中断
|
|
NFC_STATE_POLLING, // 500ms 轮询读卡中
|
|
} nfc_state_t;
|
|
|
|
static volatile nfc_state_t g_nfc_state = NFC_STATE_LPCD_WAIT;
|
|
static volatile int g_poll_fail_count = 0;
|
|
static volatile int g_nfc_irq_trigger = 0;
|
|
|
|
static void nfc_irq_wake(void *args)
|
|
{
|
|
unsigned char level = 0;
|
|
|
|
ql_eint_disable_wakeup(DP1312EA_PIN_IRQ);
|
|
ql_gpio_get_level(DP1312EA_PIN_IRQ, &level);
|
|
printf("DP1312EA_PIN_IRQ level:%d\n", level);
|
|
|
|
ql_eint_enable_wakeup(DP1312EA_PIN_IRQ, PIN_RISING_EDGE);
|
|
|
|
if(g_nfc_irq_trigger==0){
|
|
g_nfc_irq_trigger = 1;
|
|
ql_rtos_semaphore_release(nfc_irq_sem);
|
|
}
|
|
}
|
|
|
|
|
|
void ql_nfc_task_thread(void *arvg)
|
|
{
|
|
s8 read_status;
|
|
|
|
while(1)
|
|
{
|
|
// ========== 状态1: 低功耗等待中断 ==========
|
|
if (g_nfc_state == NFC_STATE_LPCD_WAIT) {
|
|
|
|
DP1312EA_LPCD_Start(); // 进入低功耗检卡,等待中断
|
|
|
|
printf("NFC enter LPCD mode, waiting for IRQ...\r\n");
|
|
|
|
// 阻塞等待中断唤醒
|
|
ql_rtos_semaphore_wait(nfc_irq_sem, 0xFFFFFFFF);
|
|
|
|
DP1312EA_CLEAR_IRQ();
|
|
ql_rtos_task_sleep_ms(10); // 防抖延迟
|
|
|
|
printf("NFC IRQ wake up, switch to polling mode\r\n");
|
|
|
|
// 切换到轮询模式,重置失败计数
|
|
g_nfc_state = NFC_STATE_POLLING;
|
|
g_poll_fail_count = 0;
|
|
|
|
// 立即执行第一次读卡(不等待200ms)
|
|
continue;
|
|
}
|
|
|
|
// ========== 状态2: 200ms 轮询读卡 ==========
|
|
if (g_nfc_state == NFC_STATE_POLLING) {
|
|
|
|
// 检查是否允许读卡(业务条件)
|
|
int can_read = 0;
|
|
|
|
if (fota_start == 0 &&
|
|
get_sleep_mode() != 1 &&
|
|
get_mqtt_connect_status() == 1) {
|
|
|
|
if ((get_mqtt_connect_status() == 1 &&
|
|
vad_audio_is_recording() != 1 &&
|
|
audio_packet_type == 0 &&
|
|
tts_playing == 0 &&
|
|
xiaoice_get_play_counter() == 0) ||
|
|
g_testmode == 1) {
|
|
|
|
can_read = 1;
|
|
}
|
|
}
|
|
|
|
if (!can_read) {
|
|
// 当前业务状态不允许读卡,继续等待下次轮询
|
|
// printf("NFC business condition not met, keep polling\r\n");
|
|
ql_rtos_task_sleep_ms(300);
|
|
continue;
|
|
}
|
|
|
|
// 执行读卡
|
|
// printf("NFC polling read, fail_count=%d\r\n", g_poll_fail_count);
|
|
read_status = Test_NTAG213();
|
|
|
|
if (read_status == 0) {
|
|
// ===== 读卡成功 =====
|
|
printf("NFC read OK, continue polling\r\n");
|
|
|
|
// 直接回到 LPCD,不再轮询
|
|
g_nfc_state = NFC_STATE_LPCD_WAIT;
|
|
g_poll_fail_count = 0;
|
|
g_nfc_irq_trigger = 0;
|
|
|
|
ql_rtos_task_sleep_ms(10);
|
|
continue; // 回到 LPCD 等待
|
|
} else {
|
|
// ===== 读卡失败 =====
|
|
g_poll_fail_count++;
|
|
printf("NFC read failed, fail_count=%d\r\n", g_poll_fail_count);
|
|
|
|
if (g_poll_fail_count >= 3) {
|
|
// 连续失败 3 次,认为卡已离开
|
|
printf("NFC fail 3 times, card removed, back to LPCD\r\n");
|
|
|
|
// 清理状态,回到低功耗
|
|
g_nfc_state = NFC_STATE_LPCD_WAIT;
|
|
g_poll_fail_count = 0;
|
|
g_nfc_irq_trigger = 0;
|
|
// 短暂延迟确保硬件稳定
|
|
ql_rtos_task_sleep_ms(10);
|
|
|
|
} else {
|
|
// 继续轮询,可能卡只是位置偏移
|
|
ql_rtos_task_sleep_ms(300);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void DP1312EA_Init(void)
|
|
{
|
|
// 创建信号量
|
|
ql_rtos_semaphore_create(&nfc_irq_sem, 0);
|
|
|
|
// 配置中断引脚
|
|
ql_gpio_init(DP1312EA_PIN_IRQ, PIN_DIRECTION_IN, PIN_PULL_PD, PIN_LEVEL_LOW);
|
|
ql_eint_register(DP1312EA_PIN_IRQ, PIN_RISING_EDGE, PIN_PULL_PD, NULL, nfc_irq_wake);
|
|
ql_eint_enable_wakeup(DP1312EA_PIN_IRQ, PIN_RISING_EDGE);
|
|
|
|
// I2C 和芯片初始化
|
|
DP1312EA_Reset();
|
|
Check_Chip();
|
|
|
|
// 初始状态为低功耗等待
|
|
g_nfc_state = NFC_STATE_LPCD_WAIT;
|
|
g_poll_fail_count = 0;
|
|
|
|
// 创建任务
|
|
ql_task_t nfcTaskRef = NULL;
|
|
ql_rtos_task_create(&nfcTaskRef, 1024*6, 100, "nfc_Task_Ref", ql_nfc_task_thread, NULL);
|
|
}
|
|
#endif
|
|
|
|
#if 0
|
|
#include <stdio.h>
|
|
#include "ql_rtos.h"
|
|
#include "ql_application.h"
|
|
#include "ql_audio.h"
|
|
#include "ql_iic.h"
|
|
#include "ql_gpio.h"
|
|
#include "app_inc.h"
|
|
#include "ql_fs.h"
|
|
#include "ql_rtos.h"
|
|
|
|
extern int fota_start;
|
|
|
|
static ql_sem_t nfc_irq_sem = NULL;
|
|
|
|
extern int audio_packet_type;
|
|
extern volatile int tts_playing;
|
|
extern int g_testmode;
|
|
|
|
#define DP1312EA_PIN_IRQ GPIO_PIN_NO_87
|
|
|
|
static void nfc_irq_wake(void *args)
|
|
{
|
|
unsigned char level = 0;
|
|
|
|
ql_eint_disable_wakeup(DP1312EA_PIN_IRQ);
|
|
|
|
ql_gpio_get_level(DP1312EA_PIN_IRQ, &level);
|
|
printf("DP1312EA_PIN_IRQ level:%d\n", level);
|
|
|
|
// ql_eint_enable_wakeup(DP1312EA_PIN_IRQ, PIN_RISING_EDGE);
|
|
|
|
ql_rtos_semaphore_release(nfc_irq_sem);
|
|
}
|
|
|
|
|
|
void ql_nfc_task_thread(void *arvg)
|
|
{
|
|
while(1)
|
|
{
|
|
#if 0
|
|
DP1312EA_LPCD_Start();//进入低功耗检卡,并等待中断,开启之后打开对应读卡函数(模拟卡除外)
|
|
|
|
ql_rtos_semaphore_wait(nfc_irq_sem, 0xFFFFFFFF);
|
|
|
|
DP1312EA_CLEAR_IRQ();
|
|
|
|
ql_rtos_task_sleep_ms(30);
|
|
#endif
|
|
|
|
if(fota_start==0&&get_sleep_mode()!=1&&get_mqtt_connect_status()==1){
|
|
//在连上mqtt和后台 且非录音和播音状态下可以支持刷nfc
|
|
if(get_xiaoice_connected_status()==2&&vad_audio_is_recording()!=1 \
|
|
&&audio_packet_type==0&&tts_playing==0&&xiaoice_get_play_counter()==0 \
|
|
|| g_testmode==1 ){
|
|
Test_NTAG213();
|
|
}
|
|
}
|
|
|
|
ql_rtos_task_sleep_ms(2000);
|
|
//ql_eint_enable_wakeup(DP1312EA_PIN_IRQ, PIN_RISING_EDGE);
|
|
}
|
|
}
|
|
|
|
|
|
void DP1312EA_Init(void)
|
|
{
|
|
#if 0
|
|
ql_gpio_init(DP1312EA_PIN_IRQ,PIN_DIRECTION_IN,PIN_PULL_PD,PIN_LEVEL_LOW);
|
|
ql_eint_register(DP1312EA_PIN_IRQ, PIN_RISING_EDGE, PIN_PULL_PD, NULL, nfc_irq_wake);
|
|
ql_eint_enable_wakeup(DP1312EA_PIN_IRQ, PIN_RISING_EDGE);
|
|
#endif
|
|
//I2C初始化
|
|
DP1312EA_Reset();
|
|
Check_Chip();
|
|
|
|
ql_rtos_semaphore_create(&nfc_irq_sem, 0);
|
|
|
|
ql_task_t nfcTaskRef = NULL;
|
|
ql_rtos_task_create(&nfcTaskRef, 1024*6, 100, "nfc_Task_Ref", ql_nfc_task_thread, NULL);
|
|
}
|
|
|
|
#endif
|