essl.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "essl.h"
  15. #include "essl_internal.h"
  16. #include "esp_log.h"
  17. #include "freertos/task.h"
  18. #define TIME_EXPIRED_SINCE_CORE(start, end, timeout, max) (bool)((end)>=(start)? \
  19. ((end)-(start)>(timeout)) :\
  20. ((max)-(timeout)>(start)-(end)))
  21. #define TIME_EXPIRED_SINCE(start, end, timeout) TIME_EXPIRED_SINCE_CORE(start, end, timeout, UINT32_MAX)
  22. #define MINUS_UNTIL_ZERO(a, b) ( ((a) > (b)) ? ((a)-(b)): 0)
  23. #define TIME_REMAIN_CORE(start, end, timeout, max) ((end)>=(start)?\
  24. MINUS_UNTIL_ZERO(timeout, (end)-(start)):\
  25. MINUS_UNTIL_ZERO((start)-(end), (max)-(timeout)))
  26. #define TIME_REMAIN(start, end, timeout) TIME_REMAIN_CORE(start, end, timeout, UINT32_MAX)
  27. #define ESSL_MIN(a, b) ((a) < (b) ? (a) : (b))
  28. __attribute__((unused)) static const char TAG[] = "esp_serial_slave_link";
  29. #define _CHECK_EXECUTE_CMD(DEV, CMD, STR, ...) do{ \
  30. if ((DEV) == NULL) { \
  31. return ESP_ERR_INVALID_ARG; \
  32. } \
  33. if ((DEV)->CMD) { \
  34. return (DEV)->CMD((DEV)->args,##__VA_ARGS__); \
  35. } else { \
  36. ESP_LOGE(TAG, STR); \
  37. return ESP_ERR_NOT_SUPPORTED; \
  38. } } while(0)
  39. #define CHECK_EXECUTE_CMD(DEV, CMD, ...) _CHECK_EXECUTE_CMD(DEV, CMD, #CMD" not supported for the current device.",##__VA_ARGS__)
  40. esp_err_t essl_init(essl_handle_t handle, uint32_t wait_ms)
  41. {
  42. CHECK_EXECUTE_CMD(handle, init, wait_ms);
  43. }
  44. esp_err_t essl_wait_for_ready(essl_handle_t handle, uint32_t wait_ms)
  45. {
  46. CHECK_EXECUTE_CMD(handle, wait_for_ready, wait_ms);
  47. }
  48. esp_err_t essl_send_packet(essl_handle_t handle, const void *start, size_t length, uint32_t wait_ms)
  49. {
  50. if (handle == NULL) {
  51. return ESP_ERR_INVALID_ARG;
  52. }
  53. if (start == NULL || length == 0) {
  54. return ESP_ERR_INVALID_ARG;
  55. }
  56. if (handle->send_packet == NULL) {
  57. return ESP_ERR_NOT_SUPPORTED;
  58. }
  59. esp_err_t err;
  60. const uint32_t timeout_ticks = pdMS_TO_TICKS(wait_ms);
  61. uint32_t pre = xTaskGetTickCount();
  62. uint32_t now;
  63. uint32_t remain_wait_ms = 0;
  64. do {
  65. now = xTaskGetTickCount();
  66. remain_wait_ms = pdTICKS_TO_MS(TIME_REMAIN(pre, now, timeout_ticks));
  67. err = handle->send_packet(handle->args, start, length, remain_wait_ms);
  68. if (err == ESP_OK) {
  69. break;
  70. } else if (err != ESP_ERR_NOT_FOUND) {
  71. return err;
  72. } // else ESP_ERR_NOT_FOUND
  73. //the slave has no enough memory, retry
  74. } while (remain_wait_ms > 0);
  75. return ESP_OK;
  76. }
  77. esp_err_t essl_get_packet(essl_handle_t handle, void *out_data, size_t size, size_t *out_length, uint32_t wait_ms)
  78. {
  79. if (handle == NULL) {
  80. return ESP_ERR_INVALID_ARG;
  81. }
  82. if (out_data == NULL || size == 0 || out_length == NULL) {
  83. return ESP_ERR_INVALID_ARG;
  84. }
  85. if (handle->get_packet == NULL || handle->update_rx_data_size == NULL || handle->get_rx_data_size == NULL) {
  86. return ESP_ERR_NOT_SUPPORTED;
  87. }
  88. esp_err_t err;
  89. const uint32_t timeout_ticks = pdMS_TO_TICKS(wait_ms);
  90. uint32_t pre = xTaskGetTickCount();
  91. uint32_t now = 3;
  92. uint32_t wait_remain_ms = 0;
  93. int data_available = handle->get_rx_data_size(handle->args);
  94. // if there is already enough data to read, skip the length update.
  95. if (data_available < size) {
  96. //loop until timeout, or there is at least one byte
  97. do {
  98. now = xTaskGetTickCount();
  99. wait_remain_ms = pdTICKS_TO_MS(TIME_REMAIN(pre, now, timeout_ticks));
  100. err = handle->update_rx_data_size(handle->args, wait_remain_ms);
  101. if (err != ESP_OK) {
  102. return err;
  103. }
  104. data_available = handle->get_rx_data_size(handle->args);
  105. if (data_available > 0) {
  106. break;
  107. }
  108. } while (wait_remain_ms > 0);
  109. }
  110. if (data_available == 0) {
  111. //the slave has no data to send
  112. return ESP_ERR_NOT_FOUND;
  113. }
  114. int len = ESSL_MIN(data_available, size);
  115. now = xTaskGetTickCount();
  116. wait_remain_ms = pdTICKS_TO_MS(TIME_REMAIN(pre, now, timeout_ticks));
  117. err = handle->get_packet(handle->args, out_data, len, wait_remain_ms);
  118. if (err != ESP_OK) {
  119. return err;
  120. }
  121. *out_length = len;
  122. if (len < data_available) {
  123. return ESP_ERR_NOT_FINISHED;
  124. }
  125. return ESP_OK;
  126. }
  127. esp_err_t essl_get_tx_buffer_num(essl_handle_t handle, uint32_t *out_tx_num, uint32_t wait_ms)
  128. {
  129. if (handle == NULL || out_tx_num == NULL) {
  130. return ESP_ERR_INVALID_ARG;
  131. }
  132. if (handle->update_tx_buffer_num == NULL|| handle->get_tx_buffer_num == NULL) {
  133. return ESP_ERR_NOT_SUPPORTED;
  134. }
  135. esp_err_t err = handle->update_tx_buffer_num(handle->args, wait_ms);
  136. if (err != ESP_OK) {
  137. return err;
  138. }
  139. *out_tx_num = handle->get_tx_buffer_num(handle->args);
  140. return ESP_OK;
  141. }
  142. esp_err_t essl_get_rx_data_size(essl_handle_t handle, uint32_t *out_rx_size, uint32_t wait_ms)
  143. {
  144. if (handle == NULL || out_rx_size == NULL) {
  145. return ESP_ERR_INVALID_ARG;
  146. }
  147. if (handle->update_rx_data_size == NULL|| handle->get_rx_data_size == NULL) {
  148. return ESP_ERR_NOT_SUPPORTED;
  149. }
  150. esp_err_t err = handle->update_rx_data_size(handle->args, wait_ms);
  151. if (err != ESP_OK) {
  152. return err;
  153. }
  154. *out_rx_size = handle->get_rx_data_size(handle->args);
  155. return ESP_OK;
  156. }
  157. esp_err_t essl_write_reg(essl_handle_t handle, uint8_t addr, uint8_t value, uint8_t *value_o, uint32_t wait_ms)
  158. {
  159. CHECK_EXECUTE_CMD(handle, write_reg, addr, value, value_o, wait_ms);
  160. }
  161. esp_err_t essl_read_reg(essl_handle_t handle, uint8_t add, uint8_t *value_o, uint32_t wait_ms)
  162. {
  163. CHECK_EXECUTE_CMD(handle, read_reg, add, value_o, wait_ms);
  164. }
  165. esp_err_t essl_wait_int(essl_handle_t handle, TickType_t wait_ms)
  166. {
  167. CHECK_EXECUTE_CMD(handle, wait_int, wait_ms);
  168. }
  169. esp_err_t essl_reset_cnt(essl_handle_t handle)
  170. {
  171. if (handle == NULL) {
  172. return ESP_ERR_INVALID_ARG;
  173. }
  174. if (handle->reset_cnt == NULL) {
  175. return ESP_ERR_NOT_SUPPORTED;
  176. }
  177. handle->reset_cnt(handle->args);
  178. return ESP_OK;
  179. }
  180. esp_err_t essl_clear_intr(essl_handle_t handle, uint32_t intr_mask, uint32_t wait_ms)
  181. {
  182. CHECK_EXECUTE_CMD(handle, clear_intr, intr_mask, wait_ms);
  183. }
  184. esp_err_t essl_get_intr(essl_handle_t handle, uint32_t *intr_raw, uint32_t *intr_st, uint32_t wait_ms)
  185. {
  186. if (intr_raw == NULL && intr_st == NULL) {
  187. return ESP_ERR_INVALID_ARG;
  188. }
  189. CHECK_EXECUTE_CMD(handle, get_intr, intr_raw, intr_st, wait_ms);
  190. }
  191. esp_err_t essl_set_intr_ena(essl_handle_t handle, uint32_t ena_mask, uint32_t wait_ms)
  192. {
  193. CHECK_EXECUTE_CMD(handle, set_intr_ena, ena_mask, wait_ms);
  194. }
  195. esp_err_t essl_get_intr_ena(essl_handle_t handle, uint32_t *ena_mask_o, uint32_t wait_ms)
  196. {
  197. if (ena_mask_o == NULL) {
  198. return ESP_ERR_INVALID_ARG;
  199. }
  200. CHECK_EXECUTE_CMD(handle, get_intr_ena, ena_mask_o, wait_ms);
  201. }
  202. esp_err_t essl_send_slave_intr(essl_handle_t handle, uint32_t intr_mask, uint32_t wait_ms)
  203. {
  204. CHECK_EXECUTE_CMD(handle, send_slave_intr, intr_mask, wait_ms);
  205. }