Coder Social home page Coder Social logo

stm32-bme280's Introduction

stm32-bme280

Bosch BME280 driver for STM32 using HAL
SPI or I2C methods available

This was originally written on an STM32F446RE-Nucleo so its header file is stm32f4xx_hal.h
If you use something like an STM32F103RB, you would need to change that to stm32f1xx_hal.h, or stm32l4xx_hal.h for an STM32L476, et cetera.

This code also assumes that you route printf() to usart2.

My changes were made to bme280_support.c, and the other two files should be identical to the factory Bosch files.

To get SPI working:
comment out the I2C line and uncomment the SPI line in bme280_support.c lines 157/158
set prescaler to 8 or higher, clock polarity to high, and phase to two-edge:

hspi2.Instance = SPI2;
hspi2.Init.Mode = SPI_MODE_MASTER;
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
hspi2.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi2.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi2.Init.NSS = SPI_NSS_SOFT;
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi2.Init.CRCPolynomial = 10;

Basically we need to get the SPI clock speed under 10MHz (according to the data sheet, pg.36)
For for a 64mhz stm32f103 running APB1 peripheral bus clock at 32Mhz, use a prescaler of 4 to yield 8 Mb/s SPI clock speed.
It is very helpful to know what clock speed your peripherals run at.

I2C is not nearly as finicky and just works.

Tested and working on an STM32F446RE-Nucleo, and with the minor changes mentioned above (header file and SPI baud rate prescaler), an STM32F103RB-Nucleo.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.