essl.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 || start == NULL || length == 0) {
  51. return ESP_ERR_INVALID_ARG;
  52. }
  53. if (handle->send_packet == NULL) {
  54. return ESP_ERR_NOT_SUPPORTED;
  55. }
  56. esp_err_t err;
  57. const uint32_t timeout_ticks = pdMS_TO_TICKS(wait_ms);
  58. uint32_t pre = xTaskGetTickCount();
  59. uint32_t now;
  60. uint32_t remain_wait_ms = 0;
  61. do {
  62. now = xTaskGetTickCount();
  63. remain_wait_ms = pdTICKS_TO_MS(TIME_REMAIN(pre, now, timeout_ticks));
  64. err = handle->send_packet(handle->args, start, length, remain_wait_ms);
  65. if (err == ESP_OK) {
  66. break;
  67. } else if (err != ESP_ERR_NOT_FOUND) {
  68. return err;
  69. } // else ESP_ERR_NOT_FOUND
  70. //the slave is not ready, retry
  71. } while (remain_wait_ms > 0);
  72. return err;
  73. }
  74. esp_err_t essl_get_packet(essl_handle_t handle, void *out_data, size_t size, size_t *out_length, uint32_t wait_ms)
  75. {
  76. if (handle == NULL) {
  77. return ESP_ERR_INVALID_ARG;
  78. }
  79. if (out_data == NULL || size == 0 || out_length == NULL) {
  80. return ESP_ERR_INVALID_ARG;
  81. }
  82. if (handle->get_packet == NULL || handle->update_rx_data_size == NULL || handle->get_rx_data_size == NULL) {
  83. return ESP_ERR_NOT_SUPPORTED;
  84. }
  85. esp_err_t err;
  86. const uint32_t timeout_ticks = pdMS_TO_TICKS(wait_ms);
  87. uint32_t pre = xTaskGetTickCount();
  88. uint32_t now = 3;
  89. uint32_t wait_remain_ms = 0;
  90. int data_available = handle->get_rx_data_size(handle->args);
  91. // if there is already enough data to read, skip the length update.
  92. if (data_available < size) {
  93. //loop until timeout, or there is at least one byte
  94. do {
  95. now = xTaskGetTickCount();
  96. wait_remain_ms = pdTICKS_TO_MS(TIME_REMAIN(pre, now, timeout_ticks));
  97. err = handle->update_rx_data_size(handle->args, wait_remain_ms);
  98. if (err != ESP_OK) {
  99. return err;
  100. }
  101. data_available = handle->get_rx_data_size(handle->args);
  102. if (data_available > 0) {
  103. break;
  104. }
  105. } while (wait_remain_ms > 0);
  106. }
  107. if (data_available == 0) {
  108. //the slave has no data to send
  109. return ESP_ERR_NOT_FOUND;
  110. }
  111. int len = ESSL_MIN(data_available, size);
  112. now = xTaskGetTickCount();
  113. wait_remain_ms = pdTICKS_TO_MS(TIME_REMAIN(pre, now, timeout_ticks));
  114. err = handle->get_packet(handle->args, out_data, len, wait_remain_ms);
  115. if (err != ESP_OK) {
  116. return err;
  117. }
  118. *out_length = len;
  119. if (len < data_available) {
  120. return ESP_ERR_NOT_FINISHED;
  121. }
  122. return ESP_OK;
  123. }
  124. esp_err_t essl_get_tx_buffer_num(essl_handle_t handle, uint32_t *out_tx_num, uint32_t wait_ms)
  125. {
  126. if (handle == NULL || out_tx_num == NULL) {
  127. return ESP_ERR_INVALID_ARG;
  128. }
  129. if (handle->update_tx_buffer_num == NULL|| handle->get_tx_buffer_num == NULL) {
  130. return ESP_ERR_NOT_SUPPORTED;
  131. }
  132. esp_err_t err = handle->update_tx_buffer_num(handle->args, wait_ms);
  133. if (err != ESP_OK) {
  134. return err;
  135. }
  136. *out_tx_num = handle->get_tx_buffer_num(handle->args);
  137. return ESP_OK;
  138. }
  139. esp_err_t essl_get_rx_data_size(essl_handle_t handle, uint32_t *out_rx_size, uint32_t wait_ms)
  140. {
  141. if (handle == NULL || out_rx_size == NULL) {
  142. return ESP_ERR_INVALID_ARG;
  143. }
  144. if (handle->update_rx_data_size == NULL|| handle->get_rx_data_size == NULL) {
  145. return ESP_ERR_NOT_SUPPORTED;
  146. }
  147. esp_err_t err = handle->update_rx_data_size(handle->args, wait_ms);
  148. if (err != ESP_OK) {
  149. return err;
  150. }
  151. *out_rx_size = handle->get_rx_data_size(handle->args);
  152. return ESP_OK;
  153. }
  154. esp_err_t essl_write_reg(essl_handle_t handle, uint8_t addr, uint8_t value, uint8_t *value_o, uint32_t wait_ms)
  155. {
  156. CHECK_EXECUTE_CMD(handle, write_reg, addr, value, value_o, wait_ms);
  157. }
  158. esp_err_t essl_read_reg(essl_handle_t handle, uint8_t add, uint8_t *value_o, uint32_t wait_ms)
  159. {
  160. CHECK_EXECUTE_CMD(handle, read_reg, add, value_o, wait_ms);
  161. }
  162. esp_err_t essl_wait_int(essl_handle_t handle, TickType_t wait_ms)
  163. {
  164. CHECK_EXECUTE_CMD(handle, wait_int, wait_ms);
  165. }
  166. esp_err_t essl_reset_cnt(essl_handle_t handle)
  167. {
  168. if (handle == NULL) {
  169. return ESP_ERR_INVALID_ARG;
  170. }
  171. if (handle->reset_cnt == NULL) {
  172. return ESP_ERR_NOT_SUPPORTED;
  173. }
  174. handle->reset_cnt(handle->args);
  175. return ESP_OK;
  176. }
  177. esp_err_t essl_clear_intr(essl_handle_t handle, uint32_t intr_mask, uint32_t wait_ms)
  178. {
  179. CHECK_EXECUTE_CMD(handle, clear_intr, intr_mask, wait_ms);
  180. }
  181. esp_err_t essl_get_intr(essl_handle_t handle, uint32_t *intr_raw, uint32_t *intr_st, uint32_t wait_ms)
  182. {
  183. if (intr_raw == NULL && intr_st == NULL) {
  184. return ESP_ERR_INVALID_ARG;
  185. }
  186. CHECK_EXECUTE_CMD(handle, get_intr, intr_raw, intr_st, wait_ms);
  187. }
  188. esp_err_t essl_set_intr_ena(essl_handle_t handle, uint32_t ena_mask, uint32_t wait_ms)
  189. {
  190. CHECK_EXECUTE_CMD(handle, set_intr_ena, ena_mask, wait_ms);
  191. }
  192. esp_err_t essl_get_intr_ena(essl_handle_t handle, uint32_t *ena_mask_o, uint32_t wait_ms)
  193. {
  194. if (ena_mask_o == NULL) {
  195. return ESP_ERR_INVALID_ARG;
  196. }
  197. CHECK_EXECUTE_CMD(handle, get_intr_ena, ena_mask_o, wait_ms);
  198. }
  199. esp_err_t essl_send_slave_intr(essl_handle_t handle, uint32_t intr_mask, uint32_t wait_ms)
  200. {
  201. CHECK_EXECUTE_CMD(handle, send_slave_intr, intr_mask, wait_ms);
  202. }