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