PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

286 lines
9.6KB

  1. #include "fnet_user_config.h"
  2. #include "fnet.h"
  3. #include "fnet_config.h"
  4. #include "fnet_usb.h"
  5. #include "stack/fnet_eth_prv.h"
  6. //#if !defined(ARDUINO_TEENSY41)
  7. /************************************************************************
  8. * NAME: inits
  9. *
  10. * DESCRIPTION: Ethernet Physical Transceiver initialization and/or reset.
  11. *************************************************************************/
  12. fnet_return_t fnet_usb_init(fnet_netif_t *netif){
  13. (void) netif;
  14. return FNET_OK;
  15. }
  16. void fnet_usb_release(fnet_netif_t *netif) {
  17. (void) netif;
  18. }
  19. /************************************************************************
  20. * NAME: fnet_usb_get_hw_addr
  21. *
  22. * DESCRIPTION: This function reads MAC address.
  23. *************************************************************************/
  24. fnet_return_t fnet_usb_get_hw_addr(fnet_netif_t *netif, uint8_t * hw_addr){
  25. fnet_return_t result;
  26. if(netif && (netif->netif_api->netif_type == FNET_NETIF_TYPE_ETHERNET)
  27. && (hw_addr) )
  28. {
  29. // Get MAC adress here
  30. if(_handleGetMACAddress != NULL){
  31. _handleGetMACAddress((fnet_mac_addr_t *)hw_addr);
  32. }
  33. result = FNET_OK;
  34. }
  35. else
  36. {
  37. result = FNET_ERR;
  38. }
  39. return result;
  40. }
  41. fnet_return_t fnet_usb_set_hw_addr(fnet_netif_t *netif, uint8_t * hw_addr){
  42. fnet_return_t result;
  43. /* Set the source address for the controller. */
  44. if(netif
  45. && (netif->netif_api->netif_type == FNET_NETIF_TYPE_ETHERNET)
  46. && hw_addr
  47. && (fnet_memcmp(hw_addr, fnet_eth_null_addr, sizeof(fnet_mac_addr_t)) != 0)
  48. && (fnet_memcmp(hw_addr, fnet_eth_broadcast, sizeof(fnet_mac_addr_t)) != 0)
  49. && ((hw_addr[0] & 0x01U) == 0x00U)) /* Most significant nibble should be always even.*/
  50. {
  51. // Set MAC adress here
  52. if(_handleSetMACAddress != NULL){
  53. _handleSetMACAddress(hw_addr);
  54. }
  55. _fnet_eth_change_addr_notify(netif);
  56. result = FNET_OK;
  57. }
  58. else
  59. {
  60. result = FNET_ERR;
  61. }
  62. return result;
  63. }
  64. fnet_return_t fnet_usb_get_statistics(struct fnet_netif *netif, struct fnet_netif_statistics * statistics){ //Number of rx and tx packets
  65. (void) netif;
  66. (void) statistics;
  67. return FNET_OK;
  68. }
  69. fnet_bool_t fnet_usb_is_connected(fnet_netif_t *netif){
  70. if (_handleIsConnected != NULL) {
  71. return _handleIsConnected();
  72. }
  73. return false;
  74. }
  75. /************************************************************************
  76. * NAME: fnet_usb_output
  77. *
  78. * DESCRIPTION: Ethernet low-level output function.
  79. *************************************************************************/
  80. void fnet_usb_eth_output(fnet_netif_t *netif, fnet_netbuf_t *nb){
  81. // fnet_uint8_t *tx_buffer;
  82. //
  83. // if(nb && (nb->total_length >= FNET_ETH_HDR_SIZE))
  84. // {
  85. // _fnet_netbuf_to_buf(nb, 0u, FNET_NETBUF_COPYALL, tx_buffer);
  86. // }
  87. if(_handleOutput != NULL){
  88. _handleOutput(netif, nb);
  89. }
  90. _fnet_netbuf_free_chain(nb);
  91. }
  92. void fnet_usb_stop(fnet_netif_t *netif) {
  93. (void) netif;
  94. }
  95. void fnet_usb_resume(fnet_netif_t *netif) {
  96. (void) netif;
  97. }
  98. /************************************************************************
  99. * DESCRIPTION: Read a value from a PHY's MII register.
  100. * reg_addr < address of the register in the PHY
  101. * data < Pointer to storage for the Data to be read from the PHY register (passed by reference)
  102. * Return FNET_ERR on failure, FNET_OK on success
  103. *************************************************************************/
  104. fnet_return_t _fnet_usb_phy_read(fnet_netif_t *netif, fnet_uint32_t reg_addr, fnet_uint16_t *data) {
  105. // fnet_return_t result;
  106. // fnet_eth_if_t *eth_if = (fnet_eth_if_t *)(netif->netif_prv);
  107. //Read phy into data pointer
  108. if(_handlePHYRead != NULL){
  109. _handlePHYRead(reg_addr, data);
  110. }
  111. return FNET_OK; //Or FNET_OK
  112. }
  113. /************************************************************************
  114. * DESCRIPTION: Write a value to a PHY's MII register.
  115. * reg_addr = address of the register in the PHY
  116. * data = Data to be writen to the PHY register (passed by reference)
  117. * Return FNET_ERR on failure (timeout), FNET_OK on success
  118. *************************************************************************/
  119. fnet_return_t _fnet_usb_phy_write(fnet_netif_t *netif, fnet_uint32_t reg_addr, fnet_uint16_t data) {
  120. // fnet_return_t result;
  121. // fnet_eth_if_t *eth_if = (fnet_eth_if_t *)(netif->netif_prv);
  122. if(_handlePHYWrite != NULL){
  123. _handlePHYWrite(reg_addr, data);
  124. }
  125. return FNET_OK; //Or FNET_OK
  126. }
  127. /*******************************************************************************
  128. * Ethernet interface API structure for PHY.
  129. ******************************************************************************/
  130. const fnet_eth_api_t fnet_usb_eth_api = {
  131. .phy_read = _fnet_usb_phy_read,
  132. .phy_write = _fnet_usb_phy_write,
  133. };
  134. #if FNET_CFG_MULTICAST
  135. /************************************************************************
  136. * DESCRIPTION: Joins a multicast group on USB interface.
  137. *************************************************************************/
  138. fnet_uint32_t fnet_usb_crc_hash(fnet_mac_addr_t multicast_addr) {
  139. fnet_uint32_t crc = 0xFFFFFFFFu;
  140. fnet_index_t i;
  141. fnet_index_t j;
  142. for (i = 0u; i < 6u; i++)
  143. {
  144. fnet_uint8_t c = multicast_addr[i];
  145. for (j = 0u; j < 8u; j++)
  146. {
  147. if (((c ^ crc) & 1u) != 0u)
  148. {
  149. crc >>= 1;
  150. c >>= 1;
  151. crc ^= 0xEDB88320u;
  152. }
  153. else
  154. {
  155. crc >>= 1;
  156. c >>= 1;
  157. }
  158. }
  159. }
  160. return crc;
  161. }
  162. void fnet_usb_multicast_join(fnet_netif_t *netif, fnet_mac_addr_t multicast_addr) {
  163. // fnet_uint32_t reg_value;
  164. // fnet_uint32_t crc;
  165. //
  166. // /* Set the appropriate bit in the hash table */
  167. // crc = fnet_usb_crc_hash(multicast_addr );
  168. // crc >>= 26;
  169. // crc &= 0x3FU;
  170. //
  171. // reg_value = (fnet_uint32_t)(0x1U << (crc & 0x1FU));
  172. if(_handleMulticastJoin != NULL){
  173. _handleMulticastJoin(netif, multicast_addr);
  174. }
  175. }
  176. /************************************************************************
  177. * DESCRIPTION: Leavess a multicast group on USB interface.
  178. *************************************************************************/
  179. void fnet_usb_multicast_leave(fnet_netif_t *netif, fnet_mac_addr_t multicast_addr) {
  180. // fnet_uint32_t reg_value;
  181. // fnet_uint32_t crc;
  182. //
  183. // /* Set the appropriate bit in the hash table */
  184. // crc = fnet_usb_crc_hash(multicast_addr );
  185. // crc >>= 26;
  186. // crc &= 0x3FU;
  187. //
  188. // reg_value = (fnet_uint32_t)(0x1U << (crc & 0x1FU));
  189. if(_handleMulticastLeave != NULL){
  190. _handleMulticastLeave(netif, multicast_addr);
  191. }
  192. }
  193. #endif /* FNET_CFG_MULTICAST */
  194. /*****************************************************************************
  195. * network-interface general API structure.
  196. ******************************************************************************/
  197. const fnet_netif_api_t fnet_usb_mac_api = {
  198. .netif_type = FNET_NETIF_TYPE_ETHERNET, /* Data-link type. */
  199. .netif_hw_addr_size = 6,
  200. .netif_init = fnet_usb_init, /* Initialization function.*/
  201. .netif_release = fnet_usb_release, /* Shutdown function.*/
  202. #if FNET_CFG_IP4
  203. .netif_output_ip4 = _fnet_eth_output_ip4, /* IPv4 Transmit function.*/
  204. #endif
  205. .netif_change_addr_notify = _fnet_eth_change_addr_notify, /* Address change notification function.*/
  206. .netif_drain = _fnet_eth_drain, /* Drain function.*/
  207. .netif_get_hw_addr = fnet_usb_get_hw_addr,
  208. .netif_set_hw_addr = fnet_usb_set_hw_addr,
  209. .netif_is_connected = fnet_usb_is_connected,
  210. .netif_get_statistics = fnet_usb_get_statistics,
  211. #if FNET_CFG_MULTICAST
  212. #if FNET_CFG_IP4
  213. .netif_multicast_join_ip4 = _fnet_eth_multicast_join_ip4,
  214. .netif_multicast_leave_ip4 = _fnet_eth_multicast_leave_ip4,
  215. #endif
  216. #if FNET_CFG_IP6
  217. .netif_multicast_join_ip6 = _fnet_eth_multicast_join_ip6,
  218. .netif_multicast_leave_ip6 = _fnet_eth_multicast_leave_ip6,
  219. #endif
  220. #endif
  221. #if FNET_CFG_IP6
  222. .netif_output_ip6 = _fnet_eth_output_ip6, /* IPv6 Transmit function.*/
  223. #endif
  224. .eth_api = &fnet_usb_eth_api,
  225. };
  226. /*****************************************************************************
  227. * Ethernet Control data structure
  228. ******************************************************************************/
  229. static fnet_eth_if_t fnet_usb_usb0_if = {
  230. // .eth_prv = NULL, /* Points to Ethernet driver-specific control data structure. */
  231. .eth_mac_number = 0, /* MAC module number. */
  232. .eth_output = fnet_usb_eth_output, /* Ethernet driver output.*/
  233. .eth_phy_addr = 0x10, /* Set default PHY address */
  234. #if FNET_CFG_MULTICAST
  235. .eth_multicast_join = fnet_usb_multicast_join, /* Ethernet driver join multicast group.*/
  236. .eth_multicast_leave = fnet_usb_multicast_leave /* Ethernet driver leave multicast group.*/
  237. #endif
  238. };
  239. /************************************************************************
  240. * Network interface structure.
  241. * DESCRIPTION: Indexes ethernet interface and network API structures. See
  242. * fnet_netif_prv.h for full specification of this struct.
  243. *************************************************************************/
  244. fnet_netif_t fnet_usb0_if = {
  245. .netif_name = {'e','t','h','0'}, /* Network interface name.*/
  246. .netif_mtu = 1500, /* Maximum transmission unit.*/
  247. .netif_prv = &fnet_usb_usb0_if, /* Points to interface specific data structure.*/
  248. .netif_api = &fnet_usb_mac_api /* Interface API */
  249. };
  250. fnet_netif_t * fnet_usb_get_netif() {
  251. return &fnet_usb0_if;
  252. }
  253. //#endif // !ARDUINO_TEENSY41