fcan.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fcan.h
  15. * Date: 2021-04-27 15:08:44
  16. * LastEditTime: 2022-02-18 08:29:25
  17. * Description:  This files is for
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. */
  23. #ifndef FT_CAN_H
  24. #define FT_CAN_H
  25. #include "ftypes.h"
  26. #include "ferror_code.h"
  27. #include "fparameters.h"
  28. #include "fkernel.h"
  29. #include "fcan_hw.h"
  30. typedef enum
  31. {
  32. FCAN_INTR_EVENT_SEND = 0, /* Handler type for frame sending interrupt */
  33. FCAN_INTR_EVENT_RECV = 1, /* Handler type for frame reception interrupt */
  34. FCAN_INTR_EVENT_ERROR, /* Handler type for error interrupt */
  35. FCAN_INTR_EVENT_NUM
  36. } FCanIntrEventType;
  37. #define FCAN_SUCCESS FT_SUCCESS /* SUCCESS */
  38. #define FCAN_NOT_READY FT_MAKE_ERRCODE(ErrModBsp, ErrBspCan, 1)
  39. #define FCAN_FAILURE FT_MAKE_ERRCODE(ErrModBsp, ErrBspCan, 2) /* failed */
  40. #define FCAN_INVAL_PARAM FT_MAKE_ERRCODE(ErrModBsp, ErrBspCan, 3) /* invalid parameters */
  41. #if defined(CONFIG_FCAN_USE_CANFD)
  42. #define FCAN_DATA_LENGTH 64U
  43. #else
  44. #define FCAN_DATA_LENGTH 8U
  45. #endif
  46. /* CAN payload length and DLC definitions according to ISO 11898-1 */
  47. #define CAN_MAX_DLC 8
  48. #define CAN_MAX_DLEN 8
  49. #define CAN_MAX_CTL 3
  50. #define CAN_SFF_ID_BITS 11
  51. #define CAN_EFF_ID_BITS 29
  52. /* special address description flags for the CAN_ID */
  53. #define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
  54. #define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
  55. #define CAN_ERR_FLAG 0x20000000U /* error message frame */
  56. /* valid bits in CAN ID for frame formats */
  57. #define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */
  58. #define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
  59. #define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */
  60. /* Frame type */
  61. #define STANDARD_FRAME 0 /* standard frame */
  62. #define EXTEND_FRAME 1 /* extended frame */
  63. /* Bit timing calculate */
  64. #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
  65. #define CAN_CALC_SYNC_SEG 1
  66. /* can segment type */
  67. typedef enum
  68. {
  69. FCAN_ARB_SEGMENT = 0, /* Arbitration segment */
  70. FCAN_DATA_SEGMENT = 1, /* Data segment */
  71. FCAN_SEGMENT_TYPE_NUM
  72. } FCanSegmentType;
  73. /* Can error status bit mask */
  74. #define FCAN_BUS_ERROR_MASK 1
  75. #define FCAN_PASSIVE_ERROR_MASK 2
  76. #define FCAN_PASSIVE_WARNING_MASK 4
  77. #define FCAN_FIFO_RX_OVERFLOW_MASK 8
  78. /* Can frame select */
  79. #define FCAN_STANDARD_FRAME 0
  80. #define FCAN_EXTENDARD_FRAME 1
  81. /* can baudrate */
  82. #define FCAN_BAUDRATE_10K 10000
  83. #define FCAN_BAUDRATE_50K 50000
  84. #define FCAN_BAUDRATE_100K 100000
  85. #define FCAN_BAUDRATE_200K 200000
  86. #define FCAN_BAUDRATE_250K 250000
  87. #define FCAN_BAUDRATE_500K 500000
  88. #define FCAN_BAUDRATE_1000K 1000000
  89. #define FCAN_BAUDRATE_2000K 2000000
  90. #define FCAN_BAUDRATE_3000K 3000000
  91. #define FCAN_BAUDRATE_4000K 4000000
  92. #define FCAN_BAUDRATE_5000K 5000000
  93. /*
  94. * defined bits for FCanFrame.flags
  95. *
  96. * The use of struct FCanFrame implies the Extended Data Length (EDL) bit to
  97. * be set in the CAN frame bitstream on the wire. The EDL bit switch turns
  98. * the CAN controllers bitstream processor into the CAN FD mode which creates
  99. * two new options within the CAN FD frame specification:
  100. *
  101. * Bit Rate Switch - to indicate a second baudrate is/was used for the payload
  102. * Error State Indicator - represents the error state of the transmitting node
  103. *
  104. * As the CANFD_ESI bit is internally generated by the transmitting CAN
  105. * controller only the CANFD_BRS bit is relevant for real CAN controllers when
  106. * building a CAN FD frame for transmission. Setting the CANFD_ESI bit can make
  107. * sense for virtual CAN interfaces to test applications with echoed frames.
  108. */
  109. #define CANFD_BRS 0x02 /* bit rate switch (second baudrate for payload data) */
  110. #define CANFD_ESI 0x04 /* error state indicator of the transmitting node */
  111. typedef void (*FCanIntrEventHandler)(void *param);
  112. typedef struct
  113. {
  114. FCanIntrEventType type;
  115. FCanIntrEventHandler handler;
  116. void *param;
  117. } FCanIntrEventConfig;
  118. typedef struct
  119. {
  120. u32 filter_index;/* filter register index*/
  121. u32 id; /* id bit to receive */
  122. u32 mask;/* id mask bit to receive */
  123. u32 type;/* frame type, standard or extended*/
  124. } FCanIdMaskConfig;
  125. typedef struct
  126. {
  127. u32 canid;/* can frame id */
  128. u8 candlc;/* can frame length */
  129. u8 flags; /* additional flags for CAN FD */
  130. u8 data[FCAN_DATA_LENGTH] __attribute__((aligned(8)));
  131. } FCanFrame;
  132. typedef struct
  133. {
  134. u32 instance_id; /* Id of device */
  135. uintptr base_address; /* Can base Address */
  136. u32 irq_num; /* interrupt number */
  137. u32 irq_prority;/* interrupt priority*/
  138. } FCanConfig;
  139. typedef struct
  140. {
  141. FCanSegmentType segment;
  142. boolean auto_calc; /* if auto calculate baudrate parameters */
  143. u32 baudrate; /* baudrate */
  144. u32 sample_point; /* sample point */
  145. u32 prop_seg; /* Propagation segment in TQs */
  146. u32 phase_seg1; /* Phase buffer segment 1 in TQs */
  147. u32 phase_seg2; /* Phase buffer segment 2 in TQs */
  148. u32 sjw; /* Synchronisation jump width in TQs */
  149. u32 brp; /* Baudrate prescaler */
  150. } FCanBaudrateConfig;
  151. typedef struct
  152. {
  153. u8 xfers;/* transfer status */
  154. u8 rs; /* receive status */
  155. u8 ts; /* transmit status */
  156. u8 fies; /* Current status of the controller state machine */
  157. u8 fras; /* Frame tagging status */
  158. } FCanXferStatus;
  159. typedef struct
  160. {
  161. FCanXferStatus xfer_status;
  162. u32 tx_err_cnt;
  163. u32 rx_err_cnt;
  164. u32 tx_fifo_cnt;
  165. u32 rx_fifo_cnt;
  166. } FCanStatus;
  167. typedef struct
  168. {
  169. FCanConfig config;
  170. u32 is_ready; /* Device is initialized and ready */
  171. boolean use_canfd; /* if use canfd function */
  172. FCanIntrEventConfig intr_event[FCAN_INTR_EVENT_NUM];/* event handler and parameters for interrupt */
  173. } FCanCtrl;
  174. /* get default configuration of specific can id */
  175. const FCanConfig *FCanLookupConfig(FCanInstance instance_id);
  176. /* reset a specific can instance */
  177. void FCanReset(FCanCtrl *instance_p);
  178. /* Deinitializes a specific can instance */
  179. void FCanDeInitialize(FCanCtrl *instance_p);
  180. /* Initializes a specific can instance */
  181. FError FCanCfgInitialize(FCanCtrl *instance_p, const FCanConfig *input_config_p);
  182. /* Set the fcan baudrate */
  183. FError FCanBaudrateSet(FCanCtrl *instance_p, FCanBaudrateConfig *baudrate_p);
  184. /* interrupt handler for the driver */
  185. void FCanIntrHandler(s32 vector, void *args);
  186. /* register FCanCtrl interrupt handler function */
  187. void FCanRegisterInterruptHandler(FCanCtrl *instance_p, FCanIntrEventConfig *intr_event_p);
  188. /* receive can message by specific can instance */
  189. FError FCanRecv(FCanCtrl *instance_p, FCanFrame *frame_p);
  190. /* send can message by specific can instance */
  191. FError FCanSend(FCanCtrl *instance_p, FCanFrame *frame_p);
  192. /* Enable the specific can instance */
  193. FError FCanEnable(FCanCtrl *instance_p, boolean enable);
  194. /* read can status, include send and receive error count */
  195. FError FCanStatusGet(FCanCtrl *instance_p, FCanStatus *status_p);
  196. /* Set the can mask and umask id */
  197. FError FCanIdMaskFilterSet(FCanCtrl *instance_p, FCanIdMaskConfig *id_mask_p);
  198. /* Set the can id mask filter enable */
  199. void FCanIdMaskFilterEnable(FCanCtrl *instance_p);
  200. /* Set the can id mask filter disable */
  201. void FCanIdMaskFilterDisable(FCanCtrl *instance_p);
  202. /* Enable can interrupt */
  203. FError FCanInterruptEnable(FCanCtrl *instance_p, FCanIntrEventType event_type);
  204. /* Enable or disable can fd */
  205. FError FCanFdEnable(FCanCtrl *instance_p, boolean enable);
  206. #endif // !FT_CAN_H