Hello guys,
I am debugging a system based on: BitCloud_XMEGA_1_14_0; AVR STUDIO 5; Target IC: ATxmeag256A3; Debugger JTAGICE3;
I need to use the UART port to send data reading commands to a RS485 sensor and gets it readings back. To do so, the following code is used.
However, by using the Debugger JTAGICE3 and hook up a Oscilloscope to watch the UART signals, the coding inside the ATOMIC_BLOCK and WRITE_USART is not executed right away as expected..
It's executed later after my coding that waits for response from the RS485 sensor.
See the following coding for details:
////////////////////////////////////////////////Coding for read in RS485 sensors data//////////////////////////////////////////////////////
case SOIL_SENSOR1:
{
// Use atomic block to force execution: Coding for sending the command to the RS485 sensor in Black bold:
ATOMIC_BLOCK(ATOMIC_RESTORESTATE )
{
PORTD_OUT |= 0x20;
memset(cmd,0x00,sizeof(cmd));
cmd[0]=0x15;//0x15;//fixture address
cmd[1]=0x03;
cmd[2]=0x00;
cmd[3]=0x00;
cmd[4]=0x00;
cmd[5]=0x01;
cmd[6]=0x09;
cmd[7]=0x10;
rs485_recv_flag=2;
//ATOMIC_BLOCK(ATOMIC_FORCEON){
//sendRS485Message(cmd,8);
rs485_wait_steady=1;
//ATOMIC_BLOCK(ATOMIC_FORCEON){
WRITE_USART(&usartDescriptor1,cmd,8); // It's not executed right away although I have added it into the ATOMIC_BLOCK!
}
// End of atomic block and now we wait for 485 to response.
// However, by using the Debugger JTAGICE3 and hook up a Oscilloscope to watch the UART signals, the coding defined by ATOMIC_BLOCK is not executed right away.
// It's executed later though but my following coding that waits for the response from the RS485 sensor can't get readings:
// Coding waiting response from RS485:
TimerValue = HAL_GetSystemTime();
while(rs485_recv_flag!=0)
{
if((HAL_GetSystemTime()-TimerValue)>=200)
break;
}
flagValue=rs485_recv_flag;
if((sensorSoil[0]==parameterData[0])&&(sensorSoil[1]==parameterData[1]))
{
dealWithRS485Data(2);
break;
}
}
break;
// WRITE_USART is not executed!
// WRITE_USART command is executed later after Coding waiting response from RS485 therefore I can't read data.
////////////////////////////////////////////////Above Coding is for read in RS485 sensors data//////////////////////////////////////////////////////
Have any clue? I think this problem might be related to stack structure of the Bitcloud, but not sure. How to force the WRITE_USART to be executed right away so that the coding waiting for response from the sensor can get readings?
Thanks,