|
| 1 | +/* |
| 2 | + ****************************************************************************** |
| 3 | + * @file tmos_data_drdy.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) 2021 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 |
| 26 | + * - NUCLEO_F411RE |
| 27 | + * - DISCOVERY_SPC584B |
| 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(N/A) |
| 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 | + |
| 65 | +#if defined(STEVAL_MKI109V3) |
| 66 | +/* MKI109V3: Define communication interface */ |
| 67 | +#define SENSOR_BUS hspi2 |
| 68 | +/* MKI109V3: Vdd and Vddio power supply values at 1V8 */ |
| 69 | +#define PWM_1V8 500 /* ((1.8 / 3.6) * htim3.Init.Period) */ |
| 70 | + |
| 71 | +#elif defined(NUCLEO_F411RE) |
| 72 | +/* NUCLEO_F411RE: Define communication interface */ |
| 73 | +#define SENSOR_BUS hi2c1 |
| 74 | + |
| 75 | +#elif defined(SPC584B_DIS) |
| 76 | +/* DISCOVERY_SPC584B: Define communication interface */ |
| 77 | +#define SENSOR_BUS I2CD1 |
| 78 | + |
| 79 | +#endif |
| 80 | + |
| 81 | +/* Includes ------------------------------------------------------------------*/ |
| 82 | +#include <string.h> |
| 83 | +#include <stdio.h> |
| 84 | +#include "sths34pf80_reg.h" |
| 85 | + |
| 86 | +#if defined(NUCLEO_F411RE) |
| 87 | +#include "stm32f4xx_hal.h" |
| 88 | +#include "usart.h" |
| 89 | +#include "gpio.h" |
| 90 | +#include "i2c.h" |
| 91 | + |
| 92 | +#elif defined(STEVAL_MKI109V3) |
| 93 | +#include "stm32f4xx_hal.h" |
| 94 | +#include "usbd_cdc_if.h" |
| 95 | +#include "gpio.h" |
| 96 | +#include "spi.h" |
| 97 | +#include "tim.h" |
| 98 | + |
| 99 | +#elif defined(SPC584B_DIS) |
| 100 | +#include "components.h" |
| 101 | +#endif |
| 102 | + |
| 103 | +/* Private macro -------------------------------------------------------------*/ |
| 104 | +#define BOOT_TIME 10 //ms |
| 105 | + |
| 106 | +/* Private variables ---------------------------------------------------------*/ |
| 107 | +static uint8_t tx_buffer[1000]; |
| 108 | +//static sths34pf80_data_t data; |
| 109 | + |
| 110 | +/* Extern variables ----------------------------------------------------------*/ |
| 111 | + |
| 112 | +/* Private functions ---------------------------------------------------------*/ |
| 113 | +/* |
| 114 | + * WARNING: |
| 115 | + * Functions declare in this section are defined at the end of this file |
| 116 | + * and are strictly related to the hardware platform used. |
| 117 | + * |
| 118 | + */ |
| 119 | +static int32_t platform_write(void *handle, uint8_t reg, const uint8_t *bufp, |
| 120 | + uint16_t len); |
| 121 | +static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, |
| 122 | + uint16_t len); |
| 123 | +static void tx_com( uint8_t *tx_buffer, uint16_t len ); |
| 124 | +static void platform_delay(uint32_t ms); |
| 125 | +static void platform_init(void); |
| 126 | + |
| 127 | +static stmdev_ctx_t dev_ctx; |
| 128 | + |
| 129 | +void sths34pf80_tmos_data_drdy_handler(void) |
| 130 | +{ |
| 131 | + sths34pf80_tmos_drdy_status_t status; |
| 132 | + sths34pf80_tmos_func_status_t func_status; |
| 133 | + |
| 134 | + sths34pf80_tmos_drdy_status_get(&dev_ctx, &status); |
| 135 | + if (status.drdy) |
| 136 | + { |
| 137 | + sths34pf80_tmos_func_status_get(&dev_ctx, &func_status); |
| 138 | + |
| 139 | + sprintf((char *)tx_buffer, "TAmbient Shock: %d - Presence: %d - Motion: %d\r\n", |
| 140 | + func_status.tamb_shock_flag, func_status.pres_flag, func_status.mot_flag); |
| 141 | + tx_com(tx_buffer, strlen((char const *)tx_buffer)); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +/* Main Example --------------------------------------------------------------*/ |
| 146 | +void sths34pf80_tmos_data_drdy(void) |
| 147 | +{ |
| 148 | + uint8_t whoami; |
| 149 | + sths34pf80_lpf_bandwidth_t lpf_m, lpf_p, lpf_p_m, lpf_a_t; |
| 150 | + |
| 151 | + /* Initialize mems driver interface */ |
| 152 | + dev_ctx.write_reg = platform_write; |
| 153 | + dev_ctx.read_reg = platform_read; |
| 154 | + dev_ctx.handle = &SENSOR_BUS; |
| 155 | + |
| 156 | + /* Initialize platform specific hardware */ |
| 157 | + platform_init(); |
| 158 | + |
| 159 | + /* Wait sensor boot time */ |
| 160 | + platform_delay(BOOT_TIME); |
| 161 | + |
| 162 | + /* Check device ID */ |
| 163 | + sths34pf80_device_id_get(&dev_ctx, &whoami); |
| 164 | + if (whoami != STHS34PF80_ID) |
| 165 | + while(1); |
| 166 | + |
| 167 | + /* Set averages (AVG_TAMB = 8, AVG_TMOS = 32) */ |
| 168 | + sths34pf80_avg_tobject_num_set(&dev_ctx, STHS34PF80_AVG_TMOS_32); |
| 169 | + sths34pf80_avg_tambient_num_set(&dev_ctx, STHS34PF80_AVG_T_8); |
| 170 | + |
| 171 | + /* read filters */ |
| 172 | + sths34pf80_lpf_m_bandwidth_get(&dev_ctx, &lpf_m); |
| 173 | + sths34pf80_lpf_p_bandwidth_get(&dev_ctx, &lpf_p); |
| 174 | + sths34pf80_lpf_p_m_bandwidth_get(&dev_ctx, &lpf_p_m); |
| 175 | + sths34pf80_lpf_a_t_bandwidth_get(&dev_ctx, &lpf_a_t); |
| 176 | + |
| 177 | + sprintf((char *)tx_buffer, |
| 178 | + "lpf_m: %02d, lpf_p: %02d, lpf_p_m: %02d, lpf_a_t: %02d\r\n", lpf_m, lpf_p, lpf_p_m, lpf_a_t); |
| 179 | + tx_com(tx_buffer, strlen((char const *)tx_buffer)); |
| 180 | + |
| 181 | + /* Set BDU */ |
| 182 | + sths34pf80_block_data_update_set(&dev_ctx, 1); |
| 183 | + |
| 184 | + /* Set interrupt */ |
| 185 | + sths34pf80_tmos_route_int_set(&dev_ctx, STHS34PF80_TMOS_INT_DRDY); |
| 186 | + |
| 187 | + /* Set ODR */ |
| 188 | + sths34pf80_tmos_odr_set(&dev_ctx, STHS34PF80_TMOS_ODR_AT_8Hz); |
| 189 | + |
| 190 | + /* Read samples in drdy handler */ |
| 191 | + while(1); |
| 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, const uint8_t *bufp, |
| 205 | + uint16_t len) |
| 206 | +{ |
| 207 | +#if defined(NUCLEO_F411RE) |
| 208 | + HAL_I2C_Mem_Write(handle, STHS34PF80_I2C_ADD, reg, |
| 209 | + I2C_MEMADD_SIZE_8BIT, (uint8_t *)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, (uint8_t *)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, STHS34PF80_I2C_ADD & 0xFE, reg, (uint8_t *)bufp, len); |
| 217 | +#endif |
| 218 | + return 0; |
| 219 | +} |
| 220 | + |
| 221 | +#if defined(STEVAL_MKI109V3) |
| 222 | +static void SPI_3W_Read(SPI_HandleTypeDef* xSpiHandle, uint8_t *val) |
| 223 | +{ |
| 224 | + __disable_irq(); |
| 225 | + |
| 226 | + __HAL_SPI_ENABLE(xSpiHandle); |
| 227 | + __asm("dsb\n"); |
| 228 | + __asm("dsb\n"); |
| 229 | + __HAL_SPI_DISABLE(xSpiHandle); |
| 230 | + |
| 231 | + __enable_irq(); |
| 232 | + |
| 233 | + while ((xSpiHandle->Instance->SR & SPI_FLAG_RXNE) != SPI_FLAG_RXNE); |
| 234 | + /* read the received data */ |
| 235 | + *val = *(__IO uint8_t *) &xSpiHandle->Instance->DR; |
| 236 | + while ((xSpiHandle->Instance->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY); |
| 237 | +} |
| 238 | + |
| 239 | +static void SPI_3W_Receive(uint8_t *pBuffer, uint16_t nBytesToRead) |
| 240 | +{ |
| 241 | + __HAL_SPI_DISABLE(&hspi2); |
| 242 | + SPI_1LINE_RX(&hspi2); |
| 243 | + |
| 244 | + for(uint16_t i = 0; i < nBytesToRead; i++) { |
| 245 | + SPI_3W_Read(&hspi2, pBuffer++); |
| 246 | + } |
| 247 | + |
| 248 | + SPI_1LINE_TX(&hspi2); |
| 249 | + __HAL_SPI_ENABLE(&hspi2); |
| 250 | +} |
| 251 | +#endif |
| 252 | + |
| 253 | +/* |
| 254 | + * @brief Read generic device register (platform dependent) |
| 255 | + * |
| 256 | + * @param handle customizable argument. In this examples is used in |
| 257 | + * order to select the correct sensor bus handler. |
| 258 | + * @param reg register to read |
| 259 | + * @param bufp pointer to buffer that store the data read |
| 260 | + * @param len number of consecutive register to read |
| 261 | + * |
| 262 | + */ |
| 263 | +static int32_t platform_read(void *handle, uint8_t reg, uint8_t *bufp, |
| 264 | + uint16_t len) |
| 265 | +{ |
| 266 | +#if defined(NUCLEO_F411RE) |
| 267 | + HAL_I2C_Mem_Read(handle, STHS34PF80_I2C_ADD, reg, |
| 268 | + I2C_MEMADD_SIZE_8BIT, bufp, len, 1000); |
| 269 | +#elif defined(STEVAL_MKI109V3) |
| 270 | + reg |= 0x80; |
| 271 | + HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_RESET); |
| 272 | + HAL_SPI_Transmit(handle, ®, 1, 1000); |
| 273 | + //HAL_SPI_Receive(handle, bufp, len, 1000); |
| 274 | + SPI_3W_Receive(bufp, len); |
| 275 | + HAL_GPIO_WritePin(CS_up_GPIO_Port, CS_up_Pin, GPIO_PIN_SET); |
| 276 | +#elif defined(SPC584B_DIS) |
| 277 | + i2c_lld_read(handle, STHS34PF80_I2C_ADD & 0xFE, reg, bufp, len); |
| 278 | +#endif |
| 279 | + return 0; |
| 280 | +} |
| 281 | + |
| 282 | +/* |
| 283 | + * @brief Write generic device register (platform dependent) |
| 284 | + * |
| 285 | + * @param tx_buffer buffer to trasmit |
| 286 | + * @param len number of byte to send |
| 287 | + * |
| 288 | + */ |
| 289 | +static void tx_com(uint8_t *tx_buffer, uint16_t len) |
| 290 | +{ |
| 291 | +#if defined(NUCLEO_F411RE) |
| 292 | + HAL_UART_Transmit(&huart2, tx_buffer, len, 1000); |
| 293 | +#elif defined(STEVAL_MKI109V3) |
| 294 | + CDC_Transmit_FS(tx_buffer, len); |
| 295 | +#elif defined(SPC584B_DIS) |
| 296 | + sd_lld_write(&SD2, tx_buffer, len); |
| 297 | +#endif |
| 298 | +} |
| 299 | + |
| 300 | +/* |
| 301 | + * @brief platform specific delay (platform dependent) |
| 302 | + * |
| 303 | + * @param ms delay in ms |
| 304 | + * |
| 305 | + */ |
| 306 | +static void platform_delay(uint32_t ms) |
| 307 | +{ |
| 308 | +#if defined(NUCLEO_F411RE) | defined(STEVAL_MKI109V3) |
| 309 | + HAL_Delay(ms); |
| 310 | +#elif defined(SPC584B_DIS) |
| 311 | + osalThreadDelayMilliseconds(ms); |
| 312 | +#endif |
| 313 | +} |
| 314 | + |
| 315 | +/* |
| 316 | + * @brief platform specific initialization (platform dependent) |
| 317 | + */ |
| 318 | +static void platform_init(void) |
| 319 | +{ |
| 320 | +#if defined(STEVAL_MKI109V3) |
| 321 | + TIM3->CCR1 = PWM_1V8; |
| 322 | + TIM3->CCR2 = PWM_1V8; |
| 323 | + HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1); |
| 324 | + HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2); |
| 325 | + HAL_Delay(1000); |
| 326 | +#endif |
| 327 | +} |
0 commit comments