Ethernet.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 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. #ifndef MBED_ETHERNET_H
  17. #define MBED_ETHERNET_H
  18. #include "platform/platform.h"
  19. #include "platform/NonCopyable.h"
  20. #if defined (DEVICE_ETHERNET) || defined(DOXYGEN_ONLY)
  21. namespace mbed {
  22. /** \addtogroup drivers */
  23. /** An ethernet interface, to use with the ethernet pins.
  24. *
  25. * @note Synchronization level: Not protected
  26. *
  27. * Example:
  28. * @code
  29. * // Read destination and source from every ethernet packet
  30. *
  31. * #include "mbed.h"
  32. *
  33. * Ethernet eth;
  34. *
  35. * int main() {
  36. * char buf[0x600];
  37. *
  38. * while(1) {
  39. * int size = eth.receive();
  40. * if(size > 0) {
  41. * eth.read(buf, size);
  42. * printf("Destination: %02X:%02X:%02X:%02X:%02X:%02X\n",
  43. * buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
  44. * printf("Source: %02X:%02X:%02X:%02X:%02X:%02X\n",
  45. * buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
  46. * }
  47. *
  48. * wait(1);
  49. * }
  50. * }
  51. * @endcode
  52. * @ingroup drivers
  53. */
  54. class Ethernet : private NonCopyable<Ethernet> {
  55. public:
  56. /** Initialise the ethernet interface.
  57. */
  58. Ethernet();
  59. /** Powers the hardware down.
  60. */
  61. virtual ~Ethernet();
  62. enum Mode {
  63. AutoNegotiate,
  64. HalfDuplex10,
  65. FullDuplex10,
  66. HalfDuplex100,
  67. FullDuplex100
  68. };
  69. /** Writes into an outgoing ethernet packet.
  70. *
  71. * It will append size bytes of data to the previously written bytes.
  72. *
  73. * @param data An array to write.
  74. * @param size The size of data.
  75. *
  76. * @returns
  77. * The number of written bytes.
  78. */
  79. int write(const char *data, int size);
  80. /** Send an outgoing ethernet packet.
  81. *
  82. * After filling in the data in an ethernet packet it must be send.
  83. * Send will provide a new packet to write to.
  84. *
  85. * @returns
  86. * 0 if the sending was failed,
  87. * or the size of the packet successfully sent.
  88. */
  89. int send();
  90. /** Receives an arrived ethernet packet.
  91. *
  92. * Receiving an ethernet packet will drop the last received ethernet packet
  93. * and make a new ethernet packet ready to read.
  94. * If no ethernet packet is arrived it will return 0.
  95. *
  96. * @returns
  97. * 0 if no ethernet packet is arrived,
  98. * or the size of the arrived packet.
  99. */
  100. int receive();
  101. /** Read from an received ethernet packet.
  102. *
  103. * After receive returned a number bigger than 0 it is
  104. * possible to read bytes from this packet.
  105. *
  106. * @param data Pointer to data packet
  107. * @param size Size of data to be read.
  108. * @returns The number of byte read.
  109. *
  110. * @note It is possible to use read multiple times.
  111. * Each time read will start reading after the last read byte before.
  112. *
  113. */
  114. int read(char *data, int size);
  115. /** Gives the ethernet address of the mbed.
  116. *
  117. * @param mac Must be a pointer to a 6 byte char array to copy the ethernet address in.
  118. */
  119. void address(char *mac);
  120. /** Returns if an ethernet link is present or not. It takes a while after Ethernet initialization to show up.
  121. *
  122. * @returns
  123. * 0 if no ethernet link is present,
  124. * 1 if an ethernet link is present.
  125. *
  126. * Example:
  127. * @code
  128. * // Using the Ethernet link function
  129. * #include "mbed.h"
  130. *
  131. * Ethernet eth;
  132. *
  133. * int main() {
  134. * wait(1); // Needed after startup.
  135. * if (eth.link()) {
  136. * printf("online\n");
  137. * } else {
  138. * printf("offline\n");
  139. * }
  140. * }
  141. * @endcode
  142. */
  143. int link();
  144. /** Sets the speed and duplex parameters of an ethernet link
  145. *
  146. * - AutoNegotiate Auto negotiate speed and duplex
  147. * - HalfDuplex10 10 Mbit, half duplex
  148. * - FullDuplex10 10 Mbit, full duplex
  149. * - HalfDuplex100 100 Mbit, half duplex
  150. * - FullDuplex100 100 Mbit, full duplex
  151. *
  152. * @param mode the speed and duplex mode to set the link to:
  153. */
  154. void set_link(Mode mode);
  155. };
  156. } // namespace mbed
  157. #endif
  158. #endif