1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "aos/hal/can.h"
8 #include "aos_hal_can.h"
9 #include "aos_system.h"
10 
11 typedef enum {
12     PORT_CAN1 = 1,
13     PORT_CAN2 = 2,
14     PORT_CAN_SIZE,
15     PORT_CAN_INVALID = 255,
16 } PORT_CAN_NUM;
17 
18 extern can_dev_t g_can_handle[2];
19 static int can_received = 0;
20 static uint8_t can1_rx_data[8] = {0};
21 static uint8_t can2_rx_data[8] = {0};
22 static can_frameheader_t can1_rx_message = {0};
23 static can_frameheader_t can2_rx_message = {0};
24 
25 
aos_hal_can_init(can_dev_t * can)26 int32_t aos_hal_can_init(can_dev_t *can)
27 {
28     return hal_can_init(can);
29 }
30 
aos_hal_can_filter_init(can_dev_t * can,const uint8_t filter_grp_cnt,can_filter_item_t * filter_config)31 int32_t aos_hal_can_filter_init(can_dev_t *can, const uint8_t filter_grp_cnt, can_filter_item_t *filter_config)
32 {
33     return hal_can_filter_init(can, filter_grp_cnt, filter_config);
34 }
35 
aos_hal_can_send(can_dev_t * can,can_frameheader_t * tx_header,const void * data,const uint32_t timeout)36 int32_t aos_hal_can_send(can_dev_t *can, can_frameheader_t *tx_header, const void *data, const uint32_t timeout)
37 {
38     return hal_can_send(can, tx_header, data, timeout);
39 }
40 
can1_receive_callback(void)41 void can1_receive_callback(void)
42 {
43     int i, ret;
44 
45     ret = hal_can_recv(&g_can_handle[PORT_CAN1], &can1_rx_message, can1_rx_data, 0xFFFFFFFF);
46     if (ret != 0) {
47         aos_printf("hal can receive failed!\r\n");
48         return;
49     }
50 
51     can_received = 1;
52 
53 }
54 
can2_receive_callback(void)55 void can2_receive_callback(void)
56 {
57     int i, ret;
58 
59     ret = hal_can_recv(&g_can_handle[PORT_CAN2], &can2_rx_message, can2_rx_data, 0xFFFFFFFF);
60     if (ret != 0) {
61         aos_printf("hal can receive failed!\r\n");
62         return;
63     }
64 
65     can_received = 1;
66 
67 }
68 
aos_hal_can_recv(can_dev_t * can,can_frameheader_t * rx_header,void * data,const uint32_t timeout)69 int32_t aos_hal_can_recv(can_dev_t *can, can_frameheader_t *rx_header, void *data, const uint32_t timeout)
70 {
71     int32_t ret = -1;
72     if (can_received) {
73         if (can->port == PORT_CAN1) {
74             *rx_header = can1_rx_message;
75             if (rx_header->rtr == 0) {
76                 memcpy(data, can1_rx_data, 8);
77             }
78             can_received = 0;
79             return 0;
80         }
81         if (can->port == PORT_CAN2) {
82             *rx_header = can2_rx_message;
83             if (rx_header->rtr == 0) {
84                 memcpy(data, can2_rx_data, 8);
85             }
86             can_received = 0;
87             return 0;
88         }
89     }
90 }
91 
aos_hal_can_finalize(can_dev_t * can)92 int32_t aos_hal_can_finalize(can_dev_t *can)
93 {
94     return hal_can_finalize(can);
95 }
96