diff --git a/.vscode/launch.json b/.vscode/launch.json index 4af936a9..368dc86f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -171,11 +171,11 @@ "type": "platformio-debug", "request": "launch", "name": "PIO Debug", - "executable": "D:/Documents/GitHub/stm32-avionics/.pio/build/blink/firmware.elf", + "executable": "/home/matt/stm32-avionics/.pio/build/blink/firmware.elf", "projectEnvName": "blink", - "toolchainBinDir": "C:/Users/matth/.platformio/packages/toolchain-gccarmnoneeabi/bin", + "toolchainBinDir": "/home/matt/.platformio/packages/toolchain-gccarmnoneeabi/bin", "internalConsoleOptions": "openOnSessionStart", - "svdPath": "C:/Users/matth/.platformio/platforms/atmelsam/misc/svd/ATSAMD21G18A.svd", + "svdPath": "/home/matt/.platformio/platforms/atmelsam/misc/svd/ATSAMD21G18A.svd", "preLaunchTask": { "type": "PlatformIO", "task": "Pre-Debug" @@ -185,21 +185,21 @@ "type": "platformio-debug", "request": "launch", "name": "PIO Debug (skip Pre-Debug)", - "executable": "D:/Documents/GitHub/stm32-avionics/.pio/build/blink/firmware.elf", + "executable": "/home/matt/stm32-avionics/.pio/build/blink/firmware.elf", "projectEnvName": "blink", - "toolchainBinDir": "C:/Users/matth/.platformio/packages/toolchain-gccarmnoneeabi/bin", + "toolchainBinDir": "/home/matt/.platformio/packages/toolchain-gccarmnoneeabi/bin", "internalConsoleOptions": "openOnSessionStart", - "svdPath": "C:/Users/matth/.platformio/platforms/atmelsam/misc/svd/ATSAMD21G18A.svd" + "svdPath": "/home/matt/.platformio/platforms/atmelsam/misc/svd/ATSAMD21G18A.svd" }, { "type": "platformio-debug", "request": "launch", "name": "PIO Debug (without uploading)", - "executable": "D:/Documents/GitHub/stm32-avionics/.pio/build/blink/firmware.elf", + "executable": "/home/matt/stm32-avionics/.pio/build/blink/firmware.elf", "projectEnvName": "blink", - "toolchainBinDir": "C:/Users/matth/.platformio/packages/toolchain-gccarmnoneeabi/bin", + "toolchainBinDir": "/home/matt/.platformio/packages/toolchain-gccarmnoneeabi/bin", "internalConsoleOptions": "openOnSessionStart", - "svdPath": "C:/Users/matth/.platformio/platforms/atmelsam/misc/svd/ATSAMD21G18A.svd", + "svdPath": "/home/matt/.platformio/platforms/atmelsam/misc/svd/ATSAMD21G18A.svd", "loadMode": "manual" } ] diff --git a/common/system/hardware_manager.cpp b/common/system/hardware_manager.cpp index 6adc1884..a0a7f76d 100644 --- a/common/system/hardware_manager.cpp +++ b/common/system/hardware_manager.cpp @@ -288,13 +288,16 @@ static bool inSim = false; static CircularBuffer_s *simRxBuffer = NULL; void hm_hardwareInit() { - - HAL_GPIO_WritePin(accelAdx375CsGpioPort[0], accelAdx375CsPin[0], GPIO_PIN_SET); - HAL_GPIO_WritePin(barometerMs5607CsGpioPort[0], barometerMs5607CsPin[0], GPIO_PIN_SET); - HAL_GPIO_WritePin(imuIcm42688CsGpioPort[0], imuIcm42688CsPin[0], GPIO_PIN_SET); - HAL_GPIO_WritePin(flashMb85rsxCsGpioPort[0], flashMb85rsxCsPin[0], GPIO_PIN_SET); - HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET); - HAL_Delay(10); + HAL_GPIO_WritePin(accelAdx375CsGpioPort[0], accelAdx375CsPin[0], + GPIO_PIN_SET); + HAL_GPIO_WritePin(barometerMs5607CsGpioPort[0], barometerMs5607CsPin[0], + GPIO_PIN_SET); + HAL_GPIO_WritePin(imuIcm42688CsGpioPort[0], imuIcm42688CsPin[0], + GPIO_PIN_SET); + HAL_GPIO_WritePin(flashMb85rsxCsGpioPort[0], flashMb85rsxCsPin[0], + GPIO_PIN_SET); + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET); + HAL_Delay(10); /* Accelerometers */ #if HAS_DEV(ACCEL_H3LIS331DL) @@ -445,8 +448,8 @@ void hm_hardwareInit() { #if HAS_DEV(MAG_LIS3MDL) for (int i = 0; i < NUM_MAG_LIS3MDL; i++) { - hardwareStatusMag[FIRST_ID_MAG_LIS3MDL + i] = magLis3mdl[i].begin( - {imuIcm42688Hspi[0], GPIOB, GPIO_PIN_11}); + hardwareStatusMag[FIRST_ID_MAG_LIS3MDL + i] = + magLis3mdl[i].begin({imuIcm42688Hspi[0], GPIOB, GPIO_PIN_11}); } #endif @@ -577,9 +580,10 @@ void hm_hardwareInit() { HAL_GPIO_WritePin(radioTi915HgmGpioPort[i], radioTi915HgmPin[i], GPIO_PIN_SET); #else - // No CC1190 -- crank output power since no input power limits (and no cc1190 to protect) + // No CC1190 -- crank output power since no input power limits (and no + // cc1190 to protect) tiRadio_setOutputPower(radioTi915 + 0, 15); - #endif // RADIO_TI_TYPE == RADIO_TI_TYPE_CC1200 +#endif // RADIO_TI_TYPE == RADIO_TI_TYPE_CC1200 } #endif // HAS_DEV(RADIO_TI_915) diff --git a/common/system/tasks/cli/task_cli_sense.cpp b/common/system/tasks/cli/task_cli_sense.cpp index 42cc3dcf..13a5da25 100644 --- a/common/system/tasks/cli/task_cli_sense.cpp +++ b/common/system/tasks/cli/task_cli_sense.cpp @@ -107,9 +107,11 @@ void CliTasks::sense() { dtoa(float1, sizeof(float1), data->gpsData[i].generalData.hdop, 3); snprintf(cliStr, sizeof(cliStr), "GPS %d hdop: %s\r\n", i, float1); cli_send(cliStr); - snprintf(cliStr, sizeof(cliStr), "GPS %d sats (num): %u\r\n", i, data->gpsData[i].generalData.satsTracked); + snprintf(cliStr, sizeof(cliStr), "GPS %d sats (num): %u\r\n", i, + data->gpsData[i].generalData.satsTracked); cli_send(cliStr); - snprintf(cliStr, sizeof(cliStr), "GPS %d fix quality: %u\r\n", i, data->gpsData[i].generalData.fixQuality); + snprintf(cliStr, sizeof(cliStr), "GPS %d fix quality: %u\r\n", i, + data->gpsData[i].generalData.fixQuality); cli_send(cliStr); } #endif // HAS_DEV(GPS) diff --git a/common/utils/bit_helper.h b/common/utils/bit_helper.h index c6a05f36..0ee370dd 100644 --- a/common/utils/bit_helper.h +++ b/common/utils/bit_helper.h @@ -10,6 +10,11 @@ #include +#ifdef __cplusplus +#include +#include +#endif + #ifdef __cplusplus extern "C" { #endif @@ -27,19 +32,17 @@ uint16_t combine_to_u16(uint8_t high, uint8_t low); #endif #ifdef __cplusplus -#include -#include namespace std { -template -constexpr T byteswap(T value) noexcept -{ - static_assert(std::has_unique_object_representations_v, - "T may not have padding bits"); - auto value_representation = std::bit_cast>(value); - std::ranges::reverse(value_representation); - return std::bit_cast(value_representation); -} +template +constexpr T byteswap(T value) noexcept { + static_assert(std::has_unique_object_representations_v, + "T may not have padding bits"); + auto value_representation = + std::bit_cast>(value); + std::ranges::reverse(value_representation); + return std::bit_cast(value_representation); } +} // namespace std #endif #endif // COMMON_UTILS_BIT_HELPER_H_ diff --git a/stm32_projects/device_drivers/imu/accel_adx375.cpp b/stm32_projects/device_drivers/imu/accel_adx375.cpp index 81ffd59d..b7941559 100644 --- a/stm32_projects/device_drivers/imu/accel_adx375.cpp +++ b/stm32_projects/device_drivers/imu/accel_adx375.cpp @@ -6,11 +6,11 @@ #if HAS_DEV(ACCEL_ADX375) struct RegisterAddress { - uint8_t address :6; - // if we want the read/write pointer to auto advance - bool multiByteRequest :1; - // set - request is a read. Reset - request is a write - bool isRead :1; + uint8_t address : 6; + // if we want the read/write pointer to auto advance + bool multiByteRequest : 1; + // set - request is a read. Reset - request is a write + bool isRead : 1; }; #define REG_WHOAMI 0 @@ -26,73 +26,80 @@ struct RegisterAddress { constexpr float ACCEL_SENSITIVITY = 20.5 / G_TO_MPS2(1); #define ACCEL_FS G_TO_MPS2(200) +#include + #include "usb_std.h" -#include void CustomPrintf(const char *format, ...) { - char buffer[256]; - va_list args; - va_start(args, format); - int len = vsprintf(buffer, format, args); - usbStd_transmit((uint8_t*) buffer, len); - va_end(args); + char buffer[256]; + va_list args; + va_start(args, format); + int len = vsprintf(buffer, format, args); + usbStd_transmit((uint8_t *)buffer, len); + va_end(args); } bool AccelAdx375::begin(SpiCtrl_t spi_) { - spi = spi_; - - HAL_GPIO_WritePin(spi.port, spi.pin, GPIO_PIN_SET); - HAL_Delay(1); - - // adapted from - // https://github.com/adafruit/Adafruit_ADX375/blob/master/Adafruit_ADX375.cpp - - auto wireID = spi_readRegister(&spi, to_size_type(RegisterAddress { - .address = REG_WHOAMI, .multiByteRequest = false, .isRead = 1 })); - - if (wireID != 0xe5) { - return false; - } - - // Interrupts default to off, but just to be sure - spi_writeRegister(&spi, to_size_type(RegisterAddress { .address = - REG_INT_ENABLE, .multiByteRequest = false, .isRead = 0 }), 0); - - // data format set to right justified/LSB first/sign extended; 3 wire spi - // enabled; interrupts active high - spi_writeRegister(&spi, to_size_type(RegisterAddress { .address = - REG_DATA_FORMAT, .multiByteRequest = false, .isRead = 0 }), 0b00001011); - - // And power up - spi_writeRegister(&spi, to_size_type(RegisterAddress { .address = - REG_POWER_CTL, .multiByteRequest = false, .isRead = 0 }), 0b0001000); - - return true; + spi = spi_; + + HAL_GPIO_WritePin(spi.port, spi.pin, GPIO_PIN_SET); + HAL_Delay(1); + + // adapted from + // https://github.com/adafruit/Adafruit_ADX375/blob/master/Adafruit_ADX375.cpp + + auto wireID = spi_readRegister( + &spi, + to_size_type(RegisterAddress{ + .address = REG_WHOAMI, .multiByteRequest = false, .isRead = 1})); + + if (wireID != 0xe5) { + return false; + } + + // Interrupts default to off, but just to be sure + spi_writeRegister( + &spi, + to_size_type(RegisterAddress{ + .address = REG_INT_ENABLE, .multiByteRequest = false, .isRead = 0}), + 0); + + // data format set to right justified/LSB first/sign extended; 3 wire spi + // enabled; interrupts active high + spi_writeRegister( + &spi, + to_size_type(RegisterAddress{ + .address = REG_DATA_FORMAT, .multiByteRequest = false, .isRead = 0}), + 0b00001011); + + // And power up + spi_writeRegister( + &spi, + to_size_type(RegisterAddress{ + .address = REG_POWER_CTL, .multiByteRequest = false, .isRead = 0}), + 0b0001000); + + return true; } void AccelAdx375::newData() { - - spi_readRegisters(&spi, to_size_type(RegisterAddress { - .address = REG_DATAX0, .multiByteRequest = true, .isRead = true }), - reinterpret_cast(&data.raw), sizeof(data.raw)); - - constexpr Axis3dReal_s accelOffset = { - .x = -2.76, - .y = 6.31, - .z = -5.63 - }; - - // and convert to real units. Note that ticks / (ticks / unit) = unit - data.realMps2.x = data.raw.x / ACCEL_SENSITIVITY; - data.realMps2.y = data.raw.y / ACCEL_SENSITIVITY; - data.realMps2.z = data.raw.z / ACCEL_SENSITIVITY; - - data.realMps2.x += accelOffset.x; - data.realMps2.y += accelOffset.y; - data.realMps2.z += accelOffset.z; + spi_readRegisters( + &spi, + to_size_type(RegisterAddress{ + .address = REG_DATAX0, .multiByteRequest = true, .isRead = true}), + reinterpret_cast(&data.raw), sizeof(data.raw)); + + constexpr Axis3dReal_s accelOffset = {.x = -2.76, .y = 6.31, .z = -5.63}; + + // and convert to real units. Note that ticks / (ticks / unit) = unit + data.realMps2.x = data.raw.x / ACCEL_SENSITIVITY; + data.realMps2.y = data.raw.y / ACCEL_SENSITIVITY; + data.realMps2.z = data.raw.z / ACCEL_SENSITIVITY; + + data.realMps2.x += accelOffset.x; + data.realMps2.y += accelOffset.y; + data.realMps2.z += accelOffset.z; } -double AccelAdx375::getAccelFullscaleMps2() { - return ACCEL_FS; -} +double AccelAdx375::getAccelFullscaleMps2() { return ACCEL_FS; } #endif // HAS_DEV(ACCEL_ADX375) diff --git a/stm32_projects/device_drivers/imu/imu_icm42688.cpp b/stm32_projects/device_drivers/imu/imu_icm42688.cpp index b5556c70..9fbfb09e 100644 --- a/stm32_projects/device_drivers/imu/imu_icm42688.cpp +++ b/stm32_projects/device_drivers/imu/imu_icm42688.cpp @@ -4,8 +4,8 @@ #include "imu_icm42688.h" -#include "const_map.h" #include "bit_helper.h" +#include "const_map.h" // registers in bank 0 #define REG_GYRO_CONFIG0 79 @@ -64,7 +64,7 @@ void ImuIcm42688::setGyroConfig(const GyroFullscale range, }; GYRO_CONFIG0 config0{ .odr = gyroRate, - .reserved=0, + .reserved = 0, .fs = fsSetting.regValue, }; @@ -83,7 +83,7 @@ void ImuIcm42688::setAccelConfig(const AccelFullscale range, }; ACCEL_CONFIG0 config0{ .odr = accelRate, - .reserved=0, + .reserved = 0, .fs = fsSetting.regValue, }; @@ -111,7 +111,7 @@ bool ImuIcm42688::begin(SpiCtrl_t spi_) { setBank(0); volatile auto bank = spi_readRegister(&spi, REG_READ(REG_BANK_SEL)); if (bank != 0) { - return false; + return false; } spi_writeRegister(&spi, REG_WRITE(REG_DEVICE_CONFIG), 1); HAL_Delay(1); // Wait 1ms per datasheet @@ -147,39 +147,38 @@ bool ImuIcm42688::begin(SpiCtrl_t spi_) { } void ImuIcm42688::newData() { + struct [[gnu::packed]] AccelTempRaw { + int16_t temp; + Axis3dRaw_s accel; + Axis3dRaw_s angVel; + }; + AccelTempRaw raw; + spi_readRegisters(&spi, REG_READ(REG_TEMP_DATA1), + reinterpret_cast(&raw), sizeof(raw)); + + // and swap all the numbers around + if constexpr (std::endian::native == std::endian::little) { + for (int i = 0; i < sizeof(raw) / sizeof(int16_t); i++) { + auto member = &raw.temp + i; + *member = std::byteswap(*member); + } + } - struct [[gnu::packed]] AccelTempRaw { - int16_t temp; - Axis3dRaw_s accel; - Axis3dRaw_s angVel; - }; - AccelTempRaw raw; - spi_readRegisters(&spi, REG_READ(REG_TEMP_DATA1), - reinterpret_cast(&raw), sizeof(raw)); - - // and swap all the numbers around - if constexpr (std::endian::native == std::endian::little) { - for (int i = 0; i < sizeof(raw) / sizeof(int16_t); i++) { - auto member = &raw.temp + i; - *member = std::byteswap(*member); - } - } - - data.accelRaw = raw.accel; - data.angVelRaw = raw.angVel; - - // and convert to real units. Note that ticks / (ticks / unit) = unit - float accelSensitivity = G_TO_MPS2(1.0) / accelFS.sensitivity; - float gyroSensitivity = DEG_TO_RAD(1.0) / gyroFS.sensitivity; - data.accelRealMps2.x = raw.accel.x * accelSensitivity; - data.accelRealMps2.y = raw.accel.y * accelSensitivity; - data.accelRealMps2.z = raw.accel.z * accelSensitivity; - data.angVelRealRadps.x = raw.angVel.x * gyroSensitivity; - data.angVelRealRadps.y = raw.angVel.y * gyroSensitivity; - data.angVelRealRadps.z = raw.angVel.z * gyroSensitivity; - - // Convert temp per page 65 - tempC = raw.temp / 132.48 + 25; + data.accelRaw = raw.accel; + data.angVelRaw = raw.angVel; + + // and convert to real units. Note that ticks / (ticks / unit) = unit + float accelSensitivity = G_TO_MPS2(1.0) / accelFS.sensitivity; + float gyroSensitivity = DEG_TO_RAD(1.0) / gyroFS.sensitivity; + data.accelRealMps2.x = raw.accel.x * accelSensitivity; + data.accelRealMps2.y = raw.accel.y * accelSensitivity; + data.accelRealMps2.z = raw.accel.z * accelSensitivity; + data.angVelRealRadps.x = raw.angVel.x * gyroSensitivity; + data.angVelRealRadps.y = raw.angVel.y * gyroSensitivity; + data.angVelRealRadps.z = raw.angVel.z * gyroSensitivity; + + // Convert temp per page 65 + tempC = raw.temp / 132.48 + 25; } double ImuIcm42688::getAccelFullscaleMps2() { return accelFS.fullscale; } diff --git a/stm32_projects/device_drivers/imu/mag_lis3mdl.cpp b/stm32_projects/device_drivers/imu/mag_lis3mdl.cpp index 8951cd16..ca66f5cb 100644 --- a/stm32_projects/device_drivers/imu/mag_lis3mdl.cpp +++ b/stm32_projects/device_drivers/imu/mag_lis3mdl.cpp @@ -62,13 +62,11 @@ bool MagLis3mdl::begin(SpiCtrl_t spi_) { } void MagLis3mdl::newData() { - spi_readRegisters(&spi, - to_size_type(RegisterAddress{ - .address = REG_OUT_X_L, - .multiByteRequest = true, - .isRead = 1}) - , reinterpret_cast(&data.raw), - sizeof(data.raw)); + spi_readRegisters( + &spi, + to_size_type(RegisterAddress{ + .address = REG_OUT_X_L, .multiByteRequest = true, .isRead = 1}), + reinterpret_cast(&data.raw), sizeof(data.raw)); // and convert to real units. Note that ticks / (ticks / unit) = unit data.realGauss.x = data.raw.x / MAG_SENSITIVITY; diff --git a/stm32_projects/device_drivers/imu/reg_helper.h b/stm32_projects/device_drivers/imu/reg_helper.h index c938c34d..93c848dc 100644 --- a/stm32_projects/device_drivers/imu/reg_helper.h +++ b/stm32_projects/device_drivers/imu/reg_helper.h @@ -36,8 +36,9 @@ requires(sizeof(T) <= sizeof(uint64_t)) using size_type_of = size_to_type::result; /** - * @brief Convert a register struct of type T to its standard uint form. Automatically converts structs of size 8, 16, 32, or 64 bits into a uintN_t. - * + * @brief Convert a register struct of type T to its standard uint form. + * Automatically converts structs of size 8, 16, 32, or 64 bits into a uintN_t. + * * @tparam T What kind of register struct we are converting to a size type. */ template