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

main.c « Src « BLE_Peripheral_Lite « BLE « Applications « P-NUCLEO-WB55.Nucleo « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c08479fca4b31f10b7c14c52d02636041c72e2b9 (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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file    main.c
* @author  MCD Application Team
* @brief   BLE application with BLE core
*
@verbatim
==============================================================================
##### IMPORTANT NOTE #####
==============================================================================

This application requests having the stm32wb5x_BLE_Stack_fw.bin binary
flashed on the Wireless Coprocessor.
If it is not the case, you need to use STM32CubeProgrammer to load the appropriate
binary.

All available binaries are located under following directory:
/Projects/STM32_Copro_Wireless_Binaries

Refer to UM2237 to learn how to use/install STM32CubeProgrammer.
Refer to /Projects/STM32_Copro_Wireless_Binaries/ReleaseNote.html for the
detailed procedure to change the Wireless Coprocessor binary.

@endverbatim
******************************************************************************
* @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
*
******************************************************************************
*/
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "app_common.h"
#include "hw_conf.h"
#include "otp.h"
#include "main.h"
#include "ble.h"
#include "hci_tl.h"
#include "shci_tl.h"
#include "shci.h"
#include "app_debug.h"
#include "gatt_service.h"
/* USER CODE END Includes */

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

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

#define EVT_END_OF_RADIO_ACTIVITY           0x0004

#define BD_ADDR_SIZE_LOCAL                  6
#define APP_BLE_GAP_DEVICE_NAME_LENGTH      sizeof(gap_device_name)
#define EVENT_POOL_SIZE                    (CFG_TLBLE_EVT_QUEUE_LENGTH*4U*DIVC(( sizeof(TL_PacketHeader_t) + TL_BLE_EVENT_FRAME_SIZE ), 4U))

#define APP_FLAG_CPU2_INITIALIZED           0
#define APP_FLAG_CPU2_ERROR                24
#define APP_FLAG_WIRELESS_FW_RUNNING        1
#define APP_FLAG_FUS_FW_RUNNING             2
#define APP_FLAG_BLE_INITIALIZATION_ERROR  25
#define APP_FLAG_BLE_INITIALIZED            3
#define APP_FLAG_BLE_ADVERTISING            4
#define APP_FLAG_BLE_CONNECTED              5
#define APP_FLAG_HCI_EVENT_PENDING         18
#define APP_FLAG_SHCI_EVENT_PENDING        19
#define APP_FLAG_GET(flag)                  VariableBit_Get_BB(((uint32_t)&APP_State), flag)
#define APP_FLAG_SET(flag)                  VariableBit_Set_BB(((uint32_t)&APP_State), flag)
#define APP_FLAG_RESET(flag)                VariableBit_Reset_BB(((uint32_t)&APP_State), flag)

/* USER CODE END PD */

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

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
static volatile uint32_t APP_State = 0x00000000;

PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t EvtPool[EVENT_POOL_SIZE];
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t SystemCmdBuffer;
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t BleCmdBuffer;
PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static uint8_t SystemSpareEvtBuffer[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255];
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t BleSpareEvtBuffer[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255];

static uint8_t bd_address_udn[BD_ADDR_SIZE_LOCAL];

static uint8_t myVeryOwnNotifyCharacteristicData[MY_VERY_OWN_NOTIFY_CHARACTERISTIC_VALUE_LENGTH] = {0x00, 0x00};

/* Generic Access GATT Service Characteristics configuration data  */
static const char gap_device_name[] = { 'H', 'E', 'L', 'L', 'O', '!' };
static const uint16_t gap_appearance = BLE_CFG_GAP_APPEARANCE;

/* GAP Advertising data */
static const char ad_local_name[] = { AD_TYPE_COMPLETE_LOCAL_NAME, 'H', 'E', 'L', 'L', 'O', '!' };
static uint8_t ad_manufacturer_specific_data[14] = { /* Manufacturer specific data used to get compliant with ST BLE Sensor smart phone apk */
    sizeof(ad_manufacturer_specific_data)-1,
    AD_TYPE_MANUFACTURER_SPECIFIC_DATA, 
    0x01, /* BlueST Protocol version */
    0x83, /* BlueST Device Id: 0x83 - P2PServer1 - for more details please see BLE_p2pServer example project */
    0x00, /* BlueST Feature Mask bits 24~31 */
    0x00, /* BlueST Feature Mask bits 16~23 */
    0x00, /* BlueST Feature Mask bits 8~15 */
    0x00, /* BlueST Feature Mask bits 0~7 */
    0x00, /* BlueST Device MAC byte 5 */
    0x00, /* BlueST Device MAC byte 4 */
    0x00, /* BlueST Device MAC byte 3 */
    0x00, /* BlueST Device MAC byte 2 */
    0x00, /* BlueST Device MAC byte 1 */
    0x00  /* BlueST Device MAC byte 0 */
};

/* More details about BlueST protocol and how it is used in our demos and examples
   can be found in the related documentation, e.g. in UM2496 */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_RF_Init(void);
/* USER CODE BEGIN PFP */
static void Tune_HSE(void);
static void CPU2_Init(void);
static void Ble_Tl_Init(void);
static void Ble_Hci_Gap_Gatt_Init(void);
static const uint8_t* Ble_GetBdAddress(void);
static void SYS_UserEventReceivedCallback(void * pData);
static void SYS_StatusNotificationCallback(SHCI_TL_CmdStatus_t status);
static void SYS_ProcessEvent(void);
static void BLE_UserEventReceivedCallback(void * pData);
static void BLE_StatusNotificationCallback(HCI_TL_CmdStatus_t status);
static void BLE_Init(void);
static void BLE_Advertising(FlagStatus setReset);
static void BLE_ProcessEvent(void);

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
  /**
   * The OPTVERR flag is wrongly set at power on
   * It shall be cleared before using any HAL_FLASH_xxx() api
   */
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);

  uint32_t prevTick = 0;
  /* USER CODE END 1 */
  
  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */
  
  /* Tune the HSE internal load capacitors - P-NUCLEO-WB55.Nucleo board */
  Tune_HSE();
  
  /* Configure the debug support if needed */
  APPD_Init();
  
  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */
  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_RF_Init();
  /* USER CODE BEGIN 2 */
  
  /* Initialize the GPIO pins for P-NUCLEO-WB55.Nucleo board LEDs control */  
  BSP_LED_Init(LED_RED);
  BSP_LED_Init(LED_GREEN);
  BSP_LED_Init(LED_BLUE);
  
  /* Initialize all transport layers */
  CPU2_Init();
   
  /* Set the red LED On to indicate that the CPU2 is initializing */
  BSP_LED_On(LED_RED); 
  
  /* Wait until the CPU2 gets initialized */
  while((APP_FLAG_GET(APP_FLAG_CPU2_INITIALIZED) == 0) \
        || (APP_FLAG_GET(APP_FLAG_WIRELESS_FW_RUNNING) == 0))
  {
    /* Process pending SYSTEM event coming from CPU2 (if any) */
    SYS_ProcessEvent();
  }
    
  /* Configure the CPU2 Debug (Optional) */
  APPD_EnableCPU2();
  
  /* Set the red LED Off to indicate that the CPU2 is initialized */
  BSP_LED_Off(LED_RED);
  
  /* Set the green LED On to indicate that the wireless stack FW is running */
  BSP_LED_On(LED_GREEN);
  
  /* At this point it is still unknown from the app perspective, which wireless stack
     and which version is installed on CPU2. It is expected that a BLE stack is installed.
     In order to check that, SHCI_GetWirelessFwInfo(...) can be used to read out
     the information about the CM0+ wireless stack FW running since the Device Information Table
     is initialized. For more information on this topic, please refer to AN5289 and AN5185. */
  
  /* Initialize BLE (BLE TL, BLE stack, HAL, HCI, GATT, GAP) */
  BLE_Init();

  /* Set the blue LED On to indicate that the BLE stack is initialized */
  BSP_LED_On(LED_BLUE);

  /* Initialize My Very Own GATT Service - user may also implement SVCCTL_InitCustomSvc()
     interface function as explained in AN5289. SVCCTL_InitCustomSvc() is called at the end of
     SVCCTL_Init() called from BLE_Init() */
  MyVeryOwnService_Init();
  
  /* Reset BLUE LED => Will be used by the example */
  BSP_LED_Off(LED_BLUE);
  
  /* Start BLE advertising */
  BLE_Advertising(SET);
  
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while(1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    /* Process pending BLE event coming from CPU2 (if any) */
    BLE_ProcessEvent();
    /* Process pending SYSTEM event coming from CPU2 (if any) */
    SYS_ProcessEvent();
        
    /* Update the My Very Own Notify Characteristic every ~1 second and only if BLE connected.
       It might be also done only after the GATT client enables the notifications,
       but that is out of scope of this basic example */
    if (APP_FLAG_GET(APP_FLAG_BLE_CONNECTED) != 0x00)
    {
      if ((HAL_GetTick() - prevTick) > 1000)
      {
        prevTick = HAL_GetTick();
        myVeryOwnNotifyCharacteristicData[1] ^= 0x01;
        if (MyVeryOwnWriteCharacteristic_Update(MY_VERY_OWN_NOTIFY_CHARACTERISTIC_UUID,
                                                MY_VERY_OWN_NOTIFY_CHARACTERISTIC_VALUE_LENGTH,
                                                myVeryOwnNotifyCharacteristicData) != BLE_STATUS_SUCCESS)
        {
          Error_Handler(); /* UNEXPECTED */
        }
      }
    }
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

  /** Configure LSE Drive Capability 
  */
  __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
  /** Configure the main internal regulator output voltage 
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE
                              |RCC_OSCILLATORTYPE_LSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK4|RCC_CLOCKTYPE_HCLK2
                              |RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.AHBCLK2Divider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.AHBCLK4Divider = RCC_SYSCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the peripherals clocks 
  */
  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SMPS|RCC_PERIPHCLK_RFWAKEUP;
  PeriphClkInitStruct.RFWakeUpClockSelection = RCC_RFWKPCLKSOURCE_LSE;
  PeriphClkInitStruct.SmpsClockSelection = RCC_SMPSCLKSOURCE_HSE;
  PeriphClkInitStruct.SmpsDivSelection = RCC_SMPSCLKDIV_RANGE1;

  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief RF Initialization Function
  * @param None
  * @retval None
  */
static void MX_RF_Init(void)
{

  /* USER CODE BEGIN RF_Init 0 */

  /* USER CODE END RF_Init 0 */

  /* USER CODE BEGIN RF_Init 1 */

  /* USER CODE END RF_Init 1 */
  /* USER CODE BEGIN RF_Init 2 */

  /* USER CODE END RF_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

}

/* USER CODE BEGIN 4 */

/**
* @brief This function initializes and releases the CPU2 subsystem
* @param None
* @retval None
*/
static void CPU2_Init( void )
{
  TL_MM_Config_t tl_mm_config;
  SHCI_TL_HciInitConf_t SHci_Tl_Init_Conf;
  
  /**< Reference table initialization */
  TL_Init();
  
  /**< System channel initialization */
  SHci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&SystemCmdBuffer;
  SHci_Tl_Init_Conf.StatusNotCallBack = SYS_StatusNotificationCallback;
  shci_init(SYS_UserEventReceivedCallback, (void*) &SHci_Tl_Init_Conf);
  
  /**< Memory Manager channel initialization */
  tl_mm_config.p_AsynchEvtPool = EvtPool;
  tl_mm_config.p_BleSpareEvtBuffer = BleSpareEvtBuffer; /* UNUSED, but kept for future compatibility */
  tl_mm_config.p_SystemSpareEvtBuffer = SystemSpareEvtBuffer; /* UNUSED, but kept for future compatibility, but used by FUS today only */
  tl_mm_config.AsynchEvtPoolSize = EVENT_POOL_SIZE;
  TL_MM_Init( &tl_mm_config );
  
  /**< Release the CPU2 */
  TL_Enable();
  
  return;
}

/**
* @brief This function initializes the BLE stack
* @param None
* @retval None
*/
static void BLE_Init( void )
{
  SHCI_CmdStatus_t ret;
  
  SHCI_C2_Ble_Init_Cmd_Packet_t ble_init_cmd_packet =
  {
    {{0,0,0}},                          /**< Header unused */
    {0,                                 /** pBleBufferAddress not used */
    0,                                  /** BleBufferSize not used */
    CFG_BLE_NUM_GATT_ATTRIBUTES,
    CFG_BLE_NUM_GATT_SERVICES,
    CFG_BLE_ATT_VALUE_ARRAY_SIZE,
    CFG_BLE_NUM_LINK,
    CFG_BLE_DATA_LENGTH_EXTENSION,
    CFG_BLE_PREPARE_WRITE_LIST_SIZE,
    CFG_BLE_MBLOCK_COUNT,
    CFG_BLE_MAX_ATT_MTU,
    CFG_BLE_SLAVE_SCA,
    CFG_BLE_MASTER_SCA,
    CFG_BLE_LSE_SOURCE,
    CFG_BLE_MAX_CONN_EVENT_LENGTH,
    CFG_BLE_HSE_STARTUP_TIME,
    CFG_BLE_VITERBI_MODE,
    CFG_BLE_OPTIONS,
    0,
    CFG_BLE_MAX_COC_INITIATOR_NBR,
    CFG_BLE_MIN_TX_POWER,
    CFG_BLE_MAX_TX_POWER}
  };
  
  /**
  * Initialize Ble Transport Layer
  */
  Ble_Tl_Init( );
  
  /**
  * Starts the BLE Stack on CPU2
  */
  ret = SHCI_C2_BLE_Init( &ble_init_cmd_packet );
  if (ret != SHCI_Success)
  {
    Error_Handler(); /* UNEXPECTED */
  }
  
  /**
  * Initialization of HCI & GATT & GAP layer
  */
  Ble_Hci_Gap_Gatt_Init();
  
  /**
  * Initialization of the BLE Services
  */
  SVCCTL_Init();
  
  return;
}

/**
* @brief This function is used to process all events coming from BLE stack by executing the related callback
* @param None
* @retval None
*/
static void BLE_ProcessEvent(void)
{
  if (APP_FLAG_GET(APP_FLAG_HCI_EVENT_PENDING) == 1)
  {
    APP_FLAG_RESET(APP_FLAG_HCI_EVENT_PENDING);
    hci_user_evt_proc();
  }
}

/**
* @brief This function initializes the BLE Transport Layer
* @param None
* @retval None
*/
static void Ble_Tl_Init( void )
{
  HCI_TL_HciInitConf_t Hci_Tl_Init_Conf;
  
  /**< BLE channel initialization */
  Hci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&BleCmdBuffer;
  Hci_Tl_Init_Conf.StatusNotCallBack = BLE_StatusNotificationCallback;
  hci_init(BLE_UserEventReceivedCallback, (void*) &Hci_Tl_Init_Conf);
  
  return;
}

/**
* @brief This function starts or stops the BLE advertising
* @param newState: SET to start the advertising, RESET to stop
* @retval None
*/
static void BLE_Advertising(FlagStatus newState)
{
  tBleStatus ret = BLE_STATUS_SUCCESS;
  
  if (newState == SET)
  {
    if (APP_FLAG_GET(APP_FLAG_BLE_ADVERTISING) == 0)
    {
      /**
      * Put the device in a advertising & connectable mode.
      */
      ret = aci_gap_set_discoverable(ADV_IND,                                       /*< Advertise as connectable, undirected. */
                                     CFG_FAST_CONN_ADV_INTERVAL_MIN,                /*< Set the advertising interval min value. */
                                     CFG_FAST_CONN_ADV_INTERVAL_MAX,                /*< Set the advertising interval max value. */
                                     PUBLIC_ADDR,                                   /*< Use the public address. */
                                     NO_WHITE_LIST_USE,                             /*< No white list. */
                                     sizeof(ad_local_name), (uint8_t*)ad_local_name,/*< Use a local name. */
                                     0, NULL,                                       /*< Do not include the service UUID list. (no adopted services) */
                                     0x0000, 0x0000);                               /*< NaN, do not put in advertising data. */
      if (ret != BLE_STATUS_SUCCESS)
      {
        Error_Handler(); /* UNEXPECTED */
      }
      
      /**
      * Update the advertising data.
      */
      ret = aci_gap_update_adv_data(sizeof(ad_manufacturer_specific_data), (uint8_t*)ad_manufacturer_specific_data);
      if (ret != BLE_STATUS_SUCCESS)
      {
        Error_Handler(); /* UNEXPECTED */
      }
      APP_FLAG_SET(APP_FLAG_BLE_ADVERTISING);
    }
  }
  else {
    /**
     * Stop device advertising.
     */
    ret = aci_gap_set_non_discoverable();
    if (ret != BLE_STATUS_SUCCESS)
    {
      Error_Handler(); /* UNEXPECTED */
    }
    APP_FLAG_RESET(APP_FLAG_BLE_ADVERTISING);
  }

  return;
}

/**
* @brief This function initializes the BLE stack and all its modules
* @param None
* @retval None
*/
static void Ble_Hci_Gap_Gatt_Init(void)
{
  uint16_t gap_service_handle, gap_dev_name_char_handle, gap_appearance_char_handle;
  const uint8_t *bd_address;
  uint32_t srd_bd_address[2];
  tBleStatus ret = BLE_STATUS_SUCCESS;
  
  /**
  * BLE HCI Reset to synchronize BLE Stack
  */
  hci_reset();
  
  /**
  * Write the BD Address
  */
  bd_address = Ble_GetBdAddress();
  aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET,
                            CONFIG_DATA_PUBADDR_LEN,
                            (uint8_t*) bd_address);
  
  /**
  * Put the BD address in the manufacturer specific advertising data (for iOS devices)
  */
  ad_manufacturer_specific_data[sizeof(ad_manufacturer_specific_data)-6] = bd_address[5];
  ad_manufacturer_specific_data[sizeof(ad_manufacturer_specific_data)-5] = bd_address[4];
  ad_manufacturer_specific_data[sizeof(ad_manufacturer_specific_data)-4] = bd_address[3];
  ad_manufacturer_specific_data[sizeof(ad_manufacturer_specific_data)-3] = bd_address[2];
  ad_manufacturer_specific_data[sizeof(ad_manufacturer_specific_data)-2] = bd_address[1];
  ad_manufacturer_specific_data[sizeof(ad_manufacturer_specific_data)-1] = bd_address[0];
  
  /**
  * Static random Address
  * The two upper bits shall be set to 1
  * The lowest 32bits is read from the UDN to differentiate between devices
  * The RNG may be used to provide a random number on each power on
  */
  srd_bd_address[1] =  0x0000ED6E;
  srd_bd_address[0] =  LL_FLASH_GetUDN( );
  ret = aci_hal_write_config_data(CONFIG_DATA_RANDOM_ADDRESS_OFFSET,
                                  CONFIG_DATA_RANDOM_ADDRESS_LEN,
                                  (uint8_t*)srd_bd_address);
  
  /**
  * Set TX Power.
  */
  ret = aci_hal_set_tx_power_level(0, CFG_TX_POWER);
  if (ret != BLE_STATUS_SUCCESS)
  {
    Error_Handler(); /* UNEXPECTED */
  }
  
  /**
  * Set Radio activity event mask.
  */
  ret = aci_hal_set_radio_activity_mask(CFG_RADIO_ACTIVITY_EVENT_MASK);
  if (ret != BLE_STATUS_SUCCESS)
  {
    Error_Handler(); /* UNEXPECTED */
  }
  
  /**
  * Initialize GATT
  */
  ret = aci_gatt_init();
  if (ret != BLE_STATUS_SUCCESS)
  {
    Error_Handler(); /* UNEXPECTED */
  }
  
  /**
  * Initialize GAP
  */
  ret = aci_gap_init(GAP_PERIPHERAL_ROLE,
                     0,
                     APP_BLE_GAP_DEVICE_NAME_LENGTH,
                     &gap_service_handle,
                     &gap_dev_name_char_handle,
                     &gap_appearance_char_handle);
  if (ret != BLE_STATUS_SUCCESS)
  {
    Error_Handler(); /* UNEXPECTED */
  }
  
  /**
  * Update GAP Service Device Name characteristic value
  */
  ret = aci_gatt_update_char_value(gap_service_handle,
                                   gap_dev_name_char_handle,
                                   0,
                                   sizeof(gap_device_name),
                                   (uint8_t *)gap_device_name);
  if (ret != BLE_STATUS_SUCCESS)
  {
    Error_Handler(); /* UNEXPECTED */
  }
  
  /**
  * Update GAP Service Appearence characteristic value
  */
  ret = aci_gatt_update_char_value(gap_service_handle,
                                gap_appearance_char_handle,
                                0,
                                sizeof(gap_appearance),
                                (uint8_t *)&gap_appearance);
  if (ret != BLE_STATUS_SUCCESS)
  {
    Error_Handler(); /* UNEXPECTED */
  }
  
  return;
}

/**
* @brief This function generates the unique BD address from the UDN
* @param None
* @retval Pointer to the array holding the BD address
*/
const uint8_t* Ble_GetBdAddress(void)
{
  const uint8_t *bd_address;
  uint32_t udn;
  uint32_t company_id;
  uint32_t device_id;
  
  udn = LL_FLASH_GetUDN();
  
  if(udn != 0xFFFFFFFF)
  {
    company_id = LL_FLASH_GetSTCompanyID();
    device_id = LL_FLASH_GetDeviceID();
    
    bd_address_udn[0] = (uint8_t)(udn & 0x000000FF);
    bd_address_udn[1] = (uint8_t)( (udn & 0x0000FF00) >> 8 );
    bd_address_udn[2] = (uint8_t)( (udn & 0x00FF0000) >> 16 );
    bd_address_udn[3] = (uint8_t)device_id;
    bd_address_udn[4] = (uint8_t)(company_id & 0x000000FF);
    bd_address_udn[5] = (uint8_t)( (company_id & 0x0000FF00) >> 8 );
    
    bd_address = (const uint8_t *)bd_address_udn;
  }
  else {
    Error_Handler(); /* UNEXPECTED */
  }
  
  return bd_address;
}

/**
* @brief  Interrupt service routine that must be called when the system channel
*         reports a packet has been received
*         As stated in AN5289, this API notifies the user that a system user event has been received.
*         The user has to call the shci_user_evt_proc() to process
*         the notification in the system transport layer.
*         As the shci_notify_asynch_evt() notification is called from the IPCC
*         Interrupt Service Routine, it is strongly recommended to implement
*         a background mechanism to call shci_user_evt_proc()
*         (out of IPCC Interrupt Service Routine).
* @param  pdata: Pointer to the packet or event data
* @retval None
*/
void shci_notify_asynch_evt(void* pdata)
{
  APP_FLAG_SET(APP_FLAG_SHCI_EVENT_PENDING);
  return;
}

/**
* @brief As stated in AN5289, this is the system event user callback. It is
*        registered and passed as argument to shci_init() function.
*        This reports the received system user event.
*        The buffer holding the received event is freed on return
*        of this function.
* @param pData: pointer to a structure of tSHCI_UserEvtRxParam type
*
*               typedef struct
*               {
*                 SHCI_TL_UserEventFlowStatus_t status;
*                 TL_EvtPacket_t *pckt;
*               } tSHCI_UserEvtRxParam;
*
*               pckt: holds the address of the received event
*               status: provides a way for user to notify the system transport layer that the received packet
*                       has not been processed and must not be thrown away. When not filled by the user on return
*                       of UserEvtRx(), this parameter is set to SHCI_TL_UserEventFlow_Enable, which means the
*                       user has processed the received event
* @retval None
*/
static void SYS_UserEventReceivedCallback( void * pData )
{
  TL_AsynchEvt_t *p_sys_event;
  SHCI_C2_Ready_Evt_t *p_sys_ready_event;
  SCHI_SystemErrCode_t *p_sys_error_code;

  p_sys_event = (TL_AsynchEvt_t*)(((tSHCI_UserEvtRxParam*)pData)->pckt->evtserial.evt.payload);
  
  /* We have received some event from CPU2, so CPU2 to be considered as running and responding */
  APP_FLAG_SET(APP_FLAG_CPU2_INITIALIZED);
  
  switch(p_sys_event->subevtcode)
  {
  case SHCI_SUB_EVT_CODE_READY:
    p_sys_ready_event = (SHCI_C2_Ready_Evt_t*)p_sys_event->payload;
    if (p_sys_ready_event->sysevt_ready_rsp == WIRELESS_FW_RUNNING)
    {
      APP_FLAG_RESET(APP_FLAG_FUS_FW_RUNNING);
      APP_FLAG_SET(APP_FLAG_WIRELESS_FW_RUNNING);
      /* RF stack installed and ready */
    }
    else if (p_sys_ready_event->sysevt_ready_rsp == FUS_FW_RUNNING)
    {
      APP_FLAG_SET(APP_FLAG_FUS_FW_RUNNING);
      APP_FLAG_RESET(APP_FLAG_WIRELESS_FW_RUNNING);
      
      /* No RF stack installed most probably */
      Error_Handler(); /* UNEXPECTED */
    }
    else {
      APP_FLAG_SET(APP_FLAG_CPU2_ERROR);
      Error_Handler(); /* UNEXPECTED */
    }
    break; /* SHCI_SUB_EVT_CODE_READY */
  case SHCI_SUB_EVT_ERROR_NOTIF:
    APP_FLAG_SET(APP_FLAG_CPU2_ERROR);
    
    p_sys_error_code = (SCHI_SystemErrCode_t*)p_sys_event->payload;
    if (p_sys_error_code == ERR_BLE_INIT)
    {
      /* Error during BLE stack initialization */
      APP_FLAG_SET(APP_FLAG_BLE_INITIALIZATION_ERROR);
      Error_Handler(); /* UNEXPECTED */
    }
    else {
      Error_Handler(); /* UNEXPECTED */
    }
    break; /* SHCI_SUB_EVT_ERROR_NOTIF */
  default:
    break;
  }
  
  ((tSHCI_UserEvtRxParam *)pData)->status = SHCI_TL_UserEventFlow_Disable;
  
  return;
}

/**
* @brief As stated in AN5289, this is the callback used to acknowledge
*        if a system command can be sent. It is registered in shci_init()
*        It must be used in a multi-thread application where the system commands
*        may be sent from different threads.
*  
*        switch (status)
*        {
*        case SHCI_TL_CmdBusy:
*          break;
*        case SHCI_TL_CmdAvailable:
*          break;
*        default:
*          break;
*
* @param status: SHCI_TL_CmdBusy in case the system transport layer is busy and no
*                new system command are be sent, SHCI_TL_CmdAvailable otherwise
* @retval None
*/
static void SYS_StatusNotificationCallback( SHCI_TL_CmdStatus_t status )
{
  /* Callback not implemented - code flow under control of the developer */
  UNUSED(status);
  return;
}

/**
* @brief This function is used to process all events coming from BLE stack by executing the related callback
* @param None
* @retval None
*/
static void SYS_ProcessEvent(void)
{
  if (APP_FLAG_GET(APP_FLAG_SHCI_EVENT_PENDING) == 1)
  {
    APP_FLAG_RESET(APP_FLAG_SHCI_EVENT_PENDING);
    shci_user_evt_proc();
  }
}

/**
* @brief  Callback called from related IPCC RX Interrupt Service Routine, called when the BLE core (CPU2)
*         reports a packet received or an event to the host.
*         As stated in AN5289, this API notifies the user that a BLE user event has been received.
*         The user has to call the hci_user_evt_proc() to process
*         the notification in the BLE transport layer.
*         As the hci_notify_asynch_evt() notification is called from the IPCC
*         Interrupt Service Routine, it is strongly recommended to implement
*         a background mechanism to call hci_user_evt_proc()
*         (out of IPCC Interrupt Service Routine).
* @param  pdata: Pointer to the packet or event data
* @retval None
*/
void hci_notify_asynch_evt(void* pdata)
{
  APP_FLAG_SET(APP_FLAG_HCI_EVENT_PENDING);
  return;
}

/**
* @brief As stated in AN5289, this is the BLE event user callback. It is
*        registered and passed as argument to hci_init() function.
*        This reports the received BLE user event.
*        The buffer holding the received event is freed on return
*        of this function.
* @param pData: pointer to a structure of tHCI_UserEvtRxParam type
*
*               typedef struct
*               {
*                 HCI_TL_UserEventFlowStatus_t status;
*                 TL_EvtPacket_t *pckt;
*               } tHCI_UserEvtRxParam;
*
*               pckt: holds the address of the received event
*               status: provides a way for the user to notify the HCI transport layer that the received packet has not been processed and
*               must not be thrown away. When not filled by the user on return of UserEvtRx(), this parameter is set to HCI_TL_UserEventFlow_Enable
*               which means the user has processed the received event.
* @retval None
*/
static void BLE_UserEventReceivedCallback( void * pData )
{
  SVCCTL_UserEvtFlowStatus_t svctl_return_status;
  tHCI_UserEvtRxParam *pParam;
  
  pParam = (tHCI_UserEvtRxParam *)pData; 
  
  svctl_return_status = SVCCTL_UserEvtRx((void *)&(pParam->pckt->evtserial));
  if (svctl_return_status != SVCCTL_UserEvtFlowDisable)
  {
    pParam->status = HCI_TL_UserEventFlow_Enable;
  }
  else
  {
    pParam->status = HCI_TL_UserEventFlow_Disable;
  }
}

/**
* @brief As stated in AN5289, this is the callback used to acknowledge
*        if a BLE command can be sent. It is registered in hci_init()
*        It must be used in a multi-thread application where the BLE commands
*        may be sent from different threads.
*  
*        switch (status)
*        {
*        case HCI_TL_CmdBusy:
*          break;
*        case HCI_TL_CmdAvailable:
*          break;
*        default:
*          break;
*
* @param status: HCI_TL_CmdBusy in case HCI transport layer is busy and no new
*        BLE command can be sent, HCI_TL_CmdAvailable otherwise
* @retval None
*/
static void BLE_StatusNotificationCallback(HCI_TL_CmdStatus_t status)
{
  /* Callback not implemented - code flow under control of the developer */
  UNUSED(status);
  return;
}

/**
* @brief Read the HSE trimming value from OTP memory
* @param None
* @retval None
*/
static void Tune_HSE(void)
{
/* !!! WARNING !!! Following code is valid only for P-NUCLEO-WB55 boards. 
Code must be reviewed and optionally reimplemented depending on the target HW 
and HSE capacitor tuning value storage location. 
Please read AN5042 - HSE trimming for RF applications using the STM32WB series. */

  OTP_ID0_t * p_otp;
  
  /**
  * Read HSE_Tuning from OTP
  */
  p_otp = (OTP_ID0_t *) OTP_Read(0);
  if (p_otp)
  {
    LL_RCC_HSE_SetCapacitorTuning(p_otp->hse_tuning);
  }
  
  return;
}

/**
* @brief This callback is triggered when either
*          + a GAP event is received from the BLE core device.
*          + a GATT event that has not been positively acknowledged by the registered handler is received from the
*            BLE core device.
*        The event is returned in a HCI packet. The full HCI packet is stored in a single buffer and is available when
*        this callback is triggered. However, an ACI event may be longer than a HCI packet and could be fragmented over
*        several HCI packets. The HCI layer only handles HCI packets so when an ACI packet is split over several HCI
*        packets, this callback is triggered for each HCI fragment. It is the responsibility of the application to
*        reassemble the ACI event.
*        This callback is triggered in the TL_BLE_HCI_UserEvtProc() context
*
* @param  pckt: The user event received from the BLE core device
* @retval None
*/
SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt )
{
  hci_event_pckt *event_pckt;
  evt_blecore_aci *blecore_evt;
  evt_le_meta_event *le_meta_evt;
  
  event_pckt = (hci_event_pckt*) ((hci_uart_pckt *) pckt)->data;
  
  switch (event_pckt->evt)
  {
  case HCI_DISCONNECTION_COMPLETE_EVT_CODE:
    APP_FLAG_RESET(APP_FLAG_BLE_CONNECTED);
    /* Start advertising */
    BLE_Advertising(SET);
    break; /* HCI_DISCONNECTION_COMPLETE_EVT_CODE */
  case HCI_LE_META_EVT_CODE:
    le_meta_evt = (evt_le_meta_event *)(event_pckt->data);
    switch (le_meta_evt->subevent)
    {
    case HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE:
      APP_FLAG_RESET(APP_FLAG_BLE_ADVERTISING);
      APP_FLAG_SET(APP_FLAG_BLE_CONNECTED);
      break; /* HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE */
    default:
      break;
    }
    break; /* HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE */
  case HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE:
    blecore_evt = (evt_blecore_aci*) event_pckt->data;
    switch (blecore_evt->ecode)
    {
    case EVT_END_OF_RADIO_ACTIVITY:
      BSP_LED_Toggle(LED_GREEN);
      break; /* EVT_END_OF_RADIO_ACTIVITY */
    }
    break; /* HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE */
    
  default:
    break;
  }
  
  return (SVCCTL_UserEvtFlowEnable);
}

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  
  while(1)
  {
    BSP_LED_Toggle(LED_RED);
    HAL_Delay(250);
  }
  
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{ 
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
  tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

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