can.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. //CAN filter setting
  27. #ifdef CAN_COMMUNICATION_SPEC_V1P4
  28. //specification 1.4
  29. #define CAN_ID_FILTER_START_OFFSET 3
  30. #define CAN_ID_FILTER_FLAG_OFFSET 7
  31. #define CAN_ID_FILTER_FLAG 0x7F
  32. #else
  33. //specification 1.3
  34. #define CAN_ID_FILTER_START_OFFSET 3
  35. #define CAN_ID_FILTER_FLAG_OFFSET 8
  36. #define CAN_ID_FILTER_FLAG 0xFF
  37. #endif
  38. #define CAN_FILTER_DEST_MASK 0x7F
  39. #define ptype_beat_heart 0 // can heat heart
  40. #define ptype_request 1 // can request with need response
  41. #define ptype_response 2 // can response of the request
  42. #define ptype_indicater 3 // can request with no need response
  43. #define can_packet_priority_value 0x06
  44. #define can_packet_quick_packet_value 0x01
  45. //used when can recv
  46. static __inline__ uint16_t decoder_can_key(const void *buf){
  47. uint8_t *key_buf = (uint8_t *)buf;
  48. return key_buf[1]<<8 | key_buf[0];
  49. }
  50. //used when can send
  51. static __inline__ void encoder_can_key(uint8_t *buff, uint16_t key) {
  52. buff[0] = key & 0xFF;
  53. buff[1] = (key >> 8) & 0xFF;
  54. }
  55. int shark_can0_send_message(uint32_t can_id, const void*buff, int len);
  56. void shark_can0_route_message(uint32_t can_id, const void *buff, int len);
  57. int shark_can0_send_ext_message(uint32_t can_id, const void*buff, int len);
  58. void shark_can0_init(void);
  59. void shark_can0_reset(void);
  60. void shark_can0_deinit(void);
  61. void can_rx_poll(void);
  62. #endif /* _Shark_Can0_h__ */