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

lowpower_app.c « Src « Core « 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: 58c6e88ecc4f0a01a1728ed0258892bd8338f977 (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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* USER CODE BEGIN Header */
/**
 ******************************************************************************
  * File Name          : lowpower_app.c
  * Description        : BLE LLD validation Application.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 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
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "app_common.h"
#include "shci.h"
#include "stm32_seq.h"
#include "stm32_lpm.h"
#include "stm_logging.h"
#include "dbg_trace.h"
#include "ble_lld.h"
#include "app_ble_lld.h"
#include "lowpower_app.h"
#include "ring_buffer.h"

/* Private includes -----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private defines -----------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define LOWPOWER_CHANNEL_1         8
#define LOWPOWER_CHANNEL_2         9
#define LOWPOWER_ID                0x5A964129
#define LOWPOWER_WAKEUP            9876
#define LOWPOWER_RECEIVE           19876
#define LOWPOWER_RECEIVE_ACK       576
#define BACK2BACK_TIME          50

/* Routines options */
#define LOWPOWER_PACKET_NUMBER_TX 50 // NB of Successif PACKET using HAL Send
#define LOWPOWER_PACKET_NUMBER_RX 40 // NB of Successif PACKET using HAL Receive
#define LOWPOWER_PACKET_STOP_RX   0   // Stop after RX
/* USER CODE END PD */

/* Private macros ------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
static void m0CmdProcess(void);

static void LP_BLE_Init(void);
static void LP_SendPacket(void);
static void LP_ReceivePacket(void);

static void Appli_m0CmdProcess_RadioStop(void);
static void Appli_m0CmdProcess_RadioEnd(void);
static void Appli_m0CmdProcess_RxAck(void);
static void Appli_m0CmdProcess_RxOk(void);
static void Appli_m0CmdProcess_RxAckEnd(void);
static void Appli_m0CmdProcess_RxOkEnd(void);

static void bleInit(void);
static void bleSend(void);
static void bleReceive(void);
/* USER CODE END PFP */

/* Private variables -----------------------------------------------*/
/* USER CODE BEGIN PV */
static bool      Toggle_in_progress = false; // used for TX/RX toggle
static uint32_t  number_of_TXRX=0;
static uint32_t  number_of_RXACK=0;

/* data buffer Tab to send TX */
static uint8_t lowpowertxBuffer[TXRX_BUF_SIZE] ={0x95,0xFF,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x73,0x65,0x70,0x68,0x26,0x48,0x6F,0x73,
0x8A,0xA8,0xBB,0xFF,0x8A,0xA8,0xA8,0xA8,
};

/* data buffer ACK Tab to send TX ACK after RX */
static uint8_t lowpowerAcktxBuffer[TXRX_BUF_SIZE] ={0x60,0x0D,
0x4a,0x75,0x6c,0x69,0x65,0x6e,0x2b,0x4d,0x61,0x72,0x69,0x6e,0x65,
};

/* data buffer Tab to receive RX */
static uint8_t lowpowerrxBuffer[TXRX_BUF_SIZE];

/* data buffer ACK Tab to receive RX ACK after TX */
static uint8_t lowpowerAckrxBuffer[TXRX_BUF_SIZE];

static ActionPacket lowpowerPacket[ACTION_PACKET_NB];

/* Hot config */
static uint32_t BLE_LLD_hot_ana_config_table[BLE_HOT_ANA_CONFIG_TABLE_LENGTH/4];

/* Timer for LED blink */
extern TIM_HandleTypeDef htim2;
uint8_t SwitchOffGPIO_timer_Id;
#define LED_ON_TIMEOUT            (0.5*1000*1000/CFG_TS_TICK_VAL) /**< 500ms */

/* USER CODE END PV */

/* Functions Definition ------------------------------------------------------*/
/* USER CODE BEGIN FD */

void LOWPOWER_APP_Init(void)
{
  /* Check the compatibility with the Coprocessor Wireless Firmware loaded */
  CheckWirelessFirmwareInfo();

  /* Do not allow standby in the application */
  UTIL_LPM_SetOffMode(1 << CFG_LPM_APP_BLE_LLD, UTIL_LPM_DISABLE);
  /* Disable low power mode for now, may be enable later depending on configuration */
  UTIL_LPM_SetStopMode(1 << CFG_LPM_APP_BLE_LLD, UTIL_LPM_DISABLE );

  /* Register tasks for event processing */
  UTIL_SEQ_RegTask( 1<<CFG_TASK_CMD_FROM_M0_TO_M4,     UTIL_SEQ_RFU, m0CmdProcess);
  UTIL_SEQ_RegTask( 1<<CFG_TASK_HAL_BLE_INIT,          UTIL_SEQ_RFU, LP_BLE_Init);
  UTIL_SEQ_RegTask( 1<<CFG_TASK_HAL_BLE_SENDPACKET,    UTIL_SEQ_RFU, LP_SendPacket);
  UTIL_SEQ_RegTask( 1<<CFG_TASK_HAL_BLE_RECEIVEPACKET, UTIL_SEQ_RFU, LP_ReceivePacket);

  /* Register callbacks for radio events processing */
  BLE_LLD_PRX_ReplyTaskCbRegister(CMD_FROM_M0_RADIO_STOP,     Appli_m0CmdProcess_RadioStop);
  BLE_LLD_PRX_ReplyTaskCbRegister(CMD_FROM_M0_RADIO_END,      Appli_m0CmdProcess_RadioEnd);
  BLE_LLD_PRX_ReplyTaskCbRegister(CMD_FROM_M0_RADIO_RXACK,    Appli_m0CmdProcess_RxAck);
  BLE_LLD_PRX_ReplyTaskCbRegister(CMD_FROM_M0_RADIO_RXOK,     Appli_m0CmdProcess_RxOk);
  BLE_LLD_PRX_ReplyTaskCbRegister(CMD_FROM_M0_RADIO_RXACKEND, Appli_m0CmdProcess_RxAckEnd);
  BLE_LLD_PRX_ReplyTaskCbRegister(CMD_FROM_M0_RADIO_RXOKEND,  Appli_m0CmdProcess_RxOkEnd);

  BLE_LLD_PRX_Init(APP_BLE_LLD_SendCmdM0);

  APP_BLE_LLD_Init();

  UTIL_SEQ_SetTask(1U << CFG_TASK_HAL_BLE_INIT, CFG_SCH_PRIO_0);

  #if (CFG_LED_SUPPORTED == 1U)
  BSP_LED_On(LED_BLUE);
  BSP_LED_On(LED_RED);
  BSP_LED_On(LED_GREEN);
  #endif


  #if ( CFG_LPM_SUPPORTED == 1)
      HW_TS_Create(CFG_TIM_PROC_ID_ISR, &SwitchOffGPIO_timer_Id, hw_ts_SingleShot, Appli_TS_Callback);
  #else
      HAL_TIM_Base_Start_IT(&htim2);
  //UTIL_LPM_SetStopMode(1 << CFG_LPM_APP_BLE_LLD, UTIL_LPM_ENABLE );
  #endif
  


}

/* Route button event to processing task */
void Appli_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  switch (GPIO_Pin)
  {
    case BUTTON_SW1_PIN:
      //UTIL_SEQ_SetTask(1U << CFG_TASK_HAL_BLE_INIT, CFG_SCH_PRIO_0);
      break;

    case BUTTON_SW2_PIN:
      //UTIL_SEQ_SetTask(1U << CFG_TASK_HAL_BLE_SENDPACKET, CFG_SCH_PRIO_0);
      break;

    case BUTTON_SW3_PIN:
      //UTIL_SEQ_SetTask(1U << CFG_TASK_HAL_BLE_RECEIVEPACKET, CFG_SCH_PRIO_0);
      break;

    default:
      break;

  }
  return;
}

