jruthe

SSI Example Code

Mar 6th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.14 KB | None | 0 0
  1. //*****************************************************************************
  2. //
  3. // spi_master.c - Example demonstrating how to configure SSI0 in SPI master
  4. //                mode.
  5. //
  6. // Copyright (c) 2010-2013 Texas Instruments Incorporated.  All rights reserved.
  7. // Software License Agreement
  8. //
  9. //   Redistribution and use in source and binary forms, with or without
  10. //   modification, are permitted provided that the following conditions
  11. //   are met:
  12. //
  13. //   Redistributions of source code must retain the above copyright
  14. //   notice, this list of conditions and the following disclaimer.
  15. //
  16. //   Redistributions in binary form must reproduce the above copyright
  17. //   notice, this list of conditions and the following disclaimer in the
  18. //   documentation and/or other materials provided with the
  19. //   distribution.
  20. //
  21. //   Neither the name of Texas Instruments Incorporated nor the names of
  22. //   its contributors may be used to endorse or promote products derived
  23. //   from this software without specific prior written permission.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. //
  37. // This is part of revision 2.0.1.11577 of the Tiva Firmware Development Package.
  38. //
  39. //*****************************************************************************
  40.  
  41. #include <stdbool.h>
  42. #include <stdint.h>
  43. #include "inc/hw_memmap.h"
  44. #include "driverlib/gpio.h"
  45. #include "driverlib/pin_map.h"
  46. #include "driverlib/ssi.h"
  47. #include "driverlib/sysctl.h"
  48. #include "driverlib/uart.h"
  49. #include "utils/uartstdio.h"
  50.  
  51. //*****************************************************************************
  52. //
  53. //! \addtogroup ssi_examples_list
  54. //! <h1>SPI Master (spi_master)</h1>
  55. //!
  56. //! This example shows how to configure the SSI0 as SPI Master.  The code will
  57. //! send three characters on the master Tx then polls the receive FIFO until
  58. //! 3 characters are received on the master Rx.
  59. //!
  60. //! This example uses the following peripherals and I/O signals.  You must
  61. //! review these and change as needed for your own board:
  62. //! - SSI0 peripheral
  63. //! - GPIO Port A peripheral (for SSI0 pins)
  64. //! - SSI0Clk - PA2
  65. //! - SSI0Fss - PA3
  66. //! - SSI0Rx  - PA4
  67. //! - SSI0Tx  - PA5
  68. //!
  69. //! The following UART signals are configured only for displaying console
  70. //! messages for this example.  These are not required for operation of SSI0.
  71. //! - UART0 peripheral
  72. //! - GPIO Port A peripheral (for UART0 pins)
  73. //! - UART0RX - PA0
  74. //! - UART0TX - PA1
  75. //!
  76. //! This example uses the following interrupt handlers.  To use this example
  77. //! in your own application you must add these interrupt handlers to your
  78. //! vector table.
  79. //! - None.
  80. //
  81. //*****************************************************************************
  82.  
  83. //*****************************************************************************
  84. //
  85. // Number of bytes to send and receive.
  86. //
  87. //*****************************************************************************
  88. #define NUM_SSI_DATA            5
  89.  
  90. void sendReceiveSPI(uint32_t *pui32DataTx, uint32_t *pui32DataRx, uint32_t size);
  91.  
  92. //*****************************************************************************
  93. //
  94. // This function sets up UART0 to be used for a console to display information
  95. // as the example is running.
  96. //
  97. //*****************************************************************************
  98. void
  99. InitConsole(void)
  100. {
  101.     //
  102.     // Enable GPIO port A which is used for UART0 pins.
  103.     // TODO: change this to whichever GPIO port you are using.
  104.     //
  105.     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
  106.  
  107.     //
  108.     // Configure the pin muxing for UART0 functions on port A0 and A1.
  109.     // This step is not necessary if your part does not support pin muxing.
  110.     // TODO: change this to select the port/pin you are using.
  111.     //
  112.     GPIOPinConfigure(GPIO_PA0_U0RX);
  113.     GPIOPinConfigure(GPIO_PA1_U0TX);
  114.  
  115.     //
  116.     // Enable UART0 so that we can configure the clock.
  117.     //
  118.     SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
  119.  
  120.     //
  121.     // Use the internal 16MHz oscillator as the UART clock source.
  122.     //
  123.     UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
  124.  
  125.     //
  126.     // Select the alternate (UART) function for these pins.
  127.     // TODO: change this to select the port/pin you are using.
  128.     //
  129.     GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
  130.  
  131.     //
  132.     // Initialize the UART for console I/O.
  133.     //
  134.     UARTStdioConfig(0, 115200, 16000000);
  135. }
  136.  
  137. //*****************************************************************************
  138. //
  139. // Configure SSI0 in master Freescale (SPI) mode.  This example will send out
  140. // 3 bytes of data, then wait for 3 bytes of data to come in.  This will all be
  141. // done using the polling method.
  142. //
  143. //*****************************************************************************
  144. int main(void) {
  145.     uint32_t pui32DataTx[NUM_SSI_DATA];
  146.     uint32_t pui32DataRx[NUM_SSI_DATA];
  147.  
  148.     //
  149.     // Set the clocking to run directly from the external crystal/oscillator.
  150.     // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
  151.     // crystal on your board.
  152.     //
  153.     SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
  154.                    SYSCTL_XTAL_16MHZ);
  155.  
  156.     //
  157.     // Set up the serial console to use for displaying messages.  This is
  158.     // just for this example program and is not needed for SSI operation.
  159.     //
  160.     InitConsole();
  161.  
  162.     //
  163.     // Display the setup on the console.
  164.     //
  165.     UARTprintf("\n\n\n--------------------------------------\n");
  166.     if (SysCtlClockGet()/1000000 > 0)
  167.         UARTprintf("System Clock Speed %dMHz\n",SysCtlClockGet()/1000000);
  168.     else if (SysCtlClockGet()/1000 > 0)
  169.         UARTprintf("System Clock Speed %dKHz\n",SysCtlClockGet()/1000);
  170.     else
  171.         UARTprintf("System Clock Speed %dHz\n",SysCtlClockGet());
  172.  
  173.     SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
  174.     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
  175.     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
  176.  
  177.     GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_6|GPIO_PIN_7);
  178.     GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_6|GPIO_PIN_7);
  179.  
  180.     GPIOPinConfigure(GPIO_PA2_SSI0CLK);
  181.     GPIOPinConfigure(GPIO_PA3_SSI0FSS);
  182.     GPIOPinConfigure(GPIO_PA4_SSI0RX);
  183.     GPIOPinConfigure(GPIO_PA5_SSI0TX);
  184.  
  185.     // The pins are assigned as follows:
  186.     //      PA5 - SSI0Tx
  187.     //      PA4 - SSI0Rx
  188.     //      PA3 - SSI0Fss
  189.     //      PA2 - SSI0CLK
  190.     GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5 | GPIO_PIN_4 | GPIO_PIN_3 |
  191.                    GPIO_PIN_2);
  192.  
  193.     //
  194.     // Configure and enable the SSI port for SPI master mode.  Use SSI0,
  195.     // system clock supply, idle clock level low and active low clock in
  196.     // freescale SPI mode, master mode, 1MHz SSI frequency, and 8-bit data.
  197.     // For SPI mode, you can set the polarity of the SSI clock when the SSI
  198.     // unit is idle.  You can also configure what clock edge you want to
  199.     // capture data on.  Please reference the datasheet for more information on
  200.     // the different SPI modes.
  201.     //
  202.     SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0,
  203.                        SSI_MODE_MASTER, 2000000, 8);
  204.     SSIEnable(SSI0_BASE);
  205.  
  206.     while(SSIDataGetNonBlocking(SSI0_BASE, &pui32DataRx[0]));
  207.  
  208.     GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6, 0);
  209.     SysCtlDelay(SysCtlClockGet()/3000);
  210.     GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6, GPIO_PIN_6);
  211.     SysCtlDelay(SysCtlClockGet());
  212.  
  213.     pui32DataTx[0] = 0x00;
  214.     pui32DataTx[1] = 0x09;
  215.     pui32DataTx[2] = 0x80;
  216.     pui32DataTx[3] = 0x01;
  217.     pui32DataTx[4] = 0x10;
  218.  
  219.     sendReceiveSPI(pui32DataTx, pui32DataRx, 5);
  220.  
  221.     pui32DataTx[0] = 0x00;
  222.     pui32DataTx[1] = 0x09;
  223.     pui32DataTx[2] = 0x00;
  224.     pui32DataTx[3] = 0x01;
  225.     pui32DataTx[4] = 0x00;
  226.  
  227.     sendReceiveSPI(pui32DataTx, pui32DataRx, 5);
  228.  
  229.     while (1);
  230. }
  231.  
  232.  
  233. void sendReceiveSPI(uint32_t *pui32DataTx, uint32_t *pui32DataRx, uint32_t size) {
  234.     uint32_t ui32Index;
  235.  
  236.     UARTprintf("\nSending\t\t");
  237.     GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_7, 0);
  238.     for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++) {
  239.         UARTprintf("0x%02x ",pui32DataTx[ui32Index]);
  240.         SSIDataPut(SSI0_BASE, pui32DataTx[ui32Index]);
  241.     }
  242.  
  243.     while(SSIBusy(SSI0_BASE));
  244.     GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_7, GPIO_PIN_7);
  245.  
  246.     UARTprintf("\nReceiving\t");
  247.     for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++) {
  248.         SSIDataGet(SSI0_BASE, &pui32DataRx[ui32Index]);
  249.         pui32DataRx[ui32Index] &= 0x00FF;
  250.         UARTprintf("0x%02x ",pui32DataRx[ui32Index]);
  251.     }
  252.     UARTprintf("\n");
  253. }
Advertisement
Add Comment
Please, Sign In to add comment