3636
3737#if defined(MICROPY_HW_LED1 )
3838
39+ // default is to not PWM LED4
40+ #if !defined(MICROPY_HW_LED4_PWM )
41+ #define MICROPY_HW_LED4_PWM (0)
42+ #endif
43+
3944/// \moduleref pyb
4045/// \class LED - LED object
4146///
@@ -78,7 +83,7 @@ void led_init(void) {
7883 HAL_GPIO_Init (led_pin -> gpio , & GPIO_InitStructure );
7984 }
8085
81- #if defined( PYBV4 ) || defined( PYBV10 )
86+ #if MICROPY_HW_LED4_PWM
8287 // LED4 (blue) is on PB4 which is TIM3_CH1
8388 // we use PWM on this channel to fade the LED
8489
@@ -102,23 +107,21 @@ void led_init(void) {
102107
103108 // start PWM
104109 TIM_CCxChannelCmd (TIM3 , TIM_CHANNEL_1 , TIM_CCx_ENABLE );
105- #endif
110+ #endif
106111}
107112
108113void led_state (pyb_led_t led , int state ) {
109114 if (led < 1 || led > NUM_LEDS ) {
110115 return ;
111116 }
112- #if defined(PYBV4 ) || defined(PYBV10 )
113- if (led == 4 ) {
117+ if (MICROPY_HW_LED4_PWM && led == 4 ) {
114118 if (state ) {
115119 TIM3 -> CCR1 = 0xffff ;
116120 } else {
117121 TIM3 -> CCR1 = 0 ;
118122 }
119123 return ;
120124 }
121- #endif
122125 const pin_obj_t * led_pin = pyb_led_obj [led - 1 ].led_pin ;
123126 //printf("led_state(%d,%d)\n", led, state);
124127 if (state == 0 ) {
@@ -135,16 +138,14 @@ void led_toggle(pyb_led_t led) {
135138 return ;
136139 }
137140
138- #if defined(PYBV4 ) || defined(PYBV10 )
139- if (led == 4 ) {
141+ if (MICROPY_HW_LED4_PWM && led == 4 ) {
140142 if (TIM3 -> CCR1 == 0 ) {
141143 TIM3 -> CCR1 = 0xffff ;
142144 } else {
143145 TIM3 -> CCR1 = 0 ;
144146 }
145147 return ;
146148 }
147- #endif
148149
149150 // toggle the output data register to toggle the LED state
150151 const pin_obj_t * led_pin = pyb_led_obj [led - 1 ].led_pin ;
@@ -156,15 +157,13 @@ int led_get_intensity(pyb_led_t led) {
156157 return 0 ;
157158 }
158159
159- #if defined(PYBV4 ) || defined(PYBV10 )
160- if (led == 4 ) {
160+ if (MICROPY_HW_LED4_PWM && led == 4 ) {
161161 mp_uint_t i = (TIM3 -> CCR1 * 255 + (USBD_CDC_POLLING_INTERVAL * 1000 ) - 2 ) / ((USBD_CDC_POLLING_INTERVAL * 1000 ) - 1 );
162162 if (i > 255 ) {
163163 i = 255 ;
164164 }
165165 return i ;
166166 }
167- #endif
168167
169168 const pin_obj_t * led_pin = pyb_led_obj [led - 1 ].led_pin ;
170169 GPIO_TypeDef * gpio = led_pin -> gpio ;
@@ -180,8 +179,7 @@ int led_get_intensity(pyb_led_t led) {
180179}
181180
182181void led_set_intensity (pyb_led_t led , mp_int_t intensity ) {
183- #if defined(PYBV4 ) || defined(PYBV10 )
184- if (led == 4 ) {
182+ if (MICROPY_HW_LED4_PWM && led == 4 ) {
185183 // set intensity using PWM pulse width
186184 if (intensity < 0 ) {
187185 intensity = 0 ;
@@ -193,7 +191,6 @@ void led_set_intensity(pyb_led_t led, mp_int_t intensity) {
193191 TIM3 -> CCR1 = intensity ;
194192 return ;
195193 }
196- #endif
197194
198195 // intensity not supported for this LED; just turn it on/off
199196 led_state (led , intensity > 0 );
0 commit comments