1 /*
2 * Copyright (c) 2022 Talkweb Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <string.h>
17 #include <stdio.h>
18 #include <los_interrupt.h>
19 #include "stm32f4xx_hal.h"
20
21 CAN_HandleTypeDef hcan1;
22
MX_CAN1_Init(void)23 void MX_CAN1_Init(void)
24 {
25 hcan1.Instance = CAN1;
26 hcan1.Init.Prescaler = 12;
27 hcan1.Init.Mode = CAN_MODE_NORMAL;
28 hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
29 hcan1.Init.TimeSeg1 = CAN_BS1_6TQ;
30 hcan1.Init.TimeSeg2 = CAN_BS2_7TQ;
31 hcan1.Init.TimeTriggeredMode = DISABLE;
32 hcan1.Init.AutoBusOff = DISABLE;
33 hcan1.Init.AutoWakeUp = DISABLE;
34 hcan1.Init.AutoRetransmission = DISABLE;
35 hcan1.Init.ReceiveFifoLocked = DISABLE;
36 hcan1.Init.TransmitFifoPriority = DISABLE;
37 if (HAL_CAN_Init(&hcan1) != HAL_OK) {
38 Error_Handler();
39 }
40 }
41
42
can_receive_irq(void)43 static void can_receive_irq(void)
44 {
45 printf("can irq\n");
46 HAL_CAN_IRQHandler(&hcan1);
47 }
48
49 /**
50 * @brief CAN MSP Initialization
51 * This function configures the hardware resources used in this example
52 * @param hcan: CAN handle pointer
53 * @retval None
54 */
HAL_CAN_MspInit(CAN_HandleTypeDef * hcan)55 void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
56 {
57 GPIO_InitTypeDef GPIO_InitStruct = {0};
58 if (hcan->Instance==CAN1) {
59 /* Peripheral clock enable */
60 __HAL_RCC_CAN1_CLK_ENABLE();
61
62 __HAL_RCC_GPIOI_CLK_ENABLE();
63 __HAL_RCC_GPIOB_CLK_ENABLE();
64 /**CAN1 GPIO Configuration
65 PI9 ------> CAN1_RX
66 PB9 ------> CAN1_TX
67 */
68 GPIO_InitStruct.Pin = GPIO_PIN_9;
69 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
70 GPIO_InitStruct.Pull = GPIO_NOPULL;
71 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
72 GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
73 HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
74
75 GPIO_InitStruct.Pin = GPIO_PIN_9;
76 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
77 GPIO_InitStruct.Pull = GPIO_NOPULL;
78 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
79 GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;
80 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
81
82 LOS_HwiCreate(CAN1_RX0_IRQn, 0, 0, (HWI_PROC_FUNC)can_receive_irq, 0);
83 }
84 }
85
86 /**
87 * @brief CAN MSP De-Initialization
88 * This function freeze the hardware resources used in this example
89 * @param hcan: CAN handle pointer
90 * @retval None
91 */
HAL_CAN_MspDeInit(CAN_HandleTypeDef * hcan)92 void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan)
93 {
94 if (hcan->Instance==CAN1) {
95 /* Peripheral clock disable */
96 __HAL_RCC_CAN1_CLK_DISABLE();
97 /**CAN1 GPIO Configuration
98 PI9 ------> CAN1_RX
99 PB9 ------> CAN1_TX
100 */
101 HAL_GPIO_DeInit(GPIOI, GPIO_PIN_9);
102 HAL_GPIO_DeInit(GPIOB, GPIO_PIN_9);
103 }
104 }
105