Skip to content

Commit

Permalink
Bms libraries porting (#217)
Browse files Browse the repository at this point in the history
* Porting

* BMS WIP

* Ported over MVP AFE Driver with our libraries

* Modifications to FSM

* Merged some FSM stuff

* Made some modifications

* Made some more changes

* Some more changes

* More modifications

* Working?

* Linting

* Linting

* Revert

* Linting

* Small fixes

* Small fixes

* Moved crc15

* Removed all AFE related stuff

* Added some files back

* FWXV-232 Add BMS Current Sense

* Fixes

* Format

---------

Co-authored-by: AFailure <[email protected]>
Co-authored-by: vagrant <vagrant@midsunbox>
Co-authored-by: Bafran <[email protected]>
  • Loading branch information
4 people authored Oct 14, 2023
1 parent 50043d5 commit 8ce9b65
Show file tree
Hide file tree
Showing 24 changed files with 1,675 additions and 1 deletion.
8 changes: 8 additions & 0 deletions libraries/ms-common/inc/crc15.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
// crc15 implementation for the LTC6811
#include <stddef.h>
#include <stdint.h>

void crc15_init_table(void);

uint16_t crc15_calculate(uint8_t *data, size_t len);
2 changes: 1 addition & 1 deletion libraries/ms-common/inc/notify.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ typedef enum {
NUM_PUB_TOPICS,
} Topic;

// Gets highest priority (highest value) event available, and clears it
// Gets highest priority (highest value) event available, and clears it
// Returns STATUS_CODE_OK if all events processed, STATUS_CODE_INCOMPLETE if any remaining
// Notification value should be processed until all events are cleared
StatusCode event_from_notification(uint32_t *notification, Event *event);
Expand Down
37 changes: 37 additions & 0 deletions libraries/ms-common/src/crc15.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "crc15.h"

// x^{15} + x^{14} + x^{10} + x^{8} + x^{7} + x^{4} + x^{3} + x^{0}
// so divisor is: 0b1100010110011001 (0xC599)
// 0xC599 - (2^15) == 0x4599
#define CRC_POLYNOMIAL 0x4599

static uint16_t s_crc15_table[256];

void crc15_init_table(void) {
for (uint32_t i = 0; i < 256; ++i) {
uint32_t remainder = i << 7;
for (uint8_t bit = 8; bit > 0; --bit) {
if (remainder & 0x4000) {
// check MSB
remainder = (remainder << 1);
remainder = (remainder ^ CRC_POLYNOMIAL);
} else {
remainder = (remainder << 1);
}
}

s_crc15_table[i] = remainder & 0xFFFF;
}
}

uint16_t crc15_calculate(uint8_t *data, size_t len) {
// CRC should be initialized to 16 (see datasheet p.44)
uint16_t remainder = 16;

for (size_t i = 0; i < len; i++) {
uint16_t addr = ((remainder >> 7) ^ data[i]) & 0xFF;
remainder = (remainder << 8) ^ s_crc15_table[addr];
}

return remainder << 1;
}
63 changes: 63 additions & 0 deletions libraries/ms-drivers/inc/ads1259.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include <stdbool.h>
#include <stdint.h>

#include "ads1259_def.h"
#include "gpio.h"
#include "spi.h"

typedef enum Ads1259StatusCode {
ADS1259_STATUS_CODE_OK = 0,
ADS1259_STATUS_CODE_OUT_OF_RANGE,
ADS1259_STATUS_CODE_CHECKSUM_FAULT,
ADS1259_STATUS_CODE_DATA_NOT_READY,
NUM_ADS1259_STATUS_CODE,
} Ads1259StatusCode;

typedef void (*Ads1259ErrorHandlerCb)(Ads1259StatusCode code, void *context);

typedef struct Ads1259RxData {
uint8_t MSB;
uint8_t MID;
uint8_t LSB;
uint8_t CHK_SUM;
} Ads1259RxData;

typedef union Ads1259ConversionData {
struct {
uint8_t LSB;
uint8_t MID;
uint8_t MSB;
};
uint32_t raw;
} Ads1259ConversionData;

// Initialize Ads1259Settings with SPI settings and error callback function
typedef struct Ads1259Settings {
SpiPort spi_port;
uint32_t spi_baudrate;
GpioAddress mosi;
GpioAddress miso;
GpioAddress sclk;
GpioAddress cs;
Ads1259ErrorHandlerCb handler;
void *error_context;
} Ads1259Settings;

// Static instance of Ads1259Storage must be declared
// ads1259_get_data() reads 24-bit conversion data into 'reading'
typedef struct Ads1259Storage {
Ads1259RxData rx_data;
SpiPort spi_port;
Ads1259ConversionData conv_data;
double reading;
Ads1259ErrorHandlerCb handler;
void *error_context;
} Ads1259Storage;

// Initializes ads1259 - soft-timers and spi must be initialized
StatusCode ads1259_init(Ads1259Storage *storage, Ads1259Settings *settings);

// Gets reading via single conversion mode
StatusCode ads1259_get_conversion_data(Ads1259Storage *storage);
85 changes: 85 additions & 0 deletions libraries/ms-drivers/inc/ads1259_def.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#pragma once

// Voltage reference value
#define EXTERNAL_VREF_V 2.5

// ADS1259 Configuration and control commands

#define ADS1259_WAKEUP 0x02 // Wake up from sleep mode
#define ADS1259_SLEEP 0x04 // Begin Sleep Mode
#define ADS1259_RESET 0x06 // Reset to power-up values
#define ADS1259_START_CONV 0x08 // Start Conversion
#define ADS1259_STOP_CONV 0x0A // Stops all conversions after one in progress is complete
#define ADS1259_START_READ_DATA_CONTINOUS 0x10 // enables the Read Data Continuous mode
#define ADS1259_STOP_READ_DATA_CONTINUOUS 0x11 // cancels the Read Data Continuous mode
#define ADS1259_READ_DATA_BY_OPCODE 0x12 // read the conversion result
#define ADS1259_OFFSET_CALIBRATION 0x18 // performs an offset calibration
#define ADS1259_GAIN_CALIBRATION 0x19 // performs a gain calibration

// ADS1259 READ/WRITE REGISTER COMMANDS
// Each command requires 2 bytes:
// command opcode and register address
// number of registers to read
#define ADS1259_READ_REGISTER 0x20
#define ADS1259_WRITE_REGISTER 0x40

// Samples per second on ADS1259
typedef enum Ads1259DataRate {
ADS1259_DATA_RATE_10 = 0,
ADS1259_DATA_RATE_17,
ADS1259_DATA_RATE_50,
ADS1259_DATA_RATE_60,
ADS1259_DATA_RATE_400,
ADS1259_DATA_RATE_1200,
ADS1259_DATA_RATE_3600,
ADS1259_DATA_RATE_14400,
NUM_ADS1259_DATA_RATE,
} Ads1259DataRate;

// REGISTER ADDRESSES
#define ADS1259_ADDRESS_CONFIG0 0x00
#define ADS1259_ADDRESS_CONFIG1 0x01
#define ADS1259_ADDRESS_CONFIG2 0x02
#define ADS1259_ADDRESS_OFC0 0x03
#define ADS1259_ADDRESS_OFC1 0x04
#define ADS1259_ADDRESS_OFC2 0x05
#define ADS1259_ADDRESS_FSC0 0x06
#define ADS1259_ADDRESS_FSC1 0x07
#define ADS1259_ADDRESS_FSC2 0x08
#define NUM_ADS1259_REGISTERS 0x09

// ADS1259 Register Configurations - LSB to MSB
// These values can be changed based on needed configurations

// CONFIG0
#define ADS1259_SPI_TIMEOUT_ENABLE 0x01 // Enable SPI Timeout
#define ADS1259_INTERNAL_REF_BIAS_ENABLE 0x04 // Enable internal ref bias

// CONFIG1
#define ADS1259_CONVERSION_DELAY_MS 0x0 // Conversion delay not used -> if needed see datasheet
#define ADS1259_VREF_EXTERNAL 0x08 // Enable external voltage reference
#define ADS1259_DIGITAL_FILTER_2 0x10 // Enable SINC2 digital filter
#define ADS1259_CHECK_SUM_ENABLE 0x40 // Enable check sum byte
#define ADS1259_OUT_OF_RANGE_FLAG_ENABLE 0x80 // Enable out of range flag

// CONFIG2
#define ADS1259_SYNCOUT_ENABLE 0x20 // Enable Syncout clock
#define ADS1259_CONVERSION_CONTROL_MODE_PULSE 0x10 // Set Conversion mode to pulse
#define ADS1259_DATA_RATE_SPS ADS1259_DATA_RATE_60

// Offset used in Checksum calculation
#define ADS1259_CHECKSUM_OFFSET 0x9B

typedef enum Ads1259RxByte {
ADS1259_MSB = 0,
ADS1259_MID,
ADS1259_LSB,
ADS1259_CHK_SUM,
NUM_ADS_RX_BYTES,
} Ads1259RxByte;

#define NUM_CONFIG_REGISTERS 3
#define NUM_REGISTER_WRITE_COMM 5
#define CHK_SUM_FLAG_BIT 0x80
#define RX_NEG_VOLTAGE_BIT 0x800000
#define RX_MAX_VALUE 0x1000000
Loading

0 comments on commit 8ce9b65

Please sign in to comment.