#include "usr_config.h" #include "bsp_time.h" #include "system.h" #include "nrf_gpio.h" #include "app_charge.h" #include "app_flash.h" #include "ble_comm.h" #include "bsp_adc.h" #include "app_pair_chargerpin.h" #include "hal_led.h" #include "hal_charge.h" #include "app_one_wire.h" //发送协议 // Host MAC[0] MAC[1] MAC[2]>>>>>> Client // Client MAC[3] MAC[4] MAC[5]>>>>>> Host //=========================================================================================================== #define PIN_LED_RUN 100 #define DATA_LENGTH 4 #define BITWIDTH 30 #define LOW_BIT_DELAY 15 #define DELAY_SEND 100 #define DELAY_REPLY 100 #define DELAY_CHECK 100 #define DELAY_LEDON (BITWIDTH * (8 * DATA_LENGTH + 3) * 2 + DELAY_SEND + DELAY_REPLY + DELAY_CHECK + 5 * BITWIDTH) #define LEDON_KEEP 100 unsigned static char rxbuf[DATA_LENGTH]; unsigned static char txbuf[DATA_LENGTH]; unsigned static char savebuf[DATA_LENGTH]; typedef void (*_Event)(unsigned char*,int); _Event _success_evt; static char one_ms_status = 0; static void null_pcs(void) {} void chargerpin_one_ms_init(_Event success_evt) { one_ms_status = 0; _success_evt=success_evt; Process_Start(0, "null_pcs", null_pcs); Process_SetHoldOn(null_pcs, 1); TIME_Regist(chargerpin_one_ms_pcs); PAIR_CHARGERPIN_PRINT("-------------------------------------------chargerpin_one_ms_init.%d\r\n", 1); } void chargerpin_one_ms_uninit(void) { Process_Stop(null_pcs); TIME_UnRegist(chargerpin_one_ms_pcs); PAIR_CHARGERPIN_PRINT("-------------------------------------------chargerpin_one_ms_uninit.%d\r\n", 1); } void chargerpin_one_ms_pcs(void *t) { unsigned int ms = *(unsigned int *)t; static unsigned int bit_width = 0; static unsigned int now_bit = 0; static unsigned int delay_send_count = 0; static unsigned int delay_reply_count = 0; static unsigned int delay_check_count = 0; static unsigned int delay_ledon_count = 0; static int bitindex = 0; static int bytesindes = 0; static int value = 0; static int txrxcount = 0; static char lowbitdelay = 0; switch (one_ms_status) { case 0: // init bit_width = 0; now_bit = 0; delay_send_count = 0; delay_reply_count = 0; delay_check_count = 0; delay_ledon_count = 0; bitindex = 0; bytesindes = 0; value = 0; txrxcount = 0; one_ms_status = 0; lowbitdelay = 0; nrf_gpio_cfg( PIN_CHARGING, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_CONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_D0H1, NRF_GPIO_PIN_NOSENSE); nrf_gpio_cfg_watcher(PIN_CHARGING); one_ms_status = 1; nrf_gpio_pin_clear(PIN_CHARGING); PAIR_CHARGERPIN_PRINT("nrf_gpio_cfg_watcher(PIN_CHARGING); clear.%d\r\n", ms); break; case 1: if (now_bit == nrf_gpio_pin_read(PIN_CHARGING)) { bit_width++; } else { //由高电平切换到低电平,下降信号 if (now_bit == 1) { now_bit = 0; if ((bit_width > 1.5 * BITWIDTH - LOW_BIT_DELAY / 3) && (bit_width < 1.5 * BITWIDTH + LOW_BIT_DELAY / 3)) { one_ms_status = 3; txrxcount = 1.5 * BITWIDTH; value = 30; if (mFlash.isHost == 0) { delay_ledon_count = 1.5 * BITWIDTH + DELAY_SEND; PAIR_CHARGERPIN_PRINT("delay_ledon_count = 1.5 * BITWIDTH + DELAY_SEND.%d\r\n", txrxcount); } else { delay_ledon_count = 2.5 * BITWIDTH + DELAY_SEND + BITWIDTH * 8 * DATA_LENGTH + 1.5 * BITWIDTH + DELAY_REPLY; PAIR_CHARGERPIN_PRINT("delay_ledon_count = 2.5 * BITWIDTH + DELAY_SEND + BITWIDTH*8*DATA_LENGTH + 1.5 * BITWIDTH + DELAY_REPLY .%d\r\n", txrxcount); } PAIR_CHARGERPIN_PRINT("-----------------------RX MODE start .%d\r\n", bit_width); break; } else { if (mFlash.isHost) { delay_send_count = 1; //左边鞋发送数据 } PAIR_CHARGERPIN_INFO("EXTI CHARGEING MOID.%d bit_width %d \r\n", ms, bit_width); } } else { now_bit = 1; delay_send_count = 0; delay_reply_count = 0; delay_check_count = 0; nrf_gpio_pin_write(PIN_LED_RUN, 0); delay_ledon_count = 0; PAIR_CHARGERPIN_PRINT("0123delay_ledon_count = 0 .%d\r\n", txrxcount); PAIR_CHARGERPIN_INFO("INTO CHARGEING MOID.%d\r\n", ms); } bit_width = 0; } if (delay_send_count != 0) { delay_send_count++; } if (delay_send_count > DELAY_SEND) { unsigned char macbuff[32]; delay_send_count = 0; PAIR_CHARGERPIN_PRINT("BEGIN send MAC.%d\r\n", ms); Get_MACaddr(macbuff); macbuff[DATA_LENGTH - 1] = 0; for (int i = 0; i < DATA_LENGTH - 1; i++) { macbuff[DATA_LENGTH - 1] += macbuff[i]; } memcpy(txbuf, macbuff, DATA_LENGTH); PAIR_CHARGERPIN_PRINT("host_mac send.%d\r\n", ms); one_ms_status = 2; txrxcount = 0; for (int i = 0; i < DATA_LENGTH; i++) { PAIR_CHARGERPIN_PRINT("txbuf.%x\r\n", txbuf[i]); } break; } if (delay_reply_count != 0) { delay_reply_count++; } if (delay_reply_count > DELAY_REPLY) { unsigned char macbuff[16]; delay_reply_count = 0; PAIR_CHARGERPIN_PRINT("BEGIN send MAC.%d\r\n", ms); Get_MACaddr(macbuff); memcpy(txbuf, &macbuff[3], DATA_LENGTH); txbuf[DATA_LENGTH - 1] = 0; for (int i = 0; i < DATA_LENGTH - 1; i++) { txbuf[DATA_LENGTH - 1] += txbuf[i]; } PAIR_CHARGERPIN_PRINT("host_mac send.%d\r\n", ms); one_ms_status = 2; txrxcount = 0; for (int i = 0; i < DATA_LENGTH; i++) { PAIR_CHARGERPIN_PRINT("txbuf.%x\r\n", txbuf[i]); } break; } if (delay_check_count != 0) { delay_check_count++; } if (delay_check_count > DELAY_CHECK) { delay_check_count = 0; one_ms_status = 4; txrxcount = 0; PAIR_CHARGERPIN_PRINT("CHECK start.%d\r\n", ms); break; } else if (delay_check_count > 0) { if (now_bit > 0) { delay_check_count = 0; PAIR_CHARGERPIN_PRINT("CANCEL CHECK.%d\r\n", ms); } } break; case 2: // tx { lowbitdelay++; static char send_interrupt = 0; if ((now_bit == 0) && (nrf_gpio_pin_read(PIN_CHARGING)) && (lowbitdelay > LOW_BIT_DELAY)) { if (mFlash.isHost) { one_ms_status = 1; nrf_gpio_pin_clear(PIN_CHARGING); PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms); delay_ledon_count = 0; PAIR_CHARGERPIN_PRINT("delay_ledon_count1 = 0;\r\n", txrxcount); PAIR_CHARGERPIN_PRINT("EXTI SEND MODE.CAN NOT SET LOW %d\r\n", txrxcount); send_interrupt = 0; break; } else { nrf_gpio_pin_clear(PIN_CHARGING); PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms); send_interrupt = 1; } } if ((txrxcount == 0) && (send_interrupt == 0)) { nrf_gpio_pin_set(PIN_CHARGING); PAIR_CHARGERPIN_PRINT("set.%d\r\n", ms); now_bit = 1; } if ((txrxcount == 1.5 * BITWIDTH) && (send_interrupt == 0)) { nrf_gpio_pin_clear(PIN_CHARGING); PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms); now_bit = 0; lowbitdelay = 0; } if ((txrxcount >= (int)(2.5 * BITWIDTH) && (txrxcount - (int)(2.5 * BITWIDTH)) % BITWIDTH == 0) && (send_interrupt == 0)&&(txrxcount < BITWIDTH * (8 * DATA_LENGTH + 2.5) - 1)) { bytesindes = ((txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH) / 8; bitindex = 7 - ((txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH) % 8; nrf_gpio_pin_write(PIN_CHARGING, (txbuf[bytesindes] >> bitindex) & 0x01); lowbitdelay = 0; PAIR_CHARGERPIN_PRINT("write.%d %d\r\n", ((txbuf[bytesindes] >> bitindex) & 0x01), txrxcount); now_bit = (txbuf[bytesindes] >> bitindex) & 0x01; PAIR_CHARGERPIN_PRINT("%d %d %d %d %x\r\n", (txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH, bytesindes, bitindex, (txbuf[bytesindes] >> bitindex) & 0x01, txbuf[bytesindes]); } if (send_interrupt) { if (nrf_gpio_pin_read(PIN_CHARGING) == 0) { send_interrupt = 0; delay_ledon_count = DELAY_SEND; PAIR_CHARGERPIN_PRINT("delay_ledon_count = DELAY_SEND;.%d\r\n", txrxcount); PAIR_CHARGERPIN_PRINT("RX SEND MODE MAC START.%d\r\n", txrxcount); txrxcount = 0; break; } } if (txrxcount == BITWIDTH * (8 * DATA_LENGTH + 2.5) - 1) { nrf_gpio_pin_clear(PIN_CHARGING); PAIR_CHARGERPIN_PRINT("clearr.%d\r\n", ms); if (mFlash.isHost == 0) { delay_check_count = 1; } } if (delay_check_count != 0) { delay_check_count++; } if (txrxcount >= BITWIDTH * (8 * DATA_LENGTH + 2.5) - 1 + LOW_BIT_DELAY) { one_ms_status = 1; send_interrupt = 0; now_bit = 0; bit_width = 0; PAIR_CHARGERPIN_PRINT("SEDN DONE .%d pin one_ms_status %d\r\n", ms, nrf_gpio_pin_read(PIN_CHARGING));//IO口高电平就有错 break; } txrxcount++; } break; case 3: // rx if (now_bit == nrf_gpio_pin_read(PIN_CHARGING)) { bit_width++; } else { if (now_bit == 1) { now_bit = 0; if ((bit_width > 1.5 * BITWIDTH - 3) && (bit_width < 1.8 * BITWIDTH + 3)) { txrxcount = 1.5 * BITWIDTH; value = 30; delay_ledon_count = DELAY_SEND + 1.5 * BITWIDTH; PAIR_CHARGERPIN_PRINT("DELAY_SEND + 1.5 * BITWIDTHt .%d\r\n", txrxcount); PAIR_CHARGERPIN_PRINT("RX MODE restart .%d\r\n", ms); } } else { now_bit = 1; } bit_width = 0; } if (txrxcount >= (int)(3.5 * BITWIDTH) && (txrxcount - (int)(3.5 * BITWIDTH)) % BITWIDTH == 0) { bytesindes = ((txrxcount - (int)(3.5 * BITWIDTH)) / BITWIDTH) / 8; bitindex = 7 - ((txrxcount - (int)(3.5 * BITWIDTH)) / BITWIDTH) % 8; if (value > 0) { rxbuf[bytesindes] |= 0x01 << bitindex; } else { rxbuf[bytesindes] &= ~(0x01 << bitindex); } PAIR_CHARGERPIN_PRINT("%d %d %d %d %x\r\n", txrxcount, bytesindes, bitindex, value, rxbuf[bytesindes]); value = 0; nrf_gpio_pin_toggle(PIN_LED_RUN); } else { if (now_bit) { value++; } else { value--; } } if (txrxcount >= BITWIDTH * (8 * DATA_LENGTH + 2.5)) { unsigned char dataerrcheck = 0; unsigned char crc = 0; one_ms_status = 1; PAIR_CHARGERPIN_PRINT("RECEVIC DONE .%d\r\n", txrxcount); nrf_gpio_pin_clear(PIN_LED_RUN); PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms); for (int i = 0; i < DATA_LENGTH - 1; i++) { crc += rxbuf[i]; } for (int i = 0; i < DATA_LENGTH - 1; i++) { if (rxbuf[i] == 0) { dataerrcheck++; } } if (dataerrcheck == DATA_LENGTH - 1) { PAIR_CHARGERPIN_PRINT("CRC ERROR ALL 0x00.%x\r\n", crc); delay_ledon_count = 0; PAIR_CHARGERPIN_PRINT("delay_ledon_count 2= 0;\r\n", txrxcount); break; } dataerrcheck = 0; for (int i = 0; i < DATA_LENGTH - 1; i++) { if (rxbuf[i] == 0xff) { dataerrcheck++; } } if (dataerrcheck == DATA_LENGTH - 1) { PAIR_CHARGERPIN_PRINT("CRC ERROR ALL 0xFF.%x\r\n", crc); delay_ledon_count = 0; PAIR_CHARGERPIN_PRINT("delay_ledon_count 3= 0;\r\n", txrxcount); break; } if (crc == rxbuf[DATA_LENGTH - 1]) { if (mFlash.isHost) { delay_check_count = 1; //延时校验计数器 } else { delay_reply_count = 1; //延时回复计数器 } PAIR_CHARGERPIN_PRINT("---------------CRC SUCCESS .%x----------------\r\n", crc); } else { PAIR_CHARGERPIN_PRINT("CRC ERROR .%x\r\n", crc); delay_ledon_count = 0; PAIR_CHARGERPIN_PRINT("delay_ledon_count 4= 0;\r\n", txrxcount); } for (int i = 0; i < DATA_LENGTH; i++) { PAIR_CHARGERPIN_PRINT("rxbuf[%d].%x\r\n", i, rxbuf[i]); } } txrxcount++; break; case 4: if (nrf_gpio_pin_read(PIN_CHARGING)) { value++; } else { value--; } if (mFlash.isHost) { if (txrxcount == 0) { value = 0; nrf_gpio_pin_set(PIN_CHARGING); PAIR_CHARGERPIN_PRINT("set.%d\r\n", ms); } if (txrxcount == 3 * BITWIDTH) { nrf_gpio_pin_clear(PIN_CHARGING); PAIR_CHARGERPIN_PRINT("clear.%d\r\n", delay_ledon_count); } if (txrxcount >= 3 * BITWIDTH + LOW_BIT_DELAY) { value = 0; one_ms_status = 1; now_bit = 0; bit_width = 0; } } else { if (txrxcount == 0) { value = 0; } if (txrxcount > 3 * BITWIDTH) { if (value < 2 * BITWIDTH) { PAIR_CHARGERPIN_PRINT("CHECK ERROR .%d\r\n", txrxcount); delay_ledon_count = 0; PAIR_CHARGERPIN_PRINT("delay_ledon_count 5= 0;\r\n", txrxcount); } else { PAIR_CHARGERPIN_PRINT("CHECK DONE .%d\r\n", txrxcount); } value = 0; one_ms_status = 1; nrf_gpio_pin_clear(PIN_CHARGING); PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms); now_bit = 1; bit_width = 0; } } txrxcount++; break; } if (delay_ledon_count != 0) { delay_ledon_count++; } if (delay_ledon_count == DELAY_LEDON) { PAIR_CHARGERPIN_PRINT("LEDCOPEN.%d\r\n", ms); memcpy(savebuf, rxbuf, DATA_LENGTH - 1); if(_success_evt)_success_evt(savebuf,DATA_LENGTH - 1); } if ((delay_ledon_count > DELAY_LEDON) && (delay_ledon_count - DELAY_LEDON == LEDON_KEEP)) { delay_ledon_count = 0; PAIR_CHARGERPIN_PRINT("delay_ledon_count 6= 0;\r\n", txrxcount); PAIR_CHARGERPIN_PRINT("LEDCLOSE.%d\r\n", ms); } } //=========================================================================================================== struct { char statu; unsigned int ms; unsigned int bit_width; int now_bit; char cf_BIT_WIDTH; short tim_cun; int bitindex; int byteindex; int receive_byteslength; unsigned char *buff; unsigned short length; int value ; _Event _evt; }one_byte_ram; void one_byte_receive_init(unsigned char *recbuff,unsigned short len,_Event evt) { nrf_gpio_cfg( PIN_CHARGING, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_CONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_D0H1, NRF_GPIO_PIN_NOSENSE); nrf_gpio_cfg_watcher(PIN_CHARGING); one_byte_ram.statu = 1; nrf_gpio_pin_clear(PIN_CHARGING); one_byte_ram.now_bit = nrf_gpio_pin_read(PIN_CHARGING); //这里应该高电平进入 one_byte_ram.bit_width=0; one_byte_ram.cf_BIT_WIDTH=30; one_byte_ram.buff=recbuff; one_byte_ram.length=len; one_byte_ram._evt=evt; Process_Start(0, "null_pcs", null_pcs); Process_SetHoldOn(null_pcs, 1); TIME_Regist(one_byte_receive_pcs); } void one_byte_receive_uninit(void) { Process_Stop(null_pcs); TIME_UnRegist(one_byte_receive_pcs); PAIR_CHARGERPIN_PRINT("-------------------------------------------chargerpin_one_ms_uninit.%d\r\n", 1); } void one_byte_receive_pcs(void*t) { for(int i=1;i>0;i--) { switch(one_byte_ram.statu) { case 1://等待完整的开始信号 if (one_byte_ram.now_bit == nrf_gpio_pin_read(PIN_CHARGING)) { one_byte_ram.bit_width++; } else { //由高电平切换到低电平,下降信号,下降沿 if(one_byte_ram.now_bit==1) { one_byte_ram.now_bit=0; } else//上升沿 { if((one_byte_ram.bit_width > 1.5*one_byte_ram.cf_BIT_WIDTH-5)&&(one_byte_ram.bit_width < 1.5*one_byte_ram.cf_BIT_WIDTH+5)) { //已经收到1.5个位宽低电平的开始信号 one_byte_ram.tim_cun=-1.5*one_byte_ram.cf_BIT_WIDTH+1;//第一个数据位开始的时刻,该变量的值为0 one_byte_ram.statu=2; PAIR_CHARGERPIN_PRINT(" into receive mode %d;\r\n",one_byte_ram.tim_cun); } one_byte_ram.now_bit=1; } one_byte_ram.bit_width=0; } break; case 2: one_byte_ram.tim_cun++; if(one_byte_ram.tim_cun==0) { one_byte_ram.statu=3; PAIR_CHARGERPIN_PRINT("one_byte_ram.tim_cun==0;%d\r\n",nrf_gpio_pin_read(PIN_CHARGING)); i=2; one_byte_ram.value=0; } break; case 3: if(one_byte_ram.tim_cun>(one_byte_ram.length)*8*one_byte_ram.cf_BIT_WIDTH + 4*one_byte_ram.cf_BIT_WIDTH)//接收超时判断 { PAIR_CHARGERPIN_PRINT("one_byte_ram receive overtime; %d \r\n",(one_byte_ram.length+1)*one_byte_ram.cf_BIT_WIDTH); one_byte_ram.statu=1; } if((one_byte_ram.tim_cun % (one_byte_ram.cf_BIT_WIDTH-1)==0)&&(one_byte_ram.tim_cun>0)) { one_byte_ram.bitindex=one_byte_ram.tim_cun/one_byte_ram.cf_BIT_WIDTH; one_byte_ram.byteindex=one_byte_ram.bitindex/8; one_byte_ram.bitindex=one_byte_ram.bitindex%8; if(one_byte_ram.byteindex 0); if (one_byte_ram.value > 0) { one_byte_ram.buff[one_byte_ram.byteindex] |= 0x01 << one_byte_ram.bitindex; } else { one_byte_ram.buff[one_byte_ram.byteindex] &= ~(0x01 << one_byte_ram.bitindex); } } else if((one_byte_ram.bitindex==0)&&(one_byte_ram.byteindex==one_byte_ram.length)) { //数据位接收完成 PAIR_CHARGERPIN_PRINT(" data receive done length : %d \r\n data :",one_byte_ram.length); for (int k = 0; k < one_byte_ram.length; k++) { PAIR_CHARGERPIN_PRINT("%x ",one_byte_ram.buff[k]); } PAIR_CHARGERPIN_PRINT("\r\n"); } one_byte_ram.value=0; } else { if(nrf_gpio_pin_read(PIN_CHARGING)==0) { one_byte_ram.value--; } else { one_byte_ram.value++; } } if (one_byte_ram.now_bit == nrf_gpio_pin_read(PIN_CHARGING)) { one_byte_ram.bit_width++; } else { //由高电平切换到低电平,下降信号,下降沿 if(one_byte_ram.now_bit==1) { if((one_byte_ram.bit_width > 1.5*one_byte_ram.cf_BIT_WIDTH-5)&&(one_byte_ram.bit_width < 1.5*one_byte_ram.cf_BIT_WIDTH+5)) { if(one_byte_ram._evt)one_byte_ram._evt(one_byte_ram.buff,one_byte_ram.byteindex); PAIR_CHARGERPIN_PRINT(" one_byte_ram._evt \r\n"); PAIR_CHARGERPIN_PRINT(" receive done tim %d; %d\r\n",one_byte_ram.tim_cun,one_byte_ram.bit_width); one_byte_ram.statu=1; } one_byte_ram.now_bit=0; } else//上升沿 { one_byte_ram.now_bit=1; } one_byte_ram.bit_width=0; } one_byte_ram.tim_cun++; break; default: break; } } }