Skip to content

Commit

Permalink
Add full-duplex transfer function, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
renzenicolai committed Jan 4, 2022
1 parent 3c7ce5b commit 9a42035
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
33 changes: 22 additions & 11 deletions ice40.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "include/ice40.h"

static const char *TAG = "hardware";
static const char *TAG = "ice40";

// SPI communication

Expand All @@ -38,28 +38,39 @@ static void IRAM_ATTR ice40_post_transaction_cb(spi_transaction_t* transaction)
}
}

esp_err_t ice40_send(ICE40* device, const uint8_t* data, int length) {
if (length == 0) return ESP_OK;
esp_err_t ice40_send(ICE40* device, const uint8_t* data, uint32_t length) {
if (device->spi_device == NULL) return ESP_FAIL;
spi_transaction_t t = {
spi_transaction_t transaction = {
.user = (void*) device,
.length = length * 8,
.tx_buffer = data,
.rx_buffer = NULL
};
return spi_device_transmit(device->spi_device, &t);
return spi_device_transmit(device->spi_device, &transaction);
}

esp_err_t ice40_receive(ICE40* device, uint8_t* data, int length) {
if (length == 0) return ESP_OK;
esp_err_t ice40_receive(ICE40* device, uint8_t* data, uint32_t length) {
if (device->spi_device == NULL) return ESP_FAIL;
spi_transaction_t t = {
spi_transaction_t transaction = {
.user = (void*) device,
.length = length * 8,
.length = 0,
.rxlength = length * 8,
.tx_buffer = NULL,
.rx_buffer = data
};
return spi_device_transmit(device->spi_device, &t);
return spi_device_transmit(device->spi_device, &transaction);
}

esp_err_t ice40_transaction(ICE40* device, uint8_t* data_out, uint32_t out_length, uint8_t* data_in, uint32_t in_length) {
if (device->spi_device == NULL) return ESP_FAIL;
spi_transaction_t transaction = {
.user = (void*) device,
.length = out_length * 8,
.rxlength = in_length * 8,
.tx_buffer = data_out,
.rx_buffer = data_in
};
return spi_device_transmit(device->spi_device, &transaction);
}

// Device state management
Expand Down Expand Up @@ -170,7 +181,7 @@ esp_err_t ice40_init(ICE40* device) {
.mode = 0,
.spics_io_num = -1,
.queue_size = 1,
.flags = (SPI_DEVICE_HALFDUPLEX | SPI_DEVICE_3WIRE),
.flags = 0,
.pre_cb = ice40_pre_transaction_cb,
.post_cb = ice40_post_transaction_cb,
.command_bits = 0,
Expand Down
5 changes: 3 additions & 2 deletions include/ice40.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ typedef struct ICE40 {
} ICE40;

// Raw SPI transfer functions
esp_err_t ice40_send(ICE40* device, const uint8_t* data, int length);
esp_err_t ice40_receive(ICE40* device, uint8_t* data, int length);
esp_err_t ice40_send(ICE40* device, const uint8_t* data, uint32_t length);
esp_err_t ice40_receive(ICE40* device, uint8_t* data, uint32_t length);
esp_err_t ice40_transaction(ICE40* device, uint8_t* data_out, uint32_t out_length, uint8_t* data_in, uint32_t in_length);

// Devic state management
esp_err_t ice40_enable(ICE40* device);
Expand Down

0 comments on commit 9a42035

Please sign in to comment.