dtm.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "ble_conn_params.h"
  2. #include "softdevice_handler.h"
  3. #include "app_error.h"
  4. #include "nrf_delay.h"
  5. #include "type.h"
  6. #include "spi.h"
  7. #include "queue.h"
  8. #include "ble_dtm.h"
  9. #include "proc.h"
  10. #include "dtm.h"
  11. extern uint8_t dtm_flag;
  12. uint16_t dtm_command = 0x8004; //command_code:freqency:length:payload in 2:6:6:2 bits.
  13. static void ble_stack_disable(void)
  14. {
  15. uint32_t err_code;
  16. // err_code = sd_nvic_DisableIRQ(SWI2_IRQn);
  17. // APP_ERROR_CHECK(err_code);
  18. err_code = softdevice_handler_sd_disable();
  19. APP_ERROR_CHECK(err_code);
  20. }
  21. void reset_prepare(void)
  22. {
  23. uint32_t err_code;
  24. // err_code = sd_ble_gap_disconnect(g_s_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  25. // APP_ERROR_CHECK(err_code);
  26. err_code = ble_conn_params_stop();
  27. APP_ERROR_CHECK(err_code);
  28. //????
  29. sd_ble_gap_adv_stop();
  30. //disable???
  31. ble_stack_disable();
  32. nrf_delay_ms(1000);
  33. }
  34. static uint32_t dtm_cmd_put(uint16_t command)
  35. {
  36. dtm_cmd_t command_code = (command >> 14) & 0x03;
  37. dtm_freq_t freq = (command >> 8) & 0x3F;
  38. uint32_t length = (command >> 2) & 0x3F;
  39. dtm_pkt_type_t payload = command & 0x03;
  40. // Check for Vendor Specific payload.
  41. if (payload == 0x03)
  42. {
  43. /* Note that in a HCI adaption layer, as well as in the DTM PDU format,
  44. the value 0x03 is a distinct bit pattern (PRBS15). Even though BLE does not
  45. support PRBS15, this implementation re-maps 0x03 to DTM_PKT_VENDORSPECIFIC,
  46. to avoid the risk of confusion, should the code be extended to greater coverage.
  47. */
  48. payload = DTM_PKT_VENDORSPECIFIC;
  49. }
  50. return dtm_cmd(command_code, freq, length, payload);
  51. }
  52. void dtm_process_data(queue_list_t* queue_send, queue_list_t* queue_recv)
  53. {
  54. uint32_t dtm_error_code;
  55. reset_prepare();
  56. dtm_error_code = dtm_init();
  57. if (dtm_error_code != DTM_SUCCESS)
  58. {
  59. spis_transfer_data(BLE_CMD_DTM_STACK_CLOSE, NULL, 0);
  60. }
  61. else
  62. {
  63. spis_transfer_data(BLE_CMD_DTM_STACK_CLOSE, NULL, 0);
  64. }
  65. while(QUEUE_MSG_NOT_EMPTY(queue_send))
  66. {
  67. QUEUE_MSG_PROCESS(queue_send,spis_send_data);
  68. }
  69. for(;;)
  70. {
  71. QUEUE_MSG_PROCESS(queue_recv,spis_process_data);
  72. if(dtm_flag == 2)
  73. {
  74. dtm_flag = 0;
  75. if (dtm_cmd_put(dtm_command) != DTM_SUCCESS)
  76. {
  77. spis_transfer_data(BLE_CMD_DTM_TX_ON, NULL, 0);
  78. }
  79. else
  80. {
  81. spis_transfer_data(BLE_CMD_DTM_TX_ON, NULL, 0);
  82. }
  83. }
  84. else if(dtm_flag == 3) //dtm tx off
  85. {
  86. dtm_flag = 0;
  87. if (dtm_cmd_put(0xC000) != DTM_SUCCESS)
  88. {
  89. spis_transfer_data(BLE_CMD_DTM_TX_OFF, NULL, 0);
  90. }
  91. else
  92. {
  93. spis_transfer_data(BLE_CMD_DTM_TX_OFF, NULL, 0);
  94. }
  95. }
  96. QUEUE_MSG_PROCESS(queue_send,spis_send_data);
  97. dtm_wait();
  98. }
  99. }