@@ -230,7 +230,7 @@ void glue_board_init(void)
230230 gpio_config (& io_conf );
231231
232232 HAL_GPIO_WritePin (GPIOA , CAN_S_Pin , GPIO_PIN_RESET );
233- glue_can_set_speed (500000 );
233+ glue_can_set_speed (& hcan1 , 500000 );
234234 HAL_CAN_Start (& hcan1 );
235235}
236236
@@ -269,47 +269,39 @@ void glue_ledc_set_duty(ledc_channel_t channel, uint32_t duty)
269269 ledc_update_duty (LEDC_LOW_SPEED_MODE , channel );
270270}
271271
272- void glue_can_set_speed (uint32_t rate )
272+ void glue_can_set_speed (CAN_HandleTypeDef * hcan , uint32_t rate )
273273{
274274 twai_timing_config_t t_50_config = TWAI_TIMING_CONFIG_50KBITS ();
275275 twai_timing_config_t t_100_config = TWAI_TIMING_CONFIG_100KBITS ();
276276 twai_timing_config_t t_125_config = TWAI_TIMING_CONFIG_125KBITS ();
277277 twai_timing_config_t t_250_config = TWAI_TIMING_CONFIG_250KBITS ();
278278 twai_timing_config_t t_500_config = TWAI_TIMING_CONFIG_500KBITS ();
279279 twai_timing_config_t t_1m_config = TWAI_TIMING_CONFIG_1MBITS ();
280- twai_timing_config_t t_config = {0 };
281280 switch (rate ) {
282281 case 50000 :
283- t_config = t_50_config ;
282+ hcan -> t_config = t_50_config ;
284283 break ;
285284 case 100000 :
286- t_config = t_100_config ;
285+ hcan -> t_config = t_100_config ;
287286 break ;
288287 case 125000 :
289- t_config = t_125_config ;
288+ hcan -> t_config = t_125_config ;
290289 break ;
291290 case 250000 :
292- t_config = t_250_config ;
291+ hcan -> t_config = t_250_config ;
293292 break ;
294293 case 500000 :
295- t_config = t_500_config ;
294+ hcan -> t_config = t_500_config ;
296295 break ;
297296 case 1000000 :
298- t_config = t_1m_config ;
297+ hcan -> t_config = t_1m_config ;
299298 break ;
300299
301300 default :
302301 ESP_LOGE (TAG , "Unsupported CAN rate" );
303302 break ;
304303 }
305304 ESP_LOGI (TAG , "set CAN rate to %d" , rate );
306- //Filter
307- const twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL ();
308- //
309- const twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT (CAN_TX_Pin , CAN_RX_Pin , TWAI_MODE_NORMAL );
310- //Install TWAI driver
311- ESP_ERROR_CHECK (twai_driver_install (& g_config , & t_config , & f_config ));
312- ESP_LOGI (TAG , "Driver installed" );
313305}
314306
315307
@@ -341,9 +333,19 @@ static void twai_receive_task(void *arg)
341333
342334HAL_StatusTypeDef HAL_CAN_Start (CAN_HandleTypeDef * hcan )
343335{
336+ //Filter
337+ twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL ();
338+ //
339+ twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT (CAN_TX_Pin , CAN_RX_Pin , TWAI_MODE_NORMAL );
340+ //Install TWAI driver
341+ esp_err_t ret = twai_driver_install (& g_config , & hcan -> t_config , & f_config );
342+ if (ESP_ERR_INVALID_STATE == ret ) {
343+ ESP_LOGW (TAG , "TWAI Driver is already installed" );
344+ }
345+ ESP_LOGI (TAG , "TWAI Driver installed" );
344346 ESP_ERROR_CHECK (twai_start ());
345- xTaskCreatePinnedToCore (twai_receive_task , "TWAI_rx" , 4096 , hcan , 10 , & hcan -> task , tskNO_AFFINITY );
346- ESP_LOGI (TAG , "Driver started" );
347+ xTaskCreatePinnedToCore (twai_receive_task , "TWAI_rx" , 4096 , hcan , 6 , & hcan -> task , 0 );
348+ ESP_LOGI (TAG , "TWAI Driver started" );
347349 return HAL_OK ;
348350}
349351
@@ -380,11 +382,11 @@ HAL_StatusTypeDef HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan, CAN_TxHeaderType
380382 // for (int i = 0; i < msg.data_length_code; i++) {
381383 // printf("%X, ", msg.data[i] );
382384 // } printf("\n");
383- esp_err_t ret = twai_transmit (& msg , portMAX_DELAY );
385+ esp_err_t ret = twai_transmit (& msg , pdMS_TO_TICKS ( 1000 ) );
384386 if (ret != ESP_OK ) {
385387 ESP_LOGE (TAG , "twai error %s" , esp_err_to_name (ret ));
386388 }
387- return HAL_OK ;
389+ return ret == ESP_OK ? HAL_OK : HAL_ERROR ;
388390}
389391
390392HAL_StatusTypeDef HAL_CAN_AbortTxRequest (CAN_HandleTypeDef * hcan , uint32_t TxMailboxes )
@@ -403,7 +405,7 @@ static void continuous_adc_init(uint16_t adc1_chan_mask, uint16_t adc2_chan_mask
403405#define GET_UNIT (x ) ((x>>3) & 0x1)
404406 adc_digi_init_config_t adc_dma_config = {
405407 .max_store_buf_size = 1024 ,
406- .conv_num_each_intr = channel_num * 4 * 2 ,
408+ .conv_num_each_intr = channel_num * 4 * 2 ,
407409 .adc1_chan_mask = adc1_chan_mask ,
408410 .adc2_chan_mask = adc2_chan_mask ,
409411 };
@@ -452,7 +454,7 @@ HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, ui
452454 // uint8_t c=0;
453455 // getAnalogBuffer(NULL, &c);
454456 // }
455-
457+
456458 return HAL_OK ;
457459}
458460
0 commit comments