This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2538 SPI Data Communication

Other Parts Discussed in Thread: CC2538

Hi, all

CC2538 is in the master mode and a mems sensor as the slave. I want to write data through MOSI line to sensor and get data from MISO line. I want to use the peripheral driver library to program my own code, but it is difficult to complete it. I plan to use the function SSIDataGet and SSIDataPut to receive data and transmit data. I will write 0x001 to the sensor and the sensor will send the data automatically.

I plan to change the example code for "ssi master". I am a beginner and the project will due, please help me .

Thanks a lot.


#include <stdbool.h>
#include <stdint.h>
#include "hw_memmap.h"
#include "hw_ioc.h"
#include "gpio.h"
#include "ioc.h"
#include "ssi.h"
#include "uart.h"
#include "sys_ctrl.h"
#include "uartstdio.h"
#include "sensor.h"


//*****************************************************************************
                                                         //   CC2538    |   SCC1300-D02PWB
                                                        //  --------- --|------------------
#define EXAMPLE_PIN_SSI_CLK             GPIO_PIN_2      //    CLK       |      J2-6 SCK
#define EXAMPLE_PIN_SSI_FSS             GPIO_PIN_3      //    CSB       |      J2-3 CSB_GYRO
#define EXAMPLE_PIN_SSI_RX              GPIO_PIN_4      //    MOSI      |      J2-5 MOSI
#define EXAMPLE_PIN_SSI_TX              GPIO_PIN_5      //    MISO      |      J2-4 MISO
#define EXAMPLE_GPIO_SSI_BASE           GPIO_A_BASE
   
#define EXAMPLE_PIN_UART_RXD            GPIO_PIN_0
#define EXAMPLE_PIN_UART_TXD            GPIO_PIN_1
#define EXAMPLE_GPIO_UART_BASE          GPIO_A_BASE

#define ADRESS_WORD_GYRO                0x0001
#define ZERO_VECTOR                     0x0001

//*****************************************************************************

void
InitConsole(void)
{
    //
    // Map UART signals to the correct GPIO pins and configure them as
    // hardware controlled.
    //
    IOCPinConfigPeriphOutput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_TXD,
                             IOC_MUX_OUT_SEL_UART0_TXD);
    GPIOPinTypeUARTOutput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_TXD);
   
    IOCPinConfigPeriphInput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_RXD,
                            IOC_UARTRXD_UART0);
    GPIOPinTypeUARTInput(EXAMPLE_GPIO_UART_BASE, EXAMPLE_PIN_UART_RXD);
    
    //
    // Initialize the UART (UART0) for console I/O.
    //
    UARTStdioInit(0);
}

//*****************************************************************************
//
// 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)
{


    // Set the clocking to run directly from the external crystal/oscillator.
    // (no ext 32k osc, no internal osc)
    //
    SysCtrlClockSet(false, false, SYS_CTRL_SYSDIV_32MHZ);

    //
    // Set IO clock to the same as system clock
    //
    SysCtrlIOClockSet(SYS_CTRL_SYSDIV_32MHZ);
  
    //
    // 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();

   
    //*************  Step 1 ******************** P123

    SysCtrlPeripheralEnable(SYS_CTRL_PERIPH_SSI0);

    //
    // Disable SSI function before configuring module
    //
    SSIDisable(SSI0_BASE);

    //
    // Set IO clock as SSI clock source
    //
    SSIClockSourceSet(SSI0_BASE, SSI_CLOCK_PIOSC);

    //
    // For this example SSI0 is used with PA2-PA5.  The actual port and pins
    // used may be different on your part, consult the data sheet for more
    // information.  GPIO port A needs to be enabled so these pins can be used.
    // TODO: change this to whichever GPIO port you are using.
    //
    // Configure the pin muxing for SSI0 functions on port A2, A3, A4, and A5.
    // This step is not necessary if your part does not support pin muxing.
    // TODO: change this to select the port/pin you are using.
    //

    IOCPinConfigPeriphOutput(EXAMPLE_GPIO_SSI_BASE, EXAMPLE_PIN_SSI_CLK,
                             IOC_MUX_OUT_SEL_SSI0_CLKOUT);   

    IOCPinConfigPeriphOutput(EXAMPLE_GPIO_SSI_BASE, EXAMPLE_PIN_SSI_FSS,
                             IOC_MUX_OUT_SEL_SSI0_FSSOUT);
   
    IOCPinConfigPeriphOutput(EXAMPLE_GPIO_SSI_BASE, EXAMPLE_PIN_SSI_TX,
                             IOC_MUX_OUT_SEL_SSI0_TXD);   
   
    IOCPinConfigPeriphInput(EXAMPLE_GPIO_SSI_BASE, EXAMPLE_PIN_SSI_RX,
                            IOC_SSIRXD_SSI0);   
    //
    // Configure the GPIO settings for the SSI pins.  This function also gives
    // control of these pins to the SSI hardware.  Consult the data sheet to
    // see which functions are allocated per pin.
    // The pins are assigned as follows:
    //      PA5 - SSI0Tx
    //      PA4 - SSI0Rx
    //      PA3 - SSI0Fss
    //      PA2 - SSI0CLK
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinTypeSSI(EXAMPLE_GPIO_SSI_BASE, EXAMPLE_PIN_SSI_CLK |
                   EXAMPLE_PIN_SSI_FSS | EXAMPLE_PIN_SSI_RX |
                   EXAMPLE_PIN_SSI_TX);
    GPIOPinTypeSSI(EXAMPLE_GPIO_SSI_BASE, EXAMPLE_PIN_SSI_CLK |
                   EXAMPLE_PIN_SSI_RX | EXAMPLE_PIN_SSI_TX);

    //
    // Configure SSI module to Motorola/Freescale SPI mode 1:
    // Polarity  = 0, SCK steady state is high
    // Phase     = 0, Data changed on first and captured on second clock edge
    // Word size = 16 bits
    //
    SSIConfigSetExpClk(SSI0_BASE, SysCtrlIOClockGet(), SSI_FRF_MOTO_MODE_0,
                       SSI_MODE_MASTER, SysCtrlClockGet()/2, 16);
   
    //
    // Enable the SSI0 module.
    //
    SSIEnable(SSI0_BASE);

    //
    // Read any residual data from the SSI port.  This makes sure the receive
    // FIFOs are empty, so we don't read any unwanted junk.  This is done here
    // because the SPI SSI mode is full-duplex, which allows you to send and
    // receive at the same time.  The SSIDataGetNonBlocking function returns
    // "true" when data was returned, and "false" when no data was returned.
    // The "non-blocking" function checks if there is any data in the receive
    // FIFO and does not "hang" if there isn't.
   


     
    //
    // Display indication that the SSI is transmitting data.
    //
   //UARTprintf("Sent:\n  ",*(uint32_t*) pui32Data);
  
}

  • Hi Tao,

    Have you run the original spi_master.c without any modification? Does it run? Basically your code is the same as the original except UART function's being commented out, so it should work.

    Can you please elaborate on exactly what kind of problem you are running into?

    - Cetri

  • Hi Tao,

    Also, please check lcd_srf06eb.c out as it is using SPI master to control the LCD and therefore can be a good example for your application.

    - Cetri