canard_avr.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2016 UAVCAN Team
  3. *
  4. * Distributed under the MIT License, available in the file LICENSE.
  5. *
  6. */
  7. #define HAS_CAN_CONFIG_H
  8. #include <canard_avr.h>
  9. #include <string.h>
  10. #include <avr/io.h>
  11. #include <avr/interrupt.h>
  12. #include <avr/pgmspace.h>
  13. #include <can.h>
  14. int canardAVRInit(uint32_t bitrate)
  15. {
  16. can_bitrate_t br;
  17. switch (bitrate)
  18. {
  19. case 10000:
  20. {
  21. br = BITRATE_10_KBPS;
  22. break;
  23. }
  24. case 20000:
  25. {
  26. br = BITRATE_20_KBPS;
  27. break;
  28. }
  29. case 50000:
  30. {
  31. br = BITRATE_50_KBPS;
  32. break;
  33. }
  34. case 100000:
  35. {
  36. br = BITRATE_100_KBPS;
  37. break;
  38. }
  39. case 125000:
  40. {
  41. br = BITRATE_125_KBPS;
  42. break;
  43. }
  44. case 250000:
  45. {
  46. br = BITRATE_250_KBPS;
  47. break;
  48. }
  49. case 500000:
  50. {
  51. br = BITRATE_500_KBPS;
  52. break;
  53. }
  54. case 1000000:
  55. {
  56. br = BITRATE_1_MBPS;
  57. break;
  58. }
  59. default:
  60. {
  61. return -1;
  62. }
  63. }
  64. can_init(br);
  65. can_set_mode(NORMAL_MODE);
  66. // create a new filter for receiving all messages
  67. can_filter_t filter = {
  68. .id = 0,
  69. .mask = 0,
  70. .flags = {
  71. .rtr = 0,
  72. .extended = 0
  73. }
  74. };
  75. can_set_filter(0, &filter);
  76. // enable interrupts
  77. sei();
  78. return 0;
  79. }
  80. int canardAVRClose(void)
  81. {
  82. return 0;
  83. }
  84. int canardAVRTransmit(const CanardCANFrame* frame)
  85. {
  86. const int poll_result = can_check_free_buffer();
  87. if (poll_result <= 0)
  88. {
  89. return 0;
  90. }
  91. can_t transmit_msg;
  92. memset(&transmit_msg, 0, sizeof(transmit_msg));
  93. transmit_msg.id = frame->id & CANARD_CAN_EXT_ID_MASK;
  94. transmit_msg.length = frame->data_len;
  95. transmit_msg.flags.extended = (frame->id & CANARD_CAN_FRAME_EFF) != 0;
  96. memcpy(transmit_msg.data, frame->data, frame->data_len);
  97. const uint8_t res = can_send_message(&transmit_msg);
  98. if (res <= 0)
  99. {
  100. return -1;
  101. }
  102. return 1;
  103. }
  104. int canardAVRReceive(CanardCANFrame* out_frame)
  105. {
  106. const int poll_result = can_check_message();
  107. if (poll_result <= 0)
  108. {
  109. return 0;
  110. }
  111. can_t receive_msg;
  112. const uint8_t res = can_get_message(&receive_msg);
  113. if (res <= 0)
  114. {
  115. return -1;
  116. }
  117. out_frame->id = receive_msg.id;
  118. out_frame->data_len = receive_msg.length;
  119. memcpy(out_frame->data, receive_msg.data, receive_msg.length);
  120. if (receive_msg.flags.extended != 0)
  121. {
  122. out_frame->id |= CANARD_CAN_FRAME_EFF;
  123. }
  124. return 1;
  125. }
  126. int canardAVRConfigureAcceptanceFilters(uint8_t node_id)
  127. {
  128. static const uint32_t DefaultFilterMsgMask = 0x80;
  129. static const uint32_t DefaultFilterMsgID = 0x0;
  130. static const uint32_t DefaultFilterSrvMask = 0x7F80;
  131. uint8_t res = 1;
  132. // create a new filter for receiving messages
  133. can_filter_t filter_Msg = {
  134. .id = DefaultFilterMsgID,
  135. .mask = DefaultFilterMsgMask,
  136. .flags = {
  137. .rtr = 0,
  138. .extended = 3
  139. }
  140. };
  141. // create a new filter for receiving services
  142. can_filter_t filter_Srv = {
  143. .id = ((uint32_t)node_id << 8) | 0x80,
  144. .mask = DefaultFilterSrvMask,
  145. .flags = {
  146. .rtr = 0,
  147. .extended = 3
  148. }
  149. };
  150. // setup 2 MOb's to receive, 12 MOb's are used as send buffer
  151. if (!can_set_filter(0, &filter_Msg))
  152. {
  153. res = -1;
  154. }
  155. if (!can_set_filter(1, &filter_Srv))
  156. {
  157. res = -1;
  158. }
  159. return res;
  160. }