18
18
* **Large packets (>7 bytes)**: Split into multiple frames - first frame sent immediately,
19
19
* remaining frames sent during esp_isotp_poll() calls.
20
20
*
21
- * ## Basic Usage
22
- * ```c
23
- * esp_isotp_handle_t handle;
24
- * esp_isotp_new_transport(twai_node, &config, &handle);
25
- *
26
- * while (1) {
27
- * esp_isotp_poll(handle); // MUST call regularly!
28
- *
29
- * // Send data (non-blocking)
30
- * esp_isotp_send(handle, data, size);
31
- *
32
- * // Check for received data (non-blocking)
33
- * uint16_t received_size;
34
- * if (esp_isotp_receive(handle, buffer, sizeof(buffer), &received_size) == ESP_OK) {
35
- * // Complete message received
36
- * }
37
- *
38
- * vTaskDelay(pdMS_TO_TICKS(5));
39
- * }
40
- * ```
41
21
*/
42
22
43
23
#include "esp_err.h"
@@ -53,67 +33,155 @@ extern "C" {
53
33
*/
54
34
typedef struct esp_isotp_link_t * esp_isotp_handle_t ;
55
35
36
+ /**
37
+ * @brief ISO-TP receive callback function type
38
+ *
39
+ * Called when a complete message has been received successfully.
40
+ *
41
+ * @warning This callback executes in the same context as isotp_on_can_message(),
42
+ * which is typically ISR context. Keep callback execution time minimal
43
+ * and avoid blocking operations.
44
+ * @note The data pointer is only valid during the callback execution.
45
+ * Copy data immediately if needed beyond the callback scope.
46
+ *
47
+ * @param[in] handle ISO-TP handle that received the message
48
+ * @param[in] data Pointer to received data (valid only during callback)
49
+ * @param[in] size Size of received data in bytes
50
+ * @param[in] user_arg User argument provided during configuration
51
+ */
52
+ typedef void (* esp_isotp_rx_callback_t )(esp_isotp_handle_t handle , const uint8_t * data , uint32_t size , void * user_arg );
53
+
54
+ /**
55
+ * @brief ISO-TP transmit callback function type
56
+ *
57
+ * Called when a complete message has been transmitted successfully.
58
+ *
59
+ * @note Execution context depends on message type:
60
+ * - Single-frame: Called immediately from isotp_send() in caller's context
61
+ * - Multi-frame: Called from isotp_poll() in task context
62
+ * @note Keep callback execution time minimal to avoid affecting system performance.
63
+ *
64
+ * @param[in] handle ISO-TP handle that transmitted the message
65
+ * @param[in] tx_size Size of transmitted data in bytes
66
+ * @param[in] user_arg User argument provided during configuration
67
+ */
68
+ typedef void (* esp_isotp_tx_callback_t )(esp_isotp_handle_t handle , uint32_t tx_size , void * user_arg );
69
+
56
70
/**
57
71
* @brief Configuration structure for creating a new ISO-TP link
58
72
*/
59
73
typedef struct {
60
- uint32_t tx_id ; /*!< TWAI ID for transmitting ISO-TP frames */
61
- uint32_t rx_id ; /*!< TWAI ID for receiving ISO-TP frames */
74
+ uint32_t tx_id ; /*!< TWAI ID for transmitting ISO-TP frames (11-bit or 29-bit, auto-detected from value) */
75
+ uint32_t rx_id ; /*!< TWAI ID for receiving ISO-TP frames (11-bit or 29-bit, auto-detected from value) */
62
76
uint32_t tx_buffer_size ; /*!< Size of the transmit buffer (max message size to send) */
63
77
uint32_t rx_buffer_size ; /*!< Size of the receive buffer (max message size to receive) */
78
+ uint32_t tx_frame_pool_size ; /*!< Size of TX frame pool */
79
+
80
+ esp_isotp_rx_callback_t rx_callback ; /*!< Receive completion callback (NULL for polling mode) */
81
+ esp_isotp_tx_callback_t tx_callback ; /*!< Transmit completion callback (NULL to disable) */
82
+ void * callback_arg ; /*!< User argument passed to callbacks */
64
83
} esp_isotp_config_t ;
65
84
66
85
/**
67
- * @brief Create a new ISO-TP link
86
+ * @brief Create a new ISO-TP transport bound to a TWAI node.
87
+ *
88
+ * Allocates internal buffers, creates TX frame pool, registers TWAI callbacks
89
+ * and enables the provided TWAI node.
68
90
*
69
- * @param twai_node TWAI node handle
70
- * @param config Pointer to the configuration structure
71
- * @param[out] out_handle Pointer to store the created ISO-TP handle
91
+ * @param twai_node TWAI node handle to bind.
92
+ * @param config Transport configuration.
93
+ * @param[out] out_handle Returned ISO-TP transport handle.
72
94
* @return esp_err_t
73
- * - ESP_OK: Success
74
- * - ESP_ERR_INVALID_ARG: Invalid argument
75
- * - ESP_ERR_NO_MEM: Out of memory
95
+ * - ESP_OK on success
96
+ * - ESP_ERR_INVALID_ARG for invalid parameters
97
+ * - ESP_ERR_INVALID_SIZE for invalid buffer sizes
98
+ * - ESP_ERR_NO_MEM when allocation fails
99
+ * - Other error codes from TWAI functions
76
100
*/
77
101
esp_err_t esp_isotp_new_transport (twai_node_handle_t twai_node , const esp_isotp_config_t * config , esp_isotp_handle_t * out_handle );
78
102
79
103
/**
80
- * @brief Send data over an ISO-TP link (non-blocking)
104
+ * @brief Send data over an ISO-TP link (non-blocking, ISR-safe )
81
105
*
82
106
* Immediately sends first/single frame and returns. For multi-frame messages,
83
107
* remaining frames are sent during subsequent esp_isotp_poll() calls.
84
108
*
109
+ * @note This function is ISR-safe and can be called from interrupt context.
110
+ * @note TX completion callback timing:
111
+ * - Single-frame: Called immediately from isotp_send()
112
+ * - Multi-frame: Called from isotp_poll() when last frame is sent
113
+ *
85
114
* @param handle ISO-TP handle
86
115
* @param data Data to send
87
116
* @param size Data length in bytes
88
117
* @return
89
118
* - ESP_OK: Send initiated successfully
90
119
* - ESP_ERR_NOT_FINISHED: Previous send still in progress
91
- * - ESP_ERR_NO_MEM: Data too large for buffer
120
+ * - ESP_ERR_NO_MEM: Data too large for buffer or no space available
121
+ * - ESP_ERR_INVALID_SIZE: Invalid data size
122
+ * - ESP_ERR_TIMEOUT: Send operation timed out
92
123
* - ESP_ERR_INVALID_ARG: Invalid parameters
124
+ * - ESP_FAIL: Other send errors
93
125
*/
94
126
esp_err_t esp_isotp_send (esp_isotp_handle_t handle , const uint8_t * data , uint32_t size );
95
127
96
128
/**
97
- * @brief Extract a complete received message (non-blocking)
129
+ * @brief Send data over an ISO-TP link with specified TWAI ID (non-blocking, ISR-safe)
130
+ *
131
+ * Similar to esp_isotp_send(), but allows specifying a different TWAI ID for transmission.
132
+ * This function is primarily used for functional addressing where multiple nodes
133
+ * may respond to the same request.
134
+ *
135
+ * @note This function is ISR-safe and can be called from interrupt context.
136
+ * @note TX completion callback timing:
137
+ * - Single-frame: Called immediately from isotp_send_with_id()
138
+ * - Multi-frame: Called from isotp_poll() when last frame is sent
139
+ *
140
+ * @param handle ISO-TP handle
141
+ * @param id TWAI identifier to use for transmission (overrides configured tx_id)
142
+ * @param data Data to send
143
+ * @param size Data length in bytes
144
+ * @return
145
+ * - ESP_OK: Send initiated successfully
146
+ * - ESP_ERR_NOT_FINISHED: Previous send still in progress
147
+ * - ESP_ERR_NO_MEM: Data too large for buffer or no space available
148
+ * - ESP_ERR_INVALID_SIZE: Invalid data size
149
+ * - ESP_ERR_TIMEOUT: Send operation timed out
150
+ * - ESP_ERR_INVALID_ARG: Invalid parameters or ID exceeds maximum value
151
+ * - ESP_FAIL: Other send errors
152
+ */
153
+ esp_err_t esp_isotp_send_with_id (esp_isotp_handle_t handle , uint32_t id , const uint8_t * data , uint32_t size );
154
+
155
+ /**
156
+ * @brief Extract a complete received message (non-blocking, task context only)
98
157
*
99
158
* This function only extracts data that has already been assembled by esp_isotp_poll().
100
159
* It does NOT process incoming TWAI frames - that happens in esp_isotp_poll().
101
160
*
102
161
* Process: TWAI frames → esp_isotp_poll() assembles → esp_isotp_receive() extracts
103
162
*
163
+ * @warning This function is NOT ISR-safe and must only be called from task context.
164
+ * @note When using callback mode (rx_callback != NULL), received messages are
165
+ * automatically delivered via callback. This function should only be used
166
+ * in polling mode (rx_callback == NULL).
167
+ *
104
168
* @param handle ISO-TP handle
105
169
* @param data Buffer to store received data
106
170
* @param size Buffer size in bytes
107
171
* @param[out] received_size Actual received data length
108
172
* @return
109
173
* - ESP_OK: Complete message extracted and internal buffer cleared
110
174
* - ESP_ERR_NOT_FOUND: No complete message ready for extraction
175
+ * - ESP_ERR_INVALID_SIZE: Receive buffer overflow or invalid size
176
+ * - ESP_ERR_INVALID_RESPONSE: Invalid sequence number or protocol error
177
+ * - ESP_ERR_TIMEOUT: Receive operation timed out
111
178
* - ESP_ERR_INVALID_ARG: Invalid parameters
179
+ * - ESP_FAIL: Other receive errors
112
180
*/
113
181
esp_err_t esp_isotp_receive (esp_isotp_handle_t handle , uint8_t * data , uint32_t size , uint32_t * received_size );
114
182
115
183
/**
116
- * @brief Poll the ISO-TP link to process messages (CRITICAL - call regularly! )
184
+ * @brief Poll the ISO-TP link to process messages (CRITICAL - call regularly, task context only )
117
185
*
118
186
* This function drives the ISO-TP state machine. Call every 1-10ms for proper operation.
119
187
*
@@ -122,9 +190,13 @@ esp_err_t esp_isotp_receive(esp_isotp_handle_t handle, uint8_t *data, uint32_t s
122
190
* - Processes incoming TWAI frames and assembles complete messages
123
191
* - Handles flow control and timeouts
124
192
* - Updates internal state machine
193
+ * - Triggers TX completion callbacks for multi-frame messages
125
194
*
126
195
* Without regular polling: multi-frame sends will stall and receives won't complete.
127
196
*
197
+ * @warning This function is NOT ISR-safe and must only be called from task context.
198
+ * @note TX completion callbacks for multi-frame messages are triggered from this function.
199
+ *
128
200
* @param handle ISO-TP handle
129
201
* @return
130
202
* - ESP_OK: Processing successful
@@ -137,8 +209,9 @@ esp_err_t esp_isotp_poll(esp_isotp_handle_t handle);
137
209
*
138
210
* @param handle The handle of the ISO-TP link to delete
139
211
* @return
140
- * - ESP_OK: Success
212
+ * - ESP_OK: Success (or TWAI disable warning logged)
141
213
* - ESP_ERR_INVALID_ARG: Invalid argument
214
+ * - Other ESP error codes: TWAI node disable failed
142
215
*/
143
216
esp_err_t esp_isotp_delete (esp_isotp_handle_t handle );
144
217
0 commit comments