@@ -132,22 +132,42 @@ enum CANBAUD
132132 * res = rt_device_control(can_dev, RT_CAN_CMD_START, &cmd_arg);
133133 * RT_ASSERT(res == RT_EOK);
134134 *
135+ * #define CAN_RX_BATCH_SIZE 8
136+ *
135137 * while (1)
136138 * {
137139 * // Block and wait for the semaphore, which is released by the receive callback.
138140 * rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
139141 *
140- * // Read one frame of data from the CAN device's general message queue.
141- * rx_msg.hdr_index = -1;
142- * rt_device_read(can_dev, 0, &rx_msg, sizeof(rx_msg));
143- *
144- * // Print the received message's ID and data.
145- * rt_kprintf("Received a message. ID: 0x%x, Data: ", rx_msg.id);
146- * for (int i = 0; i < rx_msg.len; i++)
142+ * // Drain all pending frames in batches.
143+ * struct rt_can_msg rx_buf[CAN_RX_BATCH_SIZE];
144+ * rt_ssize_t read_size;
145+ * rt_size_t count;
146+ * do
147147 * {
148- * rt_kprintf("%02x ", rx_msg.data[i]);
148+ * for (rt_size_t i = 0; i < CAN_RX_BATCH_SIZE; i++)
149+ * {
150+ * rx_buf[i].hdr_index = -1;
151+ * }
152+ *
153+ * read_size = rt_device_read(can_dev, 0, rx_buf, sizeof(rx_buf));
154+ * if (read_size <= 0)
155+ * {
156+ * break;
157+ * }
158+ * count = (rt_size_t)(read_size / sizeof(rx_buf[0]));
159+ * for (rt_size_t i = 0; i < count; i++)
160+ * {
161+ * // Print the received message's ID and data.
162+ * rt_kprintf("Received a message. ID: 0x%x, Data: ", rx_buf[i].id);
163+ * for (int j = 0; j < rx_buf[i].len; j++)
164+ * {
165+ * rt_kprintf("%02x ", rx_buf[i].data[j]);
166+ * }
167+ * rt_kprintf("\n");
168+ * }
149169 * }
150- * rt_kprintf("\n" );
170+ * while(count == CAN_RX_BATCH_SIZE );
151171 * }
152172 * }
153173 *
0 commit comments