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

app_entry.c « Src « Core « Mac_802_15_4_RFD « Mac_802_15_4 « Applications « P-NUCLEO-WB55.Nucleo « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3a868ccce326a7c50a680e89b46d2e7d56df41eb (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
/**
 ******************************************************************************
 * @file    app_entry.c
 * @author  MCD Application Team
 * @brief   Entry point of the Application
 ******************************************************************************
 * @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
 *
 ******************************************************************************
 */


/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "app_entry.h"


#include "app_rfd_mac_802_15_4.h"
#include "app_conf.h"
#include "utilities_common.h"
#include "utilities_conf.h"
#include "otp.h"
#include "stm32_seq.h"
#include "stm_logging.h"
#include "stm32wbxx_ll_rcc.h"
#include "shci.h"
#include "shci_tl.h"
#include "stm32_lpm.h"
#include "tl_mac_802_15_4.h"
#include "tl.h"
#include "dbg_trace.h"

#include "stm_logging.h"

#define HOST_SYS_EVTCODE                (0xFFU)
#define HOST_SYS_SUBEVTCODE_BASE        (0x9200U)
#define HOST_SYS_SUBEVTCODE_READY        (HOST_SYS_SUBEVTCODE_BASE + 0U)
#define POOL_SIZE (CFG_TL_EVT_QUEUE_LENGTH * 4U * DIVC(( sizeof(TL_PacketHeader_t) + TL_EVENT_FRAME_SIZE ), 4U))

#define C_SIZE_CMD_STRING               256U

extern void APP_RFD_MAC_802_15_4_SetupTask(void);
extern void APP_RFD_MAC_802_15_4_NodeSrvTask(void);
extern void APP_RFD_MAC_802_15_4_NodeDataTask(void);

extern uint8_t g_srvSerReq;
extern uint8_t g_srvDataReq;

/* Global variables  -------------------------------------------------*/
char CommandString[C_SIZE_CMD_STRING];
__IO uint16_t indexReceiveChar = 0U;
__IO uint16_t remainingRxChar = 0U;
__IO uint16_t CptReceiveCmdFromUser = 0U;

__IO uint16_t remainSendChar = 0U;
__IO uint16_t CptSendCmdToUser = 0U;

/* Private function definition -------------------------------------------------*/
PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static TL_MAC_802_15_4_Config_t Mac_802_15_4_ConfigBuffer;

PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t Mac_802_15_4_CmdBuffer;
PLACE_IN_SECTION("MB_MEM2") static uint8_t Mac_802_15_4_NotifRspEvtBuffer[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255U];

PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t EvtPool[POOL_SIZE];
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static TL_CmdPacket_t SystemCmdBuffer;
PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t SystemSpareEvtBuffer[sizeof(TL_PacketHeader_t) + TL_EVT_HDR_SIZE + 255U];

PLACE_IN_SECTION("MB_MEM2") ALIGN(4) char mac_802_15_4_CnfIndNot[C_SIZE_CMD_STRING];

/*----------------------------------------------------------------------------*/
static TL_CmdPacket_t *p_mac_802_15_4_cmdbuffer;
static TL_EvtPacket_t *p_mac_802_15_4_notif_RFCore_to_M4;

static __IO uint32_t  CptReceiveMsgFromRFCore = 0U; /* Debug counter */


/* Global function prototypes -----------------------------------------------*/
size_t DbgTraceWrite(int handle, const unsigned char * buf, size_t bufSize);

/* Private function prototypes -----------------------------------------------*/
static void appe_Tl_Init(void);
static void Led_Init(void);
static void Wait_Getting_Ack_From_RFCore(void);
static void Receive_Ack_From_RFCore(void);
static void Receive_Notification_From_RFCore(void);
static void APPE_StatusNot(SHCI_TL_CmdStatus_t status);
static void APPE_UserEvtRx(void * pPayload);


void APP_ENTRY_Init( APP_ENTRY_InitMode_t InitMode )
{
  /**
   * The Standby mode should not be entered before the initialization is over
   * The default state of the Low Power Manager is to allow the Standby Mode so an request is needed here
   */
  UTIL_LPM_SetOffMode(1U << CFG_LPM_APP, UTIL_LPM_DISABLE);
  Led_Init();
  appe_Tl_Init(); /* Initialize all transport layers */

  /**
   * From now, the application is waiting for the ready event ( VS_HCI_C2_Ready )
   * received on the system channel before starting the BLE Stack
   * This system event is received with APPE_UserEvtRx()
   */

  return;
}

static void appe_Tl_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 */
  UTIL_SEQ_RegTask( 1<< CFG_TASK_SYSTEM_HCI_ASYNCH_EVT, UTIL_SEQ_RFU, shci_user_evt_proc );
  SHci_Tl_Init_Conf.p_cmdbuffer = (uint8_t*)&SystemCmdBuffer;
  SHci_Tl_Init_Conf.StatusNotCallBack = APPE_StatusNot;
  shci_init(APPE_UserEvtRx, (void*) &SHci_Tl_Init_Conf);

  /**< Memory Manager channel initialization */
  tl_mm_config.p_BleSpareEvtBuffer = 0U;
  tl_mm_config.p_SystemSpareEvtBuffer = SystemSpareEvtBuffer;
  tl_mm_config.p_AsynchEvtPool = EvtPool;
  tl_mm_config.AsynchEvtPoolSize = POOL_SIZE;
  TL_MM_Init( &tl_mm_config );

  TL_Enable();

  return;
}

