can_dev.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #ifndef _CAN_DEV_H_
  2. #define _CAN_DEV_H_
  3. /*
  4. * Copyright (C) 2004 by Ole Reinhardt<ole.reinhardt@kernelconcepts.de>
  5. * Kernel concepts (http://www.kernelconcepts.de) 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. *
  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: can_dev.h 4477 2012-08-20 17:50:01Z haraldkipp $
  39. * \endverbatim
  40. */
  41. /*!
  42. * \file dev/can_dev.h
  43. * \brief Headers for can driver interface
  44. */
  45. #include <sys/types.h>
  46. #include <sys/device.h>
  47. /*!
  48. * \addtogroup xgCanDev
  49. */
  50. /*@{*/
  51. #define CAN_IF_2A 0x01
  52. #define CAN_IF_2B 0x02
  53. /*
  54. * CAN Baud rate constants
  55. */
  56. #define CAN_SPEED_10K 0 ///< 10 kbit/s, max. cable length 5000 m
  57. #define CAN_SPEED_20K 1 ///< 20 kbit/s, max. cable length 2500 m
  58. #define CAN_SPEED_50K 2 ///< 50 kbit/s, max. cable length 1000 m
  59. #define CAN_SPEED_100K 3 ///< 100 kbit/s, max. cable length 600 m
  60. #define CAN_SPEED_125K 4 ///< 125 kbit/s, max. cable length 500 m
  61. #define CAN_SPEED_250K 5 ///< 250 kbit/s, max. cable length 250 m
  62. #define CAN_SPEED_500K 6 ///< 500 kbit/s, max. cable length 100 m
  63. #define CAN_SPEED_800K 7 ///< 800 kbit/s, max. cable length 50 m
  64. #define CAN_SPEED_1M 8 ///< 1 Mbit/s, max. cable length 25 m
  65. #define CAN_SPEED_CUSTOM 255
  66. /*!
  67. * \struct _CANFRAME can_dev.h
  68. * \brief CAN frame structure
  69. */
  70. struct _CANFRAME { // todo: Implement flags
  71. uint32_t id; // Identifier
  72. uint8_t len; // Length of frame, max = 8
  73. uint8_t byte[8];
  74. uint8_t ext; // Boolean, extendet frame
  75. uint8_t rtr; // Boolean, remote transmition bit
  76. };
  77. /*!
  78. * \brief CAN frame type
  79. */
  80. typedef struct _CANFRAME CANFRAME;
  81. /*!
  82. * \struct _CANINFO can_dev.h
  83. * \brief CAN controller information structure.
  84. */
  85. struct _CANINFO {
  86. HANDLE volatile can_rx_rdy; /*!< Receiver event queue. */
  87. HANDLE volatile can_tx_rdy; /*!< Transmitter event queue. */
  88. uint32_t can_rx_frames; /*!< Number of packets received. */
  89. uint32_t can_tx_frames; /*!< Number of packets sent. */
  90. uint32_t can_interrupts; /*!< Number of interrupts. */
  91. uint32_t can_overruns; /*!< Number of packet overruns. */
  92. uint32_t can_errors; /*!< Number of frame errors. */
  93. };
  94. /*!
  95. * \brief CAN controller information type.
  96. */
  97. typedef struct _CANINFO CANINFO;
  98. /*!
  99. * \struct ifcan can_dev.h can/can_dev.h
  100. * \brief CAN interface structure.
  101. *
  102. * Contains information about the CAN interface.
  103. */
  104. struct ifcan {
  105. uint8_t can_type; /*!< \brief Interface type. Either CAN_IF_2A or CAN_IF_2B. */
  106. uint32_t can_baudrate; /*!< \brief Baudrate of device */
  107. uint8_t can_acc_mask[4]; /*!< \brief Acceptance mask */
  108. uint8_t can_acc_code[4]; /*!< \brief Acceptance code */
  109. uint32_t can_rtimeout; /*!< \brief Timout for receiving */
  110. uint8_t (*can_rxavail) (NUTDEVICE *); /*!< \brief Receive buffer data available? */
  111. uint8_t (*can_txfree) (NUTDEVICE *); /*!< \brief Transmit buffer free? */
  112. uint8_t (*can_recv) (NUTDEVICE *, CANFRAME *); /*!< \brief Receive routine. */
  113. void (*can_send) (NUTDEVICE *, CANFRAME *); /*!< \brief Send routine. */
  114. void (*can_set_ac) (NUTDEVICE *, uint8_t*); /*!< \brief Set accaptance code */
  115. void (*can_set_am) (NUTDEVICE *, uint8_t*); /*!< \brief Set accaptance mask */
  116. uint8_t (*can_set_baud) (NUTDEVICE *, uint32_t); /*!< \brief Set speed */
  117. };
  118. /*!
  119. * \brief Canbus interface type.
  120. */
  121. typedef struct ifcan IFCAN;
  122. uint8_t CAN_SetSpeed(NUTDEVICE *dev, uint32_t baudrate);
  123. void CAN_SetFilter(NUTDEVICE *dev, uint8_t *ac, uint8_t *am);
  124. void CAN_TxFrame(NUTDEVICE *dev, CANFRAME *frame);
  125. uint8_t CAN_TryTxFrame(NUTDEVICE *dev, CANFRAME *frame);
  126. uint8_t CAN_TxFree(NUTDEVICE *dev);
  127. uint8_t CAN_RxFrame(NUTDEVICE *dev, CANFRAME *frame);
  128. uint8_t CAN_TryRxFrame(NUTDEVICE *dev, CANFRAME *frame);
  129. uint8_t CAN_RxAvail(NUTDEVICE *dev);
  130. void CAN_SetRxTimeout(NUTDEVICE *dev, uint32_t timeout);
  131. /*@}*/
  132. #endif