33NodeControllerCore::NodeControllerCore (){
44}
55
6+
67twai_message_t NodeControllerCore::create_message (uint32_t id, uint64_t *data) {
78 twai_message_t message;
89
@@ -19,10 +20,11 @@ bool NodeControllerCore::Init(std::function<void(uint8_t nodeID, uint16_t messag
1920 this ->onMessageReceived = onMessageReceived;
2021 this ->nodeID = nodeID;
2122
22- // Initialize configuration structures using macro initializers
23+ // Initialize configuration structures using macro initializers
2324 twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT ((gpio_num_t )TX_PIN ,
2425 (gpio_num_t )RX_PIN ,
2526 TWAI_MODE_NORMAL );
27+ // TODO: deside on the timing config
2628 twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS ();
2729 twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL ();
2830
@@ -43,6 +45,7 @@ bool NodeControllerCore::Init(std::function<void(uint8_t nodeID, uint16_t messag
4345 }
4446
4547 // Reconfigure alerts to detect frame receive, Bus-Off error and RX queue full states
48+ // Not really sure what most of this means
4649 uint32_t alerts_to_enable = TWAI_ALERT_RX_DATA | TWAI_ALERT_ERR_PASS | TWAI_ALERT_BUS_ERROR | TWAI_ALERT_RX_QUEUE_FULL | TWAI_ALERT_BUS_OFF ;
4750 if (twai_reconfigure_alerts (alerts_to_enable, NULL ) == ESP_OK ) {
4851 Serial.println (" CAN Alerts reconfigured" );
@@ -51,11 +54,11 @@ bool NodeControllerCore::Init(std::function<void(uint8_t nodeID, uint16_t messag
5154 return false ;
5255 }
5356
57+ // Create tx_queue and rx_queue
5458 tx_queue = xQueueCreate (TX_QUEUE_LENGTH , sizeof (twai_message_t ));
55-
5659 rx_queue = xQueueCreate (RX_QUEUE_LENGTH , sizeof (twai_message_t ));
5760
58- // Create task to receive messages to rx_queue
61+ // Start tasks
5962 xTaskCreate (this ->start_receive_to_rx_queue_task ,
6063 " start_rx_task_impl" ,
6164 2048 ,
@@ -78,7 +81,7 @@ bool NodeControllerCore::Init(std::function<void(uint8_t nodeID, uint16_t messag
7881 30 ,
7982 NULL );
8083
81-
84+ // Return true since everything is successful
8285 return true ;
8386
8487}
@@ -89,7 +92,7 @@ void NodeControllerCore::transmit_tx_queue(void *queue) {
8992
9093 while (1 ){
9194 if (xQueueReceive (*(QueueHandle_t *) queue, &message, RX_TX_BLOCK_TIME ) == pdTRUE){
92-
95+ // If there is a message in the queue, try to transmit it
9396 if (twai_transmit (&message, 2000 ) == ESP_OK ) {
9497 Serial.println (" Message queued for transmission" );
9598 } else {
@@ -111,7 +114,6 @@ void NodeControllerCore::receive_to_rx_queue() {
111114 Serial.println (" Receive to rx queue task created" );
112115
113116 while (1 ){
114-
115117 // Check if alert happened
116118 uint32_t alerts_triggered;
117119 twai_read_alerts (&alerts_triggered, 0 );
@@ -139,15 +141,18 @@ void NodeControllerCore::receive_to_rx_queue() {
139141 }
140142
141143 bool receive = false ;
144+
145+ // TODO: Not sure if this do while loop for receiving messages is the best way to do it, but seems to be working right now
142146 do {
147+ // Try to receive a message
143148 receive = twai_receive (&message, RX_TX_BLOCK_TIME );
144-
145149 if (receive == ESP_OK ) {
150+ // If message is received succesfully, put it in the rx_queue
146151 xQueueSend (this ->rx_queue , &message, 0 );
147152 } else if (receive == ESP_ERR_INVALID_STATE || receive == ESP_ERR_INVALID_ARG ) {
148153 Serial.println (" Failed to receive message" );
149154 }
150- } while (receive != ESP_ERR_TIMEOUT );
155+ } while (receive != ESP_ERR_TIMEOUT ); // If the message is not received, try again, not sure if this is the best way to do it
151156 }
152157}
153158
@@ -160,10 +165,18 @@ void NodeControllerCore::rx_queue_event() {
160165
161166 while (1 ){
162167 if (xQueueReceive (rx_queue, &message, RX_TX_BLOCK_TIME ) == pdTRUE){
168+ // If there is a message in the rx_queue
163169 uint64_t data = 0 ;
164170 uint8_t nodeID = 0 ;
165171 uint16_t messageID = 0 ;
172+
173+ // Extract the data from the message data
166174 memcpy (&data, message.data , 8 );
175+
176+ // Extract the nodeID and messageID from the message identifier with format:
177+ // 8bit node ID, 16bit message ID, 5bit reserved
178+ // NNMMMMRR
179+ // (NNNN)(NNNN)(MMMM)(MMMM)(MMMM)(MMMM)(RRRR)(R000)
167180 nodeID = message.identifier >> 21 ;
168181 messageID = message.identifier >> 5 ;
169182
@@ -175,20 +188,28 @@ void NodeControllerCore::rx_queue_event() {
175188 Serial.print (" Data: " );
176189 Serial.println (data, HEX );
177190
191+ // Call the onMessageReceived function in the device code
178192 this ->onMessageReceived (nodeID,messageID, data);
179193 }
180194 }
181195}
182196
183197void NodeControllerCore::sendMessage (uint16_t messageID, uint64_t *data) {
198+ // Create a CAN Bus ID with format:
199+ // 8bit node ID, 16bit message ID, 5bit reserved
200+ // NNMMMMRR
201+ // (NNNN)(NNNN)(MMMM)(MMMM)(MMMM)(MMMM)(RRRR)(R000)
184202 uint32_t id = 0 ;
185203 id = ((this ->nodeID << 24 ) | (messageID << 8 )) >> 3 ;
204+
186205 Serial.print (" Sending message with Node ID: " );
187206 Serial.print (nodeID, HEX );
188207 Serial.print (" Message ID: " );
189208 Serial.print (messageID, HEX );
190209 Serial.print (" Data: " );
191210 Serial.println (*data, HEX );
211+ // Create a CAN Bus message
192212 twai_message_t message = create_message (id, data);
213+ // Send the message to the tx_queue
193214 xQueueSend (tx_queue, &message, portMAX_DELAY);
194215}
0 commit comments