Welcome to mirror list, hosted at ThFree Co, Russian Federation.

app_ble_lld.c « App « STM32_WPAN « BLE_LLD_Lowpower « BLE_LLD « Applications « P-NUCLEO-WB55.Nucleo « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ea1573ea1f10345796bddc71cd1acabc99a3a91e (plain)
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
/**
 ******************************************************************************
  * File Name          : app_ble_lld.c
  * Description        : application utilities.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */

/**
 * This file provides low level utilities for application:
 *  - IPCC for communication with radio MCU
 *  - UART management
 *  - error handling
 */

/* Includes ------------------------------------------------------------------*/
#include <stdbool.h>
#include "app_common.h"
#include "utilities_common.h"
#include "app_entry.h"
#include "dbg_trace.h"
#include "tl.h"
#include "shci.h"
#include "stm_logging.h"
#include "stm32_lpm.h"
#include "stm32_seq.h"
#include "gpio_lld.h"
#include "stm_queue.h"
#include "ble_lld.h"
#include "app_ble_lld.h"

/* Private includes ----------------------------------------------------------*/

/* Private typedef -----------------------------------------------------------*/
/*
 *  List of all errors tracked by the application
 *  running on M4. Some of these errors may be fatal
 *  or just warnings
 */
typedef enum
{
  ERR_BLE_LLD_SET_STATE_CB,
  ERR_BLE_LLD_ERASE_PERSISTENT_INFO,
  ERR_BLE_LLD_CHECK_WIRELESS
} ErrAppBleLldIdEnum_t;

/* Private defines -----------------------------------------------------------*/
#define UART_BUFFER_SIZE        64
#define TX_BUFFER_SIZE          268
#define UART_TX_CHUNK_SIZE      16
#define UART_LINE_END           "\r\n"

/* Private macros ------------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
static void uartTxSendChunk(void);
static void uartRxCpltCallback(void);
static void m0CmdProcess(void);

/* Private variables ---------------------------------------------------------*/
static queue_t uartTxBuf;
static uint8_t uartTxBufData[TX_BUFFER_SIZE];

static bool txBusy = false;

static char uartRxBuf;
static void(*uartRxUserCb)(char);

// IPCC configuration
PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static TL_BLE_LLD_Config_t BleLldConfigBuffer;

// Shared memory used by IPCC to send/receive messages to/from M0
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t BleLldM0CmdPacket;
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t BleLldCmdRspPacket;

/* Shared memory used to send/receive data and parameters to/from M0 because
 IPCC messages have a limited size */
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static msg_BLE_LLD_t bleparam_BLE_LLD_Packet;

// Shared buffers for packet transmission and reception, separate buffers are needed because radio
PLACE_IN_SECTION("MB_MEM2") static ipBLE_lld_txrxdata_Type txBuffer;
PLACE_IN_SECTION("MB_MEM2") static ipBLE_lld_txrxdata_Type rxBuffer;


/* Functions Definition ------------------------------------------------------*/

void APP_BLE_LLD_Init(void)
{
  uint32_t devId = HAL_GetDEVID();
  uint32_t revId = HAL_GetREVID();
  uint8_t  param[8];
  SHCI_CmdStatus_t LldTestsInitStatus;

  /* Initialize transport layer */
  BleLldConfigBuffer.p_BleLldCmdRspBuffer = (uint8_t*)&BleLldCmdRspPacket;
  BleLldConfigBuffer.p_BleLldM0CmdBuffer = (uint8_t*)&BleLldM0CmdPacket;
  TL_BLE_LLD_Init(&BleLldConfigBuffer);

  APP_BLE_LLD_Init_UART();

  /* Send LLD tests start information to UART */
  uartWrite("");
  uartWrite("================================");
  uartWrite("RF BLE LLD");
  uartWrite("================================");
#if (CFG_DEBUGGER_SUPPORTED == 0U)
  uartWrite("Debugger de-activated");
#endif
#if (( CFG_DEBUG_TRACE_FULL == 0 ) && ( CFG_DEBUG_TRACE_LIGHT == 0 ))
  uartWrite("Trace is de-activated");
#endif

  APP_DBG("Test appli initialized on M4, waiting for M0 initialization");

  /* Send start cmd to M0 (with device and revision ID as parameters */
  memcpy(&param[0], &devId, sizeof(devId));
  memcpy(&param[4], &revId, sizeof(revId));
  LldTestsInitStatus = SHCI_C2_BLE_LLD_Init(sizeof(param), param);
  if(LldTestsInitStatus != SHCI_Success){
    APP_DBG("!! ERROR during M0 init !!");
  }else{
    APP_DBG("M0 initialized");
  }

  UTIL_SEQ_RegTask( 1<<CFG_TASK_CMD_FROM_M0_TO_M4, UTIL_SEQ_RFU, m0CmdProcess);

  BLE_LLD_PRX_Init(&bleparam_BLE_LLD_Packet.params,
                   &txBuffer,
                   &rxBuffer,
                   APP_BLE_LLD_SendCmdM0);
}

