-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_dats.c
402 lines (342 loc) · 12.3 KB
/
stack_dats.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*************************************************************************************************/
/*!
* \file
*
* \brief Stack initialization for dats.
*
* Copyright (c) 2016-2019 Arm Ltd. All Rights Reserved.
*
* Copyright (c) 2019 Packetcraft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*************************************************************************************************/
#include "app_api.h"
#include "app_terminal.h"
#include "app_ui.h"
#include "att_api.h"
#include "att_handler.h"
#include "dats_api.h"
#include "dm_handler.h"
#include "hci_core.h"
#include "hci_defs.h"
#include "hci_handler.h"
#include "l2c_api.h"
#include "l2c_handler.h"
#include "ll_init_api.h"
#include "pal_bb.h"
#include "pal_cfg.h"
#include "sec_api.h"
#include "smp_api.h"
#include "smp_handler.h"
#include "svc_core.h"
#include "svc_dis.h"
#include "util/bstream.h"
#include "wdxs/wdxs_api.h"
#include "wsf_assert.h"
#include "wsf_buf.h"
#include "wsf_bufio.h"
#include "wsf_cs.h"
#include "wsf_heap.h"
#include "wsf_msg.h"
#include "wsf_os.h"
#include "wsf_timer.h"
#include "wsf_trace.h"
#include "wsf_types.h"
#include "FreeRTOSConfig.h"
#include "wut.h"
#include "rtc.h"
#include "trimsir_regs.h"
#include "mxc_device.h"
#include "dats_api.h"
#include "wdxs/wdxs_api.h"
#include "pal_led.h"
#if defined(HCI_TR_EXACTLE) && (HCI_TR_EXACTLE == 1)
#include "ll_init_api.h"
#endif
/**************************************************************************************************
Macros
**************************************************************************************************/
/*! \brief UART TX buffer size */
#define PLATFORM_UART_TERMINAL_BUFFER_SIZE 2048U
#define DEFAULT_TX_POWER 0 /* dBm */
/**************************************************************************************************
Global Variables
**************************************************************************************************/
/*! \brief Pool runtime configuration. */
static wsfBufPoolDesc_t mainPoolDesc[] = {
{ 16, 8 },
{ 32, 4 },
{ 192, 8 },
{ 256, 16 }
};
#if defined(HCI_TR_EXACTLE) && (HCI_TR_EXACTLE == 1)
static LlRtCfg_t mainLlRtCfg;
#endif
volatile int wutTrimComplete;
/**************************************************************************************************
Functions
**************************************************************************************************/
/*************************************************************************************************/
/*!
* \brief Initialize stack.
*
* \return None.
*/
/*************************************************************************************************/
void StackInitDats(void)
{
wsfHandlerId_t handlerId;
SecInit();
SecAesInit();
SecCmacInit();
SecEccInit();
handlerId = WsfOsSetNextHandler(HciHandler);
HciHandlerInit(handlerId);
handlerId = WsfOsSetNextHandler(DmHandler);
DmDevVsInit(0);
DmConnInit();
DmAdvInit();
DmConnSlaveInit();
DmSecInit();
DmSecLescInit();
DmPrivInit();
DmHandlerInit(handlerId);
handlerId = WsfOsSetNextHandler(L2cSlaveHandler);
L2cSlaveHandlerInit(handlerId);
L2cInit();
L2cSlaveInit();
handlerId = WsfOsSetNextHandler(AttHandler);
AttHandlerInit(handlerId);
AttsInit();
AttsIndInit();
handlerId = WsfOsSetNextHandler(SmpHandler);
SmpHandlerInit(handlerId);
SmprInit();
SmprScInit();
HciSetMaxRxAclLen(256);
handlerId = WsfOsSetNextHandler(AppHandler);
AppHandlerInit(handlerId);
handlerId = WsfOsSetNextHandler(DatsHandler);
DatsHandlerInit(handlerId);
handlerId = WsfOsSetNextHandler(WdxsHandler);
WdxsHandlerInit(handlerId);
}
/*************************************************************************************************/
/*!
* \brief Initialize WSF.
*
* \return None.
*/
/*************************************************************************************************/
static void mainWsfInit(void)
{
#if defined(HCI_TR_EXACTLE) && (HCI_TR_EXACTLE == 1)
/* +12 for message headroom, + 2 event header, +255 maximum parameter length. */
const uint16_t maxRptBufSize = 12 + 2 + 255;
/* +12 for message headroom, +4 for header. */
const uint16_t aclBufSize = 12 + mainLlRtCfg.maxAclLen + 4 + BB_DATA_PDU_TAILROOM;
/* Adjust buffer allocation based on platform configuration. */
mainPoolDesc[2].len = maxRptBufSize;
mainPoolDesc[2].num = mainLlRtCfg.maxAdvReports;
mainPoolDesc[3].len = aclBufSize;
mainPoolDesc[3].num = mainLlRtCfg.numTxBufs + mainLlRtCfg.numRxBufs;
#endif
const uint8_t numPools = sizeof(mainPoolDesc) / sizeof(mainPoolDesc[0]);
uint16_t memUsed;
memUsed = WsfBufInit(numPools, mainPoolDesc);
WsfHeapAlloc(memUsed);
WsfOsInit();
WsfTimerInit();
#if (WSF_TOKEN_ENABLED == TRUE) || (WSF_TRACE_ENABLED == TRUE)
WsfTraceRegisterHandler(WsfBufIoWrite);
WsfTraceEnable(TRUE);
#endif
}
/*************************************************************************************************/
/*!
* \fn WUT_IRQHandler
*
* \brief WUY interrupt handler.
*
* \return None.
*/
/*************************************************************************************************/
void WUT_IRQHandler(void)
{
MXC_WUT_IntClear();
NVIC_ClearPendingIRQ(WUT_IRQn);
MXC_WUT_Handler();
}
/*************************************************************************************************/
/*!
* \fn wutTrimCb
*
* \brief Callback function for the WUT 32 kHz crystal trim.
*
* \param err Error code from the WUT driver.
*
* \return None.
*/
/*************************************************************************************************/
void wutTrimCb(int err)
{
if(err != E_NO_ERROR) {
APP_TRACE_INFO1("32 kHz trim error %d\n", err);
} else {
APP_TRACE_INFO1("32kHz trimmed to 0x%x", (MXC_TRIMSIR->rtc & MXC_F_TRIMSIR_RTC_RTCX1) >> MXC_F_TRIMSIR_RTC_RTCX1_POS);
}
wutTrimComplete = 1;
}
/*************************************************************************************************/
/*!
* \fn setAdvTxPower
*
* \brief Set the default advertising TX power.
*
* \return None.
*/
/*************************************************************************************************/
void setAdvTxPower(void)
{
LlSetAdvTxPower(DEFAULT_TX_POWER);
}
void setInterruptPriority(void)
{
/* Interrupts using FreeRTOS functions must have priorities between configMAX_PRIORITIES and
configMAX_SYSCALL_INTERRUPT_PRIORITY, lower priority number is higher priority */
/* Setup BLE hardware interrupt priorities */
NVIC_SetPriority(BTLE_TX_DONE_IRQn, (configMAX_PRIORITIES-2));
NVIC_SetPriority(BTLE_RX_RCVD_IRQn, (configMAX_PRIORITIES-2));
NVIC_SetPriority(BTLE_RX_ENG_DET_IRQn, (configMAX_PRIORITIES-2));
NVIC_SetPriority(BTLE_SFD_DET_IRQn, (configMAX_PRIORITIES-2));
NVIC_SetPriority(BTLE_SFD_TO_IRQn, (configMAX_PRIORITIES-2));
NVIC_SetPriority(BTLE_GP_EVENT_IRQn, (configMAX_PRIORITIES-2));
NVIC_SetPriority(BTLE_CFO_IRQn, (configMAX_PRIORITIES-2));
NVIC_SetPriority(BTLE_SIG_DET_IRQn, (configMAX_PRIORITIES-2));
NVIC_SetPriority(BTLE_AGC_EVENT_IRQn, (configMAX_PRIORITIES-2));
NVIC_SetPriority(BTLE_RFFE_SPIM_IRQn, (configMAX_PRIORITIES-2));
NVIC_SetPriority(BTLE_TX_AES_IRQn, (configMAX_PRIORITIES-2));
NVIC_SetPriority(BTLE_RX_AES_IRQn, (configMAX_PRIORITIES-2));
NVIC_SetPriority(BTLE_INV_APB_ADDR_IRQn, (configMAX_PRIORITIES-2));
NVIC_SetPriority(BTLE_IQ_DATA_VALID_IRQn, (configMAX_PRIORITIES-2));
/* Setup scheduler timer priorities */
NVIC_SetPriority(TMR0_IRQn, (configMAX_PRIORITIES-1));
NVIC_SetPriority(TMR1_IRQn, (configMAX_PRIORITIES-1));
NVIC_SetPriority(WUT_IRQn, configMAX_PRIORITIES-1);
/* Setup additional peripheral timer priorities */
NVIC_SetPriority(UART0_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(UART1_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(UART2_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(UART3_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA0_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA1_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA2_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA3_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA4_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA5_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA6_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA7_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA8_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA9_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA10_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA11_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA12_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA13_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA14_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(DMA15_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(GPIO0_IRQn, (configMAX_PRIORITIES-0));
NVIC_SetPriority(GPIO1_IRQn, (configMAX_PRIORITIES-0));
}
void trim32k(void)
{
/* Start the 32 MHz crystal and the BLE DBB counter to trim the 32 kHz crystal */
PalBbEnable();
NVIC_EnableIRQ(WUT_IRQn);
/* Output buffered square wave of 32 kHz clock to GPIO */
// MXC_RTC_SquareWaveStart(MXC_RTC_F_32KHZ);
/* Execute the trim procedure */
wutTrimComplete = 0;
if(MXC_WUT_TrimCrystalAsync(wutTrimCb) != E_NO_ERROR) {
APP_TRACE_INFO0("Error with 32k trim");
} else {
while(!wutTrimComplete) {}
}
/* Shutdown the 32 MHz crystal and the BLE DBB */
PalBbDisable();
}
/*************************************************************************************************/
/*!
* \brief Initialize BLE.
*
* \return None.
*/
/*************************************************************************************************/
void bleStartup(void)
{
#if defined(HCI_TR_EXACTLE) && (HCI_TR_EXACTLE == 1)
/* Configurations must be persistent. */
static BbRtCfg_t mainBbRtCfg;
PalBbLoadCfg((PalBbCfg_t *)&mainBbRtCfg);
LlGetDefaultRunTimeCfg(&mainLlRtCfg);
#if (BT_VER >= LL_VER_BT_CORE_SPEC_5_0)
/* Set 5.0 requirements. */
mainLlRtCfg.btVer = LL_VER_BT_CORE_SPEC_5_0;
#endif
PalCfgLoadData(PAL_CFG_ID_LL_PARAM, &mainLlRtCfg.maxAdvSets, sizeof(LlRtCfg_t) - 9);
#if (BT_VER >= LL_VER_BT_CORE_SPEC_5_0)
PalCfgLoadData(PAL_CFG_ID_BLE_PHY, &mainLlRtCfg.phy2mSup, 4);
#endif
/* Set the 32k sleep clock accuracy into one of the following bins, default is 20
HCI_CLOCK_500PPM
HCI_CLOCK_250PPM
HCI_CLOCK_150PPM
HCI_CLOCK_100PPM
HCI_CLOCK_75PPM
HCI_CLOCK_50PPM
HCI_CLOCK_30PPM
HCI_CLOCK_20PPM
*/
mainBbRtCfg.clkPpm = 20;
/* Set the default connection power level */
mainLlRtCfg.defTxPwrLvl = DEFAULT_TX_POWER;
#endif
uint32_t memUsed;
memUsed = WsfBufIoUartInit(WsfHeapGetFreeStartAddress(), PLATFORM_UART_TERMINAL_BUFFER_SIZE);
WsfHeapAlloc(memUsed);
mainWsfInit();
APP_TRACE_INFO1("memory is used %d \n", memUsed);
AppTerminalInit();
#if defined(HCI_TR_EXACTLE) && (HCI_TR_EXACTLE == 1)
LlInitRtCfg_t llCfg = {
.pBbRtCfg = &mainBbRtCfg,
.wlSizeCfg = 4,
.rlSizeCfg = 4,
.plSizeCfg = 4,
.pLlRtCfg = &mainLlRtCfg,
.pFreeMem = WsfHeapGetFreeStartAddress(),
.freeMemAvail = WsfHeapCountAvailable()
};
memUsed = LlInit(&llCfg);
WsfHeapAlloc(memUsed);
bdAddr_t bdAddr;
PalCfgLoadData(PAL_CFG_ID_BD_ADDR, bdAddr, sizeof(bdAddr_t));
LlSetBdAddr((uint8_t *)&bdAddr);
#endif
trim32k();
setInterruptPriority();
/* Disable Cordio stack usage of the LEDs */
PalLedDeInit();
StackInitDats();
DatsStart();
}