#include "main.h" ADC_HandleTypeDef hadc; CAN_HandleTypeDef hcan; UART_HandleTypeDef huart2; CAN_FilterTypeDef sFilterConfig; CAN_TxHeaderTypeDef Tx1Header; CAN_TxHeaderTypeDef Tx2Header; CAN_RxHeaderTypeDef RxHeader; uint8_t Tx1Data[8]; uint8_t Tx2Data[8]; uint8_t RxData[8]; uint32_t TxMailbox; void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART2_UART_Init(void); static void MX_ADC_Init(void); static void MX_CAN_Init(void); static void config_CAN_interrupt(void); static void configtx1(void); static void configtx2(void); void HAL_CAN_TxMailbox0CompleteCallback(); void HAL_CAN_RxFifo0MsgPendingCallback(); int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART2_UART_Init(); MX_ADC_Init(); MX_CAN_Init(); HAL_CAN_MspInit(&hcan); HAL_CAN_IRQHandler(&hcan); /*config_CAN_interrupt();*/ configtx1(); configtx2(); HAL_CAN_Start(&hcan); HAL_CAN_ActivateNotification(&hcan,CAN_IT_RX_FIFO0_MSG_PENDING); sFilterConfig.FilterBank=0; sFilterConfig.FilterMode=CAN_FILTERMODE_IDMASK; sFilterConfig.FilterScale=CAN_FILTERSCALE_32BIT; sFilterConfig.FilterIdHigh=0x0000; sFilterConfig.FilterIdLow=0x0000; sFilterConfig.FilterMaskIdHigh=0x0000; /*filter range*/ sFilterConfig.FilterMaskIdLow=0x0000; sFilterConfig.FilterFIFOAssignment=CAN_RX_FIFO0; sFilterConfig.FilterActivation=ENABLE; sFilterConfig.SlaveStartFilterBank=14; if(HAL_CAN_ConfigFilter(&hcan,&sFilterConfig)!=HAL_OK) { Error_Handler(); } while (1) { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 1); HAL_Delay(100); HAL_CAN_AddTxMessage(&hcan,&Tx1Header,Tx1Data,&TxMailbox); Tx1Data[7]=Tx1Data[7]+1; HAL_CAN_AddTxMessage(&hcan,&Tx2Header,Tx2Data,&TxMailbox); Tx2Data[7]=Tx2Data[7]+1; HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 0); HAL_Delay(100); } } void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef*hcan) { } void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef*hcan) { /*add error handler here*/ HAL_CAN_GetRxMessage(&hcan,CAN_RX_FIFO0,&RxHeader,RxData); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 1); HAL_Delay(1000); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, 0); HAL_Delay(1000); /*HAL_CAN_MspDeInit(hcan);*/ } static void config_CAN_interrupt(void) { if(HAL_CAN_Start(&hcan)!=HAL_OK) /*start CAN*/ { Error_Handler(); } if(HAL_CAN_ActivateNotification(&hcan,CAN_IT_RX_FIFO0_MSG_PENDING |CAN_IT_TX_MAILBOX_EMPTY)!=HAL_OK) /*configure CAN interrupt*/ { Error_Handler(); } } static void configtx1(void) { Tx1Header.StdId =0x321; Tx1Header.ExtId =0x01; Tx1Header.RTR=CAN_RTR_DATA; Tx1Header.IDE=CAN_ID_STD; Tx1Header.DLC=8; Tx1Header.TransmitGlobalTime=DISABLE; Tx1Data[0]=1; Tx1Data[1]=2; Tx1Data[2]=3; Tx1Data[3]=4; Tx1Data[4]=5; Tx1Data[5]=6; Tx1Data[6]=7; Tx1Data[7]=8; } static void configtx2(void) { Tx2Header.StdId =0x123; Tx2Header.ExtId =0x01; Tx2Header.RTR=CAN_RTR_DATA; Tx2Header.IDE=CAN_ID_STD; Tx2Header.DLC=8; Tx2Header.TransmitGlobalTime=DISABLE; Tx2Data[0]=1; Tx2Data[1]=2; Tx2Data[2]=3; Tx2Data[3]=4; Tx2Data[4]=5; Tx2Data[5]=6; Tx2Data[6]=7; Tx2Data[7]=8; } void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI14|RCC_OSCILLATORTYPE_HSI48; RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; RCC_OscInitStruct.HSI14State = RCC_HSI14_ON; RCC_OscInitStruct.HSI14CalibrationValue = 16; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB busses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { Error_Handler(); } } static void MX_ADC_Init(void) { ADC_ChannelConfTypeDef sConfig = {0}; hadc.Instance = ADC1; hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; hadc.Init.Resolution = ADC_RESOLUTION_12B; hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV; hadc.Init.LowPowerAutoWait = DISABLE; hadc.Init.LowPowerAutoPowerOff = DISABLE; hadc.Init.ContinuousConvMode = DISABLE; hadc.Init.DiscontinuousConvMode = DISABLE; hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; hadc.Init.DMAContinuousRequests = DISABLE; hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED; if (HAL_ADC_Init(&hadc) != HAL_OK) { Error_Handler(); } /** Configure for the selected ADC regular channel to be converted. */ sConfig.Channel = ADC_CHANNEL_5; sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) { Error_Handler(); } /** Configure for the selected ADC regular channel to be converted. */ sConfig.Channel = ADC_CHANNEL_6; if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) { Error_Handler(); } /** Configure for the selected ADC regular channel to be converted. */ sConfig.Channel = ADC_CHANNEL_7; if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) { Error_Handler(); } /** Configure for the selected ADC regular channel to be converted. */ sConfig.Channel = ADC_CHANNEL_8; if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) { Error_Handler(); } /** Configure for the selected ADC regular channel to be converted. */ sConfig.Channel = ADC_CHANNEL_9; if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) { Error_Handler(); } /** Configure for the selected ADC regular channel to be converted. */ sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) { Error_Handler(); } } static void MX_CAN_Init(void) { hcan.Instance = CAN; hcan.Init.Prescaler = 2; hcan.Init.Mode = CAN_MODE_NORMAL; hcan.Init.SyncJumpWidth = CAN_SJW_1TQ; hcan.Init.TimeSeg1 = CAN_BS1_10TQ; hcan.Init.TimeSeg2 = CAN_BS2_1TQ; hcan.Init.TimeTriggeredMode = DISABLE; hcan.Init.AutoBusOff = DISABLE; hcan.Init.AutoWakeUp = DISABLE; hcan.Init.AutoRetransmission = DISABLE; hcan.Init.ReceiveFifoLocked = DISABLE; hcan.Init.TransmitFifoPriority = DISABLE; if (HAL_CAN_Init(&hcan) != HAL_OK) { Error_Handler(); } } static void MX_USART2_UART_Init(void) { huart2.Instance = USART2; huart2.Init.BaudRate = 38400; huart2.Init.WordLength = UART_WORDLENGTH_8B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart2.Init.OverSampling = UART_OVERSAMPLING_16; huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_UART_Init(&huart2) != HAL_OK) { Error_Handler(); } } static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOF_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET); /*Configure GPIO pin : PB3 */ GPIO_InitStruct.Pin = GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } void Error_Handler(void) { } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */