Skip to content

Commit

Permalink
wifi send logically
Browse files Browse the repository at this point in the history
  • Loading branch information
Neyzoter committed Apr 17, 2019
1 parent ce29147 commit 22703fb
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 66 deletions.
16 changes: 8 additions & 8 deletions HARDWARE/CAN/can1.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void CAN1_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode,u32 * filter_list,u8
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
/* !! 中断优先级CAN1和CAN2必须相同*/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
Expand Down Expand Up @@ -158,13 +158,13 @@ void CAN1_RX0_IRQHandler(void){
/* CAN1数据拷贝至queue.arr尾部,并更新tail*/
queue_arr_memcpy(&can_queue, (u8 *)&RxMessage , sizeof(RxMessage));

#if PRINT_UART_LOG
printf("CAN1 Data : ");
for(int i = 0; i < RxMessage.DLC;i++){
printf("%d ",RxMessage.Data[i]);
}
printf("\r\n");
#endif
// #if PRINT_UART_LOG
// printf("CAN1 Data : ");
// for(int i = 0; i < RxMessage.DLC;i++){
// printf("%d ",RxMessage.Data[i]);
// }
// printf("\r\n");
// #endif
}
}
CAN_ClearITPendingBit(CAN1, CAN_IT_FMP0);
Expand Down
16 changes: 8 additions & 8 deletions HARDWARE/CAN/can2.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void CAN2_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode,u32 * filter_list,u8
CAN_ITConfig(CAN2,CAN_IT_FMP1,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX1_IRQn;
/* !! 中断优先级CAN1和CAN2必须相同*/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
Expand Down Expand Up @@ -176,13 +176,13 @@ void CAN2_RX1_IRQHandler(void)
queue_arr_memcpy(&can_queue, (u8 *)&SYSTEMTIME , sizeof(SYSTEMTIME));
/* CAN2数据拷贝至queue.arr尾部,并更新tail*/
queue_arr_memcpy(&can_queue, (u8 *)&RxMessage , sizeof(RxMessage));
#if PRINT_UART_LOG
printf("CAN2 Data : ");
for(int i = 0; i < RxMessage.DLC;i++){
printf("%d ",RxMessage.Data[i]);
}
printf("\r\n");
#endif
// #if PRINT_UART_LOG
// printf("CAN2 Data : ");
// for(int i = 0; i < RxMessage.DLC;i++){
// printf("%d ",RxMessage.Data[i]);
// }
// printf("\r\n");
// #endif
}
}
CAN_ClearITPendingBit(CAN2, CAN_IT_FMP1);
Expand Down
13 changes: 10 additions & 3 deletions HARDWARE/QUEUE/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,15 @@ u8 queue_get(volatile Queue * pQueue)
* @retval None
*/
void queue_arr_memcpy(Queue * pQueue, u8 * buf , u8 len){
memcpy((u8 *)&pQueue->arr[pQueue->tail],buf,len);
if((len + pQueue->tail) <= QUEUE_SIZE){
memcpy((u8 *)&(pQueue->arr[pQueue->tail]),buf,len);

}else{
memcpy((u8 *)&(pQueue->arr[pQueue->tail]),buf,QUEUE_SIZE - pQueue->tail);
memcpy((u8 *)&(pQueue->arr[0]),buf+QUEUE_SIZE - pQueue->tail,len - QUEUE_SIZE + pQueue->tail);
}
pQueue->tail = (pQueue->tail + len) % QUEUE_SIZE;

}

