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.

SPIFIFO.h 8.8KB

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