Skip to content

Commit

Permalink
fewer magic numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
endail committed Nov 24, 2024
1 parent 1ae2967 commit e8ecb53
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 23 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@
"raspberry-pi-pico.cmakeAutoConfigure": true,
"raspberry-pi-pico.useCmakeTools": false,
"raspberry-pi-pico.cmakePath": "${HOME}/.pico-sdk/cmake/v3.28.6/bin/cmake",
"raspberry-pi-pico.ninjaPath": "${HOME}/.pico-sdk/ninja/v1.12.1/ninja"
"raspberry-pi-pico.ninjaPath": "${HOME}/.pico-sdk/ninja/v1.12.1/ninja",
"files.associations": {
"cstdint": "c"
}
}
78 changes: 78 additions & 0 deletions include/hx711_spi_master.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ extern "C" {
#endif

#define HX711_SPI_BAUD_RATE 1000000
#define HX711_SPI_COMMAND_BITS 3
#define HX711_SPI_DATA_BITS 5

typedef enum {
hx711_spi_command_none = 0,
Expand Down Expand Up @@ -146,6 +148,82 @@ uint8_t hx711_spi_gain_to_spi_gain(
hx711_gain_t hx711_spi_spi_gain_to_gain(
const uint8_t spi_gain);

/**
* @brief Combine command and data into a byte for an
* SPI transfer.
*
* @param cmd
* @param data
* @return uint8_t
*/
uint8_t hx711_spi_create_xfer(
const hx711_spi_command_t cmd,
const uint8_t data);

/**
* @brief Extract command and data from the byte
* obtained via SPI transfer.
*
* @param inbyte
* @param cmd
* @param data
*/
void hx711_spi_parse_xfer(
const uint8_t inbyte,
hx711_spi_command_t* const cmd,
uint8_t* const data);

/**
* @brief Check if command is a valid command.
*
* @param cmd
* @return true
* @return false
*/
bool hx711_spi_is_command_valid(
const hx711_spi_command_t cmd);

/**
* @brief Extract command from transfer data.
*
* @param xfer
* @return hx711_spi_command_t
*/
hx711_spi_command_t hx711_spi_get_command_from_xfer(
const uint8_t xfer);

/**
* @brief Insert command into transfer data.
*
* @param cmd
* @param xfer
* @return uint8_t
*/
uint8_t hx711_spi_put_command_in_xfer(
const hx711_spi_command_t cmd,
const uint8_t xfer);

/**
* @brief Extract arbitrary data from transfer
* data.
*
* @param xfer
* @return uint8_t
*/
uint8_t hx711_spi_get_data_from_xfer(
const uint8_t xfer);

/**
* @brief Put arbitrary data into transfer data.
*
* @param data
* @param xfer
* @return uint8_t
*/
uint8_t hx711_spi_put_data_in_xfer(
const uint8_t data,
uint8_t const xfer);

#ifdef __cplusplus
}
#endif
Expand Down
16 changes: 16 additions & 0 deletions include/hx711_spi_slave.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,28 @@ typedef struct {

} hx711_spi_slave_config_t;

/**
* @brief Initialise SPI slave device.
*
* @param hx_spi
* @param hx_spi_config
*/
void hx711_spi_slave_init(
hx711_spi_slave_t* const hx_spi,
const hx711_spi_slave_config_t * const hx_spi_config);

/**
* @brief Stop SPI communication.
*
* @param hx_spi
*/
void hx711_spi_slave_close(hx711_spi_slave_t* const hx_spi);

/**
* @brief Listen for incoming SPI messages and respond.
*
* @param hx_spi
*/
void hx711_spi_slave_listen(hx711_spi_slave_t* const hx_spi);

#ifdef __cplusplus
Expand Down
87 changes: 73 additions & 14 deletions src/hx711_spi_master.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// Copyright (c) 2023 Daniel Robertson
// Copyright (c) 2024 Daniel Robertson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -77,14 +77,14 @@ void hx711_spi_master_set_gain(
assert(hx_spi->_spi != NULL);
assert(hx711_is_gain_valid(gain));

const uint8_t data =
(hx711_spi_gain_to_spi_gain(gain) << 3) |
(uint8_t)hx711_spi_command_set_gain;
const uint8_t xfer = hx711_spi_create_xfer(
hx711_spi_command_set_gain,
hx711_spi_gain_to_spi_gain(gain));

spi_write_blocking(
hx_spi->_spi,
&data,
sizeof(data));
&xfer,
sizeof(xfer));

}

