Skip to content

Commit

Permalink
Merge branch 'bugfix/spi_hd_quad_issue_4.3' into 'release/v4.3'
Browse files Browse the repository at this point in the history
SPI : fix wrong dummy cycle on quad mode and put get-command function in spi_ll.h(backport v4.3)

See merge request espressif/esp-idf!19836
  • Loading branch information
ginkgm committed Sep 21, 2022
2 parents 894b1ad + 8095142 commit 02c45b6
Show file tree
Hide file tree
Showing 9 changed files with 405 additions and 145 deletions.
67 changes: 36 additions & 31 deletions components/esp_serial_slave_link/essl_spi.c
Expand Up @@ -19,10 +19,12 @@
#include "driver/periph_ctrl.h"
#include "essl_internal.h"
#include "essl_spi.h"
#include "essl_spi/esp32s2_defs.h"

#define ESSL_SPI_CHECK(cond, warn, ret) do{if(!(cond)){ESP_LOGE(TAG, warn); return ret;}} while(0)

#include "hal/spi_types.h"
#include "hal/spi_ll.h"

/**
* Initialise device function list of SPI by this macro.
*/
Expand Down Expand Up @@ -58,51 +60,54 @@ typedef struct {
} master_in;
} essl_spi_context_t;


static uint16_t get_hd_command(uint16_t cmd_i, uint32_t flags)
static uint16_t get_hd_command(spi_command_t cmd_t, uint32_t flags)
{
//have no prefixes
if (cmd_i == CMD_HD_EN_QPI_REG) return cmd_i;
//doesn't support 4-line commands
if(flags & SPI_TRANS_MODE_QIO && flags & SPI_TRANS_MODE_DIOQIO_ADDR &&
(cmd_i == CMD_HD_WR_END_REG || cmd_i == CMD_HD_INT0_REG ||
cmd_i == CMD_HD_INT1_REG || cmd_i == CMD_HD_INT2_REG)) {
//the transaction will be sent in corresponding 1/2/4 bit mode, without address and data.
//the CMD will have no 0xA- prefix
return cmd_i;
}
spi_line_mode_t line_mode = {
.cmd_lines = 1,
};

if (flags & SPI_TRANS_MODE_DIO) {
line_mode.data_lines = 2;
if (flags & SPI_TRANS_MODE_DIOQIO_ADDR) {
return cmd_i | CMD_HD_DIO_MODE;
line_mode.addr_lines = 2;
} else {
return cmd_i | CMD_HD_DOUT_MODE;
line_mode.addr_lines = 1;
}
} else if (flags & SPI_TRANS_MODE_QIO) {
line_mode.data_lines = 4;
if (flags & SPI_TRANS_MODE_DIOQIO_ADDR) {
return cmd_i | CMD_HD_QIO_MODE;
line_mode.addr_lines = 4;
} else {
return cmd_i | CMD_HD_QOUT_MODE;
line_mode.addr_lines = 1;
}
} else {
line_mode.data_lines = 1;
line_mode.addr_lines = 1;
}
return cmd_i | CMD_HD_ONEBIT_MODE;

return spi_ll_get_slave_hd_command(cmd_t, line_mode);
}

