drv_ms4525.c
Go to the documentation of this file.
1 /*
2  drv_ms4525.c : driver for MS4525 differential pressure sensor
3 
4  This file is part of BreezySTM32.
5 
6  BreezySTM32 is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  BreezySTM32 is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with BreezySTM32. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 #include <breezystm32.h>
22 
23 static const uint8_t ADDR = 0x28;
24 static uint8_t buf_[4];
25 static float diff_press_;
26 static float temp_;
27 static uint32_t next_update_ms_;
28 static uint32_t last_update_ms_;
29 static bool new_data_;
30 static bool sensor_present_;
31 
32 static void cb(uint8_t result);
33 
34 
35 static inline float sign(float x)
36 {
37  return (x > 0) - (x < 0);
38 }
39 
41 {
42  sensor_present_ = false;
43  if (i2cWrite(ADDR, 0xFF, 0) == true)
44  sensor_present_ = true;
45  else
46  sensor_present_ = false;
49  return sensor_present_;
50 }
51 
52 
54 {
55  if (millis() > next_update_ms_)
56  {
57  i2c_queue_job(READ, ADDR, 0xFF, buf_, 4, NULL, &cb);
58  next_update_ms_ += 100;
59  }
60 }
61 
63 {
64  if (sensor_present_ && millis() > last_update_ms_ + 1000)
65  sensor_present_ = false;
66  return sensor_present_;
67 }
68 
69 void cb(uint8_t result)
70 {
71  if (result != I2C_JOB_ERROR)
72  {
73  new_data_ = true;
74  sensor_present_ = true;
75  }
76  next_update_ms_ += 20;
78 }
79 
80 void ms4525_async_read(float* differential_pressure, float* temp)
81 {
82  if (new_data_)
83  {
84  uint8_t status = (buf_[0] & 0xC0) >> 6;
85  if(status == 0x00) // good data packet
86  {
87  int16_t raw_diff_pressure = 0x3FFF & ((buf_[0] << 8) + buf_[1]);
88  int16_t raw_temp = ( 0xFFE0 & ((buf_[2] << 8) + buf_[3])) >> 5;
89  // Convert to Pa and K
90  diff_press_ = -(((float)(raw_diff_pressure) - 1638.3f) / 6553.2f - 1.0f) * 6894.757f;
91  temp_ = ((200.0f * raw_temp) / 2047.0) - 50 ; // K
92  }
93  new_data_ = false;
94  }
95  (*differential_pressure) = diff_press_;
96  (*temp) = temp_;
97 }
98 
bool ms4525_init()
Definition: drv_ms4525.c:40
volatile uint32_t millis(void)
Definition: system.c:50
static void cb(uint8_t result)
Definition: drv_ms4525.c:69
static float temp_
Definition: drv_ms4525.c:26
static bool sensor_present_
Definition: drv_ms4525.c:30
static volatile uint8_t * status
Definition: drv_i2c.c:102
void ms4525_async_read(float *differential_pressure, float *temp)
Definition: drv_ms4525.c:80
static float sign(float x)
Definition: drv_ms4525.c:35
bool i2cWrite(uint8_t addr_, uint8_t reg_, uint8_t data)
Definition: drv_i2c.c:152
bool ms4525_present()
Definition: drv_ms4525.c:62
static volatile int16_t temp
Definition: drv_mpu6050.c:278
static bool new_data_
Definition: drv_ms4525.c:29
static const uint8_t ADDR
Definition: drv_ms4525.c:23
static uint32_t last_update_ms_
Definition: drv_ms4525.c:28
void i2c_queue_job(i2cJobType_t type, uint8_t addr_, uint8_t reg_, uint8_t *data, uint8_t length, volatile uint8_t *status_, void(*CB)(uint8_t))
Definition: drv_i2c.c:579
static uint32_t next_update_ms_
Definition: drv_ms4525.c:27
static uint8_t buf_[4]
Definition: drv_ms4525.c:24
static float diff_press_
Definition: drv_ms4525.c:25
Definition: drv_i2c.h:31
#define NULL
Definition: usbd_def.h:50
void ms4525_async_update()
Definition: drv_ms4525.c:53


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Apr 15 2021 05:07:46