暫無描述
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.8KB

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