static int get_hd_dummy_bits(uint32_t flags)
{
//dummy is always 4 cycles when dual or quad mode is enabled. Otherwise 8 cycles in normal mode.
if (flags & (SPI_TRANS_MODE_DIO | SPI_TRANS_MODE_QIO)) {
return 4;
spi_line_mode_t line_mode = {};

if (flags & SPI_TRANS_MODE_DIO) {
line_mode.data_lines = 2;
} else if (flags & SPI_TRANS_MODE_QIO) {
line_mode.data_lines = 4;
} else {
return 8;
line_mode.data_lines = 1;
}

return spi_ll_get_slave_hd_dummy_bits(line_mode);
}

esp_err_t essl_spi_rdbuf(spi_device_handle_t spi, uint8_t *out_data, int addr, int len, uint32_t flags)
{
spi_transaction_ext_t t = {
.base = {
.cmd = get_hd_command(CMD_HD_RDBUF_REG, flags),
.cmd = get_hd_command(SPI_CMD_HD_RDBUF, flags),
.addr = addr % 72,
.rxlength = len * 8,
.rx_buffer = out_data,
Expand All @@ -118,7 +123,7 @@ esp_err_t essl_spi_rdbuf_polling(spi_device_handle_t spi, uint8_t *out_data, int
{
spi_transaction_ext_t t = {
.base = {
.cmd = get_hd_command(CMD_HD_RDBUF_REG, flags),
.cmd = get_hd_command(SPI_CMD_HD_RDBUF, flags),
.addr = addr % 72,
.rxlength = len * 8,
.rx_buffer = out_data,
Expand All @@ -134,7 +139,7 @@ esp_err_t essl_spi_wrbuf(spi_device_handle_t spi, const uint8_t *data, int addr,
{
spi_transaction_ext_t t = {
.base = {
.cmd = get_hd_command(CMD_HD_WRBUF_REG, flags),
.cmd = get_hd_command(SPI_CMD_HD_WRBUF, flags),
.addr = addr % 72,
.length = len * 8,
.tx_buffer = data,
Expand All @@ -149,7 +154,7 @@ esp_err_t essl_spi_wrbuf_polling(spi_device_handle_t spi, const uint8_t *data, i
{
spi_transaction_ext_t t = {
.base = {
.cmd = get_hd_command(CMD_HD_WRBUF_REG, flags),
.cmd = get_hd_command(SPI_CMD_HD_WRBUF, flags),
.addr = addr % 72,
.length = len * 8,
.tx_buffer = data,
Expand All @@ -164,7 +169,7 @@ esp_err_t essl_spi_rddma_seg(spi_device_handle_t spi, uint8_t *out_data, int seg
{
spi_transaction_ext_t t = {
.base = {
.cmd = get_hd_command(CMD_HD_RDDMA_REG, flags),
.cmd = get_hd_command(SPI_CMD_HD_RDDMA, flags),
.rxlength = seg_len * 8,
.rx_buffer = out_data,
.flags = flags | SPI_TRANS_VARIABLE_DUMMY,
Expand All @@ -177,7 +182,7 @@ esp_err_t essl_spi_rddma_seg(spi_device_handle_t spi, uint8_t *out_data, int seg
esp_err_t essl_spi_rddma_done(spi_device_handle_t spi, uint32_t flags)
{
spi_transaction_t end_t = {
.cmd = get_hd_command(CMD_HD_INT0_REG, flags),
.cmd = get_hd_command(SPI_CMD_HD_INT0, flags),
.flags = flags,
};
return spi_device_transmit(spi, &end_t);
Expand Down Expand Up @@ -208,7 +213,7 @@ esp_err_t essl_spi_wrdma_seg(spi_device_handle_t spi, const uint8_t *data, int s
{
spi_transaction_ext_t t = {
.base = {
.cmd = get_hd_command(CMD_HD_WRDMA_REG, flags),
.cmd = get_hd_command(SPI_CMD_HD_WRDMA, flags),
.length = seg_len * 8,
.tx_buffer = data,
.flags = flags | SPI_TRANS_VARIABLE_DUMMY,
Expand All @@ -221,7 +226,7 @@ esp_err_t essl_spi_wrdma_seg(spi_device_handle_t spi, const uint8_t *data, int s
esp_err_t essl_spi_wrdma_done(spi_device_handle_t spi, uint32_t flags)
{
spi_transaction_t end_t = {
.cmd = get_hd_command(CMD_HD_WR_END_REG, flags),
.cmd = get_hd_command(SPI_CMD_HD_WR_END, flags),
.flags = flags,
};
return spi_device_transmit(spi, &end_t);
Expand Down Expand Up @@ -250,7 +255,7 @@ esp_err_t essl_spi_wrdma(spi_device_handle_t spi, const uint8_t *data, int len,
esp_err_t essl_spi_int(spi_device_handle_t spi, int int_n, uint32_t flags)
{
spi_transaction_t end_t = {
.cmd = get_hd_command(CMD_HD_INT0_REG + int_n, flags),
.cmd = get_hd_command(SPI_CMD_HD_INT0 + int_n, flags),
.flags = flags,
};
return spi_device_transmit(spi, &end_t);
Expand Down
38 changes: 0 additions & 38 deletions components/esp_serial_slave_link/include/essl_spi/esp32c3_defs.h

This file was deleted.

38 changes: 0 additions & 38 deletions components/esp_serial_slave_link/include/essl_spi/esp32s2_defs.h

This file was deleted.

38 changes: 0 additions & 38 deletions components/esp_serial_slave_link/include/essl_spi/esp32s3_defs.h

This file was deleted.

24 changes: 24 additions & 0 deletions components/hal/esp32/include/hal/spi_ll.h
Expand Up @@ -30,6 +30,7 @@
#include <esp_types.h>
#include "hal/hal_defs.h"
#include <stdlib.h> //for abs()
#include "hal/spi_types.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -1069,6 +1070,29 @@ static inline void spi_dma_ll_enable_out_auto_wrback(spi_dma_dev_t *dma_out, uin
//does not configure it in ESP32
}

/**
* Get the spi communication command
*
* @param cmd_t Base command value
* @param line_mode Line mode of SPI transaction phases: CMD, ADDR, DOUT/DIN.
*/
static inline uint16_t spi_ll_get_slave_hd_command(spi_command_t cmd_t, spi_line_mode_t line_mode)
{
//This is not supported in esp32
return 0;
}

/**
* Get the dummy bits
*
* @param line_mode Line mode of SPI transaction phases: CMD, ADDR, DOUT/DIN.
*/
static inline int spi_ll_get_slave_hd_dummy_bits(spi_line_mode_t line_mode)
{
//This is not supported in esp32
return 0;
}

#undef SPI_LL_RST_MASK
#undef SPI_LL_UNUSED_INT_MASK

Expand Down

0 comments on commit 02c45b6

Please sign in to comment.