static void Led_Init( void )
{
#if (CFG_LED_SUPPORTED == 1U)
  /**
   * Leds Initialization
   */
  BSP_LED_Init(LED_BLUE);
  BSP_LED_Init(LED_GREEN);
  BSP_LED_Init(LED_RED);
#endif
  return;
}

static void APPE_StatusNot( SHCI_TL_CmdStatus_t status )
{
  return;
}

void APP_ENTRY_Init_RFD(void)
{
}

/**
 * The type of the payload for a system user event is tSHCI_UserEvtRxParam
 * When the system event is both :
 *    - a ready event (subevtcode = SHCI_SUB_EVT_CODE_READY)
 *    - reported by the FUS (sysevt_ready_rsp == FUS_FW_RUNNING)
 * The buffer shall not be released
 * ( eg ((tSHCI_UserEvtRxParam*)pPayload)->status shall be set to SHCI_TL_UserEventFlow_Disable )
 * When the status is not filled, the buffer is released by default
 */
static void APPE_UserEvtRx( void * pPayload )
{
  /* Traces channel initialization */
  TL_TRACES_Init( );

  APP_RFD_MAC_802_15_4_Init(APP_MAC_802_15_4_FULL, &Mac_802_15_4_CmdBuffer);

  UTIL_LPM_SetOffMode(1U << CFG_LPM_APP, UTIL_LPM_ENABLE);

  return;
}

void APP_ENTRY_TL_MAC_802_15_4_Init(void)
{
  Mac_802_15_4_ConfigBuffer.p_Mac_802_15_4_CmdRspBuffer = (uint8_t*)&Mac_802_15_4_CmdBuffer;
  Mac_802_15_4_ConfigBuffer.p_Mac_802_15_4_NotAckBuffer = (uint8_t*)Mac_802_15_4_NotifRspEvtBuffer;
  TL_MAC_802_15_4_Init( &Mac_802_15_4_ConfigBuffer );
}


/**
 * @brief Process the messages coming from the RF Core.
 * @param  None
 * @retval None
 */
void APP_ENTRY_ProcessMsgFromRFCoreTask(void)
{

  if (CptReceiveMsgFromRFCore != 0U)
  {
    /* If CptReceiveMsgFromRFCore is > 1. it means that we did not serve all the events from the radio */
    if (CptReceiveMsgFromRFCore > 1)
    {
      APP_RFD_MAC_802_15_4_Error(ERR_APPLI_REC_MULTI_MSG_FROM_RFCore, 0U);
    }
    else
    {
      MAC_802_15_4_CallBack_Processing();
    }
    /* Reset counter */
    CptReceiveMsgFromRFCore = 0U;
  }
}

/* Received trace buffer from M0 */
void TL_TRACES_EvtReceived( TL_EvtPacket_t * hcievt )
{
#if(CFG_DEBUG_TRACE != 0)
  /* Call write/print function using DMA from dbg_trace */
  /* - Cast to TL_AsynchEvt_t* to get "real" payload (without Sub Evt code 2bytes),
     - (-2) to size to remove Sub Evt Code */
  DbgTraceWrite(1U, (const unsigned char *) ((TL_AsynchEvt_t *)(hcievt->evtserial.evt.payload))->payload, hcievt->evtserial.evt.plen - 2U);
#endif /* CFG_DEBUG_TRACE */
  /* Release buffer */
  TL_MM_EvtDone( hcievt );
}

/**
  * @brief  This function is called by the scheduler each time an event
  *         is pending.
  *
  * @param  evt_waited_bm : Event pending.
  * @retval None
  */
void UTIL_SEQ_EvtIdle( UTIL_SEQ_bm_t task_id_bm, UTIL_SEQ_bm_t evt_waited_bm )
{
  switch(evt_waited_bm)
  {
  case EVENT_ACK_FROM_RFCore_EVT:
    UTIL_SEQ_Run(0);
    break;
  case EVENT_DEVICE_RESET_CNF:
  case EVENT_SET_CNF :
  case EVENT_ASSOCIATE_CNF:
  case EVENT_DATA_CNF:
    UTIL_SEQ_Run(TASK_MSG_FROM_RF_CORE);
    break;
  case EVENT_SYNCHRO_BYPASS_IDLE:
    UTIL_SEQ_SetEvt(EVENT_SYNCHRO_BYPASS_IDLE);
    break;
  default :
    /* default case */
    UTIL_SEQ_Run( UTIL_SEQ_DEFAULT );
    break;
  }
}

