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.

150 line
4.5KB

  1. #ifndef _SPIFIFO_h_
  2. #define _SPIFIFO_h_
  3. #include "avr_emulation.h"
  4. #if F_BUS == 48000000
  5. #define HAS_SPIFIFO
  6. #define SPI_CLOCK_24MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(48 / 2) * ((1+1)/2)
  7. #define SPI_CLOCK_16MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(48 / 3) * ((1+1)/2) 33% duty cycle
  8. #define SPI_CLOCK_12MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0)) //(48 / 2) * ((1+0)/2)
  9. #define SPI_CLOCK_8MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(0)) //(48 / 3) * ((1+0)/2)
  10. #define SPI_CLOCK_6MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(1)) //(48 / 2) * ((1+0)/4)
  11. #define SPI_CLOCK_4MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(1)) //(48 / 3) * ((1+0)/4)
  12. #elif F_BUS == 24000000
  13. #define HAS_SPIFIFO
  14. #define SPI_CLOCK_24MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(24 / 2) * ((1+1)/2) 12 MHz
  15. #define SPI_CLOCK_16MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(24 / 2) * ((1+1)/2) 12 MHz
  16. #define SPI_CLOCK_12MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(24 / 2) * ((1+1)/2)
  17. #define SPI_CLOCK_8MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(0) | SPI_CTAR_DBR) //(24 / 3) * ((1+1)/2) 33% duty cycle
  18. #define SPI_CLOCK_6MHz (SPI_CTAR_PBR(0) | SPI_CTAR_BR(0)) //(24 / 2) * ((1+0)/2)
  19. #define SPI_CLOCK_4MHz (SPI_CTAR_PBR(1) | SPI_CTAR_BR(0)) //(24 / 3) * ((1+0)/2)
  20. #endif
  21. // sck = F_BUS / PBR * ((1+DBR)/BR)
  22. // PBR = 2, 3, 5, 7
  23. // DBR = 0, 1 -- zero preferred
  24. // BR = 2, 4, 6, 8, 16, 32, 64, 128, 256, 512
  25. #ifdef HAS_SPIFIFO
  26. #ifndef SPI_MODE0
  27. #define SPI_MODE0 0x00 // CPOL = 0, CPHA = 0
  28. #define SPI_MODE1 0x04 // CPOL = 0, CPHA = 1
  29. #define SPI_MODE2 0x08 // CPOL = 1, CPHA = 0
  30. #define SPI_MODE3 0x0C // CPOL = 1, CPHA = 1
  31. #endif
  32. #define SPI_CONTINUE 1
  33. class SPIFIFOclass
  34. {
  35. public:
  36. inline void begin(uint8_t pin, uint32_t speed, uint32_t mode=SPI_MODE0) __attribute__((always_inline)) {
  37. uint32_t p, ctar = speed;
  38. SIM_SCGC6 |= SIM_SCGC6_SPI0;
  39. SPI0.MCR = SPI_MCR_MSTR | SPI_MCR_MDIS | SPI_MCR_HALT | SPI_MCR_PCSIS(0x1F);
  40. if (mode & 0x08) ctar |= SPI_CTAR_CPOL;
  41. if (mode & 0x04) {
  42. ctar |= SPI_CTAR_CPHA;
  43. ctar |= (ctar & 0x0F) << 8;
  44. } else {
  45. ctar |= (ctar & 0x0F) << 12;
  46. }
  47. SPI0.CTAR0 = ctar | SPI_CTAR_FMSZ(7);
  48. SPI0.CTAR1 = ctar | SPI_CTAR_FMSZ(15);
  49. if (pin == 10) { // PTC4
  50. CORE_PIN10_CONFIG = PORT_PCR_MUX(2);
  51. p = 0x01;
  52. } else if (pin == 2) { // PTD0
  53. CORE_PIN2_CONFIG = PORT_PCR_MUX(2);
  54. p = 0x01;
  55. } else if (pin == 9) { // PTC3
  56. CORE_PIN9_CONFIG = PORT_PCR_MUX(2);
  57. p = 0x02;
  58. } else if (pin == 6) { // PTD4
  59. CORE_PIN6_CONFIG = PORT_PCR_MUX(2);
  60. p = 0x02;
  61. } else if (pin == 20) { // PTD5
  62. CORE_PIN20_CONFIG = PORT_PCR_MUX(2);
  63. p = 0x04;
  64. } else if (pin == 23) { // PTC2
  65. CORE_PIN23_CONFIG = PORT_PCR_MUX(2);
  66. p = 0x04;
  67. } else if (pin == 21) { // PTD6
  68. CORE_PIN21_CONFIG = PORT_PCR_MUX(2);
  69. p = 0x08;
  70. } else if (pin == 22) { // PTC1
  71. CORE_PIN22_CONFIG = PORT_PCR_MUX(2);
  72. p = 0x08;
  73. } else if (pin == 15) { // PTC0
  74. CORE_PIN15_CONFIG = PORT_PCR_MUX(2);
  75. p = 0x10;
  76. } else {
  77. reg = portOutputRegister(pin);
  78. *reg = 1;
  79. pinMode(pin, OUTPUT);
  80. p = 0;
  81. }
  82. pcs = p;
  83. clear();
  84. SPCR.enable_pins();
  85. }
  86. inline void write(uint32_t b, uint32_t cont=0) __attribute__((always_inline)) {
  87. uint32_t pcsbits = pcs << 16;
  88. if (pcsbits) {
  89. SPI0.PUSHR = (b & 0xFF) | pcsbits | (cont ? SPI_PUSHR_CONT : 0);
  90. while (((SPI0.SR) & (15 << 12)) > (3 << 12)) ; // wait if FIFO full
  91. } else {
  92. *reg = 0;
  93. SPI0.SR = SPI_SR_EOQF;
  94. SPI0.PUSHR = (b & 0xFF) | (cont ? 0 : SPI_PUSHR_EOQ);
  95. if (cont) {
  96. while (((SPI0.SR) & (15 << 12)) > (3 << 12)) ;
  97. } else {
  98. while (!(SPI0.SR & SPI_SR_EOQF)) ;
  99. *reg = 1;
  100. }
  101. }
  102. }
  103. inline void write16(uint32_t b, uint32_t cont=0) __attribute__((always_inline)) {
  104. uint32_t pcsbits = pcs << 16;
  105. if (pcsbits) {
  106. SPI0.PUSHR = (b & 0xFFFF) | (pcs << 16) |
  107. (cont ? SPI_PUSHR_CONT : 0) | SPI_PUSHR_CTAS(1);
  108. while (((SPI0.SR) & (15 << 12)) > (3 << 12)) ;
  109. } else {
  110. *reg = 0;
  111. SPI0.SR = SPI_SR_EOQF;
  112. SPI0.PUSHR = (b & 0xFFFF) | (cont ? 0 : SPI_PUSHR_EOQ) | SPI_PUSHR_CTAS(1);
  113. if (cont) {
  114. while (((SPI0.SR) & (15 << 12)) > (3 << 12)) ;
  115. } else {
  116. while (!(SPI0.SR & SPI_SR_EOQF)) ;
  117. *reg = 1;
  118. }
  119. }
  120. }
  121. inline uint32_t read(void) __attribute__((always_inline)) {
  122. while ((SPI0.SR & (15 << 4)) == 0) ; // TODO, could wait forever
  123. return SPI0.POPR;
  124. }
  125. inline void clear(void) __attribute__((always_inline)) {
  126. SPI0.MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F) | SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF;
  127. }
  128. private:
  129. static uint8_t pcs;
  130. static volatile uint8_t *reg;
  131. };
  132. extern SPIFIFOclass SPIFIFO;
  133. #endif
  134. #endif