1 /*
2  * @ : Copyright (c) 2021 Phytium Information Technology, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0.
5  *
6  * @Date: 2021-04-27 15:08:44
7  * @LastEditTime: 2021-04-27 15:08:44
8  * @Description:  Description of file
9  * @Modify History:
10  * * * Ver   Who        Date         Changes
11  * * ----- ------     --------    --------------------------------------
12  */
13 
14 #ifndef FT_CAN_H
15 #define FT_CAN_H
16 
17 #include "ft_types.h"
18 #include "ft_error_code.h"
19 
20 #define FCAN_SUCCESS FST_SUCCESS                                                     /* SUCCESS */
21 #define FCAN_FAILURE FT_MAKE_ERRCODE(errCan, errBspGeneral, FST_FAILURE)             /* Normal */
22 #define FCAN_TIMEOUT FT_MAKE_ERRCODE(errCan, errBspGeneral, FST_TIMEOUT)             /* Timeout */
23 #define FCAN_EILSEQ FT_MAKE_ERRCODE(errCan, errBspGeneral, FST_EILSEQ)               /* Illegal byte sequence. */
24 #define FCAN_INVALID_PARAM FT_MAKE_ERRCODE(errCan, errBspGeneral, FST_INVALID_PARAM) /* Invalid param. */
25 
26 #define FCAN_HANDLER_SEND 1U  /**< Handler type for frame sending interrupt */
27 #define FCAN_HANDLER_RECV 2U  /**< Handler type for frame reception interrupt*/
28 #define FCAN_HANDLER_ERROR 3U /**< Handler type for error interrupt */
29 #define FCAN_DATA_LENGTH 8U
30 
31 /* CAN payload length and DLC definitions according to ISO 11898-1 */
32 #define CAN_MAX_DLC 8
33 #define CAN_MAX_DLEN 8
34 #define CAN_MAX_CTL 3
35 #define CAN_SFF_ID_BITS 11
36 #define CAN_EFF_ID_BITS 29
37 
38 /* special address description flags for the CAN_ID */
39 #define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
40 #define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
41 #define CAN_ERR_FLAG 0x20000000U /* error message frame */
42 
43 /* valid bits in CAN ID for frame formats */
44 #define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */
45 #define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
46 #define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */
47 
48 /* Frame type */
49 #define STANDARD_FRAME 0 /* standard frame */
50 #define EXTEND_FRAME 1   /* extended frame */
51 
52 typedef void (*FCan_irqHandler_t)(void *Args);
53 
54 struct FCan_Frame
55 {
56     u32 CanId;
57     u8 CanDlc;
58     u8 data[FCAN_DATA_LENGTH];
59 };
60 
61 struct FCan_Bittiming
62 {
63     u32 bitrate;      /* Bit-rate in bits/second */
64     u32 sample_point; /* Sample point in one-tenth of a percent */
65     u32 tq;           /* Time quanta (TQ) in nanoseconds */
66     u32 prop_seg;     /* Propagation segment in TQs */
67     u32 phase_seg1;   /* Phase buffer segment 1 in TQs */
68     u32 phase_seg2;   /* Phase buffer segment 2 in TQs */
69     u32 sjw;          /* Synchronisation jump width in TQs */
70     u32 brp;          /* Bit-rate prescaler */
71 };
72 
73 typedef struct
74 {
75     u32 InstanceId;     /* Id of device */
76     u32 CanBaseAddress; /* Can base Address */
77     u32 IrqNum;
78     u32 BaudRate;
79     u32 TxFifoDeepth; /* The depth of the full frame , */
80 } FCan_Config_t;
81 
82 typedef struct
83 {
84     FCan_Config_t Config;
85     u32 IsReady; /* Device is initialized and ready */
86 
87     volatile u32 TxFifoCnt;
88 
89     FCan_irqHandler_t SendHandler;
90     void *SendRef;
91 
92     FCan_irqHandler_t RecvHandler;
93     void *RecvRef;
94 
95     FCan_irqHandler_t ErrorHandler;
96     void *ErrorRef;
97 
98 } FCan_t;
99 
100 FCan_Config_t *FCan_LookupConfig(u32 InstanceId);
101 
102 /**
103  * @name: FCan_CfgInitialize
104  * @msg:  This function initializes a Can instance/driver.
105  * @in param Can_p:    Can_p is a pointer to the FCan_t instance.
106  * @in param Config_p: Config_p points to the FCan_t device configuration structure.
107  * @return {*}
108  */
109 ft_error_t FCan_CfgInitialize(FCan_t *Can_p, FCan_Config_t *Config_p);
110 
111 /**
112  * @name: FCan_SetHandler
113  * @msg:  This routine installs an asynchronous callback function for the given
114  * @inout param Can_p: Can_p is a pointer to the FCan_t instance.
115  * @in param HandlerType:  specifies which handler is to be attached.
116  * @in param IrqCallBackFunc: IrqCallBackFunc is the address of the callback function.
117  * @in param IrqCallBackRef: IrqCallBackRef is a user data item that will be passed to the
118  *      callback function when it is invoked.
119  * @return {*}
120  * @param {FCan_t} *Can_p
121  * @param {u32} HandlerType
122  * @param {FCan_irqHandler_t} *IrqCallBackFunc
123  * @param {void} *IrqCallBackRef
124  */
125 ft_error_t FCan_SetHandler(FCan_t *Can_p, u32 HandlerType, FCan_irqHandler_t IrqCallBackFunc, void *IrqCallBackRef);
126 
127 ft_error_t FCan_SetTiming(FCan_t *Can_p,
128                           struct FCan_Bittiming *Bittiming_p);
129 
130 void FCan_IntrHandler(void *InstancePtr);
131 
132 ft_error_t FCan_CalcBittiming(struct FCan_Bittiming *Bt_p);
133 
134 u32 FCan_SendByIrq(FCan_t *Can_p,
135                    struct FCan_Frame *Frame_p,
136                    u32 FrameNumber, void (*UserIrqWait)(void));
137 
138 u32 FCan_RecvByIrq(FCan_t *Can_p, struct FCan_Frame *Frame_p, u32 FrameNumber);
139 
140 void FCan_Enable(FCan_t *Can_p);
141 
142 #endif // !FT_CAN_H
143