Line data Source code
1 : #ifndef CANLIB_COMMON_H
2 : #define CANLIB_COMMON_H
3 :
4 : #include <stdbool.h>
5 : #include <stdint.h>
6 :
7 : #include "can.h"
8 : #include "message_types.h"
9 :
10 : #ifndef LINT
11 : #include "common.h"
12 : #else
13 : #define w_assert(statement)
14 : #endif
15 :
16 : #ifndef CANLIB_DYNAMIC_BOARD_ID
17 :
18 : #ifndef BOARD_TYPE_UNIQUE_ID
19 : #warning BOARD_TYPE_UNIQUE_ID not defined, please set that up in project
20 : #define BOARD_TYPE_UNIQUE_ID 0
21 : #endif
22 :
23 : #ifndef BOARD_INST_UNIQUE_ID
24 : #warning BOARD_INST_UNIQUE_ID not defined, please set that up in project
25 : #define BOARD_INST_UNIQUE_ID 0
26 : #endif
27 :
28 1 : static inline uint32_t build_sid(can_msg_prio_t prio, can_msg_type_t msg_type, uint8_t metadata) {
29 1 : w_assert(((uint32_t)prio & 0xfffffffc) == 0);
30 1 : w_assert(((uint32_t)msg_type & 0xffffff80) == 0);
31 1 : w_assert(((uint32_t)BOARD_TYPE_UNIQUE_ID & 0xffffffc0) == 0);
32 1 : w_assert(((uint32_t)BOARD_INST_UNIQUE_ID & 0xffffffc0) == 0);
33 :
34 1 : return (((uint32_t)prio) << 27) | (((uint32_t)msg_type) << 20) |
35 1 : (((uint32_t)BOARD_TYPE_UNIQUE_ID) << 14) | (((uint32_t)BOARD_INST_UNIQUE_ID) << 8) |
36 : metadata;
37 : }
38 :
39 : #else
40 :
41 : extern uint8_t board_type_unique_id;
42 : extern uint8_t board_inst_unique_id;
43 :
44 : static inline uint32_t build_sid(can_msg_prio_t prio, can_msg_type_t msg_type, uint8_t metadata) {
45 : w_assert(((uint32_t)prio & 0xfffffffc) == 0);
46 : w_assert(((uint32_t)msg_type & 0xffffff80) == 0);
47 : w_assert(((uint32_t)board_type_unique_id & 0xffffffc0) == 0);
48 : w_assert(((uint32_t)board_inst_unique_id & 0xffffffc0) == 0);
49 :
50 : return (((uint32_t)prio) << 27) | (((uint32_t)msg_type) << 20) |
51 : (((uint32_t)board_type_unique_id) << 14) | (((uint32_t)board_inst_unique_id) << 8) |
52 : metadata;
53 : }
54 :
55 : #endif
56 :
57 : // SID Handling Functions
58 :
59 2 : static inline can_msg_type_t get_message_type(const can_msg_t *msg) {
60 2 : w_assert(msg);
61 :
62 2 : return (can_msg_type_t)((msg->sid >> 20) & 0x7f);
63 : }
64 :
65 : static inline uint8_t get_board_type_unique_id(const can_msg_t *msg) {
66 : w_assert(msg);
67 :
68 : return (msg->sid >> 14) & 0x3f;
69 : }
70 :
71 : static inline uint8_t get_board_inst_unique_id(const can_msg_t *msg) {
72 : w_assert(msg);
73 :
74 : return (msg->sid >> 8) & 0x3f;
75 : }
76 :
77 1 : static inline uint8_t get_message_metadata(const can_msg_t *msg) {
78 1 : w_assert(msg);
79 :
80 1 : return msg->sid & 0xff;
81 : }
82 :
83 : // Timestamp handle functions
84 1 : static inline void write_timestamp(uint16_t timestamp, can_msg_t *output) {
85 1 : w_assert(output);
86 :
87 1 : output->data[0] = (timestamp >> 8) & 0xff;
88 1 : output->data[1] = (timestamp >> 0) & 0xff;
89 1 : }
90 :
91 : static inline uint16_t get_timestamp(const can_msg_t *msg) {
92 : w_assert(msg);
93 :
94 : return ((uint16_t)msg->data[0] << 8) | msg->data[1];
95 : }
96 :
97 : // Dynamic board ID specific function
98 : #ifdef CANLIB_DYNAMIC_BOARD_ID
99 :
100 : static inline void set_board_id(uint8_t board_type_id, uint8_t board_inst_id) {
101 : w_assert(((uint32_t)board_type_id & 0xc0) == 0);
102 : w_assert(((uint32_t)board_inst_id & 0xc0) == 0);
103 :
104 : board_type_unique_id = board_type_id;
105 : board_inst_unique_id = board_inst_id;
106 : }
107 :
108 : #endif
109 :
110 : #endif
|