-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bms-monitor): add temperature read utilities
- Loading branch information
Showing
3 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @file temp.c | ||
* @brief Wrapper for temperature measurement with the LTC6811 | ||
* | ||
* @date Apr 17, 2023 | ||
* @author Antonio Gelain [[email protected]] | ||
*/ | ||
|
||
#include "temp.h" | ||
|
||
// TODO: Read temperatures in non blocking mode | ||
HAL_StatusTypeDef temp_read_all_temps( | ||
TEMP_HandleTypeDef * handler, | ||
voltage_raw_t volts[LTC_COUNT][TEMP_LTC_SENSOR_COUNT], | ||
TEMP_ADC_MODE mode, | ||
uint32_t timeout) | ||
{ | ||
HAL_StatusTypeDef status = HAL_ERROR; | ||
|
||
for (size_t i = 0; i < TEMP_LTC_SENSOR_COUNT; ++i) { | ||
TEMP_CONFIG * config = handler->config; | ||
|
||
// Set multiplexer index | ||
for (size_t ltc = 0; ltc < LTC_COUNT; ++ltc) | ||
config[ltc].GPIO = i << 1; // The first GPIO is the ouput therefore the index need to be shifted by one | ||
HAL_StatusTypeDef rd_status = ltc6811_wrcfg(handler); | ||
|
||
if (rd_status == HAL_OK) { | ||
// Start ADC conversion | ||
rd_status = ltc6811_adax(handler, mode, TEMP_MUX_OUT); | ||
|
||
if (rd_status == HAL_OK) { | ||
// Poll for conversion status | ||
uint32_t t0 = HAL_GetTick(); | ||
while ((rd_status = ltc6811_pladc(handler)) != HAL_OK && HAL_GetTick() - t0 < timeout) | ||
; | ||
|
||
if (rd_status == HAL_OK) { | ||
voltage_raw_t aux[LTC_COUNT][TEMP_LTC_REG_AUX_GPIO_COUNT] = { 0 }; | ||
// Read register | ||
rd_status = ltc6811_rdaux(handler, TEMP_AUX_REG_A, aux); | ||
|
||
if (rd_status == HAL_OK) { | ||
// Copy data | ||
for (size_t ltc = 0; ltc < LTC_COUNT; ++ltc) | ||
volts[ltc][i] = aux[ltc][TEMP_MUX_OUT_INDEX]; | ||
} | ||
else if (status == HAL_OK) | ||
status = HAL_ERROR; | ||
} | ||
else if (status == HAL_OK) | ||
status = HAL_ERROR; | ||
} | ||
else if (status == HAL_OK) | ||
status = HAL_ERROR; | ||
} | ||
else if (status == HAL_OK) | ||
status = HAL_ERROR; | ||
} | ||
return status; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/** | ||
* @file temp.h | ||
* @brief Wrapper for temperature measurement with the LTC6811 | ||
* | ||
* @date Apr 17, 2023 | ||
* @author Antonio Gelain [[email protected]] | ||
*/ | ||
|
||
#ifndef TEMP_H | ||
#define TEMP_H | ||
|
||
#include <inttypes.h> | ||
|
||
#include "stm32f4xx_hal.h" | ||
#include "../monitor_config.h" | ||
#include "../ltc6811/ltc6811.h" | ||
|
||
// LTC6811 handler structure | ||
#define TEMP_HandleTypeDef LTC6811_HandleTypeDef | ||
// Config register structure | ||
#define TEMP_CONFIG LTC6811_CFGR | ||
// Status register structure | ||
#define TEMP_STATUS LTC6811_STR | ||
// Communication register structure | ||
#define TEMP_COMM LTC6811_COMM | ||
|
||
// Auxiliary register define aliases | ||
#define TEMP_LTC_REG_AUX_COUNT LTC6811_REG_AUX_COUNT | ||
#define TEMP_LTC_REG_AUX_GPIO_COUNT LTC6811_REG_AUX_GV_COUNT | ||
#define TEMP_LTC_REG_GPIO_COUNT LTC6811_REG_GV_COUNT | ||
#define TEMP_REG_GPIO_COUNT LTC_REG_GV_COUNT | ||
|
||
// ADC conversion mode aliases | ||
#define TEMP_ADC_MODE_NORMAL LTC6811_MD_NORMAL | ||
#define TEMP_ADC_MODE_FAST LTC6811_MD_FAST | ||
#define TEMP_ADC_MODE_FILTERED LTC6811_MD_FILTERED | ||
|
||
// GPIO selection aliases | ||
#define TEMP_GPIO_CHANNEL_1 LTC6811_CHG_GPIO_1 | ||
#define TEMP_GPIO_CHANNEL_2 LTC6811_CHG_GPIO_2 | ||
#define TEMP_GPIO_CHANNEL_3 LTC6811_CHG_GPIO_3 | ||
#define TEMP_GPIO_CHANNEL_4 LTC6811_CHG_GPIO_4 | ||
#define TEMP_GPIO_CHANNEL_5 LTC6811_CHG_GPIO_5 | ||
#define TEMP_SECOND_REF LTC6811_CHG_SECOND_REF | ||
#define TEMP_GPIO_ALL LTC6811_CHG_GPIO_ALL | ||
|
||
// Auxiliary voltage register aliases | ||
#define TEMP_AUX_REG_A LTC6811_AVAR | ||
#define TEMP_AUX_REG_B LTC6811_AVBR | ||
#define TEMP_AUX_REG_COUNT LTC6811_AVXR_COUNT | ||
|
||
/* --- Specific macros for the multiplexer configuration --- */ | ||
|
||
// Number of channel of the multiplexer | ||
#define TEMP_LTC_MUX_CHANNEL_COUNT 4 | ||
// Number of multiplexed temperature sensor on a single LTC | ||
#define TEMP_LTC_SENSOR_COUNT (1 << TEMP_LTC_MUX_CHANNEL_COUNT) | ||
// Total number of multiplexed temperature sensor | ||
#define TEMP_SENSOR_COUNT (LTC_COUNT * TEMP_LTC_SENSOR_COUNT) | ||
|
||
// Ouput channel of the multiplexer on the LTC | ||
#define TEMP_MUX_OUT TEMP_GPIO_CHANNEL_1 | ||
// Input channels of the multiplexer on the LTC | ||
#define TEMP_MUX_IN0 TEMP_GPIO_CHANNEL_2 | ||
#define TEMP_MUX_IN1 TEMP_GPIO_CHANNEL_3 | ||
#define TEMP_MUX_IN2 TEMP_GPIO_CHANNEL_4 | ||
#define TEMP_MUX_IN3 TEMP_GPIO_CHANNEL_5 | ||
|
||
// Index of the output value of the multiplexer | ||
#define TEMP_MUX_OUT_INDEX 0 | ||
|
||
|
||
// Cell voltage register group | ||
typedef LTC6811_CVXR TEMP_CELL_REG; | ||
// Auxiliary voltage register group | ||
typedef LTC6811_AVXR TEMP_AUX_REG; | ||
// Status register group | ||
typedef LTC6811_STXR TEMP_STATUS_REG; | ||
|
||
// ADC conversion mode | ||
typedef LTC6811_MD TEMP_ADC_MODE; | ||
// Permit measurement during discharge | ||
typedef LTC6811_DCP TEMP_DISCHARGE_PERMITTED; | ||
// Cell selection for ADC conversion | ||
typedef LTC6811_CH TEMP_CHANNEL; | ||
// Pull-Up/Pull-Down current for Open Wire conversions | ||
typedef LTC6811_PUP TEMP_PULL_UP; | ||
// GPIO selection for ADC conversion | ||
typedef LTC6811_CHG TEMP_GPIO_CHANNEL; | ||
// Indices of the cells to discharge | ||
typedef LTC6811_DCC TEMP_DISCHARGE_CELL; | ||
|
||
|
||
/** | ||
* @brief Read all raw temperature values from the daisy chain | ||
* @attention It executes multiple wrcfg, adax, pladc and rdaux commands | ||
* to read all the temperatures from the LTCs | ||
* | ||
* @attention !!! DO NOT CALL THIS FUNCTION TOO OFTEN !!! | ||
* | ||
* @details Each row of the volts matrix represet a single LTC | ||
* and each column represent a single temperature value | ||
* | ||
* @param handler The LTC handler structure | ||
* @param volts An array where the result is stored | ||
* @param mode The ADC convrsion mode | ||
* @param timeout The timeout for each conversion and read command | ||
* @return HAL_StatusTypeDef The status of the SPI communication | ||
*/ | ||
HAL_StatusTypeDef temp_read_all_temps( | ||
TEMP_HandleTypeDef * handler, | ||
voltage_raw_t volts[LTC_COUNT][TEMP_LTC_SENSOR_COUNT], | ||
TEMP_ADC_MODE mode, | ||
uint32_t timeout | ||
); | ||
|
||
#endif // TEMP_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters