Advertisement
Guest User

temperature.c

a guest
Sep 29th, 2022
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.31 KB | None | 0 0
  1. /*!
  2.  *  @brief Example shows basic application to configure and read the temperature.
  3.  */
  4.  
  5. #include "bmp280_defs.h"
  6. #include "stdio.h"
  7. #include "bmp280.h"
  8.  
  9. #include <cstdint>
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. extern "C"
  13. {
  14. #include <linux/i2c-dev.h>
  15. #include <linux/i2c.h>
  16. #include <i2c/smbus.h>
  17.  
  18. }
  19. #include <iostream>
  20. #include <sys/ioctl.h>
  21. #include <thread>
  22. #include <chrono>
  23. #include <fstream>
  24. void delay_ms(uint32_t period_ms);
  25. int8_t i2c_reg_write(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, uint16_t length);
  26. int8_t i2c_reg_read(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, uint16_t length);
  27. int8_t spi_reg_write(uint8_t cs, uint8_t reg_addr, uint8_t *reg_data, uint16_t length);
  28. int8_t spi_reg_read(uint8_t cs, uint8_t reg_addr, uint8_t *reg_data, uint16_t length);
  29. void print_rslt(const char api_name[], int8_t rslt);
  30.  
  31. int main(void)
  32. {
  33.     int8_t rslt;
  34.     struct bmp280_dev bmp;
  35.     struct bmp280_config conf;
  36.     struct bmp280_uncomp_data ucomp_data;
  37.     int32_t temp32;
  38.     double temp;
  39.  
  40.     /* Map the delay function pointer with the function responsible for implementing the delay */
  41.     bmp.delay_ms = delay_ms;
  42.  
  43.     /* Assign device I2C address based on the status of SDO pin (GND for PRIMARY(0x76) & VDD for SECONDARY(0x77)) */
  44.     bmp.dev_id = BMP280_I2C_ADDR_PRIM;
  45.  
  46.     /* Select the interface mode as I2C */
  47.     bmp.intf = BMP280_I2C_INTF;
  48.  
  49.     /* Map the I2C read & write function pointer with the functions responsible for I2C bus transfer */
  50.     bmp.read = i2c_reg_read;
  51.     bmp.write = i2c_reg_write;
  52.  
  53.     /* To enable SPI interface: comment the above 4 lines and uncomment the below 4 lines */
  54.  
  55.     /*
  56.      * bmp.dev_id = 0;
  57.      * bmp.read = spi_reg_read;
  58.      * bmp.write = spi_reg_write;
  59.      * bmp.intf = BMP280_SPI_INTF;
  60.      */
  61.     rslt = bmp280_init(&bmp);
  62.     print_rslt(" bmp280_init status", rslt);
  63.  
  64.     /* Always read the current settings before writing, especially when
  65.      * all the configuration is not modified
  66.      */
  67.     rslt = bmp280_get_config(&conf, &bmp);
  68.     print_rslt(" bmp280_get_config status", rslt);
  69.  
  70.     /* configuring the temperature oversampling, filter coefficient and output data rate */
  71.     /* Overwrite the desired settings */
  72.     conf.filter = BMP280_FILTER_COEFF_2;
  73.  
  74.     /* Temperature oversampling set at 4x */
  75.     conf.os_temp = BMP280_OS_4X;
  76.  
  77.     /* Pressure over sampling none (disabling pressure measurement) */
  78.     conf.os_pres = BMP280_OS_NONE;
  79.  
  80.     /* Setting the output data rate as 1HZ(1000ms) */
  81.     conf.odr = BMP280_ODR_1000_MS;
  82.     rslt = bmp280_set_config(&conf, &bmp);
  83.     print_rslt(" bmp280_set_config status", rslt);
  84.  
  85.     /* Always set the power mode after setting the configuration */
  86.     rslt = bmp280_set_power_mode(BMP280_NORMAL_MODE, &bmp);
  87.     print_rslt(" bmp280_set_power_mode status", rslt);
  88.     while (1)
  89.     {
  90.         /* Reading the raw data from sensor */
  91.         rslt = bmp280_get_uncomp_data(&ucomp_data, &bmp);
  92.  
  93.         /* Getting the 32 bit compensated temperature */
  94.         rslt = bmp280_get_comp_temp_32bit(&temp32, ucomp_data.uncomp_temp, &bmp);
  95.  
  96.         /* Getting the compensated temperature as floating point value */
  97.         rslt = bmp280_get_comp_temp_double(&temp, ucomp_data.uncomp_temp, &bmp);
  98.         printf("UT: %ld, T32: %ld, T: %f \r\n", ucomp_data.uncomp_temp, temp32, temp);
  99.  
  100.         /* Sleep time between measurements = BMP280_ODR_1000_MS */
  101.         bmp.delay_ms(1000);
  102.     }
  103.  
  104.     return 0;
  105. }
  106.  
  107. /*!
  108.  *  @brief Function that creates a mandatory delay required in some of the APIs such as "bmg250_soft_reset",
  109.  *      "bmg250_set_foc", "bmg250_perform_self_test"  and so on.
  110.  *
  111.  *  @param[in] period_ms  : the required wait time in milliseconds.
  112.  *  @return void.
  113.  *
  114.  */
  115. void delay_ms(uint32_t period_ms)
  116. {
  117.     /* Implement the delay routine according to the target machine */
  118.     std::this_thread::sleep_for(std::chrono::milliseconds(period_ms));
  119. }
  120.  
  121. /*!
  122.  *  @brief Function for writing the sensor's registers through I2C bus.
  123.  *
  124.  *  @param[in] i2c_addr : sensor I2C address.
  125.  *  @param[in] reg_addr : Register address.
  126.  *  @param[in] reg_data : Pointer to the data buffer whose value is to be written.
  127.  *  @param[in] length   : No of bytes to write.
  128.  *
  129.  *  @return Status of execution
  130.  *  @retval 0 -> Success
  131.  *  @retval >0 -> Failure Info
  132.  *
  133.  */
  134. int8_t i2c_reg_write(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, uint16_t length)
  135. {
  136.     int fd;
  137.     std::cout << "write";
  138.     fd = open("/dev/i2c-1", O_RDWR);
  139.     std::cout << "fd " << fd;
  140.     if((ioctl(fd, I2C_SLAVE, i2c_addr)) < 0)
  141.     {
  142.         std::cout << "kek1";
  143.     }
  144.     int rres = write(fd, reg_data, length);
  145.     if(rres != length)
  146.     {
  147.         std::cout << " failed to write\n";
  148.     }
  149.     for(int i = 0; i < length; i++)
  150.     {
  151.         std::cout <<std::hex <<(int)reg_data[i] << ", ";
  152.     }
  153.     std::cout << "\n";
  154.     close(fd);
  155.     return 0;
  156. }
  157. /*!
  158.  *  @brief Function for reading the sensor's registers through I2C bus.
  159.  *
  160.  *  @param[in] i2c_addr : Sensor I2C address.
  161.  *  @param[in] reg_addr : Register address.
  162.  *  @param[out] reg_data    : Pointer to the data buffer to store the read data.
  163.  *  @param[in] length   : No of bytes to read.
  164.  *
  165.  *  @return Status of execution
  166.  *  @retval 0 -> Success
  167.  *  @retval >0 -> Failure Info
  168.  *
  169.  */
  170. int8_t i2c_reg_read(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, uint16_t length)
  171. {
  172.     int fd;
  173.     //std::cout << "read";
  174.     fd = open("/dev/i2c-1", O_RDWR);
  175.     //std::cout << "fd " << fd;
  176.     int rres = ioctl(fd, I2C_SLAVE, i2c_addr);
  177.     if(rres < 0)
  178.     {
  179.         std::cout << "rkek1 "<< rres << ' ';
  180.     }
  181.     std::cout << "trying to read " << length << "bytes\n";
  182.     for(int i = 0; i < length ; i++)
  183.     {
  184.         __s32 status = i2c_smbus_read_byte_data(fd, reg_addr);
  185.         if( status < 0)
  186.    
  187.         std::cout << status << ", ";
  188.         reg_data[i] = status;
  189.  
  190.     }
  191.     std::cout << "\n";
  192.     close(fd);
  193.     /* Implement the I2C read routine according to the target machine. */
  194.     return 0;
  195. }
  196.  
  197. /*!
  198.  *  @brief Function for writing the sensor's registers through SPI bus.
  199.  *
  200.  *  @param[in] cs           : Chip select to enable the sensor.
  201.  *  @param[in] reg_addr     : Register address.
  202.  *  @param[in] reg_data : Pointer to the data buffer whose data has to be written.
  203.  *  @param[in] length       : No of bytes to write.
  204.  *
  205.  *  @return Status of execution
  206.  *  @retval 0 -> Success
  207.  *  @retval >0 -> Failure Info
  208.  *
  209.  */
  210. int8_t spi_reg_write(uint8_t cs, uint8_t reg_addr, uint8_t *reg_data, uint16_t length)
  211. {
  212.  
  213.     /* Implement the SPI write routine according to the target machine. */
  214.     return -1;
  215. }
  216.  
  217. /*!
  218.  *  @brief Function for reading the sensor's registers through SPI bus.
  219.  *
  220.  *  @param[in] cs       : Chip select to enable the sensor.
  221.  *  @param[in] reg_addr : Register address.
  222.  *  @param[out] reg_data    : Pointer to the data buffer to store the read data.
  223.  *  @param[in] length   : No of bytes to read.
  224.  *
  225.  *  @return Status of execution
  226.  *  @retval 0 -> Success
  227.  *  @retval >0 -> Failure Info
  228.  *
  229.  */
  230. int8_t spi_reg_read(uint8_t cs, uint8_t reg_addr, uint8_t *reg_data, uint16_t length)
  231. {
  232.  
  233.     /* Implement the SPI read routine according to the target machine. */
  234.     return -1;
  235. }
  236.  
  237. /*!
  238.  *  @brief Prints the execution status of the APIs.
  239.  *
  240.  *  @param[in] api_name : name of the API whose execution status has to be printed.
  241.  *  @param[in] rslt     : error code returned by the API whose execution status has to be printed.
  242.  *
  243.  *  @return void.
  244.  */
  245. void print_rslt(const char api_name[], int8_t rslt)
  246. {
  247.     if (rslt != BMP280_OK)
  248.     {
  249.         printf("%s\t", api_name);
  250.         if (rslt == BMP280_E_NULL_PTR)
  251.         {
  252.             printf("Error [%d] : Null pointer error\r\n", rslt);
  253.         }
  254.         else if (rslt == BMP280_E_COMM_FAIL)
  255.         {
  256.             printf("Error [%d] : Bus communication failed\r\n", rslt);
  257.         }
  258.         else if (rslt == BMP280_E_IMPLAUS_TEMP)
  259.         {
  260.             printf("Error [%d] : Invalid Temperature\r\n", rslt);
  261.         }
  262.         else if (rslt == BMP280_E_DEV_NOT_FOUND)
  263.         {
  264.             printf("Error [%d] : Device not found\r\n", rslt);
  265.         }
  266.         else
  267.         {
  268.             /* For more error codes refer "*_defs.h" */
  269.             printf("Error [%d] : Unknown error code\r\n", rslt);
  270.         }
  271.     }
  272. }
  273.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement