EC600U_esp32_iap_uart/EC600U_lora/lora_app.c
2024-02-05 17:39:56 +08:00

144 lines
3.8 KiB
C

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include "ql_api_osi.h"
#include "ql_api_spi.h"
//#include "ql_gpio.h"
#include "ql_log.h"
#include "./inc/lora.h"
#include "./inc/user.h"
#include "lora_app.h"
/*宏定义*/
#define LOGI(msg, ...) QL_LOG(QL_LOG_LEVEL_INFO, "lora_app", msg, ##__VA_ARGS__)
#define USE_TEST 1
/*全局变量*/
int flag;
ql_task_t lora_app_task;
extern unsigned char *write_buf;
extern unsigned char *read_buf;
int count = 1;
/*函数*/
#if USE_TEST
void lora_send_test(void)
{
//uint16_t syncword = lora_get_syncword(); //00011101 00000111 01000000
//LOGI("syncword = 0x%04X\n",syncword);
//lora_set_standby(0x00);
LOGI("lora_send_test start");
if(wait_busy_low()){
write_buf[0] = 0x1D; //00011101
write_buf[1] = 0x07; //00000111
write_buf[2] = 0x40; //01000000
ql_errcode_spi_e spi_err = ql_spi_write_read(QL_SPI_PORT1, read_buf,write_buf, 3);
LOGI("ql_spi_write_read spi_err = %d",spi_err);
LOGI("read_buf 0 = 0x%02X, read_buf 1 = 0x%02X\n",read_buf[0],read_buf[1]);
LOGI("read_buf 2 = 0x%02X, read_buf 3 = 0x%02X\n",read_buf[2],read_buf[3]);
LOGI("read_buf 4 = 0x%02X, read_buf 5 = 0x%02X\n",read_buf[4],read_buf[5]);
}
else{
LOGI("lora_send_test wait_busy_low outtime");
}
LOGI("lora_send_test end");
}
#endif
void lora_receive_mode(void)
{
/*获取中断状态*/
uint16_t irq_status = lora_get_irq_status();
/*清除中断状态*/
lora_clear_irq_status(irq_status);
/*根据中断状态进行操作*/
if((irq_status & IRQ_TX_DONE) == IRQ_TX_DONE){
}
if( ( irq_status & IRQ_RX_DONE ) == IRQ_RX_DONE ){
}
if( ( irq_status & IRQ_CAD_DONE ) == IRQ_CAD_DONE ){
}
if( ( irq_status & IRQ_RX_TX_TIMEOUT ) == IRQ_RX_TX_TIMEOUT ){
}
if( ( irq_status & IRQ_PREAMBLE_DETECTED ) == IRQ_PREAMBLE_DETECTED ){
}
if( ( irq_status & IRQ_SYNCWORD_VALID ) == IRQ_SYNCWORD_VALID ){
}
if( ( irq_status & IRQ_HEADER_VALID ) == IRQ_HEADER_VALID ){
}
if( ( irq_status & IRQ_HEADER_ERROR ) == IRQ_HEADER_ERROR ){
}
}
void lora_app_thread(void *param)
{
LOGI("lora_app_thread start");
lora_init();
#if !USE_TEST
lora_start();
lora_radioConfig();
#endif
while(1){
LOGI("enter while in %d times\n",count);
count++;
// wait_busy_low();
// write_buf[0] = 0x1D; //00011101
// write_buf[1] = ((0x0740 & 0xff00) >> 8);//00000111
// write_buf[2] = (0x0740 & 0xff);//01000000
// write_buf[3] = 0;
// write_buf[4] = 0;
// LOGI("write_buf[0] = 0x%x, write_buf[1] = 0x%02x, write_buf[2] = 0x%x, write_buf[3] = 0x%x, write_buf[4] = 0x%x",write_buf[0],write_buf[1],write_buf[2],write_buf[3],write_buf[4]);
// LOGI("ql_spi_write_read err = %d",ql_spi_write_read(QL_SPI_PORT1,read_buf,write_buf,5));
// wait_busy_low();
// LOGI("read_buf[0] = 0x%x, read_buf[1] = 0x%x, read_buf[2] = 0x%x, read_buf[3] = 0x%02x, read_buf[4] = 0x%02x",read_buf[0],read_buf[1],read_buf[2],read_buf[3],read_buf[4]);
/*唤醒*/
write_buf[0] = 0xC0; //返回设备当前状态命令
write_buf[1] = 0x00;
LOGI(" write 0xC0 0x00 err = %d",ql_spi_write_read(QL_SPI_PORT1,read_buf,write_buf,2));
LOGI(" read_buf[0] = 0x%02x, read_buf[1] = 0x%02x,",read_buf[0],read_buf[1]);
wait_busy_low();
ql_rtos_task_sleep_ms(5);
}
}
void lora_app_init(void)
{
QlOSStatus err = ql_rtos_task_create(&lora_app_task, 4096, APP_PRIORITY_NORMAL,"lora",lora_app_thread,NULL,1);
if(err != 0){
LOGI("create lora_demo_task failed\n");
}
}
/*
0001 1101
0100 0101
复位
rst推挽输出低电平10ms后改为上拉输入模式持续10ms
唤醒
nss低
传输192
传输0
nss高
等待busy引脚由高变低
*/