Subversion Repositories dashGPS

Rev

Rev 18 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 18 Rev 19
Line 22... Line 22...
22
#include "usb_device.h"
22
#include "usb_device.h"
23
 
23
 
24
/* Private includes ----------------------------------------------------------*/
24
/* Private includes ----------------------------------------------------------*/
25
/* USER CODE BEGIN Includes */
25
/* USER CODE BEGIN Includes */
26
#include "libSerial/serial.h"
26
#include "libSerial/serial.h"
27
#include "libBMP280/bmp280.h"
27
#include "libBME280/bme280.h"
28
#include "display.h"
28
#include "display.h"
29
/* USER CODE END Includes */
29
/* USER CODE END Includes */
30
 
30
 
31
/* Private typedef -----------------------------------------------------------*/
31
/* Private typedef -----------------------------------------------------------*/
32
/* USER CODE BEGIN PTD */
32
/* USER CODE BEGIN PTD */
Line 53... Line 53...
53
TIM_HandleTypeDef htim4;
53
TIM_HandleTypeDef htim4;
54
 
54
 
55
UART_HandleTypeDef huart1;
55
UART_HandleTypeDef huart1;
56
 
56
 
57
/* USER CODE BEGIN PV */
57
/* USER CODE BEGIN PV */
-
 
58
/* Structure that contains identifier details used in example */
58
typedef struct
59
struct identifier
59
{
60
{
-
 
61
    /* Variable to hold device address */
60
  uint8_t dev_addr;
62
    uint8_t dev_addr;
-
 
63
 
-
 
64
    /* Variable that contains file descriptor */
61
} interface_t;
65
    int8_t fd;
-
 
66
};
-
 
67
 
62
 
68
 
63
static int8_t
69
static int8_t
64
user_i2c_write (uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, uint32_t len)
70
user_i2c_write ( uint8_t reg_addr, uint8_t *reg_data, uint32_t len, struct identifier * intf)
65
{
71
{
-
 
72
 
-
 
73
  uint8_t i2c_addr = intf->dev_addr;
66
    HAL_StatusTypeDef st = HAL_I2C_Mem_Write(&hi2c2, i2c_addr<<1, reg_addr, 1, reg_data, len, 10000);
74
    HAL_StatusTypeDef st = HAL_I2C_Mem_Write(&hi2c2, i2c_addr<<1, reg_addr, 1, reg_data, len, 10000);
67
 
75
 
68
  return st != HAL_OK ?  BMP280_E_COMM_FAIL: BMP280_OK;
76
  return st != HAL_OK ?  BME280_E_COMM_FAIL: BME280_OK;
69
 
77
 
70
}
78
}
71
static int8_t
79
static int8_t
72
user_i2c_read (uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, uint32_t len)
80
user_i2c_read ( uint8_t reg_addr, uint8_t *reg_data, uint32_t len, struct identifier * intf)
73
{
81
{
-
 
82
  uint8_t i2c_addr = intf->dev_addr;
-
 
83
 
74
  HAL_StatusTypeDef st = HAL_I2C_Mem_Read(&hi2c2, i2c_addr<<1, reg_addr, 1, reg_data, len, 10000);
84
  HAL_StatusTypeDef st = HAL_I2C_Mem_Read(&hi2c2, i2c_addr<<1, reg_addr, 1, reg_data, len, 10000);
75
 
85
 
76
  return st != HAL_OK ?  BMP280_E_COMM_FAIL: BMP280_OK;
86
  return st != HAL_OK ?  BME280_E_COMM_FAIL: BME280_OK;
77
 
87
 
78
}
88
}
79
 
89
 
80
static void
90
static void
81
user_delay_ms (uint32_t ms, void *handle)
91
user_delay_us(uint32_t us, void *handle)
82
{
92
{
83
  HAL_Delay (ms);
93
  HAL_Delay ((us+999)/1000);
84
 
-
 
85
}
94
}
86
 
95
 
87
 
96
 
88
 
97
 
-
 
98
struct bme280_dev dev;
89
 
99
 
90
struct bmp280_dev bmp =
100
struct identifier id;
91
  {
-
 
92
 
-
 
93
  .intf = BMP280_I2C_INTF, .read = user_i2c_read, .write = user_i2c_write,
-
 
94
      .delay_ms = user_delay_ms,
-
 
95
 
101
 
96
      /* Update interface pointer with the structure that contains both device address and file descriptor */
102
/* Variable to store minimum wait time between consecutive measurement in force mode */
97
     .dev_id =  BMP280_I2C_ADDR_PRIM };
