TeensyMAC.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* TeensyMAC library code is placed under the MIT license
  2. * Copyright (c) 2016 Frank Bösing
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #include "TeensyMAC.h"
  25. #include <Arduino.h>
  26. #define MY_SYSREGISTERFILE ((uint8_t *)0x40041000) // System Register File
  27. static uint32_t _getserialhw(void) {
  28. uint32_t num;
  29. __disable_irq();
  30. #if defined(HAS_KINETIS_FLASH_FTFA) || defined(HAS_KINETIS_FLASH_FTFL)
  31. FTFL_FSTAT = FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL;
  32. FTFL_FCCOB0 = 0x41;
  33. FTFL_FCCOB1 = 15;
  34. FTFL_FSTAT = FTFL_FSTAT_CCIF;
  35. while (!(FTFL_FSTAT & FTFL_FSTAT_CCIF)) ; // wait
  36. num = *(uint32_t *)&FTFL_FCCOB7;
  37. #elif defined(HAS_KINETIS_FLASH_FTFE)
  38. // Does not work in HSRUN mode :
  39. FTFL_FSTAT = FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL;
  40. *(uint32_t *)&FTFL_FCCOB3 = 0x41070000;
  41. FTFL_FSTAT = FTFL_FSTAT_CCIF;
  42. while (!(FTFL_FSTAT & FTFL_FSTAT_CCIF)) ; // wait
  43. num = *(uint32_t *)&FTFL_FCCOBB;
  44. #endif
  45. __enable_irq();
  46. return num;
  47. }
  48. #if defined(HAS_KINETIS_FLASH_FTFE) && (F_CPU > 120000000)
  49. extern "C" void startup_early_hook(void) {
  50. #if defined(KINETISK)
  51. WDOG_STCTRLH = WDOG_STCTRLH_ALLOWUPDATE;
  52. #elif defined(KINETISL)
  53. SIM_COPC = 0; // disable the watchdog
  54. #endif
  55. *(uint32_t*)(MY_SYSREGISTERFILE) = _getserialhw();
  56. }
  57. uint32_t teensySerial(void) {
  58. uint32_t num;
  59. num = *(uint32_t*)(MY_SYSREGISTERFILE);
  60. // add extra zero to work around OS-X CDC-ACM driver bug
  61. // http://forum.pjrc.com/threads/25482-Duplicate-usb-modem-number-HELP
  62. if (num < 10000000) num = num * 10;
  63. return num;
  64. }
  65. uint64_t teensyMAC(void) {
  66. return 0x04E9E5000000ULL | (*(uint32_t*)(MY_SYSREGISTERFILE));
  67. }
  68. #else
  69. uint32_t teensySerial(void) {
  70. uint32_t num;
  71. num = _getserialhw();
  72. // add extra zero to work around OS-X CDC-ACM driver bug
  73. // http://forum.pjrc.com/threads/25482-Duplicate-usb-modem-number-HELP
  74. if (num < 10000000) num = num * 10;
  75. return num;
  76. }
  77. uint64_t teensyMAC(void) {
  78. return 0x04E9E5000000ULL | _getserialhw();
  79. }
  80. #endif
  81. void teensyMAC(uint8_t *macArray) {
  82. uint64_t mac = teensyMAC();
  83. macArray[0] = (uint8_t)((mac >> 40) & 0xFF);
  84. macArray[1] = (uint8_t)((mac >> 32) & 0xFF);
  85. macArray[2] = (uint8_t)((mac >> 24) & 0XFF);
  86. macArray[3] = (uint8_t)((mac >> 16) & 0XFF);
  87. macArray[4] = (uint8_t)((mac >> 8) & 0XFF);
  88. macArray[5] = (uint8_t)(mac & 0XFF);
  89. }