/*
|
* Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and
|
* its subsidiaries and affiliates (collectly called MKSEMI).
|
*
|
* 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 an MKSEMI
|
* 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 MKSEMI 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
|
* MKSEMI 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 MKSEMI "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 MKSEMI 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 "mk_trace.h"
|
#include "mk_wdt.h"
|
#include "mk_calib.h"
|
#include "mk_misc.h"
|
#include "mk_flash.h"
|
#include "libc_rom.h"
|
#include <serial_at_cmd_app.h>
|
#include <global_param.h>
|
#include "board.h"
|
#include "wsf_nvm.h"
|
#include "mk_power.h"
|
#include "Usart.h"
|
#include "mk_adc.h"
|
#include "mk_sleep_timer.h"
|
#include "lis3dh_driver.h"
|
#include "sn74hc595.h"
|
#include "mk_4G.h"
|
#include "UART.h"
|
#include "AIR780EDriver.h"
|
#include "Internet.h"
|
#include "HIDO_ATLite.h"
|
#include "HIDO_Timer.h"
|
#include "TCPClient.h"
|
#include "mk_spi.h"
|
//#include "pan_port.h"
|
#include "mk_timer.h"
|
//#include "pan_param.h"
|
//#include "pan_rf.h"
|
#include "lora_1268.h"
|
//#include "pan_rf.h"
|
#include "PCA9555.h"
|
#include "mk_phy.h"
|
#include "HIDO_Timer.h"
|
#define DEBUG_MODE
|
extern int simple_main(void);
|
extern int temp_main(void);
|
#define TEST_UART_POLL_MODE 0
|
#define TEST_UART_INTERUPT_MODE 1
|
#define TEST_UART_DMA_MODE 2
|
#define TEST_UART_MODE TEST_UART_DMA_MODE
|
|
#define NUM_SAMPLES 1
|
#define BIND_TRIGGER_TIME 60000
|
#define MOTOR_COUNT_TIME 1
|
#define WARING_LIMIT_TIME 10
|
#define UPDATE_TIME 10
|
|
/****************************************************************Òý½Åºê¶¨Òå**************************************************/
|
#define ADC_PIN IO_PIN_0
|
#define UART0_TX IO_PIN_5
|
#define UART0_RX IO_PIN_6
|
#define UART1_TX IO_PIN_1
|
#define UART1_RX IO_PIN_14
|
|
|
/****************************************************************Òý½Åºê¶¨Òå**************************************************/
|
#define UWB_OPEN_COUNT 1
|
#define UWB_MEASUREMENT_INTERVAL 3
|
#define UWB_MEASUREMENT_INTERVAL_SLEEP 60
|
|
void test_Delay_us(uint16_t time);
|
void Lora_UploadHeartBeartPoll(void);
|
extern uint8_t mUsartReceivePack[100];
|
extern uint8_t mUsart2ReceivePack[150];
|
extern uint8_t state5V_prase_flag,gps_prase_flag;
|
extern int distance;
|
extern uint8_t anchordata_num; //bat_percent
|
uint32_t dev_id;
|
uint8_t group_id;
|
uint16_t tag_frequency;
|
uint16_t disoffset;
|
uint16_t warning_distance,prewarning_distance;
|
int16_t fVoltage_mv,first_search_flag;
|
uint8_t bat_percent,g_start_send_flag=1;
|
uint8_t LORA_REV_uwb_up =0;
|
uint8_t link_success_flag,motor_count;
|
uint16_t gps_wait_count;
|
uint8_t state5v = 1;
|
uint8_t motor_power_state=1,uwb_state,air780_power_state,gps_success_state,chaging_state,changed_state,air780_success_state,first_motor_in_flag;
|
float nomove_count=0;
|
static uint32_t sample[NUM_SAMPLES] = {0};
|
uint8_t recev_error_num,send_messgae_count,send_flag,control_state;
|
uint8_t flag_4g_usart_rx_state = 1; //4G ´®¿ÚRXµ±Ç°ÊÇ RX״̬£¬²»ÊÇGPIO״̬
|
int input_5v_flag,air780e_flag1,air780e_flag2,air780e_flag3,air780e_flag4;
|
typedef enum
|
{ UN_BIND=0,
|
LINK_SUCCESS,
|
SEARCH_DEV,
|
} Operation_step;
|
Operation_step UWB_work_state;
|
|
|
Commend_SendDate send_struct;
|
static struct ADC_CFG_T usr_adc_cfg = {
|
.mode = ADC_MODE_CONTINUE, /* Selected single conversion mode */
|
.clk_sel = ADC_CLK_HIGH, /* Selected 62.4M high speed clock */
|
.vref_sel = ADC_SEL_VREF_INT, /* Using internal reference voltage (1.2V)*/
|
.rate = 500000, /* ADC works at high frequency system clock, the maximum sampling rate is 2M */
|
.channel_p = ADC_IN_EXTPIN0, /* ADC positive channel --> GPIO0 */
|
.channel_n = ADC_IN_VREF, /* ADC negative channel --> Vref */
|
.int_en = false,
|
.dma_en = false, /* DMA support only in continue mode */
|
.acc_num = 0,
|
.high_pulse_time = 4,
|
.settle_time = 1,
|
};
|
struct UART_CFG_T test_uart_cfg =
|
{
|
.parity = UART_PARITY_NONE,
|
.stop = UART_STOP_BITS_1,
|
.data = UART_DATA_BITS_8,
|
.flow = UART_FLOW_CONTROL_NONE,
|
.rx_level = UART_RXFIFO_CHAR_1,
|
.tx_level = UART_TXFIFO_EMPTY,
|
.baud = BAUD_115200,
|
#if (TEST_UART_MODE == TEST_UART_POLL_MODE)
|
.dma_en = false,
|
.int_rx = false,
|
.int_tx = false,
|
#elif (TEST_UART_MODE == TEST_UART_INTERUPT_MODE)
|
.dma_en = false,
|
.int_rx = true,
|
.int_tx = true,
|
#elif (TEST_UART_MODE == TEST_UART_DMA_MODE)
|
.dma_en = true,
|
.int_rx = false,
|
.int_tx = false,
|
#endif
|
};
|
void uart_receive_callback(void *dev, uint32_t err_code)
|
{
|
uart_receive(UART_ID1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE,uart_receive_callback);
|
}
|
|
static void adc_callback(void *data, uint32_t number)
|
{
|
uint32_t *result = (uint32_t *)data;
|
for (uint16_t i = 0; i < number; i++)
|
{
|
// LOG_INFO(TRACE_MODULE_APP, "The voltage measured %d mv\r\n",
|
// ADC_INTERNAL_VREF_MV + adc_code_to_mv((int16_t)*result, ADC_INTERNAL_VREF_MV));
|
fVoltage_mv=ADC_INTERNAL_VREF_MV + adc_code_to_mv((int16_t)*result, ADC_INTERNAL_VREF_MV);
|
fVoltage_mv=fVoltage_mv*2;
|
if(fVoltage_mv < 3300)
|
{
|
bat_percent = 0;
|
}
|
else if(fVoltage_mv > 4100)
|
{
|
bat_percent = 100;
|
}
|
else
|
{
|
bat_percent = ((fVoltage_mv - 3300) /8);
|
}
|
}
|
// LOG_INFO(TRACE_MODULE_APP, "The voltage is %%%d \r\n",bat_percent);
|
}
|
//static void timer_callback(void *dev, uint32_t time)
|
//{
|
// enum TIMER_DEV_T id = *(enum TIMER_DEV_T *)dev;
|
// LOG_INFO(TRACE_MODULE_APP, "Timer %d elapsed time %d\r\n", id, time);
|
//}
|
//void UartInit(void)
|
//{
|
//board_pins_config();
|
//uart_open(UART_ID1, &test_uart_cfg);
|
//board_debug_console_open(TRACE_PORT_UART0);
|
//uart_receive(UART_ID1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE,uart_receive_callback);
|
//}
|
extern uint8_t gps_uwb_flag;
|
uint8_t TCP_reconnect_timer,flag_TCP_reconnectting = 1,flag_alam_state = 0;
|
uint8_t gps_enable_flag,motor_open_air_flag,moter_open_uwb_flag;
|
int need_open_gps_count;
|
void MotorPoll(void)
|
{
|
if(UWB_work_state==UN_BIND) {
|
gps_uwb_flag=0;//¹Ø±ÕGPS
|
moter_open_uwb_flag=0;//¹Ø±ÕÕð¶¯
|
need_open_gps_count=0;//Çå0¼ÆÊýgps
|
} else { //°ó¶¨×´Ì¬ÏÂ
|
if(anchordata_num==1) {
|
if(distance<g_com_map[ALARM_DISTANCE1]) { //СÓÚÔ¤¾¯¾àÀë
|
if(flag_alam_state)
|
{
|
flag_alam_state = 0;
|
_4GAlarmUpload(2);
|
}
|
need_open_gps_count=0;
|
moter_open_uwb_flag=0;//¹Ø±ÕÕð¶¯
|
gps_uwb_flag=0;//¹Ø±ÕGPS
|
} else if(g_com_map[ALARM_DISTANCE1]<=distance&&distance<=g_com_map[ALARM_DISTANCE2]) { //´óÓÚÔ¤¾¯¾àÀëСÓÚ±¨¾¯¾àÀë
|
need_open_gps_count++;
|
moter_open_uwb_flag=1;//¿ªÆôÕð¶¯
|
if(need_open_gps_count>=20) {
|
if(!flag_alam_state)
|
{
|
flag_alam_state = 1;
|
_4GAlarmUpload(1);
|
}
|
gps_uwb_flag=1;//¿ªÆôgps²â¾àÁ÷³Ì
|
need_open_gps_count=0;
|
}
|
} else if(distance>=g_com_map[ALARM_DISTANCE2]) { //´óÓÚ±¨¾¯¾àÀë
|
need_open_gps_count++;
|
moter_open_uwb_flag=1;//¿ªÆôÕð¶¯
|
if(need_open_gps_count>=20) {
|
if(!flag_alam_state)
|
{
|
flag_alam_state = 1;
|
_4GAlarmUpload(1);
|
}
|
gps_uwb_flag=1;//¿ªÆôgps²â¾àÁ÷³Ì
|
need_open_gps_count=0;
|
}
|
}
|
} else if(anchordata_num==0) {
|
if(!flag_alam_state)
|
{
|
flag_alam_state = 1;
|
_4GAlarmUpload(1);
|
}
|
distance = -1;
|
gps_uwb_flag=1;//¿ªÆôgps²â¾àÁ÷³Ì
|
moter_open_uwb_flag=1;//¿ªÆôÕð¶¯
|
need_open_gps_count=0;
|
}
|
}
|
}
|
void MinuteTask(void)
|
{
|
adc_get(&sample[0], NUM_SAMPLES, adc_callback);//adc²ÉÑù
|
}
|
void SecondTask(void)
|
{static uint8_t second_count;
|
if(second_count++>60)
|
{
|
second_count = 0;
|
MinuteTask();
|
}
|
//UWB¸üÐÂÁбí
|
// TagListUpdate();
|
//GPS_Poll();
|
gps_power_state=1;//²âÊÔgps³¤¿ª
|
//UWB״̬¼ì²â
|
|
if(IfTCPConnected())
|
{
|
TCP_reconnect_timer =0;
|
flag_TCP_reconnectting = 0;
|
} else {
|
if(TCP_reconnect_timer<30)//Èç¹ûTCPûÓÐÁ¬½Ó£¬Ã¿¸ô10·ÖÖÓ³¢ÊÔÁ¬½Ó30Ãë
|
{
|
flag_TCP_reconnectting = 1;
|
} else {
|
flag_TCP_reconnectting = 0;
|
}
|
if(TCP_reconnect_timer++>600)
|
{
|
TCP_reconnect_timer = 0;
|
}
|
|
}
|
// HIDO_TimerTick();
|
|
}
|
// ¶¨Òå״̬
|
typedef enum {
|
STATE_NORMAL, // Õý³£×´Ì¬£¬3Ãë²â¾àÉÏ´«Ò»´Î
|
STATE_SLEEP // ÐÝÃß״̬£¬1·ÖÖÓ²â¾àÉÏ´«Ò»´Î
|
} CountState_t;
|
// È«¾Ö±äÁ¿
|
uint32_t uwb_time_count = 0; // ¶¨Ê±Æ÷¼ÆÊýÖµ
|
uint32_t step_count = 0; // ²½Êý¼ÆÊý
|
uint32_t last_step_count = 0; // ÉÏÒ»´Î²½Êý
|
CountState_t current_state = STATE_NORMAL; // µ±Ç°×´Ì¬
|
uint32_t state_start_time = 0; // ״̬¿ªÊ¼Ê±¼ä
|
|
uint8_t stationary_flag;
|
extern uint8_t lora_tx_flag;
|
extern uint16_t sleep_time;
|
static void sleep_timer_callback(void *dev, uint32_t time)
|
{
|
|
// g_start_send_flag=1;
|
HIDO_TimerTick();
|
// lora_tx_flag++;
|
uwb_time_count++;
|
if(uwb_time_count>10000)
|
{
|
uwb_time_count=0;
|
}
|
if (current_state == STATE_NORMAL)
|
{
|
sleep_time++;
|
}
|
step_count = mir3da_get_step(); // »ñÈ¡²½Êý
|
// if(yundong_bushu<mir3da_get_step())
|
// {
|
// yundong_bushu=mir3da_get_step();
|
// onemin_onesecond_flag=1;
|
// }
|
// else
|
// {
|
// yundong_bushu=mir3da_get_step();
|
// onemin_onesecond_flag=0;
|
// }
|
//Âí´ïÕð¶¯Âß¼
|
if(g_com_map[MOTOR_ENABLE])
|
{
|
if(motor_open_air_flag||moter_open_uwb_flag)
|
{
|
if (motor_count++%2==0)
|
{
|
motor_power_state = 1;
|
}else{
|
motor_power_state = 0;
|
}
|
} else {
|
motor_power_state=1;
|
}
|
}else{
|
motor_power_state=1;
|
}
|
nomove_count++;
|
|
|
}
|
uint16_t sleep_time=0;
|
void check_step_and_update_state(void)
|
{
|
if ((step_count != last_step_count))
|
{
|
// ²½ÊýÓб仯£¬ÖØÖÃ״̬ΪÕý³£×´Ì¬
|
if(current_state == STATE_SLEEP)
|
{
|
state_start_time = uwb_time_count;
|
}
|
current_state = STATE_NORMAL;
|
last_step_count = step_count;
|
}
|
else
|
{
|
// ²½ÊýûÓб仯£¬¼ì²éÊÇ·ñÐèÒªÇл»µ½ÐÝÃß״̬
|
if (current_state == STATE_NORMAL)
|
{
|
// sleep_time = uwb_time_count - state_start_time;
|
if (sleep_time >= 30)
|
{ // 30ÃëºóÇл»µ½ÐÝÃß״̬
|
current_state = STATE_SLEEP;
|
sleep_time = 0;
|
}
|
}
|
}
|
}
|
//void phy_timer_callback(uint32_t time)
|
//{
|
// lora_tx_flag++;
|
//}
|
|
void uart0_receive_callback()
|
{
|
uart_receive(UART_ID0,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE,uart0_receive_callback);
|
}
|
|
|
static void voltage_input_handler(enum IO_PIN_T pin)
|
{
|
|
}
|
|
uint8_t flag_4G_recdata;
|
static void _4gUsart_handler(enum IO_PIN_T pin)
|
{
|
LOG_INFO(TRACE_MODULE_APP, "4G RX »½ÐÑ\r\n");
|
// flag_4G_recdata = 1;
|
// AIR780EUartInit();
|
}
|
extern uint16_t ip0,ip1,ip2,ip3,port;
|
extern uint8_t gps_4g_flag;
|
void Program_Init(void)
|
{
|
Usart1ParseDataCallback = UsartParseDataHandler;//Ðè¸ÄΪĬÈÏΪgps´¦Àí£¬UsartParseDataHandlerΪÉý¼¶´¦Àíµ±µ÷ÊÔʱºò¸ÄΪ
|
parameter_init_anchor();//g_com_map±í³õʼ»¯½ÇɫĬÈÏΪ»ùÕ¾
|
g_com_map[DEV_ID]=0x1234;
|
g_com_map[STATIONARY_TIME] = 10;
|
// g_com_map[GROUP_ID]=1;
|
dev_id=g_com_map[DEV_ID];//ÕâÀﲻ̫¶Ô
|
// group_id=g_com_map[GROUP_ID];//×éID
|
group_id=0x04;
|
tag_frequency = 1000/g_com_map[COM_INTERVAL];//²â¾àƵÂÊÕâ¸ö´æµÄÊDzâ¾àʱ¼ä
|
memcpy(&disoffset,&g_com_map[DIST_OFFSET],2);
|
// g_com_map[ALARM_DISTANCE1] = 40;
|
// g_com_map[ALARM_DISTANCE2] = 40;
|
warning_distance=g_com_map[ALARM_DISTANCE1];
|
prewarning_distance=g_com_map[ALARM_DISTANCE2];
|
send_struct.warnDistence=warning_distance;
|
send_struct.alarmDistence=prewarning_distance;//¸üб¨¾¯¾àÀë
|
memcpy(&send_struct.gunLableId,&g_com_map[BIND_DEV_ID],2);//¸üаó¶¨ID
|
send_struct.tagId=dev_id;//¸üÐÂÉ豸ID
|
|
if(g_com_map[BIND_DEV_ID]==0)
|
{
|
UWB_work_state = UN_BIND;
|
}else{
|
UWB_work_state = SEARCH_DEV;
|
}
|
if(g_com_map[SEND_4G_SECOND]<60)
|
g_com_map[SEND_4G_SECOND]=60;
|
g_com_map[MODBUS_MODE] = 0;
|
ip0 = (g_com_map[TCP_IP_0]>>12&0xf)*1000+(g_com_map[TCP_IP_0]>>8&0xf)*100+(g_com_map[TCP_IP_0]>>4&0xf)*10+(g_com_map[TCP_IP_0]&0xf);
|
ip1 = (g_com_map[TCP_IP_1]>>12&0xf)*1000+(g_com_map[TCP_IP_1]>>8&0xf)*100+(g_com_map[TCP_IP_1]>>4&0xf)*10+(g_com_map[TCP_IP_1]&0xf);
|
ip2 = (g_com_map[TCP_IP_2]>>12&0xf)*1000+(g_com_map[TCP_IP_2]>>8&0xf)*100+(g_com_map[TCP_IP_2]>>4&0xf)*10+(g_com_map[TCP_IP_2]&0xf);
|
ip3 = (g_com_map[TCP_IP_3]>>12&0xf)*1000+(g_com_map[TCP_IP_3]>>8&0xf)*100+(g_com_map[TCP_IP_3]>>4&0xf)*10+(g_com_map[TCP_IP_3]&0xf);
|
port = g_com_map[TCP_PORT];
|
g_com_map[VERSION] = (1<<8)|11;
|
LOG_INFO(TRACE_MODULE_APP,"É豸ID: %x .\r\n",dev_id);
|
LOG_INFO(TRACE_MODULE_APP,"¹Ì¼þ°æ±¾:BGK-mk+À¶ÑÀV%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff);
|
LOG_INFO(TRACE_MODULE_APP,"·þÎñÆ÷µØÖ·: %d.%d.%d.%d:%d.\r\n",ip0,ip1,ip2,ip3,port);
|
}
|
|
|
|
void IdleTask(void)
|
{
|
UART0_CheckReceive();
|
}
|
|
int bind_check(void)
|
{
|
if(g_com_map[BIND_DEV_ID]!=0x00)
|
{
|
return 1;
|
//°ó¶¨ÐÅϢΪ1˵Ã÷É豸ÒѾ±»Ï·¢°ó¶¨
|
} else {
|
return 0;
|
//°ó¶¨ÐÅϢΪ0˵Ã÷É豸δ±»°ó¶¨
|
}
|
}
|
void check_if_in_search(void)
|
{
|
if(link_success_flag==1) { //Èç¹ûµÚÒ»´Î²â¾àÊÕ°ü³É¹¦ÄÇôÔò½øÈëÁ¬½Ó²â¾àģʽ
|
UWB_work_state=LINK_SUCCESS;
|
} else { //·ñÔò½øÈëËÑË÷ģʽ
|
UWB_work_state=SEARCH_DEV;
|
if(first_search_flag==0)
|
first_search_flag=1;
|
}
|
}
|
void boot_deinit(void)
|
{
|
//½«bootÖд®¿Ú·µ»ØÆÕͨgpio
|
// UART0 TX/RX
|
io_pin_mux_set(IO_PIN_5, IO_FUNC0);
|
io_pin_mux_set(IO_PIN_6, IO_FUNC0);
|
// UART1 RX/TX
|
io_pin_mux_set(IO_PIN_10, IO_FUNC0);
|
io_pin_mux_set(IO_PIN_9, IO_FUNC0);
|
uart_close(UART_ID1);//½â°óÔÀ´´®¿Ú1
|
uart_close(UART_ID0);//½â°óÔÀ´´®¿Ú0
|
}
|
void test_Delay_us(uint16_t time)
|
{
|
for(int i=time;i>0;i--)
|
{
|
for(int j=1;j>0;j--)
|
{
|
__NOP();
|
}
|
}
|
|
}
|
void mcu_deep_sleep(void)
|
{
|
uint32_t lock;
|
trace_flush();
|
lock = int_lock();
|
LOG_INFO(TRACE_MODULE_APP, "½øÈëÐÝÃß\r\n");
|
//¹Ø±ÕÀ¶ÑÀ µÆ
|
LORA_LED_OFF;
|
GREEN_LED_OFF;
|
UWB_LED_OFF;
|
RED_LED_OFF;
|
BT_LED_OFF;
|
PCA9555_Set_One_Value_Output(BT_EN,0);
|
sleep_timer_stop();
|
power_enter_power_down_mode(1);
|
sys_reset(0);
|
int_unlock(lock);
|
}
|
void IMUTask(void)
|
{
|
if(nomove_count>10&&g_com_map[IMU_ENABLE]==2)
|
{
|
power_wakeup_enable((enum POWER_WAKEUP_SOURCE_T)PCA_INPUT_DETECT, POWER_WAKEUP_LEVEL_LOW);
|
mcu_deep_sleep();
|
|
}
|
}
|
#define TEST_SPI_MASTER 0
|
#define TEST_SPI_POLL_MODE 0
|
#define TEST_SPI_INTERUPT_MODE 1
|
#define TEST_SPI_DMA_MODE 2
|
#define TEST_SPI_MODE TEST_SPI_POLL_MODE
|
|
void Board_gpio_init()
|
{
|
//SPI0/MOSI/MISO/CLK/CS
|
io_pin_mux_set(LORA_CS, IO_FUNC0);
|
gpio_pin_set_dir(LORA_CS , GPIO_DIR_OUT, 0);
|
io_pull_set(LORA_CS, IO_HIGH_Z, IO_PULL_UP_NONE);
|
|
io_pin_mux_set(LORA_MOSI, IO_FUNC2);
|
io_pin_mux_set(LORA_MISO, IO_FUNC2);
|
io_pin_mux_set(LORA_CLK, IO_FUNC2);
|
io_pin_mux_set(LORA_DIO, IO_FUNC0);
|
|
|
// //ADC
|
// io_pin_mux_set(ADC_PIN, IO_FUNC1);
|
// io_pull_set(ADC_PIN, IO_HIGH_Z, IO_PULL_UP_NONE);
|
|
// //´®¿Ú0
|
// io_pin_mux_set(UART0_RX, IO_FUNC4);
|
// io_pin_mux_set(UART0_TX, IO_FUNC4);
|
|
io_pin_mux_set(UART0_RX, IO_FUNC0);
|
gpio_pin_set_dir(UART0_RX , GPIO_DIR_OUT, 0);
|
io_pin_mux_set(UART0_TX, IO_FUNC0);
|
gpio_pin_set_dir(UART0_TX , GPIO_DIR_OUT, 0);
|
// board_debug_console_open(TRACE_PORT_UART0);
|
// uart_receive(UART_ID0,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE,uart0_receive_callback);//¿ªÆôdma
|
|
//´®¿Ú1
|
// io_pin_mux_set(UART1_RX, IO_FUNC4);
|
// io_pin_mux_set(UART1_TX, IO_FUNC4);
|
}
|
void spi_init()
|
{
|
struct SPI_CFG_T usr_spi_cfg =
|
{
|
.bit_rate = 1000000,
|
.data_bits = 8,
|
//#if TEST_SPI_MASTER
|
.slave = 0,
|
//#else
|
// .slave = 1,
|
//#endif
|
.clk_phase = 0,
|
.clk_polarity = 0,
|
.ti_mode = 0,
|
//#if (TEST_SPI_MODE == TEST_SPI_POLL_MODE)
|
.dma_rx = false,
|
.dma_tx = false,
|
.int_rx = false,
|
.int_tx = false,
|
//#elif (TEST_SPI_MODE == TEST_SPI_INTERUPT_MODE)
|
// .dma_rx = false,
|
// .dma_tx = false,
|
// .int_rx = true,
|
// .int_tx = true,
|
//#elif (TEST_SPI_MODE == TEST_SPI_DMA_MODE)
|
// .dma_rx = true,
|
// .dma_tx = true,
|
// .int_rx = false,
|
// .int_tx = false,
|
//#endif
|
};
|
|
spi_open(SPI_ID0, &usr_spi_cfg);
|
}
|
uint8_t ceshi;
|
uint32_t error_cnt=0;
|
uint32_t successful_cnt=0;
|
uint8_t flag_4guart_needinit=0;
|
#define TX_LEN 10
|
#define RX_LEN 200
|
|
extern uint8_t RX_Buffer[RX_LEN];
|
extern uint16_t BufferSize;
|
static uint16_t source_id;
|
extern struct RxDoneMsg RxDoneParams;
|
|
|
extern uint16_t current_count;
|
extern uint8_t rec_index,rec_secdelay;
|
extern uint8_t yuyin_no_sleep_flag,no_rx_flag;
|
|
|
/********************************************************************************************************/
|
/********************************************************************************************************/
|
/********************************************************************************************************/
|
//static uint16_t delaytime = 771;
|
//extern uint16_t wg_report_freq,wg_report_id;
|
//extern uint32_t wg_lost_count;
|
//extern uint8_t shengji_flag;
|
//extern uint8_t lora_jianting_flag,report_ancnum;
|
//uint8_t imu_enable,motor_enable;
|
//uint32_t time_flag_lorarx=0;
|
//uint32_t lora_huanxing_count=0;
|
//uint16_t Lora_wangguanid=0;
|
//wg_state_enum wg_state = WG_Lost;
|
//uint16_t wangguan_up_id;
|
//uint8_t LoraUp_flag;
|
//uint16_t rec_wenjian_daxiao;
|
//uint16_t recnum[3];
|
//uint16_t recv_flag=0;
|
//uint16_t tx_flag=0;
|
//uint16_t CRC16=0;
|
//uint16_t DEST_ID=0;
|
//uint16_t rec_value,rec_delaytime,rx_count,datalen_offset;
|
//uint16_t Loratx_flag=0;
|
//uint16_t Lorarx_flag=0;
|
//uint16_t Lorarx_time_out_flag=0;
|
//uint8_t tx_buf[5]={0,1,0,1,1};
|
//uint32_t sleep_time_count=0;
|
//uint32_t uwb_ceju_count=0;
|
wg_state_enum wg_state = WG_Lost;
|
/********************************************************************************************************/
|
static void Lora_irq_handler(enum IO_PIN_T pin)
|
{
|
RadioIrqProcess();
|
}
|
|
void Board_LORA_NVIC_Init(GPIO_IRQ_HANDLER_T irq_handler) //LORA ÖжÏÅäÖÃ
|
{
|
// io_pin_mux_set(_4G_USART_RX_Pin,IO_FUNC0);//°ÑÔÏÈio ±äΪÆÕͨGPIO
|
gpio_pin_set_dir(LORA_DIO , GPIO_DIR_IN, 0);
|
io_pull_set(LORA_DIO, IO_HIGH_Z, IO_PULL_UP_NONE);
|
gpio_enable_irq(LORA_DIO, GPIO_IRQ_TYPE_RISING_EDGE, irq_handler);
|
}
|
extern uint8_t pca9555writedata_input[10];//ÔÝ´æÊäÈë¼Ä´æÆ÷ËùÓÐÅäÖõÄÊý×é
|
static void pca_handler(enum IO_PIN_T pin)
|
{
|
PCA9555_readdata(PCA9555_DEVICE_ADDR,pca9555writedata_input);//¶ÁÊäÈë¼Ä´æÆ÷µÄÖµ
|
check_input_change();
|
}
|
extern uint32_t uwb_time_count;
|
void upload_apppoll()
|
{
|
uint32_t elapsed_time_jibu;
|
|
switch (current_state)
|
{
|
case STATE_NORMAL:
|
elapsed_time_jibu = uwb_time_count - state_start_time;
|
switch(elapsed_time_jibu)
|
{
|
case UWB_OPEN_COUNT:
|
UWB_LED_OFF;
|
CloseUWB();
|
break;
|
|
case UWB_MEASUREMENT_INTERVAL:
|
UWB_LED_ON;
|
OpenUWB();
|
LoraReportPoll();
|
state_start_time = uwb_time_count;
|
break;
|
}
|
break;
|
|
case STATE_SLEEP:
|
elapsed_time_jibu = uwb_time_count - state_start_time;
|
switch(elapsed_time_jibu)
|
{
|
case UWB_OPEN_COUNT:
|
UWB_LED_OFF;
|
CloseUWB();
|
break;
|
|
case UWB_MEASUREMENT_INTERVAL_SLEEP:
|
UWB_LED_ON;
|
OpenUWB();
|
LoraReportPoll();
|
state_start_time = uwb_time_count;
|
break;
|
}
|
break;
|
}
|
|
}
|
uint8_t io14_state;
|
uint16_t lora_freq=0;
|
uint16_t lora_up_count=0;
|
//uint8_t TXBuffer[8]={0,1,2,3,4,5,6,7};
|
//uint8_t test_buf[10]={0,1,1,1,1,1,1,1,1,1};
|
int main(void)
|
{
|
board_clock_run();
|
boot_deinit();
|
// board_debug_console_open(TRACE_PORT_UART1);
|
// Reset reason
|
reset_cause_get();
|
reset_cause_clear();
|
// Chip calibration
|
calib_chip();
|
// Disable watchdog timer
|
wdt_close(WDT_ID0);
|
gpio_open();
|
Board_gpio_init();
|
//board_led_init();
|
spi_init();
|
|
|
Board_LORA_NVIC_Init(Lora_irq_handler);
|
pca_input_detection_init(pca_handler);
|
IIC2_Init();
|
Accelerometer_Init();
|
|
PCA9555_init();
|
Lora_1268_Init();
|
SwitchLoraSettings(478,7,22);
|
Program_Init();
|
power_init();
|
sleep_timer_open(true, SLEEP_TIMER_MODE_RELOAD, sleep_timer_callback);
|
sleep_timer_start(__MS_TO_32K_CNT(1000));//²âÊÔ
|
|
#ifdef DEBUG_BOXING
|
io_pin_mux_set(BOXING_PIN,IO_FUNC0);
|
gpio_pin_set_dir(BOXING_PIN,GPIO_DIR_OUT,0);
|
io_pull_set(BOXING_PIN,IO_PULL_DOWN,IO_PULL_UP_LEVEL4);
|
#endif
|
Uwbinit();
|
// OpenUWB();
|
while (1)
|
{
|
// Lora_Tx_Poll();
|
// uwb_app_poll();
|
check_step_and_update_state();
|
upload_apppoll();
|
IdleTask();
|
IMUTask();
|
}
|
}
|