@@ -343,44 +343,61 @@ STATIC mp_obj_t pyb_can_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp
343343 // check arguments
344344 mp_arg_check_num (n_args , n_kw , 1 , MP_OBJ_FUN_ARGS_MAX , true);
345345
346- // create object
347- pyb_can_obj_t * o = m_new_obj (pyb_can_obj_t );
348- o -> base .type = & pyb_can_type ;
349- o -> is_enabled = false;
350-
351346 // work out port
352- o -> can_id = 0 ;
347+ mp_uint_t can_idx ;
353348 if (MP_OBJ_IS_STR (args [0 ])) {
354349 const char * port = mp_obj_str_get_str (args [0 ]);
355350 if (0 ) {
356351 #ifdef MICROPY_HW_CAN1_NAME
357352 } else if (strcmp (port , MICROPY_HW_CAN1_NAME ) == 0 ) {
358- o -> can_id = PYB_CAN_1 ;
353+ can_idx = PYB_CAN_1 ;
359354 #endif
360355 #ifdef MICROPY_HW_CAN2_NAME
361356 } else if (strcmp (port , MICROPY_HW_CAN2_NAME ) == 0 ) {
362- o -> can_id = PYB_CAN_2 ;
357+ can_idx = PYB_CAN_2 ;
363358 #endif
364359 } else {
365- nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError , "CAN port %s does not exist" , port ));
360+ nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError , "CAN(%s) does not exist" , port ));
366361 }
367362 } else {
368- o -> can_id = mp_obj_get_int (args [0 ]);
363+ can_idx = mp_obj_get_int (args [0 ]);
364+ }
365+ if (can_idx < 1 || can_idx > MP_ARRAY_SIZE (MP_STATE_PORT (pyb_can_obj_all ))) {
366+ nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError , "CAN(%d) does not exist" , can_idx ));
367+ }
368+
369+ pyb_can_obj_t * self ;
370+ if (MP_STATE_PORT (pyb_can_obj_all )[can_idx - 1 ] == NULL ) {
371+ self = m_new_obj (pyb_can_obj_t );
372+ self -> base .type = & pyb_can_type ;
373+ self -> can_id = can_idx ;
374+ self -> is_enabled = false;
375+ MP_STATE_PORT (pyb_can_obj_all )[can_idx - 1 ] = self ;
376+ } else {
377+ self = MP_STATE_PORT (pyb_can_obj_all )[can_idx - 1 ];
369378 }
370- o -> rxcallback0 = mp_const_none ;
371- o -> rxcallback1 = mp_const_none ;
372- MP_STATE_PORT (pyb_can_obj_all )[o -> can_id - 1 ] = o ;
373- o -> rx_state0 = RX_STATE_FIFO_EMPTY ;
374- o -> rx_state1 = RX_STATE_FIFO_EMPTY ;
375379
376- if (n_args > 1 || n_kw > 0 ) {
377- // start the peripheral
378- mp_map_t kw_args ;
379- mp_map_init_fixed_table (& kw_args , n_kw , args + n_args );
380- pyb_can_init_helper (o , n_args - 1 , args + 1 , & kw_args );
380+ if (!self -> is_enabled || n_args > 1 ) {
381+ if (self -> is_enabled ) {
382+ // The caller is requesting a reconfiguration of the hardware
383+ // this can only be done if the hardware is in init mode
384+ pyb_can_deinit (self );
385+ }
386+
387+ self -> rxcallback0 = mp_const_none ;
388+ self -> rxcallback1 = mp_const_none ;
389+ self -> rx_state0 = RX_STATE_FIFO_EMPTY ;
390+ self -> rx_state1 = RX_STATE_FIFO_EMPTY ;
391+
392+ if (n_args > 1 || n_kw > 0 ) {
393+ // start the peripheral
394+ mp_map_t kw_args ;
395+ mp_map_init_fixed_table (& kw_args , n_kw , args + n_args );
396+ pyb_can_init_helper (self , n_args - 1 , args + 1 , & kw_args );
397+ }
381398 }
382399
383- return o ;
400+ return self ;
384401}
385402
386403STATIC mp_obj_t pyb_can_init (mp_uint_t n_args , const mp_obj_t * args , mp_map_t * kw_args ) {
0 commit comments