AnsweredAssumed Answered

STM32l4, SPI, LIS3DH Accelerometer

Question asked by regnier.pierre on Nov 30, 2015
Latest reply on Mar 3, 2016 by crisan.marius
Hi !
My name is Pierre. I m a new user of STM32L4 and the library HAL.
I m trying to establish a SPI connexion between the accelerometer LIS3DH and the STM32. The problem is that i can t read the registers of the accelerometer. When i try to read them, i have no signal on my MISO/SDO signal
SPI_lis3dh.PNG

These are the signals : 
       
  • Purple : MOSI : First transmission the 8 first bits are the adress. The 8 next bits i write 0x80 on the register. For the second transmission, i write a register adress and put the first bit high to read the register the next 8 bits on the MISO.
  •    
  • Green : CLOCK
  •    
  • Yellow : Slave Select
  •    
  • Blue : MISO, no signal.
This is my soft : 


/* Includes ------------------------------------------------------------------*/
#include "main.h"

enum {
     TRANSFER_WAIT,
     TRANSFER_COMPLETE,
     TRANSFER_ERROR
};

/* SPI handler declaration */
SPI_HandleTypeDef hspi;
volatile HAL_StatusTypeDef SPI_status;
/* Buffer used for transmission */
#define aTxBufferAdresse_registre1 ((uint8_t)0x20)
#define aTxBufferConfig_registre1 ((uint8_t)0x1F)

#define aTxBufferAdresse_registre2 ((uint8_t)0x21)
#define aTxBufferConfig_registre2 ((uint8_t)0x01)

#define aTxBufferAdresse_registre3 ((uint8_t)0x22)
#define aTxBufferConfig_registre3 ((uint8_t)0x00)

#define aTxBufferAdresse_registre4 ((uint8_t)0x23)
#define aTxBufferConfig_registre4 ((uint8_t)0x08)

#define aTxBufferAdresse_registre5 ((uint8_t)0x24)
#define aTxBufferConfig_registre5 ((uint8_t)0x80)

uint8_t aTxBufferRead_registre1 = 0xA0; // lire à l'adresse (0x20) du registre 1 : 10100000 (le 1er bit est un bit de lecture)
uint8_t aTxBufferRead_registre2 = 0xA1; // lire à l'adresse (0x21) du registre 2 : 00100010  (le 1er bit est un bit de lecture)
uint8_t aTxBufferRead_registre3 = 0xA2;
uint8_t aTxBufferRead_registre4 = 0xA3;
uint8_t aTxBufferRead_registre5 = 0xA4;

#define  AddrRead ((uint8_t)0x8F)
#define  AddrRead2 ((uint8_t)0x00)

uint8_t aTxBufferRead_value[2] ={AddrRead, AddrRead2};
uint8_t TrX_registre1 [2]  = {aTxBufferAdresse_registre1, aTxBufferConfig_registre1};
uint8_t TrX_registre2 [2]  = {aTxBufferAdresse_registre2, aTxBufferConfig_registre2};
uint8_t TrX_registre3 [2]  = {aTxBufferAdresse_registre3, aTxBufferConfig_registre3};
uint8_t TrX_registre4 [2]  = {aTxBufferAdresse_registre4, aTxBufferConfig_registre4};
uint8_t TrX_registre5 [2]  = {aTxBufferAdresse_registre5, aTxBufferConfig_registre5};

/* transfer state */
__IO uint32_t wTransferState = TRANSFER_WAIT;

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void Error_Handler(void);

/* Private functions ---------------------------------------------------------*/
int main(void)
{
     uint8_t Dataread = 0;
  HAL_Init();

  SystemClock_Config();

  HAL_SPI_MspInit(&hspi);
  if (HAL_SPI_Init(&hspi) != HAL_OK)
  {
       SPI_status = HAL_ERROR;
  }

  /* Infinite loop */
  while (1)
  {
       HAL_GPIO_WritePin( GPIOE , GPIO_PIN_12 , GPIO_PIN_RESET );
       HAL_SPI_Transmit_IT(&hspi, &TrX_registre5, 2);
       HAL_GPIO_WritePin( GPIOE , GPIO_PIN_12 , GPIO_PIN_RESET );
       HAL_SPI_Transmit_IT(&hspi, &aTxBufferRead_value, 2);
       HAL_Delay(1);
  }
}

void SystemClock_Config(void)
{
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};

  /* MSI is enabled after System reset, activate PLL with MSI as source */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 40;
  RCC_OscInitStruct.PLL.PLLR = 2;
  RCC_OscInitStruct.PLL.PLLP = 7;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    /* Initialization Error */
    while(1);
  }

  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
     clocks dividers */
  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    /* Initialization Error */
    while(1);
  }
}

void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi) {

     __HAL_RCC_SPI1_CLK_ENABLE();
     __HAL_RCC_GPIOE_CLK_ENABLE();

     GPIO_InitTypeDef GPIO_InitStruct;
     GPIO_InitStruct.Pin = GPIO_PIN_12;
     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
     GPIO_InitStruct.Pull = GPIO_NOPULL;
     GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
     HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

     GPIO_InitStruct.Pin =  GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15; //SCK MISO MOSI
     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
     GPIO_InitStruct.Pull = GPIO_PULLDOWN;
     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
     GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
     HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

     HAL_NVIC_SetPriority(SPI1_IRQn, 1, 1);
     HAL_NVIC_EnableIRQ(SPI1_IRQn);

     hspi->Instance = SPI1;
     hspi->Init.Mode = SPI_MODE_MASTER;
     hspi->Init.Direction = SPI_DIRECTION_2LINES;
     hspi->Init.DataSize = SPI_DATASIZE_8BIT;
     hspi->Init.CLKPolarity = SPI_POLARITY_LOW;
     hspi->Init.CLKPhase = SPI_PHASE_1EDGE;
     hspi->Init.NSS = SPI_NSS_SOFT;
     hspi->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
     hspi->Init.FirstBit = SPI_FIRSTBIT_MSB;
     hspi->Init.TIMode = SPI_TIMODE_DISABLE;
     hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

}

void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
{
       HAL_GPIO_WritePin( GPIOE , GPIO_PIN_12 , GPIO_PIN_SET );
}

void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
{
       HAL_GPIO_WritePin( GPIOE , GPIO_PIN_12 , GPIO_PIN_SET );
}

void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
{
      HAL_GPIO_WritePin( GPIOE , GPIO_PIN_12 , GPIO_PIN_SET );
}

static void Error_Handler(void)
{
  while(1)
  {
  }
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */


  /* Infinite loop */
  while (1)
  {
  }
}
#endif

Outcomes