void shci_notify_asynch_evt(void* pdata)
{
  UTIL_SEQ_SetTask( 1U<<CFG_TASK_SYSTEM_HCI_ASYNCH_EVT, CFG_SCH_PRIO_0);
  return;
}

void shci_cmd_resp_release(uint32_t flag)
{
  UTIL_SEQ_SetEvt( 1U<< CFG_EVT_SYSTEM_HCI_CMD_EVT_RESP);
  return;
}

void shci_cmd_resp_wait(uint32_t timeout)
{
  UTIL_SEQ_WaitEvt( 1U<< CFG_EVT_SYSTEM_HCI_CMD_EVT_RESP );
  return;
}

/**
  * @brief  Initialisation of the trace mechansimn
  * @param  None
  * @retval None
  */
#if(CFG_DEBUG_TRACE != 0U)
void DbgOutputInit( void )
{
  HW_UART_Init(CFG_DEBUG_TRACE_UART);
}

/**
  * @brief  Management of the traces
  * @param  p_data : data
  * @param  size : size
  * @param  call-back :
  * @retval None
  */
void DbgOutputTraces( uint8_t *p_data, uint16_t size, void (*cb)(void) )
{
  HW_UART_Transmit_DMA(CFG_DEBUG_TRACE_UART, p_data, size, cb);
}

#endif


/*************************************************************
 *
 * WRAP FUNCTIONS
 *
 *************************************************************/

void APP_ENTRY_RegisterCmdBuffer(TL_CmdPacket_t* p_buffer)
{
  p_mac_802_15_4_cmdbuffer = p_buffer;
}

TL_CmdPacket_t* MAC_802_15_4_GetCmdBuffer(void)
{
  return (TL_CmdPacket_t*)p_mac_802_15_4_cmdbuffer;
}

TL_Evt_t* MAC_802_15_4_GetRspPayEvt(void)
{
  return &((TL_EvtPacket_t *)p_mac_802_15_4_cmdbuffer)->evtserial.evt;
}

TL_Evt_t* MAC_802_15_4_GetNotificationBuffer(void)
{
  return &(p_mac_802_15_4_notif_RFCore_to_M4->evtserial.evt);
}

MAC_802_15_4_Notification_t* MAC_802_15_4_GetNotificationPayloadBuffer(void)
{
  return (MAC_802_15_4_Notification_t*)(p_mac_802_15_4_notif_RFCore_to_M4)->evtserial.evt.payload;
}

/**
 * @brief  This function is used to transfer the MAC 802.15.4 commands from the
 *         M4 to the RFCore.
 *
 * @param   pCmdBuffer : pointer to the buffer to send
 * @return  None
 */
void Mac_802_15_4_CmdTransfer(void)
{
  TL_MAC_802_15_4_SendCmd();

  /* Wait completion of cmd */
  Wait_Getting_Ack_From_RFCore();
}

/* For reception of MAC 802.15.4 Cmd return */
void TL_MAC_802_15_4_CmdEvtReceived( TL_EvtPacket_t * Otbuffer )
{
  Receive_Ack_From_RFCore();
}

/* For reception of MAC 802.15.4 Notification from RFCore */
void TL_MAC_802_15_4_NotReceived( TL_EvtPacket_t * Notbuffer )
{
  p_mac_802_15_4_notif_RFCore_to_M4 = Notbuffer;

  Receive_Notification_From_RFCore();
}

/**
  * @brief  This function is called before sending any ot command to the RFCore
  *         core. The purpose of this function is to be able to check if
  *         there are no notifications coming from the RFCore core which are
  *         pending before sending a new ot command.
  * @param  None
  * @retval None
  */
void Mac_802_15_4_PreCmdProcessing(void)
{
  UTIL_SEQ_WaitEvt( EVENT_SYNCHRO_BYPASS_IDLE );
}

/*************************************************************
 *
 * STATIC FUNCTIONS
 *
 *************************************************************/

/**
  * @brief  This function waits for getting an acknowledgment from the RFCore.
  *
  * @param  None
  * @retval None
  */
static void Wait_Getting_Ack_From_RFCore(void)
{
  UTIL_SEQ_WaitEvt(EVENT_ACK_FROM_RFCore_EVT);
}

/**
  * @brief  Receive an acknowledgment from the RFCore core.
  *         Each command send by the M4 to the RFCore are acknowledged.
  *         This function is called under interrupt.
  * @param  None
  * @retval None
  */
static void Receive_Ack_From_RFCore(void)
{
  UTIL_SEQ_SetEvt(EVENT_ACK_FROM_RFCore_EVT);
}

/**
  * @brief  Receive a notification from the RFCore+ through the IPCC.
  *         This function is called under interrupt.
  * @param  None
  * @retval None
  */
static void Receive_Notification_From_RFCore(void)
{
  CptReceiveMsgFromRFCore++;
  UTIL_SEQ_SetTask(TASK_MSG_FROM_RF_CORE,CFG_SCH_PRIO_0);
}


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