Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*****************************************************************************
- //
- // spi_master.c - Example demonstrating how to configure SSI0 in SPI master
- // mode.
- //
- // Copyright (c) 2010-2013 Texas Instruments Incorporated. All rights reserved.
- // Software License Agreement
- //
- // Redistribution and use in source and binary forms, with or without
- // modification, are permitted provided that the following conditions
- // are met:
- //
- // Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- //
- // Redistributions in binary form must reproduce the above copyright
- // notice, this list of conditions and the following disclaimer in the
- // documentation and/or other materials provided with the
- // distribution.
- //
- // Neither the name of Texas Instruments Incorporated nor the names of
- // its contributors may be used to endorse or promote products derived
- // from this software without specific prior written permission.
- //
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- //
- // This is part of revision 2.0.1.11577 of the Tiva Firmware Development Package.
- //
- //*****************************************************************************
- #include <stdbool.h>
- #include <stdint.h>
- #include "inc/hw_memmap.h"
- #include "driverlib/gpio.h"
- #include "driverlib/pin_map.h"
- #include "driverlib/ssi.h"
- #include "driverlib/sysctl.h"
- #include "driverlib/uart.h"
- #include "utils/uartstdio.h"
- //*****************************************************************************
- //
- //! \addtogroup ssi_examples_list
- //! <h1>SPI Master (spi_master)</h1>
- //!
- //! This example shows how to configure the SSI0 as SPI Master. The code will
- //! send three characters on the master Tx then polls the receive FIFO until
- //! 3 characters are received on the master Rx.
- //!
- //! This example uses the following peripherals and I/O signals. You must
- //! review these and change as needed for your own board:
- //! - SSI0 peripheral
- //! - GPIO Port A peripheral (for SSI0 pins)
- //! - SSI0Clk - PA2
- //! - SSI0Fss - PA3
- //! - SSI0Rx - PA4
- //! - SSI0Tx - PA5
- //!
- //! The following UART signals are configured only for displaying console
- //! messages for this example. These are not required for operation of SSI0.
- //! - UART0 peripheral
- //! - GPIO Port A peripheral (for UART0 pins)
- //! - UART0RX - PA0
- //! - UART0TX - PA1
- //!
- //! This example uses the following interrupt handlers. To use this example
- //! in your own application you must add these interrupt handlers to your
- //! vector table.
- //! - None.
- //
- //*****************************************************************************
- //*****************************************************************************
- //
- // Number of bytes to send and receive.
- //
- //*****************************************************************************
- #define NUM_SSI_DATA 5
- void sendReceiveSPI(uint32_t *pui32DataTx, uint32_t *pui32DataRx, uint32_t size);
- //*****************************************************************************
- //
- // This function sets up UART0 to be used for a console to display information
- // as the example is running.
- //
- //*****************************************************************************
- void
- InitConsole(void)
- {
- //
- // Enable GPIO port A which is used for UART0 pins.
- // TODO: change this to whichever GPIO port you are using.
- //
- SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
- //
- // Configure the pin muxing for UART0 functions on port A0 and A1.
- // This step is not necessary if your part does not support pin muxing.
- // TODO: change this to select the port/pin you are using.
- //
- GPIOPinConfigure(GPIO_PA0_U0RX);
- GPIOPinConfigure(GPIO_PA1_U0TX);
- //
- // Enable UART0 so that we can configure the clock.
- //
- SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
- //
- // Use the internal 16MHz oscillator as the UART clock source.
- //
- UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
- //
- // Select the alternate (UART) function for these pins.
- // TODO: change this to select the port/pin you are using.
- //
- GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
- //
- // Initialize the UART for console I/O.
- //
- UARTStdioConfig(0, 115200, 16000000);
- }
- //*****************************************************************************
- //
- // Configure SSI0 in master Freescale (SPI) mode. This example will send out
- // 3 bytes of data, then wait for 3 bytes of data to come in. This will all be
- // done using the polling method.
- //
- //*****************************************************************************
- int main(void) {
- uint32_t pui32DataTx[NUM_SSI_DATA];
- uint32_t pui32DataRx[NUM_SSI_DATA];
- //
- // Set the clocking to run directly from the external crystal/oscillator.
- // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
- // crystal on your board.
- //
- // SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
- // SYSCTL_XTAL_4_91MHZ);
- SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_25MHZ |
- SYSCTL_OSC_MAIN);
- //
- // Set up the serial console to use for displaying messages. This is
- // just for this example program and is not needed for SSI operation.
- //
- InitConsole();
- //
- // Display the setup on the console.
- //
- UARTprintf("\n\n\n--------------------------------------\n");
- if (SysCtlClockGet()/1000000 > 0)
- UARTprintf("System Clock Speed %dMHz\n",SysCtlClockGet()/1000000);
- else if (SysCtlClockGet()/1000 > 0)
- UARTprintf("System Clock Speed %dKHz\n",SysCtlClockGet()/1000);
- else
- UARTprintf("System Clock Speed %dHz\n",SysCtlClockGet());
- SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
- SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
- SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
- SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
- GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_4);
- GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_6|GPIO_PIN_7);
- GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_6|GPIO_PIN_7);
- GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_PIN_4);
- GPIOPinConfigure(GPIO_PA2_SSI0CLK);
- GPIOPinConfigure(GPIO_PA3_SSI0FSS);
- GPIOPinConfigure(GPIO_PA4_SSI0RX);
- GPIOPinConfigure(GPIO_PA5_SSI0TX);
- // The pins are assigned as follows:
- // PA5 - SSI0Tx
- // PA4 - SSI0Rx
- // PA3 - SSI0Fss
- // PA2 - SSI0CLK
- GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5 | GPIO_PIN_4 | GPIO_PIN_3 |
- GPIO_PIN_2);
- //
- // Configure and enable the SSI port for SPI master mode. Use SSI0,
- // system clock supply, idle clock level low and active low clock in
- // freescale SPI mode, master mode, 1MHz SSI frequency, and 8-bit data.
- // For SPI mode, you can set the polarity of the SSI clock when the SSI
- // unit is idle. You can also configure what clock edge you want to
- // capture data on. Please reference the datasheet for more information on
- // the different SPI modes.
- //
- SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_3,
- SSI_MODE_MASTER, 8333, 8);
- SSIEnable(SSI0_BASE);
- while(SSIDataGetNonBlocking(SSI0_BASE, &pui32DataRx[0]));
- GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6, 0);
- SysCtlDelay(SysCtlClockGet()/3000);
- GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6, GPIO_PIN_6);
- SysCtlDelay(SysCtlClockGet());
- /*pui32DataTx[0] = 0x00;
- pui32DataTx[1] = 0x09;
- pui32DataTx[2] = 0x80;
- pui32DataTx[3] = 0x01;
- pui32DataTx[4] = 0x10;
- sendReceiveSPI(pui32DataTx, pui32DataRx, 5);*/
- /*while(1) {
- GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6, 0);
- SysCtlDelay(SysCtlClockGet()/3000000);
- GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6, GPIO_PIN_6);
- SysCtlDelay(SysCtlClockGet()/3000000);
- }*/
- pui32DataTx[0] = 0x40;
- pui32DataTx[1] = 0x1e;
- pui32DataTx[2] = 0x00;
- pui32DataTx[3] = 0x01;
- pui32DataTx[4] = 0x00;
- uint32_t i = 0;
- while (i++ < 2) {
- sendReceiveSPI(pui32DataTx, pui32DataRx, 5);
- }
- while (1);
- }
- void sendReceiveSPI(uint32_t *pui32DataTx, uint32_t *pui32DataRx, uint32_t size) {
- uint32_t ui32Index;
- UARTprintf("\nSending\t\t");
- GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0);
- for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++)
- UARTprintf("0x%02x ",pui32DataTx[ui32Index]);
- for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++)
- SSIDataPut(SSI0_BASE, pui32DataTx[ui32Index]);
- while(SSIBusy(SSI0_BASE));
- GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_PIN_4);
- UARTprintf("\nReceiving\t");
- for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++) {
- SSIDataGet(SSI0_BASE, &pui32DataRx[ui32Index]);
- pui32DataRx[ui32Index] &= 0x00FF;
- UARTprintf("0x%02x ",pui32DataRx[ui32Index]);
- }
- UARTprintf("\n");
- SysCtlDelay(SysCtlClockGet()/6);
- }
Advertisement
Add Comment
Please, Sign In to add comment