pm_buffer.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "pm_buffer.h"
  13. #include <stdbool.h>
  14. #include <string.h>
  15. #include "nrf_error.h"
  16. #include "pm_mutex.h"
  17. #define BUFFER_IS_VALID(p_buffer) ((p_buffer != NULL) \
  18. && (p_buffer->p_memory != NULL) \
  19. && (p_buffer->p_mutex != NULL))
  20. ret_code_t pm_buffer_init(pm_buffer_t * p_buffer,
  21. uint8_t * p_buffer_memory,
  22. uint32_t buffer_memory_size,
  23. uint8_t * p_mutex_memory,
  24. uint32_t mutex_memory_size,
  25. uint32_t n_blocks,
  26. uint32_t block_size)
  27. {
  28. if ( (p_buffer != NULL)
  29. && (p_buffer_memory != NULL)
  30. && (p_mutex_memory != NULL)
  31. && (buffer_memory_size >= (n_blocks*block_size))
  32. && (mutex_memory_size >= MUTEX_STORAGE_SIZE(n_blocks))
  33. && (n_blocks != 0)
  34. && (block_size != 0))
  35. {
  36. p_buffer->p_memory = p_buffer_memory;
  37. p_buffer->p_mutex = p_mutex_memory;
  38. p_buffer->n_blocks = n_blocks;
  39. p_buffer->block_size = block_size;
  40. pm_mutex_init(p_buffer->p_mutex, n_blocks);
  41. return NRF_SUCCESS;
  42. }
  43. else
  44. {
  45. return NRF_ERROR_INVALID_PARAM;
  46. }
  47. }
  48. uint8_t pm_buffer_block_acquire(pm_buffer_t * p_buffer, uint32_t n_blocks)
  49. {
  50. if (!BUFFER_IS_VALID(p_buffer))
  51. {
  52. return ( BUFFER_INVALID_ID );
  53. }
  54. uint8_t first_locked_mutex = BUFFER_INVALID_ID;
  55. for (uint8_t i = 0; i < p_buffer->n_blocks; i++)
  56. {
  57. if (pm_mutex_lock(p_buffer->p_mutex, i))
  58. {
  59. if (first_locked_mutex == BUFFER_INVALID_ID)
  60. {
  61. first_locked_mutex = i;
  62. }
  63. if ((i - first_locked_mutex + 1) == n_blocks)
  64. {
  65. return first_locked_mutex;
  66. }
  67. }
  68. else if (first_locked_mutex != BUFFER_INVALID_ID)
  69. {
  70. for (uint8_t j = first_locked_mutex; j < i; j++)
  71. {
  72. pm_buffer_release(p_buffer, j);
  73. }
  74. first_locked_mutex = BUFFER_INVALID_ID;
  75. }
  76. }
  77. return ( BUFFER_INVALID_ID );
  78. }
  79. uint8_t * pm_buffer_ptr_get(pm_buffer_t * p_buffer, uint8_t id)
  80. {
  81. if (!BUFFER_IS_VALID(p_buffer))
  82. {
  83. return ( NULL );
  84. }
  85. if ( (id != BUFFER_INVALID_ID)
  86. && pm_mutex_lock_status_get(p_buffer->p_mutex, id) )
  87. {
  88. return ( &p_buffer->p_memory[id*p_buffer->block_size] );
  89. }
  90. else
  91. {
  92. return ( NULL );
  93. }
  94. }
  95. void pm_buffer_release(pm_buffer_t * p_buffer, uint8_t id)
  96. {
  97. if ( BUFFER_IS_VALID(p_buffer)
  98. && (id != BUFFER_INVALID_ID)
  99. && pm_mutex_lock_status_get(p_buffer->p_mutex, id))
  100. {
  101. pm_mutex_unlock(p_buffer->p_mutex, id);
  102. }
  103. }