whd_network_buffer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2024 Evlers
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. * Change Logs:
  25. * Date Author Notes
  26. * 2023-12-21 Evlers first implementation
  27. */
  28. #include "whd.h"
  29. #include "whd_network_types.h"
  30. #include "lwip/memp.h"
  31. #include "lwip/pbuf.h"
  32. #define SDIO_BLOCK_SIZE (64U)
  33. whd_result_t cy_host_buffer_get(whd_buffer_t *buffer, whd_buffer_dir_t direction, uint16_t size,
  34. uint32_t timeout_ms)
  35. {
  36. struct pbuf *p = NULL;
  37. uint32_t counter = 0;
  38. do
  39. {
  40. counter ++;
  41. if (direction == WHD_NETWORK_TX)
  42. {
  43. /* Allocate from the POOL if possible to avoid dynamic memory allocation */
  44. pbuf_type type = (size <= PBUF_POOL_BUFSIZE) ? PBUF_POOL : PBUF_RAM;
  45. p = pbuf_alloc(PBUF_RAW, size, type);
  46. }
  47. else
  48. {
  49. /* Increase allocation size to ensure the SDIO can write fully aligned blocks for
  50. * best throughput performance
  51. */
  52. p = pbuf_alloc(PBUF_RAW, size + SDIO_BLOCK_SIZE, PBUF_RAM);
  53. if (p != NULL)
  54. {
  55. p->len = size;
  56. p->tot_len -= SDIO_BLOCK_SIZE;
  57. }
  58. }
  59. if (NULL == p)
  60. {
  61. rt_thread_mdelay(1);
  62. }
  63. }
  64. while ((NULL == p) && (counter <= timeout_ms));
  65. if (p != NULL)
  66. {
  67. *buffer = p;
  68. return WHD_SUCCESS;
  69. }
  70. else
  71. {
  72. return WHD_BUFFER_ALLOC_FAIL;
  73. }
  74. }
  75. void cy_buffer_release(whd_buffer_t buffer, whd_buffer_dir_t direction)
  76. {
  77. UNUSED_PARAMETER(direction);
  78. (void)pbuf_free((struct pbuf *)buffer);
  79. }
  80. uint8_t *cy_buffer_get_current_piece_data_pointer(whd_buffer_t buffer)
  81. {
  82. RT_ASSERT(buffer != NULL);
  83. struct pbuf *pbuffer = (struct pbuf *)buffer;
  84. return (uint8_t *)pbuffer->payload;
  85. }
  86. uint16_t cy_buffer_get_current_piece_size(whd_buffer_t buffer)
  87. {
  88. RT_ASSERT(buffer != NULL);
  89. struct pbuf *pbuffer = (struct pbuf *)buffer;
  90. return (uint16_t)pbuffer->len;
  91. }
  92. whd_result_t cy_buffer_set_size(whd_buffer_t buffer, uint16_t size)
  93. {
  94. RT_ASSERT(buffer != NULL);
  95. struct pbuf *pbuffer = (struct pbuf *)buffer;
  96. if (size > ((uint16_t)WHD_LINK_MTU +
  97. LWIP_MEM_ALIGN_SIZE(LWIP_MEM_ALIGN_SIZE(sizeof(struct pbuf))) +
  98. LWIP_MEM_ALIGN_SIZE(size)))
  99. {
  100. return WHD_BUFFER_SIZE_SET_ERROR;
  101. }
  102. pbuffer->tot_len = size;
  103. pbuffer->len = size;
  104. return CY_RSLT_SUCCESS;
  105. }
  106. whd_result_t cy_buffer_add_remove_at_front(whd_buffer_t *buffer, int32_t add_remove_amount)
  107. {
  108. RT_ASSERT(buffer != NULL);
  109. struct pbuf **pbuffer = (struct pbuf **)buffer;
  110. if ((uint8_t)0 != pbuf_header(*pbuffer, (int16_t)(-add_remove_amount)))
  111. {
  112. return WHD_BUFFER_POINTER_MOVE_ERROR;
  113. }
  114. return WHD_SUCCESS;
  115. }
  116. struct whd_buffer_funcs whd_buffer_ops =
  117. {
  118. .whd_buffer_add_remove_at_front = cy_buffer_add_remove_at_front,
  119. .whd_buffer_get_current_piece_data_pointer = cy_buffer_get_current_piece_data_pointer,
  120. .whd_buffer_get_current_piece_size = cy_buffer_get_current_piece_size,
  121. .whd_buffer_release = cy_buffer_release,
  122. .whd_buffer_set_size = cy_buffer_set_size,
  123. .whd_host_buffer_get = cy_host_buffer_get,
  124. };