hpm_mbx_drv.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_MBX_DRV_H
  8. #define HPM_MBX_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_mbx_regs.h"
  11. /**
  12. * @brief MBX driver APIs
  13. * @defgroup mbx_interface MBX driver APIs
  14. * @ingroup io_interfaces
  15. * @{
  16. */
  17. /*
  18. * @brief Bus access responses
  19. */
  20. typedef enum {
  21. no_bus_error_no_wait = 0,
  22. generate_bus_error = 1,
  23. } mbx_bus_access_resp_t;
  24. /*
  25. * @brief MBX specific status
  26. */
  27. enum {
  28. status_mbx_not_available = MAKE_STATUS(status_group_mbx, 2),
  29. };
  30. #define MBX_CR_ALL_INTERRUPTS_MASK (MBX_CR_TFMAIE_MASK | MBX_CR_RFMAIE_MASK \
  31. | MBX_CR_RFMFIE_MASK | MBX_CR_TWMEIE_MASK)
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /**
  36. * @brief Set bus access response
  37. *
  38. * @param[in] ptr MBX base address
  39. * @param[in] resp response value type
  40. */
  41. static inline void mbx_set_bus_access_response(MBX_Type *ptr, mbx_bus_access_resp_t resp)
  42. {
  43. ptr->CR = (ptr->CR & ~(MBX_CR_BARCTL_MASK)) | MBX_CR_BARCTL_SET(resp);
  44. }
  45. /**
  46. * @brief Enable interrupt with mask
  47. *
  48. * @param[in] ptr MBX base address
  49. * @param[in] mask Mask of interrupts to be enabled
  50. */
  51. static inline void mbx_enable_intr(MBX_Type *ptr, uint32_t mask)
  52. {
  53. ptr->CR |= mask;
  54. }
  55. /**
  56. * @brief Disable interrupt with mask
  57. *
  58. * @param[in] ptr MBX base address
  59. * @param[in] mask Mask of interrupts to be disabled
  60. */
  61. static inline void mbx_disable_intr(MBX_Type *ptr, uint32_t mask)
  62. {
  63. ptr->CR &= ~mask;
  64. }
  65. /**
  66. * @brief Empty fifo
  67. *
  68. * @param[in] ptr MBX base address
  69. */
  70. static inline void mbx_empty_txfifo(MBX_Type *ptr)
  71. {
  72. ptr->CR |= MBX_CR_TXRESET_MASK;
  73. }
  74. /**
  75. * @brief Initialization
  76. *
  77. * @param[in] ptr MBX base address
  78. */
  79. static inline void mbx_init(MBX_Type *ptr)
  80. {
  81. mbx_empty_txfifo(ptr);
  82. mbx_disable_intr(ptr, MBX_CR_ALL_INTERRUPTS_MASK);
  83. }
  84. /**
  85. * @brief Send message
  86. *
  87. * @param[in] ptr MBX base address
  88. * @param[in] msg Message data in 32 bits
  89. *
  90. * @return status_success if everything is okay
  91. */
  92. static inline hpm_stat_t mbx_send_message(MBX_Type *ptr, uint32_t msg)
  93. {
  94. if (ptr->SR & MBX_SR_TWME_MASK) {
  95. ptr->TXREG = msg;
  96. return status_success;
  97. }
  98. return status_mbx_not_available;
  99. }
  100. /**
  101. * @brief Retrieve message
  102. *
  103. * @param[in] ptr MBX base address
  104. * @param[out] msg Pointer to buffer to save message data
  105. *
  106. * @return status_success if everything is okay
  107. */
  108. static inline hpm_stat_t mbx_retrieve_message(MBX_Type *ptr, uint32_t *msg)
  109. {
  110. if (ptr->SR & MBX_SR_RWMV_MASK) {
  111. *msg = ptr->RXREG;
  112. return status_success;
  113. }
  114. return status_mbx_not_available;
  115. }
  116. /**
  117. * @brief Send message to fifo
  118. *
  119. * @param[in] ptr MBX base address
  120. * @param[in] msg Pointer to message array to be sent
  121. * @param[in] count Number of 32-bit data to be sent
  122. *
  123. * @return status_success if everything is okay
  124. * @return status_not_available if fifo is full
  125. */
  126. static inline hpm_stat_t mbx_send_fifo(MBX_Type *ptr, uint32_t *msg, uint32_t count)
  127. {
  128. uint32_t i;
  129. hpm_stat_t status = status_success;
  130. for (i = 0; i < 4; i++) {
  131. if (ptr->SR & MBX_SR_TFMA_MASK) {
  132. ptr->TXWRD[0] = MBX_TXWRD_TXFIFO_SET(*(msg + i));
  133. count--;
  134. if (!count) {
  135. break;
  136. }
  137. } else {
  138. status = status_mbx_not_available;
  139. break;
  140. }
  141. }
  142. return status;
  143. }
  144. /**
  145. * @brief Retrieve data from fifo
  146. *
  147. * @param[in] ptr MBX base address
  148. * @param[out] msg Pointer of buffer to receive data
  149. * @param[in] count Number of 32-bit data to be retrieved
  150. *
  151. * @return status_success if everything is okay
  152. * @return status_mbx_not_available if fifo is empty
  153. */
  154. static inline hpm_stat_t mbx_retrieve_fifo(MBX_Type *ptr, uint32_t *msg, uint32_t count)
  155. {
  156. uint32_t i;
  157. hpm_stat_t status = status_success;
  158. for (i = 0; i < 4; i++) {
  159. if (ptr->SR & MBX_SR_RFMA_MASK) {
  160. *(msg + i) = (ptr->RXWRD[0] & MBX_RXWRD_RXFIFO_MASK) >> MBX_RXWRD_RXFIFO_SHIFT;
  161. count--;
  162. if (!count) {
  163. break;
  164. }
  165. } else {
  166. status = status_mbx_not_available;
  167. break;
  168. }
  169. }
  170. return status;
  171. }
  172. #ifdef __cplusplus
  173. }
  174. #endif
  175. /**
  176. * @}
  177. */
  178. #endif /* HPM_MBX_DRV_H */