49 lines
1.3 KiB
C++
49 lines
1.3 KiB
C++
#include <string.h>
|
|
|
|
#include "mcp2521.hpp"
|
|
#include "reg.hpp"
|
|
|
|
void MCP2521_SPI_Interface::reset() {
|
|
spi_transaction_ext_t t = {
|
|
.base = {
|
|
.flags = SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR,
|
|
.cmd = MCP2521_OP_RESET,
|
|
.addr = 8,
|
|
.length = 0,
|
|
.rxlength = 0,
|
|
.tx_buffer = NULL,
|
|
.rx_buffer = NULL
|
|
},
|
|
.command_bits = 8,
|
|
.address_bits = 0,
|
|
.dummy_bits = 0,
|
|
};
|
|
|
|
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
|
}
|
|
|
|
void MCP2521_SPI_Interface::read_reg(uint8_t address, uint8_t *data, size_t length) {
|
|
spi_transaction_ext_t t = {
|
|
.base = {
|
|
.flags = SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR,
|
|
.cmd = MCP2521_OP_READ,
|
|
.addr = address,
|
|
.length = 8 * length,
|
|
.rxlength = 8 * length,
|
|
.tx_buffer = &this->spi_tx_buffer,
|
|
.rx_buffer = &this->spi_rx_buffer,
|
|
},
|
|
.command_bits = 8,
|
|
.address_bits = 8,
|
|
.dummy_bits = 0,
|
|
};
|
|
|
|
spi_device_transmit(this->spi_device_handle, (spi_transaction_t*)(&t));
|
|
memcpy(data, this->spi_rx_buffer, length);
|
|
}
|
|
|
|
uint8_t MCP2521_SPI_Interface::read_reg(uint8_t address) {
|
|
uint8_t data;
|
|
read_reg(address, &data, 1);
|
|
return data;
|
|
} |