-
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.
- Loading branch information
1 parent
e1e4763
commit 6a17c56
Showing
8 changed files
with
226 additions
and
0 deletions.
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ nav: | |
- micro-libs.md | ||
- blinky | ||
- bms-monitor | ||
- ltc1865 |
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,3 @@ | ||
title: Ltc1865 | ||
nav: | ||
- ltc1865.md |
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,23 @@ | ||
# Ltc1865 | ||
|
||
This is a typical voltage reading function for the ltc1865 chip. | ||
|
||
```c | ||
float acquisinator_read_voltage() { | ||
HAL_StatusTypeDef spi_status = HAL_ERROR; | ||
HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET); | ||
uint8_t out[2] = {0}; | ||
size_t byte_count = ltc1865_select_channel_encode(LTC1865L_SE_CH1, out); | ||
spi_status = HAL_SPI_Transmit(&hspi1, out, byte_count, SPI_DEFAULT_TIMEOUT); | ||
acquisinator_spi_error_handler(spi_status); | ||
HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET); | ||
uint16_t cell_value = 0; | ||
HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET); | ||
spi_status = | ||
HAL_SPI_Receive(&hspi1, (uint8_t *)&cell_value, 2, SPI_DEFAULT_TIMEOUT); | ||
acquisinator_spi_error_handler(spi_status); | ||
HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET); | ||
return ltc1865_voltage_decode(LTC1865L_SE_CH1, cell_value); | ||
} | ||
``` | ||
|
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,22 @@ | ||
# LTC1865 | ||
|
||
This is a typical voltage reading function for the ltc1865 chip. | ||
|
||
```c | ||
float acquisinator_read_voltage() { | ||
HAL_StatusTypeDef spi_status = HAL_ERROR; | ||
HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET); | ||
uint8_t out[2] = {0}; | ||
size_t byte_count = ltc1865_select_channel_encode(LTC1865L_SE_CH1, out); | ||
spi_status = HAL_SPI_Transmit(&hspi1, out, byte_count, SPI_DEFAULT_TIMEOUT); | ||
acquisinator_spi_error_handler(spi_status); | ||
HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET); | ||
uint16_t cell_value = 0; | ||
HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET); | ||
spi_status = | ||
HAL_SPI_Receive(&hspi1, (uint8_t *)&cell_value, 2, SPI_DEFAULT_TIMEOUT); | ||
acquisinator_spi_error_handler(spi_status); | ||
HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET); | ||
return ltc1865_voltage_decode(LTC1865L_SE_CH1, cell_value); | ||
} | ||
``` |
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,33 @@ | ||
/** | ||
* @file ltc1865.h | ||
* @brief | ||
* | ||
* @date 10 Feb 2024 | ||
* @author Giacomo Mazzucchi [[email protected]] | ||
*/ | ||
|
||
#ifndef LTC1865_H | ||
#define LTC1865_H | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
/** | ||
* For a typical application you want to use LTC1865L_SE_CH1 and LTC1865L_SE_CH2 | ||
*/ | ||
typedef enum { | ||
LTC1865L_DIFF, | ||
LTC1865L_DIFF_INVERTED, | ||
LTC1865L_SE_CH1, | ||
LTC1865L_SE_CH2, | ||
LTC1865_N_CHANNELS | ||
} ltc1865_channel_t; | ||
|
||
/** | ||
* Please note that `uint8_t *out` must be 2 byte long. | ||
*/ | ||
size_t ltc1865_select_channel_encode(ltc1865_channel_t channel, uint8_t *out); | ||
|
||
float ltc1865_voltage_decode(ltc1865_channel_t channel, uint16_t raw_data); | ||
|
||
#endif // LTC1865_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @file ltc1865.c | ||
* @brief | ||
* | ||
* @date 10 Feb 2024 | ||
* @author Giacomo Mazzucchi [[email protected]] | ||
*/ | ||
|
||
#include "ltc1865.h" | ||
|
||
#define LTC1865_VREF_INT 3300 | ||
|
||
/** | ||
* Please note that `uint8_t *out` must be 2 byte long. | ||
*/ | ||
size_t ltc1865_select_channel_encode(ltc1865_channel_t channel, uint8_t *out) { | ||
if (out == NULL) { | ||
return 0U; | ||
} | ||
switch (channel) { | ||
case LTC1865L_SE_CH1: | ||
out[0] = 0xFF; | ||
out[1] = 0xFF; | ||
break; | ||
case LTC1865L_SE_CH2: | ||
out[0] = 0xAA; | ||
out[1] = 0xAA; | ||
break; | ||
case LTC1865L_DIFF: | ||
out[0] = 0x00; | ||
out[1] = 0x00; | ||
break; | ||
case LTC1865L_DIFF_INVERTED: | ||
out[0] = 0x55; | ||
out[1] = 0x55; | ||
break; | ||
default: | ||
return 0U; | ||
} | ||
return 2U; | ||
} | ||
|
||
float ltc1865_voltage_decode(ltc1865_channel_t channel, uint16_t raw_data) { | ||
return (LTC1865_VREF_INT - | ||
((((raw_data << 8) | (raw_data >> 8)) * LTC1865_VREF_INT) / | ||
(UINT16_MAX))); | ||
} |
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,73 @@ | ||
.PHONY: all test_all clear | ||
|
||
# Build directories | ||
BUILD_DIR=build | ||
BUILD_DEPS_DIR=$(BUILD_DIR)/deps | ||
|
||
# Source and include directories | ||
SRC_DIR=../src | ||
INC_DIR=../inc | ||
UNITY_DIR=../../Unity/src | ||
|
||
# Tools | ||
CC=$(shell command -v gcc || command -v clang || echo /bin/gcc) | ||
SZ=$(shell command -v size) | ||
|
||
# Sources | ||
C_SOURCES=$(wildcard *.c) | ||
DEPS_SOURCES=$(wildcard $(SRC_DIR)/*.c $(UNITY_DIR)/unity.c) | ||
SOURCES=$(C_SOURCES) $(DEPS_SOURCES) | ||
|
||
# Include directories | ||
C_INCLUDES= \ | ||
$(UNITY_DIR) \ | ||
$(INC_DIR) | ||
|
||
# Executables | ||
TARGETS=$(addprefix $(BUILD_DIR)/, $(basename $(C_SOURCES))) | ||
|
||
OPT=-Og | ||
|
||
C_DEFINES= \ | ||
UNITY_OUTPUT_COLOR=1 | ||
|
||
|
||
CFLAGS=$(addprefix -I,$(C_INCLUDES)) $(OPT) -Wall $(addprefix -D,$(C_DEFINES)) | ||
|
||
# List of object files | ||
C_OBJECTS=$(addprefix $(BUILD_DIR)/, $(notdir $(C_SOURCES:.c=.o))) | ||
DEPS_OBJECTS=$(addprefix $(BUILD_DIR)/, $(notdir $(DEPS_SOURCES:.c=.o) $(UNITY_SOURCES:.c=.o))) | ||
OBJECTS=$(C_OBJECTS) $(DEPS_OBJECTS) | ||
vpath %.c $(sort $(dir $(SOURCES))) | ||
|
||
# File with the final test results | ||
TEST_RESULTS=$(BUILD_DIR)/result.txt | ||
|
||
all: $(TARGETS) | ||
|
||
# Build | ||
$(TARGETS): $(OBJECTS) Makefile | ||
$(CC) $@.o $(DEPS_OBJECTS) -o $@ | ||
|
||
$(BUILD_DEPS_DIR)/%.o: %.c Makefile | $(BUILD_DIR) | ||
$(CC) -c $(CFLAGS) $< -o $@ | ||
|
||
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) | ||
$(CC) -c $(CFLAGS) $< -o $@ | ||
|
||
$(BUILD_DIR): $(BUILD_DEPS_DIR) | ||
|
||
$(BUILD_DEPS_DIR): | ||
mkdir -p $@ | ||
|
||
# Run all tests | ||
test_all: $(TARGETS) | ||
@echo -n "" > $(TEST_RESULTS) | ||
@for target in $?; do \ | ||
./$$target | tee -a $(TEST_RESULTS); \ | ||
done | ||
|
||
# Clean all | ||
clean: | ||
rm -rf $(BUILD_DIR) | ||
|
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,24 @@ | ||
/** | ||
* @file test-ltc1865.c | ||
* @brief | ||
* | ||
* @date 10 Feb 2024 | ||
* @author Giacomo Mazzucchi [[email protected]] | ||
*/ | ||
|
||
#include "ltc1865.h" | ||
#include "unity.h" | ||
|
||
void setUp(void) {} | ||
|
||
void tearDown(void) {} | ||
|
||
void test_unity(void) { TEST_ASSERT(1); } | ||
|
||
int main() { | ||
UNITY_BEGIN(); | ||
|
||
RUN_TEST(test_unity); | ||
|
||
UNITY_END(); | ||
} |