How to configure SPI correctly? LPC824 and accelerometer(ISM330DLC). MCU Xpresso SDK

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

How to configure SPI correctly? LPC824 and accelerometer(ISM330DLC). MCU Xpresso SDK

1,165件の閲覧回数
teleportboy
Contributor I

Problem:

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

teleportboy_2-1698136812508.png

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(); // setup ctrl reg in ism330dlc

    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);
}
//setup ctrl registr
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 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);
}

 

ラベル(1)
タグ(1)
0 件の賞賛
返信
1 返信

1,133件の閲覧回数
Pavel_Hernandez
NXP TechSupport
NXP TechSupport

Hello, my name is Pavel, and I will be supporting your case, I reviewed your information and in main there is a function that the core never calls (SPI_Deinit). I recommend reviewing the frequency if it is the same as the "accelerometer" need if the CS is in the same form, I mean the Idel. Maybe could use a logic analyzer to get more details.

I suggest reviewing the example on the SDK, you could download it from this page, 

Dashboard | MCUXpresso SDK Builder (nxp.com) 

Pavel_Hernandez_0-1698183431129.png

There are many examples available that you could modify to your application.

Best regards,
Pavel

0 件の賞賛
返信