PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ASIXEthernet_Test.ino 4.9KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //#define STATS //Print how many times each thread loops per second
  2. #include <USBHost_t36.h> //USB Host Driver
  3. #include <ASIXEthernet.h> //USB Ethernet Driver
  4. #include <TeensyThreads.h> //Multi-thread
  5. #include <fnet.h> //TCP/IP stack
  6. #include <port/fnet_usb.h> //USB settings
  7. USBHost myusb;
  8. USBHub hub1(myusb);
  9. USBHub hub2(myusb);
  10. ASIXEthernet asix1(myusb);
  11. volatile uint8_t rbuf[1024*2]; // recieve buffer
  12. volatile uint8_t sbuf[2000]; // send buffer
  13. volatile uint16_t sbuflen; // length of data to send
  14. uint8_t MacAddress[6] = {0x00,0x50,0xB6,0xBE,0x8B,0xB4}; //Not setup yet, but can't be 0
  15. //Uses your adapters builtin address right now
  16. #ifdef STATS
  17. elapsedMillis advertise;
  18. uint32_t Looped;
  19. volatile uint32_t LoopedUSB;
  20. #endif
  21. Threads::Mutex fnet_mutex1; //Used by the stack
  22. uint16_t fnet_mutex1_calls; //Used to support recursiveness
  23. Threads::Mutex fnet_mutex2; //Used by the services
  24. uint16_t fnet_mutex2_calls; //Used to support recursiveness
  25. uint8_t mutex_init_calls = 0; //Change which mutex to assign
  26. fnet_return_t teensy_mutex_init(fnet_mutex_t *mutex) {
  27. Serial.print("Mutex initialized: ");
  28. Serial.println(mutex_init_calls);
  29. fnet_return_t result = FNET_ERR;
  30. if(mutex){
  31. if(mutex_init_calls == 0)*mutex = (fnet_mutex_t)&fnet_mutex1;
  32. else if(mutex_init_calls == 1)*mutex = (fnet_mutex_t)&fnet_mutex2;
  33. mutex_init_calls++;
  34. result = FNET_OK;
  35. }
  36. return result;
  37. }
  38. void teensy_mutex_release(fnet_mutex_t *mutex) {
  39. Serial.println("Mutex released");
  40. mutex_init_calls = 0;
  41. return; //TeensyThreads has no function for this?
  42. }
  43. void teensy_mutex_lock(fnet_mutex_t *mutex) {
  44. // Serial.print("Mutex locked: ");
  45. // Serial.println((uint32_t)((Threads::Mutex*)*mutex));
  46. Threads::Mutex *_mutex = (Threads::Mutex*)*mutex;
  47. if(&fnet_mutex1 == *mutex) {
  48. fnet_mutex1_calls++;
  49. if(fnet_mutex1_calls == 1){
  50. _mutex->lock();
  51. }
  52. }
  53. else if(&fnet_mutex2 == *mutex) {
  54. fnet_mutex2_calls++;
  55. if(fnet_mutex2_calls == 1){
  56. _mutex->lock();
  57. }
  58. }
  59. }
  60. void teensy_mutex_unlock(fnet_mutex_t *mutex) {
  61. // Serial.print("Mutex unlocked: ");
  62. // Serial.println((uint32_t)((Threads::Mutex*)*mutex));
  63. Threads::Mutex *_mutex = (Threads::Mutex*)*mutex;
  64. if(&fnet_mutex1 == *mutex) {
  65. fnet_mutex1_calls--;
  66. if(fnet_mutex1_calls == 0){
  67. _mutex->unlock();
  68. }
  69. }
  70. else if(&fnet_mutex2 == *mutex) {
  71. fnet_mutex2_calls--;
  72. if(fnet_mutex2_calls == 0){
  73. _mutex->unlock();
  74. }
  75. }
  76. }
  77. const fnet_mutex_api_t teensy_mutex_api ={
  78. .mutex_init = teensy_mutex_init,
  79. .mutex_release = teensy_mutex_release,
  80. .mutex_lock = teensy_mutex_lock,
  81. .mutex_unlock = teensy_mutex_unlock,
  82. };
  83. fnet_time_t timer_get_ms(void){ //Used for multi-thread version
  84. fnet_time_t result;
  85. result = millis();
  86. return result;
  87. }
  88. fnet_dhcp_cln_params_t dhcp_params; //DHCP intialization parameters
  89. fnet_dhcp_cln_desc_t dhcp_desc; //DHCP object
  90. fnet_netif_desc_t current_netif; //Network interface, USB Ethernet
  91. static DMAMEM fnet_uint8_t stack_heap[(128U * 1024U)];
  92. void setup() {
  93. Serial.begin(115200);
  94. // Wait 1.5 seconds before turning on USB Host. If connected USB devices
  95. // use too much power, Teensy at least completes USB enumeration, which
  96. // makes isolating the power issue easier.
  97. delay(1500);
  98. Serial.println("USB Host InputFunctions example");
  99. delay(10);
  100. myusb.begin();
  101. asix1.setHandleRecieve(handleRecieve);
  102. asix1.setHandleWait(handleWait);
  103. Serial.println("USB Ready");
  104. // threads.addThread(usbpoll);
  105. threads.addThread(usbthread);
  106. //All of these have to be set to function correctly
  107. setHandleOutput(handleOutput);
  108. setHandleSetMACAddress(handleSetMACAddress);
  109. setHandleGetMACAddress(handleGetMACAddress);
  110. setHandlePHYRead(handlePHYRead);
  111. setHandlePHYWrite(handlePHYWrite);
  112. setHandleMulticastLeave(handleMulticastLeave);
  113. setHandleMulticastJoin(handleMulticastJoin);
  114. setHandleIsConnected(handleIsConnected);
  115. }
  116. void loop() {
  117. while(1){
  118. myusb.Task();
  119. asix1.read();
  120. if(uint8_t index = Serial.available()){
  121. if(index >= 64) break;
  122. char serialMessage[64];
  123. for(uint8_t i = 0;i<index;i++){
  124. serialMessage[i] = Serial.read();
  125. if(serialMessage[i] == ' '){ //Each command needs to be null-terminated
  126. serialMessage[i] = '\0';
  127. }
  128. else if(serialMessage[i] == '\n'){ //Each command needs to be null-terminated
  129. serialMessage[i] = '\0';
  130. }
  131. }
  132. serialParser(index, (char*)&serialMessage);
  133. }
  134. #ifdef STATS
  135. Looped++;
  136. if(advertise >= 1000) {
  137. advertise -= 1000;
  138. Serial.print("Looped: ");
  139. Serial.print(Looped);
  140. Serial.print(" LoopedUSB: ");
  141. Serial.print(LoopedUSB);
  142. if(fnet_netif_is_initialized(current_netif)){
  143. Serial.print(" FNETMemFree: ");
  144. Serial.print((uint32_t)fnet_free_mem_status());
  145. }
  146. Serial.print(" LinkSpeed: ");
  147. Serial.println(asix1.PHYSpeed ? "100BASE" : "10BASE");
  148. Looped = 0;
  149. LoopedUSB = 0;
  150. }
  151. #endif
  152. }
  153. }