Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Core/Inc/app_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
* Note that certain characteristics and relative descriptors are added automatically during device initialization
* so this parameters should be 9 plus the number of user Attributes
*/
#define CFG_BLE_NUM_GATT_ATTRIBUTES 68
#define CFG_BLE_NUM_GATT_ATTRIBUTES 71

/**
* Maximum supported ATT_MTU size
Expand All @@ -237,7 +237,7 @@
* The total amount of memory needed is the sum of the above quantities for each attribute.
* This parameter is ignored by the CPU2 when CFG_BLE_OPTIONS has SHCI_C2_BLE_INIT_OPTIONS_LL_ONLY flag set
*/
#define CFG_BLE_ATT_VALUE_ARRAY_SIZE (1344)
#define CFG_BLE_ATT_VALUE_ARRAY_SIZE (1385)

/**
* Prepare Write List size in terms of number of packet
Expand Down
167 changes: 161 additions & 6 deletions Docs/ble.md

Large diffs are not rendered by default.

91 changes: 91 additions & 0 deletions FlySight/accel_ble.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/***************************************************************************
** **
** FlySight 2 firmware **
** Copyright 2025 Bionic Avionics Inc. **
** **
** This program is free software: you can redistribute it and/or modify **
** it under the terms of the GNU General Public License as published by **
** the Free Software Foundation, either version 3 of the License, or **
** (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, **
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program. If not, see <http://www.gnu.org/licenses/>. **
** **
****************************************************************************
** Contact: Bionic Avionics Inc. **
** Website: http://flysight.ca/ **
****************************************************************************/

#include <string.h>

#include "accel_ble.h"
#include "ble_config.h"
#include "sensor_odr.h"

static uint8_t s_mask = ACCEL_BLE_DEFAULT_MASK;
static uint16_t s_divider = 1u; /* 1 = every sample, 2 = every 2nd sample, etc. */

void ACCEL_BLE_Init(const FS_Config_Data_t *config)
{
s_mask = ACCEL_BLE_DEFAULT_MASK;

// Calculate or use manual divider
if (config->ble_accel_divider == 0) {
// Auto-calculate based on ODR
s_divider = FS_BLE_CalculateDivider(config->accel_odr,
accel_odr_table,
ACCEL_ODR_TABLE_SIZE);
} else {
// Use manual override
s_divider = config->ble_accel_divider;
}
}

uint8_t ACCEL_BLE_GetMask(void)
{
return s_mask;
}

void ACCEL_BLE_SetMask(uint8_t mask)
{
s_mask = mask;
}

uint16_t ACCEL_BLE_GetDivider(void)
{
return s_divider;
}

void ACCEL_BLE_SetDivider(uint16_t divider)
{
if (divider == 0) divider = 1; /* Minimum divider is 1 */
s_divider = divider;
}

uint8_t ACCEL_BLE_Build(const FS_IMU_Data_t *src, uint8_t *dst)
{
uint8_t *p = dst;

*p++ = s_mask; /* byte 0 : mask */

if (s_mask & ACCEL_BLE_BIT_TIME) { /* time (ms) */
memcpy(p, &src->time, sizeof(src->time)); p += 4;
}

if (s_mask & ACCEL_BLE_BIT_ACCEL) { /* accel (g * 100000) */
memcpy(p, &src->ax, sizeof(src->ax)); p += 4;
memcpy(p, &src->ay, sizeof(src->ay)); p += 4;
memcpy(p, &src->az, sizeof(src->az)); p += 4;
}

if (s_mask & ACCEL_BLE_BIT_TEMPERATURE) { /* temperature (C * 100)*/
memcpy(p, &src->temperature, sizeof(src->temperature)); p += 2;
}

return (uint8_t)(p - dst); /* total payload length */
}
55 changes: 55 additions & 0 deletions FlySight/accel_ble.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/***************************************************************************
** **
** FlySight 2 firmware **
** Copyright 2025 Bionic Avionics Inc. **
** **
** This program is free software: you can redistribute it and/or modify **
** it under the terms of the GNU General Public License as published by **
** the Free Software Foundation, either version 3 of the License, or **
** (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, **
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program. If not, see <http://www.gnu.org/licenses/>. **
** **
****************************************************************************
** Contact: Bionic Avionics Inc. **
** Website: http://flysight.ca/ **
****************************************************************************/