void Appli_TIM_IC_CaptureCallback(void)
{
}

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

static void Appli_m0CmdProcess_RadioStop(void)
{
  logUart("Radio STOP : RXACK:%d RX:%d",number_of_RXACK ,number_of_TXRX);
  number_of_RXACK=0;
  number_of_TXRX=0;
}
static void Appli_m0CmdProcess_RadioEnd(void)
{
  #if (CFG_LPM_SUPPORTED == 1U)
    HW_TS_Start(SwitchOffGPIO_timer_Id, (uint32_t)LED_ON_TIMEOUT);
    UTIL_LPM_SetStopMode(1 << CFG_LPM_APP_BLE_LLD, UTIL_LPM_ENABLE );
  #else
    HAL_TIM_Base_Start_IT(&htim2);
  #endif  
  number_of_RXACK=0;
  number_of_TXRX=0;
  #if (CFG_LED_SUPPORTED == 1U)
  BSP_LED_Off(LED_BLUE);
  #endif
}

static void Appli_m0CmdProcess_RxAck(void)
{
  number_of_RXACK++;
  logUart("Radio RXACK %d",number_of_RXACK);
  //BSP_LED_Off(LED_BLUE);
}

static void Appli_m0CmdProcess_RxOk(void)
{
  number_of_TXRX++;
  logUart("Radio RX %d",number_of_TXRX);
  //BSP_LED_Off(LED_BLUE);
}

