stack.c 5.51 KB
Newer Older
schneider's avatar
schneider committed
1
2
3
4
/*************************************************************************************************/
/*!
 *  \file
 *
5
 *  \brief  Stack initialization
schneider's avatar
schneider committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 *  Copyright (c) 2016-2017 ARM Ltd. All Rights Reserved.
 *  ARM Ltd. confidential and proprietary.
 *
 *  IMPORTANT.  Your use of this file is governed by a Software License Agreement
 *  ("Agreement") that must be accepted in order to download or otherwise receive a
 *  copy of this file.  You may not use or copy this file for any purpose other than
 *  as described in the Agreement.  If you do not agree to all of the terms of the
 *  Agreement do not use this file and delete all copies in your possession or control;
 *  if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
 *  to any use, copying or further distribution of this software.
 */
/*************************************************************************************************/

20
21
22
23
24
25
26
27
28
29
30
31
/*
 * This file initializes the different components of the whole BLE stack. This inlucdes link level,
 * HCI, security, etc...
 *
 * This file has been copied from lib/sdk/Applications/EvKitExamples/BLE_fit/stack_fit.c
 *
 * NOTE: Different stack_*.c files in the SDK initialize different components. We have to
 * be very carefull to intitialize all needed components here. Think e.g. SecRandInit() ...
 *
 * Many components are related to the role of the device. Different components need to be
 * initialized for central and peripheral roles.
 */
schneider's avatar
schneider committed
32
33
34
35
36
#include <stdio.h>
#include <string.h>
#include "wsf_types.h"
#include "wsf_os.h"
#include "util/bstream.h"
schneider's avatar
schneider committed
37
#include "ble_api.h"
schneider's avatar
schneider committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "hci_handler.h"
#include "dm_handler.h"
#include "l2c_handler.h"
#include "att_handler.h"
#include "smp_handler.h"
#include "l2c_api.h"
#include "att_api.h"
#include "smp_api.h"
#include "app_api.h"
#include "svc_dis.h"
#include "svc_core.h"
#include "sec_api.h"
#include "ll_init_api.h"

52
/* TODO: card10: Where does this number come from? Is there any documentation? */
schneider's avatar
schneider committed
53
54
#define LL_IMPL_REV             0x2303

55
/* TODO: card10: Where does this number come from? Is there any documentation? */
schneider's avatar
schneider committed
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
#define LL_MEMORY_FOOTPRINT     0xC152

uint8_t LlMem[LL_MEMORY_FOOTPRINT];

const LlRtCfg_t _ll_cfg = {
    /* Device */
    /*compId*/                  LL_COMP_ID_ARM,
    /*implRev*/                 LL_IMPL_REV,
    /*btVer*/                   LL_VER_BT_CORE_SPEC_5_0,
    /*_align32 */               0, // padding for alignment

    /* Advertiser */
    /*maxAdvSets*/              4, // 4 Extended Advertising Sets
    /*maxAdvReports*/           8,
    /*maxExtAdvDataLen*/        LL_MAX_ADV_DATA_LEN,
    /*defExtAdvDataFrag*/       64,
    /*auxDelayUsec*/            0,

    /* Scanner */
    /*maxScanReqRcvdEvt*/       4,
    /*maxExtScanDataLen*/       LL_MAX_ADV_DATA_LEN,

    /* Connection */
    /*maxConn*/                 2,
    /*numTxBufs*/               16,
    /*numRxBufs*/               16,
    /*maxAclLen*/               512,
    /*defTxPwrLvl*/             0,
    /*ceJitterUsec*/            0,

    /* DTM */
    /*dtmRxSyncMs*/             10000,

    /* PHY */
    /*phy2mSup*/                TRUE,
    /*phyCodedSup*/             TRUE,
    /*stableModIdxTxSup*/       FALSE,
    /*stableModIdxRxSup*/       FALSE
};

const BbRtCfg_t _bb_cfg = {
    /*clkPpm*/                  20,
    /*rfSetupDelayUsec*/        BB_RF_SETUP_DELAY_US,
    /*maxScanPeriodMsec*/       BB_MAX_SCAN_PERIOD_MS,
    /*schSetupDelayUsec*/       BB_SCH_SETUP_DELAY_US
};

/*************************************************************************************************/
/*!
 *  \brief      Initialize stack.
 *
 *  \return     None.
 */
/*************************************************************************************************/
110
void StackInit(void)
schneider's avatar
schneider committed
111
112
113
{
  wsfHandlerId_t handlerId;

114
/* card10: We do not use the SDMA HCI at the moment. The block below is not compiled. */
schneider's avatar
schneider committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#ifndef ENABLE_SDMA
  uint32_t memUsed;

  /* Initialize link layer. */
  LlInitRtCfg_t ll_init_cfg =
  {
      .pBbRtCfg     = &_bb_cfg,
      .wlSizeCfg    = 4,
      .rlSizeCfg    = 4,
      .plSizeCfg    = 4,
      .pLlRtCfg     = &_ll_cfg,
      .pFreeMem     = LlMem,
      .freeMemAvail = LL_MEMORY_FOOTPRINT
  };

#ifdef DATS_APP_USE_LEGACY_API
  memUsed = LlInitControllerExtInit(&ll_init_cfg);
#else /* DATS_APP_USE_LEGACY_API */
  memUsed = LlInitControllerExtInit(&ll_init_cfg);
#endif /* DATS_APP_USE_LEGACY_API */
  if(memUsed != LL_MEMORY_FOOTPRINT)
  {
137
138
      printf("Controller memory mismatch 0x%x != 0x%x\n", (unsigned int)memUsed, 
          (unsigned int)LL_MEMORY_FOOTPRINT);
schneider's avatar
schneider committed
139
140
141
  }
#endif

142
143
144
145
146
147
148
149

  /* card10:
   * These calls register a queue for callbacks in the OS abstraction
   * and then pass a handle down to modules which uses them to
   * internally handle callbacks.
   *
   * No idea why the modules don't call WsfOsSetNextHandler()
   * internally ... */
schneider's avatar
schneider committed
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
  handlerId = WsfOsSetNextHandler(HciHandler);
  HciHandlerInit(handlerId);

  SecInit();
  SecAesInit();
  SecCmacInit();
  SecEccInit();

  handlerId = WsfOsSetNextHandler(DmHandler);
  DmDevVsInit(0);
  DmAdvInit();
  DmConnInit();
  DmConnSlaveInit();
  DmSecInit();
  DmSecLescInit();
  DmPrivInit();
  DmPhyInit();
  DmHandlerInit(handlerId);

  handlerId = WsfOsSetNextHandler(L2cSlaveHandler);
  L2cSlaveHandlerInit(handlerId);
  L2cInit();
  L2cSlaveInit();

  handlerId = WsfOsSetNextHandler(AttHandler);
  AttHandlerInit(handlerId);
  AttsInit();
  AttsIndInit();

  handlerId = WsfOsSetNextHandler(SmpHandler);
  SmpHandlerInit(handlerId);
  SmprInit();
  SmprScInit();

184
185
  /*TODO card10: Probably want to adjust this */
  HciSetMaxRxAclLen(100);
schneider's avatar
schneider committed
186
}