ChibiOS/RT Architecture - Reference Manual - Guides |
00001 /* 00002 ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio. 00003 00004 This file is part of ChibiOS/RT. 00005 00006 ChibiOS/RT is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 ChibiOS/RT is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 00019 --- 00020 00021 A special exception to the GPL can be applied should you wish to distribute 00022 a combined work that includes ChibiOS/RT, without being obliged to provide 00023 the source code for any proprietary components. See the file exception.txt 00024 for full details of how and when the exception can be applied. 00025 */ 00026 00027 /** 00028 * @file mac.c 00029 * @brief MAC Driver code. 00030 * 00031 * @addtogroup MAC 00032 * @{ 00033 */ 00034 00035 #include "ch.h" 00036 #include "hal.h" 00037 00038 #if CH_HAL_USE_MAC || defined(__DOXYGEN__) 00039 00040 /*===========================================================================*/ 00041 /* Driver exported variables. */ 00042 /*===========================================================================*/ 00043 00044 /*===========================================================================*/ 00045 /* Driver local variables. */ 00046 /*===========================================================================*/ 00047 00048 /*===========================================================================*/ 00049 /* Driver local functions. */ 00050 /*===========================================================================*/ 00051 00052 /*===========================================================================*/ 00053 /* Driver interrupt handlers. */ 00054 /*===========================================================================*/ 00055 00056 /*===========================================================================*/ 00057 /* Driver exported functions. */ 00058 /*===========================================================================*/ 00059 00060 /** 00061 * @brief MAC Driver initialization. 00062 */ 00063 void macInit(void) { 00064 00065 mac_lld_init(); 00066 } 00067 00068 /** 00069 * @brief Initialize the standard part of a @p MACDriver structure. 00070 * 00071 * @param[in] macp pointer to the @p MACDriver object 00072 */ 00073 void macObjectInit(MACDriver *macp) { 00074 00075 chSemInit(&macp->md_tdsem, 0); 00076 chSemInit(&macp->md_rdsem, 0); 00077 #if CH_USE_EVENTS 00078 chEvtInit(&macp->md_rdevent); 00079 #endif 00080 } 00081 00082 /** 00083 * @brief MAC address setup. 00084 * @note This function must be invoked only with the driver in the stopped 00085 * state. If invoked on an active interface then it is ignored. 00086 * 00087 * @param[in] macp pointer to the @p MACDriver object 00088 * @param[in] p pointer to a six bytes buffer containing the MAC 00089 * address. If this parameter is set to @p NULL then MAC 00090 * a system default is used. 00091 * 00092 */ 00093 void macSetAddress(MACDriver *macp, const uint8_t *p) { 00094 00095 mac_lld_set_address(macp, p); 00096 } 00097 00098 /** 00099 * @brief Allocates a transmission descriptor. 00100 * @details One of the available transmission descriptors is locked and 00101 * returned. If a descriptor is not currently available then the 00102 * invoking thread is queued until one is freed. 00103 * 00104 * @param[in] macp pointer to the @p MACDriver object 00105 * @param[out] tdp pointer to a @p MACTransmitDescriptor structure 00106 * @param[in] time the number of ticks before the operation timeouts, 00107 * the following special values are allowed: 00108 * - @a TIME_IMMEDIATE immediate timeout. 00109 * - @a TIME_INFINITE no timeout. 00110 * . 00111 * @return The operation status. 00112 * @retval RDY_OK the descriptor was obtained. 00113 * @retval RDY_TIMEOUT the operation timed out, descriptor not initialized. 00114 */ 00115 msg_t macWaitTransmitDescriptor(MACDriver *macp, 00116 MACTransmitDescriptor *tdp, 00117 systime_t time) { 00118 msg_t msg; 00119 00120 while (((msg = max_lld_get_transmit_descriptor(macp, tdp)) != RDY_OK) && 00121 (time > 0)) { 00122 chSysLock(); 00123 systime_t now = chTimeNow(); 00124 if ((msg = chSemWaitTimeoutS(&macp->md_tdsem, time)) == RDY_TIMEOUT) 00125 break; 00126 if (time != TIME_INFINITE) 00127 time -= (chTimeNow() - now); 00128 chSysUnlock(); 00129 } 00130 return msg; 00131 } 00132 00133 /** 00134 * @brief Releases a transmit descriptor and starts the transmission of the 00135 * enqueued data as a single frame. 00136 * 00137 * @param[in] tdp the pointer to the @p MACTransmitDescriptor structure 00138 */ 00139 void macReleaseTransmitDescriptor(MACTransmitDescriptor *tdp) { 00140 00141 mac_lld_release_transmit_descriptor(tdp); 00142 } 00143 00144 /** 00145 * @brief Waits for a received frame. 00146 * @details Stops until a frame is received and buffered. If a frame is 00147 * not immediately available then the invoking thread is queued 00148 * until one is received. 00149 * 00150 * @param[in] macp pointer to the @p MACDriver object 00151 * @param[out] rdp pointer to a @p MACReceiveDescriptor structure 00152 * @param[in] time the number of ticks before the operation timeouts, 00153 * the following special values are allowed: 00154 * - @a TIME_IMMEDIATE immediate timeout. 00155 * - @a TIME_INFINITE no timeout. 00156 * . 00157 * @return The operation status. 00158 * @retval RDY_OK the descriptor was obtained. 00159 * @retval RDY_TIMEOUT the operation timed out, descriptor not initialized. 00160 */ 00161 msg_t macWaitReceiveDescriptor(MACDriver *macp, 00162 MACReceiveDescriptor *rdp, 00163 systime_t time) { 00164 msg_t msg; 00165 00166 while (((msg = max_lld_get_receive_descriptor(macp, rdp)) != RDY_OK) && 00167 (time > 0)) { 00168 chSysLock(); 00169 systime_t now = chTimeNow(); 00170 if ((msg = chSemWaitTimeoutS(&macp->md_rdsem, time)) == RDY_TIMEOUT) 00171 break; 00172 if (time != TIME_INFINITE) 00173 time -= (chTimeNow() - now); 00174 chSysUnlock(); 00175 } 00176 return msg; 00177 } 00178 00179 /** 00180 * @brief Releases a receive descriptor. 00181 * @details The descriptor and its buffer are made available for more incoming 00182 * frames. 00183 * 00184 * @param[in] rdp the pointer to the @p MACReceiveDescriptor structure 00185 */ 00186 void macReleaseReceiveDescriptor(MACReceiveDescriptor *rdp) { 00187 00188 mac_lld_release_receive_descriptor(rdp); 00189 } 00190 00191 /** 00192 * @brief Updates and returns the link status. 00193 * 00194 * @param[in] macp pointer to the @p MACDriver object 00195 * @return The link status. 00196 * @retval TRUE if the link is active. 00197 * @retval FALSE if the link is down. 00198 */ 00199 bool_t macPollLinkStatus(MACDriver *macp) { 00200 00201 return mac_lld_poll_link_status(macp); 00202 } 00203 00204 #endif /* CH_HAL_USE_MAC */ 00205 00206 /** @} */