diff --git a/examples/peripherals/spi_slave/receiver/main/app_main.c b/examples/peripherals/spi_slave/receiver/main/app_main.c index 8032f2b830..5d1da4ea64 100644 --- a/examples/peripherals/spi_slave/receiver/main/app_main.c +++ b/examples/peripherals/spi_slave/receiver/main/app_main.c @@ -100,11 +100,10 @@ void app_main(void) ret = spi_slave_initialize(RCV_HOST, &buscfg, &slvcfg, SPI_DMA_CH_AUTO); assert(ret == ESP_OK); - WORD_ALIGNED_ATTR char sendbuf[129] = ""; - WORD_ALIGNED_ATTR char recvbuf[129] = ""; - memset(recvbuf, 0, 33); - spi_slave_transaction_t t; - memset(&t, 0, sizeof(t)); + char *sendbuf = spi_bus_dma_memory_alloc(RCV_HOST, 129, 0); + char *recvbuf = spi_bus_dma_memory_alloc(RCV_HOST, 129, 0); + assert(sendbuf && recvbuf); + spi_slave_transaction_t t = {0}; while (1) { //Clear receive buffer, set send buffer to something sane diff --git a/examples/peripherals/spi_slave_hd/append_mode/master/main/app_main.c b/examples/peripherals/spi_slave_hd/append_mode/master/main/app_main.c index b16711641a..480921c982 100644 --- a/examples/peripherals/spi_slave_hd/append_mode/master/main/app_main.c +++ b/examples/peripherals/spi_slave_hd/append_mode/master/main/app_main.c @@ -24,7 +24,7 @@ #define GPIO_CS 10 #define HOST_ID SPI2_HOST -#define TRANSACTION_LEN 16 +#define TRANSACTION_LEN 64 //The SPI transaction cycles in this example. You may change the cycle. e.g., use the ``sender`` and change it to a infinite loop #define EXAMPLE_CYCLES 10 diff --git a/examples/peripherals/spi_slave_hd/append_mode/slave/main/app_main.c b/examples/peripherals/spi_slave_hd/append_mode/slave/main/app_main.c index 49676693b3..88065a994d 100644 --- a/examples/peripherals/spi_slave_hd/append_mode/slave/main/app_main.c +++ b/examples/peripherals/spi_slave_hd/append_mode/slave/main/app_main.c @@ -25,7 +25,7 @@ #define HOST_ID SPI2_HOST #define QUEUE_SIZE 6 -#define TRANSACTION_LEN 16 +#define TRANSACTION_LEN 64 #define SYNC_REG_FROM_HOST (14 * 4) #define SYNC_REG_TO_HOST (15 * 4) @@ -72,7 +72,7 @@ static esp_err_t create_transaction_pool(uint8_t **data_buf, trans_link_t *trans { for (int i = 0; i < times; i++) { //malloc data buffers for transaction - data_buf[i] = heap_caps_calloc(1, TRANSACTION_LEN, MALLOC_CAP_DMA); + data_buf[i] = spi_bus_dma_memory_alloc(HOST_ID, TRANSACTION_LEN, 0); if (!data_buf[i]) { ESP_LOGI("Create pool:", "No enough memory"); return ESP_ERR_NO_MEM; diff --git a/examples/peripherals/spi_slave_hd/segment_mode/seg_slave/main/app_main.c b/examples/peripherals/spi_slave_hd/segment_mode/seg_slave/main/app_main.c index ad59703d6b..e13c3b1499 100644 --- a/examples/peripherals/spi_slave_hd/segment_mode/seg_slave/main/app_main.c +++ b/examples/peripherals/spi_slave_hd/segment_mode/seg_slave/main/app_main.c @@ -198,7 +198,9 @@ void sender(void *arg) data_ready = get_tx_data(send_buf[descriptor_id], send_buf_size, &ready_data_size); if (data_ready) { slave_trans[descriptor_id].data = send_buf[descriptor_id]; - slave_trans[descriptor_id].len = ready_data_size; + slave_trans[descriptor_id].len = send_buf_size; + //To use dma, data buffer address and trans_len should byte align to hardware requirement, or using following flag for auto deal by driver. + slave_trans[descriptor_id].flags |= SPI_SLAVE_HD_TRANS_DMA_BUFFER_ALIGN_AUTO; //Due to the `queue_sent_cnt` and `queue_recv_cnt` logic above, we are sure there is space to send data, this will return ESP_OK immediately ESP_ERROR_CHECK(spi_slave_hd_queue_trans(SLAVE_HOST, SPI_SLAVE_CHAN_TX, &slave_trans[descriptor_id], portMAX_DELAY)); descriptor_id = (descriptor_id + 1) % QUEUE_SIZE; //descriptor_id will be: 0, 1, 2, ..., QUEUE_SIZE, 0, 1, .... @@ -234,7 +236,7 @@ void receiver(void *arg) uint8_t *recv_buf[QUEUE_SIZE]; spi_slave_hd_data_t slave_trans[QUEUE_SIZE]; for (int i = 0; i < QUEUE_SIZE; i++) { - recv_buf[i] = heap_caps_calloc(1, recv_buf_size, MALLOC_CAP_DMA); + recv_buf[i] = spi_bus_dma_memory_alloc(SLAVE_HOST, recv_buf_size, MALLOC_CAP_8BIT); if (!recv_buf[i]) { ESP_LOGE(TAG, "No enough memory!"); abort(); @@ -249,6 +251,7 @@ void receiver(void *arg) for (int i = 0; i < QUEUE_SIZE; i++) { slave_trans[descriptor_id].data = recv_buf[descriptor_id]; slave_trans[descriptor_id].len = recv_buf_size; + slave_trans[descriptor_id].flags |= SPI_SLAVE_HD_TRANS_DMA_BUFFER_ALIGN_AUTO; ESP_ERROR_CHECK(spi_slave_hd_queue_trans(SLAVE_HOST, SPI_SLAVE_CHAN_RX, &slave_trans[descriptor_id], portMAX_DELAY)); descriptor_id = (descriptor_id + 1) % QUEUE_SIZE; //descriptor_id will be: 0, 1, 2, ..., QUEUE_SIZE, 0, 1, .... } @@ -266,7 +269,6 @@ void receiver(void *arg) */ ESP_ERROR_CHECK(spi_slave_hd_get_trans_res(SLAVE_HOST, SPI_SLAVE_CHAN_RX, &ret_trans, portMAX_DELAY)); //Process the received data in your own code. Here we just print it out. - printf("%d bytes are received: \n%s\n", ret_trans->trans_len, ret_trans->data); memset(ret_trans->data, 0x0, recv_buf_size); /** @@ -288,10 +290,10 @@ void app_main(void) uint8_t init_value[SOC_SPI_MAXIMUM_BUFFER_SIZE] = {0x0}; spi_slave_hd_write_buffer(SLAVE_HOST, 0, init_value, SOC_SPI_MAXIMUM_BUFFER_SIZE); - static uint32_t send_buf_size = 5000; + static uint32_t send_buf_size = 4800; spi_slave_hd_write_buffer(SLAVE_HOST, SLAVE_MAX_TX_BUF_LEN_REG, (uint8_t *)&send_buf_size, sizeof(send_buf_size)); - static uint32_t recv_buf_size = 120; + static uint32_t recv_buf_size = 128; spi_slave_hd_write_buffer(SLAVE_HOST, SLAVE_MAX_RX_BUF_LEN_REG, (uint8_t *)&recv_buf_size, sizeof(recv_buf_size)); uint32_t slave_ready_flag = SLAVE_READY_FLAG;