Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
can.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012 Piotr Esden-Tempski <piotr@esden.net>
3 *
4 * This file is part of paparazzi.
5 *
6 * paparazzi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * paparazzi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with paparazzi; see the file COPYING. If not, write to
18 * the Free Software Foundation, 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 *
21 */
22
23#include <stdint.h>
24
25#include "mcu_periph/can.h"
26#include "mcu_periph/can_arch.h"
27
28#if USE_CAN1
29
30struct can_periph can1 = {
31 .fd = 1,
32 .nb_errors = 0,
33 .callbacks = {0},
34 .callback_user_data = {0}
35};
36#endif
37
38#if USE_CAN2
39struct can_periph can2 = {
40 .fd = 2,
41 .nb_errors = 0,
42 .callbacks = {0},
43 .callback_user_data = {0}
44};
45#endif
46
47static const uint8_t dlc_to_len[] = {0,1,2,3,4,5,6,7,8,12,16,20,24,32,48,64};
48
50 if(dlc < 16) {
51 return dlc_to_len[dlc];
52 }
53 return 0;
54}
55
57 if(len <= 8) {
58 return len;
59 }
60 for(int i=9; i<16; i++) {
61 if(dlc_to_len[i] >= len) {
62 return i;
63 }
64 }
65 return 15;
66}
67
68
70{
72}
73
74
75static int add_can_callback(struct can_periph* canp, can_rx_frame_callback_t callback, void* user_data) {
76 for(int i =0; i<CAN_NB_CALLBACKS_MAX; i++) {
77 // use the first available slot
78 if(canp->callbacks[i] == NULL) {
79 canp->callbacks[i] = callback;
80 canp->callback_user_data[i] = user_data;
81 return 0;
82 }
83 }
84 // no available slot
85 return -1;
86}
87
88int can_register_callback(can_rx_frame_callback_t callback, struct pprzaddr_can* src_addr, void* user_data) {
89 #if USE_CAN1
90 if(src_addr->can_ifindex == 1 || src_addr->can_ifindex == 0) {
91 int ret = add_can_callback(&can1, callback, user_data);
92 if(ret) { return ret; }
93 }
94 #endif
95 #if USE_CAN2
96 if(src_addr->can_ifindex == 2 || src_addr->can_ifindex == 0) {
97 int ret = add_can_callback(&can2, callback, user_data);
98 if(ret) { return ret; }
99 }
100 #endif
101 return 0;
102}
void can_init()
Definition can.c:69
uint8_t can_dlc_to_len(uint8_t dlc)
Definition can.c:49
static int add_can_callback(struct can_periph *canp, can_rx_frame_callback_t callback, void *user_data)
Definition can.c:75
int can_register_callback(can_rx_frame_callback_t callback, struct pprzaddr_can *src_addr, void *user_data)
Add a callback on received frames from an interface.
Definition can.c:88
uint8_t can_len_to_dlc(uint8_t len)
Definition can.c:56
static const uint8_t dlc_to_len[]
Definition can.c:47
void(* can_rx_frame_callback_t)(struct pprzcan_frame *rxframe, struct pprzaddr_can *src_addr, void *user_data)
Definition can.h:86
#define CAN_NB_CALLBACKS_MAX
Definition can.h:40
int fd
Definition can.h:91
void can_hw_init()
Definition can_arch.c:68
uint16_t foo
Definition main_demo5.c:58
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.