/**
  * @brief  Warn the user that an error has occurred.In this case,
  *         the LEDs on the Board will start blinking.
  * @param  ErrId :
  * @param  ErrCode
  * @retval None
  */
void APP_BLE_LLD_Error(uint32_t ErrId, uint32_t ErrCode)
{
  char *msg;
  switch(ErrId)
  {
    case ERR_BLE_LLD_SET_STATE_CB:          msg = "ERROR: ERR_BLE_LLD_SET_STATE_CB";           break;
    case ERR_BLE_LLD_ERASE_PERSISTENT_INFO: msg = "ERROR: ERR_BLE_BLE_LLD_ERASE_PERSISTENT_INFO"; break;
    case ERR_BLE_LLD_CHECK_WIRELESS:        msg = "ERROR: ERR_BLE_LLD_CHECK_WIRELESS ";       break;
    default:                                msg = "ERROR Unknown ";                            break;
  }
  APP_DBG("**** Fatal error = %s (Err = %d)", msg, ErrCode);
  while(true)
  {
    BSP_LED_Toggle(LED1);
    HAL_Delay(500U);
    BSP_LED_Toggle(LED2);
    HAL_Delay(500U);
    BSP_LED_Toggle(LED3);
    HAL_Delay(500U);
  }
}

/**
 * @brief Check if the Coprocessor Wireless Firmware loaded supports Thread
 *        and display associated informations
 * @param  None
 * @retval None
 */
void CheckWirelessFirmwareInfo(void)
{
  WirelessFwInfo_t  wireless_info_instance;
  WirelessFwInfo_t* p_wireless_info = &wireless_info_instance;
  if (SHCI_GetWirelessFwInfo(p_wireless_info) != SHCI_Success)
  {
    APP_BLE_LLD_Error(ERR_BLE_LLD_CHECK_WIRELESS, 0);
  }
  else
  {
    APP_DBG("**********************************************************");
    APP_DBG("Loaded M0 TEST FW info:");
    switch(p_wireless_info->StackType)
    {
    case INFO_STACK_TYPE_BLE_PHY_VALID :
      APP_DBG("  M0 FW Type: BLE and radio PHY validation");
      break;

    default :
      APP_DBG("  ERROR: incompatible firmware");
      APP_BLE_LLD_Error(ERR_BLE_LLD_CHECK_WIRELESS, 0);
      break;
    }
    APP_DBG("  M0 FW VERSION: v%d.%d.%d", p_wireless_info->VersionMajor, p_wireless_info->VersionMinor, p_wireless_info->VersionSub);
    APP_DBG("**********************************************************");
  }
}

/*************************************************************
 *
 * LOCAL FUNCTIONS
 *
 *************************************************************/

/*************************************************************
 *
 * WRAP FUNCTIONS
 *
 *************************************************************/
/**
 * @brief Perform initialization of UART.
 * @param  None
 * @retval None
 */
void APP_BLE_LLD_Init_UART(void)
{
#ifdef CFG_UART
  MX_UART_Init(CFG_UART);
#endif

  CircularQueue_Init(&uartTxBuf,
                     uartTxBufData,
                     sizeof(uartTxBufData),
                     sizeof(char),
                     CIRCULAR_QUEUE_NO_FLAG);
  txBusy = false;
}

/**
 * @brief Perform de-initialization of UART.
 * @param  None
 * @retval None
 */
void APP_BLE_LLD_DeInit_UART(void)
{
#ifdef CFG_UART
  MX_UART_Deinit(CFG_UART);
#endif
}

static void uartRxStart(void)
{
  if (HW_UART_Receive_IT(CFG_UART, (uint8_t *)&uartRxBuf, 1, uartRxCpltCallback) != hw_uart_ok){
    APP_DBG("ERROR returned by HW_UART_Receive_IT()");
  }
}

void APP_BLE_LLD_uartRxStart(void(*callback)(char))
{
  uartRxUserCb = callback;
  uartRxStart();
}

static void uartRxCpltCallback(void)
{
  // No need to buffer uartRxBuf since the callback is called by value
  uartRxUserCb(uartRxBuf);
  // Since UART is in full duplex, receive can be always active without blocking send
  uartRxStart();
}

