Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMU Firmware #27

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
15 changes: 15 additions & 0 deletions projects/imu/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!--
General guidelines
These are just guidelines, not strict rules - document however seems best.
A README for a firmware-only project (e.g. Babydriver, MPXE, bootloader, CAN explorer) should answer the following questions:
- What is it?
- What problem does it solve?
- How do I use it? (with usage examples / example commands, etc)
- How does it work? (architectural overview)
A README for a board project (powering a hardware board, e.g. power distribution, centre console, charger, BMS carrier) should answer the following questions:
- What is the purpose of the board?
- What are all the things that the firmware needs to do?
- How does it fit into the overall system?
- How does it work? (architectural overview, e.g. what each module's purpose is or how data flows through the firmware)
-->
# imu
8 changes: 8 additions & 0 deletions projects/imu/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"libs": [
"FreeRTOS",
"ms-common",
"master"
],
"can": true
}
109 changes: 109 additions & 0 deletions projects/imu/inc/bmi323.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#pragma once

#include "can.h"
#include "log.h"
#include "spi.h"

#define CMD 0x7E
#define WRITE 0x7F
#define READ 0x80
#define FEATURE_IO0 0x10
#define FEATURE_IO1 0x11
#define FEATURE_IO2 0x12
#define FEATURE_IO3 0x13
#define FEATURE_IO_STATUS 0x14
#define ACC_CONF 0x20
#define GYR_CONF 0x21
#define FEATURE_CTRL 0x40
#define INT_STATUS_INT1 0x0D
#define INT_STATUS_INT2 0x0E

#define DUMMY_BYTE 0x00
#define BMI_SET_LOW_BYTE 0x00FF
#define BMI_SET_HIGH_BYTE 0xFF00
#define BMI3_E_NULL_PTR INT8_C(-1)

/*
this might be 13 or 14 idk
#define BMI3_ACC_DP_OFF_XYZ_13_BIT_MASK UINT16_C(0x1FFF)
#define BMI3_ACC_DP_OFF_XYZ_14_BIT_MASK UINT16_C(0x3FFF)
*/
#define BMI3_ACC_DP_DOFFSET_X_MASK UINT16_C(0xFFFF)
#define BMI3_ACC_DP_DOFFSET_Y_MASK UINT16_C(0xFFFF)
#define BMI3_ACC_DP_DOFFSET_Z_MASK UINT16_C(0xFFFF)

#define BMI3_ACC_DP_DGAIN_X_MASK UINT16_C(0x00FF)
#define BMI3_ACC_DP_DGAIN_Y_MASK UINT16_C(0x00FF)
#define BMI3_ACC_DP_DGAIN_Z_MASK UINT16_C(0x00FF)

#define BMI3_GYR_DP_OFF_X_MASK UINT16_C(0x03FF)
#define BMI3_GYR_DP_DGAIN_X_MASK UINT16_C(0x007F)
#define BMI3_GYR_DP_OFF_Y_MASK UINT16_C(0x03FF)
#define BMI3_GYR_DP_DGAIN_Y_MASK UINT16_C(0x007F)
#define BMI3_GYR_DP_OFF_Z_MASK UINT16_C(0x03FF)
#define BMI3_GYR_DP_DGAIN_Z_MASK UINT16_C(0x007F)

/** @brief IMU Accel range */
typedef enum { IMU_ACCEL_RANGE_2G /**< +- 2gs */, IMU_ACCEL_RANGE_4G /**< +- 4gs */, IMU_ACCEL_RANGE_8G /**< +- 8gs */, IMU_ACCEL_RANGE_16G /**< +- 16gs */, NUM_IMU_ACCEL_RANGE } IMUAccelRange;

/** @brief IMU Gyro range */
typedef enum {
IMU_GYRO_RANGE_125_DEG /**< +- 125deg */,
IMU_GYRO_RANGE_250_DEG /**< +- 250deg */,
IMU_GYRO_RANGE_500_DEG /**< +- 500deg */,
IMU_GYRO_RANGE_1000_DEG /**< +- 1000deg */,
IMU_GYRO_RANGE_2000_DEG /**< +- 2000deg */,
NUM_IMU_GYRO_RANGES
} IMUGyroRange;