static void Appli_m0CmdProcess_RxAckEnd(void)
{
  number_of_RXACK++;
  #if (CFG_LPM_SUPPORTED == 1U)
    HW_TS_Start(SwitchOffGPIO_timer_Id, (uint32_t)LED_ON_TIMEOUT);
  #else
    HAL_TIM_Base_Start_IT(&htim2);
  #endif
  logUart("Radio END: RXACK:%d",number_of_RXACK);
  number_of_RXACK=0;

}

static void Appli_m0CmdProcess_RxOkEnd(void)
{
  number_of_TXRX++;
  #if (CFG_LPM_SUPPORTED == 1U)
    HW_TS_Start(SwitchOffGPIO_timer_Id, (uint32_t)LED_ON_TIMEOUT);
  #else
    HAL_TIM_Base_Start_IT(&htim2);
  #endif
  logUart("Radio END: RX:%d",number_of_TXRX);
  number_of_TXRX=0;
}

/* Appli custom functions */
static void LP_BLE_Init(void)
{
      bleInit();
}

static void LP_SendPacket(void)
{
      #if (CFG_LED_SUPPORTED == 1U)
      BSP_LED_On(LED_RED);
      BSP_LED_Off(LED_GREEN);
      #endif

      bleSend();
}

static void LP_ReceivePacket(void)
{
      #if (CFG_LED_SUPPORTED == 1U)
      BSP_LED_On(LED_GREEN);
      BSP_LED_Off(LED_RED);
      #endif
      bleReceive();
}

/* ---------------------------- LLD specific code ----------------------------*/
static void bleInit(void)
{
  #if (CFG_LED_SUPPORTED == 1U)
  BSP_LED_Off(LED_BLUE);
  #endif
  UTIL_LPM_SetStopMode(1 << CFG_LPM_APP_BLE_LLD, UTIL_LPM_DISABLE );

  BLE_LLD_Init(HS_STARTUP_TIME, 1, BLE_LLD_hot_ana_config_table, ENABLE);

  #if (CFG_LED_SUPPORTED == 1U)
  BSP_LED_On(LED_BLUE);
  BSP_LED_Off(LED_BLUE);
  #endif
  uint8_t map[5]= {0xFF,0xFF,0xFF,0xFF,0xFF};

  BLE_LLD_SetChannelMap(STATE_MACHINE_0, &map);
  BLE_LLD_SetChannel(STATE_MACHINE_0,LOWPOWER_CHANNEL_2,0);
  BLE_LLD_SetTxAttributes(STATE_MACHINE_0, LOWPOWER_ID, 0x555555,0);
  BLE_LLD_SetTx_Rx_Phy(STATE_MACHINE_0, TX_PHY_2MBPS, RX_PHY_2MBPS);
  BLE_LLD_SetBackToBackTime(BACK2BACK_TIME);

  BLE_LLD_SetChannelMap(STATE_MACHINE_3, &map);
  BLE_LLD_SetChannel(STATE_MACHINE_3, LOWPOWER_CHANNEL_2, 0);
  BLE_LLD_SetTxAttributes(STATE_MACHINE_3, ~LOWPOWER_ID, 0x555555,0);
  BLE_LLD_SetTx_Rx_Phy(STATE_MACHINE_3, TX_PHY_2MBPS, RX_PHY_2MBPS);
  BLE_LLD_SetBackToBackTime(BACK2BACK_TIME);
      
     
  #if (CFG_LPM_SUPPORTED == 1U)
    HW_TS_Start(SwitchOffGPIO_timer_Id, (uint32_t)LED_ON_TIMEOUT);
  #else
    HAL_TIM_Base_Start_IT(&htim2);
  #endif

}

static void bleSend(void)
{
  lowpowerPacket[APACKET_0].StateMachineNo = STATE_MACHINE_0;
  lowpowerPacket[APACKET_0].ActionTag = AT_TXRX | AT_PLL_TRIG | AT_TIMER_WAKEUP | AT_RELATIVE;
  lowpowerPacket[APACKET_0].WakeupTime = LOWPOWER_WAKEUP;
  lowpowerPacket[APACKET_0].data = lowpowertxBuffer;
  lowpowerPacket[APACKET_0].next_true = APACKET_1;
  lowpowerPacket[APACKET_0].next_false = APACKET_NULL;
  lowpowerPacket[APACKET_0].condRoutine = condCase_Done;
  lowpowerPacket[APACKET_0].dataRoutine = DATA_ROUT_LL_TXMULTIACK;
  lowpowerPacket[APACKET_0].actionPacketNb = APACKET_0;
  BLE_LLD_SetReservedArea(&lowpowerPacket[APACKET_0]);

  lowpowerPacket[APACKET_1].StateMachineNo = STATE_MACHINE_3;
  lowpowerPacket[APACKET_1].ActionTag = 0;
  lowpowerPacket[APACKET_1].WakeupTime = LOWPOWER_WAKEUP;
  lowpowerPacket[APACKET_1].ReceiveWindowLength = LOWPOWER_RECEIVE;
  lowpowerPacket[APACKET_1].data = lowpowerAckrxBuffer;
  lowpowerPacket[APACKET_1].next_true = APACKET_NULL;
  lowpowerPacket[APACKET_1].next_false = APACKET_0;
  lowpowerPacket[APACKET_1].condRoutine = condCase_Done;
  lowpowerPacket[APACKET_1].dataRoutine = DATA_ROUT_LL_TXMULTIACK;
  lowpowerPacket[APACKET_1].actionPacketNb = APACKET_1;
  BLE_LLD_SetReservedArea(&lowpowerPacket[APACKET_1]);

  BLE_LLD_PRX_SetdataRoutineMultiOptions(LOWPOWER_PACKET_NUMBER_TX,LOWPOWER_PACKET_STOP_RX);
  BLE_LLD_MakeActionPacketPending(&lowpowerPacket[APACKET_0]);
}

