|
#include "Rcc_Nvic_Systick.h"
|
#include "Usart.h"
|
#include "Spi.h"
|
#include "Flash.h"
|
#include "led.h"
|
#include "beep.h"
|
#include "dw_driver.h"
|
#include "dw_app.h"
|
#include "stm32f10x_it.h"
|
#include "serial_at_cmd_app.h"
|
#include "global_param.h"
|
#include "ADC.h"
|
|
//#define DEBUG_MODE
|
/*
|
* rlv:ÖØ×°ÔØ¼Ä´æÆ÷µÄÖµ£¬È¡Öµ·¶Î§Îª£º0-0XFFF
|
* º¯Êýµ÷ÓþÙÀý£º
|
* IWDG_Config(IWDG_Prescaler_64 ,625); // IWDG 1s ³¬Ê±Òç³ö
|
* £¨64/40£©*625 = 1s
|
*/
|
|
void IWDG_Config(uint8_t prv ,uint16_t rlv)
|
{
|
// ʹÄÜ Ô¤·ÖƵ¼Ä´æÆ÷PRºÍÖØ×°ÔØ¼Ä´æÆ÷RLR¿Éд
|
IWDG_WriteAccessCmd( IWDG_WriteAccess_Enable );
|
|
// ÉèÖÃÔ¤·ÖƵÆ÷Öµ
|
IWDG_SetPrescaler( prv );
|
|
// ÉèÖÃÖØ×°ÔØ¼Ä´æÆ÷Öµ
|
IWDG_SetReload( rlv );
|
|
// °ÑÖØ×°ÔØ¼Ä´æÆ÷µÄÖµ·Åµ½¼ÆÊýÆ÷ÖÐ
|
IWDG_ReloadCounter();
|
|
// ʹÄÜ IWDG
|
IWDG_Enable();
|
}
|
void IWDG_Feed(void)
|
{
|
IWDG_ReloadCounter();
|
}
|
void Device_Init(void)
|
{
|
RCC_Configuration();
|
//SystemInit();
|
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x5000);
|
Nvic_Init();
|
// Systick_Init();
|
TIM3_Int_Init();
|
Led_Init();
|
Beep_Init();
|
DW_GPIO_Init();
|
Uart1_Init();
|
Spi_Init();
|
ADC_Configuration();
|
IWDG_Config(IWDG_Prescaler_128 ,4095); //10s
|
IWDG_Feed();
|
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
|
}
|
u8 anchor_type;
|
u32 dev_id;
|
u8 hbsend[16];
|
void HeartBeatInit(void)
|
{
|
u16 checksum;
|
hbsend[0]=0x55;
|
hbsend[1]=0xAA;
|
hbsend[2]=0x2;
|
hbsend[3]=0xc;
|
memcpy(&hbsend[4],&g_com_map[DEV_ID],2);
|
checksum = Checksum_u16(&hbsend[2],12);
|
memcpy(&hbsend[14],&checksum,2);
|
}
|
|
uint16_t vel_factor,pos_factor;
|
u16 tyncpoll_time,dist_threshold,group_id;
|
u16 slottime,max_slotpos;
|
extern u8 module_power;
|
extern u16 total_slotnum;
|
void ComMapCheck(void)
|
{
|
|
if(g_com_map[ANT_LENGTH]>10000)
|
{
|
g_com_map[ANT_LENGTH] = 0;
|
}
|
if(g_com_map[WAR_JDQ_TIME]>1000||g_com_map[WAR_JDQ_TIME]<0)
|
{
|
g_com_map[WAR_JDQ_TIME] = 1;
|
}
|
if(g_com_map[COM_INTERVAL]==0)
|
{
|
g_com_map[COM_INTERVAL]=100;
|
}
|
}
|
u16 JDQ_time;
|
int16_t g_commap_antdelay;
|
void Program_Init(void)
|
{ float temp;
|
u16 temp2;
|
uint16_t i;
|
Usart1ParseDataCallback = UsartParseDataHandler;
|
parameter_init();
|
//deca_sleep(1000);
|
HeartBeatInit();
|
Modbus_RegMap();
|
#ifdef DEBUG_MODE
|
// g_com_map[DEV_ROLE]=1;
|
// g_com_map[DEV_ID]=1;
|
// g_com_map[COM_INTERVAL]=100;
|
// g_com_map[MAX_REPORT_ANC_NUM]=3;
|
// g_com_map[NEARBASE_NUM]=1;
|
// g_com_map[NEARBASE_ID1]=2;
|
g_com_map[ANC_FLAG]=1;
|
save_com_map_to_flash();
|
#endif
|
//
|
// g_com_map[COM_INTERVAL]=100;
|
// g_com_map[MAX_REPORT_ANC_NUM]=10;
|
// g_com_map[GROUP_ID]=0;
|
// g_com_map[DIST_OFFSET]=-116;
|
|
// g_com_map[DEV_ROLE] =0x0; //»ùÕ¾
|
//
|
// g_com_map[ANC_POLL]=1;
|
// g_com_map[ALARM_DEV] = 0;
|
// g_com_map[ALARM_DISTANCE1] = 0; //ĬÈϱ¨¾¯¾àÀë50cm
|
// g_com_map[ALARM_DISTANCE2] = 900; //ĬÈϱ¨¾¯¾àÀë50cm
|
// g_com_map[ALARM_DISTANCE3] = 1400; //ĬÈϱ¨¾¯¾àÀë50cm
|
// g_com_map[RELAY2_DISTANCE1] = 0; //ĬÈϱ¨¾¯¾àÀë50cm
|
// g_com_map[RELAY2_DISTANCE2] = 900; //ĬÈϱ¨¾¯¾àÀë50cm
|
// g_com_map[RELAY2_DISTANCE3] = 1400; //ĬÈϱ¨¾¯¾àÀë50cm
|
// g_com_map[WAR_JDQ_TIME] = 1; //1Ãë
|
// g_com_map[POWER]=30;
|
// g_com_map[SPEEDFILTER_THRES]=10000;
|
// g_com_map[FILTER_COEFFICIENT]=40;
|
// g_com_map[ANT_LENGTH]=0;
|
// g_com_map[NOMOVESLEEP_TIME]=728;
|
|
|
OUT485_ENABLE;
|
ComMapCheck();
|
g_com_map[VERSION] = 1<<8|76;
|
FLASH_Read(FLASH_HARDWARE_FLAG, (uint8_t*)&g_com_map[HARDWAREFLAG_INDEX], 16);
|
dist_threshold = (g_com_map[SPEEDFILTER_THRES]*10)/(1000/g_com_map[COM_INTERVAL]);
|
if(dist_threshold<300)
|
{dist_threshold = 300;}
|
if(dist_threshold>15000)
|
{dist_threshold = 15000;}
|
g_commap_antdelay = g_com_map[ANT_LENGTH];
|
JDQ_time = g_com_map[WAR_JDQ_TIME]*10;
|
g_com_map[SW_TYPE] = SW_FANGZHUANG;
|
dev_id = g_com_map[DEV_ID];
|
group_id=g_com_map[GROUP_ID];
|
slottime=ceil((double)g_com_map[MAX_REPORT_ANC_NUM]*SLOT_SCALE)+3;
|
max_slotpos=g_com_map[COM_INTERVAL]/slottime;
|
tyncpoll_time=(g_com_map[DEV_ID]%max_slotpos)*slottime;
|
module_power = g_com_map[POWER];
|
total_slotnum = 1000/g_com_map[COM_INTERVAL];
|
vel_factor=g_com_map[FILTER_COEFFICIENT];
|
pos_factor=g_com_map[FILTER_COEFFICIENT];
|
if(module_power>67)
|
{
|
module_power=67;
|
}
|
|
if(g_com_map[DEV_ROLE])
|
{
|
printf("±êÇ©ID: %d .\r\n",dev_id);
|
printf("ͨѶ¼ä¸ô: %d ms.\r\n",g_com_map[COM_INTERVAL]);
|
printf("µ¥´ÎͨѶ»ùÕ¾ÊýÁ¿: %d¸ö.\r\n",g_com_map[MAX_REPORT_ANC_NUM]);
|
}else{
|
|
anchor_type = dev_id%g_com_map[MAX_REPORT_ANC_NUM];
|
printf("»ùÕ¾ID: %x .\r\n",dev_id);
|
printf("»ùÕ¾ÀàÐÍ: %c .\r\n",anchor_type+0x41);
|
printf("µ¥´ÎͨѶ»ùÕ¾ÊýÁ¿: %d¸ö.\r\n",g_com_map[MAX_REPORT_ANC_NUM]);
|
}
|
OUT485_DISABLE;
|
// printf("DEVICE PAIRID: %d .\r\n",g_com_map[PAIR_ID]);
|
// printf("DEVICE ALARM DISTANCE: 1.%d 2.%d 3.%d .\r\n",g_com_map[ALARM_DISTANCE1],g_com_map[ALARM_DISTANCE2],g_com_map[ALARM_DISTANCE3]);
|
|
|
for(i=0;i<255;i++)
|
{
|
g_Tagdist[i]=0x1ffff;
|
}
|
}
|
|
/*! ------------------------------------------------------------------------------------------------------------------
|
* @fn main()
|
*
|
* @brief Application entry point.
|
*
|
* @param none
|
*
|
* @return none
|
*/
|
void HeatBeat(void)
|
{
|
UART_PushFrame(hbsend,16);
|
}
|
uint16_t temp;
|
float dw_vbat;
|
void StartUpTest(void)
|
{
|
temp=dwt_readtempvbat(1);
|
dw_vbat=(float)((temp&0xff)-173)/173+3.3;
|
if(dw_vbat<2.8)
|
{
|
SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //Èí¸´Î»»Øµ½bootloader
|
}
|
}
|
extern u8 g_start_sync_flag,usart_send_flag,anc_report_num;
|
u16 heartbeat_timer,poll_timer;
|
int16_t sync_timer;
|
extern u8 flag_newsecond, tagpos_rec[50], tagpos_send[50],ancidlist_num;
|
uint32_t tagpos_binary;
|
extern u16 ancidlist_rec[20],ancidlist_send[20];
|
extern u16 target_time;
|
void IdleTask(void)
|
{
|
|
UART_CheckReceive();
|
UART_CheckSend();
|
|
if(heartbeat_timer>1000)
|
{
|
heartbeat_timer=0;
|
if(g_com_map[HEARTBEAT])
|
HeatBeat();
|
}
|
if(flag_newsecond)
|
{
|
uint32_t temp23;
|
// temp23 = dwt_read32bitreg(CHAN_CTRL_ID) ;
|
// if(temp23!=0x4A480022)
|
// {
|
// temp23 = dwt_read32bitreg(CHAN_CTRL_ID) ;
|
// if(temp23!=0x4A480022)
|
// {
|
// NVIC_SystemReset();
|
// }
|
// }
|
flag_newsecond=0;
|
tagpos_binary=0;
|
memcpy(ancidlist_send,ancidlist_rec,ancidlist_num*2);
|
ancidlist_num=0;
|
for(u8 i=0;i<max_slotpos;i++)
|
{
|
if(tagpos_rec[i])
|
tagpos_binary|=1<<i;
|
tagpos_rec[i] = 0;
|
}
|
}
|
if(target_time>=1000)
|
{target_time-=1000;}
|
#ifdef USART_INTEGRATE_OUTPUT
|
if(g_com_map[DEV_ROLE]==0&&usart_send_flag)
|
{u16 checksum;
|
usart_send_flag=0;
|
usart_send_anc[2] = 5;//Õý³£Ä£Ê½
|
usart_send_anc[3] = anc_report_num*6+2;//Õý³£Ä£Ê½
|
checksum = Checksum_u16(&usart_send_anc[2],anc_report_num*6+2);
|
memcpy(&usart_send_anc[4+anc_report_num*6],&checksum,2);
|
UART_PushFrame(usart_send_anc,6+anc_report_num*6);
|
anc_report_num=0;
|
}
|
#endif
|
if(g_com_map[CNT_UPDATE]==1)
|
{
|
uint32_t result = 0;
|
u16 tmp = 0xAAAA;
|
__disable_irq();
|
result = FLASH_Prepare(0x8004A38, 2);
|
if(result)
|
result = FLASH_Write(0x8004A38, (const uint8_t*)&tmp, 2);
|
__enable_irq();
|
printf("½øÈëÉý¼¶Ä£Ê½\r\n");
|
g_com_map[CNT_UPDATE]=0;
|
save_com_map_to_flash();
|
delay_ms(100);
|
// STMFLASH_Write_NoCheck(0x8004A38,0xAAAA);
|
// Delay_ms(100);
|
SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //Èí¸´Î»»Øµ½bootloader
|
}
|
if(g_com_map[CNT_REBOOT]==1)
|
{
|
FactorySettings();
|
|
SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //Èí¸´Î»»Øµ½bootloader
|
}
|
if(g_com_map[CNT_RESTART]==1)
|
{
|
g_com_map[CNT_RESTART]=0;
|
save_com_map_to_flash();
|
delay_ms(100);
|
SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //Èí¸´Î»»Øµ½bootloader
|
}
|
if(g_com_map[MAP_SIGN_INDEX]!=0x55AA||g_com_map[COM_INTERVAL]==0)
|
{
|
SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //Èí¸´Î»»Øµ½bootloader
|
}
|
g_com_map[RELAY1_STATE_INDEX] = READ_RELAY1;
|
g_com_map[RELAY2_STATE_INDEX] = READ_RELAY2;
|
|
}
|
|
int main(void)
|
{
|
|
Device_Init();
|
//StartUpTest();
|
Program_Init();
|
Dw1000_Init();
|
delay_ms(10);
|
Dw1000_App_Init();
|
/* Loop forever initiating ranging exchanges. */
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
usart_send[0]=0x55;
|
usart_send[1]=0xAA;
|
usart_send_anc[0]=0x55;
|
usart_send_anc[1]=0xAA;
|
|
while(1)
|
{
|
g_start_sync_flag=0;
|
if(g_start_send_flag)
|
{
|
g_start_send_flag = 0;
|
Tag_App();
|
}
|
IdleTask();
|
if(g_com_map[DEV_ROLE]==0)
|
Anchor_App();
|
|
}
|
}
|