CAN.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_CAN_H
  17. #define MBED_CAN_H
  18. #include "platform/platform.h"
  19. #if defined (DEVICE_CAN) || defined(DOXYGEN_ONLY)
  20. #include "hal/can_api.h"
  21. #include "platform/Callback.h"
  22. #include "platform/PlatformMutex.h"
  23. #include "platform/NonCopyable.h"
  24. namespace mbed {
  25. /** \addtogroup drivers */
  26. /** CANMessage class
  27. *
  28. * @note Synchronization level: Thread safe
  29. * @ingroup drivers
  30. */
  31. class CANMessage : public CAN_Message {
  32. public:
  33. /** Creates empty CAN message.
  34. */
  35. CANMessage() : CAN_Message()
  36. {
  37. len = 8;
  38. type = CANData;
  39. format = CANStandard;
  40. id = 0;
  41. memset(data, 0, 8);
  42. }
  43. /** Creates CAN message with specific content.
  44. *
  45. * @param _id Message ID
  46. * @param _data Mesaage Data
  47. * @param _len Message Data length
  48. * @param _type Type of Data: Use enum CANType for valid parameter values
  49. * @param _format Data Format: Use enum CANFormat for valid parameter values
  50. */
  51. CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard)
  52. {
  53. len = _len & 0xF;
  54. type = _type;
  55. format = _format;
  56. id = _id;
  57. memcpy(data, _data, _len);
  58. }
  59. /** Creates CAN remote message.
  60. *
  61. * @param _id Message ID
  62. * @param _format Data Format: Use enum CANType for valid parameter values
  63. */
  64. CANMessage(int _id, CANFormat _format = CANStandard)
  65. {
  66. len = 0;
  67. type = CANRemote;
  68. format = _format;
  69. id = _id;
  70. memset(data, 0, 8);
  71. }
  72. };
  73. /** A can bus client, used for communicating with can devices
  74. * @ingroup drivers
  75. */
  76. class CAN : private NonCopyable<CAN> {
  77. public:
  78. /** Creates an CAN interface connected to specific pins.
  79. *
  80. * @param rd read from transmitter
  81. * @param td transmit to transmitter
  82. *
  83. * Example:
  84. * @code
  85. * #include "mbed.h"
  86. *
  87. * Ticker ticker;
  88. * DigitalOut led1(LED1);
  89. * DigitalOut led2(LED2);
  90. * CAN can1(p9, p10);
  91. * CAN can2(p30, p29);
  92. *
  93. * char counter = 0;
  94. *
  95. * void send() {
  96. * if(can1.write(CANMessage(1337, &counter, 1))) {
  97. * printf("Message sent: %d\n", counter);
  98. * counter++;
  99. * }
  100. * led1 = !led1;
  101. * }
  102. *
  103. * int main() {
  104. * ticker.attach(&send, 1);
  105. * CANMessage msg;
  106. * while(1) {
  107. * if(can2.read(msg)) {
  108. * printf("Message received: %d\n\n", msg.data[0]);
  109. * led2 = !led2;
  110. * }
  111. * wait(0.2);
  112. * }
  113. * }
  114. * @endcode
  115. */
  116. CAN(PinName rd, PinName td);
  117. /** Initialize CAN interface and set the frequency
  118. *
  119. * @param rd the rd pin
  120. * @param td the td pin
  121. * @param hz the bus frequency in hertz
  122. */
  123. CAN(PinName rd, PinName td, int hz);
  124. virtual ~CAN();
  125. /** Set the frequency of the CAN interface
  126. *
  127. * @param hz The bus frequency in hertz
  128. *
  129. * @returns
  130. * 1 if successful,
  131. * 0 otherwise
  132. */
  133. int frequency(int hz);
  134. /** Write a CANMessage to the bus.
  135. *
  136. * @param msg The CANMessage to write.
  137. *
  138. * @returns
  139. * 0 if write failed,
  140. * 1 if write was successful
  141. */
  142. int write(CANMessage msg);
  143. /** Read a CANMessage from the bus.
  144. *
  145. * @param msg A CANMessage to read to.
  146. * @param handle message filter handle (0 for any message)
  147. *
  148. * @returns
  149. * 0 if no message arrived,
  150. * 1 if message arrived
  151. */
  152. int read(CANMessage &msg, int handle = 0);
  153. /** Reset CAN interface.
  154. *
  155. * To use after error overflow.
  156. */
  157. void reset();
  158. /** Puts or removes the CAN interface into silent monitoring mode
  159. *
  160. * @param silent boolean indicating whether to go into silent mode or not
  161. */
  162. void monitor(bool silent);
  163. enum Mode {
  164. Reset = 0,
  165. Normal,
  166. Silent,
  167. LocalTest,
  168. GlobalTest,
  169. SilentTest
  170. };
  171. /** Change CAN operation to the specified mode
  172. *
  173. * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
  174. *
  175. * @returns
  176. * 0 if mode change failed or unsupported,
  177. * 1 if mode change was successful
  178. */
  179. int mode(Mode mode);
  180. /** Filter out incomming messages
  181. *
  182. * @param id the id to filter on
  183. * @param mask the mask applied to the id
  184. * @param format format to filter on (Default CANAny)
  185. * @param handle message filter handle (Optional)
  186. *
  187. * @returns
  188. * 0 if filter change failed or unsupported,
  189. * new filter handle if successful
  190. */
  191. int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
  192. /** Detects read errors - Used to detect read overflow errors.
  193. *
  194. * @returns number of read errors
  195. */
  196. unsigned char rderror();
  197. /** Detects write errors - Used to detect write overflow errors.
  198. *
  199. * @returns number of write errors
  200. */
  201. unsigned char tderror();
  202. enum IrqType {
  203. RxIrq = 0,
  204. TxIrq,
  205. EwIrq,
  206. DoIrq,
  207. WuIrq,
  208. EpIrq,
  209. AlIrq,
  210. BeIrq,
  211. IdIrq,
  212. IrqCnt
  213. };
  214. /** Attach a function to call whenever a CAN frame received interrupt is
  215. * generated.
  216. *
  217. * This function locks the deep sleep while a callback is attached
  218. *
  219. * @param func A pointer to a void function, or 0 to set as none
  220. * @param type Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error)
  221. */
  222. void attach(Callback<void()> func, IrqType type = RxIrq);
  223. /** Attach a member function to call whenever a CAN frame received interrupt
  224. * is generated.
  225. *
  226. * @param obj pointer to the object to call the member function on
  227. * @param method pointer to the member function to be called
  228. * @param type Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
  229. * @deprecated
  230. * The attach function does not support cv-qualifiers. Replaced by
  231. * attach(callback(obj, method), type).
  232. */
  233. template<typename T>
  234. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  235. "The attach function does not support cv-qualifiers. Replaced by "
  236. "attach(callback(obj, method), type).")
  237. void attach(T *obj, void (T::*method)(), IrqType type = RxIrq)
  238. {
  239. // Underlying call thread safe
  240. attach(callback(obj, method), type);
  241. }
  242. /** Attach a member function to call whenever a CAN frame received interrupt
  243. * is generated.
  244. *
  245. * @param obj pointer to the object to call the member function on
  246. * @param method pointer to the member function to be called
  247. * @param type Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
  248. * @deprecated
  249. * The attach function does not support cv-qualifiers. Replaced by
  250. * attach(callback(obj, method), type).
  251. */
  252. template<typename T>
  253. MBED_DEPRECATED_SINCE("mbed-os-5.1",
  254. "The attach function does not support cv-qualifiers. Replaced by "
  255. "attach(callback(obj, method), type).")
  256. void attach(T *obj, void (*method)(T *), IrqType type = RxIrq)
  257. {
  258. // Underlying call thread safe
  259. attach(callback(obj, method), type);
  260. }
  261. static void _irq_handler(uint32_t id, CanIrqType type);
  262. protected:
  263. virtual void lock();
  264. virtual void unlock();
  265. can_t _can;
  266. Callback<void()> _irq[IrqCnt];
  267. PlatformMutex _mutex;
  268. };
  269. } // namespace mbed
  270. #endif
  271. #endif // MBED_CAN_H