static void bleReceive(void)
{
  lowpowerPacket[APACKET_5].StateMachineNo = STATE_MACHINE_0;
  lowpowerPacket[APACKET_5].ActionTag = AT_PLL_TRIG | AT_TIMER_WAKEUP | AT_RELATIVE;
  lowpowerPacket[APACKET_5].WakeupTime = LOWPOWER_WAKEUP;
  lowpowerPacket[APACKET_5].ReceiveWindowLength = LOWPOWER_RECEIVE;
  lowpowerPacket[APACKET_5].data = lowpowerrxBuffer;
  lowpowerPacket[APACKET_5].next_true = APACKET_1;
  lowpowerPacket[APACKET_5].next_false = APACKET_NULL;
  lowpowerPacket[APACKET_5].condRoutine = condCase_Rx;
  lowpowerPacket[APACKET_5].dataRoutine = DATA_ROUT_LL_RXMULTIACK;
  lowpowerPacket[APACKET_5].actionPacketNb = APACKET_5;
  BLE_LLD_SetReservedArea(&lowpowerPacket[APACKET_5]);

  lowpowerPacket[APACKET_1].StateMachineNo = STATE_MACHINE_3;
  lowpowerPacket[APACKET_1].ActionTag = AT_TXRX;
  lowpowerPacket[APACKET_1].WakeupTime = LOWPOWER_WAKEUP;
  lowpowerPacket[APACKET_1].data = lowpowerAcktxBuffer;
  lowpowerPacket[APACKET_1].next_true = APACKET_NULL;
  lowpowerPacket[APACKET_1].next_false = APACKET_5;
  lowpowerPacket[APACKET_1].condRoutine = condCase_Done;
  lowpowerPacket[APACKET_1].dataRoutine = DATA_ROUT_LL_RXMULTIACK;
  lowpowerPacket[APACKET_1].actionPacketNb = APACKET_1;
  BLE_LLD_SetReservedArea(&lowpowerPacket[APACKET_1]);

  BLE_LLD_PRX_SetdataRoutineMultiOptions(LOWPOWER_PACKET_NUMBER_RX,LOWPOWER_PACKET_STOP_RX);
  BLE_LLD_MakeActionPacketPending(&lowpowerPacket[APACKET_5]);
}

void Appli_TIM_PeriodElapsedCallback(void)
{
  HAL_TIM_Base_Stop_IT(&htim2);
  #if (CFG_LED_SUPPORTED == 1U)
  BSP_LED_On(LED_BLUE);
  #endif

  if(Toggle_in_progress){
      Toggle_in_progress = false;
      UTIL_SEQ_SetTask(1U << CFG_TASK_HAL_BLE_SENDPACKET, CFG_SCH_PRIO_0);
  } else {
      Toggle_in_progress = true;
      UTIL_SEQ_SetTask(1U << CFG_TASK_HAL_BLE_RECEIVEPACKET, CFG_SCH_PRIO_0);
  }
}

void Appli_TS_Callback(void)
{
  #if (CFG_LED_SUPPORTED == 1U)
  BSP_LED_On(LED_BLUE);
  #endif
  
  if(Toggle_in_progress){
      Toggle_in_progress = false;
      UTIL_SEQ_SetTask(1U << CFG_TASK_HAL_BLE_SENDPACKET, CFG_SCH_PRIO_0);
  } else {
      Toggle_in_progress = true;
      UTIL_SEQ_SetTask(1U << CFG_TASK_HAL_BLE_RECEIVEPACKET, CFG_SCH_PRIO_0);
  }
}
/* USER CODE END FD */

/* USER CODE BEGIN FD_WRAP_FUNCTIONS */
/* USER CODE END FD_WRAP_FUNCTIONS */

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