accelerometer_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2013, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <string.h>
  31. #include <math.h>
  32. #include "accelerometer/mma8451.h"
  33. #include "board_i2c.h"
  34. #include "gpio/gpio.h"
  35. #include "timer/timer.h"
  36. ////////////////////////////////////////////////////////////////////////////////
  37. // Code
  38. ////////////////////////////////////////////////////////////////////////////////
  39. void accelerometer_test(void)
  40. {
  41. mma8451_device_t dev;
  42. printf("\n== Accelerometer Test ==\n\n");
  43. #if BOARD_SMART_DEVICE || BOARD_SABRE_AI
  44. #if defined(BOARD_SMART_DEVICE)
  45. // Make sure Audio codec, also on I2C1, is powered up to
  46. // allow I2C1 bus to work properly.
  47. //
  48. //CODEC PWR_EN, key_col12
  49. gpio_set_gpio(GPIO_PORT4, 10);
  50. gpio_set_direction(GPIO_PORT4, 10, GPIO_GDIR_OUTPUT);
  51. gpio_set_level(GPIO_PORT4, 10, GPIO_HIGH_LEVEL);
  52. // SENSOR_PWR_EN
  53. gpio_set_gpio(GPIO_PORT2, 31);
  54. gpio_set_direction(GPIO_PORT2, 31, GPIO_GDIR_OUTPUT);
  55. gpio_set_level(GPIO_PORT2, 31, GPIO_LOW_LEVEL);
  56. hal_delay_us(1000);
  57. gpio_set_level(GPIO_PORT2, 31, GPIO_HIGH_LEVEL);
  58. #endif
  59. int err = mma8451_init(&dev, &g_mma8451_i2c_device);
  60. if (err)
  61. {
  62. printf("Failed to initialize the MMA8451 (err=%d)!\n", err);
  63. return;
  64. }
  65. printf("Reading acceleration data... Press any key to stop.\n");
  66. while (true)
  67. {
  68. // Read acceleration data.
  69. acceleration_t accel;
  70. err = mma8451_get_acceleration(&dev, &accel);
  71. if (err)
  72. {
  73. printf("Failed to read acceleration data (err=%d)!\n", err);
  74. return;
  75. }
  76. // Having problems with float format specifiers, so we're just converting to
  77. // decimal before printing.
  78. char buf[128];
  79. float integralPart;
  80. sprintf(buf, "x=%+d.%03d, y=%+d.%03d, z=%+d.%03d",
  81. (int)accel.x, (int)(fabsf(modff(accel.x, &integralPart)) * 1000.0),
  82. (int)accel.y, (int)(fabsf(modff(accel.y, &integralPart)) * 1000.0),
  83. (int)accel.z, (int)(fabsf(modff(accel.z, &integralPart)) * 1000.0));
  84. printf("%s", buf);
  85. fflush(stdout);
  86. // Check if we should exit.
  87. if (getchar() != NONE_CHAR)
  88. {
  89. break;
  90. }
  91. // Delay 50 ms.
  92. hal_delay_us(50000);
  93. // Back up to start of the line by outputting a bunch of backspace chars.
  94. int len = strlen(buf);
  95. memset(buf, 0x08, sizeof(buf));
  96. buf[len] = 0;
  97. fputs(buf, stdout);
  98. fflush(stdout);
  99. }
  100. printf("\n\n");
  101. #else // BOARD_SMART_DEVICE || BOARD_SABRE_AI
  102. printf("Sorry, no accelerometer available on this board.\n");
  103. #endif
  104. }
  105. ////////////////////////////////////////////////////////////////////////////////
  106. // EOF
  107. ////////////////////////////////////////////////////////////////////////////////