1
0

F2837xD_Spi.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //###########################################################################
  2. //
  3. // FILE: F2837xD_Spi.c
  4. //
  5. // TITLE: F2837xD SPI Initialization & Support Functions.
  6. //
  7. //###########################################################################
  8. // $TI Release: F2837xD Support Library v3.05.00.00 $
  9. // $Release Date: Tue Jun 26 03:15:23 CDT 2018 $
  10. // $Copyright:
  11. // Copyright (C) 2013-2018 Texas Instruments Incorporated - http://www.ti.com/
  12. //
  13. // Redistribution and use in source and binary forms, with or without
  14. // modification, are permitted provided that the following conditions
  15. // are met:
  16. //
  17. // Redistributions of source code must retain the above copyright
  18. // notice, this list of conditions and the following disclaimer.
  19. //
  20. // Redistributions in binary form must reproduce the above copyright
  21. // notice, this list of conditions and the following disclaimer in the
  22. // documentation and/or other materials provided with the
  23. // distribution.
  24. //
  25. // Neither the name of Texas Instruments Incorporated nor the names of
  26. // its contributors may be used to endorse or promote products derived
  27. // from this software without specific prior written permission.
  28. //
  29. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. // $
  41. //###########################################################################
  42. //
  43. // Included Files
  44. //
  45. #include "F2837xD_device.h"
  46. #include "F2837xD_Examples.h"
  47. //
  48. // Calculate BRR: 7-bit baud rate register value
  49. // SPI CLK freq = 500 kHz
  50. // LSPCLK freq = CPU freq / 4 (by default)
  51. // BRR = (LSPCLK freq / SPI CLK freq) - 1
  52. //
  53. #if CPU_FRQ_200MHZ
  54. #define SPI_BRR ((200E6 / 4) / 500E3) - 1
  55. #endif
  56. #if CPU_FRQ_150MHZ
  57. #define SPI_BRR ((150E6 / 4) / 500E3) - 1
  58. #endif
  59. #if CPU_FRQ_120MHZ
  60. #define SPI_BRR ((120E6 / 4) / 500E3) - 1
  61. #endif
  62. //
  63. // InitSPI - This function initializes the SPI to a known state
  64. //
  65. void InitSpi(void)
  66. {
  67. // Initialize SPI-A
  68. // Set reset low before configuration changes
  69. // Clock polarity (0 == rising, 1 == falling)
  70. // 16-bit character
  71. // Enable loop-back
  72. SpiaRegs.SPICCR.bit.SPISWRESET = 0;
  73. SpiaRegs.SPICCR.bit.CLKPOLARITY = 0;
  74. SpiaRegs.SPICCR.bit.SPICHAR = (16-1);
  75. SpiaRegs.SPICCR.bit.SPILBK = 1;
  76. // Enable master (0 == slave, 1 == master)
  77. // Enable transmission (Talk)
  78. // Clock phase (0 == normal, 1 == delayed)
  79. // SPI interrupts are disabled
  80. SpiaRegs.SPICTL.bit.MASTER_SLAVE = 1;
  81. SpiaRegs.SPICTL.bit.TALK = 1;
  82. SpiaRegs.SPICTL.bit.CLK_PHASE = 0;
  83. SpiaRegs.SPICTL.bit.SPIINTENA = 0;
  84. // Set the baud rate
  85. SpiaRegs.SPIBRR.bit.SPI_BIT_RATE = SPI_BRR;
  86. // Set FREE bit
  87. // Halting on a breakpoint will not halt the SPI
  88. SpiaRegs.SPIPRI.bit.FREE = 1;
  89. // Release the SPI from reset
  90. SpiaRegs.SPICCR.bit.SPISWRESET = 1;
  91. }
  92. //
  93. // InitSpiGpio - This function initializes GPIO pins to function as SPI pins.
  94. // Each GPIO pin can be configured as a GPIO pin or up to 3
  95. // different peripheral functional pins. By default all pins come
  96. // up as GPIO inputs after reset.
  97. //
  98. // Caution:
  99. // For each SPI peripheral
  100. // Only one GPIO pin should be enabled for SPISOMO operation.
  101. // Only one GPIO pin should be enabled for SPISOMI operation.
  102. // Only one GPIO pin should be enabled for SPICLK operation.
  103. // Only one GPIO pin should be enabled for SPISTE operation.
  104. // Comment out other unwanted lines.
  105. //
  106. void InitSpiGpio()
  107. {
  108. InitSpiaGpio();
  109. }
  110. //
  111. // InitSpiaGpio - Initialize SPIA GPIOs
  112. //
  113. void InitSpiaGpio()
  114. {
  115. EALLOW;
  116. //
  117. // Enable internal pull-up for the selected pins
  118. //
  119. // Pull-ups can be enabled or disabled by the user.
  120. // This will enable the pullups for the specified pins.
  121. // Comment out other unwanted lines.
  122. //
  123. GpioCtrlRegs.GPAPUD.bit.GPIO16 = 0; // Enable pull-up on GPIO16 (SPISIMOA)
  124. // GpioCtrlRegs.GPAPUD.bit.GPIO5 = 0; // Enable pull-up on GPIO5 (SPISIMOA)
  125. GpioCtrlRegs.GPAPUD.bit.GPIO17 = 0; // Enable pull-up on GPIO17 (SPISOMIA)
  126. // GpioCtrlRegs.GPAPUD.bit.GPIO3 = 0; // Enable pull-up on GPIO3 (SPISOMIA)
  127. GpioCtrlRegs.GPAPUD.bit.GPIO18 = 0; // Enable pull-up on GPIO18 (SPICLKA)
  128. GpioCtrlRegs.GPAPUD.bit.GPIO19 = 0; // Enable pull-up on GPIO19 (SPISTEA)
  129. //
  130. // Set qualification for selected pins to asynch only
  131. //
  132. // This will select asynch (no qualification) for the selected pins.
  133. // Comment out other unwanted lines.
  134. //
  135. GpioCtrlRegs.GPAQSEL2.bit.GPIO16 = 3; // Asynch input GPIO16 (SPISIMOA)
  136. // GpioCtrlRegs.GPAQSEL1.bit.GPIO5 = 3; // Asynch input GPIO5 (SPISIMOA)
  137. GpioCtrlRegs.GPAQSEL2.bit.GPIO17 = 3; // Asynch input GPIO17 (SPISOMIA)
  138. // GpioCtrlRegs.GPAQSEL1.bit.GPIO3 = 3; // Asynch input GPIO3 (SPISOMIA)
  139. GpioCtrlRegs.GPAQSEL2.bit.GPIO18 = 3; // Asynch input GPIO18 (SPICLKA)
  140. GpioCtrlRegs.GPAQSEL2.bit.GPIO19 = 3; // Asynch input GPIO19 (SPISTEA)
  141. //
  142. //Configure SPI-A pins using GPIO regs
  143. //
  144. // This specifies which of the possible GPIO pins will be SPI functional
  145. // pins.
  146. // Comment out other unwanted lines.
  147. //
  148. GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 1; // Configure GPIO16 as SPISIMOA
  149. // GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 2; // Configure GPIO5 as SPISIMOA
  150. GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 1; // Configure GPIO17 as SPISOMIA
  151. // GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 2; // Configure GPIO3 as SPISOMIA
  152. GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1; // Configure GPIO18 as SPICLKA
  153. GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 1; // Configure GPIO19 as SPISTEA
  154. EDIS;
  155. }
  156. //
  157. // End of file
  158. //