led.c 5.61 KB
Newer Older
1
2
#include <stdio.h>
#include <stm32f4xx_hal.h>
3
4
#include "usbd_cdc_msc.h"
#include "usbd_cdc_interface.h"
5
6
7
8
9

#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
10
#include "runtime.h"
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "led.h"
#include "pin.h"
#include "build/pins.h"

static const pin_obj_t *gLed[] = {
    &PYB_LED1,
#if defined(PYB_LED2)
    &PYB_LED2,
#if defined(PYB_LED3)
    &PYB_LED3,
#if defined(PYB_LED4)
    &PYB_LED4,
#endif
#endif
#endif
};
#define NUM_LEDS (sizeof(gLed) / sizeof(gLed[0]))

void led_init(void) {
    /* GPIO structure */
    GPIO_InitTypeDef GPIO_InitStructure;

    /* Configure I/O speed, mode, output type and pull */
    GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
35
    GPIO_InitStructure.Mode = PYB_OTYPE;
36
37
38
39
40
41
42
43
    GPIO_InitStructure.Pull = GPIO_NOPULL;

    /* Turn off LEDs and initialize */
    for (int led = 0; led < NUM_LEDS; led++) {
        PYB_LED_OFF(gLed[led]); 
        GPIO_InitStructure.Pin = gLed[led]->pin_mask;
        HAL_GPIO_Init(gLed[led]->gpio, &GPIO_InitStructure);
    }
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

    // LED4 (blue) is on PB4 which is TIM3_CH1
    // we use PWM on this channel to fade the LED

    // GPIO configuration
    GPIO_InitStructure.Pin = GPIO_PIN_4;
    GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
    GPIO_InitStructure.Alternate = GPIO_AF2_TIM3;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);

    // PWM mode configuration
    TIM_OC_InitTypeDef oc_init;
    oc_init.OCMode = TIM_OCMODE_PWM1;
    oc_init.Pulse = 0; // off
    oc_init.OCPolarity = TIM_OCPOLARITY_HIGH;
    oc_init.OCFastMode = TIM_OCFAST_DISABLE;
    HAL_TIM_PWM_ConfigChannel(&TIM3_Handle, &oc_init, TIM_CHANNEL_1);

    // start PWM
    TIM_CCxChannelCmd(TIM3, TIM_CHANNEL_1, TIM_CCx_ENABLE);
    //HAL_TIM_PWM_Start(&USBD_CDC_TIM3_Handle, TIM_CHANNEL_1);
67
68
69
70
71
72
}

void led_state(pyb_led_t led, int state) {
    if (led < 1 || led > NUM_LEDS) {
        return;
    }
73
74
75
76
77
78
79
80
    if (led == 4) {
        if (state) {
            TIM3->CCR1 = 0xffff;
        } else {
            TIM3->CCR1 = 0;
        }
        return;
    }
81
    const pin_obj_t *led_pin = gLed[led - 1];
82
//printf("led_state(%d,%d)\n", led, state);
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    if (state == 0) {
        // turn LED off
        PYB_LED_OFF(led_pin);
    } else {
        // turn LED on
        PYB_LED_ON(led_pin);
    }
}

void led_toggle(pyb_led_t led) {
    if (led < 1 || led > NUM_LEDS) {
        return;
    }
    const pin_obj_t *led_pin = gLed[led - 1];
    GPIO_TypeDef *gpio = led_pin->gpio;

    // We don't know if we're turning the LED on or off, but we don't really
    // care. Just invert the state.
    if (gpio->ODR & led_pin->pin_mask) {
        // pin is high, make it low
        gpio->BSRRH = led_pin->pin_mask;
    } else {
        // pin is low, make it high
        gpio->BSRRL = led_pin->pin_mask;
    }
}

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
int led_get_state(pyb_led_t led) {
    if (led < 1 || led > NUM_LEDS) {
        return 0;
    }
    const pin_obj_t *led_pin = gLed[led - 1];
    GPIO_TypeDef *gpio = led_pin->gpio;

    // TODO convert high/low to on/off depending on board
    if (gpio->ODR & led_pin->pin_mask) {
        // pin is high
        return 1;
    } else {
        // pin is low
        return 0;
    }
}

127
128
129
130
131
132
133
134
void led_debug(int n, int delay) {
    led_state(1, n & 1);
    led_state(2, n & 2);
    led_state(3, n & 4);
    led_state(4, n & 8);
    HAL_Delay(delay);
}

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/******************************************************************************/
/* Micro Python bindings                                                      */

typedef struct _pyb_led_obj_t {
    mp_obj_base_t base;
    uint led_id;
} pyb_led_obj_t;

void led_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
    pyb_led_obj_t *self = self_in;
    print(env, "<LED %lu>", self->led_id);
}

mp_obj_t led_obj_on(mp_obj_t self_in) {
    pyb_led_obj_t *self = self_in;
    led_state(self->led_id, 1);
    return mp_const_none;
}

mp_obj_t led_obj_off(mp_obj_t self_in) {
    pyb_led_obj_t *self = self_in;
    led_state(self->led_id, 0);
    return mp_const_none;
}

mp_obj_t led_obj_toggle(mp_obj_t self_in) {
    pyb_led_obj_t *self = self_in;
    led_toggle(self->led_id);
    return mp_const_none;
}

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
mp_obj_t led_obj_state(uint n_args, const mp_obj_t *args) {
    pyb_led_obj_t *self = args[0];
    if (n_args == 0) {
        return MP_BOOL(led_get_state(self->led_id));
    } else {
        led_state(self->led_id, rt_is_true(args[1]));
        return mp_const_none;
    }
}

mp_obj_t led_obj_intensity(mp_obj_t self_in, mp_obj_t intensity) {
    pyb_led_obj_t *self = self_in;
    if (self->led_id == 4) {
        TIM3->CCR1 = mp_obj_get_int(intensity);
    }
    return mp_const_none;
}

STATIC MP_DEFINE_CONST_FUN_OBJ_1(led_obj_on_obj, led_obj_on);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(led_obj_off_obj, led_obj_off);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(led_obj_toggle_obj, led_obj_toggle);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(led_obj_state_obj, 1, 2, led_obj_state);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(led_obj_intensity_obj, led_obj_intensity);
189

190
STATIC const mp_method_t led_methods[] = {
191
192
193
    { "on", &led_obj_on_obj },
    { "off", &led_obj_off_obj },
    { "toggle", &led_obj_toggle_obj },
194
195
    { "state", &led_obj_state_obj },
    { "intensity", &led_obj_intensity_obj },
196
197
198
    { NULL, NULL },
};

199
STATIC const mp_obj_type_t led_obj_type = {
200
201
202
203
204
205
    { &mp_type_type },
    .name = MP_QSTR_Led,
    .print = led_obj_print,
    .methods = led_methods,
};

206
STATIC mp_obj_t pyb_Led(mp_obj_t led_id) {
207
208
209
210
211
212
213
    pyb_led_obj_t *o = m_new_obj(pyb_led_obj_t);
    o->base.type = &led_obj_type;
    o->led_id = mp_obj_get_int(led_id);
    return o;
}

MP_DEFINE_CONST_FUN_OBJ_1(pyb_Led_obj, pyb_Led);