2015-09-17 04:36 AM
I am using STM32L152 Nucleo (Nucleo L152RE) with ALS sensor connected to it.ALS sensor has a 16 bit data register containing both MSB and LSB .
Below is the register Map 0x04 15:8 MSB Data of whole ALS 16bits 7:0 LSB Data of whole ALS 16bits Read :S-Slave-Address-Wr-A-Command-Code-A-DataLSB-A-DataMSB-A-PWrite :
S-Slave-Address-Wr-A-Command-Code-A-S-Slave-Address-Rd-A-Data LSB-A-Data-MSB-A-Pwhere : S-StartP-StopA-AckBelow is the code Which I am using to read the registers MSB and LSB but I am always getting MSB as 0xFF while LSB shows some data which I believe is correct.union { uint8_t ubuff8[2]; uint16_t ubuff16; } buffer; uint8_t const length = 2U; // Do a block read of sensor data status = als_sensor_read_raw_register(0x04, buffer.ubuff8, length); uint als_sensor_read_raw_register(uint8_t const register_address,uint8_t * buffer,uint8_t length) { als_buffer[0] = register_address; //0x04 pBuffer = als_buffer; uint8_t len = 1; if ((2 == length)) { if( HAL_I2C_Master_Transmit(&hi2c1, ALS_ADDRESS_8_BIT, (uint8_t*)pBuffer,len, 1) == HAL_OK) { if( HAL_I2C_Master_Receive(&hi2c1, ALS_ADDRESS_8_BIT, (uint8_t*)buffer,length, 1) == HAL_OK) { status = true; // data1 = buffer[0]; //for debugging - shows correct data // data2 = buffer[1]; //for debugging - shows 0xFF always } else { status = false; } } } }I think the reason for this is The smt32l1xx library in the HAL_Transmit is sending a stop in the library and I am using the same Transmit for both read and write/* Wait until TXE flag is set */
if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_TXE, RESET, Timeout) != HAL_OK)
{
return HAL_TIMEOUT;
}
/* Generate Stop */
SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
/* Wait until BUSY flag is reset */
if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_BUSY, SET, Timeout) != HAL_OK)
{It will work for write but not for multibyte read .Am I correct?.Can some one please review this and tell me what I am doing wrong here.How can I read the 16 bit register(containing both LSB and MSB data) of the Sensor in STM32l152 or STM32 Nucleo.With the same code Sensor having different 8 bit LSB and MSB data register I can read both the MSB and LSB data correctly.Please assist.Thanks in advanceRp #stm32-nucleo #nucleo #stm32l152