#ifndef ACCEL_BLE_H_
#define ACCEL_BLE_H_

#include <stdint.h>
#include "stm32wbxx_hal.h"
#include "imu.h"
#include "config.h"

/* ------------------------------------------------------------------ */
/* Packet layout control */
/* ------------------------------------------------------------------ */
#define ACCEL_BLE_MAX_LEN 20u

/* Bit-layout of mask byte (MSB first) */
#define ACCEL_BLE_BIT_TIME 0x80u
#define ACCEL_BLE_BIT_ACCEL 0x40u
#define ACCEL_BLE_BIT_TEMPERATURE 0x20u

/* Default mask: all fields enabled */
#define ACCEL_BLE_DEFAULT_MASK 0xE0u

/* ------------------------------------------------------------------ */
/* Public API */
/* ------------------------------------------------------------------ */
void ACCEL_BLE_Init(const FS_Config_Data_t *config);
uint8_t ACCEL_BLE_GetMask(void);
void ACCEL_BLE_SetMask(uint8_t mask);
uint16_t ACCEL_BLE_GetDivider(void);
void ACCEL_BLE_SetDivider(uint16_t divider);
uint8_t ACCEL_BLE_Build(const FS_IMU_Data_t *src, uint8_t *dst);

#endif /* ACCEL_BLE_H_ */
44 changes: 40 additions & 4 deletions FlySight/active_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
#include "state.h"
#include "time.h"
#include "vbat.h"
#include "baro_ble.h"
#include "hum_ble.h"
#include "accel_ble.h"
#include "gyro_ble.h"
#include "mag_ble.h"
#include "ble_config.h"

#define LED_BLINK_MSEC 900
#define LED_BLINK_TICKS (LED_BLINK_MSEC*1000/CFG_TS_TICK_VAL)
Expand Down Expand Up @@ -68,6 +74,8 @@ static void FS_ActiveControl_LED_Timer(void)

void FS_ActiveControl_Init(void)
{
const FS_Config_Data_t *config = FS_Config_Get();

// Set callback functions
FS_GNSS_DataReady_SetCallback(FS_ActiveControl_DataReady_Callback);
FS_GNSS_TimeReady_SetCallback(FS_ActiveControl_TimeReady_Callback);
Expand All @@ -90,6 +98,13 @@ void FS_ActiveControl_Init(void)

// Initialize saved GNSS time
memset(&savedTime, 0, sizeof(FS_GNSS_Time_t));

// Initialize BLE sensor modules with configuration
BARO_BLE_Init(config);
HUM_BLE_Init(config);
ACCEL_BLE_Init(config);
GYRO_BLE_Init(config);
MAG_BLE_Init(config);
}

void FS_ActiveControl_DeInit(void)
Expand Down Expand Up @@ -172,35 +187,50 @@ void FS_ActiveControl_DeInit(void)

void FS_Baro_DataReady_Callback(void)
{
const FS_Baro_Data_t *data = FS_Baro_GetData();

if (state != FS_CONTROL_ACTIVE) return;

if (FS_Config_Get()->enable_logging)
{
// Save to log file
FS_Log_WriteBaroData(FS_Baro_GetData());
FS_Log_WriteBaroData(data);
}

// Update BLE characteristic
Custom_BARO_Update(data);
}

void FS_Hum_DataReady_Callback(void)
{
const FS_Hum_Data_t *data = FS_Hum_GetData();

if (state != FS_CONTROL_ACTIVE) return;

if (FS_Config_Get()->enable_logging)
{
// Save to log file
FS_Log_WriteHumData(FS_Hum_GetData());
FS_Log_WriteHumData(data);
}

// Update BLE characteristic
Custom_HUM_Update(data);
}

void FS_Mag_DataReady_Callback(void)
{
const FS_Mag_Data_t *data = FS_Mag_GetData();

if (state != FS_CONTROL_ACTIVE) return;

if (FS_Config_Get()->enable_logging)
{
// Save to log file
FS_Log_WriteMagData(FS_Mag_GetData());
FS_Log_WriteMagData(data);
}

// Update BLE characteristic
Custom_MAG_Update(data);
}

