Hi, many thanks for your answer.
I choice the first street so I use fread with one char every time.
I NOT try (for now) to overwrite interrupt.
Now my code is working, I'm setting up a timer (for timeout) after that I will modify this post and add the code.
I have a question about your "simple" code :
Do
{
bytesRead = fread(inBuffer,1,1,fd);
_time_get_elapsed_ticks(&lastByteTime);
memcpy(&inPacket[inPacketSize], inBuffer, bytesReaded);
inPacketSize += bytesReaded;
_time_get_elapsed_ticks(&now);
} while (_time_diff_milliseconds(&now, &lastByteTime, &overflow) <250);
You, before all, try to read something with "fread" after that you store a timestamp, copy in memory the byte and store another timestamp, right?
but you in while (_time_diff_milliseconds(&now, &lastByteTime, &overflow) <250); are using the total time used from CPU to store data, NOT the time between two byte, isn't it? I'm sorry maybe I'm wrong but I understand in this way....
And another question: I suppose to move the instruction in this way:
while (1)
Do
{ _time_get_elapsed_ticks(&lastByteTime);
bytesRead = fread(inBuffer,1,1,fd);
memcpy(&inPacket[inPacketSize], inBuffer, bytesReaded);
inPacketSize += bytesReaded;
_time_get_elapsed_ticks(&now);
} while (_time_diff_milliseconds(&now, &lastByteTime, &overflow) <250);
lwsempost(&lwUartSem)
}
So in this way I have the time between two byte BUT fread(inBuffer,1,1,fd); is a blocking function so on the last byte, my do while cycle will stuck on last byte and never reach the other instruction (like the: memcpy inPacketSize and the major important time_get_elapsed_ticks that store the time elapsed for the exit-check-Do/while-cycle) and never the instruction } while (_time_diff_milliseconds... that check if the time are elapsed... or not?
(I add another cycle only for post the LW-sempahore)
THE FIRST VERSION of my code, that not use timeout is that:
This version stop rx when a CR/LF occours (ascii char 0x0D)
It is only a demo version:
-> second packet will overwrite and lenght wil not reset
-> improvement #1 : best way is to use Queue but now I do not know how to do them in MQX (I never tryied... Now I will try do do them!!!)
-> improvement #2 : use timer to set a timeout instead "stop char" 0x0D and store the packet in a MQX-Queue (improvement #1)
This is the code (please do not smile on my name of ittyb...) for any critical / suggestion / advice feel free to write them and I will answer: Maybe there is a better way and I will learn a new method (very probably that my code need changes...)
main.c:
#include <mqx.h>
#include <bsp.h>
#include <sem.h>
#include "main.h"
#if ! BSPCFG_ENABLE_IO_SUBSYSTEM
#error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined non-zero in user_config.h. Please recompile BSP with this option.
#endif
#ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED
#error This application requires BSP_DEFAULT_IO_CHANNEL to be not NULL. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero in user_config.h and recompile BSP with this option.
#endif
#define MaxSci1_CHANNEL "ittyb:"
LWSEM_STRUCT lwsemUartRX;
typedef struct rx_str_template
{
char data_buf_RX[256];
_mqx_uint nCharRx;
_mqx_uint ConteggioInternoCharRicevuti;
} RX_STR, * RX_STR_PTR;
RX_STR rx_str;
void main_task (uint32_t initial_data){
_task_id task_id;
uint32_t result;
result = _lwsem_create(&lwsemUartRX, 0);
if (result != MQX_OK) {
printf("\nCreating lwsemUartRX failed: 0x%X", result);
_task_block();
}
task_id = _task_create(0,READ_TASK, 0);
printf("\nread_task created, id 0x%lX", task_id);
while(1)
{
_lwsem_wait(&lwsemUartRX);
printf("\nData:\n ");
int i =0;
for(i=0;i<rx_str.nCharRx;i++){
printf("%c",rx_str.data_buf_RX[i]);
}
printf("\n");
}
_task_block();
}
void read_task(uint32_t initial_data){
MQX_FILE_PTR MaxSci1_dev = NULL;
bool disable_rx = FALSE;
rx_str.nCharRx = 0;
MaxSci1_dev = fopen( MaxSci1_CHANNEL, NULL );
if (MaxSci1_dev == NULL) {printf("\nError opening Sci1");}
else{printf("\nSci1 opned!");}
while (TRUE) {
fread(&(rx_str.data_buf_RX[rx_str.nCharRx]), 1, 1, MaxSci1_dev);
if(rx_str.data_buf_RX[rx_str.nCharRx] == 0x0D){
_lwsem_post(&lwsemUartRX);
}
rx_str.nCharRx++;
}
}
I suppose that is not much inherent in this topic, so I open a new thred for speach about it, here:
what is the best way for Timeout on UART RX with MQX?
Many thanks
Massimiliano