|
11 | 11 | #if defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(TARGET_NANO_RP2040_CONNECT)
|
12 | 12 | #include "HCINinaSpiTransport.h"
|
13 | 13 | #include "spi_drv.h"
|
14 |
| -#include "ble_drv.h" |
| 14 | + |
| 15 | +enum { |
| 16 | + BLE_BEGIN = 0x4A, |
| 17 | + BLE_END = 0x4B, |
| 18 | + BLE_AVAILABLE = 0x4C, |
| 19 | + BLE_PEEK = 0x4D, |
| 20 | + BLE_READ = 0x4E, |
| 21 | + BLE_WRITE = 0x4F, |
| 22 | +}; |
| 23 | + |
| 24 | +int BleDrv::bleBegin() { |
| 25 | + WAIT_FOR_SLAVE_SELECT(); |
| 26 | + |
| 27 | + SpiDrv::sendCmd(BLE_BEGIN, PARAM_NUMS_0); |
| 28 | + |
| 29 | + SpiDrv::spiSlaveDeselect(); |
| 30 | + //Wait the reply elaboration |
| 31 | + SpiDrv::waitForSlaveReady(); |
| 32 | + SpiDrv::spiSlaveSelect(); |
| 33 | + |
| 34 | + uint8_t len = 1; |
| 35 | + uint8_t result = 0; |
| 36 | + SpiDrv::waitResponseCmd(BLE_BEGIN, PARAM_NUMS_1, (uint8_t*)&result, &len); |
| 37 | + SpiDrv::spiSlaveDeselect(); |
| 38 | + |
| 39 | + return result == 0; |
| 40 | +} |
| 41 | + |
| 42 | +void BleDrv::bleEnd() { |
| 43 | + WAIT_FOR_SLAVE_SELECT(); |
| 44 | + |
| 45 | + SpiDrv::sendCmd(BLE_END, PARAM_NUMS_0); |
| 46 | + |
| 47 | + SpiDrv::spiSlaveDeselect(); |
| 48 | + //Wait the reply elaboration |
| 49 | + SpiDrv::waitForSlaveReady(); |
| 50 | + SpiDrv::spiSlaveSelect(); |
| 51 | + |
| 52 | + uint8_t len = 1; |
| 53 | + uint8_t result = 0; |
| 54 | + SpiDrv::waitResponseCmd(BLE_END, PARAM_NUMS_1, (uint8_t*)&result, &len); |
| 55 | + SpiDrv::spiSlaveDeselect(); |
| 56 | +} |
| 57 | + |
| 58 | +int BleDrv::bleAvailable() { |
| 59 | + WAIT_FOR_SLAVE_SELECT(); |
| 60 | + uint16_t result = 0; |
| 61 | + |
| 62 | + SpiDrv::sendCmd(BLE_AVAILABLE, PARAM_NUMS_0); |
| 63 | + |
| 64 | + SpiDrv::spiSlaveDeselect(); |
| 65 | + //Wait the reply elaboration |
| 66 | + SpiDrv::waitForSlaveReady(); |
| 67 | + SpiDrv::spiSlaveSelect(); |
| 68 | + |
| 69 | + uint8_t len = 2; |
| 70 | + SpiDrv::waitResponseCmd(BLE_AVAILABLE, PARAM_NUMS_1, (uint8_t*)&result, &len); |
| 71 | + SpiDrv::spiSlaveDeselect(); |
| 72 | + |
| 73 | + return result; |
| 74 | +} |
| 75 | + |
| 76 | +int BleDrv::bleRead(uint8_t data[], size_t length) { |
| 77 | + WAIT_FOR_SLAVE_SELECT(); |
| 78 | + |
| 79 | + SpiDrv::sendCmd(BLE_READ, PARAM_NUMS_1); |
| 80 | + |
| 81 | + int commandSize = 7; // 4 for the normal command length + 3 for the parameter |
| 82 | + uint16_t param = length; // TODO check length doesn't exceed 2^16 |
| 83 | + SpiDrv::sendParam((uint8_t*)¶m, sizeof(param), LAST_PARAM); |
| 84 | + |
| 85 | + // pad to multiple of 4 |
| 86 | + while (commandSize % 4 != 0) { |
| 87 | + SpiDrv::readChar(); |
| 88 | + commandSize++; |
| 89 | + } |
| 90 | + |
| 91 | + SpiDrv::spiSlaveDeselect(); |
| 92 | + //Wait the reply elaboration |
| 93 | + SpiDrv::waitForSlaveReady(); |
| 94 | + SpiDrv::spiSlaveSelect(); |
| 95 | + |
| 96 | + uint16_t res_len = 0; |
| 97 | + SpiDrv::waitResponseData16(BLE_READ, data, (uint16_t*)&res_len); |
| 98 | + |
| 99 | + SpiDrv::spiSlaveDeselect(); |
| 100 | + |
| 101 | + return res_len; |
| 102 | +} |
| 103 | + |
| 104 | +int BleDrv::blePeek(uint8_t data[], size_t length) { |
| 105 | + WAIT_FOR_SLAVE_SELECT(); |
| 106 | + |
| 107 | + SpiDrv::sendCmd(BLE_PEEK, PARAM_NUMS_1); |
| 108 | + |
| 109 | + int commandSize = 7; // 4 for the normal command length + 3 for the parameter |
| 110 | + uint16_t param = length; // TODO check length doesn't exceed 2^16 |
| 111 | + SpiDrv::sendParam((uint8_t*)¶m, sizeof(param), LAST_PARAM); |
| 112 | + |
| 113 | + // pad to multiple of 4 |
| 114 | + while (commandSize % 4 != 0) { |
| 115 | + SpiDrv::readChar(); |
| 116 | + commandSize++; |
| 117 | + } |
| 118 | + |
| 119 | + SpiDrv::spiSlaveDeselect(); |
| 120 | + //Wait the reply elaboration |
| 121 | + SpiDrv::waitForSlaveReady(); |
| 122 | + SpiDrv::spiSlaveSelect(); |
| 123 | + |
| 124 | + uint16_t res_len = 0; |
| 125 | + SpiDrv::waitResponseData16(BLE_READ, data, (uint16_t*)&res_len); |
| 126 | + |
| 127 | + SpiDrv::spiSlaveDeselect(); |
| 128 | + |
| 129 | + return res_len; |
| 130 | +} |
| 131 | + |
| 132 | +size_t BleDrv::bleWrite(const uint8_t* data, size_t len) { |
| 133 | + WAIT_FOR_SLAVE_SELECT(); |
| 134 | + |
| 135 | + int commandSize = 4; |
| 136 | + SpiDrv::sendCmd(BLE_WRITE, PARAM_NUMS_1); |
| 137 | + |
| 138 | + SpiDrv::sendBuffer((uint8_t*)data, len, LAST_PARAM); |
| 139 | + commandSize += len+2; |
| 140 | + |
| 141 | + // pad to multiple of 4 |
| 142 | + while (commandSize % 4 != 0) { |
| 143 | + SpiDrv::readChar(); |
| 144 | + commandSize++; |
| 145 | + } |
| 146 | + |
| 147 | + SpiDrv::spiSlaveDeselect(); |
| 148 | + //Wait the reply elaboration |
| 149 | + SpiDrv::waitForSlaveReady(); |
| 150 | + SpiDrv::spiSlaveSelect(); |
| 151 | + |
| 152 | + uint8_t res_len = 1; |
| 153 | + uint16_t res = 0; |
| 154 | + SpiDrv::waitResponseCmd(BLE_WRITE, PARAM_NUMS_1, (uint8_t*)&res, &res_len); |
| 155 | + SpiDrv::spiSlaveDeselect(); |
| 156 | + |
| 157 | + return res; |
| 158 | +} |
15 | 159 |
|
16 | 160 | int HCINinaSpiTransportClass::begin()
|
17 | 161 | {
|
|
0 commit comments