nrf_queue.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(NRF_QUEUE)
  42. #include "nrf_queue.h"
  43. #include "app_util_platform.h"
  44. #if NRF_QUEUE_CONFIG_LOG_ENABLED
  45. #define NRF_LOG_LEVEL NRF_QUEUE_CONFIG_LOG_LEVEL
  46. #define NRF_LOG_INIT_FILTER_LEVEL NRF_QUEUE_CONFIG_LOG_INIT_FILTER_LEVEL
  47. #define NRF_LOG_INFO_COLOR NRF_QUEUE_CONFIG_INFO_COLOR
  48. #define NRF_LOG_DEBUG_COLOR NRF_QUEUE_CONFIG_DEBUG_COLOR
  49. #else
  50. #define NRF_LOG_LEVEL 0
  51. #endif // NRF_QUEUE_CONFIG_LOG_ENABLED
  52. #include "nrf_log.h"
  53. NRF_SECTION_DEF(nrf_queue, nrf_queue_t);
  54. #if NRF_QUEUE_CLI_CMDS && NRF_CLI_ENABLED
  55. #include "nrf_cli.h"
  56. static void nrf_queue_status(nrf_cli_t const * p_cli, size_t argc, char **argv)
  57. {
  58. UNUSED_PARAMETER(argv);
  59. if (nrf_cli_help_requested(p_cli))
  60. {
  61. nrf_cli_help_print(p_cli, NULL, 0);
  62. return;
  63. }
  64. if (argc > 1)
  65. {
  66. nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "Bad argument count");
  67. return;
  68. }
  69. uint32_t num_of_instances = NRF_SECTION_ITEM_COUNT(nrf_queue, nrf_queue_t);
  70. uint32_t i;
  71. for (i = 0; i < num_of_instances; i++)
  72. {
  73. const nrf_queue_t * p_instance = NRF_SECTION_ITEM_GET(nrf_queue, nrf_queue_t, i);
  74. uint32_t element_size = p_instance->element_size;
  75. uint32_t size = p_instance->size;
  76. uint32_t max_util = nrf_queue_max_utilization_get(p_instance);
  77. uint32_t util = nrf_queue_utilization_get(p_instance);
  78. const char * p_name = p_instance->p_name;
  79. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL,
  80. "%s\r\n\t- Element size:\t%d\r\n"
  81. "\t- Usage:\t%u%% (%u out of %u elements)\r\n"
  82. "\t- Maximum:\t%u%% (%u out of %u elements)\r\n"
  83. "\t- Mode:\t\t%s\r\n\r\n",
  84. p_name, element_size,
  85. 100ul * util/size, util,size,
  86. 100ul * max_util/size, max_util,size,
  87. (p_instance->mode == NRF_QUEUE_MODE_OVERFLOW) ? "Overflow" : "No overflow");
  88. }
  89. }
  90. // Register "queue" command and its subcommands in CLI.
  91. NRF_CLI_CREATE_STATIC_SUBCMD_SET(nrf_queue_commands)
  92. {
  93. NRF_CLI_CMD(status, NULL, "Print status of queue instances.", nrf_queue_status),
  94. NRF_CLI_SUBCMD_SET_END
  95. };
  96. NRF_CLI_CMD_REGISTER(queue, &nrf_queue_commands, "Commands for BALLOC management", nrf_queue_status);
  97. #endif //NRF_QUEUE_CLI_CMDS
  98. __STATIC_INLINE size_t circullar_buffer_size_get(nrf_queue_t const * p_queue)
  99. {
  100. static const uint8_t full_queue_indicator = 1;
  101. /* When a queue is implemented as a cyclic buffer, it is not possible to
  102. * distinguish a full queue from an empty queue. In order to solve this
  103. * problem, the cyclic buffer has been implemented one element larger than
  104. * the queue size.
  105. */
  106. return p_queue->size + full_queue_indicator;
  107. }
  108. /**@brief Get next element index.
  109. *
  110. * @param[in] p_queue Pointer to the queue instance.
  111. * @param[in] idx Current index.
  112. *
  113. * @return Next element index.
  114. */
  115. __STATIC_INLINE size_t nrf_queue_next_idx(nrf_queue_t const * p_queue, size_t idx)
  116. {
  117. ASSERT(p_queue != NULL);
  118. return (idx < p_queue->size) ? (idx + 1) : 0;
  119. }
  120. /**@brief Get current queue utilization. This function assumes that this process will not be interrupted.
  121. *
  122. * @param[in] p_queue Pointer to the queue instance.
  123. *
  124. * @return Current queue utilization.
  125. */
  126. __STATIC_INLINE size_t queue_utilization_get(nrf_queue_t const * p_queue)
  127. {
  128. size_t front = p_queue->p_cb->front;
  129. size_t back = p_queue->p_cb->back;
  130. return (back >= front) ? (back - front) :
  131. (circullar_buffer_size_get(p_queue) - front + back);
  132. }
  133. bool nrf_queue_is_full(nrf_queue_t const * p_queue)
  134. {
  135. ASSERT(p_queue != NULL);
  136. size_t front = p_queue->p_cb->front;
  137. size_t back = p_queue->p_cb->back;
  138. return (nrf_queue_next_idx(p_queue, back) == front);
  139. }
  140. ret_code_t nrf_queue_push(nrf_queue_t const * p_queue, void const * p_element)
  141. {
  142. ret_code_t status = NRF_SUCCESS;
  143. ASSERT(p_queue != NULL);
  144. ASSERT(p_element != NULL);
  145. CRITICAL_REGION_ENTER();
  146. bool is_full = nrf_queue_is_full(p_queue);
  147. if (!is_full || (p_queue->mode == NRF_QUEUE_MODE_OVERFLOW))
  148. {
  149. // Get write position.
  150. size_t write_pos = p_queue->p_cb->back;
  151. p_queue->p_cb->back = nrf_queue_next_idx(p_queue, p_queue->p_cb->back);
  152. if (is_full)
  153. {
  154. // Overwrite the oldest element.
  155. NRF_LOG_INST_WARNING(p_queue->p_log, "Queue full. Overwriting oldest element.");
  156. p_queue->p_cb->front = nrf_queue_next_idx(p_queue, p_queue->p_cb->front);
  157. }
  158. // Write a new element.
  159. switch (p_queue->element_size)
  160. {
  161. case sizeof(uint8_t):
  162. ((uint8_t *)p_queue->p_buffer)[write_pos] = *((uint8_t *)p_element);
  163. break;
  164. case sizeof(uint16_t):
  165. ((uint16_t *)p_queue->p_buffer)[write_pos] = *((uint16_t *)p_element);
  166. break;
  167. case sizeof(uint32_t):
  168. ((uint32_t *)p_queue->p_buffer)[write_pos] = *((uint32_t *)p_element);
  169. break;
  170. case sizeof(uint64_t):
  171. ((uint64_t *)p_queue->p_buffer)[write_pos] = *((uint64_t *)p_element);
  172. break;
  173. default:
  174. memcpy((void *)((size_t)p_queue->p_buffer + write_pos * p_queue->element_size),
  175. p_element,
  176. p_queue->element_size);
  177. break;
  178. }
  179. // Update utilization.
  180. size_t utilization = queue_utilization_get(p_queue);
  181. if (p_queue->p_cb->max_utilization < utilization)
  182. {
  183. p_queue->p_cb->max_utilization = utilization;
  184. }
  185. }
  186. else
  187. {
  188. status = NRF_ERROR_NO_MEM;
  189. }
  190. CRITICAL_REGION_EXIT();
  191. NRF_LOG_INST_DEBUG(p_queue->p_log, "pushed element 0x%08X, status:%d", p_element, status);
  192. return status;
  193. }
  194. ret_code_t nrf_queue_generic_pop(nrf_queue_t const * p_queue,
  195. void * p_element,
  196. bool just_peek)
  197. {
  198. ret_code_t status = NRF_SUCCESS;
  199. ASSERT(p_queue != NULL);
  200. ASSERT(p_element != NULL);
  201. CRITICAL_REGION_ENTER();
  202. if (!nrf_queue_is_empty(p_queue))
  203. {
  204. // Get read position.
  205. size_t read_pos = p_queue->p_cb->front;
  206. // Update next read position.
  207. if (!just_peek)
  208. {
  209. p_queue->p_cb->front = nrf_queue_next_idx(p_queue, p_queue->p_cb->front);
  210. }
  211. // Read element.
  212. switch (p_queue->element_size)
  213. {
  214. case sizeof(uint8_t):
  215. *((uint8_t *)p_element) = ((uint8_t *)p_queue->p_buffer)[read_pos];
  216. break;
  217. case sizeof(uint16_t):
  218. *((uint16_t *)p_element) = ((uint16_t *)p_queue->p_buffer)[read_pos];
  219. break;
  220. case sizeof(uint32_t):
  221. *((uint32_t *)p_element) = ((uint32_t *)p_queue->p_buffer)[read_pos];
  222. break;
  223. case sizeof(uint64_t):
  224. *((uint64_t *)p_element) = ((uint64_t *)p_queue->p_buffer)[read_pos];
  225. break;
  226. default:
  227. memcpy(p_element,
  228. (void const *)((size_t)p_queue->p_buffer + read_pos * p_queue->element_size),
  229. p_queue->element_size);
  230. break;
  231. }
  232. }
  233. else
  234. {
  235. status = NRF_ERROR_NOT_FOUND;
  236. }
  237. CRITICAL_REGION_EXIT();
  238. NRF_LOG_INST_DEBUG(p_queue->p_log, "%s element 0x%08X, status:%d",
  239. just_peek ? "peeked" : "popped", p_element, status);
  240. return status;
  241. }
  242. /* Purpose of this function is to provide number of continous bytes in the queue's
  243. * array before circullar buffer needs to wrapp.
  244. */
  245. static size_t continous_items_get(nrf_queue_t const * p_queue, bool write)
  246. {
  247. size_t front = p_queue->p_cb->front;
  248. size_t back = p_queue->p_cb->back;
  249. /* Number of continous items for queue write operation */
  250. if (write)
  251. {
  252. return (back >= front) ? circullar_buffer_size_get(p_queue) - back : front - back;
  253. }
  254. else
  255. {
  256. return (back >= front) ? back - front : circullar_buffer_size_get(p_queue) - front;
  257. }
  258. }
  259. /**@brief Write elements to the queue. This function assumes that there is enough room in the queue
  260. * to write the requested number of elements and that this process will not be interrupted.
  261. *
  262. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  263. * @param[in] p_data Pointer to the buffer with elements to write.
  264. * @param[in] element_count Number of elements to write.
  265. */
  266. static void queue_write(nrf_queue_t const * p_queue, void const * p_data, uint32_t element_count)
  267. {
  268. size_t prev_available = nrf_queue_available_get(p_queue);
  269. size_t continuous = continous_items_get(p_queue, true);
  270. void * p_write_ptr = (void *)((size_t)p_queue->p_buffer
  271. + p_queue->p_cb->back * p_queue->element_size);
  272. if (element_count <= continuous)
  273. {
  274. memcpy(p_write_ptr,
  275. p_data,
  276. element_count * p_queue->element_size);
  277. p_queue->p_cb->back = ((p_queue->p_cb->back + element_count) <= p_queue->size)
  278. ? (p_queue->p_cb->back + element_count)
  279. : 0;
  280. }
  281. else
  282. {
  283. size_t first_write_length = continuous * p_queue->element_size;
  284. memcpy(p_write_ptr,
  285. p_data,
  286. first_write_length);
  287. size_t elements_left = element_count - continuous;
  288. memcpy(p_queue->p_buffer,
  289. (void const *)((size_t)p_data + first_write_length),
  290. elements_left * p_queue->element_size);
  291. p_queue->p_cb->back = elements_left;
  292. if (prev_available < element_count)
  293. {
  294. // Overwrite the oldest elements.
  295. p_queue->p_cb->front = nrf_queue_next_idx(p_queue, p_queue->p_cb->back);
  296. }
  297. }
  298. // Update utilization.
  299. size_t utilization = queue_utilization_get(p_queue);
  300. if (p_queue->p_cb->max_utilization < utilization)
  301. {
  302. p_queue->p_cb->max_utilization = utilization;
  303. }
  304. }
  305. ret_code_t nrf_queue_write(nrf_queue_t const * p_queue,
  306. void const * p_data,
  307. size_t element_count)
  308. {
  309. ret_code_t status = NRF_SUCCESS;
  310. ASSERT(p_queue != NULL);
  311. ASSERT(p_data != NULL);
  312. ASSERT(element_count <= p_queue->size);
  313. if (element_count == 0)
  314. {
  315. return NRF_SUCCESS;
  316. }
  317. CRITICAL_REGION_ENTER();
  318. if ((nrf_queue_available_get(p_queue) >= element_count)
  319. || (p_queue->mode == NRF_QUEUE_MODE_OVERFLOW))
  320. {
  321. queue_write(p_queue, p_data, element_count);
  322. }
  323. else
  324. {
  325. status = NRF_ERROR_NO_MEM;
  326. }
  327. CRITICAL_REGION_EXIT();
  328. NRF_LOG_INST_DEBUG(p_queue->p_log, "Write %d elements (start address: 0x%08X), status:%d",
  329. element_count, p_data, status);
  330. return status;
  331. }
  332. size_t nrf_queue_in(nrf_queue_t const * p_queue,
  333. void const * p_data,
  334. size_t element_count)
  335. {
  336. ASSERT(p_queue != NULL);
  337. ASSERT(p_data != NULL);
  338. size_t req_element_count = element_count;
  339. if (element_count == 0)
  340. {
  341. return 0;
  342. }
  343. CRITICAL_REGION_ENTER();
  344. if (p_queue->mode == NRF_QUEUE_MODE_OVERFLOW)
  345. {
  346. element_count = MIN(element_count, p_queue->size);
  347. }
  348. else
  349. {
  350. size_t available = nrf_queue_available_get(p_queue);
  351. element_count = MIN(element_count, available);
  352. }
  353. queue_write(p_queue, p_data, element_count);
  354. CRITICAL_REGION_EXIT();
  355. NRF_LOG_INST_DEBUG(p_queue->p_log, "Put in %d elements (start address: 0x%08X), requested :%d",
  356. element_count, p_data, req_element_count);
  357. return element_count;
  358. }
  359. /**@brief Read elements from the queue. This function assumes that there are enough elements
  360. * in the queue to read and that this process will not be interrupted.
  361. *
  362. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  363. * @param[out] p_data Pointer to the buffer where elements will be copied.
  364. * @param[in] element_count Number of elements to read.
  365. */
  366. static void queue_read(nrf_queue_t const * p_queue, void * p_data, uint32_t element_count)
  367. {
  368. size_t front = p_queue->p_cb->front;
  369. size_t continuous = continous_items_get(p_queue, false);
  370. void const * p_read_ptr = (void const *)((size_t)p_queue->p_buffer
  371. + front * p_queue->element_size);
  372. if (element_count <= continuous)
  373. {
  374. memcpy(p_data,
  375. p_read_ptr,
  376. element_count * p_queue->element_size);
  377. p_queue->p_cb->front = ((front + element_count) <= p_queue->size)
  378. ? (front + element_count)
  379. : 0;
  380. }
  381. else
  382. {
  383. size_t first_read_length = continuous * p_queue->element_size;
  384. memcpy(p_data,
  385. p_read_ptr,
  386. first_read_length);
  387. size_t elements_left = element_count - continuous;
  388. memcpy((void *)((size_t)p_data + first_read_length),
  389. p_queue->p_buffer,
  390. elements_left * p_queue->element_size);
  391. p_queue->p_cb->front = elements_left;
  392. }
  393. }
  394. ret_code_t nrf_queue_read(nrf_queue_t const * p_queue,
  395. void * p_data,
  396. size_t element_count)
  397. {
  398. ret_code_t status = NRF_SUCCESS;
  399. ASSERT(p_queue != NULL);
  400. ASSERT(p_data != NULL);
  401. if (element_count == 0)
  402. {
  403. return NRF_SUCCESS;
  404. }
  405. CRITICAL_REGION_ENTER();
  406. if (element_count <= queue_utilization_get(p_queue))
  407. {
  408. queue_read(p_queue, p_data, element_count);
  409. }
  410. else
  411. {
  412. status = NRF_ERROR_NOT_FOUND;
  413. }
  414. CRITICAL_REGION_EXIT();
  415. NRF_LOG_INST_DEBUG(p_queue->p_log, "Read %d elements (start address: 0x%08X), status :%d",
  416. element_count, p_data, status);
  417. return status;
  418. }
  419. size_t nrf_queue_out(nrf_queue_t const * p_queue,
  420. void * p_data,
  421. size_t element_count)
  422. {
  423. ASSERT(p_queue != NULL);
  424. ASSERT(p_data != NULL);
  425. size_t req_element_count = element_count;
  426. if (element_count == 0)
  427. {
  428. return 0;
  429. }
  430. CRITICAL_REGION_ENTER();
  431. size_t utilization = queue_utilization_get(p_queue);
  432. element_count = MIN(element_count, utilization);
  433. queue_read(p_queue, p_data, element_count);
  434. CRITICAL_REGION_EXIT();
  435. NRF_LOG_INST_DEBUG(p_queue->p_log, "Out %d elements (start address: 0x%08X), requested :%d",
  436. element_count, p_data, req_element_count);
  437. return element_count;
  438. }
  439. void nrf_queue_reset(nrf_queue_t const * p_queue)
  440. {
  441. ASSERT(p_queue != NULL);
  442. CRITICAL_REGION_ENTER();
  443. memset(p_queue->p_cb, 0, sizeof(nrf_queue_cb_t));
  444. CRITICAL_REGION_EXIT();
  445. NRF_LOG_INST_DEBUG(p_queue->p_log, "Reset");
  446. }
  447. size_t nrf_queue_utilization_get(nrf_queue_t const * p_queue)
  448. {
  449. size_t utilization;
  450. ASSERT(p_queue != NULL);
  451. CRITICAL_REGION_ENTER();
  452. utilization = queue_utilization_get(p_queue);
  453. CRITICAL_REGION_EXIT();
  454. return utilization;
  455. }
  456. bool nrf_queue_is_empty(nrf_queue_t const * p_queue)
  457. {
  458. ASSERT(p_queue != NULL);
  459. size_t front = p_queue->p_cb->front;
  460. size_t back = p_queue->p_cb->back;
  461. return (front == back);
  462. }
  463. size_t nrf_queue_available_get(nrf_queue_t const * p_queue)
  464. {
  465. ASSERT(p_queue != NULL);
  466. return p_queue->size - nrf_queue_utilization_get(p_queue);
  467. }
  468. size_t nrf_queue_max_utilization_get(nrf_queue_t const * p_queue)
  469. {
  470. ASSERT(p_queue != NULL);
  471. return p_queue->p_cb->max_utilization;
  472. }
  473. void nrf_queue_max_utilization_reset(nrf_queue_t const * p_queue)
  474. {
  475. ASSERT(p_queue != NULL);
  476. p_queue->p_cb->max_utilization = 0;
  477. }
  478. #endif // NRF_MODULE_ENABLED(NRF_QUEUE)