ClearCore Library
EthernetManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Teknic, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
31 #ifndef __ETHERNETMANAGER_H__
32 #define __ETHERNETMANAGER_H__
33 
34 #include <cstring>
35 #include <sam.h>
36 #include "EthernetApi.h"
37 #include "HardwareMapping.h"
38 #include "IpAddress.h"
39 #include "Phy.h"
40 #include "SysUtils.h"
41 #include "lwip/dhcp.h"
42 #include "lwip/ip_addr.h"
43 #include "lwip/netif.h"
44 
45 namespace ClearCore {
46 
57  friend class SysManager;
58 
59 public:
60 #ifndef HIDE_FROM_DOXYGEN
61 
64  static EthernetManager &Instance();
65 
75  void Initialize();
76 
90  void PhyInitialize();
91 
105  uint32_t PhyShift(uint32_t phyOp, uint32_t phyReg, uint32_t contents);
106 
114  uint32_t PhyRead(uint32_t phyReg);
115 
124  uint32_t PhyWrite(uint32_t phyReg, uint32_t contents);
125 #endif
126 
136  volatile const bool &PhyLinkActive() {
137  return m_phyLinkUp;
138  }
139 
145  volatile const bool &PhyRemoteFault() {
146  return m_phyRemoteFault;
147  }
148 
149 #ifndef HIDE_FROM_DOXYGEN
150 
155  volatile const bool &PhyInitFailed() {
156  return m_phyInitFailed;
157  }
158 
164  void Enable(bool enable);
165 
171  bool ReceivedFrameFlag();
172 
176  void IrqHandlerPhy();
177 
183  void IrqHandlerGmac();
184 #endif
185 
191  uint8_t *MacAddress();
192 
198  IpAddress LocalIp();
199 
212  void LocalIp(IpAddress ipaddr);
213 
220 
227  void NetmaskIp(IpAddress address);
228 
235 
241  void GatewayIp(IpAddress address);
242 
248  IpAddress DnsIp();
249 
257  void DnsIp(IpAddress dns);
258 
259 #ifndef HIDE_FROM_DOXYGEN
260 
265  netif *MacInterface() {
266  return &m_macInterface;
267  }
268 #endif
269 
275  volatile const uint16_t &RetransmissionTimeout() {
276  return m_retransmissionTimeout;
277  }
278 
285  void RetransmissionTimeout(uint8_t timeout) {
286  m_retransmissionTimeout = timeout;
287  }
288 
294  volatile const uint8_t &RetransmissionCount() {
295  return m_retransmissionCount;
296  }
297 
306  void RetransmissionCount(uint8_t count) {
307  m_retransmissionCount = count;
308  }
309 
317  bool DhcpBegin();
318 
323  void Setup();
324 
334  void Refresh();
335 
341  volatile const bool &EthernetActive() {
342  return m_ethernetActive;
343  }
344 
345 private:
346  // GMAC port/pin combinations
347  uint32_t m_portPhyTxen;
348  uint32_t m_pinPhyTxen;
349  uint32_t m_portPhyTxd0;
350  uint32_t m_pinPhyTxd0;
351  uint32_t m_portPhyTxd1;
352  uint32_t m_pinPhyTxd1;
353  uint32_t m_portPhyRxd0;
354  uint32_t m_pinPhyRxd0;
355  uint32_t m_portPhyRxd1;
356  uint32_t m_pinPhyRxd1;
357  uint32_t m_portPhyRxer;
358  uint32_t m_pinPhyRxer;
359  uint32_t m_portPhyRxdv;
360  uint32_t m_pinPhyRxdv;
361  uint32_t m_portPhyMdio;
362  uint32_t m_pinPhyMdio;
363  uint32_t m_portPhyMdc;
364  uint32_t m_pinPhyMdc;
365  uint32_t m_portPhyTxclk;
366  uint32_t m_pinPhyTxclk;
367  uint32_t m_portPhyInt;
368  uint32_t m_pinPhyInt;
369 
370  uint32_t m_phyExtInt;
371  // PHY link up bit - updated via PHY interrupt
372  bool m_phyLinkUp;
373  // PHY remote fault bit -- updated via PHY interrupt
374  bool m_phyRemoteFault;
375  // PHY initialization failed status
376  bool m_phyInitFailed;
377 
378  // received a frame flag
379  bool m_recv;
380  // DHCP flag
381  bool m_dhcp;
382  // Ethernet setup complete flag
383  bool m_ethernetActive;
384 
385  // Receive Buffer Current Index
386  uint8_t m_rxBuffIndex;
387  // Transmit Buffer Current Index
388  uint16_t m_txBuffIndex;
389  // Receive Buffer Descriptor List
390  GMAC_RX_DESC m_rxDesc[RX_BUFF_CNT];
391  // Transmit Buffer Descriptor List
392  GMAC_TX_DESC m_txDesc[TX_BUFF_CNT];
393  // Receive Buffers
394  uint8_t m_rxBuffer[RX_BUFF_CNT][RX_BUFFER_SIZE];
395  // Transmit Buffers
396  uint8_t m_txBuffer[TX_BUFF_CNT][TX_BUFFER_SIZE];
397 
398  // Blocking retransmission timeout in milliseconds
399  uint16_t m_retransmissionTimeout;
400  // Number of transmission attempts before giving up
401  uint8_t m_retransmissionCount;
402 
403  // internal network interface
404  ethInt m_ethernetInterface;
405 
406  /* Built-in lwIP types.
407  */
408  // lwIP's network interface
409  struct netif m_macInterface;
410  // DHCP configuration
411  struct dhcp *m_dhcpData;
412 
417  void NetifInit();
418 
428  void ConfigureGpioPerGmac(uint32_t port, uint32_t pin);
429 
433  EthernetManager();
434 
435 }; // EthernetManager
436 
437 } // ClearCore namespace
438 
439 #endif // !__ETHERNETMANAGER_H__
bool DhcpBegin()
Set up DHCP connection to retrieve local IP.
IpAddress DnsIp()
Get the DNS IP address used for address resolution.
IpAddress GatewayIp()
Get the gateway IP address.
IpAddress LocalIp()
Get the local IP address.
An IP Address class.
Definition: IpAddress.h:41
volatile const bool & PhyRemoteFault()
Check the remote fault status from the PHY.
Definition: EthernetManager.h:145
ClearCore Ethernet configuration manager.
Definition: EthernetManager.h:56
volatile const uint16_t & RetransmissionTimeout()
Get the retransmission timeout.
Definition: EthernetManager.h:275
volatile const bool & PhyLinkActive()
Check the link status from the PHY.
Definition: EthernetManager.h:136
void RetransmissionTimeout(uint8_t timeout)
Set the retransmission timeout.
Definition: EthernetManager.h:285
volatile const uint8_t & RetransmissionCount()
Get the retransmission count.
Definition: EthernetManager.h:294
void Setup()
Setup LwIP with the local network interface.
Namespace to encompass the ClearCore board API.
Definition: AdcManager.h:36
uint8_t * MacAddress()
Get the MAC address.
volatile const bool & EthernetActive()
A flag to indicate whether Ethernet setup has been invoked.
Definition: EthernetManager.h:341
void RetransmissionCount(uint8_t count)
Set the retransmission count.
Definition: EthernetManager.h:306
IpAddress NetmaskIp()
Get the netmask IP.
void Refresh()
Perform any necessary periodic Ethernet and LwIP updates.
ClearCore Board Supervisory System Manager.
Definition: SysManager.h:58
ClearCore common utility functions.