2016-11-14 01:47 PM
I am using an STM32VLDISCOVERY with an accelerometer which is connected using SPI. I am trying to determine whether or not I have it connected to the STM correctly so I am trying to read its Device ID through the terminal of the IDE I am using (Atollic TrueStudio) but I cannot figure out how to make it work. I have the STM connected via USB but cannot seem to make the STM function as a COM port.
Can somebody please point me in the right direction? Thanks.2016-11-15 04:00 AM
Hi,
Refer to the “Device electronic signature� section, “unique device ID register (96 bits)� paragraph in your related reference manual to have idea about the unique device identifier.You can refer to this manual : Developing your STM32VLDISCOVERY applicationusing the Atollic TrueSTUDIO® software.Hope this helps you.Regards2016-11-22 02:42 PM
Hey thanks for your reply (sorry for my own delay!).
I'm still confused about this. I'm really not sure how to go about reading the register, it isn't something I've had to do. I've only used the STM32 functionally, if you know what I mean. If you could point me a bit more in the direction I would really appreciate it.2016-11-22 08:55 PM
So you're not looking at the STM32 Unique ID, but rather a response from an SPI device that identifies it.
Perhaps you should expand a little on exactly what part it is you are using, and how it is connected, ie pins, SPI#, etc. Review some of the SPI FLASH examples for this and other STM32 family parts.2016-11-23 10:40 AM
So it's SPI1 I'm using. For that, I have PA5 connected as SCK, PA6 as MISO, PA7 as MOSI, PA0 is Int1 (I intend to use the botton as the interrupt), and PA4 is NSS. The device I'm using is the ADXL362z, and I've looked at the data sheet for the device ID, but it doesn't say anything about how I should go about accessing it... I'm very new to all this so even tasks which appear simple take a long time to figure out for me
2016-11-23 12:32 PM
The document does go into some detail
http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL362.pdf
You'd need to send the byte sequence 0x0B,0x02,0xFF, the 0xFF is a dummy byte, but the Part ID should be returned, the 0x02 is the PARTID register number.2016-11-23 12:40 PM
Something like this should work,
#define ACCEL_SPI SPI1
#define ACCEL_DUMMY_BYTE 0xFF
/**
* @brief Select ACCEL: Chip Select pin low
*/
#define ACCEL_CS_LOW() GPIO_ResetBits(GPIOA, GPIO_Pin_4)
/**
* @brief Deselect ACCEL: Chip Select pin high
*/
#define ACCEL_CS_HIGH() GPIO_SetBits(GPIOA, GPIO_Pin_4)
/**
* @brief Initializes the peripherals used by the SPI ACCEL driver.
* @param None
* @retval None
*/
void ACCEL_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
ACCEL_LowLevel_Init(); /* Pins */
/*!< Deselect the ACCEL: Chip Select high */
ACCEL_CS_HIGH();
/*!< SPI configuration */
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_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(ACCEL_SPI, &SPI_InitStructure);
/*!< Enable the ACCEL_SPI */
SPI_Cmd(ACCEL_SPI, ENABLE);
}
/**
* @brief Sends a byte through the SPI interface and return the byte received
* from the SPI bus.
* @param byte: byte to send.
* @retval The value of the received byte.
*/
uint8_t ACCEL_SendByte(uint8_t byte)
{
/*!< Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(ACCEL_SPI, SPI_I2S_FLAG_TXE) == RESET);
/*!< Send byte through the SPI1 peripheral */
SPI_I2S_SendData(ACCEL_SPI, byte);
/*!< Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(ACCEL_SPI, SPI_I2S_FLAG_RXNE) == RESET);
/*!< Return the byte read from the SPI bus */
return SPI_I2S_ReceiveData(ACCEL_SPI);
}
/**
* @brief Reads ACCEL identification.
* @param None
* @retval ACCEL identification
*/
uint8_t ACCEL_ReadID(void)
{
uint8_t Temp = 0;
/*!< Select the ACCEL: Chip Select low */
ACCEL_CS_LOW();
/*!< Send read register instruction */
ACCEL_SendByte(0x0B);
/*!< Send PARTID register */
ACCEL_SendByte(0x02);
/*!< Read a byte from the ACCEL */
Temp = ACCEL_SendByte(ACCEL_DUMMY_BYTE);
/*!< Deselect the ACCEL: Chip Select high */
ACCEL_CS_HIGH();
return Temp;
}
This is a quick blind port, you'd need to configure the SPI pins, and the chip select pin (PA4) as a GPIO
2016-11-23 03:29 PM
Man, that's class. Thank you so much.
I'm just womdering why I need to send those bytes in that order. Is it not enough to input the register I'm looking for in order to get it back? Why is it not enough to put in 0x00, for example, to get DEVID_ID?2016-11-24 07:08 AM
Actually, I should alter my question. I get that the first byte sent, 0x0B lets me read the register, then the second byte, 0x02, specifies what register to read. Why then do I need to send the dummy variable, 0xFF?
2016-11-24 07:36 AM
The SPI traffic is symmetrical, you clock data in, data clocks out. After you have clocked the 0x02 into the chip, you have to clock the register content out.
The 0xFF is sent to generate the clocks. It could be any value.