accel.c 8.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * 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.
 */

27
28
29
#include <stdio.h>
#include <string.h>

30
#include STM32_HAL_H
31

32
33
#include "py/nlr.h"
#include "py/runtime.h"
34
35
#include "pin.h"
#include "genhdr/pins.h"
36
#include "i2c.h"
37
38
#include "accel.h"

39
40
#if MICROPY_HW_HAS_MMA7660

41
42
43
/// \moduleref pyb
/// \class Accel - accelerometer control
///
44
/// Accel is an object that controls the accelerometer.  Example usage:
45
///
46
47
48
49
50
///     accel = pyb.Accel()
///     for i in range(10):
///         print(accel.x(), accel.y(), accel.z())
///
/// Raw values are between -32 and 31.
51

52
#define MMA_ADDR (0x98)
53
54
55
56
#define MMA_REG_X (0)
#define MMA_REG_Y (1)
#define MMA_REG_Z (2)
#define MMA_REG_TILT (3)
57
#define MMA_REG_MODE (7)
58
#define MMA_AXIS_SIGNED_VALUE(i) (((i) & 0x3f) | ((i) & 0x20 ? (~0x1f) : 0))
59
60
61
62
63

void accel_init(void) {
    GPIO_InitTypeDef GPIO_InitStructure;

    // PB5 is connected to AVDD; pull high to enable MMA accel device
64
65
    MICROPY_HW_MMA_AVDD_PIN.gpio->BSRRH = MICROPY_HW_MMA_AVDD_PIN.pin_mask; // turn off AVDD
    GPIO_InitStructure.Pin = MICROPY_HW_MMA_AVDD_PIN.pin_mask;
66
67
68
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
69
    HAL_GPIO_Init(MICROPY_HW_MMA_AVDD_PIN.gpio, &GPIO_InitStructure);
70
71
}

72
STATIC void accel_start(void) {
73
74
75
76
77
78
79
80
81
    // 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;
    I2CHandle1.Init.NoStretchMode   = I2C_NOSTRETCH_DISABLED;
    I2CHandle1.Init.OwnAddress1     = PYB_I2C_MASTER_ADDRESS;
    I2CHandle1.Init.OwnAddress2     = 0xfe; // unused
82
    i2c_init(&I2CHandle1);
83

84
    // turn off AVDD, wait 30ms, turn on AVDD, wait 30ms again
85
    MICROPY_HW_MMA_AVDD_PIN.gpio->BSRRH = MICROPY_HW_MMA_AVDD_PIN.pin_mask; // turn off
86
    HAL_Delay(30);
87
    MICROPY_HW_MMA_AVDD_PIN.gpio->BSRRL = MICROPY_HW_MMA_AVDD_PIN.pin_mask; // turn on
88
    HAL_Delay(30);
89
90
91
92
93

    HAL_StatusTypeDef status;

    //printf("IsDeviceReady\n");
    for (int i = 0; i < 10; i++) {
94
        status = HAL_I2C_IsDeviceReady(&I2CHandle1, MMA_ADDR, 10, 200);
95
96
97
98
99
100
        //printf("  got %d\n", status);
        if (status == HAL_OK) {
            break;
        }
    }

101
102
    // set MMA to active mode
    uint8_t data[1] = {1}; // active mode
103
    status = HAL_I2C_Mem_Write(&I2CHandle1, MMA_ADDR, MMA_REG_MODE, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
104
105
106

    // wait for MMA to become active
    HAL_Delay(30);
107
108
109
110
111
}

/******************************************************************************/
/* Micro Python bindings                                                      */

112
113
#define NUM_AXIS (3)
#define FILT_DEPTH (4)
114

115
116
117
118
typedef struct _pyb_accel_obj_t {
    mp_obj_base_t base;
    int16_t buf[NUM_AXIS * FILT_DEPTH];
} pyb_accel_obj_t;
119

120
121
STATIC pyb_accel_obj_t pyb_accel_obj;

122
123
/// \classmethod \constructor()
/// Create and return an accelerometer object.
124
125
126
127
128
129
130
131
132
///
/// 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())
133
STATIC mp_obj_t pyb_accel_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
134
    // check arguments
Damien George's avatar
Damien George committed
135
    mp_arg_check_num(n_args, n_kw, 0, 0, false);
136
137
138

    // init accel object
    pyb_accel_obj.base.type = &pyb_accel_type;
139
    accel_start();
140
141
142
143

    return &pyb_accel_obj;
}

