1 /**
2  * \file
3  *
4  * \brief Control Area Network(CAN) functionality declaration.
5  *
6  * Copyright (c) 2016-2018 Microchip Technology Inc. and its subsidiaries.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Subject to your compliance with these terms, you may use Microchip
13  * software and any derivatives exclusively with Microchip products.
14  * It is your responsibility to comply with third party license terms applicable
15  * to your use of third party software (including open source software) that
16  * may accompany Microchip software.
17  *
18  * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
19  * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
20  * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
21  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
22  * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
23  * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
24  * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
25  * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
26  * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
27  * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
28  * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
29  *
30  * \asf_license_stop
31  *
32  */
33 
34 #ifndef HPL_CAN_ASYNC_H_INCLUDED
35 #define HPL_CAN_ASYNC_H_INCLUDED
36 
37 #include <utils.h>
38 #include <hpl_can.h>
39 #include <hpl_irq.h>
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 /**
46  * \addtogroup hpl_can_async CAN HPL Driver
47  *
48  * \section can_rev Revision History
49  * - v0.0.0.1 Initial Commit
50  *
51  *@{
52  */
53 
54 /**
55  * \brief CAN device descriptor forware declaration
56  */
57 struct _can_async_device;
58 
59 /**
60  * \brief CAN callback types
61  */
62 enum can_async_callback_type {
63 	CAN_ASYNC_RX_CB, /*!< A new message arrived */
64 	CAN_ASYNC_TX_CB, /*!< A message transmitted */
65 	CAN_ASYNC_IRQ_CB /*!< Message error of some kind on the CAN bus IRQ */
66 };
67 
68 enum can_async_interrupt_type {
69 	CAN_IRQ_EW, /*!< Error warning, Error counter has reached the
70 	              error warning limit of 96, An error count value
71 	              greater than about 96 indicates a heavily disturbed
72 	              bus. It may be of advantage to provide means to test
73 	              for this condition. Refer to ISO 11898-1 (Bosch CAN
74 	              specification 2.0 part A,B)
75 	              */
76 	CAN_IRQ_EA, /*!< Error Active State, The CAN node normally take
77 	               part in bus communication and sends an ACTIVE ERROR
78 	               FLAG when an error has been detected.
79 	               Refer to ISO 11898-1 (7)
80 	               */
81 	CAN_IRQ_EP, /*!< Error Passive State, The Can node goes into error
82 	              passive state if at least one of its error counters is
83 	              greater than 127. It still takes part in bus
84 	              activities, but it sends a passive error frame only,
85 	              on errors. Refer to ISO 11898-1 (7)
86 	              */
87 	CAN_IRQ_BO, /*!< Bus Off State, The CAN node is 'bus off' when the
88 	               TRANSMIT ERROR COUNT is greater than or equal to 256.
89 	               Refer to ISO 11898-1 (7)
90 	               */
91 	CAN_IRQ_DO  /*!< Data Overrun in receive queue. A message was lost
92 	               because the messages in the queue was not reading and
93 	               releasing fast enough. There is not enough space for
94 	               a new message in receive queue.
95 	               */
96 };
97 /**
98  * \brief CAN interrupt handlers structure
99  */
100 struct _can_async_callback {
101 	void (*tx_done)(struct _can_async_device *dev);
102 	void (*rx_done)(struct _can_async_device *dev);
103 	void (*irq_handler)(struct _can_async_device *dev, enum can_async_interrupt_type type);
104 };
105 
106 /**
107  * \brief CAN device descriptor
108  */
109 struct _can_async_device {
110 	void *                     hw;      /*!< CAN hardware pointer */
111 	struct _can_async_callback cb;      /*!< CAN interrupt handler */
112 	struct _irq_descriptor     irq;     /*!< Interrupt descriptor */
113 	void *                     context; /*!< CAN hardware context */
114 };
115 
116 /**
117  * \brief Initialize CAN.
118  *
119  * This function initializes the given CAN device descriptor.
120  *
121  * \param[in, out] dev   A CAN device descriptor to initialize
122  * \param[in]      hw    The pointer to hardware instance
123  *
124  * \return Initialization status.
125  */
126 int32_t _can_async_init(struct _can_async_device *const dev, void *const hw);
127 
128 /**
129  * \brief Deinitialize CAN.
130  *
131  * This function deinitializes the given can device descriptor.
132  *
133  * \param[in] dev The CAN device descriptor to deinitialize
134  *
135  * \return De-initialization status.
136  */
137 int32_t _can_async_deinit(struct _can_async_device *const dev);
138 
139 /**
140  * \brief Enable CAN
141  *
142  * This function enable CAN by the given can device descriptor.
143  *
144  * \param[in] dev The CAN device descriptor to enable
145  *
146  * \return Enabling status.
147  */
148 int32_t _can_async_enable(struct _can_async_device *const dev);
149 
150 /**
151  * \brief Disable CAN
152  *
153  * This function disable CAN by the given can device descriptor.
154  *
155  * \param[in] dev The CAN descriptor to disable
156  *
157  * \return Disabling status.
158  */
159 int32_t _can_async_disable(struct _can_async_device *const dev);
160 
161 /**
162  * \brief Read a CAN message
163  *
164  * \param[in] dev   The CAN device descriptor to read message.
165  * \param[in] msg   The CAN message to read to.
166  *
167  * \return The status of read message.
168  */
169 int32_t _can_async_read(struct _can_async_device *const dev, struct can_message *msg);
170 
171 /**
172  * \brief Write a CAN message
173  *
174  * \param[in] dev   The CAN device descriptor to write message.
175  * \param[in] msg   The CAN message to write.
176  *
177  * \return The status of write message.
178  */
179 int32_t _can_async_write(struct _can_async_device *const dev, struct can_message *msg);
180 
181 /**
182  * \brief Set CAN Interrupt State
183  *
184  * \param[in] dev   The CAN device descriptor
185  * \param[in] type  Callback type
186  * \param[in] state ture for enable or false for disable
187  *
188  */
189 void _can_async_set_irq_state(struct _can_async_device *const dev, enum can_async_callback_type type, bool state);
190 
191 /**
192  * \brief Return number of read errors
193  *
194  * This function return number of read errors
195  *
196  * \param[in] dev The CAN device descriptor pointer
197  *
198  * \return Number of read errors.
199  */
200 uint8_t _can_async_get_rxerr(struct _can_async_device *const dev);
201 
202 /**
203  * \brief Return number of write errors
204  *
205  * This function return number of write errors
206  *
207  * \param[in] dev The CAN device descriptor pointer
208  *
209  * \return Number of write errors.
210  */
211 uint8_t _can_async_get_txerr(struct _can_async_device *const dev);
212 
213 /**
214  * \brief Set CAN to the specified mode
215  *
216  * This function set CAN to a specified mode
217  *
218  * \param[in] dev The CAN device descriptor pointer
219  * \param[in] mode  CAN operation mode
220  *
221  * \return Status of the operation
222  */
223 int32_t _can_async_set_mode(struct _can_async_device *const dev, enum can_mode mode);
224 
225 /**
226  * \brief Set CAN to the specified mode
227  *
228  * This function set CAN to a specified mode
229  *
230  * \param[in] dev The CAN device descriptor pointer
231  * \param[in] index   Index of Filter list
232  * \param[in] filter  CAN Filter struct, NULL for clear filter
233  *
234  * \return Status of the operation
235  */
236 int32_t _can_async_set_filter(struct _can_async_device *const dev, uint8_t index, enum can_format fmt,
237                               struct can_filter *filter);
238 
239 /**@}*/
240 
241 #ifdef __cplusplus
242 }
243 #endif
244 
245 #endif /* HPL_CAN_ASYNC_H_INCLUDED */
246