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

lowpower_app.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: 484545618aa7f5deb592d33d1d0716a58902bfc4 (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
/**
  ******************************************************************************
  * File Name          : lowpower_app.c
  * Description        : BLE LLD simple demo to control LEDs remotely
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2019-2021 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stdbool.h"
#include "app_common.h"
#include "stm32_seq.h"
#include "stm32_lpm.h"
#include "stm_logging.h"
#include "dbg_trace.h"
#include "ble_hal.h"
#include "ipBLE_lld_public.h"
#include "app_ble_lld.h"
#include "lowpower_app.h"

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

/* Private typedef -----------------------------------------------------------*/
// Allows to pack and unpack user data in payload (must not exceed 255 bytes)
typedef PACKED_STRUCT
{
  uint8_t led;
} userPayload;

/* Private defines -----------------------------------------------------------*/
#define CHANNEL           8 // radio channel
#define POWER             TX_POW_PLUS_6_DB // Transmit power
#define NET_ID            0x5A964129 // network ID, both devices must use the same
#define WAKEUP_US         10000 // delay before starting radio operation
#define RX_TIMEOUT_US     (1*1000*1000) // max delay radio will listen for a packet
#define LED_ON_TIMER      (2*1000*1000/CFG_TS_TICK_VAL) /**< 2000ms */ // delay of Toggle

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

/* Private function prototypes -----------------------------------------------*/
static void radioInit(void);
static void sendStart(void);
static void sendLed1(radioEventType cmd, ActionPacket *ap, void *data, uint8_t size);
static void sendLed2(radioEventType cmd, ActionPacket *ap, void *data, uint8_t size);
static void sendLed3(radioEventType cmd, ActionPacket *ap, void *data, uint8_t size);

/* Private variables -----------------------------------------------*/
static ActionPacket apSend[ACTION_PACKET_NB];

uint8_t SwitchOffGPIO_timer_Id;

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

void LOWPOWER_APP_Init(void)
{
  CheckWirelessFirmwareInfo();

  /* Disable low power */
  UTIL_LPM_SetOffMode(1 << CFG_LPM_APP_BLE_LLD, UTIL_LPM_DISABLE);
  UTIL_LPM_SetStopMode(1 << CFG_LPM_APP_BLE_LLD, UTIL_LPM_DISABLE );

  /* Register tasks for event processing */
  UTIL_SEQ_RegTask(1<<CFG_TASK_TIMER, UTIL_SEQ_RFU, sendStart);

  APP_BLE_LLD_Init();
  HW_TS_Create(CFG_TIM_PROC_ID_ISR, &SwitchOffGPIO_timer_Id, hw_ts_SingleShot, Appli_TS_Callback);

  radioInit();
  HW_TS_Start(SwitchOffGPIO_timer_Id, (uint32_t)LED_ON_TIMER);
  UTIL_LPM_SetStopMode(1 << CFG_LPM_APP_BLE_LLD, UTIL_LPM_DISABLE ); // TO ENABLE

}

static void radioInit(void)
{
  HAL_BLE_LLD_Init(CFG_HS_STARTUP_TIME, true);
  HAL_BLE_LLD_Configure(POWER, CHANNEL, true, CFG_BACK2BACK_TIME, NET_ID);

  ActionPacket *ap1;
  userPayload payload1;
  payload1.led = LED1;
  uint32_t wakeup_time1 = WAKEUP_US;
  // Packet to send
  ap1 = &apSend[APACKET_1];
  ap1->actionPacketNb = APACKET_1;
  ap1->StateMachineNo = STATE_MACHINE_0;
  ap1->ActionTag = TXRX | TIMER_WAKEUP;
  ap1->WakeupTime = wakeup_time1;
  ap1->data = &payload1;
  ap1->dataSize = sizeof(payload1);
  ap1->nextTrue = APACKET_2;
  ap1->nextFalse = APACKET_STOP;
  ap1->callback = sendLed1;
  BLE_LLD_SetReservedArea(ap1);

  ActionPacket *ap2;
  userPayload payload2;
  payload2.led = LED2;
  uint32_t wakeup_time2 = 2*WAKEUP_US;
  // Packet to send
  ap2 = &apSend[APACKET_2];
  ap2->actionPacketNb = APACKET_2;
  ap2->StateMachineNo = STATE_MACHINE_0;
  ap2->ActionTag = TXRX | TIMER_WAKEUP;
  ap2->WakeupTime = wakeup_time2;
  ap2->data = &payload2;
  ap2->dataSize = sizeof(payload2);
  ap2->nextTrue = APACKET_3;
  ap2->nextFalse = APACKET_STOP;
  ap2->callback = sendLed2;
  BLE_LLD_SetReservedArea(ap2);

  ActionPacket *ap3;
  userPayload payload3;
  payload3.led = LED3;
  uint32_t wakeup_time3 = 4*WAKEUP_US;
  // Packet to send
  ap3 = &apSend[APACKET_3];
  ap3->actionPacketNb = APACKET_3;
  ap3->StateMachineNo = STATE_MACHINE_0;
  ap3->ActionTag = TXRX | TIMER_WAKEUP;
  ap3->WakeupTime = wakeup_time3;
  ap3->data = &payload3;
  ap3->dataSize = sizeof(payload3);
  ap3->nextTrue = APACKET_STOP;
  ap3->nextFalse = APACKET_STOP;
  ap3->callback = sendLed3;
  BLE_LLD_SetReservedArea(ap3);
}


static void sendStart(void)
{
  BLE_LLD_MakeActionPacketPending(&apSend[APACKET_1]);
}

static void sendLed1(radioEventType cmd, ActionPacket *ap, void *data, uint8_t size)
{
}

static void sendLed2(radioEventType cmd, ActionPacket *ap, void *data, uint8_t size)
{
}

static void sendLed3(radioEventType cmd, ActionPacket *ap, void *data, uint8_t size)
{
  UTIL_LPM_SetStopMode(1 << CFG_LPM_APP_BLE_LLD, UTIL_LPM_ENABLE ); // TO ENABLE
  HW_TS_Start(SwitchOffGPIO_timer_Id, (uint32_t)LED_ON_TIMER);
}


void Appli_TS_Callback(void)
{
  UTIL_SEQ_SetTask(1U << CFG_TASK_TIMER, CFG_SCH_PRIO_0);
}