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