[运动传感器] LSM6DSL的BlueNRG-1测试代码

littleshrimp   2018-7-31 13:59 楼主

传感器驱动使用STMems_Standard_C_drivers
BlueNRG-1的SDK_EVAL_I2C.c库使用的是7位设备地址,使用lsm6dsl_reg.h的LSM6DSL_I2C_ADD_x时需要或移一位转成7位地址


  1. /******************** (C) COPYRIGHT 2015 STMicroelectronics ********************
  2. * File Name          : I2C/Master_Polling/main.c
  3. * Author             : RF Application Team
  4. * Version            : V1.0.0
  5. * Date               : September-2015
  6. * Description        : Code demostrating the I2C master functionality
  7. ********************************************************************************
  8. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  9. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  10. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  11. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  12. * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  13. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  14. *******************************************************************************/


  15. /* Includes ------------------------------------------------------------------*/
  16. #include "BlueNRG_x_device.h"
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "BlueNRG1_conf.h"
  20. #include "SDK_EVAL_Config.h"
  21. #include "lsm6dsl_reg.h"

  22. /** @addtogroup BlueNRG1_StdPeriph_Examples BlueNRG1 Standard Peripheral Examples
  23.   * @{
  24.   */

  25. /** @addtogroup I2C_Examples I2C Examples
  26.   * @{
  27.   */

  28. /** @addtogroup I2C_Master I2C Master
  29.   * @{
  30.   */

  31. /* Private typedef -----------------------------------------------------------*/
  32. typedef enum {
  33.   I2C_OP_FAILED = 0,
  34.   I2C_OP_SUCCEED
  35. }
  36. I2C_AppOpStatus;

  37. /* Private define ------------------------------------------------------------*/
  38. #define TOGGLE_DL1                         '1'
  39. #define TOGGLE_DL2                         '2'
  40. #define TOGGLE_DL3                         '3'

  41. /* I2C Device address */
  42. #define DEV_ADDRESS             0xBE

  43. /* I2C clock frequency */
  44. #define SDK_EVAL_I2C_CLK_SPEED  (10000)

  45. #define TX_BUF_DIM          1000
  46. #define MAX_BUF_SIZE 256

  47. /* Private macro -------------------------------------------------------------*/
  48. /* Private variables ---------------------------------------------------------*/
  49. /* Private function prototypes -----------------------------------------------*/



  50. static axis3bit16_t data_raw_acceleration;
  51. static axis3bit16_t data_raw_angular_rate;
  52. static axis1bit16_t data_raw_temperature;
  53. static float acceleration_mg[3];
  54. static float angular_rate_mdps[3];
  55. static float temperature_degC;
  56. static uint8_t whoamI, rst;
  57. static char tx_buffer[TX_BUF_DIM];
  58. lsm6dsl_ctx_t dev_ctx;
  59. /* Private functions ---------------------------------------------------------*/
  60. static int32_t platform_write(void *handle, uint8_t Reg, uint8_t *Bufp,
  61.                               uint16_t len)
  62. {
  63.   SdkEvalI2CWrite(Bufp, LSM6DSL_I2C_ADD_L >> 1, Reg, len);
  64.   return 0;
  65. }

  66. static int32_t platform_read(void *handle, uint8_t Reg, uint8_t *Bufp,
  67.                              uint16_t len)
  68. {
  69.   SdkEvalI2CRead(Bufp, LSM6DSL_I2C_ADD_L >> 1, Reg, len);
  70.   return 0;
  71. }

  72. /* Main Example --------------------------------------------------------------*/
  73. void example_main(void)
  74. {
  75.   /*
  76.    *  Initialize mems driver interface
  77.    */
  78.   dev_ctx.write_reg = platform_write;
  79.   dev_ctx.read_reg = platform_read;
  80.   
  81.   SdkEvalI2CInit(10000);
  82.   printf("i2c init");
  83.   /*
  84.    *  Check device ID
  85.    */
  86.   whoamI = 0;
  87.   lsm6dsl_device_id_get(&dev_ctx, &whoamI);
  88.   
  89.   printf("%d",whoamI);
  90.   if ( whoamI != LSM6DSL_ID )
  91.     while(1); /*manage here device not found */
  92.   /*
  93.    *  Restore default configuration
  94.    */
  95.   lsm6dsl_reset_set(&dev_ctx, PROPERTY_ENABLE);
  96.   do {
  97.     lsm6dsl_reset_get(&dev_ctx, &rst);
  98.   } while (rst);
  99.   /*
  100.    *  Enable Block Data Update
  101.    */
  102.   lsm6dsl_block_data_update_set(&dev_ctx, PROPERTY_ENABLE);
  103.   /*
  104.    * Set Output Data Rate
  105.    */
  106.   lsm6dsl_xl_data_rate_set(&dev_ctx, LSM6DSL_XL_ODR_12Hz5);
  107.   lsm6dsl_gy_data_rate_set(&dev_ctx, LSM6DSL_GY_ODR_12Hz5);
  108.   /*
  109.    * Set full scale
  110.    */  
  111.   lsm6dsl_xl_full_scale_set(&dev_ctx, LSM6DSL_2g);
  112.   lsm6dsl_gy_full_scale_set(&dev_ctx, LSM6DSL_2000dps);
  113.   
  114.   /*
  115.    * Configure filtering chain(No aux interface)
  116.    */  
  117.   /* Accelerometer - analog filter */
  118.   lsm6dsl_xl_filter_analog_set(&dev_ctx, LSM6DSL_XL_ANA_BW_400Hz);
  119.   
  120.   /* Accelerometer - LPF1 path ( LPF2 not used )*/
  121.   //lsm6dsl_xl_lp1_bandwidth_set(&dev_ctx, LSM6DSL_XL_LP1_ODR_DIV_4);
  122.   
  123.   /* Accelerometer - LPF1 + LPF2 path */   
  124.   lsm6dsl_xl_lp2_bandwidth_set(&dev_ctx, LSM6DSL_XL_LOW_NOISE_LP_ODR_DIV_100);
  125.   
  126.   /* Accelerometer - High Pass / Slope path */
  127.   //lsm6dsl_xl_reference_mode_set(&dev_ctx, PROPERTY_DISABLE);
  128.   //lsm6dsl_xl_hp_bandwidth_set(&dev_ctx, LSM6DSL_XL_HP_ODR_DIV_100);
  129.   
  130.   /* Gyroscope - filtering chain */
  131.   lsm6dsl_gy_band_pass_set(&dev_ctx, LSM6DSL_HP_260mHz_LP1_STRONG);
  132.   
  133.   /*
  134.    * Read samples in polling mode (no int)
  135.    */
  136.   while(1)
  137.   {
  138.     /*
  139.      * Read output only if new value is available
  140.      */
  141.     lsm6dsl_reg_t reg;
  142.     lsm6dsl_status_reg_get(&dev_ctx, ®.status_reg);

  143.     if (reg.status_reg.xlda)
  144.     {
  145.       /* Read magnetic field data */
  146.       memset(data_raw_acceleration.u8bit, 0x00, 3*sizeof(int16_t));
  147.       lsm6dsl_acceleration_raw_get(&dev_ctx, data_raw_acceleration.u8bit);
  148.       acceleration_mg[0] = LSM6DSL_FROM_FS_2g_TO_mg( data_raw_acceleration.i16bit[0]);
  149.       acceleration_mg[1] = LSM6DSL_FROM_FS_2g_TO_mg( data_raw_acceleration.i16bit[1]);
  150.       acceleration_mg[2] = LSM6DSL_FROM_FS_2g_TO_mg( data_raw_acceleration.i16bit[2]);
  151.       
  152.       sprintf((char*)tx_buffer, "Acceleration [mg]:%4.2f\t%4.2f\t%4.2f\r\n",
  153.               acceleration_mg[0], acceleration_mg[1], acceleration_mg[2]);
  154.       printf( tx_buffer);
  155.     }
  156.     if (reg.status_reg.gda)
  157.     {
  158.       /* Read magnetic field data */
  159.       memset(data_raw_angular_rate.u8bit, 0x00, 3*sizeof(int16_t));
  160.       lsm6dsl_angular_rate_raw_get(&dev_ctx, data_raw_angular_rate.u8bit);
  161.       angular_rate_mdps[0] = LSM6DSL_FROM_FS_2000dps_TO_mdps(data_raw_angular_rate.i16bit[0]);
  162.       angular_rate_mdps[1] = LSM6DSL_FROM_FS_2000dps_TO_mdps(data_raw_angular_rate.i16bit[1]);
  163.       angular_rate_mdps[2] = LSM6DSL_FROM_FS_2000dps_TO_mdps(data_raw_angular_rate.i16bit[2]);
  164.       
  165.       sprintf((char*)tx_buffer, "Angular rate [mdps]:%4.2f\t%4.2f\t%4.2f\r\n",
  166.               angular_rate_mdps[0], angular_rate_mdps[1], angular_rate_mdps[2]);
  167.       printf( tx_buffer);
  168.     }   
  169.     if (reg.status_reg.tda)
  170.     {   
  171.       /* Read temperature data */
  172.       memset(data_raw_temperature.u8bit, 0x00, sizeof(int16_t));
  173.       lsm6dsl_temperature_raw_get(&dev_ctx, data_raw_temperature.u8bit);
  174.       temperature_degC = LSM6DSL_FROM_LSB_TO_degC( data_raw_temperature.i16bit );
  175.       
  176.       sprintf((char*)tx_buffer, "Temperature [degC]:%6.2f\r\n", temperature_degC );
  177.       printf( tx_buffer);
  178.     }
  179.   }
  180. }
  181. /**
  182. * [url=home.php?mod=space&uid=159083]@brief[/url]  Main program code
  183. * @param  None
  184. * @retval None
  185. */
  186. int main(void)
  187. {
  188.   /* System initialization function */
  189.   SystemInit();
  190.   
  191.   /* Identify BlueNRG1 platform */
  192.   SdkEvalIdentification();
  193.   
  194.   /* UART initialization */
  195.   SdkEvalComUartInit(UART_BAUDRATE);
  196.      
  197.   /* Infinite loop for process command */
  198.   example_main();
  199. }


  200. #ifdef USE_FULL_ASSERT

  201. /**
  202.   * @brief  Reports the name of the source file and the source line number
  203.   *         where the assert_param error has occurred.
  204.   * @param  file: pointer to the source file name
  205.   * @param  line: assert_param error line source number
  206.   */
  207. void assert_failed(uint8_t* file, uint32_t line)
  208. {
  209.   /* User can add his own implementation to report the file name and line number,
  210.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  211.   /* Infinite loop */
  212.   while (1)
  213.   {
  214.   }
  215. }

  216. #endif

  217. /**
  218.   * @}
  219.   */

  220. /**
  221.   * @}
  222.   */

  223. /**
  224.   * @}
  225.   */

  226. /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/



游客,如果您要查看本帖隐藏内容请回复

虾扯蛋,蛋扯虾,虾扯蛋扯虾

回复评论 (2)

不错的代码,我也正在学习
点赞  2018-10-29 15:15

学习下最近项目有遇到

点赞  2019-6-19 19:05
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复