flexcan.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2012, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __CAN_H__
  31. #define __CAN_H__
  32. #include "sdk.h"
  33. #define CAN_TIMING_MASK 0x00C0FFF8 // to zero out presdiv, pseg1, pseg2, prop_seg
  34. #define CAN_NUMBER_OF_BUFFERS 64 // Define the number of MB
  35. #define CAN_LAST_MB 63
  36. #define CAN_TIMING_PARAMETERS 0x0892
  37. #define CAN_MB_OFFSET 0x80
  38. #define CAN_IRQS(x) ( (x) == HW_FLEXCAN1 ? IMX_INT_FLEXCAN1 : (x) == HW_FLEXCAN2 ? IMX_INT_FLEXCAN2 : 0xFFFFFFFF)
  39. //! @brief CAN message buffer structure
  40. struct can_mb {
  41. volatile uint32_t cs; //! Code and Status
  42. volatile uint32_t id; //! ID
  43. volatile uint32_t data0;
  44. volatile uint32_t data1;
  45. };
  46. //! @brief CAN Message Buffers 0x80 - 0x170
  47. struct can_message_buffers {
  48. volatile struct can_mb MB[64];
  49. };
  50. //! @brief CAN timing related structures
  51. struct time_segment {
  52. uint32_t propseg;
  53. uint32_t pseg1;
  54. uint32_t pseg2;
  55. };
  56. //! @brief Baudrates of CAN bus(kps)
  57. enum can_bitrate {
  58. MBPS_1,
  59. KBPS_800,
  60. KBPS_500,
  61. KBPS_250,
  62. KBPS_125,
  63. KBPS_62, //62.5kps
  64. KBPS_20,
  65. KBPS_10
  66. };
  67. ////////////////////////////////////////////////////////////////////////////////
  68. // API
  69. ////////////////////////////////////////////////////////////////////////////////
  70. /* CAN driver list of functions */
  71. /*!
  72. * @brief Reset FlexCAN controller
  73. *
  74. * @param instance the FlexCAN instance number.
  75. */
  76. void can_sw_reset(uint32_t instance);
  77. /*!
  78. * @brief Initialize CAN controller
  79. *
  80. * @param instance the FlexCAN instance number.
  81. * @param max_mb Max mailbox will be used
  82. */
  83. void can_init(uint32_t instance, uint32_t max_mb);
  84. /*!
  85. * @brief Set message box fields
  86. *
  87. * @param instance the FlexCAN instance number.
  88. * @param mbID Index of the message box
  89. * @param cs control/statuc code
  90. * @param id ID of the message to be transfer
  91. * @param data0 first 4 bytes of the CAN message
  92. * @param data1 last 4 bytes of the CAN message
  93. */
  94. void set_can_mb(uint32_t instance, uint32_t mbID, uint32_t cs, uint32_t id, uint32_t data0, uint32_t data1);
  95. /*!
  96. * @brief Dump the message box
  97. *
  98. * @param instance the FlexCAN instance number.
  99. * @param mbID Index of the message box
  100. */
  101. void print_can_mb(uint32_t instance, uint32_t mbID);
  102. /*!
  103. * @brief Enable the interrupt of the FlexCAN module
  104. *
  105. * @param instance the FlexCAN instance number.
  106. * @param mbID Index of the message box
  107. */
  108. void can_enable_mb_interrupt(uint32_t instance, uint32_t mbID);
  109. /*!
  110. * @brief Disable the interrupt of the FlexCAN module
  111. *
  112. * @param instance the FlexCAN instance number.
  113. * @param mbID Index of the message box
  114. */
  115. void can_disable_mb_interrupt(uint32_t instance, uint32_t mbID);
  116. /*!
  117. * @brief Setup the interrupt of the FlexCAN module
  118. *
  119. * It enables or disables the related HW module interrupt, and attached the related sub-routine
  120. * into the vector table.
  121. *
  122. * @param instance the FlexCAN instance number.
  123. * @param irq_subroutine the FlexCAN interrupt interrupt routine.
  124. * @param enableIt True to enable the interrupt, false to disable.
  125. */
  126. void can_setup_interrupt(uint32_t instance, void (*irq_subroutine)(void), bool enableIt);
  127. /*!
  128. * @brief Un-freeze the FlexCAN module
  129. *
  130. * @param instance the FlexCAN instance number.
  131. */
  132. void can_exit_freeze(uint32_t instance);
  133. /*!
  134. * @brief Freeze the FlexCAN module
  135. *
  136. * @param instance the FlexCAN instance number.
  137. */
  138. void can_freeze(uint32_t instance);
  139. /*!
  140. * @brief Set bit rate
  141. *
  142. * CAN bit rate = sclk (aka Freq-TQ) / number-of-time-quanta
  143. *
  144. * @param instance the FlexCAN instance number.
  145. * @param bitrate CAN bit rate.
  146. */
  147. void can_update_bitrate(uint32_t instance, enum can_bitrate bitrate);
  148. /*!
  149. * @brief Get the interrupt flags(iflag1 | (iflag2<<32))
  150. *
  151. * @param instance the FlexCAN instance number.
  152. *
  153. * @return interrupt flags(ie, iflag1 | (iflag2<<32))
  154. */
  155. uint64_t can_mb_int_flag(uint32_t instance);
  156. /*!
  157. * @brief Clear the interrupt flag of the message box
  158. *
  159. * @param instance the FlexCAN instance number.
  160. * @param mbID Index of the message box
  161. */
  162. void can_mb_int_ack(uint32_t instance, uint32_t mbID);
  163. //! @name Board support functions
  164. //!
  165. //! These functions are called by the flexcan driver and must be defined in the
  166. //! board support library or the application.
  167. //@{
  168. /*!
  169. * @brief Configure IOMUX and GPIOs to enable CAN.
  170. *
  171. * @param module_instance The CAN module instance number to configure for, starting at 1.
  172. */
  173. void hw_can_iomux_config(uint32_t module_instance);
  174. //@}
  175. #endif //__CAN_H__