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.

TM4C123G SPI problem

Hi,

I'm using TM4C123G for reading register (data ) through SPI from a BMP280 Barometric Pressure Sensor. I've tried almost everything, but nothing works. It seems that TM4c123g does not receive anything. I post my code here. What ist wrong?

/*
 * main.c
 */

#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "driverlib/ssi.h"
#include "driverlib/ssi.c"

enum
 {
   BMP280_REG_RESULT_PRESSURE         = 0xF7,
   BMP280_REG_RESULT_TEMPRERATURE     = 0xFA,

   BMP280_COMMAND_TEMPERATURE         = 0x2E,
   BMP280_COMMAND_PRESSURE0           = 0x25,
   BMP280_COMMAND_PRESSURE1           = 0x29,
   BMP280_COMMAND_PRESSURE2           = 0x2D,
   BMP280_COMMAND_PRESSURE3           = 0x31,
   BMP280_COMMAND_PRESSURE4           = 0x5D,

   SLAVE_ADDRESS 					   = 0x76,

   BMP280_REGISTER_DIG_T1             = 0x88,
   BMP280_REGISTER_DIG_T2             = 0x8A,
   BMP280_REGISTER_DIG_T3             = 0x8C,

   BMP280_REGISTER_DIG_P1             = 0x8E,
   BMP280_REGISTER_DIG_P2             = 0x90,
   BMP280_REGISTER_DIG_P3             = 0x92,
   BMP280_REGISTER_DIG_P4             = 0x94,
   BMP280_REGISTER_DIG_P5             = 0x96,
   BMP280_REGISTER_DIG_P6             = 0x98,
   BMP280_REGISTER_DIG_P7             = 0x9A,
   BMP280_REGISTER_DIG_P8             = 0x9C,
   BMP280_REGISTER_DIG_P9             = 0x9E,
   BMP280_REGISTER_CHIPID             = 0xD0,
   BMP280_REGISTER_VERSION            = 0xD1,
   BMP280_REGISTER_SOFTRESET          = 0xE0,
   BMP280_REGISTER_CAL26              = 0xE1,  // R calibration stored in 0xE1-0xF0
   BMP280_REGISTER_CONTROL            = 0xF4,
   BMP280_REGISTER_CONFIG             = 0xF5,
   BMP280_REGISTER_PRESSUREDATA       = 0xF7,
   BMP280_REGISTER_TEMPDATA           = 0xFA,
};



void initSPI();

void SPIWrite(uint32_t SPI_BASE , uint16_t SPI_WAdres ,uint16_t SPI_Wdata );
int32_t SPIRead(uint32_t SPI_BASE , uint32_t SPI_RAdres );

int32_t TEST;

void main(void){
	SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
	initSPI();
	SysCtlDelay(40000);
	while(1){

		TEST = SPIRead(SSI0_BASE,0x89);


		}
	}

void initSPI(void)
{

	// SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |SYSCTL_XTAL_8MHZ);
	// Enable the SSI0 peripheral
	SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
	// The SSI0 peripheral is on Port A and pins 2,3,4 and 5.
	// Enable GPIO port A
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
	uint32_t clockk=SysCtlClockGet();
	SysCtlClockGet();
	SSIConfigSetExpClk(SSI0_BASE, SysCtlClockGet(), SSI_FRF_TI, SSI_MODE_MASTER, 500000, 8);
	// This function/s configures the pin muxing on port A pins 2,3,4 and 5
	// It uses the values DEFINED at the top of the program and takes 1 parameter
	GPIOPinConfigure(GPIO_PA2_SSI0CLK);
	GPIOPinConfigure(GPIO_PA3_SSI0FSS);
	GPIOPinConfigure(GPIO_PA4_SSI0RX);
	GPIOPinConfigure(GPIO_PA5_SSI0TX);
	SysCtlDelay(4);
	// Configures the pins for use by the SSI, takes 2 parameters
	GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5 | GPIO_PIN_4 | GPIO_PIN_3 |
	GPIO_PIN_2);
	SysCtlDelay(4);
	// Configures the SSI port for SPI master mode, it takes 6 parameters
	//SSIConfigSetExpClk(SSI0_BASE, 2000000 , SSI_FRF_MOTO_MODE_3, SSI_MODE_MASTER, 1600 , 8 );

	GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_3);
	GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0xFF);
	// Enables the SSI0 module
	SSIEnable(SSI0_BASE);
	SysCtlDelay(4);
	uint32_t scrap;
	while(SSIDataGetNonBlocking(SSI0_BASE, &scrap));
}

void SPIWrite(uint32_t SPI_BASE , uint16_t SPI_WAdres ,uint16_t SPI_Wdata ){
//uint32_t send_data=0;
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0x00);
SSIDataPut(SPI_BASE, SPI_WAdres);
while(SSIBusy(SPI_BASE));
SSIDataPut(SPI_BASE, SPI_Wdata);
while(SSIBusy(SPI_BASE));
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0xff);
}

int32_t SPIRead(uint32_t SPI_BASE , uint32_t SPI_RAdres ){
int32_t receive_data_adress=0,values=0;
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0x00);
receive_data_adress=SPI_RAdres|0x80;
SSIDataPut(SPI_BASE, receive_data_adress);
while(SSIBusy(SPI_BASE));
SSIDataPut(SPI_BASE, 0xff);
while(SSIBusy(SPI_BASE));
//SSIDataGet(SPI_BASE, &values);
while(SSIDataGetNonBlocking(SPI_BASE, &values));

GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0xff);
return values;
}

  • Hello First_Last9

    When using the SSI controller, a write must always be followed by a Read to flush the FIFO. Otherwise what you may be reading during the Read operation is the data byte from the First write operation that was shifted in.

    Also do check the physical pins to see if the data transaction between the two devices is happening.