11#include <stdio.h>
22#include <stm32f4xx.h>
33#include <stm32f4xx_rcc.h>
4+ #include <stm32f4xx_syscfg.h>
45#include <stm32f4xx_gpio.h>
6+ #include <stm32f4xx_exti.h>
57#include <stm32f4xx_tim.h>
68#include <stm32f4xx_pwr.h>
79#include <stm32f4xx_rtc.h>
@@ -68,6 +70,30 @@ void sw_init(void) {
6870 GPIO_InitStructure .GPIO_Mode = GPIO_Mode_IN ;
6971 GPIO_InitStructure .GPIO_PuPd = GPIO_PuPd_UP ;
7072 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 );
7197}
7298
7399int sw_get (void ) {
@@ -173,6 +199,34 @@ py_obj_t pyb_sw(void) {
173199 }
174200}
175201
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+
176230/*
177231void g(uint i) {
178232 printf("g:%d\n", i);
@@ -272,6 +326,51 @@ static py_obj_t pyb_info(void) {
272326 return py_const_none ;
273327}
274328
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+
275374py_obj_t pyb_usart_send (py_obj_t data ) {
276375 usart_tx_char (py_obj_get_int (data ));
277376 return py_const_none ;
@@ -322,6 +421,9 @@ int readline(vstr_t *line, const char *prompt) {
322421 break ;
323422 }
324423 sys_tick_delay_ms (1 );
424+ if (storage_needs_flush ()) {
425+ storage_flush ();
426+ }
325427 }
326428 if (escape == 0 ) {
327429 if (c == 4 && vstr_len (line ) == len ) {
@@ -422,7 +524,7 @@ void do_repl(void) {
422524 rt_call_function_0 (module_fun );
423525 nlr_pop ();
424526 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);
426528 } else {
427529 // uncaught exception
428530 py_obj_print ((py_obj_t )nlr .ret_val );
@@ -583,8 +685,8 @@ void servo_init(void) {
583685py_obj_t pyb_servo_set (py_obj_t port , py_obj_t value ) {
584686 int p = py_obj_get_int (port );
585687 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 ; }
588690 switch (p ) {
589691 case 1 : TIM2 -> CCR1 = v ; break ;
590692 case 2 : TIM2 -> CCR2 = v ; break ;
@@ -843,13 +945,13 @@ int main(void) {
843945 servo_init ();
844946
845947 // audio
846- audio_init ();
948+ // audio_init();
847949
848950 // timer
849951 timer_init ();
850952
851953 // RNG
852- {
954+ if ( 0 ) {
853955 RCC_AHB2PeriphClockCmd (RCC_AHB2Periph_RNG , ENABLE );
854956 RNG_Cmd (ENABLE );
855957 }
@@ -858,6 +960,8 @@ int main(void) {
858960 {
859961 py_obj_t m = py_module_new ();
860962 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 ));
861965 rt_store_attr (m , qstr_from_str_static ("source_dir" ), rt_make_function_1 (pyb_source_dir ));
862966 rt_store_attr (m , qstr_from_str_static ("main" ), rt_make_function_1 (pyb_main ));
863967 rt_store_attr (m , qstr_from_str_static ("sync" ), rt_make_function_0 (pyb_sync ));
@@ -875,6 +979,7 @@ int main(void) {
875979 rt_store_attr (m , qstr_from_str_static ("ustat" ), rt_make_function_0 (pyb_usart_status ));
876980 rt_store_attr (m , qstr_from_str_static ("rng" ), rt_make_function_0 (pyb_rng_get ));
877981 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 ));
878983 rt_store_name (qstr_from_str_static ("pyb" ), m );
879984
880985 rt_store_name (qstr_from_str_static ("open" ), rt_make_function_2 (pyb_io_open ));
0 commit comments