Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into 81-bmsfaultpriority-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
tszwinglitw committed Jan 16, 2025
2 parents 3565881 + 07a22e3 commit 9721701
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 71 deletions.
18 changes: 9 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
repos:
- repo: local
hooks:
- id: clang_restage
name: Restage formatted files
entry: clang_restage
language: system
pass_filenames: false
always_run: true
repos:
- repo: local
hooks:
- id: clang_restage
name: Restage formatted files
entry: clang_restage
language: system
pass_filenames: false
always_run: true
stages: [pre-commit]
2 changes: 2 additions & 0 deletions cangen/can-messages/mpu.json
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@
},
"points": [
{
"endianness": "little",
"size": 32
}
]
Expand All @@ -1037,6 +1038,7 @@
},
"points": [
{
"endianness": "little",
"size": 32
}
]
Expand Down
16 changes: 7 additions & 9 deletions general/include/pca9539.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,19 @@ PCA 9539 16 bit GPIO expander. Datasheet: https://www.ti.com/lit/ds/symlink/pca
* @brief Pointer to write function
*
*/
typedef int (*WritePtr)(uint16_t dev_addr, uint16_t mem_address,
uint16_t mem_add_size, uint8_t *data, uint16_t size,
int delay);
typedef uint8_t (*PCA_Write)(uint16_t dev_addr, uint8_t reg, uint8_t *data,
uint8_t size);

/**
* @brief Pointer to read function
*
*/
typedef int (*ReadPtr)(uint16_t dev_addr, uint16_t mem_address,
uint16_t mem_add_size, uint8_t *data, uint16_t size,
int delay);
typedef uint8_t (*PCA_Read)(uint16_t dev_addr, uint8_t reg, uint8_t *data,
uint8_t size);

typedef struct {
WritePtr write;
ReadPtr read;
PCA_Write write;
PCA_Read read;

uint16_t dev_addr;
} pca9539_t;
Expand All @@ -66,7 +64,7 @@ typedef struct {
* @param readFunc function pointer to HAL specific read func
* @param dev_addr i2c device address
*/
void pca9539_init(pca9539_t *pca, WritePtr writeFunc, ReadPtr readFunc,
void pca9539_init(pca9539_t *pca, PCA_Write writeFunc, PCA_Read readFunc,
uint8_t dev_addr);

/**
Expand Down
21 changes: 5 additions & 16 deletions general/src/pca9539.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,17 @@

#define REG_SIZE_BITS 8

int pca_write_reg(pca9539_t *pca, uint16_t address, uint8_t *data)
static int pca_write_reg(pca9539_t *pca, uint8_t address, uint8_t *data)
{
//Parameters in the HAL Function
uint16_t mem_add_size = 8;
uint16_t data_size = 1;
int delay = 0xFFFFFFFFU;

return pca->write(pca->dev_addr, address, mem_add_size, data, data_size,
delay);
return pca->write(pca->dev_addr, address, data, 1);
}

int pca_read_reg(pca9539_t *pca, uint16_t address, uint8_t *data)
static int pca_read_reg(pca9539_t *pca, uint8_t address, uint8_t *data)
{
uint16_t mem_add_size = 8;
uint16_t data_size = 1;
int delay = 0xFFFFFFFFU;

return pca->read(pca->dev_addr, address, mem_add_size, data, data_size,
delay);
return pca->read(pca->dev_addr, address, data, 1);
}

void pca9539_init(pca9539_t *pca, WritePtr writeFunc, ReadPtr readFunc,
void pca9539_init(pca9539_t *pca, PCA_Write writeFunc, PCA_Read readFunc,
uint8_t dev_addr)
{
pca->dev_addr = dev_addr << 1u;
Expand Down
47 changes: 29 additions & 18 deletions middleware/include/c_utils.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
#ifndef C_UTILS
#define C_UTILS

/*
* Will retrieve the container of a certain pointer given the container type and
* its pointer Trick pulled from Linux Kernel
*/
#define CONTAINER_OF(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))

#endif /* C_UTILS */

void endian_swap(void *ptr, int size);

/// @brief Reverses the bit order of a byte
/// @param b byte
/// @return the same byte but wuth the bits reversed
unsigned char reverse_bits(unsigned char b);
#ifndef C_UTILS
#define C_UTILS

/*
* Will retrieve the container of a certain pointer given the container type and
* its pointer Trick pulled from Linux Kernel
*/
#define CONTAINER_OF(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))

/*
* Gets a bit of a binary number at desired location
*/
#define NER_GET_BIT(num, bit) ((num >> bit) & 1)

/*
* Sets a bit of a binary number at desired location
*/
#define NER_SET_BIT(num, bit) \
bit < (sizeof(num) * 8) ? (num |= (1UL << bit)) : num

void endian_swap(void *ptr, int size);

/// @brief Reverses the bit order of a byte
/// @param b byte
/// @return the same byte but wuth the bits reversed
unsigned char reverse_bits(unsigned char b);

#endif /* C_UTILS */
8 changes: 8 additions & 0 deletions middleware/simple_ema.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>
#include "simple_ema.h"

void ema_filter(float current_value, float *previous_ema, float alpha)
{
*previous_ema =
(alpha * current_value) + ((1 - alpha) * (*previous_ema));
}
7 changes: 7 additions & 0 deletions middleware/simple_ema.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef SIMPLE_EMA
#define SIMPLE_EMA

// Exponential Moving Average Lowpass Filter
void ema_filter(float current_value, float *previous_ema, float alpha);

#endif /* SIMPLE_EMA */
38 changes: 19 additions & 19 deletions middleware/src/c_utils.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include "c_utils.h"

void endian_swap(void *ptr, int size)
{
char *p = (char *)ptr;
char tmp;
for (int i = 0; i < size / 2; i++) {
tmp = p[i];
p[i] = p[size - i - 1];
p[size - i - 1] = tmp;
}
}

unsigned char reverse_bits(unsigned char b)
{
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
#include "c_utils.h"

void endian_swap(void *ptr, int size)
{
char *p = (char *)ptr;
char tmp;
for (int i = 0; i < size / 2; i++) {
tmp = p[i];
p[i] = p[size - i - 1];
p[size - i - 1] = tmp;
}
}

unsigned char reverse_bits(unsigned char b)
{
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
}

0 comments on commit 9721701

Please sign in to comment.