can.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifndef _Shark_Can0_h__
  2. #define _Shark_Can0_h__
  3. #include <stdio.h>
  4. #include "os/os_types.h"
  5. #include "config.h"
  6. //CAN DLC lenght
  7. #define CAN_DLC_LENGTH 8
  8. #define CAN_EXTENDE_FRAME 1
  9. #ifdef CAN_EXTENDE_FRAME
  10. #define CAN_DATA_SIZE CAN_DLC_LENGTH
  11. #else
  12. #define CAN_DATA_SIZE 64
  13. #endif
  14. typedef union {
  15. uint32_t id;
  16. struct {
  17. uint32_t dest :7; /*bit 0-6 */
  18. uint32_t src :7; /*bit 7-13 */
  19. uint32_t idx :5; /*bit 14-18 */
  20. uint32_t total :5; /*bit 19-23 */
  21. uint32_t type :2; /*bit 24-25 */ /*1:PT_REQ_NEED_RES ; 2:PT_RES 3:PT_REQ_NO_IND*/
  22. uint32_t retry :3; /*bit 26-28 */
  23. uint32_t length :3; /*bit 29-13 */ //0-7:1-8
  24. };
  25. }can_id_t;
  26. #if 0
  27. typedef union {
  28. uint32_t id;
  29. struct {
  30. uint32_t dest :5; /*bit 0-4 */
  31. uint32_t src :5; /*bit 5-9 */
  32. uint32_t idx :4; /*bit 10-13 */
  33. uint32_t total :4; /*bit 14-17 */
  34. uint32_t command :8; /*bit 18-25 */
  35. uint32_t type :1; /*bit 26, 1:Request(Need ack), 0:Ack(No ack needed)*/
  36. uint32_t res :1; /*bit 27 */
  37. uint32_t lpkg :1; /*bit 28 , 0: short package, 1: long package*/
  38. };
  39. struct {
  40. uint32_t ext_command :27; /*bit 0-26 */
  41. uint32_t ext_type :1; /*bit 26, 1:Request(Need ack), 0:Ack(No ack needed)*/
  42. uint32_t ext_pkg :1; /*bit 28 , 0: short package, 1: long package*/
  43. };
  44. }cid_v2;
  45. #endif
  46. //CAN filter setting
  47. #ifdef CAN_COMMUNICATION_SPEC_V1P4
  48. //specification 1.4
  49. #define CAN_ID_FILTER_START_OFFSET 3
  50. #define CAN_ID_FILTER_FLAG_OFFSET 7
  51. #define CAN_ID_FILTER_FLAG 0x7F
  52. #else
  53. //specification 1.3
  54. #define CAN_ID_FILTER_START_OFFSET 3
  55. #define CAN_ID_FILTER_FLAG_OFFSET 8
  56. #define CAN_ID_FILTER_FLAG 0xFF
  57. #endif
  58. #define CAN_FILTER_DEST_MASK 0x7F
  59. #define ptype_beat_heart 0 // can heat heart
  60. #define ptype_request 1 // can request with need response
  61. #define ptype_response 2 // can response of the request
  62. #define ptype_indicater 3 // can request with no need response
  63. #define can_packet_priority_value 0x06
  64. #define can_packet_quick_packet_value 0x01
  65. //used when can recv
  66. static __inline__ uint16_t decoder_can_key(const void *buf){
  67. uint8_t *key_buf = (uint8_t *)buf;
  68. return key_buf[1]<<8 | key_buf[0];
  69. }
  70. //used when can send
  71. static __inline__ void encoder_can_key(uint8_t *buff, uint16_t key) {
  72. buff[0] = key & 0xFF;
  73. buff[1] = (key >> 8) & 0xFF;
  74. }
  75. int shark_can0_send_message(uint32_t can_id, const void*buff, int len);
  76. void shark_can0_route_message(uint32_t can_id, const void *buff, int len);
  77. int shark_can0_send_ext_message(uint32_t can_id, const void*buff, int len);
  78. void shark_can0_init(void);
  79. void shark_can0_reset(void);
  80. void shark_can0_deinit(void);
  81. void can_rx_poll(void);
  82. #endif /* _Shark_Can0_h__ */