So I'm looking to delay a task that calls the asynchronous int USART_RTOS_Receive(usart_rtos_handle_t *handle, uint8_t *buffer, uint32_t length, size_t *received) method until the received count equals the desired length.
The function returns immediately and received tells you how many characters were read but I need a blocking version or a version that delays the task until the read is complete because the next part of the task depend on the data being read.
Is there a built in way to do this or what would the best way to go about implementing this be?
Hello Marc Bressler,
I think there is an easiest way to receive certain amount of characters by using the UART FIFOS. Please refer the user manual https://www.nxp.com/docs/en/user-guide/UM10914.pdf chapter 24 sections 24.6.10 to 24.6.19.
As you can see in the image below you can configure the RxFIFO to trigger an interrupt when you reach the desire length which can go from 1 character to 16 characters.
Hope it helps!
Victor.
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hello, this method definitely seems like the right direction for my use case. Do you know if it plays nice with RTOS tasks?
Hello Marc Bressler,
Yes you shouldn't have any problems on working with this. I recommend you to take a look into the example lpc_usart_interrupt_transfer of the SDK. Although this example doesn't use RTOS is definitely a good starting point for your project.
Hope it helps!
Victor.
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
So came up with the possible solution but I'm concerned it's inefficient. To give you some context, the task reads from the RS485 till it finds the start byte (0x53) then reads the next 4 bytes to get the LSB and MSB of the message size, with that in hand it reads the whole message.
Any feedback would be appreciated.
void usart_receive_task(void* pvParameters) {
int error;
size_t n;
NVIC_SetPriority(FLEXCOMM2_IRQn, 5); // Still not 100% clear what this is doing, think it's setting the FC2 interrupt to a high enough priority (low number is higher priority)
do {
// Read till start bit
do {
USART_RTOS_Receive(RS485_2_rtos_handle, receive_buffer, 1, &n);
} while ((n != 1) && (receive_buffer[0] != 0x53));
// Read next four bits to get message size
size_t bits_for_address = 4;
do {
USART_RTOS_Receive(RS485_2_rtos_handle, receive_buffer + 1 + (4 - bits_for_address), bits_for_address, &n);
bits_for_address -= n;
} while (bits_for_address > 0);
uint16_t msg_size = receive_buffer[2] + (receive_buffer[3] << 8);
// Read rest of message same way
} while (error != kStatus_Success);
}
I suggest your use the FreeRTOS features for this Message buffers and blocking reads:
Hello Marc Bressler,
In order to give you a more accurate support could you please tell me which MCU are you using for this?
Regards,
Victor.
MCUXpresso v 10.2.0 running the SDK 2.4.1 for the LPCXpresso54114