Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bme280_interface.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include "spi.h"
  4. #include "bme280.h"
  5. #include "bme280_interface.h"
  6. /* for debugging */
  7. #include "uart_debug.h"
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. void user_delay_ms(uint32_t period);
  12. int8_t user_spi_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len);
  13. int8_t user_spi_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len);
  14. struct bme280_dev deviceStructure;
  15. uint32_t req_delay;
  16. int8_t Initialize_BME280(void)
  17. {
  18. int8_t rslt = BME280_OK;
  19. uint8_t settings_sel;
  20. char debugString[20] = "";
  21. /* Setup the SPI interface */
  22. BME_CSN_DDR |= (1 << BME_CSN_PIN);
  23. BME_CSN_PORT |= (1 << BME_CSN_PIN);
  24. /* Sensor_0 interface over SPI with native chip select line */
  25. deviceStructure.dev_id = 0;
  26. deviceStructure.intf = BME280_SPI_INTF;
  27. deviceStructure.read = user_spi_read;
  28. deviceStructure.write = user_spi_write;
  29. deviceStructure.delay_ms = user_delay_ms;
  30. rslt = bme280_init(&deviceStructure);
  31. /* Settings for mode of operation: weather monitorint */
  32. deviceStructure.settings.osr_h = BME280_OVERSAMPLING_1X;
  33. deviceStructure.settings.osr_p = BME280_OVERSAMPLING_1X;
  34. deviceStructure.settings.osr_t = BME280_OVERSAMPLING_1X;
  35. deviceStructure.settings.filter = BME280_FILTER_COEFF_OFF;
  36. settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;
  37. rslt = bme280_set_sensor_settings(settings_sel, &deviceStructure);
  38. /* calculate minimum delay required between consecutive measurments */
  39. req_delay = bme280_cal_meas_delay(&deviceStructure.settings);
  40. sprintf(debugString, "req delay: %ld\r\n", req_delay);
  41. Print_Debug_String(debugString);
  42. return rslt;
  43. }
  44. /* Get one measurement in forced mode */
  45. void BME280_Get_Measurement(void)
  46. {
  47. int8_t rslt = BME280_OK;
  48. struct bme280_data comp_data;
  49. char debugString[30] = "";
  50. rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, &deviceStructure);
  51. /* Wait for the measurement to complete and print data @25Hz */
  52. deviceStructure.delay_ms(req_delay);
  53. rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, &deviceStructure);
  54. sprintf(debugString, "rslt: %d\r\n", rslt);
  55. Print_Debug_String(debugString);
  56. sprintf(debugString, "temp: %ld\r\n", comp_data.temperature);
  57. Print_Debug_String(debugString);
  58. sprintf(debugString, "hum: %ld\r\n", comp_data.humidity);
  59. Print_Debug_String(debugString);
  60. sprintf(debugString, "pres: %ld\r\n", comp_data.pressure);
  61. Print_Debug_String(debugString);
  62. }
  63. /* Implementation of the interface function needed by the BME280 library */
  64. void user_delay_ms(uint32_t period)
  65. {
  66. while (period--)
  67. {
  68. _delay_ms(1);
  69. }
  70. }
  71. int8_t user_spi_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
  72. {
  73. int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
  74. /* unused parameters */
  75. (void)dev_id;
  76. SPI_Start_Transmission(&BME_CSN_PORT, BME_CSN_PIN);
  77. SPI_Transfer_Byte(reg_addr);
  78. for (uint16_t i = 0; i < len; i ++)
  79. {
  80. reg_data[i] = SPI_Transfer_Byte(0x0); /* Value to send doesn't matter */
  81. }
  82. SPI_Stop_Transmission(&BME_CSN_PORT, BME_CSN_PIN);
  83. return rslt;
  84. }
  85. int8_t user_spi_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
  86. {
  87. int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
  88. /* unused parameters */
  89. (void)dev_id;
  90. /* SPI routine */
  91. SPI_Start_Transmission(&BME_CSN_PORT, BME_CSN_PIN);
  92. SPI_Transfer_Byte(reg_addr);
  93. for (uint16_t i = 0; i < len; i ++)
  94. {
  95. SPI_Transfer_Byte(reg_data[i]);
  96. }
  97. SPI_Stop_Transmission(&BME_CSN_PORT, BME_CSN_PIN);
  98. return rslt;
  99. }