I'm trying to use the BME280 via an STM8L101F3. However I cannot seem to get any data from the sensor. As a start I would like to read the chip ID to verify that everything is working. However, I have not found a suitable library that works with the STM8L10x and I have been unable to create my own. I have the following code but all I receive are zero's. I have tried everything I could find online but I haven't progressed. I have checked that the sensor is functioning with an arduino and I'm using 4-wire spi.
#include "stm8l10x.h"
#include "bme280.h"
void DelayS(uint16_t sec) {
uint32_t freq = 400000;
for (; sec != 0; sec--) {
freq = 400000;
for (; freq != 0; freq--) {
}
}
}
static void SPI_Config(void)
{
/*Set the MOSI,MISO and SCK at high level*/
GPIO_ExternalPullUpConfig(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7, ENABLE);
// Disable SPI before configuration
SPI_DeInit();
/* Initialize SPI in Slave mode */
SPI_Init(SPI_FirstBit_MSB, SPI_BaudRatePrescaler_8, SPI_Mode_Master, SPI_CPOL_Low,
SPI_CPHA_1Edge, SPI_Direction_2Lines_FullDuplex, SPI_NSS_Soft);
}
void main(void)
{
volatile uint8_t received = 33;
volatile uint8_t sendByte = 0xD0; // ChipID register
/* High speed internal clock prescaler: 1*/
CLK_MasterPrescalerConfig(CLK_MasterPrescaler_HSIDiv1);
// Disable SPI for configuration
SPI_Cmd(DISABLE);
/* Enable SPI clock */
CLK_PeripheralClockConfig(CLK_Peripheral_SPI, ENABLE);
SPI_Config();
// Init CSB
GPIO_DeInit(GPIOB);
GPIO_Init(GPIOB, GPIO_Pin_1, GPIO_Mode_Out_PP_High_Fast);
// Enable SPI
SPI_Cmd(ENABLE);
// Wait for SPI to be idle
while (SPI_GetFlagStatus(SPI_FLAG_BSY)) {};
DelayS(5);
// Begin Transaction, pull CSB low
GPIO_WriteBit(GPIOB, GPIO_Pin_1, RESET);
// Write to ChipId register
SPI_SendData(sendByte);
// Wait for buffer to be empty
while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET) {};
// Wait for the register to be filled
while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET) {};
// Receive the data
received = SPI_ReceiveData();
// End transaction, pull CSB high
GPIO_WriteBit(GPIOB, GPIO_Pin_1, SET);
}
#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