I am currently trying to send a test message from board 1 (bl475e-iot01a) to board 2(ublox-EVK-odin-w2) and then send that message from board 2 to my PC. All of this is through UART. I am also trying to send an acknowledgement byte from board 2 to board 1, so that board 1 only sends a message to board 2 when board 2 has finished reading and sending the previous message to the pc. The problem I am facing is that after 4 to 5 messages are successfully sent to the pc, no more values gets sent to the PC. I think what is happening is that board 1 ends up waiting for the acknowledgment byte from board 2, whereas board 2 is waiting for the message from board 1 (deadlock). I am not sure how to solve this, does anyone see any possible issues in my code?
Note: I have added several debugging print statements to the code for both boards. I do not intend to keep them, and they are there only to help me debug the code. (The same problem still exists even with the debugging statements removed. )
Board 1 code:
#define BUFF_SIZE 25
#define UART_BAUD_RATE 115200
#define MODE 1 //0 for UART to PC, 1 for UART to ODIN
#if MODE == 0
#define TX_PIN PB_6
#define RX_PIN PB_7
#elif MODE == 1
#define TX_PIN D1
#define RX_PIN D0
#endif
/* Includes */
#include "mbed.h"
#include <iostream>
#include<string>
char* mystrcat( char* dest, char* src )
{
while (*dest) dest++;
while ((*dest++ = *src++) != '\0');
return --dest;
}
int main() {
Serial uart_odin(TX_PIN, RX_PIN, UART_BAUD_RATE);
uart_odin.set_flow_control(mbed::SerialBase::Disabled);
uart_odin.format(8, SerialBase::None, 1);
Serial uart_debug(PB_6, PB_7, UART_BAUD_RATE);
uart_debug.set_flow_control(mbed::SerialBase::Disabled);
uart_debug.format(8, SerialBase::None, 1);
//set test message and intiialize buffer
char test_msg[] = "-1000,-2000,-3000";
char buffer[BUFF_SIZE * (sizeof(test_msg) + 10)]; //+10 needed to make space for the count and \r\n
memset(buffer, 0, sizeof(buffer));
//intialize variables
int count = 0;
int global_count = 0;
char * p = buffer;
char count_str[10];
bool first_iteration = true;
char transmit_check = '0';
while(1) {
//saves messages to buffer
while(count != BUFF_SIZE){
//Append test message to buffer
p = mystrcat(p, test_msg);
*p = ' '; //add space for the count
//convert global count to a string
std::string tmp = std::to_string(global_count);
char *global_count_str = (char *) tmp.c_str();
//append global count to the buffer as well as new line characters
p = mystrcat(p, global_count_str);
*p = '\r';
*(++p) = '\n';
//incriment count and global count
count++;
global_count++;
}
//loop until ODIN board send a transmit check signal ('1')
while(transmit_check == '0' && first_iteration == false){
uart_debug.puts("start loop\r\n");
uart_debug.printf("readable: %d\r\n", uart_odin.readable());
if(uart_odin.readable()){
transmit_check = uart_odin.getc();
}
uart_debug.printf("transmit check: %c\r\n", transmit_check);
}
//send buffer data to ODIN
uart_debug.puts("sending buffer to odin\r\n");
uart_odin.puts(buffer);
uart_debug.puts("sending to odin complete\r\n");
//reset variables for new iteration
first_iteration = false;
transmit_check = '0';
p = buffer; //reser the pointer to the start of the buffer
count = 0;
memset(buffer, 0, sizeof(buffer));
}
}
Board 2 code:
#include "mbed.h"
#include "platform/mbed_thread.h"
#include <cstdint>
#define UART_BAUD_RATE_L475 115200
#define UART_BAUD_RATE_PC 1843200
#define BUFF_SIZE 25
#define MESSAGE_SIZE 30 //includes sensor value and count
int main()
{
Serial uart_l475(D1, D0, UART_BAUD_RATE_L475);
uart_l475.format(8, SerialBase::None, 1);
uart_l475.set_flow_control(mbed::SerialBase::Disabled);
Serial uart_pc(USBTX, USBRX, UART_BAUD_RATE_PC);
uart_pc.format(8, SerialBase::None, 1);
uart_pc.set_flow_control(mbed::SerialBase::Disabled);
char buffer[BUFF_SIZE * MESSAGE_SIZE];
char recieved_char;
char * p_buffer = buffer;
while (true) {
uart_pc.printf("readable outside loop: %d\r\n", uart_l475.readable());
if (uart_l475.readable()){
uart_pc.printf("readable inside loop: %d\r\n", uart_l475.readable());
uart_pc.puts("reading from l475 \r\n");
while((recieved_char = uart_l475.getc()) != '\0' && uart_l475.readable()){
*p_buffer = recieved_char;
p_buffer ++;
}
*p_buffer = '\0';
uart_pc.puts("completed reading buffer from l475\r\n");
uart_pc.puts("sending buffer data to PC\r\n");
uart_pc.puts(buffer);
uart_pc.puts("completed sending buffer data to PC\r\n");
p_buffer = buffer; //make the pointer point to the start of the buffer
uart_pc.puts("sending aknolwedgement bit to l475 \r\n");
uart_l475.putc('1');
uart_pc.puts("aknowledgement bit sent complete \r\n");
}
}
}