2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-20 19:33:30 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-09-06 11:56:41 -04:00
|
|
|
#include "py/mphal.h"
|
2015-01-01 16:06:20 -05:00
|
|
|
#include "py/runtime.h"
|
2015-10-29 18:40:42 -04:00
|
|
|
#include "pin.h"
|
2014-03-25 19:26:14 -04:00
|
|
|
#include "i2c.h"
|
2014-03-20 19:33:30 -04:00
|
|
|
#include "accel.h"
|
|
|
|
|
2014-04-20 14:06:15 -04:00
|
|
|
#if MICROPY_HW_HAS_MMA7660
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \moduleref pyb
|
|
|
|
/// \class Accel - accelerometer control
|
|
|
|
///
|
2014-06-01 12:57:06 -04:00
|
|
|
/// Accel is an object that controls the accelerometer. Example usage:
|
2014-04-29 17:55:34 -04:00
|
|
|
///
|
2014-06-01 12:57:06 -04:00
|
|
|
/// accel = pyb.Accel()
|
|
|
|
/// for i in range(10):
|
|
|
|
/// print(accel.x(), accel.y(), accel.z())
|
|
|
|
///
|
|
|
|
/// Raw values are between -32 and 31.
|
2014-04-29 17:55:34 -04:00
|
|
|
|
2014-03-20 19:33:30 -04:00
|
|
|
#define MMA_ADDR (0x98)
|
2014-03-23 11:34:54 -04:00
|
|
|
#define MMA_REG_X (0)
|
|
|
|
#define MMA_REG_Y (1)
|
|
|
|
#define MMA_REG_Z (2)
|
|
|
|
#define MMA_REG_TILT (3)
|
2014-03-20 19:33:30 -04:00
|
|
|
#define MMA_REG_MODE (7)
|
2014-03-23 11:34:54 -04:00
|
|
|
#define MMA_AXIS_SIGNED_VALUE(i) (((i) & 0x3f) | ((i) & 0x20 ? (~0x1f) : 0))
|
2014-03-20 19:33:30 -04:00
|
|
|
|
|
|
|
void accel_init(void) {
|
|
|
|
// PB5 is connected to AVDD; pull high to enable MMA accel device
|
2018-03-28 01:13:21 -04:00
|
|
|
mp_hal_pin_low(MICROPY_HW_MMA_AVDD_PIN); // turn off AVDD
|
|
|
|
mp_hal_pin_output(MICROPY_HW_MMA_AVDD_PIN);
|
2014-03-23 11:34:54 -04:00
|
|
|
}
|
|
|
|
|
2014-03-25 19:26:14 -04:00
|
|
|
STATIC void accel_start(void) {
|
2014-04-20 19:10:04 -04:00
|
|
|
// start the I2C bus in master mode
|
|
|
|
I2CHandle1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
|
|
|
I2CHandle1.Init.ClockSpeed = 400000;
|
|
|
|
I2CHandle1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
|
|
|
|
I2CHandle1.Init.DutyCycle = I2C_DUTYCYCLE_16_9;
|
|
|
|
I2CHandle1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
|
2016-11-22 01:40:50 -05:00
|
|
|
I2CHandle1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
2014-04-20 19:10:04 -04:00
|
|
|
I2CHandle1.Init.OwnAddress1 = PYB_I2C_MASTER_ADDRESS;
|
|
|
|
I2CHandle1.Init.OwnAddress2 = 0xfe; // unused
|
2018-04-24 03:32:16 -04:00
|
|
|
pyb_i2c_init(&I2CHandle1);
|
2014-03-25 19:26:14 -04:00
|
|
|
|
2014-07-31 05:39:52 -04:00
|
|
|
// turn off AVDD, wait 30ms, turn on AVDD, wait 30ms again
|
2018-03-28 01:13:21 -04:00
|
|
|
mp_hal_pin_low(MICROPY_HW_MMA_AVDD_PIN); // turn off
|
2017-03-01 23:02:57 -05:00
|
|
|
mp_hal_delay_ms(30);
|
2018-03-28 01:13:21 -04:00
|
|
|
mp_hal_pin_high(MICROPY_HW_MMA_AVDD_PIN); // turn on
|
2017-03-01 23:02:57 -05:00
|
|
|
mp_hal_delay_ms(30);
|
2014-03-20 19:33:30 -04:00
|
|
|
|
|
|
|
HAL_StatusTypeDef status;
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
2014-04-19 19:30:09 -04:00
|
|
|
status = HAL_I2C_IsDeviceReady(&I2CHandle1, MMA_ADDR, 10, 200);
|
2014-03-20 19:33:30 -04:00
|
|
|
if (status == HAL_OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 10:43:15 -04:00
|
|
|
if (status != HAL_OK) {
|
2017-06-14 21:54:41 -04:00
|
|
|
mp_raise_msg(&mp_type_OSError, "accelerometer not found");
|
2016-04-29 10:43:15 -04:00
|
|
|
}
|
|
|
|
|
2014-07-31 05:39:52 -04:00
|
|
|
// set MMA to active mode
|
|
|
|
uint8_t data[1] = {1}; // active mode
|
2014-04-19 19:30:09 -04:00
|
|
|
status = HAL_I2C_Mem_Write(&I2CHandle1, MMA_ADDR, MMA_REG_MODE, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
|
2014-07-31 05:39:52 -04:00
|
|
|
|
|
|
|
// wait for MMA to become active
|
2017-03-01 23:02:57 -05:00
|
|
|
mp_hal_delay_ms(30);
|
2014-03-20 19:33:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-06-30 03:22:17 -04:00
|
|
|
/* MicroPython bindings */
|
2014-03-20 19:33:30 -04:00
|
|
|
|
2014-03-23 11:34:54 -04:00
|
|
|
#define NUM_AXIS (3)
|
|
|
|
#define FILT_DEPTH (4)
|
2014-03-20 19:33:30 -04:00
|
|
|
|
2014-03-23 11:34:54 -04:00
|
|
|
typedef struct _pyb_accel_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
int16_t buf[NUM_AXIS * FILT_DEPTH];
|
|
|
|
} pyb_accel_obj_t;
|
2014-03-20 19:33:30 -04:00
|
|
|
|
2014-03-23 11:34:54 -04:00
|
|
|
STATIC pyb_accel_obj_t pyb_accel_obj;
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \classmethod \constructor()
|
|
|
|
/// Create and return an accelerometer object.
|
2014-06-01 12:57:06 -04:00
|
|
|
///
|
|
|
|
/// Note: if you read accelerometer values immediately after creating this object
|
|
|
|
/// you will get 0. It takes around 20ms for the first sample to be ready, so,
|
|
|
|
/// unless you have some other code between creating this object and reading its
|
|
|
|
/// values, you should put a `pyb.delay(20)` after creating it. For example:
|
|
|
|
///
|
|
|
|
/// accel = pyb.Accel()
|
|
|
|
/// pyb.delay(20)
|
|
|
|
/// print(accel.x())
|
2017-01-04 08:10:42 -05:00
|
|
|
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) {
|
2014-03-24 07:48:39 -04:00
|
|
|
// check arguments
|
2014-04-19 19:16:30 -04:00
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 0, false);
|
2014-03-24 07:48:39 -04:00
|
|
|
|
|
|
|
// init accel object
|
|
|
|
pyb_accel_obj.base.type = &pyb_accel_type;
|
2014-03-25 19:26:14 -04:00
|
|
|
accel_start();
|
2014-03-24 07:48:39 -04:00
|
|
|
|
|
|
|
return &pyb_accel_obj;
|
|
|
|
}
|
|
|
|
|
2014-03-23 11:34:54 -04:00
|
|
|
STATIC mp_obj_t read_axis(int axis) {
|
|
|
|
uint8_t data[1];
|
2014-04-19 19:30:09 -04:00
|
|
|
HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, axis, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
|
2014-03-23 11:34:54 -04:00
|
|
|
return mp_obj_new_int(MMA_AXIS_SIGNED_VALUE(data[0]));
|
|
|
|
}
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \method x()
|
|
|
|
/// Get the x-axis value.
|
2014-03-23 11:34:54 -04:00
|
|
|
STATIC mp_obj_t pyb_accel_x(mp_obj_t self_in) {
|
|
|
|
return read_axis(MMA_REG_X);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_x_obj, pyb_accel_x);
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \method y()
|
|
|
|
/// Get the y-axis value.
|
2014-03-23 11:34:54 -04:00
|
|
|
STATIC mp_obj_t pyb_accel_y(mp_obj_t self_in) {
|
|
|
|
return read_axis(MMA_REG_Y);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_y_obj, pyb_accel_y);
|
2014-03-20 19:33:30 -04:00
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \method z()
|
|
|
|
/// Get the z-axis value.
|
2014-03-23 11:34:54 -04:00
|
|
|
STATIC mp_obj_t pyb_accel_z(mp_obj_t self_in) {
|
|
|
|
return read_axis(MMA_REG_Z);
|
2014-03-20 19:33:30 -04:00
|
|
|
}
|
2014-03-23 11:34:54 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_z_obj, pyb_accel_z);
|
2014-03-20 19:33:30 -04:00
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \method tilt()
|
|
|
|
/// Get the tilt register.
|
2014-03-23 11:34:54 -04:00
|
|
|
STATIC mp_obj_t pyb_accel_tilt(mp_obj_t self_in) {
|
|
|
|
uint8_t data[1];
|
2014-04-19 19:30:09 -04:00
|
|
|
HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, MMA_REG_TILT, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
|
2014-03-23 11:34:54 -04:00
|
|
|
return mp_obj_new_int(data[0]);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_tilt_obj, pyb_accel_tilt);
|
|
|
|
|
2014-04-29 17:55:34 -04:00
|
|
|
/// \method filtered_xyz()
|
|
|
|
/// Get a 3-tuple of filtered x, y and z values.
|
2014-03-23 11:34:54 -04:00
|
|
|
STATIC mp_obj_t pyb_accel_filtered_xyz(mp_obj_t self_in) {
|
|
|
|
pyb_accel_obj_t *self = self_in;
|
|
|
|
|
|
|
|
memmove(self->buf, self->buf + NUM_AXIS, NUM_AXIS * (FILT_DEPTH - 1) * sizeof(int16_t));
|
|
|
|
|
|
|
|
uint8_t data[NUM_AXIS];
|
2014-04-19 19:30:09 -04:00
|
|
|
HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, MMA_REG_X, I2C_MEMADD_SIZE_8BIT, data, NUM_AXIS, 200);
|
2014-03-23 11:34:54 -04:00
|
|
|
|
|
|
|
mp_obj_t tuple[NUM_AXIS];
|
|
|
|
for (int i = 0; i < NUM_AXIS; i++) {
|
|
|
|
self->buf[NUM_AXIS * (FILT_DEPTH - 1) + i] = MMA_AXIS_SIGNED_VALUE(data[i]);
|
|
|
|
int32_t val = 0;
|
|
|
|
for (int j = 0; j < FILT_DEPTH; j++) {
|
|
|
|
val += self->buf[i + NUM_AXIS * j];
|
|
|
|
}
|
|
|
|
tuple[i] = mp_obj_new_int(val);
|
2014-03-20 19:33:30 -04:00
|
|
|
}
|
|
|
|
|
2014-03-31 11:28:13 -04:00
|
|
|
return mp_obj_new_tuple(3, tuple);
|
2014-03-20 19:33:30 -04:00
|
|
|
}
|
2014-03-23 11:34:54 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_filtered_xyz_obj, pyb_accel_filtered_xyz);
|
2014-03-20 19:33:30 -04:00
|
|
|
|
2014-03-26 16:15:40 -04:00
|
|
|
STATIC mp_obj_t pyb_accel_read(mp_obj_t self_in, mp_obj_t reg) {
|
2014-03-23 11:34:54 -04:00
|
|
|
uint8_t data[1];
|
2014-04-19 19:30:09 -04:00
|
|
|
HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200);
|
2014-03-23 11:34:54 -04:00
|
|
|
return mp_obj_new_int(data[0]);
|
|
|
|
}
|
2014-03-26 16:15:40 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(pyb_accel_read_obj, pyb_accel_read);
|
2014-03-23 11:34:54 -04:00
|
|
|
|
2014-03-26 16:15:40 -04:00
|
|
|
STATIC mp_obj_t pyb_accel_write(mp_obj_t self_in, mp_obj_t reg, mp_obj_t val) {
|
2014-03-23 11:34:54 -04:00
|
|
|
uint8_t data[1];
|
|
|
|
data[0] = mp_obj_get_int(val);
|
2014-04-19 19:30:09 -04:00
|
|
|
HAL_I2C_Mem_Write(&I2CHandle1, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200);
|
2014-03-20 19:33:30 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-03-26 16:15:40 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_3(pyb_accel_write_obj, pyb_accel_write);
|
2014-03-23 11:34:54 -04:00
|
|
|
|
2017-05-06 03:03:40 -04:00
|
|
|
STATIC const mp_rom_map_elem_t pyb_accel_locals_dict_table[] = {
|
2014-04-21 13:30:38 -04:00
|
|
|
// TODO add init, deinit, and perhaps reset methods
|
2017-05-06 03:03:40 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&pyb_accel_x_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&pyb_accel_y_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_z), MP_ROM_PTR(&pyb_accel_z_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_tilt), MP_ROM_PTR(&pyb_accel_tilt_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_filtered_xyz), MP_ROM_PTR(&pyb_accel_filtered_xyz_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&pyb_accel_read_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&pyb_accel_write_obj) },
|
2014-03-23 11:34:54 -04:00
|
|
|
};
|
|
|
|
|
2014-03-26 17:47:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(pyb_accel_locals_dict, pyb_accel_locals_dict_table);
|
|
|
|
|
2014-03-24 07:48:39 -04:00
|
|
|
const mp_obj_type_t pyb_accel_type = {
|
2014-03-23 11:34:54 -04:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_Accel,
|
2014-03-24 07:48:39 -04:00
|
|
|
.make_new = pyb_accel_make_new,
|
2017-05-06 03:03:40 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t*)&pyb_accel_locals_dict,
|
2014-03-23 11:34:54 -04:00
|
|
|
};
|
2014-04-20 14:06:15 -04:00
|
|
|
|
|
|
|
#endif // MICROPY_HW_HAS_MMA7660
|