ChibiOS 21.11.4
hal_mac.c
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.c
19 * @brief MAC Driver code.
20 *
21 * @addtogroup MAC
22 * @{
23 */
24
25#include "hal.h"
26
27#if (HAL_USE_MAC == TRUE) || defined(__DOXYGEN__)
28
29/*===========================================================================*/
30/* Driver local definitions. */
31/*===========================================================================*/
32
33#if (MAC_USE_ZERO_COPY == TRUE) && (MAC_SUPPORTS_ZERO_COPY == FALSE)
34#error "MAC_USE_ZERO_COPY not supported by this implementation"
35#endif
36
37/*===========================================================================*/
38/* Driver exported variables. */
39/*===========================================================================*/
40
41/*===========================================================================*/
42/* Driver local variables and types. */
43/*===========================================================================*/
44
45/*===========================================================================*/
46/* Driver local functions. */
47/*===========================================================================*/
48
49/*===========================================================================*/
50/* Driver interrupt handlers. */
51/*===========================================================================*/
52
53/*===========================================================================*/
54/* Driver exported functions. */
55/*===========================================================================*/
56
57/**
58 * @brief MAC Driver initialization.
59 * @note This function is implicitly invoked by @p halInit(), there is
60 * no need to explicitly initialize the driver.
61 *
62 * @init
63 */
64void macInit(void) {
65
67}
68
69/**
70 * @brief Initialize the standard part of a @p MACDriver structure.
71 *
72 * @param[out] macp pointer to the @p MACDriver object
73 *
74 * @init
75 */
77
78 macp->state = MAC_STOP;
79 macp->config = NULL;
80 macp->flags = (eventflags_t)0;
81 macp->cb = NULL;
82 macp->arg = NULL;
85#if MAC_USE_EVENTS == TRUE
86 osalEventObjectInit(&macp->es);
87#endif
88}
89
90/**
91 * @brief Configures and activates the MAC peripheral.
92 *
93 * @param[in] macp pointer to the @p MACDriver object
94 * @param[in] config pointer to the @p MACConfig object
95 * @return The operation status.
96 *
97 * @api
98 */
99msg_t macStart(MACDriver *macp, const MACConfig *config) {
100 msg_t msg;
101
102 osalDbgCheck((macp != NULL) && (config != NULL));
103
104 osalSysLock();
106 "invalid state");
107
108 macp->config = config;
109
110#if defined(MAC_LLD_ENHANCED_API)
111 msg = mac_lld_start(macp);
112 if (msg == HAL_RET_SUCCESS) {
113 macp->state = MAC_ACTIVE;
114 }
115 else {
116 macp->state = MAC_STOP;
117 }
118#else
119 mac_lld_start(macp);
120 macp->state = MAC_ACTIVE;
121 msg = HAL_RET_SUCCESS;
122#endif
123
125
126 return msg;
127}
128
129/**
130 * @brief Deactivates the MAC peripheral.
131 *
132 * @param[in] macp pointer to the @p MACDriver object
133 *
134 * @api
135 */
136void macStop(MACDriver *macp) {
137
138 osalDbgCheck(macp != NULL);
139
140 osalSysLock();
141
142 osalDbgAssert((macp->state == MAC_STOP) || (macp->state == MAC_ACTIVE),
143 "invalid state");
144
145 mac_lld_stop(macp);
146 macp->config = NULL;
147 macp->state = MAC_STOP;
148
150}
151
152/**
153 * @brief Get and clears MAC event flags.
154 *
155 * @param[in] macp pointer to the @p MACDriver object
156 * @return The pending event flags.
157 *
158 * @iclass
159 */
161 eventflags_t flags;
162
163 flags = macp->flags;
164 macp->flags = (eventflags_t)0;
165
166 return flags;
167}
168
169/**
170 * @brief Allocates a transmission descriptor.
171 * @details One of the available transmission descriptors is locked and
172 * returned. If a descriptor is not currently available then the
173 * invoking thread is queued until one is freed.
174 *
175 * @param[in] macp pointer to the @p MACDriver object
176 * @param[out] tdp pointer to a @p MACTransmitDescriptor structure
177 * @param[in] timeout the number of ticks before the operation timeouts,
178 * the following special values are allowed:
179 * - @a TIME_IMMEDIATE immediate timeout.
180 * - @a TIME_INFINITE no timeout.
181 * @return The operation status.
182 * @retval MSG_OK the descriptor was obtained.
183 * @retval MSG_TIMEOUT the operation timed out, descriptor not initialized.
184 *
185 * @api
186 */
189 sysinterval_t timeout) {
190 msg_t msg;
191
192 osalDbgCheck((macp != NULL) && (tdp != NULL));
193 osalDbgAssert(macp->state == MAC_ACTIVE, "not active");
194
195 osalSysLock();
196
197 while ((msg = macGetTransmitDescriptorX(macp, tdp)) != MSG_OK) {
198 msg = osalThreadEnqueueTimeoutS(&macp->tdqueue, timeout);
199 if (msg == MSG_TIMEOUT) {
200 break;
201 }
202 }
203
205
206 return msg;
207}
208
209/**
210 * @brief Waits for a received frame.
211 * @details Stops until a frame is received and buffered. If a frame is
212 * not immediately available then the invoking thread is queued
213 * until one is received.
214 *
215 * @param[in] macp pointer to the @p MACDriver object
216 * @param[out] rdp pointer to a @p MACReceiveDescriptor structure
217 * @param[in] timeout the number of ticks before the operation timeouts,
218 * the following special values are allowed:
219 * - @a TIME_IMMEDIATE immediate timeout.
220 * - @a TIME_INFINITE no timeout.
221 * @return The operation status.
222 * @retval MSG_OK the descriptor was obtained.
223 * @retval MSG_TIMEOUT the operation timed out, descriptor not initialized.
224 *
225 * @api
226 */
229 sysinterval_t timeout) {
230 msg_t msg;
231
232 osalDbgCheck((macp != NULL) && (rdp != NULL));
233 osalDbgAssert(macp->state == MAC_ACTIVE, "not active");
234
235 osalSysLock();
236
237 while (((msg = macGetReceiveDescriptorX(macp, rdp)) != MSG_OK)) {
238 msg = osalThreadEnqueueTimeoutS(&macp->rdqueue, timeout);
239 if (msg == MSG_TIMEOUT) {
240 break;
241 }
242 }
243
245
246 return msg;
247}
248
249/**
250 * @brief Updates and returns the link status.
251 *
252 * @param[in] macp pointer to the @p MACDriver object
253 * @return The link status.
254 * @retval true if the link is active.
255 * @retval false if the link is down.
256 *
257 * @api
258 */
260
261 osalDbgCheck(macp != NULL);
262 osalDbgAssert(macp->state == MAC_ACTIVE, "not active");
263
264 return mac_lld_poll_link_status(macp);
265}
266
267#endif /* HAL_USE_MAC == TRUE */
268
269/** @} */
#define HAL_RET_SUCCESS
Definition hal.h:93
void macStop(MACDriver *macp)
Deactivates the MAC peripheral.
Definition hal_mac.c:136
void macInit(void)
MAC Driver initialization.
Definition hal_mac.c:64
struct hal_mac_driver MACDriver
Type of a structure representing a MAC driver.
Definition hal_mac.h:75
void macObjectInit(MACDriver *macp)
Initialize the standard part of a MACDriver structure.
Definition hal_mac.c:76
void mac_lld_init(void)
Low level MAC initialization.
Definition hal_mac_lld.c:69
struct hal_mac_receive_descriptor MACReceiveDescriptor
Type of structure representing a MAC receive descriptor.
Definition hal_mac.h:90
struct hal_mac_transmit_descriptor MACTransmitDescriptor
Type of structure representing a MAC transmit descriptor.
Definition hal_mac.h:85
msg_t macWaitTransmitDescriptor(MACDriver *macp, MACTransmitDescriptor *tdp, sysinterval_t timeout)
Allocates a transmission descriptor.
Definition hal_mac.c:187
msg_t macStart(MACDriver *macp, const MACConfig *config)
Configures and activates the MAC peripheral.
Definition hal_mac.c:99
#define macGetTransmitDescriptorX(macp, tdp)
Returns a transmission descriptor.
Definition hal_mac.h:310
void mac_lld_stop(MACDriver *macp)
Deactivates the MAC peripheral.
eventflags_t macGetAndClearEventsI(MACDriver *macp)
Get and clears MAC event flags.
Definition hal_mac.c:160
void mac_lld_start(MACDriver *macp)
Configures and activates the MAC peripheral.
Definition hal_mac_lld.c:84
msg_t macWaitReceiveDescriptor(MACDriver *macp, MACReceiveDescriptor *rdp, sysinterval_t timeout)
Waits for a received frame.
Definition hal_mac.c:227
bool mac_lld_poll_link_status(MACDriver *macp)
Updates and returns the link status.
#define macGetReceiveDescriptorX(macp, rdp)
Returns a receive descriptor.
Definition hal_mac.h:335
bool macPollLinkStatus(MACDriver *macp)
Updates and returns the link status.
Definition hal_mac.c:259
struct hal_mac_config MACConfig
Type of structure representing a MAC configuration.
Definition hal_mac.h:80
@ MAC_ACTIVE
Definition hal_mac.h:105
@ MAC_STOP
Definition hal_mac.h:104
msg_t osalThreadEnqueueTimeoutS(threads_queue_t *tqp, sysinterval_t timeout)
Enqueues the caller thread.
Definition osal.c:273
static void osalSysLock(void)
Enters a critical zone from thread context.
Definition osal.h:601
static void osalSysUnlock(void)
Leaves a critical zone from thread context.
Definition osal.h:611
static void osalThreadQueueObjectInit(threads_queue_t *tqp)
Initializes a threads queue object.
Definition osal.h:725
static void osalEventObjectInit(event_source_t *esp)
Initializes an event source object.
Definition osal.h:737
#define osalDbgAssert(c, remark)
Condition assertion.
Definition osal.h:264
#define osalDbgCheck(c)
Function parameters check.
Definition osal.h:284
uint32_t eventflags_t
Definition chearly.h:91
int32_t msg_t
Definition chearly.h:88
#define MSG_OK
Normal wakeup message.
Definition chschd.h:39
#define MSG_TIMEOUT
Wakeup caused by a timeout condition.
Definition chschd.h:40
uint64_t sysinterval_t
Type of time interval.
Definition chtime.h:119
HAL subsystem header.
const MACConfig * config
Current configuration data.
Definition hal_mac.h:135
macstate_t state
Driver state.
Definition hal_mac.h:131
threads_queue_t rdqueue
Receive threads queue.
Definition hal_mac.h:143
event_source_t es
MAC events source.
Definition hal_mac.h:148
eventflags_t flags
Locally held event flags for callback use.
Definition hal_mac.h:153
maccb_t cb
Events callback.
Definition hal_mac.h:158
void * arg
User argument.
Definition hal_mac.h:163
threads_queue_t tdqueue
Transmit threads queue.
Definition hal_mac.h:139