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

ble_hal.c « hal « ble_lld « STM32_WPAN « ST « Middlewares - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 02298fb9d5faac6e70dba28bcf64413d0237af0f (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
/**
 ******************************************************************************
  * File Name          : ble_hal.c
  * Description        : BLE HAL based on LLD
  ******************************************************************************
  * @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 functions for a simpler access to radio than LLD.
 * It relies on the LLD API running on application MCU, and so does not depend
 * on an HAL API provided by radio MCU.
 */

/* Includes ------------------------------------------------------------------*/
#include "stdint.h"
#include "stdbool.h"
#include "ble_lld.h"
#include "ble_hal.h"

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

/* Private typedef -----------------------------------------------------------*/

/* Private defines -----------------------------------------------------------*/
#define WAKEUP_TIME_US 1000 

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

/* Private function prototypes -----------------------------------------------*/

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

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

/**
 * @brief Initializes the radio
 *
 * Before actual use, radio must be configured with HAL_BLE_LLD_Configure().
 * This function enables whitening.
 *
 * @param[in] hsStartupTime Startup time (system time unit)
 * @param[in] lsOscInternal Use internal RO for the 32 kHz slow speed clock
 *                          (else external crystal)
 */
uint8_t HAL_BLE_LLD_Init(uint16_t hsStartupTime, bool lsOscInternal)
{
  BLE_LLD_Init(hsStartupTime, lsOscInternal, ENABLE);
  return SUCCESS_0;
}

/**
 * @brief Configures the radio
 *
 * @param[in] txPower Transmit power for outgoing packets
 * @param[in] channel Radio channel
 * @param[in] phy2mbps Use 2Mb/s PHY speed (else 1Mb/s)
 * @param[in] b2bTimeUs Back to back time (us), delay between packet and ACK
 * @param[in] networkId Network ID (access address)
 */

uint8_t HAL_BLE_LLD_Configure(txPower_t txPower,
                              uint8_t channel,
                              bool phy2mbps,
                              uint32_t b2bTimeUs,
                              uint32_t networkId)

{
  BLE_LLD_SetTxPower(txPower);
  BLE_LLD_SetChannel(STATE_MACHINE_0, channel);
  BLE_LLD_SetTx_Rx_Phy(STATE_MACHINE_0,
                       phy2mbps ? TX_PHY_2MBPS : TX_PHY_1MBPS,
                       phy2mbps ? RX_PHY_2MBPS : RX_PHY_1MBPS);
  BLE_LLD_SetBackToBackTime(b2bTimeUs);
  BLE_LLD_SetTxAttributes(STATE_MACHINE_0, networkId);
  return SUCCESS_0;
}

/**
 * @brief Sends one packet without listening for an acknowledge.
 *
 * @param[in] txBuffer Data to transmit (header+length+payload) 
 * @param[in] callback Function that will be called once radio operation is done
 *
 * @retval SUCCESS_0 if success
 * @retval INVALID_PARAMETER_C0 if invalid parameters
 * @retval RADIO_BUSY_C4 if radio is busy
 */
uint8_t HAL_BLE_LLD_SendPacket(void *txBuffer,
                           uint8_t size,
                           lldCallback_t *callback)
{
  ActionPacket *ap;

  // Packet to send
  ap = &actPack[APACKET_0];
  ap->actionPacketNb = APACKET_0;
  ap->StateMachineNo = STATE_MACHINE_0;
  ap->ActionTag = TXRX | TIMER_WAKEUP;
  ap->WakeupTime = WAKEUP_TIME_US;
  ap->data = txBuffer;
  ap->dataSize = size;
  ap->nextTrue = APACKET_STOP;
  ap->nextFalse = APACKET_STOP;
  ap->callback = callback;
  BLE_LLD_SetReservedArea(ap);

  BLE_LLD_MakeActionPacketPending(ap);
  return SUCCESS_0;
}



/**
 * @brief Sends one packet and listen for an acknowledge.
 *
 * @param[in] txBuffer Data to transmit (header+length+payload)
 * @param[in] receiveTimeout Timeout for ACK reception (us)
 * @param[in] callback Function that will be called once radio operation is done
 *
 * @retval SUCCESS_0 if success
 * @retval INVALID_PARAMETER_C0 if invalid parameters
 * @retval RADIO_BUSY_C4 if radio is busy
 */
uint8_t HAL_BLE_LLD_SendPacketWithAck(void *txBuffer,
                                  uint8_t size,
                                  uint32_t receiveTimeout,
                                  lldCallback_t *callback)
{
  ActionPacket *ap;

  // Packet to send
  ap = &actPack[APACKET_0];
  ap->actionPacketNb = APACKET_0;
  ap->StateMachineNo = STATE_MACHINE_0;
  ap->ActionTag = TXRX | TIMER_WAKEUP;
  ap->WakeupTime = WAKEUP_TIME_US;
  ap->data = txBuffer;
  ap->dataSize = size;
  ap->nextTrue = APACKET_1;
  ap->nextFalse = APACKET_STOP;
  ap->callback = callback;
  BLE_LLD_SetReservedArea(ap);

  // ACK to receive
  ap = &actPack[APACKET_1];
  ap->actionPacketNb = APACKET_1;
  ap->StateMachineNo = STATE_MACHINE_0;
  ap->ActionTag = 0;
  ap->ReceiveWindowLength = receiveTimeout;
  ap->nextTrue = APACKET_STOP;
  ap->nextFalse = APACKET_STOP;
  ap->callback = callback;
  BLE_LLD_SetReservedArea(ap);

  BLE_LLD_MakeActionPacketPending(&actPack[APACKET_0]);
  return SUCCESS_0;
}

/**
 * @brief Receives one packet without transmitting an acknowledge.
 *
 * @param[in] receiveTimeout Timeout for data reception (us)
 * @param[in] callback Function that will be called once radio operation is done
 *
 * @retval SUCCESS_0 if success
 * @retval INVALID_PARAMETER_C0 if invalid parameters
 * @retval RADIO_BUSY_C4 if radio is busy
 */
uint8_t HAL_BLE_LLD_ReceivePacket(uint32_t receiveTimeout,
                              lldCallback_t *callback)
{
  ActionPacket *ap;

  // Packet to receive
  ap = &actPack[APACKET_2];
  ap->actionPacketNb = APACKET_2;
  ap->StateMachineNo = STATE_MACHINE_0;
  ap->ActionTag = TIMER_WAKEUP;
  ap->WakeupTime = WAKEUP_TIME_US;
  ap->ReceiveWindowLength = receiveTimeout;
  ap->nextTrue = APACKET_STOP;
  ap->nextFalse = APACKET_STOP;
  ap->callback = callback;
  BLE_LLD_SetReservedArea(ap);

  BLE_LLD_MakeActionPacketPending(ap);
  return SUCCESS_0;
}

/**
 * @brief Receives one packet and transmit an acknowledge.
 *
 * @param[in] txBuffer Acknowledge to transmit
 * @param[in] receiveTimeout Timeout for data reception (us)
 * @param[in] callback Function that will be called once radio operation is done
 *
 * @retval SUCCESS_0 if success
 * @retval INVALID_PARAMETER_C0 if invalid parameters
 * @retval RADIO_BUSY_C4 if radio is busy
 */
uint8_t HAL_BLE_LLD_ReceivePacketWithAck(void *txBuffer,
                                     uint8_t size,
                                     uint32_t receiveTimeout,
                                     lldCallback_t *callback)
{
  ActionPacket *ap;

  // Packet to receive
  ap = &actPack[APACKET_2];
  ap->actionPacketNb = APACKET_2;
  ap->StateMachineNo = STATE_MACHINE_0;
  ap->ActionTag = TIMER_WAKEUP;
  ap->WakeupTime = WAKEUP_TIME_US;
  ap->ReceiveWindowLength = receiveTimeout;
  ap->nextTrue = APACKET_3;
  ap->nextFalse = APACKET_STOP;
  ap->callback = callback;
  BLE_LLD_SetReservedArea(ap);

  // ACK to send
  ap = &actPack[APACKET_3];
  ap->actionPacketNb = APACKET_3;
  ap->StateMachineNo = STATE_MACHINE_0;
  ap->ActionTag = TXRX;
  ap->data = txBuffer;
  ap->dataSize = size;
  ap->nextTrue = APACKET_STOP;
  ap->nextFalse = APACKET_STOP;
  ap->callback = callback;
  BLE_LLD_SetReservedArea(ap);

  BLE_LLD_MakeActionPacketPending(&actPack[APACKET_2]);
  return SUCCESS_0;
}

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