Skip to content

Commit

Permalink
fix mcp2515 hw test
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiCheng-Lu committed Nov 12, 2023
1 parent 6f30724 commit 871dd39
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 188 deletions.
6 changes: 3 additions & 3 deletions libraries/ms-common/inc/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ StatusCode spi_init(SpiPort spi, const SpiSettings *settings);
StatusCode spi_exchange(SpiPort spi, uint8_t *tx_data, size_t tx_len, uint8_t *rx_data,
size_t rx_len);

#ifdef x86
#ifdef MS_PLATFORM_X86
// Reads data from the tx buffer into data
StatusCode spi_get_tx(uint8_t *data, uint8_t len);
StatusCode spi_get_tx(SpiPort spi, uint8_t *data, uint8_t len);

// Writes from data into the rx buffer
StatusCode spi_set_rx(uint8_t *data, uint8_t len);
StatusCode spi_set_rx(SpiPort spi, uint8_t *data, uint8_t len);
#endif
27 changes: 9 additions & 18 deletions libraries/ms-common/src/x86/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "spi_mcu.h"

#define SPI_BUF_SIZE 32
#define SPI_QUEUE_DELAY_MS 0
#define SPI_QUEUE_DELAY_MS 10

typedef struct {
uint8_t tx_buf[SPI_BUF_SIZE];
Expand Down Expand Up @@ -89,31 +89,22 @@ StatusCode spi_exchange(SpiPort spi, uint8_t *tx_data, size_t tx_len, uint8_t *r
}

StatusCode spi_get_tx(SpiPort spi, uint8_t *data, uint8_t len) {
status_ok_or_return(mutex_lock(&s_port[spi].spi_buf.mutex, SPI_TIMEOUT_MS));

uint8_t placeholder = 0; // response during tx
for (size_t tx = 0; tx < len; tx++) {
if (queue_receive(&s_port[spi].spi_buf.tx_queue, &data[tx], SPI_QUEUE_DELAY_MS)) {
queue_reset(&s_port[spi].spi_buf.tx_queue);
mutex_unlock(&s_port[spi].spi_buf.mutex);
return STATUS_CODE_EMPTY;
}
queue_receive(&s_port[spi].spi_buf.tx_queue, &data[tx], SPI_QUEUE_DELAY_MS);
queue_send(&s_port[spi].spi_buf.rx_queue, &placeholder, 0);
}

mutex_unlock(&s_port[spi].spi_buf.mutex);
return STATUS_CODE_OK;
}

StatusCode spi_set_rx(SpiPort spi, uint8_t *data, uint8_t len) {
status_ok_or_return(mutex_lock(&s_buf[spi].mutex, SPI_TIMEOUT_MS));

uint8_t placeholder = 0;
for (size_t rx = 0; rx < len; rx++) {
if (queue_send(&s_port[spi].spi_buf.rx_queue, &data[rx], SPI_QUEUE_DELAY_MS)) {
queue_reset(&s_port[spi].spi_buf.rx_queue);
mutex_unlock(&s_port[spi].spi_buf.mutex);
return STATUS_CODE_RESOURCE_EXHAUSTED;
queue_receive(&s_port[spi].spi_buf.tx_queue, &placeholder, SPI_QUEUE_DELAY_MS);
if (placeholder != 0) {
return STATUS_CODE_INTERNAL_ERROR; // expected spi tx to be 0 during rx.
}
queue_send(&s_port[spi].spi_buf.rx_queue, &data[rx], 0);
}

mutex_unlock(&s_port[spi].spi_buf.mutex);
return STATUS_CODE_OK;
}
1 change: 0 additions & 1 deletion projects/motor_controller/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
],
"can": true,
"mocks": {
"mcp2515_hw": ["spi_init", "spi_tx", "spi_rx", "spi_exchange"],
"motor_can_tx": ["mcp2515_hw_transmit", "mcp2515_hw_init"],
"motor_can_rx": ["mcp2515_hw_transmit", "mcp2515_hw_init"]
}
Expand Down
12 changes: 0 additions & 12 deletions projects/motor_controller/src/mcp2515_hw.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ static const uint8_t s_brp_lookup[NUM_CAN_HW_BITRATES] = {
// SPI commands - See Table 12-1
static void prv_reset() {
uint8_t payload[] = { MCP2515_CMD_RESET };
spi_cs_set_state(s_storage->spi_port, GPIO_STATE_LOW);
spi_tx(s_storage->spi_port, payload, sizeof(payload));
spi_cs_set_state(s_storage->spi_port, GPIO_STATE_HIGH);
}

static void prv_read(uint8_t addr, uint8_t *read_data, size_t read_len) {
Expand All @@ -76,18 +74,14 @@ static void prv_write(uint8_t addr, uint8_t *write_data, size_t write_len) {
payload[1] = addr;
memcpy(&payload[2], write_data, write_len);

spi_cs_set_state(s_storage->spi_port, GPIO_STATE_LOW);
spi_tx(s_storage->spi_port, payload, write_len + 2);
spi_cs_set_state(s_storage->spi_port, GPIO_STATE_HIGH);
}

// See 12.10: *addr = (data & mask) | (*addr & ~mask)
static void prv_bit_modify(uint8_t addr, uint8_t mask, uint8_t data) {
uint8_t payload[] = { MCP2515_CMD_BIT_MODIFY, addr, mask, data };

spi_cs_set_state(s_storage->spi_port, GPIO_STATE_LOW);
spi_tx(s_storage->spi_port, payload, sizeof(payload));
spi_cs_set_state(s_storage->spi_port, GPIO_STATE_HIGH);
}

static uint8_t prv_read_status() {
Expand Down Expand Up @@ -309,23 +303,17 @@ StatusCode mcp2515_hw_transmit(uint32_t id, bool extended, uint8_t *data, size_t
tx_id_regs.registers[1], tx_id_regs.registers[0],
(len & 0xf) | (false << 6), // Reg 3-7 RTR and DLC
};
spi_cs_set_state(s_storage->spi_port, GPIO_STATE_LOW);
spi_tx(s_storage->spi_port, id_payload, sizeof(id_payload));
spi_cs_set_state(s_storage->spi_port, GPIO_STATE_HIGH);

// Load data
uint8_t data_payload[9] = { MCP2515_CMD_LOAD_TX | tx_buf->data };
memcpy(&data_payload[1], data, len);

spi_cs_set_state(s_storage->spi_port, GPIO_STATE_LOW);
spi_tx(s_storage->spi_port, (uint8_t *)&data_payload, len + 1);
spi_cs_set_state(s_storage->spi_port, GPIO_STATE_HIGH);

// Send message
uint8_t send_payload[] = { MCP2515_CMD_RTS | tx_buf->rts };
spi_cs_set_state(s_storage->spi_port, GPIO_STATE_LOW);
spi_tx(s_storage->spi_port, send_payload, sizeof(send_payload));
spi_cs_set_state(s_storage->spi_port, GPIO_STATE_HIGH);

return STATUS_CODE_OK;
}
Expand Down
Loading

0 comments on commit 871dd39

Please sign in to comment.