sdio_slave_hal.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. // The HAL layer for SDIO slave (common part)
  7. #include <string.h>
  8. #include "soc/slc_struct.h"
  9. #include "soc/hinf_struct.h"
  10. #include "hal/sdio_slave_types.h"
  11. #include "soc/host_struct.h"
  12. #include "hal/sdio_slave_hal.h"
  13. #include "hal/assert.h"
  14. #include "hal/log.h"
  15. #include "esp_attr.h"
  16. #define SDIO_SLAVE_CHECK(res, str, ret_val) do { if(!(res)){\
  17. HAL_LOGE(TAG, "%s", str);\
  18. return ret_val;\
  19. } }while (0)
  20. /* The tag may be unused if log level is set to NONE */
  21. static const __attribute__((unused)) char TAG[] = "SDIO_HAL";
  22. static esp_err_t init_send_queue(sdio_slave_context_t *hal);
  23. /**************** Ring buffer for SDIO sending use *****************/
  24. typedef enum {
  25. RINGBUF_GET_ONE = 0,
  26. RINGBUF_GET_ALL = 1,
  27. } ringbuf_get_all_t;
  28. typedef enum {
  29. RINGBUF_WRITE_PTR,
  30. RINGBUF_READ_PTR,
  31. RINGBUF_FREE_PTR,
  32. } sdio_ringbuf_pointer_t;
  33. static esp_err_t sdio_ringbuf_send(sdio_ringbuf_t *buf, esp_err_t (*copy_callback)(uint8_t *, void *), void *arg);
  34. esp_err_t sdio_ringbuf_recv(sdio_ringbuf_t *buf, uint8_t **start, uint8_t **end, ringbuf_get_all_t get_all);
  35. static inline int sdio_ringbuf_return(sdio_ringbuf_t* buf, uint8_t *ptr);
  36. #define _SEND_DESC_NEXT(x) STAILQ_NEXT(&((sdio_slave_hal_send_desc_t*)x)->dma_desc, qe)
  37. #define SEND_DESC_NEXT(x) (sdio_slave_hal_send_desc_t*)_SEND_DESC_NEXT(x)
  38. #define SEND_DESC_NEXT_SET(x, target) do { \
  39. _SEND_DESC_NEXT(x)=(lldesc_t*)target; \
  40. }while(0)
  41. static esp_err_t link_desc_to_last(uint8_t* desc, void* arg)
  42. {
  43. SEND_DESC_NEXT_SET(arg, desc);
  44. return ESP_OK;
  45. }
  46. //calculate a pointer with offset to a original pointer of the specific ringbuffer
  47. static inline uint8_t* sdio_ringbuf_offset_ptr(sdio_ringbuf_t *buf, sdio_ringbuf_pointer_t ptr, uint32_t offset)
  48. {
  49. uint8_t *buf_ptr;
  50. switch (ptr) {
  51. case RINGBUF_WRITE_PTR:
  52. buf_ptr = buf->write_ptr;
  53. break;
  54. case RINGBUF_READ_PTR:
  55. buf_ptr = buf->read_ptr;
  56. break;
  57. case RINGBUF_FREE_PTR:
  58. buf_ptr = buf->free_ptr;
  59. break;
  60. default:
  61. abort();
  62. }
  63. uint8_t *offset_ptr=buf_ptr+offset;
  64. if (offset_ptr >= buf->data + buf->size) {
  65. offset_ptr -= buf->size;
  66. }
  67. return offset_ptr;
  68. }
  69. static esp_err_t sdio_ringbuf_send(sdio_ringbuf_t *buf, esp_err_t (*copy_callback)(uint8_t *, void *), void *arg)
  70. {
  71. uint8_t* get_ptr = sdio_ringbuf_offset_ptr(buf, RINGBUF_WRITE_PTR, SDIO_SLAVE_SEND_DESC_SIZE);
  72. esp_err_t err = ESP_OK;
  73. if (copy_callback) {
  74. err = (*copy_callback)(get_ptr, arg);
  75. }
  76. if (err != ESP_OK) return err;
  77. buf->write_ptr = get_ptr;
  78. return ESP_OK;
  79. }
  80. // this ringbuf is a return-before-recv-again strategy
  81. // since this is designed to be called in the ISR, no parallel logic
  82. /*
  83. * Workaround for gcc 11. GCC-277. Break the inferring of callers.
  84. * This function used to be static inline.
  85. */
  86. __attribute__((weak))
  87. esp_err_t sdio_ringbuf_recv(sdio_ringbuf_t *buf, uint8_t **start, uint8_t **end, ringbuf_get_all_t get_all)
  88. {
  89. HAL_ASSERT(buf->free_ptr == buf->read_ptr); //must return before recv again
  90. if (start == NULL && end == NULL) return ESP_ERR_INVALID_ARG; // must have a output
  91. if (buf->read_ptr == buf->write_ptr) return ESP_ERR_NOT_FOUND; // no data
  92. uint8_t *get_start = sdio_ringbuf_offset_ptr(buf, RINGBUF_READ_PTR, SDIO_SLAVE_SEND_DESC_SIZE);
  93. if (get_all != RINGBUF_GET_ONE) {
  94. buf->read_ptr = buf->write_ptr;
  95. } else {
  96. buf->read_ptr = get_start;
  97. }
  98. if (start != NULL) {
  99. *start = get_start;
  100. }
  101. if (end != NULL) {
  102. *end = buf->read_ptr;
  103. }
  104. return ESP_OK;
  105. }
  106. static inline int sdio_ringbuf_return(sdio_ringbuf_t* buf, uint8_t *ptr)
  107. {
  108. HAL_ASSERT(sdio_ringbuf_offset_ptr(buf, RINGBUF_FREE_PTR, SDIO_SLAVE_SEND_DESC_SIZE) == ptr);
  109. size_t size = (buf->read_ptr + buf->size - buf->free_ptr) % buf->size;
  110. size_t count = size / SDIO_SLAVE_SEND_DESC_SIZE;
  111. HAL_ASSERT(count * SDIO_SLAVE_SEND_DESC_SIZE==size);
  112. buf->free_ptr = buf->read_ptr;
  113. return count;
  114. }
  115. static inline uint8_t* sdio_ringbuf_peek_front(sdio_ringbuf_t* buf)
  116. {
  117. if (buf->read_ptr != buf->write_ptr) {
  118. return sdio_ringbuf_offset_ptr(buf, RINGBUF_READ_PTR, SDIO_SLAVE_SEND_DESC_SIZE);
  119. } else {
  120. return NULL;
  121. }
  122. }
  123. static inline uint8_t* sdio_ringbuf_peek_rear(sdio_ringbuf_t *buf)
  124. {
  125. return buf->write_ptr;
  126. }
  127. static inline bool sdio_ringbuf_empty(sdio_ringbuf_t* buf)
  128. {
  129. return (buf->read_ptr == buf->write_ptr);
  130. }
  131. /**************** End of Ring buffer *****************/
  132. void sdio_slave_hal_init(sdio_slave_context_t *hal)
  133. {
  134. hal->host = sdio_slave_ll_get_host(0);
  135. hal->slc = sdio_slave_ll_get_slc(0);
  136. hal->hinf = sdio_slave_ll_get_hinf(0);
  137. hal->send_state = STATE_IDLE;
  138. hal->recv_link_list = (sdio_slave_hal_recv_stailq_t)STAILQ_HEAD_INITIALIZER(hal->recv_link_list);
  139. init_send_queue(hal);
  140. }
  141. void sdio_slave_hal_hw_init(sdio_slave_context_t *hal)
  142. {
  143. sdio_slave_ll_init(hal->slc);
  144. sdio_slave_ll_enable_hs(hal->hinf, true);
  145. sdio_slave_ll_set_timing(hal->host, hal->timing);
  146. sdio_slave_ll_slvint_t intr_ena = 0xff;
  147. sdio_slave_ll_slvint_set_ena(hal->slc, &intr_ena);
  148. }
  149. static esp_err_t init_send_queue(sdio_slave_context_t *hal)
  150. {
  151. esp_err_t ret;
  152. esp_err_t rcv_res __attribute((unused));
  153. sdio_ringbuf_t *buf = &(hal->send_desc_queue);
  154. //initialize pointers
  155. buf->write_ptr = buf->data;
  156. buf->read_ptr = buf->data;
  157. buf->free_ptr = buf->data;
  158. sdio_slave_hal_send_desc_t *first = NULL, *last = NULL;
  159. //no copy for the first descriptor
  160. ret = sdio_ringbuf_send(buf, NULL, NULL);
  161. if (ret != ESP_OK) return ret;
  162. //loop in the ringbuf to link all the desc one after another as a ring
  163. for (int i = 0; i < hal->send_queue_size + 1; i++) {
  164. rcv_res = sdio_ringbuf_recv(buf, (uint8_t **) &last, NULL, RINGBUF_GET_ONE);
  165. assert (rcv_res == ESP_OK);
  166. ret = sdio_ringbuf_send(buf, link_desc_to_last, last);
  167. if (ret != ESP_OK) return ret;
  168. sdio_ringbuf_return(buf, (uint8_t *) last);
  169. }
  170. first = NULL;
  171. last = NULL;
  172. //clear the queue
  173. rcv_res = sdio_ringbuf_recv(buf, (uint8_t **) &first, (uint8_t **) &last, RINGBUF_GET_ALL);
  174. assert (rcv_res == ESP_OK);
  175. HAL_ASSERT(first == last); //there should be only one desc remain
  176. sdio_ringbuf_return(buf, (uint8_t *) first);
  177. return ESP_OK;
  178. }
  179. void sdio_slave_hal_set_ioready(sdio_slave_context_t *hal, bool ready)
  180. {
  181. sdio_slave_ll_set_ioready(hal->hinf, ready); //set IO ready to 1 to allow host to use
  182. }
  183. /*---------------------------------------------------------------------------
  184. * Send
  185. *
  186. * The hardware has a cache, so that once a descriptor is loaded onto the linked-list, it cannot be modified
  187. * until returned (used) by the hardware. This forbids us from loading descriptors onto the linked list during
  188. * the transfer (or the time waiting for host to start a transfer). However, we use a "ringbuffer" (different from
  189. * the one in ``freertos/`` folder) holding descriptors to solve this:
  190. * 1. The driver allocates continuous memory for several buffer descriptors (the maximum buffer number) during
  191. * initialization. Then the driver points the STAILQ_NEXT pointer of all the descriptors except the last one
  192. * to the next descriptor of each of them. Then the pointer of the last descriptor points back to the first one:
  193. * now the descriptor is in a ring.
  194. * 2. The "ringbuffer" has a write pointer points to where app can write new descriptor. The app writes the new descriptor
  195. * indicated by the write pointer without touching the STAILQ_NEXT pointer so that the descriptors are always in a
  196. * ring-like linked-list. The app never touches the part of linked-list being used by the hardware.
  197. * 3. When the hardware needs some data to send, it automatically pick a part of linked descriptors. According to the mode:
  198. * - Buffer mode: only pick the next one to the last one sent;
  199. * - Stream mode: pick the whole unsent linked list, starting from the one above, to the latest linked one.
  200. * The driver removes the STAILQ_NEXT pointer of the last descriptor and put the head of the part to the DMA controller so
  201. * that it looks like just a linear linked-list rather than a ring to the hardware.
  202. * 4. The counter of sending FIFO can increase when app load new buffers (in STREAM_MODE) or when new transfer should
  203. * start (in PACKET_MODE).
  204. * 5. When the sending transfer is finished, the driver goes through the descriptors just send in the ISR and push all
  205. * the ``arg`` member of descriptors to the queue back to the app, so that the app can handle finished buffers. The
  206. * driver also fix the STAILQ_NEXT pointer of the last descriptor so that the descriptors are now in a ring again.
  207. ----------------------------------------------------------------------------*/
  208. static inline void send_set_state(sdio_slave_context_t *hal, send_state_t state)
  209. {
  210. hal->send_state = state;
  211. }
  212. static inline send_state_t send_get_state(sdio_slave_context_t* hal)
  213. {
  214. return hal->send_state;
  215. }
  216. DMA_ATTR static const lldesc_t start_desc = {
  217. .owner = 1,
  218. .buf = (void*)0x3ffbbbbb, //assign a dma-capable pointer other than NULL, which will not be used
  219. .size = 1,
  220. .length = 1,
  221. .eof = 1,
  222. };
  223. //force trigger rx_done interrupt. the interrupt is abused to invoke ISR from the app by the enable bit and never cleared.
  224. static void send_isr_invoker_enable(const sdio_slave_context_t *hal)
  225. {
  226. sdio_slave_ll_send_reset(hal->slc);
  227. sdio_slave_ll_send_start(hal->slc, &start_desc);
  228. //wait for rx_done
  229. while(!sdio_slave_ll_send_invoker_ready(hal->slc));
  230. sdio_slave_ll_send_stop(hal->slc);
  231. sdio_slave_ll_send_hostint_clr(hal->host);
  232. }
  233. static void send_isr_invoker_disable(sdio_slave_context_t *hal)
  234. {
  235. sdio_slave_ll_send_part_done_clear(hal->slc);
  236. }
  237. void sdio_slave_hal_send_handle_isr_invoke(sdio_slave_context_t *hal)
  238. {
  239. sdio_slave_ll_send_part_done_intr_ena(hal->slc, false);
  240. }
  241. //start hw operation with existing data (if exist)
  242. esp_err_t sdio_slave_hal_send_start(sdio_slave_context_t *hal)
  243. {
  244. SDIO_SLAVE_CHECK(send_get_state(hal) == STATE_IDLE,
  245. "already started", ESP_ERR_INVALID_STATE);
  246. send_set_state(hal, STATE_WAIT_FOR_START);
  247. send_isr_invoker_enable(hal);
  248. sdio_slave_ll_send_intr_clr(hal->slc);
  249. sdio_slave_ll_send_intr_ena(hal->slc, true);
  250. return ESP_OK;
  251. }
  252. //only stop hw operations, no touch to data as well as counter
  253. void sdio_slave_hal_send_stop(sdio_slave_context_t *hal)
  254. {
  255. sdio_slave_ll_send_stop(hal->slc);
  256. send_isr_invoker_disable(hal);
  257. sdio_slave_ll_send_intr_ena(hal->slc, false);
  258. send_set_state(hal, STATE_IDLE);
  259. }
  260. static void send_new_packet(sdio_slave_context_t *hal)
  261. {
  262. // since eof is changed, we have to stop and reset the link list,
  263. // and restart new link list operation
  264. sdio_slave_hal_send_desc_t *const start_desc = hal->in_flight_head;
  265. sdio_slave_hal_send_desc_t *const end_desc = hal->in_flight_end;
  266. HAL_ASSERT(start_desc != NULL && end_desc != NULL);
  267. sdio_slave_ll_send_stop(hal->slc);
  268. sdio_slave_ll_send_reset(hal->slc);
  269. sdio_slave_ll_send_start(hal->slc, (lldesc_t*)start_desc);
  270. // update pkt_len register to allow host reading.
  271. sdio_slave_ll_send_write_len(hal->slc, end_desc->pkt_len);
  272. HAL_EARLY_LOGV(TAG, "send_length_write: %d, last_len: %08X", end_desc->pkt_len, sdio_slave_ll_send_read_len(hal->host));
  273. send_set_state(hal, STATE_SENDING);
  274. HAL_EARLY_LOGD(TAG, "restart new send: %p->%p, pkt_len: %d", start_desc, end_desc, end_desc->pkt_len);
  275. }
  276. static esp_err_t send_check_new_packet(sdio_slave_context_t *hal)
  277. {
  278. esp_err_t ret;
  279. sdio_slave_hal_send_desc_t *start = NULL;
  280. sdio_slave_hal_send_desc_t *end = NULL;
  281. if (hal->sending_mode == SDIO_SLAVE_SEND_PACKET) {
  282. ret = sdio_ringbuf_recv(&(hal->send_desc_queue), (uint8_t **) &start, (uint8_t **) &end, RINGBUF_GET_ONE);
  283. } else { //stream mode
  284. ret = sdio_ringbuf_recv(&(hal->send_desc_queue), (uint8_t **) &start, (uint8_t **) &end, RINGBUF_GET_ALL);
  285. }
  286. if (ret == ESP_OK) {
  287. hal->in_flight_head = start;
  288. hal->in_flight_end = end;
  289. end->dma_desc.eof = 1;
  290. //temporarily break the link ring here, the ring will be re-connected in ``send_isr_eof()``.
  291. hal->in_flight_next = SEND_DESC_NEXT(end);
  292. SEND_DESC_NEXT_SET(end, NULL);
  293. }
  294. return ESP_OK;
  295. }
  296. bool sdio_slave_hal_send_eof_happened(sdio_slave_context_t* hal)
  297. {
  298. // Goto idle state (cur_start=NULL) if transmission done,
  299. // also update sequence and recycle descs.
  300. if (sdio_slave_ll_send_done(hal->slc)) {
  301. //check current state
  302. HAL_ASSERT(send_get_state(hal) == STATE_SENDING);
  303. sdio_slave_ll_send_intr_clr(hal->slc);
  304. return true;
  305. } else {
  306. return false;
  307. }
  308. }
  309. //clear counter but keep data
  310. esp_err_t sdio_slave_hal_send_reset_counter(sdio_slave_context_t* hal)
  311. {
  312. SDIO_SLAVE_CHECK(send_get_state(hal) == STATE_IDLE,
  313. "reset counter when transmission started", ESP_ERR_INVALID_STATE);
  314. sdio_slave_ll_send_write_len(hal->slc, 0);
  315. HAL_EARLY_LOGV(TAG, "last_len: %08X", sdio_slave_ll_send_read_len(hal->host));
  316. hal->tail_pkt_len = 0;
  317. sdio_slave_hal_send_desc_t *desc = hal->in_flight_head;
  318. while(desc != NULL) {
  319. hal->tail_pkt_len += desc->dma_desc.length;
  320. desc->pkt_len = hal->tail_pkt_len;
  321. desc = SEND_DESC_NEXT(desc);
  322. }
  323. // in theory the desc should be the one right next to the last of in_flight_head,
  324. // but the link of last is NULL, so get the desc from the ringbuf directly.
  325. desc = (sdio_slave_hal_send_desc_t*)sdio_ringbuf_peek_front(&(hal->send_desc_queue));
  326. while(desc != NULL) {
  327. hal->tail_pkt_len += desc->dma_desc.length;
  328. desc->pkt_len = hal->tail_pkt_len;
  329. desc = SEND_DESC_NEXT(desc);
  330. }
  331. return ESP_OK;
  332. }
  333. static esp_err_t send_get_inflight_desc(sdio_slave_context_t *hal, void **out_arg, uint32_t *out_returned_cnt,
  334. bool init)
  335. {
  336. esp_err_t ret;
  337. if (init) {
  338. HAL_ASSERT(hal->returned_desc == NULL);
  339. hal->returned_desc = hal->in_flight_head;
  340. send_set_state(hal, STATE_GETTING_RESULT);
  341. }
  342. if (hal->returned_desc != NULL) {
  343. *out_arg = hal->returned_desc->arg;
  344. hal->returned_desc = SEND_DESC_NEXT(hal->returned_desc);
  345. ret = ESP_OK;
  346. } else {
  347. if (hal->in_flight_head != NULL) {
  348. // fix the link broken of last desc when being sent
  349. HAL_ASSERT(hal->in_flight_end != NULL);
  350. SEND_DESC_NEXT_SET(hal->in_flight_end, hal->in_flight_next);
  351. *out_returned_cnt = sdio_ringbuf_return(&(hal->send_desc_queue), (uint8_t*)hal->in_flight_head);
  352. }
  353. hal->in_flight_head = NULL;
  354. hal->in_flight_end = NULL;
  355. ret = ESP_ERR_NOT_FOUND;
  356. }
  357. return ret;
  358. }
  359. static esp_err_t send_get_unsent_desc(sdio_slave_context_t *hal, void **out_arg, uint32_t *out_return_cnt)
  360. {
  361. esp_err_t ret;
  362. sdio_slave_hal_send_desc_t *head = NULL;
  363. sdio_slave_hal_send_desc_t *tail = NULL;
  364. ret = sdio_ringbuf_recv(&(hal->send_desc_queue), (uint8_t **) &head, (uint8_t **) &tail, RINGBUF_GET_ONE);
  365. if (ret == ESP_OK) {
  366. //currently each packet takes only one desc.
  367. HAL_ASSERT(head == tail);
  368. (*out_arg) = head->arg;
  369. (*out_return_cnt) = sdio_ringbuf_return(&(hal->send_desc_queue), (uint8_t*) head);
  370. } else if (ret == ESP_ERR_NOT_FOUND) {
  371. // if in wait to send state, set the sequence number of tail to the value last sent, just as if the packet wait to
  372. // send never queued.
  373. // Go to idle state (cur_end!=NULL and cur_start=NULL)
  374. send_set_state(hal, STATE_IDLE);
  375. hal->tail_pkt_len = sdio_slave_ll_send_read_len(hal->host);
  376. }
  377. return ret;
  378. }
  379. esp_err_t sdio_slave_hal_send_get_next_finished_arg(sdio_slave_context_t *hal, void **out_arg, uint32_t* out_returned_cnt)
  380. {
  381. bool init = (send_get_state(hal) == STATE_SENDING);
  382. if (init) {
  383. HAL_ASSERT(hal->in_flight_head != NULL);
  384. } else {
  385. HAL_ASSERT(send_get_state(hal) == STATE_GETTING_RESULT);
  386. }
  387. *out_returned_cnt = 0;
  388. esp_err_t ret = send_get_inflight_desc(hal, out_arg, out_returned_cnt, init);
  389. if (ret == ESP_ERR_NOT_FOUND) {
  390. // Go to wait for packet state
  391. send_set_state(hal, STATE_WAIT_FOR_START);
  392. }
  393. return ret;
  394. }
  395. esp_err_t sdio_slave_hal_send_flush_next_buffer(sdio_slave_context_t *hal, void **out_arg, uint32_t *out_return_cnt)
  396. {
  397. esp_err_t ret = ESP_OK;
  398. *out_return_cnt = 0;
  399. bool init = (send_get_state(hal) == STATE_IDLE);
  400. if (!init) {
  401. if (send_get_state(hal) != STATE_GETTING_RESULT && send_get_state(hal) != STATE_GETTING_UNSENT_DESC) {
  402. return ESP_ERR_INVALID_STATE;
  403. }
  404. }
  405. if (init || send_get_state(hal) == STATE_GETTING_RESULT) {
  406. ret = send_get_inflight_desc(hal, out_arg, out_return_cnt, init);
  407. if (ret == ESP_ERR_NOT_FOUND) {
  408. send_set_state(hal, STATE_GETTING_UNSENT_DESC);
  409. }
  410. }
  411. if (send_get_state(hal) == STATE_GETTING_UNSENT_DESC) {
  412. ret = send_get_unsent_desc(hal, out_arg, out_return_cnt);
  413. if (ret == ESP_ERR_NOT_FOUND) {
  414. send_set_state(hal, STATE_IDLE);
  415. }
  416. }
  417. return ret;
  418. }
  419. esp_err_t sdio_slave_hal_send_new_packet_if_exist(sdio_slave_context_t *hal)
  420. {
  421. esp_err_t ret;
  422. // Go to wait sending state (cur_start!=NULL && cur_end==NULL) if not sending and new packet ready.
  423. // Note we may also enter this state by stopping sending in the app.
  424. if (send_get_state(hal) == STATE_WAIT_FOR_START) {
  425. if (hal->in_flight_head == NULL) {
  426. send_check_new_packet(hal);
  427. }
  428. // Go to sending state (cur_start and cur_end != NULL) if has packet to send.
  429. if (hal->in_flight_head) {
  430. send_new_packet(hal);
  431. ret = ESP_OK;
  432. } else {
  433. ret = ESP_ERR_NOT_FOUND;
  434. }
  435. } else {
  436. ret = ESP_ERR_INVALID_STATE;
  437. }
  438. return ret;
  439. }
  440. static esp_err_t send_write_desc(uint8_t* desc, void* arg)
  441. {
  442. sdio_slave_hal_send_desc_t* next_desc = SEND_DESC_NEXT(desc);
  443. memcpy(desc, arg, sizeof(sdio_slave_hal_send_desc_t));
  444. SEND_DESC_NEXT_SET(desc, next_desc);
  445. return ESP_OK;
  446. }
  447. static void send_isr_invoke(sdio_slave_context_t *hal)
  448. {
  449. sdio_slave_ll_send_part_done_intr_ena(hal->slc, true);
  450. }
  451. esp_err_t sdio_slave_hal_send_queue(sdio_slave_context_t* hal, uint8_t *addr, size_t len, void *arg)
  452. {
  453. hal->tail_pkt_len += len;
  454. sdio_slave_hal_send_desc_t new_desc = {
  455. .dma_desc = {
  456. .size = len,
  457. .length = len,
  458. .buf = addr,
  459. .owner = 1,
  460. // in stream mode, the eof is only appended (in ISR) when new packet is ready to be sent
  461. .eof = (hal->sending_mode == SDIO_SLAVE_SEND_PACKET),
  462. },
  463. .arg = arg,
  464. .pkt_len = hal->tail_pkt_len,
  465. };
  466. esp_err_t ret = sdio_ringbuf_send(&(hal->send_desc_queue), send_write_desc, &new_desc);
  467. send_isr_invoke(hal);
  468. return ret;
  469. }
  470. /*---------------------------------------------------------------------------
  471. * Receive
  472. *--------------------------------------------------------------------------*/
  473. static lldesc_t* recv_get_first_empty_buf(sdio_slave_context_t* hal)
  474. {
  475. sdio_slave_hal_recv_stailq_t *const queue = &(hal->recv_link_list);
  476. lldesc_t *desc = STAILQ_FIRST(queue);
  477. while(desc && desc->owner == 0) {
  478. desc = STAILQ_NEXT(desc, qe);
  479. }
  480. return desc;
  481. }
  482. void sdio_slave_hal_recv_stop(sdio_slave_context_t* hal)
  483. {
  484. sdio_slave_ll_set_ioready(hal->hinf, false); //set IO ready to 0 to stop host from using
  485. sdio_slave_ll_send_stop(hal->slc);
  486. sdio_slave_ll_recv_stop(hal->slc);
  487. sdio_slave_ll_recv_intr_ena(hal->slc, false);
  488. }
  489. //touching linked list, should be protected by spinlock
  490. bool sdio_slave_hal_recv_has_next_item(sdio_slave_context_t* hal)
  491. {
  492. if (hal->recv_cur_ret == NULL || hal->recv_cur_ret->owner != 0) return false;
  493. // This may cause the ``cur_ret`` pointer to be NULL, indicating the list is empty,
  494. // in this case the ``tx_done`` should happen no longer until new desc is appended.
  495. // The app is responsible to place the pointer to the right place again when appending new desc.
  496. hal->recv_cur_ret = STAILQ_NEXT(hal->recv_cur_ret, qe);
  497. return true;
  498. }
  499. bool sdio_slave_hal_recv_done(sdio_slave_context_t *hal)
  500. {
  501. bool ret = sdio_slave_ll_recv_done(hal->slc);
  502. if (ret) {
  503. sdio_slave_ll_recv_done_clear(hal->slc);
  504. }
  505. return ret;
  506. }
  507. lldesc_t *sdio_slave_hal_recv_unload_desc(sdio_slave_context_t *hal)
  508. {
  509. sdio_slave_hal_recv_stailq_t *const queue = &hal->recv_link_list;
  510. lldesc_t *desc = STAILQ_FIRST(queue);
  511. if (desc) {
  512. STAILQ_REMOVE_HEAD(queue, qe);
  513. }
  514. return desc;
  515. }
  516. void sdio_slave_hal_recv_init_desc(sdio_slave_context_t* hal, lldesc_t *desc, uint8_t *start)
  517. {
  518. *desc = (lldesc_t) {
  519. .size = hal->recv_buffer_size,
  520. .buf = start,
  521. };
  522. }
  523. void sdio_slave_hal_recv_start(sdio_slave_context_t *hal)
  524. {
  525. sdio_slave_ll_recv_reset(hal->slc);
  526. lldesc_t *desc = recv_get_first_empty_buf(hal);
  527. if (!desc) {
  528. HAL_LOGD(TAG, "recv: restart without desc");
  529. } else {
  530. //the counter is handled when add/flush/reset
  531. sdio_slave_ll_recv_start(hal->slc, desc);
  532. sdio_slave_ll_recv_intr_ena(hal->slc, true);
  533. }
  534. }
  535. void sdio_slave_hal_recv_reset_counter(sdio_slave_context_t *hal)
  536. {
  537. sdio_slave_ll_recv_size_reset(hal->slc);
  538. lldesc_t *desc = recv_get_first_empty_buf(hal);
  539. while (desc != NULL) {
  540. sdio_slave_ll_recv_size_inc(hal->slc);
  541. desc = STAILQ_NEXT(desc, qe);
  542. }
  543. }
  544. void sdio_slave_hal_recv_flush_one_buffer(sdio_slave_context_t *hal)
  545. {
  546. sdio_slave_hal_recv_stailq_t *const queue = &hal->recv_link_list;
  547. lldesc_t *desc = STAILQ_FIRST(queue);
  548. assert (desc != NULL && desc->owner == 0);
  549. STAILQ_REMOVE_HEAD(queue, qe);
  550. desc->owner = 1;
  551. STAILQ_INSERT_TAIL(queue, desc, qe);
  552. sdio_slave_ll_recv_size_inc(hal->slc);
  553. //we only add it to the tail here, without start the DMA nor increase buffer num.
  554. }
  555. void sdio_slave_hal_load_buf(sdio_slave_context_t *hal, lldesc_t *desc)
  556. {
  557. sdio_slave_hal_recv_stailq_t *const queue = &(hal->recv_link_list);
  558. desc->owner = 1;
  559. lldesc_t *const tail = STAILQ_LAST(queue, lldesc_s, qe);
  560. STAILQ_INSERT_TAIL(queue, desc, qe);
  561. if (hal->recv_cur_ret == NULL) {
  562. hal->recv_cur_ret = desc;
  563. }
  564. if (tail == NULL) {
  565. //no one in the ll, start new ll operation.
  566. sdio_slave_ll_recv_start(hal->slc, desc);
  567. sdio_slave_ll_recv_intr_ena(hal->slc, true);
  568. HAL_LOGV(TAG, "recv_load_buf: start new");
  569. } else {
  570. //restart former ll operation
  571. sdio_slave_ll_recv_restart(hal->slc);
  572. HAL_LOGV(TAG, "recv_load_buf: restart");
  573. }
  574. sdio_slave_ll_recv_size_inc(hal->slc);
  575. }
  576. static inline void show_queue_item(lldesc_t *item)
  577. {
  578. HAL_EARLY_LOGI(TAG, "=> %p: size: %d(%d), eof: %d, owner: %d", item, item->size, item->length, item->eof, item->owner);
  579. HAL_EARLY_LOGI(TAG, " buf: %p, stqe_next: %p", item->buf, item->qe.stqe_next);
  580. }
  581. static void __attribute((unused)) dump_queue(sdio_slave_hal_recv_stailq_t *queue)
  582. {
  583. int cnt = 0;
  584. lldesc_t *item = NULL;
  585. HAL_EARLY_LOGI(TAG, ">>>>> first: %p, last: %p <<<<<", queue->stqh_first, queue->stqh_last);
  586. STAILQ_FOREACH(item, queue, qe) {
  587. cnt++;
  588. show_queue_item(item);
  589. }
  590. HAL_EARLY_LOGI(TAG, "total: %d", cnt);
  591. }
  592. /*---------------------------------------------------------------------------
  593. * Host
  594. *--------------------------------------------------------------------------*/
  595. void sdio_slave_hal_hostint_get_ena(sdio_slave_context_t *hal, sdio_slave_hostint_t *out_int_mask)
  596. {
  597. *out_int_mask = sdio_slave_ll_host_get_intena(hal->host);
  598. }
  599. void sdio_slave_hal_hostint_clear(sdio_slave_context_t *hal, const sdio_slave_hostint_t *mask)
  600. {
  601. sdio_slave_ll_host_intr_clear(hal->host, mask);//clear all interrupts
  602. }
  603. void sdio_slave_hal_hostint_set_ena(sdio_slave_context_t *hal, const sdio_slave_hostint_t *mask)
  604. {
  605. sdio_slave_ll_host_set_intena(hal->host, mask);
  606. }
  607. void sdio_slave_hal_hostint_send(sdio_slave_context_t *hal, const sdio_slave_hostint_t *mask)
  608. {
  609. sdio_slave_ll_host_send_int(hal->slc, mask);
  610. }
  611. uint8_t sdio_slave_hal_host_get_reg(sdio_slave_context_t *hal, int pos)
  612. {
  613. return sdio_slave_ll_host_get_reg(hal->host, pos);
  614. }
  615. void sdio_slave_hal_host_set_reg(sdio_slave_context_t *hal, int pos, uint8_t reg)
  616. {
  617. sdio_slave_ll_host_set_reg(hal->host, pos, reg);
  618. }
  619. void sdio_slave_hal_slvint_fetch_clear(sdio_slave_context_t *hal, sdio_slave_ll_slvint_t *out_int_mask)
  620. {
  621. sdio_slave_ll_slvint_fetch_clear(hal->slc, out_int_mask);
  622. }