|
1 | 1 | #include <stdio.h> |
2 | 2 | #include <stm32f4xx_hal.h> |
| 3 | +#include "usbd_cdc_msc.h" |
| 4 | +#include "usbd_cdc_interface.h" |
3 | 5 |
|
4 | 6 | #include "misc.h" |
5 | 7 | #include "mpconfig.h" |
6 | 8 | #include "qstr.h" |
7 | 9 | #include "obj.h" |
| 10 | +#include "runtime.h" |
8 | 11 | #include "led.h" |
9 | 12 | #include "pin.h" |
10 | 13 | #include "build/pins.h" |
@@ -38,12 +41,43 @@ void led_init(void) { |
38 | 41 | GPIO_InitStructure.Pin = gLed[led]->pin_mask; |
39 | 42 | HAL_GPIO_Init(gLed[led]->gpio, &GPIO_InitStructure); |
40 | 43 | } |
| 44 | + |
| 45 | + // LED4 (blue) is on PB4 which is TIM3_CH1 |
| 46 | + // we use PWM on this channel to fade the LED |
| 47 | + |
| 48 | + // GPIO configuration |
| 49 | + GPIO_InitStructure.Pin = GPIO_PIN_4; |
| 50 | + GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; |
| 51 | + GPIO_InitStructure.Speed = GPIO_SPEED_FAST; |
| 52 | + GPIO_InitStructure.Pull = GPIO_NOPULL; |
| 53 | + GPIO_InitStructure.Alternate = GPIO_AF2_TIM3; |
| 54 | + HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); |
| 55 | + |
| 56 | + // PWM mode configuration |
| 57 | + TIM_OC_InitTypeDef oc_init; |
| 58 | + oc_init.OCMode = TIM_OCMODE_PWM1; |
| 59 | + oc_init.Pulse = 0; // off |
| 60 | + oc_init.OCPolarity = TIM_OCPOLARITY_HIGH; |
| 61 | + oc_init.OCFastMode = TIM_OCFAST_DISABLE; |
| 62 | + HAL_TIM_PWM_ConfigChannel(&TIM3_Handle, &oc_init, TIM_CHANNEL_1); |
| 63 | + |
| 64 | + // start PWM |
| 65 | + TIM_CCxChannelCmd(TIM3, TIM_CHANNEL_1, TIM_CCx_ENABLE); |
| 66 | + //HAL_TIM_PWM_Start(&USBD_CDC_TIM3_Handle, TIM_CHANNEL_1); |
41 | 67 | } |
42 | 68 |
|
43 | 69 | void led_state(pyb_led_t led, int state) { |
44 | 70 | if (led < 1 || led > NUM_LEDS) { |
45 | 71 | return; |
46 | 72 | } |
| 73 | + if (led == 4) { |
| 74 | + if (state) { |
| 75 | + TIM3->CCR1 = 0xffff; |
| 76 | + } else { |
| 77 | + TIM3->CCR1 = 0; |
| 78 | + } |
| 79 | + return; |
| 80 | + } |
47 | 81 | const pin_obj_t *led_pin = gLed[led - 1]; |
48 | 82 | //printf("led_state(%d,%d)\n", led, state); |
49 | 83 | if (state == 0) { |
@@ -73,6 +107,23 @@ void led_toggle(pyb_led_t led) { |
73 | 107 | } |
74 | 108 | } |
75 | 109 |
|
| 110 | +int led_get_state(pyb_led_t led) { |
| 111 | + if (led < 1 || led > NUM_LEDS) { |
| 112 | + return 0; |
| 113 | + } |
| 114 | + const pin_obj_t *led_pin = gLed[led - 1]; |
| 115 | + GPIO_TypeDef *gpio = led_pin->gpio; |
| 116 | + |
| 117 | + // TODO convert high/low to on/off depending on board |
| 118 | + if (gpio->ODR & led_pin->pin_mask) { |
| 119 | + // pin is high |
| 120 | + return 1; |
| 121 | + } else { |
| 122 | + // pin is low |
| 123 | + return 0; |
| 124 | + } |
| 125 | +} |
| 126 | + |
76 | 127 | void led_debug(int n, int delay) { |
77 | 128 | led_state(1, n & 1); |
78 | 129 | led_state(2, n & 2); |
@@ -112,25 +163,47 @@ mp_obj_t led_obj_toggle(mp_obj_t self_in) { |
112 | 163 | return mp_const_none; |
113 | 164 | } |
114 | 165 |
|
115 | | -static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_on_obj, led_obj_on); |
116 | | -static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_off_obj, led_obj_off); |
117 | | -static MP_DEFINE_CONST_FUN_OBJ_1(led_obj_toggle_obj, led_obj_toggle); |
| 166 | +mp_obj_t led_obj_state(uint n_args, const mp_obj_t *args) { |
| 167 | + pyb_led_obj_t *self = args[0]; |
| 168 | + if (n_args == 0) { |
| 169 | + return MP_BOOL(led_get_state(self->led_id)); |
| 170 | + } else { |
| 171 | + led_state(self->led_id, rt_is_true(args[1])); |
| 172 | + return mp_const_none; |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +mp_obj_t led_obj_intensity(mp_obj_t self_in, mp_obj_t intensity) { |
| 177 | + pyb_led_obj_t *self = self_in; |
| 178 | + if (self->led_id == 4) { |
| 179 | + TIM3->CCR1 = mp_obj_get_int(intensity); |
| 180 | + } |
| 181 | + return mp_const_none; |
| 182 | +} |
| 183 | + |
| 184 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(led_obj_on_obj, led_obj_on); |
| 185 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(led_obj_off_obj, led_obj_off); |
| 186 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(led_obj_toggle_obj, led_obj_toggle); |
| 187 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(led_obj_state_obj, 1, 2, led_obj_state); |
| 188 | +STATIC MP_DEFINE_CONST_FUN_OBJ_2(led_obj_intensity_obj, led_obj_intensity); |
118 | 189 |
|
119 | | -static const mp_method_t led_methods[] = { |
| 190 | +STATIC const mp_method_t led_methods[] = { |
120 | 191 | { "on", &led_obj_on_obj }, |
121 | 192 | { "off", &led_obj_off_obj }, |
122 | 193 | { "toggle", &led_obj_toggle_obj }, |
| 194 | + { "state", &led_obj_state_obj }, |
| 195 | + { "intensity", &led_obj_intensity_obj }, |
123 | 196 | { NULL, NULL }, |
124 | 197 | }; |
125 | 198 |
|
126 | | -static const mp_obj_type_t led_obj_type = { |
| 199 | +STATIC const mp_obj_type_t led_obj_type = { |
127 | 200 | { &mp_type_type }, |
128 | 201 | .name = MP_QSTR_Led, |
129 | 202 | .print = led_obj_print, |
130 | 203 | .methods = led_methods, |
131 | 204 | }; |
132 | 205 |
|
133 | | -static mp_obj_t pyb_Led(mp_obj_t led_id) { |
| 206 | +STATIC mp_obj_t pyb_Led(mp_obj_t led_id) { |
134 | 207 | pyb_led_obj_t *o = m_new_obj(pyb_led_obj_t); |
135 | 208 | o->base.type = &led_obj_type; |
136 | 209 | o->led_id = mp_obj_get_int(led_id); |
|
0 commit comments