103
uint32_t req_delay;
98
 
104
 
99
int8_t rslt;
105
int8_t rslt;
100
struct bmp280_config conf;
-
 
101
 
106
 
102
 
107
 
103
/* USER CODE END PV */
108
/* USER CODE END PV */
104
 
109
 
105
/* Private function prototypes -----------------------------------------------*/
110
/* Private function prototypes -----------------------------------------------*/
Line 111... Line 116...
111
static void MX_TIM3_Init(void);
116
static void MX_TIM3_Init(void);
112
static void MX_I2C2_Init(void);
117
static void MX_I2C2_Init(void);
113
static void MX_RTC_Init(void);
118
static void MX_RTC_Init(void);
114
/* USER CODE BEGIN PFP */
119
/* USER CODE BEGIN PFP */
115
 
120
 
-
 
121
/*!
-
 
122
 * @brief This API reads the sensor temperature, pressure and humidity data in forced mode.
-
 
123
 */
-
 
124
int8_t
-
 
125
stream_sensor_data_forced_mode (struct bme280_dev *dev)
-
 
126
{
-
 
127
  /* Variable to define the result */
-
 
128
  int8_t rslt = BME280_OK;
-
 
129
 
-
 
130
  /* Variable to define the selecting sensors */
-
 
131
  uint8_t settings_sel = 0;
-
 
132
 
-
 
133
  /* Structure to get the pressure, temperature and humidity values */
-
 
134
  struct bme280_data comp_data;
-
 
135
 
-
 
136
  /* Recommended mode of operation: Indoor navigation */
-
 
137
  dev->settings.osr_h = BME280_OVERSAMPLING_1X;
-
 
138
  dev->settings.osr_p = BME280_OVERSAMPLING_16X;
-
 
139
  dev->settings.osr_t = BME280_OVERSAMPLING_2X;
-
 
140
  dev->settings.filter = BME280_FILTER_COEFF_16;
-
 
141
 
-
 
142
  settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL
-
 
143
      | BME280_FILTER_SEL;
-
 
144
 
-
 
145
  /* Set the sensor settings */
-
 
146
  rslt = bme280_set_sensor_settings (settings_sel, dev);
-
 
147
  if (rslt != BME280_OK)
-
 
148
    {
-
 
149
//        fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);
-
 
150
 
-
 
151
      return rslt;
-
 
152
    }
-
 
153
 
-
 
154
  /*Calculate the minimum delay required between consecutive measurement based upon the sensor enabled
-
 
155
   *  and the oversampling configuration. */
-
 
156
  req_delay = bme280_cal_meas_delay (&dev->settings);
-
 
157
 
-
 
158
  /* Set the sensor to forced mode */
-
 
159
  rslt = bme280_set_sensor_mode (BME280_FORCED_MODE, dev);
-
 
160
  if (rslt != BME280_OK)
-
 
161
    {
-
 
162
      return rslt;
-
 
163
    }
-
 
164
 
-
 
165
  return rslt;
-
 
166
}
116
/* USER CODE END PFP */
167
/* USER CODE END PFP */
117
 
168
 
118
/* Private user code ---------------------------------------------------------*/
169
/* Private user code ---------------------------------------------------------*/
119
/* USER CODE BEGIN 0 */
170
/* USER CODE BEGIN 0 */
120
 
171
 
Line 126... Line 177...
126
  */
177
  */
