1 /*****************************************************************************
2 * Copyright (c) 2019, Nations Technologies Inc.
3 *
4 * All rights reserved.
5 * ****************************************************************************
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * - Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the disclaimer below.
12 *
13 * Nations' name may not be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
19 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * ****************************************************************************/
27
28 /**
29 * @file n32wb452_ble_api.c
30 * @author Nations
31 * @version v1.0.1
32 *
33 * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
34 */
35 #include "n32wb452.h"
36 #include "Eif_uart.h"
37 #include "user.h"
38 #include "n32wb452_ble_api.h"
39 #include "n32wb452_data_fifo.h"
40 #include "n32wb452_log_level.h"
41 #include "app_task.h"
42 #include "eif_spi.h"
43 #include "interface.h"
44 #include "app.h"
45 #include "app_user.h"
46 #include "rwip.h"
47 #include "ke.h"
48 #include "ble_monitor.h"
49 #include "main.h"
50
51 #define XOTRIM 0
52
53 #ifdef N32WB452_BT_API
54
55 //static uint32_t g_overtime_count;
56 //static uint32_t g_timer_count_flag;
57 bt_attr_param * g_bt_init = NULL;
58 uint32_t g_bt_start_finished_flag = 0;
59 uint32_t g_connnet_start;
60 bt_event_callback_handler_t g_pcallback = NULL;
61
62
63 void eif_timer_init(void);
64 void bt_state_get(void);
65
bt_handler(void)66 void bt_handler(void)
67 {
68 eif_spi_recv_irq_handler();
69 }
70
71
72
73 /***************************************************************************************
74 declaraction: int32_t bt_ware_init(bt_attr_param *pinit, bt_event_callback_handler_t pcallback)
75 function : 初始化蓝牙模??
76 parameter : 蓝牙初始化数??
77 parameter : 蓝牙事件回调函数
78 return : NONE
79 ***************************************************************************************/
bt_ware_init(bt_attr_param * pinit,bt_event_callback_handler_t pcallback)80 int32_t bt_ware_init(bt_attr_param *pinit, bt_event_callback_handler_t pcallback)
81 {
82 int32_t ret;
83 // uint32_t ble_init_status = 0;
84 g_bt_init = pinit;
85
86 ret = ble_interface_init();
87 if (ret != E_OK) {
88 return BT_RET_OPERATED_FAILDED_ERR;
89 }
90
91 //2
92 ret = ble_hardware_init();
93 if (ret != E_OK) {
94 return BT_RET_OPERATED_FAILDED_ERR;
95 }
96
97 //3
98 ret = ble_initdata_down();
99 if (ret != E_OK) {
100 return BT_RET_OPERATED_FAILDED_ERR;
101 }
102
103 #if (XOTRIM)
104 //--crystal frequency accuracy calibration--
105 eif_delay_ms(300);
106
107 //01 03 fd 08 04 00 00 43 ff ff ff ff
108 uint8_t cmd_1[12] ={0x01, 0x03, 0xfd,0x08, 0x04, 0x00, 0x00, 0x43, 0xff, 0xff , 0xff, 0xff};
109 eif_spi_send_bytes(cmd_1, 12);
110 eif_spi_recv_bytes(cmd_1, 7) ; //no need to return
111 //01 03 fd 08 00 00 40 52 0A 00 03 00
112 uint8_t cmd_2[12] ={0x01, 0x03, 0xfd,0x08, 0x00, 0x00, 0x40, 0x52, 0x0a, 0x00, 0x03, 0x00};
113 eif_spi_send_bytes(cmd_2, 12);
114 eif_spi_recv_bytes(cmd_2, 7) ; //no need to return
115 while (1);
116 #endif
117
118 //4
119 ret = ble_host_init();
120 if (ret != E_OK) {
121 return BT_RET_OPERATED_FAILDED_ERR;
122 }
123
124 g_pcallback = pcallback;
125
126 gBT_STS = BT_INITIALIZED;
127
128 return BT_RET_SUCCESS;
129 }
130
131
132 /***************************************************************************************
133 declaraction: void bt_run_thread(void)
134 function : 蓝牙运行的主线程,用于蓝牙接收、发送、通知等处??
135 parameter : void
136 return : NONE
137 ***************************************************************************************/
bt_run_thread(void)138 void bt_run_thread(void)
139 {
140 BT_handle();
141 }
142
143
144 /***************************************************************************************
145 declaraction: uint32_t bt_rcv_data(uint8_t *data, uint32_t size, uint32_t character)
146 function : 根据BT_EVENT_RCV_DATA事件,读取相应服务特征字的数??
147 parameter : uint8_t *data 接收数据buf
148 parameter : uint32_t size buf大小(最大不超过512Bytes)
149 parameter : uint32_t character 发生数据通知对应的特征字ID
150 return : 返回实际读取大小
151 ***************************************************************************************/
bt_rcv_data(uint8_t * data,uint32_t size,uint32_t character)152 uint32_t bt_rcv_data(uint8_t *data, uint32_t size, uint32_t character)
153 {
154 uint32_t read_size;
155
156 read_size = fifo_read(data, &size);
157 return read_size;
158 }
159
160
161 /***************************************************************************************
162 declaraction: uint32_t bt_rcv_data(const uint8_t *data, uint32_t size, uint32_t character)
163 function : 发送数据到对应的特征字
164 parameter : uint8_t *data 发送数据buf
165 parameter : uint32_t 发送数据大??最大不超过512Bytes)
166 parameter : uint32_t character 对应特征字ID
167 return : 返回值0
168 ***************************************************************************************/
bt_snd_data(const uint8_t * data,uint32_t size,uint32_t character)169 uint32_t bt_snd_data(const uint8_t *data, uint32_t size, uint32_t character)
170 {
171 // if (g_connnet_start) {
172 // if (data && size && character) {
173 app_user_data_notify(size, (uint8_t *)data, character);
174 // return size;
175 // }
176 // }
177 return 0;
178 }
179
180 /***************************************************************************************
181 declaraction: void bt_disconnect(void)
182 function : slave主动断开蓝牙连接
183 parameter : NULL
184 return : NULL
185 ***************************************************************************************/
bt_disconnect(void)186 void bt_disconnect(void)
187 {
188 appm_disconnect();
189 }
190
191 /***************************************************************************************
192 declaraction: uint32_t bluetooth_state_get(void)
193 function : 获取蓝牙连接状态,不对外开放
194 parameter : NULL
195 return : NULL
196 ***************************************************************************************/
bt_state_get(void)197 void bt_state_get(void)
198 {
199 static ke_state_t ble_state_last;
200 ke_state_t ble_state;
201 // bt_event_enum bt_status = BT_EVENT_DISCONNECTD;
202
203 ble_state = ke_state_get(TASK_APP);
204
205
206 switch (ble_state)
207 {
208 case APPM_CREATE_DB:
209 break;
210 case APPM_ADVERTISING:
211 if (ble_state_last == APPM_CONNECTED)
212 {
213 if (g_pcallback) {
214 g_pcallback(BT_EVENT_DISCONNECTD, NULL, 0, 0);
215 }
216 g_connnet_start = 0;
217 }
218 break;
219 case APPM_CONNECTED:
220 if (ble_state_last == APPM_ADVERTISING)
221 {
222 if (g_pcallback) {
223 g_pcallback(BT_EVENT_CONNECTED, NULL, 0, 0);
224 }
225 g_connnet_start = 1;
226 }
227 break;
228 case APPM_START_ENC:
229 break;
230 case APPM_ENCRYPTED:
231 break;
232 case APPM_INIT:
233 break;
234 }
235 ble_state_last = ble_state;
236 }
237
238 /*contain standard bluetooth version and frimware version*/
239 #define BT_VERSION_INFO "V4.2_1127"
240
241
242 /*==============================================================================
243 BT_Init
244 <Description>
245 This function is to initialize bluetooth stack
246 ==============================================================================*/
BT_init(void)247 void BT_init(void)
248 {
249 rwip_init(RESET_NO_ERROR);
250 }
251
252 /*==============================================================================
253 is_bt_busy
254
255 <Description>
256 This function is to get the bt kernel processing is ongoing or not.
257 if not onging, then allow to sleep, otherwise not allow to sleep.
258
259 <return>
260 0: bt kernel processing is not ongoing
261 1: bt kernel processing is ongoing
262 ==============================================================================*/
is_bt_busy(void)263 bool is_bt_busy(void)
264 {
265 return (!ke_sleep_check());
266 }
267
268
269 /*==============================================================================
270 BT_handle
271
272 <Description>
273 This function is schedule the BT tasks
274 ==============================================================================*/
BT_handle(void)275 void BT_handle(void)
276 {
277 rwip_schedule();
278 }
279
280
281 /*==============================================================================
282 BT_get_version
283
284 <Description>
285 This function is to get version of BT API.
286
287 <parameter out>
288 version: store the data of the BT API version, size 10 byte;
289 ==============================================================================*/
BT_get_version(uint8_t * version)290 void BT_get_version(uint8_t * version)
291 {
292 if (version == NULL)
293 {
294 return;
295 }
296 memcpy(version, BT_VERSION_INFO, sizeof(BT_VERSION_INFO));
297 }
298
299 #endif
300
301