/** @brief IMU Registers */
typedef enum { REG_BANK_SEL = 0x7F, GYRO_REG_ADDR = 0x06, ACCEL_REG_ADDR = 0x03 } bmi323_registers;

/** @brief IMU Accel Gain and Offset Registers */
typedef enum { ACC_DP_OFF_X = 0x60, ACC_DP_DGAIN_X = 0x61, ACC_DP_OFF_Y = 0x62, ACC_DP_DGAIN_Y = 0x63, ACC_DP_OFF_Z = 0x64, ACC_DP_DGAIN_Z = 0x65 } accel_go_registers;

/** @brief IMU Accel Gain and Offset Registers */
typedef enum { GYR_DP_OFF_X = 0x66, GYR_DP_DGAIN_X = 0x67, GYR_DP_OFF_Y = 0x68, GYR_DP_DGAIN_Y = 0x69, GYR_DP_OFF_Z = 0x6A, GYR_DP_DGAIN_Z = 0x6B } gyro_go_registers;

typedef struct {
uint16_t accel_offset_x;
uint16_t accel_offset_y;
uint16_t accel_offset_z;
uint8_t accel_gain_x;
uint8_t accel_gain_y;
uint8_t accel_gain_z;
} AccelGainOffsetValues;

typedef struct {
uint16_t gyro_offset_x;
uint16_t gyro_offset_y;
uint16_t gyro_offset_z;
uint8_t gyro_gain_x;
uint8_t gyro_gain_y;
uint8_t gyro_gain_z;
} GyroGainOffsetValues;

typedef struct {
int16_t x;
int16_t y;
int16_t z;
} Axes;

typedef struct {
SpiPort spi_port;
SpiSettings spi_settings;
GpioAddress int1;
GpioAddress int2;
IMUAccelRange accel_range;
IMUGyroRange gyro_range;
} bmi323_settings;

typedef struct {
bmi323_settings *settings;
Axes accel;
Axes gyro;
AccelGainOffsetValues accel_go_values;
GyroGainOffsetValues gyro_go_values;
} bmi323_storage;

StatusCode imu_init(bmi323_settings *settings);
26 changes: 26 additions & 0 deletions projects/imu/inc/imu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

/************************************************************************************************
* @file imu.h
*
* @brief Header file for imu
*
* @date 2025-02-01
* @author Midnight Sun Team #24 - MSXVI
************************************************************************************************/

/* Standard library Headers */

/* Inter-component Headers */
#include "can.h"
#include "spi.h"

/* Intra-component Headers */

/**
* @defgroup imu
* @brief imu Firmware
* @{
*/

/** @} */
60 changes: 60 additions & 0 deletions projects/imu/inc/imu_hw_defs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#pragma once

/************************************************************************************************
* @file imu_hw_defs.h
*
* @brief Header file for IMU hardware definitions
*
* @date 2025-01-12
* @author Midnight Sun Team #24 - MSXVI
************************************************************************************************/

/* Standard library Headers */

/* Inter-component Headers */

/* Intra-component Headers */

/**
* @defgroup imu
* @brief imu Firmware
* @{
*/

/************************************************************************************************
* IMU CAN definitions
************************************************************************************************/

/** @brief IMU CAN RX Port */
#define IMU_CAN_RX \
{ .port = GPIO_PORT_B, .pin = 8 }

/** @brief IMU CAN TX Port */
#define IMU_CAN_TX \
{ .port = GPIO_PORT_B, .pin = 9 }

/************************************************************************************************
* IMU SPI definitions
************************************************************************************************/

#define IMU_SPI_PORT SPI_PORT_2

#define IMU_SPI_MOSI \
{ .port = GPIO_PORT_B, .pin = 15 }

#define IMU_SPI_MISO \
{ .port = GPIO_PORT_B, .pin = 14 }

#define IMU_SPI_NSS \
{ .port = GPIO_PORT_B, .pin = 12 }

#define IMU_SPI_SCK \
{ .port = GPIO_PORT_B, .pin = 13 }

#define IMU_SPI_INT1 \
{ .port = GPIO_PORT_B, .pin = 0 }

#define IMU_SPI_INT2 \
{ .port = GPIO_PORT_B, .pin = 1 }

#define SPI_BAUDRATE 2500000
Loading
Loading