accel.c 5.15 KB
Newer Older
1
2
3
4
5
#include <stdio.h>
#include <string.h>

#include <stm32f4xx_hal.h>

6
#include "nlr.h"
7
8
9
10
11
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
12
#include "i2c.h"
13
14
15
#include "accel.h"

#define MMA_ADDR (0x98)
16
17
18
19
#define MMA_REG_X (0)
#define MMA_REG_Y (1)
#define MMA_REG_Z (2)
#define MMA_REG_TILT (3)
20
#define MMA_REG_MODE (7)
21
#define MMA_AXIS_SIGNED_VALUE(i) (((i) & 0x3f) | ((i) & 0x20 ? (~0x1f) : 0))
22
23
24
25
26
27
28
29
30
31
32

void accel_init(void) {
    GPIO_InitTypeDef GPIO_InitStructure;

    // PB5 is connected to AVDD; pull high to enable MMA accel device
    GPIOB->BSRRH = GPIO_PIN_5; // turn off AVDD
    GPIO_InitStructure.Pin = GPIO_PIN_5;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
33
34
}

35
36
37
38
STATIC void accel_start(void) {
    // start the I2C bus
    i2c_start(&I2cHandle_X);

39
40
41
42
43
    // turn off AVDD, wait 20ms, turn on AVDD, wait 20ms again
    GPIOB->BSRRH = GPIO_PIN_5; // turn off
    HAL_Delay(20);
    GPIOB->BSRRL = GPIO_PIN_5; // turn on
    HAL_Delay(20);
44
45
46
47
48

    HAL_StatusTypeDef status;

    //printf("IsDeviceReady\n");
    for (int i = 0; i < 10; i++) {
49
        status = HAL_I2C_IsDeviceReady(&I2cHandle_X, MMA_ADDR, 10, 200);
50
51
52
53
54
55
56
57
58
        //printf("  got %d\n", status);
        if (status == HAL_OK) {
            break;
        }
    }

    //printf("MemWrite\n");
    uint8_t data[1];
    data[0] = 1; // active mode
59
    status = HAL_I2C_Mem_Write(&I2cHandle_X, MMA_ADDR, MMA_REG_MODE, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
60
61
62
63
64
65
    //printf("  got %d\n", status);
}

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

66
67
#define NUM_AXIS (3)
#define FILT_DEPTH (4)
68

69
70
71
72
typedef struct _pyb_accel_obj_t {
    mp_obj_base_t base;
    int16_t buf[NUM_AXIS * FILT_DEPTH];
} pyb_accel_obj_t;
73

74
75
STATIC pyb_accel_obj_t pyb_accel_obj;

76
77
78
79
80
81
82
83
STATIC mp_obj_t pyb_accel_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
    // check arguments
    if (!(n_args == 0 && n_kw == 0)) {
        nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "Accel accepts no arguments"));
    }

    // init accel object
    pyb_accel_obj.base.type = &pyb_accel_type;
84
    accel_start();
85
86
87
88

    return &pyb_accel_obj;
}

89
90
STATIC mp_obj_t read_axis(int axis) {
    uint8_t data[1];
91
    HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, axis, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
92
93
94
95
96
97
98
99
100
101
102
103
    return mp_obj_new_int(MMA_AXIS_SIGNED_VALUE(data[0]));
}

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);

STATIC mp_obj_t pyb_accel_y(mp_obj_t self_in) {
    return read_axis(MMA_REG_Y);
}
104

105
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_y_obj, pyb_accel_y);
106

107
108
STATIC mp_obj_t pyb_accel_z(mp_obj_t self_in) {
    return read_axis(MMA_REG_Z);
109
110
}

111
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_z_obj, pyb_accel_z);
112

113
114
STATIC mp_obj_t pyb_accel_tilt(mp_obj_t self_in) {
    uint8_t data[1];
115
    HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, MMA_REG_TILT, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
116
117
118
119
120
121
122
123
124
125
126
    return mp_obj_new_int(data[0]);
}

STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_tilt_obj, pyb_accel_tilt);

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];
127
    HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, MMA_REG_X, I2C_MEMADD_SIZE_8BIT, data, NUM_AXIS, 200);
128
129
130
131
132
133
134
135
136

    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);
137
138
    }

139
    return rt_build_tuple(3, tuple);
140
141
}

142
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_accel_filtered_xyz_obj, pyb_accel_filtered_xyz);
143

144
STATIC mp_obj_t pyb_accel_read(mp_obj_t self_in, mp_obj_t reg) {
145
    uint8_t data[1];
146
    HAL_I2C_Mem_Read(&I2cHandle_X, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200);
147
148
149
    return mp_obj_new_int(data[0]);
}

150
MP_DEFINE_CONST_FUN_OBJ_2(pyb_accel_read_obj, pyb_accel_read);
151

152
STATIC mp_obj_t pyb_accel_write(mp_obj_t self_in, mp_obj_t reg, mp_obj_t val) {
153
154
    uint8_t data[1];
    data[0] = mp_obj_get_int(val);
155
    HAL_I2C_Mem_Write(&I2cHandle_X, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200);
156
157
158
    return mp_const_none;
}

159
MP_DEFINE_CONST_FUN_OBJ_3(pyb_accel_write_obj, pyb_accel_write);
160

161
STATIC const mp_method_t pyb_accel_methods[] = {
162
163
164
165
166
167
168
169
    { MP_QSTR_x, &pyb_accel_x_obj },
    { MP_QSTR_y, &pyb_accel_y_obj },
    { MP_QSTR_z, &pyb_accel_z_obj },
    { MP_QSTR_tilt, &pyb_accel_tilt_obj },
    { MP_QSTR_filtered_xyz, &pyb_accel_filtered_xyz_obj },
    { MP_QSTR_read, &pyb_accel_read_obj },
    { MP_QSTR_write, &pyb_accel_write_obj },
    { MP_QSTR_NULL, NULL },
170
171
};

172
const mp_obj_type_t pyb_accel_type = {
173
174
    { &mp_type_type },
    .name = MP_QSTR_Accel,
175
176
    .make_new = pyb_accel_make_new,
    .methods = pyb_accel_methods,
177
};