127
int main(void)
178
int main(void)
128
{
179
{
129
  /* USER CODE BEGIN 1 */
180
  /* USER CODE BEGIN 1 */
130
 
181
 
-
 
182
 
131
  /* USER CODE END 1 */
183
  /* USER CODE END 1 */
132
 
184
 
133
  /* MCU Configuration--------------------------------------------------------*/
185
  /* MCU Configuration--------------------------------------------------------*/
134
 
186
 
135
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
187
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
Line 164... Line 216...
164
  /* setup the USART control blocks */
216
  /* setup the USART control blocks */
165
  init_usart_ctl (&uc1, &huart1);
217
  init_usart_ctl (&uc1, &huart1);
166
 
218
 
167
  EnableSerialRxInterrupt (&uc1);
219
  EnableSerialRxInterrupt (&uc1);
168
 
220
 
-
 
221
  /* BME 280 */
-
 
222
  struct bme280_dev dev;
-
 
223
 
-
 
224
  struct identifier id;
-
 
225
 
-
 
226
  /* Variable to define the result */
-
 
227
  int8_t rslt = BME280_OK;
-
 
228
 
-
 
229
  /* Make sure to select BME280_I2C_ADDR_PRIM or BME280_I2C_ADDR_SEC as needed */
-
 
230
  id.dev_addr = BME280_I2C_ADDR_PRIM >> 1;
-
 
231
 
-
 
232
  dev.intf = BME280_I2C_INTF;
-
 
233
  dev.read = user_i2c_read;
-
 
234
  dev.write = user_i2c_write;
-
 
235
  dev.delay_us = user_delay_us;
-
 
236
 
-
 
237
  /* Update interface pointer with the structure that contains both device address and file descriptor */
-
 
238
  dev.intf_ptr = &id;
-
 
239
 
-
 
240
  /* Initialize the bme280 */
-
 
241
    rslt = bme280_init(&dev);
-
 
242
    if (rslt != BME280_OK)
-
 
243
    {
-
 
244
//        fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt);
-
 
245
        exit(1);
-
 
246
    }
-
 
247
 
-
 
248
 
-
 
249
    rslt = stream_sensor_data_forced_mode(&dev);
-
 
250
    if (rslt != BME280_OK)
-
 
251
    {
-
 
252
  //      fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);
-
 
253
        exit(1);
-
 
254
    }
-
 
255
 
169
 
256
 
170
  /* Initialize the bmp280 */
-
 
171
  rslt = bmp280_init(&bmp);
-
 
172
//  print_rslt(" bmp280_init status", rslt);
-
 
173
 
-
 
174
  /* Always read the current settings before writing, especially when
-
 
175
   * all the configuration is not modified
-
 
176
   */
-
 
177
  rslt = bmp280_get_config(&conf, &bmp);
-
 
178
 // print_rslt(" bmp280_get_config status", rslt);
-
 
179
 
-
 
180
  /* configuring the temperature oversampling, filter coefficient and output data rate */
-
 
181
  /* Overwrite the desired settings */
-
 
182
  conf.filter = BMP280_FILTER_COEFF_2;
-
 
183
 
-
 
184
  /* Temperature oversampling set at 4x */
-
 
185
  conf.os_temp = BMP280_OS_4X;
-
 
186
 
-
 
187
  /* Pressure over sampling none (disabling pressure measurement) */
-
 
188
  conf.os_pres = BMP280_OS_4X;
-
 
189
 
-
 
190
  /* Setting the output data rate as 2HZ(500ms) */
-
 
191
  conf.odr = BMP280_ODR_500_MS;
-
 
192
  rslt = bmp280_set_config(&conf, &bmp);
-
 
193
  //print_rslt(" bmp280_set_config status", rslt);
-
 
194
 
-
 
195
  /* Always set the power mode after setting the configuration */
-
 
196
  rslt = bmp280_set_power_mode(BMP280_NORMAL_MODE, &bmp);
-
 
197
  //print_rslt(" bmp280_set_power_mode status", rslt);
-
 
198
 
257
 
199
 
258
 
200
 
259
 
201
  cc_init ();
260
  cc_init ();
202
  /* USER CODE END 2 */
261
  /* USER CODE END 2 */
203
 
262
 
204
  /* Infinite loop */
263
  /* Infinite loop */
205
  /* USER CODE BEGIN WHILE */
264
  /* USER CODE BEGIN WHILE */
206
  while (1)
265
  while (1)
207
    {
266
    {
208
      cc_run (&bmp);
267
      cc_run (&dev);
209
 
268
 
210
 
269
 
211
 
270
 
212
      HAL_Delay (50);
271
      HAL_Delay (50);
213
 
272
 
Line 411... Line 470...
411
 
470
 
412
  /* USER CODE BEGIN TIM3_Init 1 */
471
  /* USER CODE BEGIN TIM3_Init 1 */
413
 
472
 
414
  /* USER CODE END TIM3_Init 1 */
473
  /* USER CODE END TIM3_Init 1 */
415
  htim3.Instance = TIM3;
474
  htim3.Instance = TIM3;
416
  htim3.Init.Prescaler = 640;
475
  htim3.Init.Prescaler = 719;
417
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
476
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
418
  htim3.Init.Period = 10000;
477
  htim3.Init.Period = 10000;
419
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
478
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
420
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
479
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
421
  if (HAL_TIM_OC_Init(&htim3) != HAL_OK)
480
  if (HAL_TIM_OC_Init(&htim3) != HAL_OK)