|
| 1 | +/* |
| 2 | + ****************************************************************************** |
| 3 | + * @file read_data_polling.c |
| 4 | + * @author Sensors Software Solution Team |
| 5 | + * @brief This file show the simplest way to get data from sensor. |
| 6 | + * |
| 7 | + ****************************************************************************** |
| 8 | + * @attention |
| 9 | + * |
| 10 | + * <h2><center>© Copyright (c) 2020 STMicroelectronics. |
| 11 | + * All rights reserved.</center></h2> |
| 12 | + * |
| 13 | + * This software component is licensed by ST under BSD 3-Clause license, |
| 14 | + * the "License"; You may not use this file except in compliance with the |
| 15 | + * License. You may obtain a copy of the License at: |
| 16 | + * opensource.org/licenses/BSD-3-Clause |
| 17 | + * |
| 18 | + ****************************************************************************** |
| 19 | + */ |
| 20 | + |
| 21 | +/* |
| 22 | + * This example was developed using the following STMicroelectronics |
| 23 | + * evaluation boards: |
| 24 | + * |
| 25 | + * - STEVAL_MKI109V3 + STEVAL-MKI213V1 |
| 26 | + * - NUCLEO_F411RE + STEVAL-MKI213V1 |
| 27 | + * - DISCOVERY_SPC584B + STEVAL-MKI213V1 |
| 28 | + * |
| 29 | + * and STM32CubeMX tool with STM32CubeF4 MCU Package |
| 30 | + * |
| 31 | + * Used interfaces: |
| 32 | + * |
| 33 | + * STEVAL_MKI109V3 - Host side: USB (Virtual COM) |
| 34 | + * - Sensor side: SPI(Default) / I2C(supported) |
| 35 | + * |
| 36 | + * NUCLEO_STM32F411RE - Host side: UART(COM) to USB bridge |
| 37 | + * - I2C(Default) / SPI(supported) |
| 38 | + * |
| 39 | + * DISCOVERY_SPC584B - Host side: UART(COM) to USB bridge |
| 40 | + * - Sensor side: I2C(Default) / SPI(supported) |
| 41 | + * |
| 42 | + * If you need to run this example on a different hardware platform a |
| 43 | + * modification of the functions: `platform_write`, `platform_read`, |
| 44 | + * `tx_com` and 'platform_init' is required. |
| 45 | + * |
| 46 | + */ |
| 47 | + |
| 48 | +/* STMicroelectronics evaluation boards definition |
| 49 | + * |
| 50 | + * Please uncomment ONLY the evaluation boards in use. |
| 51 | + * If a different hardware is used please comment all |
| 52 | + * following target board and redefine yours. |
| 53 | + */ |
| 54 | + |
| 55 | +//#define STEVAL_MKI109V3 /* little endian */ |
| 56 | +#define NUCLEO_F411RE /* little endian */ |
| 57 | +//#define SPC584B_DIS /* big endian */ |
| 58 | + |
| 59 | +/* ATTENTION: By default the driver is little endian. If you need switch |
| 60 | + * to big endian please see "Endianness definitions" in the |
| 61 | + * header file of the driver (_reg.h). |
| 62 | + */ |
| 63 | + |
| 64 | +#if defined(STEVAL_MKI109V3) |
| 65 | +/* MKI109V3: Define communication interface */ |
| 66 | +#define SENSOR_BUS hspi2 |
| 67 | +/* MKI109V3: Vdd and Vddio power supply values */ |
| 68 | +#define PWM_3V3 915 |
| 69 | + |
| 70 | +#elif defined(NUCLEO_F411RE) |
| 71 | +/* NUCLEO_F411RE: Define communication interface */ |
| 72 | +#define SENSOR_BUS hi2c1 |
| 73 | + |
| 74 | +#elif defined(SPC584B_DIS) |
| 75 | +/* DISCOVERY_SPC584B: Define communication interface */ |
| 76 | +#define SENSOR_BUS I2CD1 |
| 77 | + |
| 78 | +#endif |
| 79 | + |
| 80 | +/* Includes ------------------------------------------------------------------*/ |
| 81 | +#include "lps27hhw_reg.h" |
| 82 | +#include <string.h> |
| 83 | +#include <stdio.h> |
| 84 | + |
| 85 | +#if defined(NUCLEO_F411RE) |
| 86 | +#include "stm32f4xx_hal.h" |
| 87 | +#include "usart.h" |
| 88 | +#include "gpio.h" |
| 89 | +#include "i2c.h" |
| 90 | + |
| 91 | +#elif defined(STEVAL_MKI109V3) |
| 92 | +#include "stm32f4xx_hal.h" |
| 93 | +#include "usbd_cdc_if.h" |
| 94 | +#include "gpio.h" |
| 95 | +#include "spi.h" |
| 96 | + |
| 97 | +#elif defined(SPC584B_DIS) |
| 98 | +#include "components.h" |
| 99 | +#endif |
| 100 | + |
| 101 | +/* Private macro -------------------------------------------------------------*/ |
| 102 | +#define BOOT_TIME 5 //ms |
| 103 | + |
| 104 | +#define TX_BUF_DIM 1000 |
| 105 | + |
| 106 | +/* Private variables ---------------------------------------------------------*/ |
| 107 | +static uint32_t data_raw_pressure; |
| 108 | +static int16_t data_raw_temperature; |
| 109 | +static float pressure_hPa; |
| 110 | +static float temperature_degC; |
| 111 | +static uint8_t whoamI, rst; |
| 112 | +static uint8_t tx_buffer[TX_BUF_DIM]; |
| 113 | + |
| 114 | +/* Extern variables ----------------------------------------------------------*/ |
| 115 | + |
| 116 | +/* Private functions ---------------------------------------------------------*/ |
| 117 | + |
| 118 | +/* |
| 119 | + * WARNING: |
| 120 | + * Functions declare in this section are defined at the end of this file |
| 121 | + * and are strictly related to the hardware platform used. |
| 122 | + * |
| 123 | + */ |
| 124 | + |
| 125 | +static int32_t platform_write(void *handle, uint8_t reg, uint8_t *bufp, |
| 126 | + uint16_t len); |
| 127 | +static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, |
| 128 | + uint16_t len); |
| 129 | +static void tx_com( uint8_t *tx_buffer, uint16_t len ); |
| 130 | +static void platform_delay(uint32_t ms); |
| 131 | +static void platform_init(void); |
| 132 | + |
| 133 | +/* Main Example --------------------------------------------------------------*/ |
| 134 | + |
| 135 | +void example_main(void) |
| 136 | +{ |
| 137 | + /* Initialize mems driver interface */ |
| 138 | + stmdev_ctx_t dev_ctx; |
| 139 | + dev_ctx.write_reg = platform_write; |
| 140 | + dev_ctx.read_reg = platform_read; |
| 141 | + dev_ctx.handle = &SENSOR_BUS; |
| 142 | + |
| 143 | + /* Initialize platform specific hardware */ |
| 144 | + platform_init(); |
| 145 | + |
| 146 | + /* Wait sensor boot time */ |
| 147 | + platform_delay(BOOT_TIME); |
| 148 | + |
| 149 | + /* Check device ID */ |
| 150 | + whoamI = 0; |
| 151 | + lps27hhw_device_id_get(&dev_ctx, &whoamI); |
| 152 | + if ( whoamI != LPS27HHW_ID ) |
| 153 | + while(1); /*manage here device not found */ |
| 154 | + |
| 155 | + /* Restore default configuration */ |
| 156 | + lps27hhw_reset_set(&dev_ctx, PROPERTY_ENABLE); |
| 157 | + do { |
| 158 | + lps27hhw_reset_get(&dev_ctx, &rst); |
| 159 | + } while (rst); |
| 160 | + |
| 161 | + /* Enable Block Data Update */ |
| 162 | + lps27hhw_block_data_update_set(&dev_ctx, PROPERTY_ENABLE); |
| 163 | + |
| 164 | + /* Set Output Data Rate */ |
| 165 | + lps27hhw_data_rate_set(&dev_ctx, LPS27HHW_10_Hz_LOW_NOISE); |
| 166 | + |
| 167 | + /* Read samples in polling mode (no int) */ |
| 168 | + while(1) |
| 169 | + { |
| 170 | + /* Read output only if new value is available */ |
| 171 | + lps27hhw_reg_t reg; |
| 172 | + lps27hhw_read_reg(&dev_ctx, LPS27HHW_STATUS, (uint8_t *)®, 1); |
| 173 | + |
| 174 | + if (reg.status.p_da) |
| 175 | + { |
| 176 | + memset(&data_raw_pressure, 0x00, sizeof(int32_t)); |
| 177 | + lps27hhw_pressure_raw_get(&dev_ctx, &data_raw_pressure); |
| 178 | + pressure_hPa = lps27hhw_from_lsb_to_hpa( data_raw_pressure ); |
| 179 | + sprintf((char*)tx_buffer, "pressure [hPa]:%6.2f\r\n", pressure_hPa); |
| 180 | + tx_com( tx_buffer, strlen( (char const*)tx_buffer ) ); |
| 181 | + } |
| 182 | + |
| 183 | + if (reg.status.t_da) |
| 184 | + { |
| 185 | + memset( &data_raw_temperature, 0x00, sizeof(int16_t)); |
| 186 | + lps27hhw_temperature_raw_get(&dev_ctx, &data_raw_temperature); |
| 187 | + temperature_degC = lps27hhw_from_lsb_to_celsius( data_raw_temperature ); |
| 188 | + sprintf((char*)tx_buffer, "temperature [degC]:%6.2f\r\n", temperature_degC ); |
| 189 | + tx_com( tx_buffer, strlen( (char const*)tx_buffer ) ); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +/* |
| 195 | + * @brief Write generic device register (platform dependent) |
| 196 | + * |
| 197 | + * @param handle customizable argument. In this examples is used in |
| 198 | + * order to select the correct sensor bus handler. |
| 199 | + * @param reg register to write |
| 200 | + * @param bufp pointer to data to write in register reg |
| 201 | + * @param len number of consecutive register to write |
| 202 | + * |
| 203 | + */ |
| 204 | +static int32_t platform_write(void *handle, uint8_t reg, uint8_t *bufp, |
| 205 | + uint16_t len) |
| 206 | +{ |
| 207 | +#if defined(NUCLEO_F411RE) |
| 208 | + HAL_I2C_Mem_Write(handle, LPS27HHW_I2C_ADD_H, reg, |
| 209 | + I2C_MEMADD_SIZE_8BIT, bufp, len, 1000); |
| 210 | +#elif defined(STEVAL_MKI109V3) |
| 211 | + HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_RESET); |
| 212 | + HAL_SPI_Transmit(handle, ®, 1, 1000); |
| 213 | + HAL_SPI_Transmit(handle, bufp, len, 1000); |
| 214 | + HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_SET); |
| 215 | +#elif defined(SPC584B_DIS) |
| 216 | + i2c_lld_write(handle, LPS27HHW_I2C_ADD_H & 0xFE, reg, bufp, len); |
| 217 | +#endif |
| 218 | + return 0; |
| 219 | +} |
| 220 | + |
| 221 | +/* |
| 222 | + * @brief Read generic device register (platform dependent) |
| 223 | + * |
| 224 | + * @param handle customizable argument. In this examples is used in |
| 225 | + * order to select the correct sensor bus handler. |
| 226 | + * @param reg register to read |
| 227 | + * @param bufp pointer to buffer that store the data read |
| 228 | + * @param len number of consecutive register to read |
| 229 | + * |
| 230 | + */ |
| 231 | +static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, |
| 232 | + uint16_t len) |
| 233 | +{ |
| 234 | +#if defined(NUCLEO_F411RE) |
| 235 | + HAL_I2C_Mem_Read(handle, LPS27HHW_I2C_ADD_H, reg, |
| 236 | + I2C_MEMADD_SIZE_8BIT, bufp, len, 1000); |
| 237 | +#elif defined(STEVAL_MKI109V3) |
| 238 | + reg |= 0x80; |
| 239 | + HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_RESET); |
| 240 | + HAL_SPI_Transmit(handle, ®, 1, 1000); |
| 241 | + HAL_SPI_Receive(handle, bufp, len, 1000); |
| 242 | + HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_SET); |
| 243 | +#elif defined(SPC584B_DIS) |
| 244 | + i2c_lld_read(handle, LPS27HHW_I2C_ADD_H & 0xFE, reg, bufp, len); |
| 245 | +#endif |
| 246 | + return 0; |
| 247 | +} |
| 248 | + |
| 249 | +/* |
| 250 | + * @brief Write generic device register (platform dependent) |
| 251 | + * |
| 252 | + * @param tx_buffer buffer to trasmit |
| 253 | + * @param len number of byte to send |
| 254 | + * |
| 255 | + */ |
| 256 | +static void tx_com(uint8_t *tx_buffer, uint16_t len) |
| 257 | +{ |
| 258 | +#if defined(NUCLEO_F411RE) |
| 259 | + HAL_UART_Transmit(&huart2, tx_buffer, len, 1000); |
| 260 | +#elif defined(STEVAL_MKI109V3) |
| 261 | + CDC_Transmit_FS(tx_buffer, len); |
| 262 | +#elif defined(SPC584B_DIS) |
| 263 | + sd_lld_write(&SD2, tx_buffer, len); |
| 264 | +#endif |
| 265 | +} |
| 266 | + |
| 267 | +/* |
| 268 | + * @brief platform specific delay (platform dependent) |
| 269 | + * |
| 270 | + * @param ms delay in ms |
| 271 | + * |
| 272 | + */ |
| 273 | +static void platform_delay(uint32_t ms) |
| 274 | +{ |
| 275 | +#if defined(NUCLEO_F411RE) | defined(STEVAL_MKI109V3) |
| 276 | + HAL_Delay(ms); |
| 277 | +#elif defined(SPC584B_DIS) |
| 278 | + osalThreadDelayMilliseconds(ms); |
| 279 | +#endif |
| 280 | +} |
| 281 | + |
| 282 | +/* |
| 283 | + * @brief platform specific initialization (platform dependent) |
| 284 | + */ |
| 285 | +static void platform_init(void) |
| 286 | +{ |
| 287 | +#if defined(STEVAL_MKI109V3) |
| 288 | + TIM3->CCR1 = PWM_3V3; |
| 289 | + TIM3->CCR2 = PWM_3V3; |
| 290 | + HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1); |
| 291 | + HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2); |
| 292 | + HAL_Delay(1000); |
| 293 | +#endif |
| 294 | +} |
| 295 | + |
0 commit comments