Naze32 clone with Frysky receiver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CAN.h 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.h"
  19. #if DEVICE_CAN
  20. #include "can_api.h"
  21. #include "can_helper.h"
  22. #include "FunctionPointer.h"
  23. namespace mbed {
  24. /** CANMessage class
  25. */
  26. class CANMessage : public CAN_Message {
  27. public:
  28. /** Creates empty CAN message.
  29. */
  30. CANMessage() : CAN_Message() {
  31. len = 8;
  32. type = CANData;
  33. format = CANStandard;
  34. id = 0;
  35. memset(data, 0, 8);
  36. }
  37. /** Creates CAN message with specific content.
  38. */
  39. CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
  40. len = _len & 0xF;
  41. type = _type;
  42. format = _format;
  43. id = _id;
  44. memcpy(data, _data, _len);
  45. }
  46. /** Creates CAN remote message.
  47. */
  48. CANMessage(int _id, CANFormat _format = CANStandard) {
  49. len = 0;
  50. type = CANRemote;
  51. format = _format;
  52. id = _id;
  53. memset(data, 0, 8);
  54. }
  55. };
  56. /** A can bus client, used for communicating with can devices
  57. */
  58. class CAN {
  59. public:
  60. /** Creates an CAN interface connected to specific pins.
  61. *
  62. * @param rd read from transmitter
  63. * @param td transmit to transmitter
  64. *
  65. * Example:
  66. * @code
  67. * #include "mbed.h"
  68. *
  69. * Ticker ticker;
  70. * DigitalOut led1(LED1);
  71. * DigitalOut led2(LED2);
  72. * CAN can1(p9, p10);
  73. * CAN can2(p30, p29);
  74. *
  75. * char counter = 0;
  76. *
  77. * void send() {
  78. * if(can1.write(CANMessage(1337, &counter, 1))) {
  79. * printf("Message sent: %d\n", counter);
  80. * counter++;
  81. * }
  82. * led1 = !led1;
  83. * }
  84. *
  85. * int main() {
  86. * ticker.attach(&send, 1);
  87. * CANMessage msg;
  88. * while(1) {
  89. * if(can2.read(msg)) {
  90. * printf("Message received: %d\n\n", msg.data[0]);
  91. * led2 = !led2;
  92. * }
  93. * wait(0.2);
  94. * }
  95. * }
  96. * @endcode
  97. */
  98. CAN(PinName rd, PinName td);
  99. virtual ~CAN();
  100. /** Set the frequency of the CAN interface
  101. *
  102. * @param hz The bus frequency in hertz
  103. *
  104. * @returns
  105. * 1 if successful,
  106. * 0 otherwise
  107. */
  108. int frequency(int hz);
  109. /** Write a CANMessage to the bus.
  110. *
  111. * @param msg The CANMessage to write.
  112. *
  113. * @returns
  114. * 0 if write failed,
  115. * 1 if write was successful
  116. */
  117. int write(CANMessage msg);
  118. /** Read a CANMessage from the bus.
  119. *
  120. * @param msg A CANMessage to read to.
  121. * @param handle message filter handle (0 for any message)
  122. *
  123. * @returns
  124. * 0 if no message arrived,
  125. * 1 if message arrived
  126. */
  127. int read(CANMessage &msg, int handle = 0);
  128. /** Reset CAN interface.
  129. *
  130. * To use after error overflow.
  131. */
  132. void reset();
  133. /** Puts or removes the CAN interface into silent monitoring mode
  134. *
  135. * @param silent boolean indicating whether to go into silent mode or not
  136. */
  137. void monitor(bool silent);
  138. enum Mode {
  139. Reset = 0,
  140. Normal,
  141. Silent,
  142. LocalTest,
  143. GlobalTest,
  144. SilentTest
  145. };
  146. /** Change CAN operation to the specified mode
  147. *
  148. * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
  149. *
  150. * @returns
  151. * 0 if mode change failed or unsupported,
  152. * 1 if mode change was successful
  153. */
  154. int mode(Mode mode);
  155. /** Filter out incomming messages
  156. *
  157. * @param id the id to filter on
  158. * @param mask the mask applied to the id
  159. * @param format format to filter on (Default CANAny)
  160. * @param handle message filter handle (Optional)
  161. *
  162. * @returns
  163. * 0 if filter change failed or unsupported,
  164. * new filter handle if successful
  165. */
  166. int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
  167. /** Returns number of read errors to detect read overflow errors.
  168. */
  169. unsigned char rderror();
  170. /** Returns number of write errors to detect write overflow errors.
  171. */
  172. unsigned char tderror();
  173. enum IrqType {
  174. RxIrq = 0,
  175. TxIrq,
  176. EwIrq,
  177. DoIrq,
  178. WuIrq,
  179. EpIrq,
  180. AlIrq,
  181. BeIrq,
  182. IdIrq
  183. };
  184. /** Attach a function to call whenever a CAN frame received interrupt is
  185. * generated.
  186. *
  187. * @param fptr A pointer to a void function, or 0 to set as none
  188. * @param event 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)
  189. */
  190. void attach(void (*fptr)(void), IrqType type=RxIrq);
  191. /** Attach a member function to call whenever a CAN frame received interrupt
  192. * is generated.
  193. *
  194. * @param tptr pointer to the object to call the member function on
  195. * @param mptr pointer to the member function to be called
  196. * @param event 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)
  197. */
  198. template<typename T>
  199. void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
  200. if((mptr != NULL) && (tptr != NULL)) {
  201. _irq[type].attach(tptr, mptr);
  202. can_irq_set(&_can, (CanIrqType)type, 1);
  203. }
  204. else {
  205. can_irq_set(&_can, (CanIrqType)type, 0);
  206. }
  207. }
  208. static void _irq_handler(uint32_t id, CanIrqType type);
  209. protected:
  210. can_t _can;
  211. FunctionPointer _irq[9];
  212. };
  213. } // namespace mbed
  214. #endif
  215. #endif // MBED_CAN_H