void FS_ActiveControl_DataReady_Callback(void)
Expand Down Expand Up @@ -271,13 +301,19 @@ void FS_ActiveControl_RawReady_Callback(void)

void FS_IMU_DataReady_Callback(void)
{
const FS_IMU_Data_t *data = FS_IMU_GetData();

if (state != FS_CONTROL_ACTIVE) return;

if (FS_Config_Get()->enable_logging)
{
// Save to log file
FS_Log_WriteIMUData(FS_IMU_GetData());
FS_Log_WriteIMUData(data);
}

// Update BLE characteristics
Custom_ACCEL_Update(data);
Custom_GYRO_Update(data);
}

void FS_VBAT_ValueReady_Callback(void)
Expand Down
11 changes: 11 additions & 0 deletions FlySight/active_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "sensor.h"
#include "state.h"
#include "vbat.h"
#include "ble_config.h"

extern UART_HandleTypeDef huart1;
extern ADC_HandleTypeDef hadc1;
Expand Down Expand Up @@ -70,6 +71,16 @@ void FS_ActiveMode_Init(void)
FS_Config_Read(FS_State_Get()->config_filename);
}

/* Validate BLE configuration */
{
FS_BLE_ValidationResult_t result = FS_BLE_ValidateConfig(FS_Config_Get());
if (!result.valid)
{
isSystemHealthy = false;
FS_Log_WriteEvent("BLE config validation failed: %s", result.error_msg);
}
}

if (FS_Config_Get()->al_mode != 0)
{
/* Initialize ActiveLook interface */
Expand Down
89 changes: 89 additions & 0 deletions FlySight/baro_ble.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/***************************************************************************
** **
** FlySight 2 firmware **
** Copyright 2025 Bionic Avionics Inc. **
** **
** This program is free software: you can redistribute it and/or modify **
** it under the terms of the GNU General Public License as published by **
** the Free Software Foundation, either version 3 of the License, or **
** (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, **
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program. If not, see <http://www.gnu.org/licenses/>. **
** **
****************************************************************************
** Contact: Bionic Avionics Inc. **
** Website: http://flysight.ca/ **
****************************************************************************/

#include <string.h>

#include "baro_ble.h"
#include "ble_config.h"
#include "sensor_odr.h"

static uint8_t s_mask = BARO_BLE_DEFAULT_MASK;
static uint16_t s_divider = 1u; /* 1 = every sample, 2 = every 2nd sample, etc. */

void BARO_BLE_Init(const FS_Config_Data_t *config)
{
s_mask = BARO_BLE_DEFAULT_MASK;

// Calculate or use manual divider
if (config->ble_baro_divider == 0) {
// Auto-calculate based on ODR
s_divider = FS_BLE_CalculateDivider(config->baro_odr,
baro_odr_table,
BARO_ODR_TABLE_SIZE);
} else {
// Use manual override
s_divider = config->ble_baro_divider;
}
}

uint8_t BARO_BLE_GetMask(void)
{
return s_mask;
}

void BARO_BLE_SetMask(uint8_t mask)
{
s_mask = mask;
}

uint16_t BARO_BLE_GetDivider(void)
{
return s_divider;
}

void BARO_BLE_SetDivider(uint16_t divider)
{
if (divider == 0) divider = 1; /* Minimum divider is 1 */
s_divider = divider;
}

uint8_t BARO_BLE_Build(const FS_Baro_Data_t *src, uint8_t *dst)
{
uint8_t *p = dst;

*p++ = s_mask; /* byte 0 : mask */

if (s_mask & BARO_BLE_BIT_TIME) { /* time (ms) */
memcpy(p, &src->time, sizeof(src->time)); p += 4;
}

if (s_mask & BARO_BLE_BIT_PRESSURE) { /* pressure (Pa * 100) */
memcpy(p, &src->pressure, sizeof(src->pressure)); p += 4;
}

if (s_mask & BARO_BLE_BIT_TEMPERATURE) { /* temperature (C * 100)*/
memcpy(p, &src->temperature, sizeof(src->temperature)); p += 2;
}

return (uint8_t)(p - dst); /* total payload length */
}
Loading