24
24
#include "timer.h"
25
25
#include "audio.h"
26
26
27
- static FATFS fatfs0 ;
28
-
29
27
extern uint32_t _heap_start ;
30
28
29
+ static FATFS fatfs0 ;
30
+
31
31
void flash_error (int n ) {
32
32
for (int i = 0 ; i < n ; i ++ ) {
33
33
led_state (PYB_LED_R1 , 1 );
@@ -376,9 +376,12 @@ void do_repl(void) {
376
376
py_obj_t module_fun = rt_make_function_from_id (1 );
377
377
if (module_fun != py_const_none ) {
378
378
nlr_buf_t nlr ;
379
+ uint32_t start = sys_tick_counter ;
379
380
if (nlr_push (& nlr ) == 0 ) {
380
381
rt_call_function_0 (module_fun );
381
382
nlr_pop ();
383
+ uint32_t ticks = sys_tick_counter - start ; // TODO implement a function that does this properly
384
+ printf ("(took %lu ms)\n" , ticks );
382
385
} else {
383
386
// uncaught exception
384
387
py_obj_print ((py_obj_t )nlr .ret_val );
@@ -468,7 +471,9 @@ void servo_init(void) {
468
471
// TIM2 clock enable
469
472
RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM2 , ENABLE );
470
473
471
- // GPIOC Configuration: TIM2_CH3 (PB10)
474
+ // for PB10
475
+ /*
476
+ // GPIOB Configuration: TIM2_CH3 (PB10)
472
477
GPIO_InitTypeDef GPIO_InitStructure;
473
478
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
474
479
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
@@ -479,6 +484,25 @@ void servo_init(void) {
479
484
480
485
// Connect TIM2 pins to AF1
481
486
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_TIM2);
487
+ */
488
+
489
+ // for PA0, PA1, PA2, PA3
490
+ {
491
+ // GPIOA Configuration: TIM2_CH0, TIM2_CH1 (PA0, PA1)
492
+ GPIO_InitTypeDef GPIO_InitStructure ;
493
+ GPIO_InitStructure .GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 ;
494
+ GPIO_InitStructure .GPIO_Mode = GPIO_Mode_AF ;
495
+ GPIO_InitStructure .GPIO_Speed = GPIO_Speed_2MHz ;
496
+ GPIO_InitStructure .GPIO_OType = GPIO_OType_PP ;
497
+ GPIO_InitStructure .GPIO_PuPd = GPIO_PuPd_NOPULL ;
498
+ GPIO_Init (GPIOA , & GPIO_InitStructure );
499
+
500
+ // Connect TIM2 pins to AF1
501
+ GPIO_PinAFConfig (GPIOA , GPIO_PinSource0 , GPIO_AF_TIM2 );
502
+ GPIO_PinAFConfig (GPIOA , GPIO_PinSource1 , GPIO_AF_TIM2 );
503
+ GPIO_PinAFConfig (GPIOA , GPIO_PinSource2 , GPIO_AF_TIM2 );
504
+ GPIO_PinAFConfig (GPIOA , GPIO_PinSource3 , GPIO_AF_TIM2 );
505
+ }
482
506
483
507
// Compute the prescaler value so TIM2 runs at 100kHz
484
508
uint16_t PrescalerValue = (uint16_t ) ((SystemCoreClock / 2 ) / 100000 ) - 1 ;
@@ -491,16 +515,22 @@ void servo_init(void) {
491
515
TIM_TimeBaseStructure .TIM_CounterMode = TIM_CounterMode_Up ;
492
516
TIM_TimeBaseInit (TIM2 , & TIM_TimeBaseStructure );
493
517
494
- // PWM1 Mode configuration: Channel1
518
+ // PWM Mode configuration
495
519
TIM_OCInitTypeDef TIM_OCInitStructure ;
496
520
TIM_OCInitStructure .TIM_OCMode = TIM_OCMode_PWM1 ;
497
521
TIM_OCInitStructure .TIM_OutputState = TIM_OutputState_Enable ;
498
522
TIM_OCInitStructure .TIM_Pulse = 150 ; // units of 10us
499
523
TIM_OCInitStructure .TIM_OCPolarity = TIM_OCPolarity_High ;
500
- TIM_OC3Init (TIM2 , & TIM_OCInitStructure );
524
+ TIM_OC1Init (TIM2 , & TIM_OCInitStructure ); // channel 1
525
+ TIM_OC2Init (TIM2 , & TIM_OCInitStructure ); // channel 2
526
+ TIM_OC3Init (TIM2 , & TIM_OCInitStructure ); // channel 3
527
+ TIM_OC4Init (TIM2 , & TIM_OCInitStructure ); // channel 4
501
528
502
529
// ?
503
- TIM_OC3PreloadConfig (TIM2 , TIM_OCPreload_Enable );
530
+ TIM_OC1PreloadConfig (TIM2 , TIM_OCPreload_Enable ); // channel 1
531
+ TIM_OC2PreloadConfig (TIM2 , TIM_OCPreload_Enable ); // channel 2
532
+ TIM_OC3PreloadConfig (TIM2 , TIM_OCPreload_Enable ); // channel 3
533
+ TIM_OC4PreloadConfig (TIM2 , TIM_OCPreload_Enable ); // channel 4
504
534
505
535
// ?
506
536
TIM_ARRPreloadConfig (TIM2 , ENABLE );
@@ -509,11 +539,17 @@ void servo_init(void) {
509
539
TIM_Cmd (TIM2 , ENABLE );
510
540
}
511
541
512
- py_obj_t pyb_servo_set (py_obj_t value ) {
542
+ py_obj_t pyb_servo_set (py_obj_t port , py_obj_t value ) {
543
+ int p = py_obj_get_int (port );
513
544
int v = py_obj_get_int (value );
514
545
if (v < 100 ) { v = 100 ; }
515
546
if (v > 200 ) { v = 200 ; }
516
- TIM2 -> CCR3 = v ;
547
+ switch (p ) {
548
+ case 1 : TIM2 -> CCR1 = v ; break ;
549
+ case 2 : TIM2 -> CCR2 = v ; break ;
550
+ case 3 : TIM2 -> CCR3 = v ; break ;
551
+ case 4 : TIM2 -> CCR4 = v ; break ;
552
+ }
517
553
return py_const_none ;
518
554
}
519
555
@@ -527,19 +563,33 @@ py_obj_t pyb_pwm_set(py_obj_t period, py_obj_t pulse) {
527
563
528
564
#define MMA_ADDR (0x4c)
529
565
566
+ int mma_buf [12 ];
567
+
530
568
py_obj_t pyb_mma_read (void ) {
569
+ for (int i = 0 ; i <= 6 ; i += 3 ) {
570
+ mma_buf [0 + i ] = mma_buf [0 + i + 3 ];
571
+ mma_buf [1 + i ] = mma_buf [1 + i + 3 ];
572
+ mma_buf [2 + i ] = mma_buf [2 + i + 3 ];
573
+ }
574
+
531
575
mma_start (MMA_ADDR , 1 );
532
576
mma_send_byte (0 );
533
577
mma_restart (MMA_ADDR , 0 );
534
- py_obj_t data [4 ];
535
- for (int i = 3 ; i >= 1 ; i -- ) {
578
+ for (int i = 0 ; i <= 2 ; i ++ ) {
536
579
int v = mma_read_ack () & 0x3f ;
537
580
if (v & 0x20 ) {
538
581
v |= ~0x1f ;
539
582
}
540
- data [ i ] = py_obj_new_int ( v ) ;
583
+ mma_buf [ 9 + i ] = v ;
541
584
}
542
- data [0 ] = py_obj_new_int (mma_read_nack ());
585
+ int jolt_info = mma_read_nack ();
586
+
587
+ py_obj_t data [4 ];
588
+ data [0 ] = py_obj_new_int (jolt_info );
589
+ data [1 ] = py_obj_new_int (mma_buf [2 ] + mma_buf [5 ] + mma_buf [8 ] + mma_buf [11 ]);
590
+ data [2 ] = py_obj_new_int (mma_buf [1 ] + mma_buf [4 ] + mma_buf [7 ] + mma_buf [10 ]);
591
+ data [3 ] = py_obj_new_int (mma_buf [0 ] + mma_buf [3 ] + mma_buf [6 ] + mma_buf [9 ]);
592
+
543
593
return rt_build_tuple (4 , data ); // items in reverse order in data
544
594
}
545
595
@@ -742,7 +792,7 @@ int main(void) {
742
792
rt_init ();
743
793
744
794
// LCD init
745
- lcd_init ();
795
+ // lcd_init(); disabled while servos on PA0 PA1
746
796
747
797
// servo
748
798
servo_init ();
@@ -770,7 +820,7 @@ int main(void) {
770
820
rt_store_attr (m , qstr_from_str_static ("delay" ), rt_make_function_1 (pyb_delay ));
771
821
rt_store_attr (m , qstr_from_str_static ("led" ), rt_make_function_1 (pyb_led ));
772
822
rt_store_attr (m , qstr_from_str_static ("sw" ), rt_make_function_0 (pyb_sw ));
773
- rt_store_attr (m , qstr_from_str_static ("servo" ), rt_make_function_1 (pyb_servo_set ));
823
+ rt_store_attr (m , qstr_from_str_static ("servo" ), rt_make_function_2 (pyb_servo_set ));
774
824
rt_store_attr (m , qstr_from_str_static ("pwm" ), rt_make_function_2 (pyb_pwm_set ));
775
825
rt_store_attr (m , qstr_from_str_static ("mma" ), rt_make_function_0 (pyb_mma_read ));
776
826
rt_store_attr (m , qstr_from_str_static ("hid" ), rt_make_function_1 (pyb_hid_send_report ));
@@ -1126,7 +1176,7 @@ int main(void) {
1126
1176
// SD card testing
1127
1177
if (1 ) {
1128
1178
extern void sdio_init (void );
1129
- sdio_init ();
1179
+ // sdio_init();
1130
1180
}
1131
1181
1132
1182
printf ("PYB: sync filesystems\n" );
0 commit comments