at90can_send_message.c
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 /*
3  * Copyright (c) 2007 Fabian Greif, Roboterclub Aachen e.V.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $Id: at90can_send_message.c 6837 2008-11-16 19:05:15Z fabian $
28  */
29 // ----------------------------------------------------------------------------
30 
31 #include "at90can_private.h"
32 #ifdef SUPPORT_FOR_AT90CAN__
33 
34 #include <string.h>
35 
36 // ----------------------------------------------------------------------------
42 void at90can_copy_message_to_mob(const can_t *msg)
43 {
44  // write DLC (Data Length Code)
45  CANCDMOB = msg->length;
46 
47  #if SUPPORT_EXTENDED_CANID
48 
49  if (msg->flags.extended) {
50  // extended CAN ID
51  CANCDMOB |= (1 << IDE);
52 
53  CANIDT4 = (uint8_t) msg->id << 3;
54 
55  uint32_t temp = msg->id << 3;
56  uint8_t *ptr = (uint8_t *) &temp;
57 
58  CANIDT3 = *(ptr + 1);
59  CANIDT2 = *(ptr + 2);
60  CANIDT1 = *(ptr + 3);
61  }
62  else {
63  // standard CAN ID
64  CANIDT4 = 0;
65  CANIDT3 = 0;
66  CANIDT2 = (uint8_t) msg->id << 5;
67  CANIDT1 = (uint16_t) msg->id >> 3;
68  }
69 
70  #else
71 
72  CANIDT4 = 0;
73  CANIDT3 = 0;
74  CANIDT2 = (uint8_t) msg->id << 5;
75  CANIDT1 = (uint16_t) msg->id >> 3;
76 
77  #endif
78 
79  if (msg->flags.rtr) {
80  CANIDT4 |= (1<<RTRTAG);
81  }
82  else {
83  const uint8_t *p = msg->data;
84  for (uint8_t i = 0;i < msg->length;i++) {
85  CANMSG = *p++;
86  }
87  }
88 }
89 
90 // ----------------------------------------------------------------------------
91 uint8_t at90can_send_message(const can_t *msg)
92 {
93  // check if there is any free MOb
94  uint8_t mob = _find_free_mob();
95  if (mob >= 15)
96  return 0;
97 
98  // load corresponding MOb page ...
99  CANPAGE = (mob << 4);
100 
101  // clear flags
102  CANSTMOB &= 0;
103 
104  // ... and copy the data
105  at90can_copy_message_to_mob( msg );
106 
107  // enable interrupt
108  _enable_mob_interrupt(mob);
109 
111  #if CAN_TX_BUFFER_SIZE == 0
112  _free_buffer--;
113  #elif CAN_FORCE_TX_ORDER
114  _transmission_in_progress = 1;
115  #endif
117 
118  // enable transmission
119  CANCDMOB |= (1<<CONMOB0);
120 
121  return (mob + 1);
122 }
123 
124 #endif // SUPPORT_FOR_AT90CAN__
ENTER_CRITICAL_SECTION
#define ENTER_CRITICAL_SECTION
Definition: utils.h:126
can_t::length
uint8_t length
Anzahl der Datenbytes.
Definition: can.h:192
uavcan::uint32_t
std::uint32_t uint32_t
Definition: std.hpp:26
can_t::rtr
int rtr
Remote-Transmit-Request-Frame?
Definition: can.h:182
can_t
Datenstruktur zum Aufnehmen von CAN Nachrichten.
Definition: can.h:177
can_t::data
uint8_t data[8]
Die Daten der CAN Nachricht.
Definition: can.h:193
uavcan::uint16_t
std::uint16_t uint16_t
Definition: std.hpp:25
IDE
#define IDE
Definition: mcp2515_defs.h:296
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
at90can_private.h
can_t::extended
int extended
extended ID?
Definition: can.h:183
can_t::flags
struct can_t::@136 flags
LEAVE_CRITICAL_SECTION
#define LEAVE_CRITICAL_SECTION
Definition: utils.h:127
can_t::id
uint32_t id
ID der Nachricht (11 oder 29 Bit)
Definition: can.h:180


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02