Teensy 4.1 core updated for 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.

SPIFIFO.h 10KB

11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #ifndef _SPIFIFO_h_
  2. #define _SPIFIFO_h_
  3. #include "avr_emulation.h"
  4. #if F_BUS == 60000000
  5. #define HAS_SPIFIFO
  6. #define SPI_CLOCK_24MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(60 / 3) * ((1+1)/2) = 20 MHz
  7. #define SPI_CLOCK_16MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0)) //(60 / 2) * ((1+0)/2) = 15 MHz
  8. #define SPI_CLOCK_12MHz (SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(60 / 5) * ((1+1)/2)
  9. #define SPI_CLOCK_8MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(1)) //(60 / 2) * ((1+0)/4) = 7.5 MHz
  10. #define SPI_CLOCK_6MHz (SPI_CTAR_PBR(2) | SPI_CTAR_BR(0)) //(60 / 5) * ((1+0)/2)
  11. #define SPI_CLOCK_4MHz (SPI_CTAR_PBR(2) | SPI_CTAR_BR(2) | SPI_CTAR_DBR) //(60 / 5) * ((1+1)/6)
  12. #elif F_BUS == 56000000
  13. #define HAS_SPIFIFO
  14. #define SPI_CLOCK_24MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(56 / 3) * ((1+1)/2) = 18.67
  15. #define SPI_CLOCK_16MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0)) //(56 / 2) * ((1+0)/2) = 14
  16. #define SPI_CLOCK_12MHz (SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(56 / 5) * ((1+1)/2) = 11.2
  17. #define SPI_CLOCK_8MHz (SPI_CTAR_PBR(3) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(56 / 7) * ((1+1)/2)
  18. #define SPI_CLOCK_6MHz (SPI_CTAR_PBR(2) | SPI_CTAR_BR(0)) //(56 / 5) * ((1+0)/2)
  19. #define SPI_CLOCK_4MHz (SPI_CTAR_PBR(3) | SPI_CTAR_BR(0)) //(56 / 7) * ((1+0)/2)
  20. #elif F_BUS == 48000000
  21. #define HAS_SPIFIFO
  22. #define SPI_CLOCK_24MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(48 / 2) * ((1+1)/2)
  23. #define SPI_CLOCK_16MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(48 / 3) * ((1+1)/2)
  24. #define SPI_CLOCK_12MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0)) //(48 / 2) * ((1+0)/2)
  25. #define SPI_CLOCK_8MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(2) | SPI_CTAR_DBR) //(48 / 2) * ((1+1)/6)
  26. #define SPI_CLOCK_6MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(1)) //(48 / 2) * ((1+0)/4)
  27. #define SPI_CLOCK_4MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(2)) //(48 / 2) * ((1+0)/6)
  28. #elif F_BUS == 40000000
  29. #define HAS_SPIFIFO
  30. #define SPI_CLOCK_24MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(40 / 2) * ((1+1)/2) = 20
  31. #define SPI_CLOCK_16MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(40 / 3) * ((1+1)/2) = 13.33
  32. #define SPI_CLOCK_12MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0)) //(40 / 2) * ((1+0)/2) = 10
  33. #define SPI_CLOCK_8MHz (SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(40 / 5) * ((1+1)/2)
  34. #define SPI_CLOCK_6MHz (SPI_CTAR_PBR(3) | SPI_CTAR_BR(1) | SPI_CTAR_DBR) //(40 / 7) * ((1+1)/2) = 5.71
  35. #define SPI_CLOCK_4MHz (SPI_CTAR_PBR(2) | SPI_CTAR_BR(1)) //(40 / 5) * ((1+0)/2)
  36. #elif F_BUS == 36000000
  37. #define HAS_SPIFIFO
  38. #define SPI_CLOCK_24MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(36 / 2) * ((1+1)/2) = 18
  39. #define SPI_CLOCK_16MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(36 / 3) * ((1+1)/2) = 12
  40. #define SPI_CLOCK_12MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(36 / 3) * ((1+1)/2) = 12
  41. #define SPI_CLOCK_8MHz (SPI_CTAR_PBR(2) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(36 / 5) * ((1+1)/2) = 7.2
  42. #define SPI_CLOCK_6MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(2) | SPI_CTAR_DBR) //(36 / 2) * ((1+1)/6)
  43. #define SPI_CLOCK_4MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(2) | SPI_CTAR_DBR) //(36 / 3) * ((1+1)/6)
  44. #elif F_BUS == 24000000
  45. #define HAS_SPIFIFO
  46. #define SPI_CLOCK_24MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(24 / 2) * ((1+1)/2) 12 MHz
  47. #define SPI_CLOCK_16MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(24 / 2) * ((1+1)/2) 12 MHz
  48. #define SPI_CLOCK_12MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(24 / 2) * ((1+1)/2)
  49. #define SPI_CLOCK_8MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(24 / 3) * ((1+1)/2)
  50. #define SPI_CLOCK_6MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0)) //(24 / 2) * ((1+0)/2)
  51. #define SPI_CLOCK_4MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(2) | SPI_CTAR_DBR) //(24 / 2) * ((1+1)/6)
  52. #elif F_BUS == 16000000
  53. #define HAS_SPIFIFO
  54. #define SPI_CLOCK_24MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(3) | SPI_CTAR_DBR) //(16 / 2) * ((1+1)/8) = 2 MHz
  55. #define SPI_CLOCK_16MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(3) | SPI_CTAR_DBR) //(16 / 2) * ((1+1)/8) = 2 MHz
  56. #define SPI_CLOCK_12MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(3) | SPI_CTAR_DBR) //(16 / 2) * ((1+1)/8) = 2 MHz
  57. #define SPI_CLOCK_8MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(3) | SPI_CTAR_DBR) //(16 / 2) * ((1+1)/8) = 2 MHz
  58. #define SPI_CLOCK_6MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(3) | SPI_CTAR_DBR) //(16 / 2) * ((1+1)/8) = 2 MHz
  59. #define SPI_CLOCK_4MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(3) | SPI_CTAR_DBR) //(16 / 2) * ((1+1)/8) = 2 MHz
  60. #elif F_BUS == 8000000
  61. #define HAS_SPIFIFO
  62. #define SPI_CLOCK_24MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_DBR) //(8 / 2) * ((1+1)/4) = 2 MHz
  63. #define SPI_CLOCK_16MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_DBR) //(8 / 2) * ((1+1)/4) = 2 MHz
  64. #define SPI_CLOCK_12MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_DBR) //(8 / 2) * ((1+1)/4) = 2 MHz
  65. #define SPI_CLOCK_8MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_DBR) //(8 / 2) * ((1+1)/4) = 2 MHz
  66. #define SPI_CLOCK_6MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_DBR) //(8 / 2) * ((1+1)/4) = 2 MHz
  67. #define SPI_CLOCK_4MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(1) | SPI_CTAR_DBR) //(8 / 2) * ((1+1)/4) = 2 MHz
  68. #elif F_BUS == 4000000
  69. #define HAS_SPIFIFO
  70. #define SPI_CLOCK_24MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(4 / 2) * ((1+1)/2) = 2 MHz
  71. #define SPI_CLOCK_16MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(4 / 2) * ((1+1)/2) = 2 MHz
  72. #define SPI_CLOCK_12MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(4 / 2) * ((1+1)/2) = 2 MHz
  73. #define SPI_CLOCK_8MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(4 / 2) * ((1+1)/2) = 2 MHz
  74. #define SPI_CLOCK_6MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(4 / 2) * ((1+1)/2) = 2 MHz
  75. #define SPI_CLOCK_4MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(4 / 2) * ((1+1)/2) = 2 MHz
  76. #elif F_BUS == 2000000
  77. #define HAS_SPIFIFO
  78. #define SPI_CLOCK_24MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(4 / 2) * ((1+1)/2) = 1 MHz
  79. #define SPI_CLOCK_16MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(4 / 2) * ((1+1)/2) = 1 MHz
  80. #define SPI_CLOCK_12MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(4 / 2) * ((1+1)/2) = 1 MHz
  81. #define SPI_CLOCK_8MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(4 / 2) * ((1+1)/2) = 1 MHz
  82. #define SPI_CLOCK_6MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(4 / 2) * ((1+1)/2) = 1 MHz
  83. #define SPI_CLOCK_4MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(4 / 2) * ((1+1)/2) = 1 MHz
  84. #endif
  85. /*
  86. #! /usr/bin/perl
  87. $clock = 60;
  88. for $i (2, 3, 5, 7) {
  89. for $j (0, 1) {
  90. for $k (2, 4, 6, 8, 16, 32) {
  91. $out = $clock / $i * (1 + $j) / $k;
  92. printf "%0.2f : ", $out;
  93. print "$clock / $i * (1 + $j) / $k = $out\n";
  94. }
  95. }
  96. }
  97. */
  98. // sck = F_BUS / PBR * ((1+DBR)/BR)
  99. // PBR = 2, 3, 5, 7
  100. // DBR = 0, 1 -- zero preferred
  101. // BR = 2, 4, 6, 8, 16, 32, 64, 128, 256, 512
  102. #ifdef HAS_SPIFIFO
  103. #ifndef SPI_MODE0
  104. #define SPI_MODE0 0x00 // CPOL = 0, CPHA = 0
  105. #define SPI_MODE1 0x04 // CPOL = 0, CPHA = 1
  106. #define SPI_MODE2 0x08 // CPOL = 1, CPHA = 0
  107. #define SPI_MODE3 0x0C // CPOL = 1, CPHA = 1
  108. #endif
  109. #define SPI_CONTINUE 1
  110. class SPIFIFOclass
  111. {
  112. public:
  113. inline void begin(uint8_t pin, uint32_t speed, uint32_t mode=SPI_MODE0) __attribute__((always_inline)) {
  114. uint32_t p, ctar = speed;
  115. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  116. SPI0.MCR = SPI_MCR_MSTR | SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  117. if (mode & 0x08) ctar |= SPI_CTAR_CPOL;
  118. if (mode & 0x04) {
  119. ctar |= SPI_CTAR_CPHA;
  120. ctar |= (ctar & 0x0F) << 8;
  121. } else {
  122. ctar |= (ctar & 0x0F) << 12;
  123. }
  124. SPI0.CTAR0 = ctar | SPI_CTAR_FMSZ(7);
  125. SPI0.CTAR1 = ctar | SPI_CTAR_FMSZ(15);
  126. if (pin == 10) { // PTC4
  127. CORE_PIN10_CONFIG = PORT_PCR_MUX(2);
  128. p = 0x01;
  129. } else if (pin == 2) { // PTD0
  130. CORE_PIN2_CONFIG = PORT_PCR_MUX(2);
  131. p = 0x01;
  132. } else if (pin == 9) { // PTC3
  133. CORE_PIN9_CONFIG = PORT_PCR_MUX(2);
  134. p = 0x02;
  135. } else if (pin == 6) { // PTD4
  136. CORE_PIN6_CONFIG = PORT_PCR_MUX(2);
  137. p = 0x02;
  138. } else if (pin == 20) { // PTD5
  139. CORE_PIN20_CONFIG = PORT_PCR_MUX(2);
  140. p = 0x04;
  141. } else if (pin == 23) { // PTC2
  142. CORE_PIN23_CONFIG = PORT_PCR_MUX(2);
  143. p = 0x04;
  144. } else if (pin == 21) { // PTD6
  145. CORE_PIN21_CONFIG = PORT_PCR_MUX(2);
  146. p = 0x08;
  147. } else if (pin == 22) { // PTC1
  148. CORE_PIN22_CONFIG = PORT_PCR_MUX(2);
  149. p = 0x08;
  150. } else if (pin == 15) { // PTC0
  151. CORE_PIN15_CONFIG = PORT_PCR_MUX(2);
  152. p = 0x10;
  153. } else {
  154. reg = portOutputRegister(pin);
  155. *reg = 1;
  156. pinMode(pin, OUTPUT);
  157. p = 0;
  158. }
  159. pcs = p;
  160. clear();
  161. SPCR.enable_pins();
  162. }
  163. inline void write(uint32_t b, uint32_t cont=0) __attribute__((always_inline)) {
  164. uint32_t pcsbits = pcs << 16;
  165. if (pcsbits) {
  166. SPI0.PUSHR = (b & 0xFF) | pcsbits | (cont ? SPI_PUSHR_CONT : 0);
  167. while (((SPI0.SR) & (15 << 12)) > (3 << 12)) ; // wait if FIFO full
  168. } else {
  169. *reg = 0;
  170. SPI0.SR = SPI_SR_EOQF;
  171. SPI0.PUSHR = (b & 0xFF) | (cont ? 0 : SPI_PUSHR_EOQ);
  172. if (cont) {
  173. while (((SPI0.SR) & (15 << 12)) > (3 << 12)) ;
  174. } else {
  175. while (!(SPI0.SR & SPI_SR_EOQF)) ;
  176. *reg = 1;
  177. }
  178. }
  179. }
  180. inline void write16(uint32_t b, uint32_t cont=0) __attribute__((always_inline)) {
  181. uint32_t pcsbits = pcs << 16;
  182. if (pcsbits) {
  183. SPI0.PUSHR = (b & 0xFFFF) | (pcs << 16) |
  184. (cont ? SPI_PUSHR_CONT : 0) | SPI_PUSHR_CTAS(1);
  185. while (((SPI0.SR) & (15 << 12)) > (3 << 12)) ;
  186. } else {
  187. *reg = 0;
  188. SPI0.SR = SPI_SR_EOQF;
  189. SPI0.PUSHR = (b & 0xFFFF) | (cont ? 0 : SPI_PUSHR_EOQ) | SPI_PUSHR_CTAS(1);
  190. if (cont) {
  191. while (((SPI0.SR) & (15 << 12)) > (3 << 12)) ;
  192. } else {
  193. while (!(SPI0.SR & SPI_SR_EOQF)) ;
  194. *reg = 1;
  195. }
  196. }
  197. }
  198. inline uint32_t read(void) __attribute__((always_inline)) {
  199. while ((SPI0.SR & (15 << 4)) == 0) ; // TODO, could wait forever
  200. return SPI0.POPR;
  201. }
  202. inline void clear(void) __attribute__((always_inline)) {
  203. SPI0.MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F) | SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF;
  204. }
  205. private:
  206. static uint8_t pcs;
  207. static volatile uint8_t *reg;
  208. };
  209. extern SPIFIFOclass SPIFIFO;
  210. #endif
  211. #endif