-
Notifications
You must be signed in to change notification settings - Fork 127
spi_transfer Refactor Pointers into std::array #3755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
1c189cb
2a74486
81e3ad4
979480e
d1818d3
5d06fcf
9540d69
fb35e5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -582,7 +582,7 @@ uint8_t TmcMotorController::readWriteByte(const uint8_t motor, const uint8_t dat | |
| // The first byte should contain the address on a read operation. | ||
| // Trigger a transfer (1 byte) and buffer the response (4 bytes) | ||
| tx_[position_] = data; | ||
| spiTransfer(file_descriptors_[motor], tx_, rx_, 5, spi_speed); | ||
| spiTransfer<5>(file_descriptors_[motor], tx_, rx_, spi_speed); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a constant, not a magic number |
||
|
|
||
| currently_reading_ = true; | ||
| currently_writing_ = false; | ||
|
|
@@ -608,7 +608,7 @@ uint8_t TmcMotorController::readWriteByte(const uint8_t motor, const uint8_t dat | |
| { | ||
| // we have all the bytes for this transfer, lets trigger the transfer and | ||
| // reset state | ||
| spiTransfer(file_descriptors_[motor], tx_, rx_, 5, spi_speed); | ||
| spiTransfer<5>(file_descriptors_[motor], tx_, rx_, spi_speed); | ||
| transfer_started_ = false; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,13 +202,13 @@ class TmcMotorController : public MotorController | |
| std::unique_ptr<Gpio> reset_gpio_; | ||
|
|
||
| // Transfer Buffers for spiTransfer | ||
| uint8_t tx_[5] = {}; | ||
| uint8_t rx_[5] = {}; | ||
| std::array<uint8_t, 5> tx_ = {}; | ||
| std::array<uint8_t, 5> rx_ = {}; | ||
|
|
||
| // Transfer Buffers for readThenWriteSpiTransfer | ||
| uint8_t write_tx_[5] = {}; | ||
| uint8_t read_tx_[5] = {}; | ||
| uint8_t read_rx_[5] = {}; | ||
| std::array<uint8_t, 5> write_tx_ = {}; | ||
| std::array<uint8_t, 5> read_tx_ = {}; | ||
| std::array<uint8_t, 5> read_rx_ = {}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. more magic numbers |
||
|
|
||
| // Transfer State | ||
| bool transfer_started_ = false; | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. template header files should be names .hpp |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,9 @@ | ||
| #pragma once | ||
|
|
||
| #include <string.h> | ||
|
|
||
| #include <array> | ||
| #include <cstdint> | ||
|
|
||
| // TODO: #3747 Wrap in spi_utils namespace | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might as well do this TODO |
||
| // TODO: #3751 Use std::array instead of raw pointers for tx and rx buffers. | ||
| /** | ||
| * Trigger an SPI transfer over an open SPI connection | ||
| * | ||
|
|
@@ -14,11 +12,10 @@ | |
| * @param fd the SPI file descriptor to transfer data over | ||
| * @param tx the tx buffer, data to send out | ||
| * @param rx the rx buffer, will be updated with data from the full-duplex transfer | ||
| * @param len the length of the tx and rx buffer | ||
| * @param spi_speed the speed to run spi at in Hz | ||
| * | ||
| */ | ||
| void spiTransfer(int fd, uint8_t const* tx, uint8_t const* rx, unsigned len, | ||
| template <unsigned len> | ||
| void spiTransfer(int fd, const std::array<uint8_t, len>& tx, std::array<uint8_t, len>& rx, | ||
| uint32_t spi_speed); | ||
|
|
||
| /** | ||
|
|
@@ -32,7 +29,7 @@ void spiTransfer(int fd, uint8_t const* tx, uint8_t const* rx, unsigned len, | |
| * @param read_rx the buffer our read response will be placed in | ||
| * @param spi_speed the speed to run spi at in Hz | ||
| */ | ||
| void readThenWriteSpiTransfer( | ||
| int fd, const uint8_t* read_tx, const uint8_t* write_tx, const uint8_t* read_rx, | ||
| const uint32_t read_len, const uint32_t write_len, | ||
| uint32_t spi_speed); // refactor to take std::array, not raw pointers | ||
| template <uint32_t read_len, uint32_t write_len> | ||
| void readThenWriteSpiTransfer(int fd, const std::array<uint8_t, read_len>& read_tx, | ||
| const std::array<uint8_t, write_len>& write_tx, | ||
| std::array<uint8_t, read_len>& read_rx, uint32_t spi_speed); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The template param can be deduced from the length of
txandrx, so you do not need to explicitly specify it with<FRAME_LEN>. There are similar cases in tmc_motor_controller.cpp