canbus.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #ifndef _CANBUS_H_
  2. #define _CANBUS_H_
  3. /*
  4. * Copyright (C) 2012 by Uwe Bonnes(bon@elektron.ikp.physik.tu-darmstadt.de)
  5. *
  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. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. *
  35. */
  36. /*!
  37. * \verbatim
  38. * $Id$
  39. * \endverbatim
  40. */
  41. /*!
  42. * \file dev/canbus.h
  43. * \brief Headers for canbus interface
  44. */
  45. #include <cfg/arch.h>
  46. #include <cfg/can_dev.h>
  47. #include <sys/types.h>
  48. #include <sys/device.h>
  49. #include <dev/irqreg.h>
  50. /*!
  51. * \addtogroup xgCanDev
  52. */
  53. /*@{*/
  54. /*!
  55. * \brief CAN Baud rate constants
  56. */
  57. #define CAN_SPEED_10K 0 ///< 10 kbit/s, max. cable length 5000 m
  58. #define CAN_SPEED_20K 1 ///< 20 kbit/s, max. cable length 2500 m
  59. #define CAN_SPEED_50K 2 ///< 50 kbit/s, max. cable length 1000 m
  60. #define CAN_SPEED_100K 3 ///< 100 kbit/s, max. cable length 600 m
  61. #define CAN_SPEED_125K 4 ///< 125 kbit/s, max. cable length 500 m
  62. #define CAN_SPEED_250K 5 ///< 250 kbit/s, max. cable length 250 m
  63. #define CAN_SPEED_500K 6 ///< 500 kbit/s, max. cable length 100 m
  64. #define CAN_SPEED_800K 7 ///< 800 kbit/s, max. cable length 50 m
  65. #define CAN_SPEED_1M 8 ///< 1 Mbit/s, max. cable length 25 m
  66. #define CAN_SPEED_CUSTOM -1
  67. /*!
  68. * \brief CAN error codes
  69. */
  70. enum CAN_RESULT
  71. {
  72. CAN_SUCCESS = 0, ///< Successful operation
  73. CAN_ERROR = -1, ///< Unspecified error
  74. CAN_TXBUF_FULL = -2, ///< All TX message objects busy
  75. CAN_RXBUF_EMPTY = -3, ///< All RX message objects busy
  76. CAN_ILLEGAL_MOB = -4, ///< Message object index out of range
  77. CAN_INVALID_SPEED = -5, ///< Invalid baud rate parameter
  78. CAN_PASSIVE = -6, ///< Bus is in passive state
  79. CAN_BUS_OFF = -7, ///< Bus is bus-off
  80. CAN_NO_COMPANION = -8, ///< FIFO2 needs FIFO1 active
  81. CAN_IS_COMPANION = -9, ///< FIFO2 can only set Filters
  82. };
  83. /*!
  84. * \brief CAN event counters
  85. */
  86. enum CAN_COUNTERS
  87. {
  88. CAN_RX_FRAMES = 0, /*!< Number of packets received. */
  89. CAN_TX_FRAMES = 1, /*!< Number of packets sent. */
  90. CAN_INTERRUPTS = 2, /*!< Number of interrupts. */
  91. CAN_RX_INTERRUPTS = 3, /*!< Number of RX interrupts. */
  92. CAN_TX_INTERRUPTS = 4, /*!< Number of TX interrupts. */
  93. CAN_SCE_INTERRUPTS = 5, /*!< Number of ERROR interrupts. */
  94. CAN_OVERRUNS = 6, /*!< Number of packet overruns. */
  95. CAN_ERRORS = 7, /*!< Number of frame errors. */
  96. CAN_NO_COUNTERS = 8,
  97. };
  98. /* Enable / disable time triggered communication mode */
  99. #define CAN_TTCM 0x0001
  100. /* Enable / disable automatic bus-off management */
  101. #define CAN_ABOM 0x0002
  102. /* Set the automatic wake-up mode */
  103. #define CAN_AWUM 0x0004
  104. /* Enable / disable no automatic retransmission */
  105. #define CAN_NART 0x0008
  106. /* Enable / disable receive FIFO locked mode */
  107. #define CAN_RFLM 0x0010
  108. /* Enable / disable transmit FIFO priority */
  109. #define CAN_TXFP 0x0020
  110. /*!
  111. * \brief CAN bus structure, defined by device
  112. */
  113. typedef struct _NUTCANBUS NUTCANBUS;
  114. /*!
  115. * \brief CAN bus info structure, defined by device
  116. */
  117. typedef struct _CANBUSINFO CANBUSINFO;
  118. /*!
  119. * \brief CAN BUFFER structure, defined by device
  120. */
  121. typedef struct _CANBUFFER CANBUFFER;
  122. /* Include architecture specific CAN implementation */
  123. #if defined(DEV_CANBUS)
  124. #if defined(MCU_STM32)
  125. #include <arch/cm3/stm/stm32_can.h>
  126. #else
  127. #warning DEV_CANBUS defined, but no device specific implementation givem
  128. #endif
  129. #endif
  130. /*!
  131. * \struct _CANFRAME canbus.h
  132. * \brief CAN frame structure
  133. */
  134. struct _CANFRAME { // todo: Implement flags
  135. uint32_t id; // Identifier
  136. uint8_t byte[8];
  137. uint8_t len; // Length of frame, max = 8
  138. uint8_t ext; // Boolean, extendet frame
  139. uint8_t rtr; // Boolean, remote transmition bit
  140. };
  141. /*!
  142. * \brief CAN frame type
  143. */
  144. typedef struct _CANFRAME CANFRAME;
  145. /*!
  146. * \struct _CANFILTER canbus.h
  147. * \brief CAN message filter structure
  148. */
  149. struct _CANFILTER{ //
  150. uint32_t id; ///< Identifier
  151. uint32_t mask; ///< Mask, use 0xffffffff for exactt match
  152. uint8_t id_ext; ///< Boolean, extended frame
  153. uint8_t id_rtr; ///< Boolean, remote transmition bit
  154. uint8_t mask_ext; ///< Boolean, match id_ext
  155. uint8_t mask_rtr; ///< Boolean, match id_rtr
  156. };
  157. /*!
  158. * \brief CAN message filter type
  159. */
  160. typedef struct _CANFILTER CANFILTER;
  161. #define FILTER_EXPLICIT 0xffffffff
  162. /*!
  163. * \brief _NUTCANBUS
  164. */
  165. extern int NutRegisterCanBus( NUTCANBUS *bus, int entries );
  166. extern int CanAddFilter( NUTCANBUS *bus, CANFILTER *filter );
  167. extern int CanSetBaud( NUTCANBUS *bus, int baud, uint32_t alt_btr);
  168. extern void CANSetRxTimeout(NUTCANBUS *bus, uint32_t timeout);
  169. extern void CANSetTxTimeout(NUTCANBUS *bus, uint32_t timeout);
  170. extern void CanEnableRx( NUTCANBUS *bus);
  171. extern int CanRxAvail( NUTCANBUS *bus);
  172. extern int CanInput(NUTCANBUS *bus, CANFRAME *output);
  173. extern int CanTxFree(NUTCANBUS *can);
  174. extern int CanOutput( NUTCANBUS *bus, CANFRAME *output);
  175. extern int CanGetCounter( NUTCANBUS *bus, enum CAN_COUNTERS index);
  176. #endif