| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- #include "nrf_drv_spis.h"
- #include "ble_gap.h"
- #include "type.h"
- #include "queue.h"
- #include "ver.h"
- #include "crc.h"
- #include "spi.h"
- #include "flash.h"
- #include "nrf_delay.h"
- #include "main.h"
- #include "md5.h"
- #include "ble_dtm.h"
- #include "proc.h"
- #include "sys_lib.h"
- //DTM TEST
- extern uint8_t dtm_flag;
- uint8_t ble_cfg_flag;
- uint8_t stack_flag = 1; // 1:open 0:close
- extern volatile bool spis_eint_on;
- queue_process_func g_s_ble_send_func, g_s_spi_send_func;
- void spis_process_callback_register(queue_process_func ble_send_func, queue_process_func spi_send_func)
- {
- if((NULL != ble_send_func) && (NULL != spi_send_func))
- {
- g_s_ble_send_func = ble_send_func;
- g_s_spi_send_func = spi_send_func;
- }
- }
- void spis_transfer_data(uint8_t cmd, uint8_t* src, uint16_t len)
- {
- uint8_t tx_buf[MAX_BUF] = {0};
- pack_header_t* pHeader = (pack_header_t*)tx_buf;
- uint16_t header_len = sizeof(pack_header_t);
- uint16_t crc;
- uint16_t pack_len = len;
- spis_pin_reset();
-
- memset(tx_buf, 0, MAX_BUF);
- pHeader->ident[0] = 0xF1;
- pHeader->ident[1] = 0xF1;
- pHeader->len = 3 + len; //包长度+指令ID+数据内容+校验码
- pHeader->message_code = cmd;
- if((len>0)&&(len<=MAX_BUF-header_len-2))
- {
- memcpy(&tx_buf[header_len], src, len);
- }
- crc = make_crc(&tx_buf[2], header_len-2+pack_len);
- tx_buf[header_len + pack_len] = (crc >> 8) & 0xFF;
- tx_buf[header_len + pack_len + 1] = crc & 0xFF;
- pack_len = header_len + pack_len + 2; //头+数据内容+crc
- DLOG("spi_tx_data:%s,len:%d", SYS_LibHex2Str(tx_buf, pack_len), pack_len);
- g_s_spi_send_func(tx_buf, pack_len);
- }
- void spis_version_report(void)
- {
- uint8_t ver = get_version();
- spis_transfer_data(BLE_CMD_VER, &ver, 1);
- }
- void spis_ble_heart(void)
- {
- spis_transfer_data(BLE_CMD_HEART, NULL, 0);
- }
- void spis_ota_resend(void)
- {
- }
- void small_big_transfer16(uint16_t *data)
- {
- uint8_t *p_data = (uint8_t *)data;
- uint8_t a = p_data[0];
- uint8_t b = p_data[1];
- p_data[0] = b;
- p_data[1] = a;
- }
- static uint8_t tx_buf[MAX_BUF];
- void spis_process_data(uint8_t* data, uint16_t length)
- {
- pack_header_t* pHeader = (pack_header_t*)data;
- uint16_t check_crc = 0, pack_crc;
- uint8_t pack_len;
- uint8_t pack_cmd;
- uint8_t header_len = sizeof(pack_header_t);
- uint8_t crc_index;
- memset(tx_buf, 0, MAX_BUF);
- pack_cmd = pHeader->message_code;
- pack_len = pHeader->len;
- crc_index = header_len-2+pack_len-3;
-
- check_crc = make_crc(&data[2], crc_index);
- memcpy(&pack_crc, &data[crc_index+2], 2);
- small_big_transfer16(&pack_crc);
- if(check_crc != pack_crc)
- {
- return;
- }
- if(pHeader->message_code == BLE_CMD_VER)
- {
- }
- else if(pack_cmd == BLE_CMD_TRANSFER)
- {
- DLOG("cmd_transfer:%s,len:%d", SYS_LibHex2Str(&data[header_len], pack_len-3), pack_len-3);
- memcpy(tx_buf, &data[header_len], pack_len-3);
- g_s_ble_send_func(tx_buf, pack_len-3);
- }
- else if(pHeader->message_code == BLE_CMD_CFG)
- {
- memcpy(tx_buf, &data[header_len], pack_len-3);
- ble_configure_all(tx_buf, pack_len-3);
- }
- else if(pHeader->message_code == BLE_CMD_DTM_STACK_CHECK)
- {
- if(ble_cfg_flag)
- {
- spis_transfer_data(BLE_CMD_DTM_STACK_CHECK, NULL, 0);
- }
- }
- else if(pHeader->message_code == BLE_CMD_DTM_STACK_CLOSE)
- {
- dtm_flag = 1;
- }
- else if(pHeader->message_code == BLE_CMD_DTM_TX_ON)
- {
- dtm_flag = 2;
- }
- else if(pHeader->message_code == BLE_CMD_DTM_TX_OFF)
- {
- dtm_flag = 3;
- }
- else if(pHeader->message_code == BLE_CMD_HEART)
- {
- spis_transfer_data(BLE_CMD_HEART, NULL, 0);
- }
- else
- {
- NRF_LOG_PRINTF("SPI Process Non Data \r\n");
- }
- }
- uint16_t spi_tlv_get_value(uint16_t id, void* input,uint16_t inputLen, void* output)
- {
- uint8_t* tmp;
- uint16_t curr = 0;
- uint16_t len = 0;
- uint8_t* end;
- tmp = input;
- end = (uint8_t*)input + inputLen;
- while (tmp + 4 <= end)
- {
- curr = spi_byte2short(tmp);
- tmp += 2;
- len = spi_byte2short(tmp);
- tmp += 2;
- if( tmp + len > end )
- return 0;
- if (curr == id)
- {
- memcpy(output, tmp, len);
- return len;
- }
- tmp += len;
- }
- return 0;
- }
- uint16_t spi_byte2short(uint8_t* bytes)
- {
- uint16_t result = bytes[1] & 0xFF;
- result |= ((bytes[0] << 8) & 0xFF00);
- return result;
- }
|