1
1
#include <stdio.h>
2
2
#include <stm32f4xx.h>
3
3
#include <stm32f4xx_rcc.h>
4
+ #include <stm32f4xx_syscfg.h>
4
5
#include <stm32f4xx_gpio.h>
6
+ #include <stm32f4xx_exti.h>
5
7
#include <stm32f4xx_tim.h>
6
8
#include <stm32f4xx_pwr.h>
7
9
#include <stm32f4xx_rtc.h>
@@ -68,6 +70,30 @@ void sw_init(void) {
68
70
GPIO_InitStructure .GPIO_Mode = GPIO_Mode_IN ;
69
71
GPIO_InitStructure .GPIO_PuPd = GPIO_PuPd_UP ;
70
72
GPIO_Init (PYB_USRSW_PORT , & GPIO_InitStructure );
73
+
74
+ // the rest does the EXTI interrupt
75
+
76
+ /* Enable SYSCFG clock */
77
+ RCC_APB2PeriphClockCmd (RCC_APB2Periph_SYSCFG , ENABLE );
78
+
79
+ /* Connect EXTI Line13 to PA13 pin */
80
+ SYSCFG_EXTILineConfig (EXTI_PortSourceGPIOA , EXTI_PinSource13 );
81
+
82
+ /* Configure EXTI Line13, rising edge */
83
+ EXTI_InitTypeDef EXTI_InitStructure ;
84
+ EXTI_InitStructure .EXTI_Line = EXTI_Line13 ;
85
+ EXTI_InitStructure .EXTI_Mode = EXTI_Mode_Interrupt ;
86
+ EXTI_InitStructure .EXTI_Trigger = EXTI_Trigger_Rising ;
87
+ EXTI_InitStructure .EXTI_LineCmd = ENABLE ;
88
+ EXTI_Init (& EXTI_InitStructure );
89
+
90
+ /* Enable and set EXTI15_10 Interrupt to the lowest priority */
91
+ NVIC_InitTypeDef NVIC_InitStructure ;
92
+ NVIC_InitStructure .NVIC_IRQChannel = EXTI15_10_IRQn ;
93
+ NVIC_InitStructure .NVIC_IRQChannelPreemptionPriority = 0x0F ;
94
+ NVIC_InitStructure .NVIC_IRQChannelSubPriority = 0x0F ;
95
+ NVIC_InitStructure .NVIC_IRQChannelCmd = ENABLE ;
96
+ NVIC_Init (& NVIC_InitStructure );
71
97
}
72
98
73
99
int sw_get (void ) {
@@ -173,6 +199,34 @@ py_obj_t pyb_sw(void) {
173
199
}
174
200
}
175
201
202
+ py_obj_t servo_obj_angle (py_obj_t self , py_obj_t angle ) {
203
+ machine_uint_t servo_id ;
204
+ py_user_get_data (self , & servo_id , NULL );
205
+ machine_int_t v = 152 + 85.0 * py_obj_get_float (angle ) / 90.0 ;
206
+ if (v < 65 ) { v = 65 ; }
207
+ if (v > 210 ) { v = 210 ; }
208
+ switch (servo_id ) {
209
+ case 1 : TIM2 -> CCR1 = v ; break ;
210
+ case 2 : TIM2 -> CCR2 = v ; break ;
211
+ case 3 : TIM2 -> CCR3 = v ; break ;
212
+ case 4 : TIM2 -> CCR4 = v ; break ;
213
+ }
214
+ return py_const_none ;
215
+ }
216
+
217
+ const py_user_info_t servo_obj_info = {
218
+ "Servo" ,
219
+ NULL , // print
220
+ {
221
+ {"angle" , 1 , servo_obj_angle },
222
+ {NULL , 0 , NULL },
223
+ }
224
+ };
225
+
226
+ py_obj_t pyb_Servo (py_obj_t servo_id ) {
227
+ return py_obj_new_user (& servo_obj_info , (machine_uint_t )py_obj_get_int (servo_id ), 0 );
228
+ }
229
+
176
230
/*
177
231
void g(uint i) {
178
232
printf("g:%d\n", i);
@@ -272,6 +326,51 @@ static py_obj_t pyb_info(void) {
272
326
return py_const_none ;
273
327
}
274
328
329
+ static void SYSCLKConfig_STOP (void ) {
330
+ /* After wake-up from STOP reconfigure the system clock */
331
+ /* Enable HSE */
332
+ RCC_HSEConfig (RCC_HSE_ON );
333
+
334
+ /* Wait till HSE is ready */
335
+ while (RCC_GetFlagStatus (RCC_FLAG_HSERDY ) == RESET ) {
336
+ }
337
+
338
+ /* Enable PLL */
339
+ RCC_PLLCmd (ENABLE );
340
+
341
+ /* Wait till PLL is ready */
342
+ while (RCC_GetFlagStatus (RCC_FLAG_PLLRDY ) == RESET ) {
343
+ }
344
+
345
+ /* Select PLL as system clock source */
346
+ RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK );
347
+
348
+ /* Wait till PLL is used as system clock source */
349
+ while (RCC_GetSYSCLKSource () != 0x08 ) {
350
+ }
351
+ }
352
+
353
+ static py_obj_t pyb_stop (void ) {
354
+ PWR_EnterSTANDBYMode ();
355
+ //PWR_FlashPowerDownCmd(ENABLE); don't know what the logic is with this
356
+
357
+ /* Enter Stop Mode */
358
+ PWR_EnterSTOPMode (PWR_Regulator_LowPower , PWR_STOPEntry_WFI );
359
+
360
+ /* Configures system clock after wake-up from STOP: enable HSE, PLL and select
361
+ * PLL as system clock source (HSE and PLL are disabled in STOP mode) */
362
+ SYSCLKConfig_STOP ();
363
+
364
+ //PWR_FlashPowerDownCmd(DISABLE);
365
+
366
+ return py_const_none ;
367
+ }
368
+
369
+ static py_obj_t pyb_standby (void ) {
370
+ PWR_EnterSTANDBYMode ();
371
+ return py_const_none ;
372
+ }
373
+
275
374
py_obj_t pyb_usart_send (py_obj_t data ) {
276
375
usart_tx_char (py_obj_get_int (data ));
277
376
return py_const_none ;
@@ -322,6 +421,9 @@ int readline(vstr_t *line, const char *prompt) {
322
421
break ;
323
422
}
324
423
sys_tick_delay_ms (1 );
424
+ if (storage_needs_flush ()) {
425
+ storage_flush ();
426
+ }
325
427
}
326
428
if (escape == 0 ) {
327
429
if (c == 4 && vstr_len (line ) == len ) {
@@ -422,7 +524,7 @@ void do_repl(void) {
422
524
rt_call_function_0 (module_fun );
423
525
nlr_pop ();
424
526
uint32_t ticks = sys_tick_counter - start ; // TODO implement a function that does this properly
425
- printf ("(took %lu ms)\n" , ticks );
527
+ // printf("(took %lu ms)\n", ticks);
426
528
} else {
427
529
// uncaught exception
428
530
py_obj_print ((py_obj_t )nlr .ret_val );
@@ -583,8 +685,8 @@ void servo_init(void) {
583
685
py_obj_t pyb_servo_set (py_obj_t port , py_obj_t value ) {
584
686
int p = py_obj_get_int (port );
585
687
int v = py_obj_get_int (value );
586
- if (v < 100 ) { v = 100 ; }
587
- if (v > 200 ) { v = 200 ; }
688
+ if (v < 50 ) { v = 50 ; }
689
+ if (v > 250 ) { v = 250 ; }
588
690
switch (p ) {
589
691
case 1 : TIM2 -> CCR1 = v ; break ;
590
692
case 2 : TIM2 -> CCR2 = v ; break ;
@@ -843,13 +945,13 @@ int main(void) {
843
945
servo_init ();
844
946
845
947
// audio
846
- audio_init ();
948
+ // audio_init();
847
949
848
950
// timer
849
951
timer_init ();
850
952
851
953
// RNG
852
- {
954
+ if ( 0 ) {
853
955
RCC_AHB2PeriphClockCmd (RCC_AHB2Periph_RNG , ENABLE );
854
956
RNG_Cmd (ENABLE );
855
957
}
@@ -858,6 +960,8 @@ int main(void) {
858
960
{
859
961
py_obj_t m = py_module_new ();
860
962
rt_store_attr (m , qstr_from_str_static ("info" ), rt_make_function_0 (pyb_info ));
963
+ rt_store_attr (m , qstr_from_str_static ("stop" ), rt_make_function_0 (pyb_stop ));
964
+ rt_store_attr (m , qstr_from_str_static ("standby" ), rt_make_function_0 (pyb_standby ));
861
965
rt_store_attr (m , qstr_from_str_static ("source_dir" ), rt_make_function_1 (pyb_source_dir ));
862
966
rt_store_attr (m , qstr_from_str_static ("main" ), rt_make_function_1 (pyb_main ));
863
967
rt_store_attr (m , qstr_from_str_static ("sync" ), rt_make_function_0 (pyb_sync ));
@@ -875,6 +979,7 @@ int main(void) {
875
979
rt_store_attr (m , qstr_from_str_static ("ustat" ), rt_make_function_0 (pyb_usart_status ));
876
980
rt_store_attr (m , qstr_from_str_static ("rng" ), rt_make_function_0 (pyb_rng_get ));
877
981
rt_store_attr (m , qstr_from_str_static ("Led" ), rt_make_function_1 (pyb_Led ));
982
+ rt_store_attr (m , qstr_from_str_static ("Servo" ), rt_make_function_1 (pyb_Servo ));
878
983
rt_store_name (qstr_from_str_static ("pyb" ), m );
879
984
880
985
rt_store_name (qstr_from_str_static ("open" ), rt_make_function_2 (pyb_io_open ));
0 commit comments