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 .callbacks = {0},
33 .callback_user_data = {0}
34};
35#endif
36
37static const uint8_t dlc_to_len[] = {0,1,2,3,4,5,6,7,8,12,16,20,24,32,48,64};
38
40 if(dlc < 16) {
41 return dlc_to_len[dlc];
42 }
43 return 0;
44}
45
47 if(len <= 8) {
48 return len;
49 }
50 for(int i=9; i<16; i++) {
51 if(dlc_to_len[i] >= len) {
52 return i;
53 }
54 }
55 return 15;
56}
57
58
60{
62}
63
64
65static int add_can_callback(struct can_periph* canp, can_rx_frame_callback_t callback, void* user_data) {
66 for(int i =0; i<CAN_NB_CALLBACKS_MAX; i++) {
67 // use the first available slot
68 if(canp->callbacks[i] == NULL) {
69 canp->callbacks[i] = callback;
70 canp->callback_user_data[i] = user_data;
71 return 0;
72 }
73 }
74 // no available slot
75 return -1;
76}
77
78int can_register_callback(can_rx_frame_callback_t callback, struct pprzaddr_can* src_addr, void* user_data) {
79 #if USE_CAN1
80 if(src_addr->can_ifindex == 1 || src_addr->can_ifindex == 0) {
81 int ret = add_can_callback(&can1, callback, user_data);
82 if(ret) { return ret; }
83 }
84 #endif
85 #if USE_CAN2
86 if(src_addr->can_ifindex == 2 || src_addr->can_ifindex == 0) {
87 int ret = add_can_callback(&can2, callback, user_data);
88 if(ret) { return ret; }
89 }
90 #endif
91 return 0;
92}
void can_init()
Definition can.c:59
uint8_t can_dlc_to_len(uint8_t dlc)
Definition can.c:39
static int add_can_callback(struct can_periph *canp, can_rx_frame_callback_t callback, void *user_data)
Definition can.c:65
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:78
uint8_t can_len_to_dlc(uint8_t len)
Definition can.c:46
static const uint8_t dlc_to_len[]
Definition can.c:37
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.