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 <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include "ohos_run.h"
21 #include "cmsis_os2.h"
22 #include "stm32f4xx_hal.h"
23 #include "can_init.h"
24
25 CAN_TxHeaderTypeDef TxHeader;
26 CAN_TxHeaderTypeDef RxHeader;
27 CAN_FilterTypeDef Filter;
28
HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef * hcan)29 void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
30 {
31 uint8_t aRxData[8] = {0};
32 if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &RxHeader, aRxData) == HAL_OK) {
33 uint32_t canid = (RxHeader.IDE == CAN_ID_STD)?RxHeader.StdId:RxHeader.ExtId;
34 printf("\nRecv CANID:0x%08X Data:", canid);
35 for (uint8_t i = 0; i<8; i++)
36 printf("%02X ", aRxData[i]);
37 printf("\n");
38 }
39 }
40
thread_entry(void)41 void thread_entry(void)
42 {
43 uint8_t TxData[8] = {0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11};
44 uint32_t TxMailbox;
45 uint32_t send_can_id = 0x1234;
46
47 TxHeader.RTR = CAN_RTR_DATA;
48 TxHeader.IDE = CAN_ID_EXT;
49 TxHeader.ExtId = send_can_id;
50 TxHeader.TransmitGlobalTime = DISABLE;
51 TxHeader.DLC = 8;
52
53 Filter.FilterIdHigh = 0;
54 Filter.FilterIdLow = 0;
55 Filter.FilterMaskIdHigh = 0;
56 Filter.FilterMaskIdLow = 0;
57 Filter.FilterFIFOAssignment = CAN_FILTER_FIFO0;
58 Filter.FilterBank = 0;
59 Filter.FilterMode = CAN_FILTERMODE_IDMASK;
60 Filter.FilterScale = CAN_FILTERSCALE_32BIT;
61 Filter.FilterActivation = ENABLE;
62 Filter.SlaveStartFilterBank = 0;
63
64 HAL_CAN_ConfigFilter(&hcan1, &Filter);
65
66 while (HAL_OK != HAL_CAN_Start(&hcan1)) {
67 printf("HAL_CAN_Start fail! try again!\n");
68 osDelay(100);
69 }
70
71 printf("HAL_CAN_Start success!\n");
72 HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
73
74 while (1) {
75 uint32_t ret = HAL_CAN_AddTxMessage(&hcan1, &TxHeader, TxData, &TxMailbox);
76 if (ret == HAL_OK) {
77 printf("send can data [0x%04X : %02X %02X %02X %02X %02X %02X %02X %02X] success! Tx_Mail:%u\n", \
78 send_can_id, TxData[0], TxData[1], TxData[2], TxData[3], TxData[4], TxData[5], TxData[6], \
79 TxData[7], TxMailbox);
80 }
81 else {
82 printf("send can data fail,ret = %s\n", \
83 (ret==HAL_ERROR)?"HAL_ERROR":((ret==HAL_BUSY)?"HAL_BUSY":"HAL_TIMEOUT"));
84 }
85 osDelay(1000);
86 }
87 }
88
can_send_example(void)89 static void can_send_example(void)
90 {
91 MX_CAN1_Init();
92
93 osThreadAttr_t attr;
94 attr.name = "thread1";
95 attr.attr_bits = 0U;
96 attr.cb_mem = NULL;
97 attr.cb_size = 0U;
98 attr.stack_mem = NULL;
99 attr.stack_size = 4096;
100 attr.priority = 25;
101
102 if (osThreadNew((osThreadFunc_t)thread_entry, NULL, &attr) == NULL) {
103 printf("Failed to create thread!\n");
104 }
105 }
106
107 OHOS_APP_RUN(can_send_example);
108