teleportboy
teleportboy

Reputation: 11

Can’t read data from the accelerometer (ISM330DLC) connected to the microcontroller (LPC824) via SPI

Problem:

The main issue is that I can't read/write the register. The sensor is connected to the **LPC824 ** controller via SPI. In the project, I use the MCUXpresso SDK

main.c:

#include "fsl_spi.h"
#include "pin_mux.h"
#include "board.h"
#include "fsl_debug_console.h"

#include <stdbool.h>

#define SPI_MASTER          SPI0
#define MASTER_CLK_SRC      kCLOCK_MainClk
#define SPI_MASTER_CLK_FREQ CLOCK_GetFreq(MASTER_CLK_SRC)
#define SPI_MASTER_BAUDRATE 500000U
#define SPI_MASTER_SSEL     kSPI_Ssel0Assert

static void SPIMasterInit(void);
static void MasterStartTransfer(uint8_t reg);
static void TransferDataCheck(void);
static void activateISM330(void);
static void writeToISM330Registr(uint8_t, uint8_t);

#define BUFFER_SIZE (2)
static uint8_t txBuffer[BUFFER_SIZE];
static uint8_t rxBuffer[BUFFER_SIZE];

int main(void)
{
    CLOCK_EnableClock(kCLOCK_Uart0);

    CLOCK_SetClkDivider(kCLOCK_DivUsartClk, 1U);

    CLOCK_EnableClock(kCLOCK_Spi0);

    BOARD_InitBootPins();
    BOARD_InitBootClocks();
    BOARD_InitDebugConsole();

    SPIMasterInit();

    activateISM330();

    MasterStartTransfer(0x0F);
    TransferDataCheck();

    while(1) {

    }

    SPI_Deinit(SPI_MASTER);
}



static void SPIMasterInit(void)
{
    spi_master_config_t userConfig = {0};
    uint32_t srcFreq               = 0U;

    SPI_MasterGetDefaultConfig(&userConfig);
    userConfig.baudRate_Bps   = SPI_MASTER_BAUDRATE;
    userConfig.sselNumber     = kSPI_Ssel0Assert;
    userConfig.clockPolarity  = kSPI_ClockPolarityActiveLow;
    userConfig.clockPhase     = kSPI_ClockPhaseFirstEdge;
    userConfig.direction      = kSPI_MsbFirst;
    userConfig.dataWidth      = kSPI_Data8Bits;
    userConfig.sselPolarity   = kSPI_SpolActiveAllLow;
    userConfig.enableLoopback = false;
    //userConfig.delayConfig.postDelay     = 0xFF;
    //userConfig.delayConfig.preDelay      = 0xFF;
    //userConfig.delayConfig.transferDelay = 0xFF;
    PRINTF("CLK FREQ: %d\n\r", SPI_MASTER_CLK_FREQ);
    srcFreq = SPI_MASTER_CLK_FREQ;
    SPI_MasterInit(SPI_MASTER, &userConfig, srcFreq);
}

static void activateISM330(void)
{
    txBuffer[0] = 0x01; // 0000 0001 first bit = r/w
    txBuffer[1] = 0x80; // 1000 0000


    spi_transfer_t xfer = {0};

    xfer.txData      = txBuffer;
    xfer.rxData      = rxBuffer;
    xfer.dataSize    = sizeof(txBuffer);
    xfer.configFlags = kSPI_EndOfTransfer | kSPI_EndOfFrame;

    SPI_MasterTransferBlocking(SPI_MASTER, &xfer);
}

static void writeToISM330Registr(uint8_t reg, uint8_t data)
{
    spi_transfer_t xfer = {0};
    for(int i = 0; i < BUFFER_SIZE; i++) {
        rxBuffer[i] = 0;
        txBuffer[i] = 0;
    }

    txBuffer[0] = (0 << 7) | reg;
    txBuffer[1] = data;

    xfer.txData = txBuffer;
    xfer.rxData = rxBuffer;
    xfer.dataSize = sizeof(txBuffer);
    xfer.configFlags = kSPI_EndOfTransfer | kSPI_EndOfFrame;

    SPI_MasterTransferBlocking(SPI_MASTER, &xfer);
}

static void MasterStartTransfer(uint8_t reg)
{
    uint32_t i          = 0U;
    spi_transfer_t xfer = {0};

    for(int i = 0; i < BUFFER_SIZE; i++) {
        rxBuffer[i] = 0;
        txBuffer[i] = 0;
    }

    txBuffer[0] = (1 << 7) | reg;

    xfer.txData      = txBuffer;
    xfer.rxData      = rxBuffer;
    xfer.dataSize    = sizeof(txBuffer);
    xfer.configFlags = kSPI_EndOfTransfer | kSPI_EndOfFrame;
    SPI_MasterTransferBlocking(SPI_MASTER, &xfer);
}

static void TransferDataCheck(void)
{
    uint32_t i = 0U;
//    PRINTF("\n\rThe TX data are: ");
//    for(i = 0; i < BUFFER_SIZE; i++) {
//        PRINTF("0x%02X ", txBuffer[i]);
//    }

    PRINTF("\n\rThe RX data are: ");
    for(i = 0; i < BUFFER_SIZE; i++) {
        PRINTF("0X%02X ", rxBuffer[i]);
    }

}

Pins initializaton:

void BOARD_InitPins(void)
{
    CLOCK_EnableClock(kCLOCK_Iocon);
    CLOCK_EnableClock(kCLOCK_Swm);

    gpio_pin_config_t dereConfig = {
            kGPIO_DigitalOutput,
            1,
    };
    GPIO_PinInit(GPIO, 0, 15U, &dereConfig);

    gpio_pin_config_t ssel = {
            kGPIO_DigitalOutput,
            0,
    };
    GPIO_PinInit(GPIO, 0, 12U, &ssel);

    gpio_pin_config_t lampochka = {
            kGPIO_DigitalOutput,
            0
    };
    GPIO_PinInit(GPIO, 0, 0, &lampochka);

    SWM_SetMovablePinSelect(SWM0, kSWM_USART0_TXD, kSWM_PortPin_P0_14);
    //SWM_SetMovablePinSelect(SWM0, kSWM_USART0_RTS, kSWM_PortPin_P0_15);
    SWM_SetMovablePinSelect(SWM0, kSWM_USART0_RXD, kSWM_PortPin_P0_11);
    SWM_SetMovablePinSelect(SWM0, kSWM_SPI0_SCK, kSWM_PortPin_P0_13);
    SWM_SetMovablePinSelect(SWM0, kSWM_SPI0_MOSI, kSWM_PortPin_P0_23);
    SWM_SetMovablePinSelect(SWM0, kSWM_SPI0_MISO, kSWM_PortPin_P0_17);
    SWM_SetMovablePinSelect(SWM0, kSWM_SPI0_SSEL0, kSWM_PortPin_P0_12);

    CLOCK_DisableClock(kCLOCK_Swm);
}

I've tried various ways to configure and initialize the SPI, because in my opinion, that's where the problem lies, but to no avail. I send a command to the accelerometer according to the datasheet, for example, requesting to read the WHO_AM_I register, but I get garbage in response [0xFF, 0x88]. The root of the problem might be elsewhere, but I have no ideas on how to identify it. SPI bus inteface description from datasheet of ism330dlc

Upvotes: 0

Views: 101

Answers (0)

Related Questions