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