144
145
STATIC mp_obj_t read_axis(int axis) {
    uint8_t data[1];
146
    HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, axis, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
147
148
149
    return mp_obj_new_int(MMA_AXIS_SIGNED_VALUE(data[0]));
}

150
151
/// \method x()
/// Get the x-axis value.
152
153
154
155
156
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);

157
158
/// \method y()
/// Get the y-axis value.
159
160
161
162
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);
163

164
165
/// \method z()
/// Get the z-axis value.
166
167
STATIC mp_obj_t pyb_accel_z(mp_obj_t self_in) {
    return read_axis(MMA_REG_Z);
168
}
169
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_z_obj, pyb_accel_z);
170

171
172
/// \method tilt()
/// Get the tilt register.
173
174
STATIC mp_obj_t pyb_accel_tilt(mp_obj_t self_in) {
    uint8_t data[1];
175
    HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, MMA_REG_TILT, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
176
177
178
179
    return mp_obj_new_int(data[0]);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_tilt_obj, pyb_accel_tilt);

180
181
/// \method filtered_xyz()
/// Get a 3-tuple of filtered x, y and z values.
182
183
184
185
186
187
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];
188
    HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, MMA_REG_X, I2C_MEMADD_SIZE_8BIT, data, NUM_AXIS, 200);
189
190
191
192
193
194
195
196
197

    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);
198
199
    }

200
    return mp_obj_new_tuple(3, tuple);
201
}
202
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_filtered_xyz_obj, pyb_accel_filtered_xyz);
203

204
STATIC mp_obj_t pyb_accel_read(mp_obj_t self_in, mp_obj_t reg) {
205
    uint8_t data[1];
206
    HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200);
207
208
    return mp_obj_new_int(data[0]);
}
209
MP_DEFINE_CONST_FUN_OBJ_2(pyb_accel_read_obj, pyb_accel_read);
210

211
STATIC mp_obj_t pyb_accel_write(mp_obj_t self_in, mp_obj_t reg, mp_obj_t val) {
212
213
    uint8_t data[1];
    data[0] = mp_obj_get_int(val);
214
    HAL_I2C_Mem_Write(&I2CHandle1, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200);
215
216
    return mp_const_none;
}
217
MP_DEFINE_CONST_FUN_OBJ_3(pyb_accel_write_obj, pyb_accel_write);
218

219
STATIC const mp_map_elem_t pyb_accel_locals_dict_table[] = {
220
    // TODO add init, deinit, and perhaps reset methods
221
222
223
224
225
226
227
    { MP_OBJ_NEW_QSTR(MP_QSTR_x), (mp_obj_t)&pyb_accel_x_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_y), (mp_obj_t)&pyb_accel_y_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_z), (mp_obj_t)&pyb_accel_z_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_tilt), (mp_obj_t)&pyb_accel_tilt_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_filtered_xyz), (mp_obj_t)&pyb_accel_filtered_xyz_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&pyb_accel_read_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&pyb_accel_write_obj },
228
229
};

230
231
STATIC MP_DEFINE_CONST_DICT(pyb_accel_locals_dict, pyb_accel_locals_dict_table);

232
const mp_obj_type_t pyb_accel_type = {
233
234
    { &mp_type_type },
    .name = MP_QSTR_Accel,
235
    .make_new = pyb_accel_make_new,
236
    .locals_dict = (mp_obj_t)&pyb_accel_locals_dict,
237
};
238
239

#endif // MICROPY_HW_HAS_MMA7660