Skip to content

Commit

Permalink
ltc1865 driver
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzucchi committed Feb 10, 2024
1 parent e1e4763 commit 6a17c56
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.pages
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ nav:
- micro-libs.md
- blinky
- bms-monitor
- ltc1865
3 changes: 3 additions & 0 deletions docs/ltc1865/.pages
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title: Ltc1865
nav:
- ltc1865.md
23 changes: 23 additions & 0 deletions docs/ltc1865/ltc1865.md
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);
}
```

22 changes: 22 additions & 0 deletions ltc1865/README.md
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);
}
```
33 changes: 33 additions & 0 deletions ltc1865/inc/ltc1865.h
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
47 changes: 47 additions & 0 deletions ltc1865/src/ltc1865.c
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)));
}
73 changes: 73 additions & 0 deletions ltc1865/test/Makefile
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)

24 changes: 24 additions & 0 deletions ltc1865/test/test-ltc1865.c
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();
}

0 comments on commit 6a17c56

Please sign in to comment.