void uartWrite(const char *format, ...)
{
  char out[UART_BUFFER_SIZE];
  int nbChar;
  va_list argp;
  va_start(argp, format);
  nbChar = vsnprintf(out, sizeof(out), format, argp);
  va_end(argp);
  if (nbChar < 0){
    return;
  }
  if (nbChar > (sizeof(out) - ((strlen(UART_LINE_END) + 1)))){
    strcpy(&(out[sizeof(out) - (strlen(UART_LINE_END) + 1)]), UART_LINE_END);
  }else{
    strcat(out, UART_LINE_END);
  }
  uartWriteRaw(out);
}

void uartWriteRaw(const char *str)
{
  CRITICAL_BEGIN();
  while (*str != '\0'){
    CircularQueue_Add(&uartTxBuf, (uint8_t *)str, 0, 1);
    str++;
  }
  if (! txBusy){
    uartTxSendChunk();
  }
  CRITICAL_END();
}

// Send multiple chars through the UART
// must be called inside critical section
// loop on itself via the UART callback
static void uartTxSendChunk(void){
  static char hwBuf[UART_TX_CHUNK_SIZE];
  char *charPtr;
  uint32_t count = 0;

  while ((charPtr = (char *)CircularQueue_Remove(&uartTxBuf, NULL)) != NULL){
    hwBuf[count] = *charPtr;
    count++;
    if (count >= UART_TX_CHUNK_SIZE){
      break;
    }
  }
  if (count != 0){
    txBusy = true;
    if (HW_UART_Transmit_IT(CFG_UART, (uint8_t *)hwBuf, count, uartTxSendChunk) != hw_uart_ok){
      APP_DBG("ERROR returned by HW_UART_Transmit_IT()");
    }
  }else{
    txBusy = false;
  }
}

static void m0CmdProcess(void)
{
  BLE_LLD_PRX_EventProcessTask();
}

/**
 * @brief Processes an event from radio CPU
 *
 * @param   cmdBuffer : a pointer to TL_CmdPacket_t
 * @return  None
 */
void TL_BLE_LLD_ReceiveM0Cmd( TL_CmdPacket_t * cmdBuffer )
{
  BLE_LLD_PRX_EventProcessInter((radioEventType)cmdBuffer->cmdserial.cmd.cmdcode);
  UTIL_SEQ_SetTask(1U << CFG_TASK_CMD_FROM_M0_TO_M4, CFG_SCH_PRIO_0);
  TL_BLE_LLD_SendM0CmdAck();
}

/**
 * @brief Sends a command to radio CPU
 *
 * Waits for reply from radio CPU before returning (synchronous calls).
 *
 * @param[in] command BLE command already packed (by LLD)
 */
uint8_t APP_BLE_LLD_SendCmdM0(BLE_LLD_Code_t bleCmd)
{
  BleLldCmdRspPacket.cmdserial.cmd.cmdcode = bleCmd;
  payload_BLE_LLD_t *payload = (payload_BLE_LLD_t *)&BleLldCmdRspPacket.cmdserial.cmd.payload;
  payload->msg = &bleparam_BLE_LLD_Packet;
  UTIL_SEQ_ClrEvt(1U << CFG_EVT_RECEIVE_RSPACKEVT);
  TL_BLE_LLD_SendCmd();
  UTIL_SEQ_WaitEvt(1U << CFG_EVT_RECEIVE_RSPACKEVT);

  return bleparam_BLE_LLD_Packet.returnValue;
}

/**
 * @brief Processes a reply (to a command) from radio CPU
 *
 * Unlocks task waiting in APP_BLE_LLD_SendCmdM0(), this is used to make LLD
 * API calls synchronous.
 *
 * @param   Notbuffer : a pointer to TL_CmdPacket_t
 * @return  None
 */
void TL_BLE_LLD_ReceiveRsp( TL_CmdPacket_t * Notbuffer )
{
  switch (Notbuffer->cmdserial.cmd.cmdcode){
    case BLE_LLD_RSP_END:
      UTIL_SEQ_SetEvt(1U << CFG_EVT_RECEIVE_RSPACKEVT);
      break;
    default:
      APP_DBG("WARNING: unknown response received %d", Notbuffer->cmdserial.cmd.cmdcode);
  }

    /* This is just a trace from M0, write to UART */
    //uartWriteRaw(sourceBuf);

  TL_BLE_LLD_SendRspAck();
}

/* USER CODE END FD_WRAP_FUNCTIONS */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/