/**
Expand All @@ -85,7 +92,8 @@ void queue_clear(volatile Queue * pQueue)
*/
void queue_addtime_addIO(volatile Queue * pQueue, u32 count, u8 id, u8 IO_input1, u8 IO_input2,u8 dataType)
{
pQueue->head = (pQueue->head-PACKAGE_HEAD_FRAME_LENGTH + QUEUE_SIZE)% QUEUE_SIZE; //往前距离为包的帧头长度,包括测试名称、时间、IO高低电平等
pQueue->head = (pQueue->head + QUEUE_SIZE -PACKAGE_HEAD_FRAME_LENGTH)% QUEUE_SIZE; //往前距离为包的帧头长度,包括测试名称、时间、IO高低电平等
// printf("Head: %d \r\nTail: %d \r\n\r\n",pQueue->head,pQueue->tail);
pQueue->arr[(pQueue->head+0)% QUEUE_SIZE] = (u8)(pQueue->YYYY_MM_DD);
pQueue->arr[(pQueue->head+1)% QUEUE_SIZE] = (u8)(pQueue->YYYY_MM_DD>>8);
pQueue->arr[(pQueue->head+2)% QUEUE_SIZE] = (u8)(pQueue->YYYY_MM_DD>>16);
Expand Down Expand Up @@ -159,7 +167,6 @@ u8 queue_empty(volatile Queue queue)
*/
u32 queue_length(volatile Queue queue)
{
// printf("len : %d\r\n",(queue.tail-queue.head+QUEUE_SIZE)%QUEUE_SIZE);
return (queue.tail-queue.head+QUEUE_SIZE)%QUEUE_SIZE;
}

Expand Down
6 changes: 3 additions & 3 deletions HARDWARE/QUEUE/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
* @note 队列存储ADC和CAN数据,数据长度以ADC长度设置
* @{
*/
#define UDP_SEND_SIZE (8*60 + PACKAGE_HEAD_FRAME_LENGTH) /*!< UDP数据包最大大小 540 = 8 * 60 + PACKAGE_HEAD_FRAME_LENGTH>*/
#define QUEUE_SIZE 700 /*!< 队列正常容量>*/
#define ARR_SIZE (QUEUE_SIZE + UDP_SEND_SIZE+50) /*!< 队列最大容量>*/
#define UDP_SEND_SIZE (8*40 + PACKAGE_HEAD_FRAME_LENGTH) /*!< UDP数据包最大大小 540 = 8 * 60 + PACKAGE_HEAD_FRAME_LENGTH>*/
#define QUEUE_SIZE 500 /*!< 队列正常容量,必须比UDP_SEND_SIZE大一些,不然会在被后面的信息覆盖>*/
#define ARR_SIZE (QUEUE_SIZE + UDP_SEND_SIZE+52) /*!< 队列最大容量>*/
/**
* @}
*/
Expand Down
2 changes: 1 addition & 1 deletion HARDWARE/TIMER/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void TIM4_Int_Init(u16 arr,u16 psc)
/* TIM4中断*/
NVIC_InitStructure.NVIC_IRQChannel=TIM4_IRQn;
/* 抢占优先级*/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00;
/* 子优先级3*/
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
Expand Down
76 changes: 36 additions & 40 deletions HARDWARE/WIFI/userwifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
u32 SYSTEMTIME = 0; //系统时间
u32 YYMMDD =0; //年月日
u8 Time_Sync_Flag = 0; //最近时钟是否同步
volatile u8 Wifi_Send_EN = 0; //数据采集和发送使能,是CAN和ADC采集的总开关
volatile u8 Wifi_Send_EN = 1; //数据采集和发送使能,是CAN和ADC采集的总开关
u8 CAN_Get_EN = CAN1_ENABLE_BIT_SLC|CAN2_ENABLE_BIT_SLC; //CAN数据发送使能(第0位使能can1,第1位使能can2),默认开启(必须满足Wifi_Send_EN=1,才能采集)
u8 ADC_Get_EN = 0; //ADC数据采集使能,默认开启(必须满足Wifi_Send_EN=1,才能采集)
u8 ADC_Get_EN = 1; //ADC数据采集使能,默认开启(必须满足Wifi_Send_EN=1,才能采集)
Queue adc_queue; //ADC数据存储
Queue can_queue; //can数据存储
u8 localDestIp_txrx[4] = {255,255,255,255};
Expand Down Expand Up @@ -143,25 +143,23 @@ u8 wifi_send_package()

/* ADC Queue加入帧头*/
queue_addtime_addIO(&adc_queue,Adc_Length, nodeId, DIGITAL_INPUT1,DIGITAL_INPUT2,ADC_DATA_PACKAGE);

/**获取队列头,并更新队列*/
Adc_Head = adc_queue.head;
adc_queue.head = adc_queue.tail;
/* CAN Queue加入帧头*/
queue_addtime_addIO(&can_queue,Can_Length, nodeId, DIGITAL_INPUT1,DIGITAL_INPUT2,CAN_DATA_PACKAGE);
Can_Head = can_queue.head;
can_queue.head = can_queue.tail;

/* ADC如果分成两段,将前面一段复制到后面*/
if(adc_queue.head + Adc_Length > QUEUE_SIZE ) {
queue_oversize(&adc_queue,adc_queue.head + PACKAGE_HEAD_FRAME_LENGTH + Adc_Length - QUEUE_SIZE);
if(Adc_Head + Adc_Length + PACKAGE_HEAD_FRAME_LENGTH > QUEUE_SIZE ) {
queue_oversize(&adc_queue,Adc_Head + PACKAGE_HEAD_FRAME_LENGTH + Adc_Length - QUEUE_SIZE);
}
/* CAN如果分成两段,将前面一段复制到后面*/
if(can_queue.head + Can_Length > QUEUE_SIZE ) {
queue_oversize(&can_queue,can_queue.head + PACKAGE_HEAD_FRAME_LENGTH + Can_Length - QUEUE_SIZE);
if(Can_Head + Can_Length + PACKAGE_HEAD_FRAME_LENGTH > QUEUE_SIZE ) {
queue_oversize(&can_queue,Can_Head + PACKAGE_HEAD_FRAME_LENGTH + Can_Length - QUEUE_SIZE);
}

/**获取队列头,并更新队列*/
Adc_Head = adc_queue.head;
Can_Head = can_queue.head;
adc_queue.head = adc_queue.tail;
can_queue.head = can_queue.tail;

temp = DATA_AUTO_CHECK_EN;
DATA_AUTO_CHECK_EN = 0;
/* ADC数据发送到远程服务器*/
Expand Down Expand Up @@ -196,13 +194,13 @@ u8 wifi_send_package()
if(queue_length(adc_queue) >= (UDP_SEND_SIZE - PACKAGE_HEAD_FRAME_LENGTH )){

Adc_Length = queue_length(adc_queue);

queue_addtime_addIO(&adc_queue,Adc_Length,nodeId, DIGITAL_INPUT1,DIGITAL_INPUT2,ADC_DATA_PACKAGE); // head <- head-10;
if(adc_queue.head + UDP_SEND_SIZE > QUEUE_SIZE ){
queue_oversize(&adc_queue,adc_queue.head + UDP_SEND_SIZE - QUEUE_SIZE);
}
Adc_Head = adc_queue.head;
adc_queue.head = adc_queue.tail;
adc_queue.head = adc_queue.tail;
if(Adc_Head + Adc_Length + PACKAGE_HEAD_FRAME_LENGTH > QUEUE_SIZE ){
queue_oversize(&adc_queue,Adc_Head + Adc_Length + PACKAGE_HEAD_FRAME_LENGTH - QUEUE_SIZE);
}

//发送到远程服务器
temp = DATA_AUTO_CHECK_EN;
DATA_AUTO_CHECK_EN = 0;
Expand All @@ -218,55 +216,53 @@ u8 wifi_send_package()
}
/* CAN队列已满*/
if( queue_length(can_queue) >= (UDP_SEND_SIZE - PACKAGE_HEAD_FRAME_LENGTH )){
/* 得到can队列的长度,只有数据*/
Can_Length = queue_length(can_queue);

/* CAN Queue加入帧头*/
queue_addtime_addIO(&can_queue,Can_Length, nodeId, DIGITAL_INPUT1,DIGITAL_INPUT2,CAN_DATA_PACKAGE);
Can_Head = can_queue.head;
can_queue.head = can_queue.tail;

/* 如果分成两段,将前面一段复制到后面*/
if(adc_queue.head + UDP_SEND_SIZE > QUEUE_SIZE ) {
queue_oversize(&can_queue,can_queue.head + UDP_SEND_SIZE - QUEUE_SIZE);
if(Can_Head + Can_Length + PACKAGE_HEAD_FRAME_LENGTH > QUEUE_SIZE ) {
queue_oversize(&can_queue,Can_Head + Can_Length + PACKAGE_HEAD_FRAME_LENGTH - QUEUE_SIZE);
}

/**获取队列头,并更新队列*/
Can_Head = can_queue.head;
can_queue.head = can_queue.tail;
// for(int i =0 ;i < Can_Length+PACKAGE_HEAD_FRAME_LENGTH;i++){
// printf("%x ",can_queue.arr[can_queue.head+i]);
// }

temp = DATA_AUTO_CHECK_EN;
DATA_AUTO_CHECK_EN = 0;
/* 发送到远程服务器*/
rsi_send_ludp_data(socketDescriptor_txrx, &can_queue.arr[Can_Head],Can_Length+PACKAGE_HEAD_FRAME_LENGTH, RSI_PROTOCOL_UDP_V4, (uint8 *)destIp_txrx, destSocket_txrx, &bytes_sent);

rsi_send_ludp_data(socketDescriptor_txrx, &can_queue.arr[Can_Head],Can_Length, RSI_PROTOCOL_UDP_V4, (uint8 *)destIp_txrx, destSocket_txrx, &bytes_sent);
DATA_AUTO_CHECK_EN = temp;
delay_ms(5);
temp = DATA_AUTO_CHECK_EN;
DATA_AUTO_CHECK_EN = 0;
/* CAN数据发送到局域网*/
rsi_send_ludp_data(localSocketDescriptor_txrx, &can_queue.arr[Can_Head],Can_Length+PACKAGE_HEAD_FRAME_LENGTH, RSI_PROTOCOL_UDP_V4, (uint8 *)localDestIp_txrx, localDestSocket_txrx, &bytes_sent);
rsi_send_ludp_data(localSocketDescriptor_txrx, &can_queue.arr[Can_Head],Can_Length, RSI_PROTOCOL_UDP_V4, (uint8 *)localDestIp_txrx, localDestSocket_txrx, &bytes_sent);
DATA_AUTO_CHECK_EN = temp;
}
/* CAN队列中数据存储时间过长,以us为单位,就发出来*/
if(queue_length(can_queue) > 0){
Can_Length = queue_length(can_queue);
}else if(queue_length(can_queue) > 0){ /* CAN队列中数据存储时间过长,以us为单位,就发出来*/
if(((SYSTEMTIME - ((u32)(can_queue.arr[can_queue.head+1]&0xff)|((u32)(can_queue.arr[can_queue.head+2]&0xff)<<8)|((u32)(can_queue.arr[can_queue.head+3]&0xff)<<16)
|((u32)(can_queue.arr[can_queue.head+4]&0xff)<<24)))*(TIM4_ARR + 1)*(TIM4_PSC + 1) / TIM3_4_PCLK_MHZ) > CAN_OVERTIME_SEND_TIME){
/* 得到can队列的长度,只有数据*/
Can_Length = queue_length(can_queue);
/* CAN Queue加入帧头*/
queue_addtime_addIO(&can_queue,Can_Length, nodeId, DIGITAL_INPUT1,DIGITAL_INPUT2,CAN_DATA_PACKAGE);

/* 如果分成两段,将前面一段复制到后面*/
if(can_queue.head + Can_Length > QUEUE_SIZE ) {
queue_oversize(&can_queue,can_queue.head + Can_Length + PACKAGE_HEAD_FRAME_LENGTH - QUEUE_SIZE);
}

/**获取队列头,并更新队列*/
Can_Head = can_queue.head;
can_queue.head = can_queue.tail;
can_queue.head = can_queue.tail;
/* 如果分成两段,将前面一段复制到后面*/
if(can_queue.head + Can_Length + PACKAGE_HEAD_FRAME_LENGTH > QUEUE_SIZE ) {
queue_oversize(&can_queue,can_queue.head + Can_Length + PACKAGE_HEAD_FRAME_LENGTH - QUEUE_SIZE);
};


temp = DATA_AUTO_CHECK_EN;
DATA_AUTO_CHECK_EN = 0;
/* 发送到远程服务器*/
rsi_send_ludp_data(socketDescriptor_txrx, &can_queue.arr[Can_Head],Can_Length+PACKAGE_HEAD_FRAME_LENGTH, RSI_PROTOCOL_UDP_V4, (uint8 *)destIp_txrx, destSocket_txrx, &bytes_sent);

DATA_AUTO_CHECK_EN = temp;
delay_ms(5);
temp = DATA_AUTO_CHECK_EN;
Expand Down
2 changes: 1 addition & 1 deletion HARDWARE/WIFI/userwifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ typedef enum{
/** @defgroup 超时发送
* @{
*/
#define CAN_OVERTIME_SEND_TIME 2000000 /*!< 2000ms>*/
#define CAN_OVERTIME_SEND_TIME 600000 /*!< 8000ms>*/
/**
* @}
*/
Expand Down
5 changes: 3 additions & 2 deletions USER/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ void Initialization (void)

/*队列配置*/
queue_init(&adc_queue);
queue_init(&can_queue);

/*ADC相关引脚初始化*/
ADC_CTRL_Conf();

// /* CAN测试*/
// u32 filter[7] = {0x1800f001,0x1800f002,0x1800f003,0x1800f004,0x1800f005,0x1800f006,0x1800f007};
// CAN1_Mode_Init(CAN_SJW_1tq,CAN_BS1_6tq,CAN_BS2_7tq,12,CAN_Mode_Normal,(u32 *)filter,7);
u32 filter[7] = {0x1800f001,0x1800f002,0x1800f003,0x1800f004,0x1800f005,0x1800f006,0x1800f007};
CAN1_Mode_Init(CAN_SJW_1tq,CAN_BS1_6tq,CAN_BS2_7tq,12,CAN_Mode_Normal,(u32 *)filter,7);
// CAN2_Mode_Init(CAN_SJW_1tq,CAN_BS1_6tq,CAN_BS2_7tq,6,CAN_Mode_Normal,(u32 *)filter,7);
/*系统时间100us中断*/
TIM4_Int_Init(TIM4_ARR,TIM4_PSC);
Expand Down

0 comments on commit 22703fb

Please sign in to comment.