Expand Down Expand Up @@ -114,15 +114,15 @@ void hx711_spi_master_power_up(
assert(hx_spi->_spi != NULL);
assert(hx711_is_gain_valid(gain));

const uint8_t data =
(hx711_spi_gain_to_spi_gain(gain) << 3) |
(uint8_t)hx711_spi_command_set_gain;
const uint8_t xfer = hx711_spi_create_xfer(
hx711_spi_command_power_up,
hx711_spi_gain_to_spi_gain(gain));

spi_write_read_blocking(
hx_spi->_spi,
&data,
&xfer,
NULL,
sizeof(data));
sizeof(xfer));

}

Expand All @@ -132,12 +132,13 @@ void hx711_spi_master_power_down(
assert(hx_spi != NULL);
assert(hx_spi->_spi != NULL);

const uint8_t cmd = (uint8_t)hx711_spi_command_power_down;
const uint8_t xfer = hx711_spi_create_xfer(
hx711_spi_command_power_down, 0);

spi_write_blocking(
hx_spi->_spi,
&cmd,
sizeof(cmd));
&xfer,
sizeof(xfer));

}

Expand All @@ -156,3 +157,61 @@ hx711_gain_t hx711_spi_spi_gain_to_gain(const uint8_t spi_gain) {
assert(hx711_is_gain_valid(gain));
return gain;
}

uint8_t hx711_spi_create_xfer(
const hx711_spi_command_t cmd,
const uint8_t data) {
uint8_t xfer = 0;
xfer = hx711_spi_put_command_in_xfer(cmd, xfer);
xfer = hx711_spi_put_data_in_xfer(data, xfer);
return xfer;
}

void hx711_spi_parse_xfer(
const uint8_t inbyte,
hx711_spi_command_t* const cmd,
uint8_t* const data) {

assert(cmd != NULL);

*cmd = hx711_spi_get_command_from_xfer(inbyte);
assert(hx711_spi_is_command_valid(*cmd));

if(data != NULL) {
*data = hx711_spi_get_data_from_xfer(inbyte);
}

}

bool hx711_spi_is_command_valid(
const hx711_spi_command_t cmd) {
return (uint8_t)cmd <= hx711_spi_command_get_value;
}

hx711_spi_command_t hx711_spi_get_command_from_xfer(
const uint8_t data) {
const hx711_spi_command_t cmd =
(hx711_spi_command_t)(data & ((1 << HX711_SPI_COMMAND_BITS) - 1));
assert(hx711_spi_is_command_valid(cmd));
return cmd;
}

uint8_t hx711_spi_put_command_in_xfer(
const hx711_spi_command_t cmd,
const uint8_t xfer) {
assert(hx711_spi_is_command_valid(cmd));
const uint8_t mask = (1 << HX711_SPI_COMMAND_BITS) - 1;
const uint8_t cmdbits = ((uint8_t)cmd) & mask;
return xfer | cmdbits;
}

uint8_t hx711_spi_get_data_from_xfer(
const uint8_t xfer) {
return xfer >> HX711_SPI_COMMAND_BITS;
}

uint8_t hx711_spi_put_data_in_xfer(
const uint8_t data,
const uint8_t xfer) {
return xfer | (data << HX711_SPI_COMMAND_BITS);
}
28 changes: 20 additions & 8 deletions src/hx711_spi_slave.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,36 +70,48 @@ void hx711_spi_slave_close(hx711_spi_slave_t* const hx_spi) {

void hx711_spi_slave_listen(hx711_spi_slave_t* const hx_spi) {

uint8_t xfer;
hx711_spi_command_t cmd;
uint8_t data;
hx711_gain_t gain;
int32_t out;
int32_t val;

while(true) {

// read one byte containing command
spi_read_blocking(
hx_spi->_spi,
hx711_spi_command_none,
&data,
sizeof(data));
0,
&xfer,
sizeof(xfer));

cmd = (hx711_spi_command_t)(data & 0b00000111);
data = data >> 5;
// parse the xfer into cmd and data
hx711_spi_parse_xfer(
xfer,
&cmd,
&data);

switch(cmd) {

default:
case hx711_spi_command_get_value:
out = hx711_get_value(hx_spi->_hx);
spi_write_blocking(hx_spi->_spi, (uint8_t*)&out, sizeof(out));
val = hx711_get_value(hx_spi->_hx);
spi_write_blocking(
hx_spi->_spi,
(uint8_t*)&val,
sizeof(val));
break;

case hx711_spi_command_set_gain:
gain = hx711_spi_spi_gain_to_gain(data);
hx711_set_gain(hx_spi->_hx, gain);
break;

case hx711_spi_command_power_up:
gain = hx711_spi_spi_gain_to_gain(data);
hx711_power_up(hx_spi->_hx, gain);
break;

case hx711_spi_command_power_down:
hx711_power_down(hx_spi->_hx);
break;
Expand Down

0 comments on commit e8ecb53

Please sign in to comment.