can.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. // coding: utf-8
  2. // ----------------------------------------------------------------------------
  3. /*
  4. * Copyright (c) 2007 Fabian Greif, Roboterclub Aachen e.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. // ----------------------------------------------------------------------------
  29. /**
  30. * \file can.h
  31. * \brief Header-Datei für das allgemeine CAN Interface
  32. */
  33. // ----------------------------------------------------------------------------
  34. #ifndef CAN_H
  35. #define CAN_H
  36. #if defined (__cplusplus)
  37. extern "C" {
  38. #endif
  39. // ----------------------------------------------------------------------------
  40. /**
  41. * \ingroup communication
  42. * \defgroup can_interface Universelles CAN Interface
  43. * \brief allgemeines CAN Interface für AT90CAN32/64/128, MCP2515 und SJA1000
  44. *
  45. * \author Fabian Greif <fabian.greif@rwth-aachen.de>
  46. * \author Roboterclub Aachen e.V. (http://www.roboterclub.rwth-aachen.de)
  47. *
  48. * can_sleep() and can_wakeup() functions by Frédéric Lamorce.
  49. *
  50. * \version $Id: can.h 8086 2009-07-14 14:08:25Z fabian $
  51. */
  52. // ----------------------------------------------------------------------------
  53. #include <avr/pgmspace.h>
  54. #include <stdint.h>
  55. #include <stdbool.h>
  56. #ifndef CAN_CONFIG_LOADED
  57. #ifdef HAS_CAN_CONFIG_H
  58. /* try to load can_config.h */
  59. #include "can_config.h"
  60. #else
  61. /* try to load config.h - compatibility */
  62. #include "config.h"
  63. #endif
  64. #endif
  65. // ----------------------------------------------------------------------------
  66. /** \ingroup can_interface
  67. * \name Bitdefinitionen
  68. */
  69. //@{
  70. #define ONLY_NON_RTR 2
  71. #define ONLY_RTR 3
  72. //@}
  73. /** \ingroup can_interface
  74. * \brief Bitraten fuer den CAN-Bus
  75. */
  76. typedef enum {
  77. BITRATE_10_KBPS = 0, // ungetestet
  78. BITRATE_20_KBPS = 1, // ungetestet
  79. BITRATE_50_KBPS = 2, // ungetestet
  80. BITRATE_100_KBPS = 3, // ungetestet
  81. BITRATE_125_KBPS = 4,
  82. BITRATE_250_KBPS = 5, // ungetestet
  83. BITRATE_500_KBPS = 6, // ungetestet
  84. BITRATE_1_MBPS = 7, // ungetestet
  85. } can_bitrate_t;
  86. /**
  87. * \ingroup can_interface
  88. * \brief Symbol um auf alle Filter zuzugreifen
  89. */
  90. #define CAN_ALL_FILTER 0xff
  91. /**
  92. * \ingroup can_interface
  93. * \brief Unterstuetzung fuer Extended-IDs aktivieren
  94. */
  95. #ifndef SUPPORT_EXTENDED_CANID
  96. #define SUPPORT_EXTENDED_CANID 1
  97. #endif
  98. /**
  99. * \ingroup can_interface
  100. * \brief Unterstützung für Zeitstempel aktivieren
  101. * \warning Wird nur vom AT90CANxxx unterstützt
  102. */
  103. #ifndef SUPPORT_TIMESTAMPS
  104. #define SUPPORT_TIMESTAMPS 0
  105. #endif
  106. /**
  107. * \ingroup can_interface
  108. * \name Bits des Filters fuer den MCP2515 umformatieren
  109. *
  110. * \code
  111. * prog_uint8_t can_filter[] =
  112. * {
  113. * MCP2515_FILTER_EXTENDED(0), // Filter 0
  114. * MCP2515_FILTER_EXTENDED(0), // Filter 1
  115. *
  116. * MCP2515_FILTER_EXTENDED(0), // Filter 2
  117. * MCP2515_FILTER_EXTENDED(0), // Filter 3
  118. * MCP2515_FILTER_EXTENDED(0), // Filter 4
  119. * MCP2515_FILTER_EXTENDED(0), // Filter 5
  120. *
  121. * MCP2515_FILTER_EXTENDED(0), // Maske 0
  122. * MCP2515_FILTER_EXTENDED(0), // Maske 1
  123. * };
  124. * \endcode
  125. *
  126. * \see can_static_filter()
  127. *
  128. * \~german
  129. * \warning Dieses Makro sollte nur Werte verwendet die schon zur
  130. * Compile-Zeit bekannt sind. Der Code sollte ansonsten zwar trotzdem
  131. * funktionieren, wird danner aber sehr groß.
  132. *
  133. * \~english
  134. * \warning Do not use this Makro for Variables, only for static values
  135. * known at compile-time.
  136. */
  137. //@{
  138. #if defined(__DOXYGEN__)
  139. #define MCP2515_FILTER_EXTENDED(id)
  140. #define MCP2515_FILTER(id)
  141. #else
  142. #if SUPPORT_EXTENDED_CANID
  143. #define MCP2515_FILTER_EXTENDED(id) \
  144. (uint8_t) ((uint32_t) (id) >> 21), \
  145. (uint8_t)((((uint32_t) (id) >> 13) & 0xe0) | (1<<3) | \
  146. (((uint32_t) (id) >> 16) & 0x3)), \
  147. (uint8_t) ((uint32_t) (id) >> 8), \
  148. (uint8_t) ((uint32_t) (id))
  149. #endif
  150. #define MCP2515_FILTER(id) \
  151. (uint8_t)((uint32_t) id >> 3), \
  152. (uint8_t)((uint32_t) id << 5), \
  153. 0, \
  154. 0
  155. #endif
  156. //@}
  157. // ----------------------------------------------------------------------------
  158. /**
  159. * \ingroup can_interface
  160. * \brief Datenstruktur zum Aufnehmen von CAN Nachrichten
  161. */
  162. typedef struct
  163. {
  164. #if SUPPORT_EXTENDED_CANID
  165. uint32_t id; //!< ID der Nachricht (11 oder 29 Bit)
  166. struct {
  167. int rtr : 1; //!< Remote-Transmit-Request-Frame?
  168. int extended : 1; //!< extended ID?
  169. } flags;
  170. #else
  171. uint16_t id; //!< ID der Nachricht (11 Bit)
  172. struct {
  173. int rtr : 1; //!< Remote-Transmit-Request-Frame?
  174. } flags;
  175. #endif
  176. uint8_t length; //!< Anzahl der Datenbytes
  177. uint8_t data[8]; //!< Die Daten der CAN Nachricht
  178. #if SUPPORT_TIMESTAMPS
  179. uint16_t timestamp;
  180. #endif
  181. } can_t;
  182. // ----------------------------------------------------------------------------
  183. /**
  184. * \ingroup can_interface
  185. * \brief Datenstruktur zur Aufnahme von CAN-Filtern
  186. *
  187. * \code
  188. * rtr | Funtion
  189. * -----|------
  190. * 00 | alle Nachrichten unabhaengig vom RTR-Bit
  191. * 01 | ungültig
  192. * 10 | empfange nur nicht RTR-Nachrichten
  193. * 11 | empfange nur Nachrichten mit gesetzem RTR-Bit
  194. * \endcode
  195. *
  196. * \b ACHTUNG:
  197. * Funktioniert nur mit dem AT90CAN, beim MCP2515 wird der Parameter ignoriert.
  198. *
  199. * \code
  200. * ext | Funtion
  201. * -----|------
  202. * 00 | alle Nachrichten
  203. * 01 | ungueltig
  204. * 10 | empfange nur Standard-Nachrichten
  205. * 11 | empfange nur Extended-Nachrichten
  206. * \endcode
  207. *
  208. * \warning Filter sind beim SJA1000 nur begrenzt nutzbar, man sollte ihn nur
  209. * in Systemen mit entweder Standard- oder Extended-Frames einsetzten,
  210. * aber nicht beidem zusammen.
  211. */
  212. typedef struct
  213. {
  214. #if SUPPORT_EXTENDED_CANID
  215. uint32_t id; //!< ID der Nachricht (11 oder 29 Bit)
  216. uint32_t mask; //!< Maske
  217. struct {
  218. uint8_t rtr : 2; //!< Remote Request Frame
  219. uint8_t extended : 2; //!< extended ID
  220. } flags;
  221. #else
  222. uint16_t id; //!< ID der Nachricht 11 Bits
  223. uint16_t mask; //!< Maske
  224. struct {
  225. uint8_t rtr : 2; //!< Remote Request Frame
  226. } flags;
  227. #endif
  228. } can_filter_t;
  229. // ----------------------------------------------------------------------------
  230. /**
  231. * \ingroup can_interface
  232. * \brief Inhalt der Fehler-Register
  233. */
  234. typedef struct {
  235. uint8_t rx; //!< Empfangs-Register
  236. uint8_t tx; //!< Sende-Register
  237. } can_error_register_t;
  238. // ----------------------------------------------------------------------------
  239. /**
  240. * \ingroup can_interface
  241. * \brief Modus des CAN Interfaces
  242. */
  243. typedef enum {
  244. LISTEN_ONLY_MODE, //!< der CAN Contoller empfängt nur und verhält sich völlig passiv
  245. LOOPBACK_MODE, //!< alle Nachrichten direkt auf die Empfangsregister umleiten ohne sie zu senden
  246. NORMAL_MODE, //!< normaler Modus, CAN Controller ist aktiv
  247. SLEEP_MODE // sleep mode
  248. } can_mode_t;
  249. // ----------------------------------------------------------------------------
  250. /**
  251. * \ingroup can_interface
  252. * \brief Initialisierung des CAN Interfaces
  253. *
  254. * \param bitrate Gewuenschte Geschwindigkeit des CAN Interfaces
  255. *
  256. * \return false falls das CAN Interface nicht initialisiert werden konnte,
  257. * true ansonsten.
  258. */
  259. extern bool
  260. can_init(can_bitrate_t bitrate);
  261. // ----------------------------------------------------------------------------
  262. /**
  263. * \ingroup can_interface
  264. *
  265. * \~english
  266. * \brief Put CAN interface to sleep and wake up
  267. *
  268. * MCP2515 active : 5mA
  269. * MCP2515 sleep : 1µA
  270. *
  271. * MCP2551 active : 10mA+
  272. * MCP2551 sleep : 400µA
  273. *
  274. * \code
  275. * // before we are going to sleep, enable the interrupt that will wake us up
  276. // attach interrupt 1 to the routine
  277. EICRA = 0; // int on low level
  278. // Enable the interrupt1
  279. EIMSK = _BV(INT1);
  280. // put the MCP2515 to sleep
  281. can_sleep();
  282. // enable atmega sleep mode
  283. cli();
  284. sleep_enable();
  285. // turn off BOD
  286. sleep_bod_disable();
  287. // and we go to sleep
  288. sei();
  289. sleep_cpu();
  290. // here int1 has been executed and we are woken up
  291. sleep_disable();
  292. // disable int1
  293. EIMSK = 0;
  294. // re-enable 2515 and 2551
  295. can_wake();
  296. * \endcode
  297. *
  298. * \warning Only implemented for the MCP2515
  299. */
  300. extern void
  301. can_sleep(void);
  302. extern void
  303. can_wakeup(void);
  304. // ----------------------------------------------------------------------------
  305. /**
  306. * \ingroup can_interface
  307. * \brief Setzen eines Filters
  308. *
  309. * Für einen MCP2515 sollte die Funktion can_static_filter() bevorzugt werden.
  310. *
  311. * \param number Position des Filters
  312. * \param filter zu setzender Filter
  313. *
  314. * \return false falls ein Fehler auftrat, true ansonsten
  315. */
  316. extern bool
  317. can_set_filter(uint8_t number, const can_filter_t *filter);
  318. // ----------------------------------------------------------------------------
  319. /**
  320. * \ingroup can_interface
  321. * \brief Filter deaktivieren
  322. *
  323. * \param number Nummer des Filters der deaktiviert werden soll,
  324. * 0xff deaktiviert alle Filter.
  325. * \return false falls ein Fehler auftrat, true ansonsten
  326. *
  327. * \warning Wird nur vom AT90CAN32/64/128 unterstuetzt.
  328. */
  329. extern bool
  330. can_disable_filter(uint8_t number);
  331. // ----------------------------------------------------------------------------
  332. /**
  333. * \ingroup can_interface
  334. * \brief Setzt die Werte für alle Filter
  335. *
  336. * \code
  337. * // Filter und Masken-Tabelle anlegen
  338. * prog_char can_filter[] = {
  339. * MCP2515_FILTER_EXTENDED(0), // Filter 0
  340. * MCP2515_FILTER_EXTENDED(0), // Filter 1
  341. *
  342. * MCP2515_FILTER_EXTENDED(0), // Filter 2
  343. * MCP2515_FILTER_EXTENDED(0), // Filter 3
  344. * MCP2515_FILTER_EXTENDED(0), // Filter 4
  345. * MCP2515_FILTER_EXTENDED(0), // Filter 5
  346. *
  347. * MCP2515_FILTER_EXTENDED(0), // Maske 0
  348. * MCP2515_FILTER_EXTENDED(0), // Maske 1
  349. * };
  350. *
  351. * ...
  352. *
  353. * // Filter und Masken-Tabelle laden
  354. * can_static_filter(can_filter);
  355. * \endcode
  356. *
  357. * \param *filter_array Array im Flash des AVRs mit den Initialisierungs-
  358. * werten für die Filter des MCP2515
  359. *
  360. * \see MCP2515_FILTER_EXTENDED()
  361. * \see MCP2515_FILTER()
  362. * \warning Wird nur vom MCP2515 unterstuetzt.
  363. */
  364. extern void
  365. can_static_filter(const uint8_t *filter_array);
  366. // ----------------------------------------------------------------------------
  367. /**
  368. * \ingroup can_interface
  369. *
  370. * \~german
  371. * \brief Filterdaten auslesen
  372. *
  373. * \param number Nummer des Filters dessen Daten man haben moechte
  374. * \param *filter Pointer in den die Filterstruktur geschrieben wird
  375. *
  376. * \return \b 0 falls ein Fehler auftrat, \
  377. * \b 1 falls der Filter korrekt gelesen werden konnte, \
  378. * \b 2 falls der Filter im Moment nicht verwendet wird (nur AT90CAN), \
  379. * \b 0xff falls gerade keine Aussage moeglich ist (nur AT90CAN).
  380. *
  381. * \warning Da der SJA1000 nicht feststellen kann ob der ausgelesene Filter
  382. * nun zwei 11-Bit Filter oder ein 29-Bit Filter ist werden nicht
  383. * die Filter sondern die Registerinhalte direkt zurück gegeben.
  384. * Der Programmierer muss dann selbst entscheiden was er mit den
  385. * Werten macht.
  386. *
  387. * \~english
  388. * \warning SJA1000 doesn't return the filter and id directly but the content
  389. * of the corresponding registers because it is not possible to
  390. * check the type of the filter.
  391. */
  392. extern uint8_t
  393. can_get_filter(uint8_t number, can_filter_t *filter);
  394. // ----------------------------------------------------------------------------
  395. /**
  396. * \ingroup can_interface
  397. * \brief Ueberpruefen ob neue CAN Nachrichten vorhanden sind
  398. *
  399. * \return true falls neue Nachrichten verfuegbar sind, false ansonsten.
  400. */
  401. extern bool
  402. can_check_message(void);
  403. // ----------------------------------------------------------------------------
  404. /**
  405. * \ingroup can_interface
  406. * \brief Ueberprueft ob ein Puffer zum Versenden einer Nachricht frei ist.
  407. *
  408. * \return true falls ein Sende-Puffer frei ist, false ansonsten.
  409. */
  410. extern bool
  411. can_check_free_buffer(void);
  412. // ----------------------------------------------------------------------------
  413. /**
  414. * \ingroup can_interface
  415. * \brief Verschickt eine Nachricht über den CAN Bus
  416. *
  417. * \param msg Nachricht die verschickt werden soll
  418. * \return FALSE falls die Nachricht nicht verschickt werden konnte, \n
  419. * ansonsten der Code des Puffes in den die Nachricht gespeichert wurde
  420. */
  421. extern uint8_t
  422. can_send_message(const can_t *msg);
  423. // ----------------------------------------------------------------------------
  424. /**
  425. * \ingroup can_interface
  426. * \brief Liest eine Nachricht aus den Empfangspuffern des CAN Controllers
  427. *
  428. * \param msg Pointer auf die Nachricht die gelesen werden soll.
  429. * \return FALSE falls die Nachricht nicht ausgelesen konnte,
  430. * ansonsten Filtercode welcher die Nachricht akzeptiert hat.
  431. */
  432. extern uint8_t
  433. can_get_message(can_t *msg);
  434. // ----------------------------------------------------------------------------
  435. /**
  436. * \ingroup can_interface
  437. *
  438. * \~german
  439. * \brief Liest den Inhalt der Fehler-Register
  440. *
  441. * \~english
  442. * \brief Reads the Contents of the CAN Error Counter
  443. */
  444. extern can_error_register_t
  445. can_read_error_register(void);
  446. // ----------------------------------------------------------------------------
  447. /**
  448. * \ingroup can_interface
  449. *
  450. * \~german
  451. * \brief Überprüft ob der CAN Controller im Bus-Off-Status
  452. *
  453. * \return true wenn der Bus-Off-Status aktiv ist, false ansonsten
  454. *
  455. * \warning aktuell nur auf dem SJA1000
  456. */
  457. extern bool
  458. can_check_bus_off(void);
  459. // ----------------------------------------------------------------------------
  460. /**
  461. * \ingroup can_interface
  462. *
  463. * \~german
  464. * \brief Setzt einen Bus-Off Status zurück und schaltet den CAN Controller
  465. * wieder aktiv
  466. *
  467. * \warning aktuell nur auf dem SJA1000
  468. */
  469. extern void
  470. can_reset_bus_off(void);
  471. // ----------------------------------------------------------------------------
  472. /**
  473. * \ingroup can_interface
  474. * \brief Setzt den Operations-Modus
  475. *
  476. * \param mode Gewünschter Modus des CAN Controllers
  477. */
  478. extern void
  479. can_set_mode(can_mode_t mode);
  480. #if defined (__cplusplus)
  481. }
  482. #endif
  483. #endif // CAN_H