|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2018 ARM Limited |
| 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 | +#include <ctype.h> |
| 18 | +#include <stdio.h> |
| 19 | +#include <string.h> |
| 20 | +#include <stdlib.h> |
| 21 | + |
| 22 | +#include "cmsis_os.h" |
| 23 | +#include "fvp_emac.h" |
| 24 | +#include "mbed_interface.h" |
| 25 | +#include "mbed_assert.h" |
| 26 | +#include "netsocket/nsapi_types.h" |
| 27 | +#include "mbed_shared_queues.h" |
| 28 | + |
| 29 | + |
| 30 | +/******************************************************************************** |
| 31 | + * Internal data |
| 32 | + ********************************************************************************/ |
| 33 | + |
| 34 | +#define THREAD_STACKSIZE 512 |
| 35 | + |
| 36 | +/* Flags for worker thread */ |
| 37 | +#define FLAG_TX (0x1u << 0) |
| 38 | +#define FLAG_RX (0x1u << 1) |
| 39 | + |
| 40 | +/** \brief Driver thread priority */ |
| 41 | +#define THREAD_PRIORITY (osPriorityNormal) |
| 42 | + |
| 43 | +#define PHY_TASK_PERIOD_MS 200 |
| 44 | + |
| 45 | + |
| 46 | +fvp_EMAC::fvp_EMAC() : _thread(THREAD_PRIORITY, THREAD_STACKSIZE, NULL, "fvp_emac_thread") |
| 47 | + |
| 48 | +{ |
| 49 | +} |
| 50 | + |
| 51 | +void fvp_EMAC::ethernet_callback(lan91_event_t event, void *param) |
| 52 | +{ |
| 53 | + fvp_EMAC *enet = static_cast<fvp_EMAC *>(param); |
| 54 | + switch (event) { |
| 55 | + case LAN91_RxEvent: |
| 56 | + enet->rx_isr(); |
| 57 | + break; |
| 58 | + case LAN91_TxEvent: |
| 59 | + enet->tx_isr(); |
| 60 | + break; |
| 61 | + default: |
| 62 | + break; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/** \brief Ethernet receive interrupt handler */ |
| 67 | +void fvp_EMAC::rx_isr() |
| 68 | +{ |
| 69 | + _thread.flags_set(FLAG_RX); |
| 70 | +} |
| 71 | + |
| 72 | +/** \brief Ethernet transmit interrupt handler */ |
| 73 | +void fvp_EMAC::tx_isr() |
| 74 | +{ |
| 75 | + _thread.flags_set(FLAG_TX); |
| 76 | +} |
| 77 | + |
| 78 | +/** \brief Low level init of the MAC and PHY. */ |
| 79 | +bool fvp_EMAC::low_level_init_successful() |
| 80 | +{ |
| 81 | + LAN91_init(); |
| 82 | + LAN91_SetCallback(&fvp_EMAC::ethernet_callback, this); |
| 83 | + return true; |
| 84 | +} |
| 85 | + |
| 86 | +/** \brief Worker thread. |
| 87 | + * |
| 88 | + * Woken by thread flags to receive packets or clean up transmit |
| 89 | + * |
| 90 | + * \param[in] pvParameters pointer to the interface data |
| 91 | + */ |
| 92 | +void fvp_EMAC::thread_function(void *pvParameters) |
| 93 | +{ |
| 94 | + struct fvp_EMAC *fvp_enet = static_cast<fvp_EMAC *>(pvParameters); |
| 95 | + |
| 96 | + for (;;) { |
| 97 | + uint32_t flags = ThisThread::flags_wait_any(FLAG_RX | FLAG_TX); |
| 98 | + if (flags & FLAG_RX) { |
| 99 | + fvp_enet->packet_rx(); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +/** \brief Packet reception task |
| 106 | + * |
| 107 | + * This task is called when a packet is received. It will |
| 108 | + * pass the packet to the LWIP core. |
| 109 | + */ |
| 110 | +void fvp_EMAC::packet_rx() |
| 111 | +{ |
| 112 | + while (!LAN91_RxFIFOEmpty()) { |
| 113 | + emac_mem_buf_t *temp_rxbuf = NULL; |
| 114 | + uint32_t *rx_payload_ptr; |
| 115 | + uint32_t rx_length = 0; |
| 116 | + |
| 117 | + temp_rxbuf = _memory_manager->alloc_heap(FVP_ETH_MAX_FLEN, LAN91_BUFF_ALIGNMENT); |
| 118 | + |
| 119 | + /* no memory been allocated*/ |
| 120 | + if (NULL != temp_rxbuf) { |
| 121 | + |
| 122 | + rx_payload_ptr = (uint32_t *)_memory_manager->get_ptr(temp_rxbuf); |
| 123 | + rx_length = _memory_manager->get_len(temp_rxbuf); |
| 124 | + bool state; |
| 125 | + |
| 126 | +#ifdef LOCK_RX_THREAD |
| 127 | + /* Get exclusive access */ |
| 128 | + _TXLockMutex.lock(); |
| 129 | +#endif |
| 130 | + state = LAN91_receive_frame(rx_payload_ptr, &rx_length); |
| 131 | + |
| 132 | +#ifdef LOCK_RX_THREAD |
| 133 | + _TXLockMutex.unlock(); |
| 134 | +#endif |
| 135 | + if (!state) { |
| 136 | + _memory_manager->free(temp_rxbuf); |
| 137 | + continue; |
| 138 | + } else { |
| 139 | + _memory_manager->set_len(temp_rxbuf, rx_length); |
| 140 | + } |
| 141 | + _emac_link_input_cb(temp_rxbuf); |
| 142 | + } |
| 143 | + } |
| 144 | + LAN91_SetInterruptMasks(MSK_RCV); |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | +/** \brief Low level output of a packet. Never call this from an |
| 149 | + * interrupt context, as it may block until TX descriptors |
| 150 | + * become available. |
| 151 | + * |
| 152 | + * \param[in] buf the MAC packet to send (e.g. IP packet including MAC addresses and type) |
| 153 | + * \return ERR_OK if the packet could be sent or an err_t value if the packet couldn't be sent |
| 154 | + */ |
| 155 | +bool fvp_EMAC::link_out(emac_mem_buf_t *buf) |
| 156 | +{ |
| 157 | + // If buffer is chained or not aligned then make a contiguous aligned copy of it |
| 158 | + if (_memory_manager->get_next(buf) || |
| 159 | + reinterpret_cast<uint32_t>(_memory_manager->get_ptr(buf)) % LAN91_BUFF_ALIGNMENT) { |
| 160 | + emac_mem_buf_t *copy_buf; |
| 161 | + copy_buf = _memory_manager->alloc_heap(_memory_manager->get_total_len(buf), LAN91_BUFF_ALIGNMENT); |
| 162 | + if (NULL == copy_buf) { |
| 163 | + _memory_manager->free(buf); |
| 164 | + return false; |
| 165 | + } |
| 166 | + |
| 167 | + // Copy to new buffer and free original |
| 168 | + _memory_manager->copy(copy_buf, buf); |
| 169 | + _memory_manager->free(buf); |
| 170 | + buf = copy_buf; |
| 171 | + } |
| 172 | + |
| 173 | + /* Save the buffer so that it can be freed when transmit is done */ |
| 174 | + uint32_t *buffer; |
| 175 | + uint32_t tx_length = 0; |
| 176 | + bool state; |
| 177 | + buffer = (uint32_t *)(_memory_manager->get_ptr(buf)); |
| 178 | + tx_length = _memory_manager->get_len(buf); |
| 179 | + |
| 180 | + /* Get exclusive access */ |
| 181 | + _TXLockMutex.lock(); |
| 182 | + |
| 183 | + /* Setup transfers */ |
| 184 | + state = LAN91_send_frame(buffer, &tx_length); |
| 185 | + _TXLockMutex.unlock(); |
| 186 | + /* Restore access */ |
| 187 | + |
| 188 | + |
| 189 | + if (!state) { |
| 190 | + return false; |
| 191 | + } |
| 192 | + /* Free the buffer */ |
| 193 | + _memory_manager->free(buf); |
| 194 | + |
| 195 | + return true; |
| 196 | +} |
| 197 | + |
| 198 | +/** \brief PHY task monitoring the link */ |
| 199 | +void fvp_EMAC::phy_task() |
| 200 | +{ |
| 201 | + // Get current status |
| 202 | + lan91_phy_status_t connection_status; |
| 203 | + connection_status = LAN91_GetLinkStatus(); |
| 204 | + |
| 205 | + if (connection_status != _prev_state && _emac_link_state_cb) { |
| 206 | + _emac_link_state_cb(connection_status); |
| 207 | + } |
| 208 | + _prev_state = connection_status; |
| 209 | +} |
| 210 | + |
| 211 | +bool fvp_EMAC::power_up() |
| 212 | +{ |
| 213 | + /* Initialize the hardware */ |
| 214 | + if (!low_level_init_successful()) { |
| 215 | + return false; |
| 216 | + } |
| 217 | + |
| 218 | + /* Start ethernet Worker thread */ |
| 219 | + _thread.start(callback(&fvp_EMAC::thread_function, this)); |
| 220 | + |
| 221 | + /* Trigger thread to deal with any RX packets that arrived before thread was started */ |
| 222 | + rx_isr(); |
| 223 | + |
| 224 | + /* PHY monitoring task */ |
| 225 | + _prev_state = STATE_LINK_DOWN; |
| 226 | + |
| 227 | + mbed::mbed_event_queue()->call(mbed::callback(this, &fvp_EMAC::phy_task)); |
| 228 | + |
| 229 | + /* Allow the PHY task to detect the initial link state and set up the proper flags */ |
| 230 | + ThisThread::sleep_for(10); |
| 231 | + |
| 232 | + _phy_task_handle = mbed::mbed_event_queue()->call_every(PHY_TASK_PERIOD_MS, mbed::callback(this, &fvp_EMAC::phy_task)); |
| 233 | + |
| 234 | + return true; |
| 235 | +} |
| 236 | + |
| 237 | +uint32_t fvp_EMAC::get_mtu_size() const |
| 238 | +{ |
| 239 | + return LAN91_ETH_MTU_SIZE; |
| 240 | +} |
| 241 | + |
| 242 | +uint32_t fvp_EMAC::get_align_preference() const |
| 243 | +{ |
| 244 | + return LAN91_BUFF_ALIGNMENT; |
| 245 | +} |
| 246 | + |
| 247 | +void fvp_EMAC::get_ifname(char *name, uint8_t size) const |
| 248 | +{ |
| 249 | + memcpy(name, FVP_ETH_IF_NAME, (size < sizeof(FVP_ETH_IF_NAME)) ? size : sizeof(FVP_ETH_IF_NAME)); |
| 250 | +} |
| 251 | + |
| 252 | +uint8_t fvp_EMAC::get_hwaddr_size() const |
| 253 | +{ |
| 254 | + return FVP_HWADDR_SIZE; |
| 255 | +} |
| 256 | + |
| 257 | +bool fvp_EMAC::get_hwaddr(uint8_t *addr) const |
| 258 | +{ |
| 259 | + read_MACaddr(addr); |
| 260 | + return true; |
| 261 | +} |
| 262 | + |
| 263 | +void fvp_EMAC::set_hwaddr(const uint8_t *addr) |
| 264 | +{ |
| 265 | + /* No-op at this stage */ |
| 266 | +} |
| 267 | + |
| 268 | +void fvp_EMAC::set_link_input_cb(emac_link_input_cb_t input_cb) |
| 269 | +{ |
| 270 | + _emac_link_input_cb = input_cb; |
| 271 | +} |
| 272 | + |
| 273 | +void fvp_EMAC::set_link_state_cb(emac_link_state_change_cb_t state_cb) |
| 274 | +{ |
| 275 | + _emac_link_state_cb = state_cb; |
| 276 | +} |
| 277 | + |
| 278 | +void fvp_EMAC::add_multicast_group(const uint8_t *addr) |
| 279 | +{ |
| 280 | + /* No-op at this stage */ |
| 281 | +} |
| 282 | + |
| 283 | +void fvp_EMAC::remove_multicast_group(const uint8_t *addr) |
| 284 | +{ |
| 285 | + /* No-op at this stage */ |
| 286 | +} |
| 287 | + |
| 288 | +void fvp_EMAC::set_all_multicast(bool all) |
| 289 | +{ |
| 290 | + /* No-op at this stage */ |
| 291 | +} |
| 292 | + |
| 293 | +void fvp_EMAC::power_down() |
| 294 | +{ |
| 295 | + /* No-op at this stage */ |
| 296 | +} |
| 297 | + |
| 298 | +void fvp_EMAC::set_memory_manager(EMACMemoryManager &mem_mngr) |
| 299 | +{ |
| 300 | + _memory_manager = &mem_mngr; |
| 301 | +} |
| 302 | + |
| 303 | + |
| 304 | +fvp_EMAC &fvp_EMAC::get_instance() |
| 305 | +{ |
| 306 | + static fvp_EMAC emac; |
| 307 | + return emac; |
| 308 | +} |
| 309 | + |
| 310 | +// Weak so a module can override |
| 311 | +MBED_WEAK EMAC &EMAC::get_default_instance() |
| 312 | +{ |
| 313 | + return fvp_EMAC::get_instance(); |
| 314 | +} |
| 315 | + |
| 316 | +/** @} */ |
| 317 | + |
| 318 | +/* --------------------------------- End Of File ------------------------------ */ |
0 commit comments