Trouble about Configurating LIS302DL on STM32F4discovery

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
kenji
Posts: 2
Joined: Tue Jul 02, 2019 9:28 am

Trouble about Configurating LIS302DL on STM32F4discovery

Post by kenji »

Hi, everyone.

Trying to use LIS302DL embedded on STM32F4discovery, I'm writing a code but it doesn't work.
That is, I wrote the following code and I execute "int hoge = Accel_Read(LIS302DL_ADDRESS_WHOAMI);" in the main function after calling config_SPI(), but Accel_Read() does not return 0x3b, which is a response of "who am I" described in the datasheet.

I checked outputs of PA5,PA6,PA7 using an oscilloscope.
PA5( SCK Pin ) takes always low voltage and hence I'm guessing initializing SPI feature is not fine.
Could you point out me which part is not fine?

I'm really in trouble...

Code: Select all

#include <stm32f4xx.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_spi.h>

//STM32F4---LIS-302DL connection
//PA5---14 SCL/SPC      SPC=SPI Serial Port Clock
//PA6---12 SDO          SDO=SPI Serial Data Output
//PA7---13 SDA/SDI/SDO  SDI=SPI Serial Data Input
//PE0--- 8 INT1         INT1=Intertial interrupt 1
//PE1--- 9 INT2         INT2=Intertial interrupt 2
//PE3--- 7 CS_I2C/SPI    CS=SPI enable

//Pin-feauture on STM32F4discovery
//PA5---SPI1_SCK
//PA6---SPI1_MISO
//PA7---SPI1_MOSI

#define SPI1_NSS_Pin      GPIO_Pin_3 //GPIOE
#define SPI1_SCK_Pin      GPIO_Pin_5 //GPIOA
#define SPI1_MISO_Pin      GPIO_Pin_6 //GPIOA
#define SPI1_MOSI_Pin      GPIO_Pin_7 //GPIOA

//LIS302DL address
#define LIS302DL_ADDRESS_WHOAMI 0x0f
#define LIS302DL_ADDRESS_OUTX 0x29
#define LIS302DL_ADDRESS_OUTY 0x2B
#define LIS302DL_ADDRESS_OUTZ 0x2D

uint8_t SPI_Send(uint8_t Data)
{
   uint16_t RxData;
   while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);      // wait until transmit complete
   SPI_I2S_SendData(SPI1, Data);                              // write data to be transmitted to the SPI data register
   while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);   // wait until receive complete
   RxData = SPI_I2S_ReceiveData(SPI1);                        // return received data from SPI data register
   return (uint8_t)RxData;
}

void SPI_Select()
{
   GPIO_ResetBits(GPIOE,SPI1_NSS_Pin);
}

void SPI_Deselect()
{
   GPIO_SetBits(GPIOE,SPI1_NSS_Pin);
}

void Accel_Write(uint8_t add, uint8_t val)
{
   SPI_Select();
   uint8_t RxData;

   RxData = SPI_Send(0b10000000 | add);
   RxData = SPI_Send(val);

   SPI_Deselect();
}

uint8_t Accel_Read(uint8_t add)
{
   SPI_Select();
   uint8_t RxData;

   RxData = SPI_Send(0b00000000 | add);
   RxData = SPI_Send(0x0);

   SPI_Deselect();

   return RxData;
}

//SPIinit
void config_SPI(){
   //clock enable
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

   //GPIO configure
   GPIO_InitTypeDef GPIO_InitStructure;
   GPIO_InitStructure.GPIO_Pin                = SPI1_SCK_Pin | SPI1_MISO_Pin | SPI1_MOSI_Pin;
   GPIO_InitStructure.GPIO_Mode        = GPIO_Mode_AF;
   GPIO_InitStructure.GPIO_Speed        = GPIO_Speed_100MHz;
   GPIO_InitStructure.GPIO_OType        = GPIO_OType_PP;
   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
   GPIO_Init(GPIOA, &GPIO_InitStructure);
   GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
   GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
   GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

   GPIO_StructInit(&GPIO_InitStructure);
   GPIO_InitStructure.GPIO_Pin = SPI1_NSS_Pin;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
   GPIO_Init(GPIOE, &GPIO_InitStructure);
   SPI_Deselect();

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
   GPIO_Init(GPIOE, &GPIO_InitStructure);


   /* SPI1   configuration */
   SPI_InitTypeDef SPI_InitStructure;
   SPI_I2S_DeInit(SPI1);
   SPI_InitStructure.SPI_Direction         = SPI_Direction_2Lines_FullDuplex;
   SPI_InitStructure.SPI_Mode            = SPI_Mode_Master;
   SPI_InitStructure.SPI_DataSize         = SPI_DataSize_8b;
   SPI_InitStructure.SPI_CPOL            = SPI_CPOL_Low;
   SPI_InitStructure.SPI_CPHA            = SPI_CPHA_1Edge;
   SPI_InitStructure.SPI_NSS            = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
   SPI_InitStructure.SPI_BaudRatePrescaler   = SPI_BaudRatePrescaler_128;
   SPI_InitStructure.SPI_CRCPolynomial = 7;
   SPI_InitStructure.SPI_FirstBit         = SPI_FirstBit_MSB;
   SPI_Init(SPI1, &SPI_InitStructure);
   SPI_Cmd(SPI1, ENABLE);
}
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Trouble about Configurating LIS302DL on STM32F4discovery

Post by Giovanni »

Hi,

This forum is about ChibiOS, for support regarding ST libraries the ST forum would be more appropriate.

Anyway, that MEMS is supported in ChibiOS/EX already, you may at least look at the existing driver and demo under: /testex/STM32/STM32F4xx/SPI-LIS302DL

Giovanni
kenji
Posts: 2
Joined: Tue Jul 02, 2019 9:28 am

Re: Trouble about Configurating LIS302DL on STM32F4discovery

Post by kenji »

Thanks for your very quick reply and sorry for my late response and improper post.

Thanks to your advice and many try and error, my device now works.
It looks that PA4 and PA5 were used by other peripherals.
Post Reply