nrf_queue.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /**
  2. * Copyright (c) 2016 - 2018, 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
  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. /**@brief Get next element index.
  99. *
  100. * @param[in] p_queue Pointer to the queue instance.
  101. * @param[in] idx Current index.
  102. *
  103. * @return Next element index.
  104. */
  105. __STATIC_INLINE size_t nrf_queue_next_idx(nrf_queue_t const * p_queue, size_t idx)
  106. {
  107. ASSERT(p_queue != NULL);
  108. return (idx < p_queue->size) ? (idx + 1) : 0;
  109. }
  110. /**@brief Get current queue utilization. This function assumes that this process will not be interrupted.
  111. *
  112. * @param[in] p_queue Pointer to the queue instance.
  113. *
  114. * @return Current queue utilization.
  115. */
  116. __STATIC_INLINE size_t queue_utilization_get(nrf_queue_t const * p_queue)
  117. {
  118. size_t front = p_queue->p_cb->front;
  119. size_t back = p_queue->p_cb->back;
  120. return (back >= front) ? (back - front) : (p_queue->size + 1 - front + back);
  121. }
  122. bool nrf_queue_is_full(nrf_queue_t const * p_queue)
  123. {
  124. ASSERT(p_queue != NULL);
  125. size_t front = p_queue->p_cb->front;
  126. size_t back = p_queue->p_cb->back;
  127. return (nrf_queue_next_idx(p_queue, back) == front);
  128. }
  129. ret_code_t nrf_queue_push(nrf_queue_t const * p_queue, void const * p_element)
  130. {
  131. ret_code_t status = NRF_SUCCESS;
  132. ASSERT(p_queue != NULL);
  133. ASSERT(p_element != NULL);
  134. CRITICAL_REGION_ENTER();
  135. bool is_full = nrf_queue_is_full(p_queue);
  136. if (!is_full || (p_queue->mode == NRF_QUEUE_MODE_OVERFLOW))
  137. {
  138. // Get write position.
  139. size_t write_pos = p_queue->p_cb->back;
  140. p_queue->p_cb->back = nrf_queue_next_idx(p_queue, p_queue->p_cb->back);
  141. if (is_full)
  142. {
  143. // Overwrite the oldest element.
  144. NRF_LOG_INST_WARNING(p_queue->p_log, "Queue full. Overwriting oldest element.");
  145. p_queue->p_cb->front = nrf_queue_next_idx(p_queue, p_queue->p_cb->front);
  146. }
  147. // Write a new element.
  148. switch (p_queue->element_size)
  149. {
  150. case sizeof(uint8_t):
  151. ((uint8_t *)p_queue->p_buffer)[write_pos] = *((uint8_t *)p_element);
  152. break;
  153. case sizeof(uint16_t):
  154. ((uint16_t *)p_queue->p_buffer)[write_pos] = *((uint16_t *)p_element);
  155. break;
  156. case sizeof(uint32_t):
  157. ((uint32_t *)p_queue->p_buffer)[write_pos] = *((uint32_t *)p_element);
  158. break;
  159. case sizeof(uint64_t):
  160. ((uint64_t *)p_queue->p_buffer)[write_pos] = *((uint64_t *)p_element);
  161. break;
  162. default:
  163. memcpy((void *)((size_t)p_queue->p_buffer + write_pos * p_queue->element_size),
  164. p_element,
  165. p_queue->element_size);
  166. break;
  167. }
  168. // Update utilization.
  169. size_t utilization = queue_utilization_get(p_queue);
  170. if (p_queue->p_cb->max_utilization < utilization)
  171. {
  172. p_queue->p_cb->max_utilization = utilization;
  173. }
  174. }
  175. else
  176. {
  177. status = NRF_ERROR_NO_MEM;
  178. }
  179. CRITICAL_REGION_EXIT();
  180. NRF_LOG_INST_DEBUG(p_queue->p_log, "pushed element 0x%08X, status:%d", p_element, status);
  181. return status;
  182. }
  183. ret_code_t nrf_queue_generic_pop(nrf_queue_t const * p_queue,
  184. void * p_element,
  185. bool just_peek)
  186. {
  187. ret_code_t status = NRF_SUCCESS;
  188. ASSERT(p_queue != NULL);
  189. ASSERT(p_element != NULL);
  190. CRITICAL_REGION_ENTER();
  191. if (!nrf_queue_is_empty(p_queue))
  192. {
  193. // Get read position.
  194. size_t read_pos = p_queue->p_cb->front;
  195. // Update next read position.
  196. if (!just_peek)
  197. {
  198. p_queue->p_cb->front = nrf_queue_next_idx(p_queue, p_queue->p_cb->front);
  199. }
  200. // Read element.
  201. switch (p_queue->element_size)
  202. {
  203. case sizeof(uint8_t):
  204. *((uint8_t *)p_element) = ((uint8_t *)p_queue->p_buffer)[read_pos];
  205. break;
  206. case sizeof(uint16_t):
  207. *((uint16_t *)p_element) = ((uint16_t *)p_queue->p_buffer)[read_pos];
  208. break;
  209. case sizeof(uint32_t):
  210. *((uint32_t *)p_element) = ((uint32_t *)p_queue->p_buffer)[read_pos];
  211. break;
  212. case sizeof(uint64_t):
  213. *((uint64_t *)p_element) = ((uint64_t *)p_queue->p_buffer)[read_pos];
  214. break;
  215. default:
  216. memcpy(p_element,
  217. (void const *)((size_t)p_queue->p_buffer + read_pos * p_queue->element_size),
  218. p_queue->element_size);
  219. break;
  220. }
  221. }
  222. else
  223. {
  224. status = NRF_ERROR_NOT_FOUND;
  225. }
  226. CRITICAL_REGION_EXIT();
  227. NRF_LOG_INST_DEBUG(p_queue->p_log, "%s element 0x%08X, status:%d",
  228. just_peek ? "peeked" : "popped", p_element, status);
  229. return status;
  230. }
  231. /**@brief Write elements to the queue. This function assumes that there is enough room in the queue
  232. * to write the requested number of elements and that this process will not be interrupted.
  233. *
  234. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  235. * @param[in] p_data Pointer to the buffer with elements to write.
  236. * @param[in] element_count Number of elements to write.
  237. */
  238. static void queue_write(nrf_queue_t const * p_queue, void const * p_data, uint32_t element_count)
  239. {
  240. size_t prev_available = nrf_queue_available_get(p_queue);
  241. size_t continuous = p_queue->size + 1 - p_queue->p_cb->back;
  242. void * p_write_ptr = (void *)((size_t)p_queue->p_buffer
  243. + p_queue->p_cb->back * p_queue->element_size);
  244. if (element_count <= continuous)
  245. {
  246. memcpy(p_write_ptr,
  247. p_data,
  248. element_count * p_queue->element_size);
  249. p_queue->p_cb->back = ((p_queue->p_cb->back + element_count) <= p_queue->size)
  250. ? (p_queue->p_cb->back + element_count)
  251. : 0;
  252. }
  253. else
  254. {
  255. size_t first_write_length = continuous * p_queue->element_size;
  256. memcpy(p_write_ptr,
  257. p_data,
  258. first_write_length);
  259. size_t elements_left = element_count - continuous;
  260. memcpy(p_queue->p_buffer,
  261. (void const *)((size_t)p_data + first_write_length),
  262. elements_left * p_queue->element_size);
  263. p_queue->p_cb->back = elements_left;
  264. if (prev_available < element_count)
  265. {
  266. // Overwrite the oldest elements.
  267. p_queue->p_cb->front = nrf_queue_next_idx(p_queue, p_queue->p_cb->back);
  268. }
  269. }
  270. // Update utilization.
  271. size_t utilization = queue_utilization_get(p_queue);
  272. if (p_queue->p_cb->max_utilization < utilization)
  273. {
  274. p_queue->p_cb->max_utilization = utilization;
  275. }
  276. }
  277. ret_code_t nrf_queue_write(nrf_queue_t const * p_queue,
  278. void const * p_data,
  279. size_t element_count)
  280. {
  281. ret_code_t status = NRF_SUCCESS;
  282. ASSERT(p_queue != NULL);
  283. ASSERT(p_data != NULL);
  284. ASSERT(element_count <= p_queue->size);
  285. if (element_count == 0)
  286. {
  287. return NRF_SUCCESS;
  288. }
  289. CRITICAL_REGION_ENTER();
  290. if ((nrf_queue_available_get(p_queue) >= element_count)
  291. || (p_queue->mode == NRF_QUEUE_MODE_OVERFLOW))
  292. {
  293. queue_write(p_queue, p_data, element_count);
  294. }
  295. else
  296. {
  297. status = NRF_ERROR_NO_MEM;
  298. }
  299. CRITICAL_REGION_EXIT();
  300. NRF_LOG_INST_DEBUG(p_queue->p_log, "Write %d elements (start address: 0x%08X), status:%d",
  301. element_count, p_data, status);
  302. return status;
  303. }
  304. size_t nrf_queue_in(nrf_queue_t const * p_queue,
  305. void const * p_data,
  306. size_t element_count)
  307. {
  308. ASSERT(p_queue != NULL);
  309. ASSERT(p_data != NULL);
  310. size_t req_element_count = element_count;
  311. if (element_count == 0)
  312. {
  313. return 0;
  314. }
  315. CRITICAL_REGION_ENTER();
  316. if (p_queue->mode == NRF_QUEUE_MODE_OVERFLOW)
  317. {
  318. element_count = MIN(element_count, p_queue->size);
  319. }
  320. else
  321. {
  322. size_t available = nrf_queue_available_get(p_queue);
  323. element_count = MIN(element_count, available);
  324. }
  325. queue_write(p_queue, p_data, element_count);
  326. CRITICAL_REGION_EXIT();
  327. NRF_LOG_INST_DEBUG(p_queue->p_log, "Put in %d elements (start address: 0x%08X), requested :%d",
  328. element_count, p_data, req_element_count);
  329. return element_count;
  330. }
  331. /**@brief Read elements from the queue. This function assumes that there are enough elements
  332. * in the queue to read and that this process will not be interrupted.
  333. *
  334. * @param[in] p_queue Pointer to the nrf_queue_t instance.
  335. * @param[out] p_data Pointer to the buffer where elements will be copied.
  336. * @param[in] element_count Number of elements to read.
  337. */
  338. static void queue_read(nrf_queue_t const * p_queue, void * p_data, uint32_t element_count)
  339. {
  340. size_t front = p_queue->p_cb->front;
  341. size_t back = p_queue->p_cb->back;
  342. size_t continuous = (front <= back) ? (back - front) : (p_queue->size + 1 - front);
  343. void const * p_read_ptr = (void const *)((size_t)p_queue->p_buffer
  344. + front * p_queue->element_size);
  345. if (element_count <= continuous)
  346. {
  347. memcpy(p_data,
  348. p_read_ptr,
  349. element_count * p_queue->element_size);
  350. p_queue->p_cb->front = ((front + element_count) <= p_queue->size)
  351. ? (front + element_count)
  352. : 0;
  353. }
  354. else
  355. {
  356. size_t first_read_length = continuous * p_queue->element_size;
  357. memcpy(p_data,
  358. p_read_ptr,
  359. first_read_length);
  360. size_t elements_left = element_count - continuous;
  361. memcpy((void *)((size_t)p_data + first_read_length),
  362. p_queue->p_buffer,
  363. elements_left * p_queue->element_size);
  364. p_queue->p_cb->front = elements_left;
  365. }
  366. }
  367. ret_code_t nrf_queue_read(nrf_queue_t const * p_queue,
  368. void * p_data,
  369. size_t element_count)
  370. {
  371. ret_code_t status = NRF_SUCCESS;
  372. ASSERT(p_queue != NULL);
  373. ASSERT(p_data != NULL);
  374. if (element_count == 0)
  375. {
  376. return NRF_SUCCESS;
  377. }
  378. CRITICAL_REGION_ENTER();
  379. if (element_count <= queue_utilization_get(p_queue))
  380. {
  381. queue_read(p_queue, p_data, element_count);
  382. }
  383. else
  384. {
  385. status = NRF_ERROR_NOT_FOUND;
  386. }
  387. CRITICAL_REGION_EXIT();
  388. NRF_LOG_INST_DEBUG(p_queue->p_log, "Read %d elements (start address: 0x%08X), status :%d",
  389. element_count, p_data, status);
  390. return status;
  391. }
  392. size_t nrf_queue_out(nrf_queue_t const * p_queue,
  393. void * p_data,
  394. size_t element_count)
  395. {
  396. ASSERT(p_queue != NULL);
  397. ASSERT(p_data != NULL);
  398. size_t req_element_count = element_count;
  399. if (element_count == 0)
  400. {
  401. return 0;
  402. }
  403. CRITICAL_REGION_ENTER();
  404. size_t utilization = queue_utilization_get(p_queue);
  405. element_count = MIN(element_count, utilization);
  406. queue_read(p_queue, p_data, element_count);
  407. CRITICAL_REGION_EXIT();
  408. NRF_LOG_INST_DEBUG(p_queue->p_log, "Out %d elements (start address: 0x%08X), requested :%d",
  409. element_count, p_data, req_element_count);
  410. return element_count;
  411. }
  412. void nrf_queue_reset(nrf_queue_t const * p_queue)
  413. {
  414. ASSERT(p_queue != NULL);
  415. CRITICAL_REGION_ENTER();
  416. memset(p_queue->p_cb, 0, sizeof(nrf_queue_cb_t));
  417. CRITICAL_REGION_EXIT();
  418. NRF_LOG_INST_DEBUG(p_queue->p_log, "Reset");
  419. }
  420. size_t nrf_queue_utilization_get(nrf_queue_t const * p_queue)
  421. {
  422. size_t utilization;
  423. ASSERT(p_queue != NULL);
  424. CRITICAL_REGION_ENTER();
  425. utilization = queue_utilization_get(p_queue);
  426. CRITICAL_REGION_EXIT();
  427. return utilization;
  428. }
  429. bool nrf_queue_is_empty(nrf_queue_t const * p_queue)
  430. {
  431. ASSERT(p_queue != NULL);
  432. size_t front = p_queue->p_cb->front;
  433. size_t back = p_queue->p_cb->back;
  434. return (front == back);
  435. }
  436. size_t nrf_queue_available_get(nrf_queue_t const * p_queue)
  437. {
  438. ASSERT(p_queue != NULL);
  439. return p_queue->size - nrf_queue_utilization_get(p_queue);
  440. }
  441. size_t nrf_queue_max_utilization_get(nrf_queue_t const * p_queue)
  442. {
  443. ASSERT(p_queue != NULL);
  444. return p_queue->p_cb->max_utilization;
  445. }
  446. void nrf_queue_max_utilization_reset(nrf_queue_t const * p_queue)
  447. {
  448. ASSERT(p_queue != NULL);
  449. p_queue->p_cb->max_utilization = 0;
  450. }
  451. #endif // NRF_MODULE_ENABLED(NRF_QUEUE)