Skip to content

Commit

Permalink
add set iam master clock
Browse files Browse the repository at this point in the history
  • Loading branch information
Neyzoter committed Apr 26, 2019
1 parent bfbb2ca commit 21299be
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 49 deletions.
53 changes: 26 additions & 27 deletions HARDWARE/TIMER/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "adc.h"
#include "can1.h"
#include "can2.h"
#include "config.h"

/**
* @brief 定时器4初始化
Expand Down Expand Up @@ -107,9 +108,7 @@ void TIM3_Int_Init(u16 arr,u16 psc)
}

//!同步时钟模块距离上次同步的时间间隔
#if IAM_MASTER_CLOCK
volatile u32 sync_interval_time = 0;
#endif

//!ms时间
u32 MS_TIME = 0;
Expand Down Expand Up @@ -138,33 +137,33 @@ void TIM3_IRQHandler(void)
* 在系统初始化后才能运行的程序
*/
if(BOARD_STA == BOARD_RUNNING){
/* 如果队列空了,时间戳更新*/
if(queue_empty(&adc_queue)){
adc_queue.HeadTime = SYSTEMTIME;
adc_queue.YYYY_MM_DD = YYMMDD;
}
/* 开始发数据了再开始采集*/
if((Wifi_Send_EN == 1) && (ADC_Get_EN == 1)){
/*! @note
// 转换时间 = N * Tconv + (N-1) * 1us,Tconv = 2us for AD7606-4,Tconv = 3us for AD7606-6
// AD7606-4,64 Sample ratio,T = 193
// CONV : H(25ns,转换中) -> L(25ns) -> H(25ns,转换中)
*/
ADC_CONV_L();//最短时间25ns
/**采集数据,顺便当做延时用*/
adcTamp = ADC_Read(ADC_MAX_BYTES);
/* 读八个字节数据*/
for(int i=0;i<ADC_MAX_BYTES;i++){
queue_put((Queue *)&adc_queue, *(adcTamp+i));

if(IAM_MASTER_CLOCK == 'Y'){
sync_interval_time++;
}else{
/* 如果队列空了,时间戳更新*/
if(queue_empty(&adc_queue)){
adc_queue.HeadTime = SYSTEMTIME;
adc_queue.YYYY_MM_DD = YYMMDD;
}
/* 拉高开始下一次转换*/
ADC_CONV_H();//最短时间25ns
/* 开始发数据了再开始采集*/
if((Wifi_Send_EN == 1) && (ADC_Get_EN == 1)){
/*! @note
// 转换时间 = N * Tconv + (N-1) * 1us,Tconv = 2us for AD7606-4,Tconv = 3us for AD7606-6
// AD7606-4,64 Sample ratio,T = 193
// CONV : H(25ns,转换中) -> L(25ns) -> H(25ns,转换中)
*/
ADC_CONV_L();//最短时间25ns
/**采集数据,顺便当做延时用*/
adcTamp = ADC_Read(ADC_MAX_BYTES);
/* 读八个字节数据*/
for(int i=0;i<ADC_MAX_BYTES;i++){
queue_put((Queue *)&adc_queue, *(adcTamp+i));

}
/* 拉高开始下一次转换*/
ADC_CONV_H();//最短时间25ns
}
}

#if IAM_MASTER_CLOCK
sync_interval_time++;
#endif

/* LED闪烁*/
#if WORKING_LED_SHINE_IN_TIMER
Expand Down
5 changes: 1 addition & 4 deletions HARDWARE/TIMER/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@
extern u32 SYSTEMTIME;

//!同步时钟模块距离上次同步的时间间隔
#if IAM_MASTER_CLOCK
extern u16 sync_interval_time;
#endif

extern volatile u32 sync_interval_time;

void TIM3_Int_Init(u16 arr,u16 psc);
void TIM4_Int_Init(u16 arr,u16 psc);
Expand Down
2 changes: 0 additions & 2 deletions HARDWARE/WIFI/userwifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,6 @@ u8 OpenTcpSocket(u8 *destIp,unsigned short destSocket,unsigned short moduleSocke
return 0;
}

#if IAM_MASTER_CLOCK
#define SYNC_TIME_BYTES 9 //同步时钟命令长度
/**
* @brief 同步时钟模块发送同步时钟信号
Expand All @@ -468,4 +467,3 @@ void Send_Sync_Time(void)
receive_udp_package();
DATA_AUTO_CHECK_EN = temp;
}
#endif
5 changes: 1 addition & 4 deletions HARDWARE/WIFI/userwifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
//-------------include---------------//
#include "sys.h"
#include "queue.h"
#include "config.h"

//!是不是主时钟设备
#define IAM_MASTER_CLOCK 0
#include "config.h"


/**
Expand Down
42 changes: 41 additions & 1 deletion USER/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ u8 RSI_BAND = RSI_DUAL_BAND; /*!< @
u8 BOARD_STA = BOARD_INITING; /*!< 板子的状态>*/
u8 catPara[PARA_CAT_CH_MAX_LENGTH] = {0}; /*!< 存储连接后的数据>*/
u8 localhost[IPV4_LENGTH] = {0}; /*!< 本地ip>*/
u8 IAM_MASTER_CLOCK = 'N'; /*!< 我是不是主时钟>*/


/**
Expand Down Expand Up @@ -73,6 +74,7 @@ u32 getCatParaChLenInFlash(u32 startAddr){
* @arg LOAD_PARA_POINTER_NULL
* @arg LOAD_PARA_SUCCESS
*/
void setMc(c8 * val);
u8 loadParaAndCheck(u8 * catPara,u32 startAddr){
/*先读出长度*/
u32 catParaChLen = getCatParaChLenInFlash(startAddr);
Expand Down Expand Up @@ -155,6 +157,9 @@ u8 loadParaAndCheck(u8 * catPara,u32 startAddr){
#if PRINT_UART_LOG
printf("destIp_txrx : %d.%d.%d.%d\r\n",destIp_txrx[0],destIp_txrx[1],destIp_txrx[2],destIp_txrx[3]);
#endif
/*IAM_MASTER_CLOCK*/
setMc((c8 *)paraStartAddr);
paraStartAddr = splitAddr+1;//开始下一个
return LOAD_PARA_SUCCESS;
}

Expand Down Expand Up @@ -301,6 +306,9 @@ u32 getParaLen(void){
catChLen += sizeof(FLASH_LABLE_TYPE);
catChLen += strlen((c8 *)itoa(destIp_txrx[3]));//IP第4组
catChLen += sizeof(FLASH_LABLE_TYPE);
/*是否是主时钟*/
catChLen += sizeof(IAM_MASTER_CLOCK);
catChLen += sizeof(FLASH_LABLE_TYPE);
/*CRC校验*/
catChLen += FLASH_CRC_BYTE_LENGTH;//加入一个CRC校验

Expand Down Expand Up @@ -358,6 +366,9 @@ u32 getCatPara(void){
*(catPara+i) = FLASH_LABEL_SPLIT;i += sizeof(FLASH_LABLE_TYPE);//分隔符1byte
memcpy((char *)(catPara+i),ip4,strlen((c8 *)ip4));i += strlen((c8 *)ip4);
*(catPara+i) = FLASH_LABEL_SPLIT;i += sizeof(FLASH_LABLE_TYPE);//分隔符1byte
//存是不是主时钟
memcpy((char *)(catPara+i),&IAM_MASTER_CLOCK,sizeof(IAM_MASTER_CLOCK));i += sizeof(IAM_MASTER_CLOCK);
*(catPara+i) = FLASH_LABEL_SPLIT;i += sizeof(FLASH_LABLE_TYPE);//分隔符1byte
//存crc
CRC_TYPE crc = CalCrc(0, (c8 *)catPara,catParaLen- FLASH_CRC_BYTE_LENGTH - sizeof(FLASH_LABLE_TYPE),0x8005);//得到crc,把结束符号和crc位都去掉
#if PRINT_UART_LOG
Expand Down Expand Up @@ -596,6 +607,7 @@ void getHelp(void){
printf("- SET_RSI_PSK : Set RSI_PSK\r\n E.G. SET_RSI_PSK 123456\r\n\r\n");
printf("- SET_NODE_ID : Set Id of the Node\r\n E.G. SET_NODE_ID 1\r\n\r\n");
printf("- SET_SERVER_IP : Set IP of the Server\r\n E.G. SET_SERVER_IP 115.159.154.160\r\n\r\n");
printf("- SET_MASTER_CLK : Set MasterClock('Y') or Sensoring Node('N')\r\n E.G. SET_MASTER_CLK Y\r\n\r\n");
//CMD
printf("- HELP : Print Help Document\r\n E.G. HELP\r\n\r\n");
printf("- GET_PARA : Print Parameters List\r\n E.G. GET_PARA\r\n\r\n");
Expand All @@ -619,6 +631,7 @@ void getPara(void){
printf("destIp_txrx : %d.%d.%d.%d\r\n",destIp_txrx[0],destIp_txrx[1],destIp_txrx[2],destIp_txrx[3]);
printf("localhost : %d.%d.%d.%d\r\n",localhost[0],localhost[1],localhost[2],localhost[3]);
printf("test_name : %s\r\n",test_name);
printf("IAM_MASTER_CLK : %c\r\n",IAM_MASTER_CLOCK);
if(RSI_WIFI_OPER_MODE == RSI_WIFI_CLIENT_MODE_VAL){
printf("Module Mode : CLIENT\r\n");
}else if(RSI_WIFI_OPER_MODE == RSI_WIFI_AP_MODE_VAL){
Expand Down Expand Up @@ -745,6 +758,32 @@ void setIpVal(u8 * val,u8 * obj,c8 * objName){
#endif
}

/**
* @brief 是否设置为主时钟
* @param val:'Y'是主时钟;'N'不是主时钟,是传感节点
* @retval None
*/
void setMc(c8 * val){
#if PRINT_UART_LOG
printf("Setting IAM_MASTER_CLOCK...\r\n");
#endif
if(*val == 'Y'){
IAM_MASTER_CLOCK = 'Y';
}else if(*val == 'N'){
IAM_MASTER_CLOCK = 'N';
}else{
#if PRINT_UART_LOG
printf("Please Input Y or N\r\n");
#endif
return;
}
#if PRINT_UART_LOG
printf("Now IAM_MASTER_CLOCK = %c\r\n",IAM_MASTER_CLOCK);
printf("Setted IAM_MASTER_CLOCK OK\r\n");
printf("To Save Flash.Send \"%s\"\r\n",CMD_SAVE_ALL_PARA);
#endif
}

/**
* @brief 处理已分割的命令和信息
* @param cmd:命令指针
Expand All @@ -763,8 +802,9 @@ void handleCmdVal(c8 * cmd,c8 * val){
setU8Val((u8 *)val,&nodeId,NODE_ID_STRNAME);
}else if(!strcmp(cmd,CMD_SET_SERVER_IP)){//设置服务器IP
setIpVal((u8 * )val,(u8 * )destIp_txrx,SERVER_IP_STRNAME);
}else if(strcmp(cmd,CMD_SET_MASTER_CLK) == 0){//设置是否是主时钟
setMc(val);
}


}

Expand Down
3 changes: 3 additions & 0 deletions USER/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ typedef enum
#define CMD_SET_PSK "SET_RSI_PSK" /*!< 设置PSK>*/
#define CMD_SET_NODE_ID "SET_NODE_ID" /*!< 设置节点id>*/
#define CMD_SET_SERVER_IP "SET_SERVER_IP" /*!< 设置私有云服务器IP>*/
#define CMD_SET_MASTER_CLK "SET_MASTER_CLK" /*!< 设置是否是主时钟>*/
/** !纯命令*/
#define CMD_HELP "HELP" /*!< 输出帮助>*/
#define CMD_GET_PARA "GET_PARA" /*!< 输出参数情况>*/
Expand Down Expand Up @@ -246,6 +247,8 @@ extern u8 RSI_BAND;
extern u8 BOARD_STA;
//!节点的当前IP
extern u8 localhost[IPV4_LENGTH];
//!我是不是主时钟
extern u8 IAM_MASTER_CLOCK;

void writeSectorPara(void);
u8 splitCmd(volatile CMD_QUEUE * pQueue,u8 * pCmd,u8 * pValue);
Expand Down
15 changes: 4 additions & 11 deletions USER/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,15 @@ int main(void)
while(1)
{
if(RSI_WIFI_OPER_MODE == RSI_WIFI_CLIENT_MODE_VAL){
// CAN1_Send_Msg((u8 *)&SYSTEMTIME,4);
// delay_ms(1000);
// CAN2_Send_Msg((u8 *)&SYSTEMTIME,4);
// CAN1_Receive_Msg(CanBuf);
// delay_ms(1000);
wifi_send_package();//发送数据,每次时钟更新后或者数据到达一定数量ADC_SEND_SIZE 8bytes时间+2bytes数字IO+8*N bytes ADC信号
// testAdc();
// receive_udp_package();\\stm32_wifi_ap_1_x\../HARDWARE/WIFI/userwifi.c\adc_queue.tail
// wifi_send_package_test();
#if IAM_MASTER_CLOCK
if(IAM_MASTER_CLOCK == 'Y'){
if(sync_interval_time >= SYNC_INTERVAL_TIME&&Wifi_Send_EN){
TEST_LED_CONV();//翻转
sync_interval_time = 0;
Send_Sync_Time();//时钟同步一下
}
#endif
}else{ //不是主时钟需要发送ADC和CAN数据
wifi_send_package();
}
}else if(RSI_WIFI_OPER_MODE == RSI_WIFI_AP_MODE_VAL){
delay_ms(10);
RspCode =Check_PKT();
Expand Down

0 comments on commit 21299be

Please sign in to comment.