accel.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "py/mphal.h"
  29. #include "py/runtime.h"
  30. #include "pin.h"
  31. #include "i2c.h"
  32. #include "accel.h"
  33. #if MICROPY_HW_HAS_MMA7660
  34. /// \moduleref pyb
  35. /// \class Accel - accelerometer control
  36. ///
  37. /// Accel is an object that controls the accelerometer. Example usage:
  38. ///
  39. /// accel = pyb.Accel()
  40. /// for i in range(10):
  41. /// print(accel.x(), accel.y(), accel.z())
  42. ///
  43. /// Raw values are between -32 and 31.
  44. #define MMA_ADDR (76)
  45. #define MMA_REG_X (0)
  46. #define MMA_REG_Y (1)
  47. #define MMA_REG_Z (2)
  48. #define MMA_REG_TILT (3)
  49. #define MMA_REG_MODE (7)
  50. #define MMA_AXIS_SIGNED_VALUE(i) (((i) & 0x3f) | ((i) & 0x20 ? (~0x1f) : 0))
  51. void accel_init(void) {
  52. // PB5 is connected to AVDD; pull high to enable MMA accel device
  53. mp_hal_pin_low(MICROPY_HW_MMA_AVDD_PIN); // turn off AVDD
  54. mp_hal_pin_output(MICROPY_HW_MMA_AVDD_PIN);
  55. }
  56. STATIC void accel_start(void) {
  57. // start the I2C bus in master mode
  58. i2c_init(I2C1, MICROPY_HW_I2C1_SCL, MICROPY_HW_I2C1_SDA, 400000);
  59. // turn off AVDD, wait 30ms, turn on AVDD, wait 30ms again
  60. mp_hal_pin_low(MICROPY_HW_MMA_AVDD_PIN); // turn off
  61. mp_hal_delay_ms(30);
  62. mp_hal_pin_high(MICROPY_HW_MMA_AVDD_PIN); // turn on
  63. mp_hal_delay_ms(30);
  64. int ret;
  65. for (int i = 0; i < 4; i++) {
  66. ret = i2c_writeto(I2C1, MMA_ADDR, NULL, 0, true);
  67. if (ret == 0) {
  68. break;
  69. }
  70. }
  71. if (ret != 0) {
  72. mp_raise_msg(&mp_type_OSError, "accelerometer not found");
  73. }
  74. // set MMA to active mode
  75. uint8_t data[2] = {MMA_REG_MODE, 1}; // active mode
  76. i2c_writeto(I2C1, MMA_ADDR, data, 2, true);
  77. // wait for MMA to become active
  78. mp_hal_delay_ms(30);
  79. }
  80. /******************************************************************************/
  81. /* MicroPython bindings */
  82. #define NUM_AXIS (3)
  83. #define FILT_DEPTH (4)
  84. typedef struct _pyb_accel_obj_t {
  85. mp_obj_base_t base;
  86. int16_t buf[NUM_AXIS * FILT_DEPTH];
  87. } pyb_accel_obj_t;
  88. STATIC pyb_accel_obj_t pyb_accel_obj;
  89. /// \classmethod \constructor()
  90. /// Create and return an accelerometer object.
  91. ///
  92. /// Note: if you read accelerometer values immediately after creating this object
  93. /// you will get 0. It takes around 20ms for the first sample to be ready, so,
  94. /// unless you have some other code between creating this object and reading its
  95. /// values, you should put a `pyb.delay(20)` after creating it. For example:
  96. ///
  97. /// accel = pyb.Accel()
  98. /// pyb.delay(20)
  99. /// print(accel.x())
  100. STATIC mp_obj_t pyb_accel_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  101. // check arguments
  102. mp_arg_check_num(n_args, n_kw, 0, 0, false);
  103. // init accel object
  104. pyb_accel_obj.base.type = &pyb_accel_type;
  105. accel_start();
  106. return MP_OBJ_FROM_PTR(&pyb_accel_obj);
  107. }
  108. STATIC mp_obj_t read_axis(int axis) {
  109. uint8_t data[1] = { axis };
  110. i2c_writeto(I2C1, MMA_ADDR, data, 1, false);
  111. i2c_readfrom(I2C1, MMA_ADDR, data, 1, true);
  112. return mp_obj_new_int(MMA_AXIS_SIGNED_VALUE(data[0]));
  113. }
  114. /// \method x()
  115. /// Get the x-axis value.
  116. STATIC mp_obj_t pyb_accel_x(mp_obj_t self_in) {
  117. return read_axis(MMA_REG_X);
  118. }
  119. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_x_obj, pyb_accel_x);
  120. /// \method y()
  121. /// Get the y-axis value.
  122. STATIC mp_obj_t pyb_accel_y(mp_obj_t self_in) {
  123. return read_axis(MMA_REG_Y);
  124. }
  125. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_y_obj, pyb_accel_y);
  126. /// \method z()
  127. /// Get the z-axis value.
  128. STATIC mp_obj_t pyb_accel_z(mp_obj_t self_in) {
  129. return read_axis(MMA_REG_Z);
  130. }
  131. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_z_obj, pyb_accel_z);
  132. /// \method tilt()
  133. /// Get the tilt register.
  134. STATIC mp_obj_t pyb_accel_tilt(mp_obj_t self_in) {
  135. uint8_t data[1] = { MMA_REG_TILT };
  136. i2c_writeto(I2C1, MMA_ADDR, data, 1, false);
  137. i2c_readfrom(I2C1, MMA_ADDR, data, 1, true);
  138. return mp_obj_new_int(data[0]);
  139. }
  140. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_tilt_obj, pyb_accel_tilt);
  141. /// \method filtered_xyz()
  142. /// Get a 3-tuple of filtered x, y and z values.
  143. STATIC mp_obj_t pyb_accel_filtered_xyz(mp_obj_t self_in) {
  144. pyb_accel_obj_t *self = MP_OBJ_TO_PTR(self_in);
  145. memmove(self->buf, self->buf + NUM_AXIS, NUM_AXIS * (FILT_DEPTH - 1) * sizeof(int16_t));
  146. uint8_t data[NUM_AXIS] = { MMA_REG_X };
  147. i2c_writeto(I2C1, MMA_ADDR, data, 1, false);
  148. i2c_readfrom(I2C1, MMA_ADDR, data, 3, true);
  149. mp_obj_t tuple[NUM_AXIS];
  150. for (int i = 0; i < NUM_AXIS; i++) {
  151. self->buf[NUM_AXIS * (FILT_DEPTH - 1) + i] = MMA_AXIS_SIGNED_VALUE(data[i]);
  152. int32_t val = 0;
  153. for (int j = 0; j < FILT_DEPTH; j++) {
  154. val += self->buf[i + NUM_AXIS * j];
  155. }
  156. tuple[i] = mp_obj_new_int(val);
  157. }
  158. return mp_obj_new_tuple(3, tuple);
  159. }
  160. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_filtered_xyz_obj, pyb_accel_filtered_xyz);
  161. STATIC mp_obj_t pyb_accel_read(mp_obj_t self_in, mp_obj_t reg) {
  162. uint8_t data[1] = { mp_obj_get_int(reg) };
  163. i2c_writeto(I2C1, MMA_ADDR, data, 1, false);
  164. i2c_writeto(I2C1, MMA_ADDR, data, 1, true);
  165. return mp_obj_new_int(data[0]);
  166. }
  167. MP_DEFINE_CONST_FUN_OBJ_2(pyb_accel_read_obj, pyb_accel_read);
  168. STATIC mp_obj_t pyb_accel_write(mp_obj_t self_in, mp_obj_t reg, mp_obj_t val) {
  169. uint8_t data[2] = { mp_obj_get_int(reg), mp_obj_get_int(val) };
  170. i2c_writeto(I2C1, MMA_ADDR, data, 2, true);
  171. return mp_const_none;
  172. }
  173. MP_DEFINE_CONST_FUN_OBJ_3(pyb_accel_write_obj, pyb_accel_write);
  174. STATIC const mp_rom_map_elem_t pyb_accel_locals_dict_table[] = {
  175. // TODO add init, deinit, and perhaps reset methods
  176. { MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&pyb_accel_x_obj) },
  177. { MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&pyb_accel_y_obj) },
  178. { MP_ROM_QSTR(MP_QSTR_z), MP_ROM_PTR(&pyb_accel_z_obj) },
  179. { MP_ROM_QSTR(MP_QSTR_tilt), MP_ROM_PTR(&pyb_accel_tilt_obj) },
  180. { MP_ROM_QSTR(MP_QSTR_filtered_xyz), MP_ROM_PTR(&pyb_accel_filtered_xyz_obj) },
  181. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&pyb_accel_read_obj) },
  182. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&pyb_accel_write_obj) },
  183. };
  184. STATIC MP_DEFINE_CONST_DICT(pyb_accel_locals_dict, pyb_accel_locals_dict_table);
  185. const mp_obj_type_t pyb_accel_type = {
  186. { &mp_type_type },
  187. .name = MP_QSTR_Accel,
  188. .make_new = pyb_accel_make_new,
  189. .locals_dict = (mp_obj_dict_t*)&pyb_accel_locals_dict,
  190. };
  191. #endif // MICROPY_HW_HAS_MMA7660