|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include "ble_log/ble_log_spi_out.h" |
| 7 | + |
| 8 | +// Private defines |
| 9 | +#define SPI_OUT_BUS SPI2_HOST |
| 10 | + |
| 11 | +// Private typedefs |
| 12 | +typedef struct spi_out_trans |
| 13 | +{ |
| 14 | + spi_transaction_t trans; |
| 15 | + struct spi_out_trans *next; |
| 16 | +} spi_out_trans_t; |
| 17 | + |
| 18 | +// Private variables |
| 19 | +static spi_device_handle_t spi_handle = NULL; |
| 20 | +static spi_out_trans_t *trans_head = NULL; |
| 21 | +static SemaphoreHandle_t mutex_handle = NULL; |
| 22 | +static bool spi_out_inited = false; |
| 23 | + |
| 24 | +// Private function declarations |
| 25 | +static void spi_out_init_trans(void); |
| 26 | +static void spi_out_deinit_trans(void); |
| 27 | +static void spi_out_recycle_trans(uint32_t ms_to_wait); |
| 28 | +static void spi_out_append_trans(void); |
| 29 | + |
| 30 | +// Private functions |
| 31 | +static void spi_out_init_trans(void) |
| 32 | +{ |
| 33 | + for (int i = 0; i < CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_QUEUE_SIZE; i++) |
| 34 | + { |
| 35 | + // Allocate memory for SPI transaction |
| 36 | + uint8_t *buf = (uint8_t *)spi_bus_dma_memory_alloc(SPI_OUT_BUS, CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_TRANS_BUF_SIZE, 0); |
| 37 | + assert(buf); |
| 38 | + |
| 39 | + // Initialize new trans |
| 40 | + spi_out_trans_t *new_trans = (spi_out_trans_t *)malloc(sizeof(spi_out_trans_t)); |
| 41 | + assert(new_trans); |
| 42 | + memset(new_trans, 0, sizeof(spi_out_trans_t)); |
| 43 | + new_trans->trans.tx_buffer = buf; |
| 44 | + new_trans->trans.length = 0; |
| 45 | + |
| 46 | + // Append new trans to free trans list |
| 47 | + new_trans->next = trans_head; |
| 48 | + trans_head = new_trans; |
| 49 | + } |
| 50 | + return; |
| 51 | +} |
| 52 | + |
| 53 | +static void spi_out_deinit_trans(void) |
| 54 | +{ |
| 55 | + // Wait up to QUEUE_SIZE * 100 ms for all transactions to complete and be recycled |
| 56 | + spi_out_recycle_trans(100); |
| 57 | + |
| 58 | + // Release memory |
| 59 | + spi_out_trans_t *next; |
| 60 | + while (trans_head != NULL) |
| 61 | + { |
| 62 | + next = trans_head->next; |
| 63 | + free((uint8_t *)trans_head->trans.tx_buffer); |
| 64 | + free(trans_head); |
| 65 | + trans_head = next; |
| 66 | + } |
| 67 | + trans_head = NULL; |
| 68 | + return; |
| 69 | +} |
| 70 | + |
| 71 | +IRAM_ATTR static void spi_out_recycle_trans(uint32_t ms_to_wait) |
| 72 | +{ |
| 73 | + // Try to recycle transaction |
| 74 | + spi_transaction_t *ret_trans; |
| 75 | + spi_out_trans_t *recycled_trans; |
| 76 | + while (ESP_OK == spi_device_get_trans_result(spi_handle, &ret_trans, pdMS_TO_TICKS(ms_to_wait))) |
| 77 | + { |
| 78 | + recycled_trans = __containerof(ret_trans, spi_out_trans_t, trans); |
| 79 | + recycled_trans->next = trans_head; |
| 80 | + trans_head = recycled_trans; |
| 81 | + trans_head->trans.length = 0; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +IRAM_ATTR static void spi_out_append_trans(void) |
| 86 | +{ |
| 87 | + // Transaction head shall not be NULL for appending |
| 88 | + assert(trans_head); |
| 89 | + |
| 90 | + // Detach transaction head |
| 91 | + spi_out_trans_t *trans_to_append = trans_head; |
| 92 | + trans_head = trans_head->next; |
| 93 | + trans_to_append->next = NULL; |
| 94 | + |
| 95 | + // CRITICAL: Length unit conversion from bytes to bits |
| 96 | + trans_to_append->trans.length *= 8; |
| 97 | + assert(ESP_OK == spi_device_queue_trans(spi_handle, &trans_to_append->trans, 0)); |
| 98 | + |
| 99 | + // Try to recycle trans |
| 100 | + spi_out_recycle_trans(0); |
| 101 | +} |
| 102 | + |
| 103 | +// Public functions |
| 104 | +void ble_log_spi_out_init(void) |
| 105 | +{ |
| 106 | + // Avoid double init |
| 107 | + if (spi_out_inited) |
| 108 | + { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + // Initialize SPI |
| 113 | + spi_bus_config_t bus_config = { |
| 114 | + .miso_io_num = -1, |
| 115 | + .mosi_io_num = CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_MOSI_IO_NUM, |
| 116 | + .sclk_io_num = CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_SCLK_IO_NUM, |
| 117 | + .quadwp_io_num = -1, |
| 118 | + .quadhd_io_num = -1, |
| 119 | + .max_transfer_sz = 10240 |
| 120 | + }; |
| 121 | + spi_device_interface_config_t dev_config = { |
| 122 | + .clock_speed_hz = SPI_MASTER_FREQ_20M, |
| 123 | + .mode = 0, |
| 124 | + .spics_io_num = CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_CS_IO_NUM, |
| 125 | + .queue_size = CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_QUEUE_SIZE |
| 126 | + }; |
| 127 | + ESP_ERROR_CHECK(spi_bus_initialize(SPI_OUT_BUS, &bus_config, SPI_DMA_CH_AUTO)); |
| 128 | + ESP_ERROR_CHECK(spi_bus_add_device(SPI_OUT_BUS, &dev_config, &spi_handle)); |
| 129 | + |
| 130 | + // Initialize transaction link nodes |
| 131 | + spi_out_init_trans(); |
| 132 | + |
| 133 | + // Initialize mutex |
| 134 | + mutex_handle = xSemaphoreCreateMutex(); |
| 135 | + |
| 136 | + // Set init flag |
| 137 | + spi_out_inited = true; |
| 138 | +} |
| 139 | + |
| 140 | +void ble_log_spi_out_deinit(void) |
| 141 | +{ |
| 142 | + // Avoid double deinit |
| 143 | + if (!spi_out_inited) |
| 144 | + { |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + // Deinitialize transaction link nodes |
| 149 | + spi_out_deinit_trans(); |
| 150 | + |
| 151 | + // Deinitialize SPI |
| 152 | + ESP_ERROR_CHECK(spi_bus_remove_device(spi_handle)); |
| 153 | + ESP_ERROR_CHECK(spi_bus_free(SPI_OUT_BUS)); |
| 154 | + spi_handle = NULL; |
| 155 | + |
| 156 | + // Deinitialize mutex |
| 157 | + vSemaphoreDelete(mutex_handle); |
| 158 | + mutex_handle = NULL; |
| 159 | + |
| 160 | + // Reset init flag |
| 161 | + spi_out_inited = false; |
| 162 | +} |
| 163 | + |
| 164 | +IRAM_ATTR void ble_log_spi_out_write(uint32_t len, const uint8_t *addr, spi_out_source_t source) |
| 165 | +{ |
| 166 | + // Take semaphore |
| 167 | + assert(xSemaphoreTake(mutex_handle, portMAX_DELAY) == pdTRUE); |
| 168 | + |
| 169 | + // Recycle trans if free buffer list is empty |
| 170 | + if (!trans_head) |
| 171 | + { |
| 172 | + spi_out_recycle_trans(0); |
| 173 | + } |
| 174 | + |
| 175 | + // Length of 0 means flush out |
| 176 | + if (!len) |
| 177 | + { |
| 178 | + assert(trans_head); |
| 179 | + if (trans_head->trans.length) |
| 180 | + { |
| 181 | + spi_out_append_trans(); |
| 182 | + } |
| 183 | + goto release; |
| 184 | + } |
| 185 | + |
| 186 | + // Copy user data to buffer |
| 187 | + uint32_t copy_buf_len; |
| 188 | + uint32_t data_left_len = len; |
| 189 | + uint32_t empty_buf_len = CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_TRANS_BUF_SIZE - trans_head->trans.length; |
| 190 | + while (data_left_len) |
| 191 | + { |
| 192 | + // There shall always be available buffer in free buffer list during write operation |
| 193 | + assert(trans_head); |
| 194 | + |
| 195 | + // Copy data to buffer and update length |
| 196 | + copy_buf_len = (data_left_len > empty_buf_len) ? empty_buf_len : data_left_len; |
| 197 | + memcpy((uint8_t *)trans_head->trans.tx_buffer + trans_head->trans.length, addr + (len - data_left_len), copy_buf_len); |
| 198 | + trans_head->trans.length += copy_buf_len; |
| 199 | + data_left_len -= copy_buf_len; |
| 200 | + |
| 201 | + // Transaction buffer length shall never exceed buffer size |
| 202 | + assert(trans_head->trans.length <= CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_TRANS_BUF_SIZE); |
| 203 | + |
| 204 | + // If buffer is full, append transaction and reset buffer length |
| 205 | + if (trans_head->trans.length == CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_TRANS_BUF_SIZE) |
| 206 | + { |
| 207 | + spi_out_append_trans(); |
| 208 | + empty_buf_len = CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_TRANS_BUF_SIZE; |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | +release: |
| 213 | + xSemaphoreGive(mutex_handle); |
| 214 | + return; |
| 215 | +} |
| 216 | + |
| 217 | +IRAM_ATTR void ble_log_spi_out_write_esp(uint32_t len, const uint8_t *addr, bool end) |
| 218 | +{ |
| 219 | + ble_log_spi_out_write(len, addr, esp_controller); |
| 220 | +} |
0 commit comments