hal_mac.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file hal_mac.h
  15. * @brief MAC Driver macros and structures.
  16. * @addtogroup MAC
  17. * @{
  18. */
  19. #ifndef HAL_MAC_H
  20. #define HAL_MAC_H
  21. #if (HAL_USE_MAC == TRUE) || defined(__DOXYGEN__)
  22. /*===========================================================================*/
  23. /* Driver constants. */
  24. /*===========================================================================*/
  25. /*===========================================================================*/
  26. /* Driver pre-compile time settings. */
  27. /*===========================================================================*/
  28. /**
  29. * @name MAC configuration options
  30. * @{
  31. */
  32. /**
  33. * @brief Enables an event sources for incoming packets.
  34. */
  35. #if !defined(MAC_USE_ZERO_COPY) || defined(__DOXYGEN__)
  36. #define MAC_USE_ZERO_COPY FALSE
  37. #endif
  38. /**
  39. * @brief Enables an event sources for incoming packets.
  40. */
  41. #if !defined(MAC_USE_EVENTS) || defined(__DOXYGEN__)
  42. #define MAC_USE_EVENTS TRUE
  43. #endif
  44. /** @} */
  45. /*===========================================================================*/
  46. /* Derived constants and error checks. */
  47. /*===========================================================================*/
  48. /*===========================================================================*/
  49. /* Driver data structures and types. */
  50. /*===========================================================================*/
  51. /**
  52. * @brief Driver state machine possible states.
  53. */
  54. typedef enum {
  55. MAC_UNINIT = 0, /**< Not initialized. */
  56. MAC_STOP = 1, /**< Stopped. */
  57. MAC_ACTIVE = 2 /**< Active. */
  58. } macstate_t;
  59. /**
  60. * @brief Type of a structure representing a MAC driver.
  61. */
  62. typedef struct MACDriver MACDriver;
  63. #include "hal_mac_lld.h"
  64. /*===========================================================================*/
  65. /* Driver macros. */
  66. /*===========================================================================*/
  67. /**
  68. * @name Macro Functions
  69. * @{
  70. */
  71. /**
  72. * @brief Enables the zero-copy API.
  73. *
  74. * @param[in] macp pointer to the @p MACDriver object
  75. * @return The pointer to the @p EventSource structure.
  76. *
  77. * @api
  78. */
  79. #if (MAC_USE_EVENTS == TRUE) || defined(__DOXYGEN__)
  80. #define macGetReceiveEventSource(macp) (&(macp)->rdevent)
  81. #endif
  82. /**
  83. * @brief Writes to a transmit descriptor's stream.
  84. *
  85. * @param[in] tdp pointer to a @p MACTransmitDescriptor structure
  86. * @param[in] buf pointer to the buffer containing the data to be written
  87. * @param[in] size number of bytes to be written
  88. * @return The number of bytes written into the descriptor's
  89. * stream, this value can be less than the amount
  90. * specified in the parameter @p size if the maximum frame
  91. * size is reached.
  92. *
  93. * @api
  94. */
  95. #define macWriteTransmitDescriptor(tdp, buf, size) \
  96. mac_lld_write_transmit_descriptor(tdp, buf, size)
  97. /**
  98. * @brief Reads from a receive descriptor's stream.
  99. *
  100. * @param[in] rdp pointer to a @p MACReceiveDescriptor structure
  101. * @param[in] buf pointer to the buffer that will receive the read data
  102. * @param[in] size number of bytes to be read
  103. * @return The number of bytes read from the descriptor's stream,
  104. * this value can be less than the amount specified in the
  105. * parameter @p size if there are no more bytes to read.
  106. *
  107. * @api
  108. */
  109. #define macReadReceiveDescriptor(rdp, buf, size) \
  110. mac_lld_read_receive_descriptor(rdp, buf, size)
  111. #if (MAC_USE_ZERO_COPY == TRUE) || defined(__DOXYGEN__)
  112. /**
  113. * @brief Returns a pointer to the next transmit buffer in the descriptor
  114. * chain.
  115. * @note The API guarantees that enough buffers can be requested to fill
  116. * a whole frame.
  117. *
  118. * @param[in] tdp pointer to a @p MACTransmitDescriptor structure
  119. * @param[in] size size of the requested buffer. Specify the frame size
  120. * on the first call then scale the value down subtracting
  121. * the amount of data already copied into the previous
  122. * buffers.
  123. * @param[out] sizep pointer to variable receiving the real buffer size.
  124. * The returned value can be less than the amount
  125. * requested, this means that more buffers must be
  126. * requested in order to fill the frame data entirely.
  127. * @return Pointer to the returned buffer.
  128. *
  129. * @api
  130. */
  131. #define macGetNextTransmitBuffer(tdp, size, sizep) \
  132. mac_lld_get_next_transmit_buffer(tdp, size, sizep)
  133. /**
  134. * @brief Returns a pointer to the next receive buffer in the descriptor
  135. * chain.
  136. * @note The API guarantees that the descriptor chain contains a whole
  137. * frame.
  138. *
  139. * @param[in] rdp pointer to a @p MACReceiveDescriptor structure
  140. * @param[out] sizep pointer to variable receiving the buffer size, it is
  141. * zero when the last buffer has already been returned.
  142. * @return Pointer to the returned buffer.
  143. * @retval NULL if the buffer chain has been entirely scanned.
  144. *
  145. * @api
  146. */
  147. #define macGetNextReceiveBuffer(rdp, sizep) \
  148. mac_lld_get_next_receive_buffer(rdp, sizep)
  149. #endif /* MAC_USE_ZERO_COPY */
  150. /** @} */
  151. /*===========================================================================*/
  152. /* External declarations. */
  153. /*===========================================================================*/
  154. #ifdef __cplusplus
  155. extern "C" {
  156. #endif
  157. void macInit(void);
  158. void macObjectInit(MACDriver *macp);
  159. void macStart(MACDriver *macp, const MACConfig *config);
  160. void macStop(MACDriver *macp);
  161. void macSetAddress(MACDriver *macp, const uint8_t *p);
  162. msg_t macWaitTransmitDescriptor(MACDriver *macp,
  163. MACTransmitDescriptor *tdp,
  164. sysinterval_t timeout);
  165. void macReleaseTransmitDescriptor(MACTransmitDescriptor *tdp);
  166. msg_t macWaitReceiveDescriptor(MACDriver *macp,
  167. MACReceiveDescriptor *rdp,
  168. sysinterval_t timeout);
  169. void macReleaseReceiveDescriptor(MACReceiveDescriptor *rdp);
  170. bool macPollLinkStatus(MACDriver *macp);
  171. #ifdef __cplusplus
  172. }
  173. #endif
  174. #endif /* HAL_USE_MAC == TRUE */
  175. #endif /* HAL_MAC_H */
  176. /** @} */