@@ -453,7 +453,7 @@ int readline(vstr_t *line, const char *prompt) {
453
453
}
454
454
455
455
void do_repl (void ) {
456
- stdout_tx_str ("Micro Python 0.1; STM32F405RG; PYBv3\r\n" );
456
+ stdout_tx_str ("Micro Python build <git hash> on 2/1/2014; PYBv3 with STM32F405RG \r\n" );
457
457
stdout_tx_str ("Type \"help()\" for more information.\r\n" );
458
458
459
459
vstr_t line ;
@@ -587,38 +587,53 @@ mp_obj_t pyb_gc(void) {
587
587
return mp_const_none ;
588
588
}
589
589
590
- #define MMA_ADDR (0x4c)
590
+ mp_obj_t pyb_gpio (int n_args , mp_obj_t * args ) {
591
+ //assert(1 <= n_args && n_args <= 2);
591
592
592
- int mma_buf [ 12 ] ;
593
-
594
- mp_obj_t pyb_mma_read ( void ) {
595
- for ( int i = 0 ; i <= 6 ; i += 3 ) {
596
- mma_buf [ 0 + i ] = mma_buf [ 0 + i + 3 ] ;
597
- mma_buf [ 1 + i ] = mma_buf [ 1 + i + 3 ] ;
598
- mma_buf [ 2 + i ] = mma_buf [ 2 + i + 3 ] ;
593
+ const char * pin_name = qstr_str ( mp_obj_get_qstr ( args [ 0 ])) ;
594
+ GPIO_TypeDef * port ;
595
+ switch ( pin_name [ 0 ] ) {
596
+ case 'A' : case 'a' : port = GPIOA ; break ;
597
+ case 'B' : case 'b' : port = GPIOB ; break ;
598
+ case 'C' : case 'c' : port = GPIOC ; break ;
599
+ default : goto pin_error ;
599
600
}
600
-
601
- mma_start (MMA_ADDR , 1 );
602
- mma_send_byte (0 );
603
- mma_restart (MMA_ADDR , 0 );
604
- for (int i = 0 ; i <= 2 ; i ++ ) {
605
- int v = mma_read_ack () & 0x3f ;
606
- if (v & 0x20 ) {
607
- v |= ~0x1f ;
601
+ uint pin_num = 0 ;
602
+ for (const char * s = pin_name + 1 ; * s ; s ++ ) {
603
+ if (!('0' <= * s && * s <= '9' )) {
604
+ goto pin_error ;
608
605
}
609
- mma_buf [9 + i ] = v ;
606
+ pin_num = 10 * pin_num + * s - '0' ;
607
+ }
608
+ if (!(0 <= pin_num && pin_num <= 15 )) {
609
+ goto pin_error ;
610
610
}
611
- int jolt_info = mma_read_nack ();
612
611
613
- mp_obj_t data [4 ];
614
- data [0 ] = mp_obj_new_int (jolt_info );
615
- data [1 ] = mp_obj_new_int (mma_buf [2 ] + mma_buf [5 ] + mma_buf [8 ] + mma_buf [11 ]);
616
- data [2 ] = mp_obj_new_int (mma_buf [1 ] + mma_buf [4 ] + mma_buf [7 ] + mma_buf [10 ]);
617
- data [3 ] = mp_obj_new_int (mma_buf [0 ] + mma_buf [3 ] + mma_buf [6 ] + mma_buf [9 ]);
612
+ if (n_args == 1 ) {
613
+ // get pin
614
+ if ((port -> IDR & (1 << pin_num )) != (uint32_t )Bit_RESET ) {
615
+ return MP_OBJ_NEW_SMALL_INT (1 );
616
+ } else {
617
+ return MP_OBJ_NEW_SMALL_INT (0 );
618
+ }
619
+ } else {
620
+ // set pin
621
+ if (rt_is_true (args [1 ])) {
622
+ // set pin high
623
+ port -> BSRRL = 1 << pin_num ;
624
+ } else {
625
+ // set pin low
626
+ port -> BSRRH = 1 << pin_num ;
627
+ }
628
+ return mp_const_none ;
629
+ }
618
630
619
- return rt_build_tuple (4 , data ); // items in reverse order in data
631
+ pin_error :
632
+ nlr_jump (mp_obj_new_exception_msg_1_arg (rt_q_ValueError , "pin %s does not exist" , pin_name ));
620
633
}
621
634
635
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (pyb_gpio_obj , 1 , 2 , pyb_gpio );
636
+
622
637
mp_obj_t pyb_hid_send_report (mp_obj_t arg ) {
623
638
mp_obj_t * items = mp_obj_get_array_fixed_n (arg , 4 );
624
639
uint8_t data [4 ];
@@ -855,7 +870,7 @@ int main(void) {
855
870
{
856
871
rt_store_name (qstr_from_str_static ("help" ), rt_make_function_0 (pyb_help ));
857
872
858
- mp_obj_t m = mp_module_new ( );
873
+ mp_obj_t m = mp_obj_new_module ( qstr_from_str_static ( "pyb" ) );
859
874
rt_store_attr (m , qstr_from_str_static ("info" ), rt_make_function_0 (pyb_info ));
860
875
rt_store_attr (m , qstr_from_str_static ("sd_test" ), rt_make_function_0 (pyb_sd_test ));
861
876
rt_store_attr (m , qstr_from_str_static ("stop" ), rt_make_function_0 (pyb_stop ));
@@ -869,7 +884,9 @@ int main(void) {
869
884
rt_store_attr (m , qstr_from_str_static ("switch" ), rt_make_function_0 (pyb_sw ));
870
885
rt_store_attr (m , qstr_from_str_static ("servo" ), rt_make_function_2 (pyb_servo_set ));
871
886
rt_store_attr (m , qstr_from_str_static ("pwm" ), rt_make_function_2 (pyb_pwm_set ));
872
- rt_store_attr (m , qstr_from_str_static ("accel" ), rt_make_function_0 (pyb_mma_read ));
887
+ rt_store_attr (m , qstr_from_str_static ("accel" ), (mp_obj_t )& pyb_mma_read_obj );
888
+ rt_store_attr (m , qstr_from_str_static ("mma_read" ), (mp_obj_t )& pyb_mma_read_all_obj );
889
+ rt_store_attr (m , qstr_from_str_static ("mma_mode" ), (mp_obj_t )& pyb_mma_write_mode_obj );
873
890
rt_store_attr (m , qstr_from_str_static ("hid" ), rt_make_function_1 (pyb_hid_send_report ));
874
891
rt_store_attr (m , qstr_from_str_static ("time" ), rt_make_function_0 (pyb_rtc_read ));
875
892
rt_store_attr (m , qstr_from_str_static ("uout" ), rt_make_function_1 (pyb_usart_send ));
@@ -879,6 +896,7 @@ int main(void) {
879
896
rt_store_attr (m , qstr_from_str_static ("Led" ), rt_make_function_1 (pyb_Led ));
880
897
rt_store_attr (m , qstr_from_str_static ("Servo" ), rt_make_function_1 (pyb_Servo ));
881
898
rt_store_attr (m , qstr_from_str_static ("I2C" ), rt_make_function_2 (pyb_I2C ));
899
+ rt_store_attr (m , qstr_from_str_static ("gpio" ), (mp_obj_t )& pyb_gpio_obj );
882
900
rt_store_name (qstr_from_str_static ("pyb" ), m );
883
901
884
902
rt_store_name (qstr_from_str_static ("open" ), rt_make_function_2 (pyb_io_open ));
@@ -985,56 +1003,6 @@ int main(void) {
985
1003
if (first_soft_reset ) {
986
1004
// init and reset address to zero
987
1005
mma_init ();
988
- mma_start (MMA_ADDR , 1 );
989
- mma_send_byte (0 );
990
- mma_stop ();
991
-
992
- /*
993
- // read and print all 11 registers
994
- mma_start(MMA_ADDR, 1);
995
- mma_send_byte(0);
996
- mma_restart(MMA_ADDR, 0);
997
- for (int i = 0; i <= 0xa; i++) {
998
- int data;
999
- if (i == 0xa) {
1000
- data = mma_read_nack();
1001
- } else {
1002
- data = mma_read_ack();
1003
- }
1004
- printf(" %02x", data);
1005
- }
1006
- printf("\n");
1007
- */
1008
-
1009
- // put into active mode
1010
- mma_start (MMA_ADDR , 1 );
1011
- mma_send_byte (7 ); // mode
1012
- mma_send_byte (1 ); // active mode
1013
- mma_stop ();
1014
-
1015
- /*
1016
- // infinite loop to read values
1017
- for (;;) {
1018
- sys_tick_delay_ms(500);
1019
-
1020
- mma_start(MMA_ADDR, 1);
1021
- mma_send_byte(0);
1022
- mma_restart(MMA_ADDR, 0);
1023
- for (int i = 0; i <= 3; i++) {
1024
- int data;
1025
- if (i == 3) {
1026
- data = mma_read_nack();
1027
- printf(" %02x\n", data);
1028
- } else {
1029
- data = mma_read_ack() & 0x3f;
1030
- if (data & 0x20) {
1031
- data |= ~0x1f;
1032
- }
1033
- printf(" % 2d", data);
1034
- }
1035
- }
1036
- }
1037
- */
1038
1006
}
1039
1007
1040
1008
// turn boot-up LED off
@@ -1220,9 +1188,9 @@ int main(void) {
1220
1188
} else {
1221
1189
data [0 ] = 0x00 ;
1222
1190
}
1223
- mma_start (MMA_ADDR , 1 );
1191
+ mma_start (0x4c /* MMA_ADDR */ , 1 );
1224
1192
mma_send_byte (0 );
1225
- mma_restart (MMA_ADDR , 0 );
1193
+ mma_restart (0x4c /* MMA_ADDR */ , 0 );
1226
1194
for (int i = 0 ; i <= 1 ; i ++ ) {
1227
1195
int v = mma_read_ack () & 0x3f ;
1228
1196
if (v & 0x20 ) {
0 commit comments