1 /*
2  * Copyright (c) 2019 Alexander Wachter
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_SUBSYS_NET_L2_CANBUS_INTERNAL_H_
8 #define ZEPHYR_SUBSYS_NET_L2_CANBUS_INTERNAL_H_
9 
10 
11 #ifdef NET_CAN_USE_CAN_FD
12 #define NET_CAN_DL 64
13 #else
14 #define NET_CAN_DL 8
15 #endif/*NET_CAN_USE_CAN_FD*/
16 
17 /* Protocol control information*/
18 #define NET_CAN_PCI_SF 0x00 /* Single frame*/
19 #define NET_CAN_PCI_FF 0x01 /* First frame */
20 #define NET_CAN_PCI_CF 0x02 /* Consecutive frame */
21 #define NET_CAN_PCI_FC 0x03 /* Flow control frame */
22 
23 #define NET_CAN_PCI_TYPE_BYTE        0
24 #define NET_CAN_PCI_TYPE_POS         4
25 #define NET_CAN_PCI_TYPE_MASK        0xF0
26 #define NET_CAN_PCI_TYPE_SF   (NET_CAN_PCI_SF << NET_CAN_PCI_TYPE_POS)
27 #define NET_CAN_PCI_TYPE_FF   (NET_CAN_PCI_FF << NET_CAN_PCI_TYPE_POS)
28 #define NET_CAN_PCI_TYPE_CF   (NET_CAN_PCI_CF << NET_CAN_PCI_TYPE_POS)
29 #define NET_CAN_PCI_TYPE_FC   (NET_CAN_PCI_FC << NET_CAN_PCI_TYPE_POS)
30 
31 #define NET_CAN_PCI_SF_DL_MASK       0x0F
32 
33 #define NET_CAN_PCI_FF_DL_UPPER_BYTE 0
34 #define NET_CAN_PCI_FF_DL_UPPER_MASK 0x0F
35 #define NET_CAN_PCI_FF_DL_LOWER_BYTE 1
36 
37 #define NET_CAN_PCI_FS_BYTE          0
38 #define NET_CAN_PCI_FS_MASK          0x0F
39 #define NET_CAN_PCI_BS_BYTE          1
40 #define NET_CAN_PCI_ST_MIN_BYTE      2
41 
42 #define NET_CAN_PCI_FS_CTS           0x0
43 #define NET_CAN_PCI_FS_WAIT          0x1
44 #define NET_CAN_PCI_FS_OVFLW         0x2
45 
46 #define NET_CAN_PCI_SN_MASK          0x0F
47 
48 #define NET_CAN_FF_DL_MIN            (NET_CAN_CAN_DL)
49 
50 #define NET_CAN_WFT_FIRST            0xFF
51 
52 #define NET_CAN_BS_TIME K_MSEC(1000)
53 #define NET_CAN_A_TIME  K_MSEC(1000)
54 
55 #define NET_CAN_FF_CF_TIME K_MSEC(1)
56 
57 #define NET_CAN_STMIN_MAX            0xFA
58 #define NET_CAN_STMIN_MS_MAX         0x7F
59 #define NET_CAN_STMIN_US_BEGIN       0xF1
60 #define NET_CAN_STMIN_US_END         0xF9
61 
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65 
66 enum net_can_isotp_tx_state {
67 	NET_CAN_TX_STATE_UNUSED,
68 	NET_CAN_TX_STATE_RESET,
69 	NET_CAN_TX_STATE_WAIT_FC,
70 	NET_CAN_TX_STATE_SEND_CF,
71 	NET_CAN_TX_STATE_WAIT_ST,
72 	NET_CAN_TX_STATE_WAIT_TX_BACKLOG,
73 	NET_CAN_TX_STATE_FIN,
74 	NET_CAN_TX_STATE_ERR
75 };
76 
77 enum net_can_isotp_rx_state {
78 	NET_CAN_RX_STATE_UNUSED,
79 	NET_CAN_RX_STATE_RESET,
80 	NET_CAN_RX_STATE_FF,
81 	NET_CAN_RX_STATE_CF,
82 	NET_CAN_RX_STATE_FIN,
83 	NET_CAN_RX_STATE_TIMEOUT
84 };
85 
86 struct canbus_l2_ctx {
87 	struct canbus_isotp_tx_ctx tx_ctx[CONFIG_NET_PKT_TX_COUNT];
88 	struct canbus_isotp_rx_ctx rx_ctx[CONFIG_NET_PKT_RX_COUNT];
89 	struct k_mutex tx_ctx_mtx;
90 	struct k_mutex rx_ctx_mtx;
91 	struct k_sem tx_sem;
92 };
93 
94 #ifdef __cplusplus
95 }
96 #endif
97 
98 #endif /* ZEPHYR_SUBSYS_NET_L2_CANBUS_INTERNAL_H_ */
99