Hi,
I am working on UART code using MQX now. I went the user guide and the example code. I am still not clear on how should i initialise the uart function and transmit or received a set of data. I know the following steps are require for a UART to work
1) Initailise the UART port with required baud rate, parity and stop bits
3) Initialise the interrupt register to enable the UART ISR
4) Transmit the data to the UART TX register to transmit the data
5) Receive the data from UARTRX register to receive the data.
6) Change the baud rate if required.
But i am not clear on how sholud this be done using MQX :smileysad: . Please help me in explaining the steps i need to follow for writing code using MQX. I would be more happy if the steps are explained using example code. I am struck up with this module and looking forward for help at the earliest.
Thanks and Regards
Sai Bhargavi
Hi JuroV,
Thankyou for your reply. This mail helped me a lot in learning UART code.. After going thorugh the files i wrote an UART0 code as below
extern void main_task(uint_32);
extern void uart0_task(uint_32);
#define UART0_TASK 11
TASK_TEMPLATE_STRUCT MQX_template_list[] =
{
{ 10, main_task, 2000, 9, "Main",MQX_AUTO_START_TASK, 0, 0},
{ UART0_TASK, uart0_task, 2000, 8, "uart0",0, 0, 0},
{ 0, 0, 0, 0, 0, 0,0, 0}
};
void main_task
(
uint_32 initial_data
)
{
_task_id uart0_task_id;
uart0_task_id = _task_create(0, UART0_TASK, 0);
if(uart0_task_id == MQX_NULL_TASK_ID)
{
printf("\n could not create uart0 task \n");
_mqx_exit(0);
}
else
{
}
}
void uart0_task(uint_32 initial_data)
{
pointer uart0_ptr;
char Z, uart0[200];
uint_16 i = 0;
volatile boolean result=0xdeadbeef;
uart0_ptr = (pointer)fopen("ittya:",BSP_DEFAULT_IO_OPEN_MODE);
if(uart0_ptr == NULL)
{
printf("cannot open ittya device \n");
}
else
{
printf("device opened succesfully \n");
fflush(stdout);
}
while(1)
{
read(uart0_ptr,(pointer)&Z,1);
uart0[i] = Z;
//dont know why this checking is done ... please explain me this
if(result = fstatus(uart0_ptr))
{
}
else
{
if(uart0[i] == 0x31)
{
if(i ==45)
{
i =0;
}
else
{
i++;
}
write(uart0_ptr,(pointer)"\n",1);
write(uart0_ptr,(pointer)"Y",1);
}
else
{
}
}
}
}
After i flash and run the above code it should send "Y" to the comm port if i press 1 on the key board but it sends "1Y" as output instead . I dont know why is this happening ?
Looking forward for your suggestion in sorting out this :smileysad:
Thanks and Regards
Sai Bhargavi
Hi. Please use code tag when you post a code in the discussion.
fstatus returns TRUE if you have unread character in the buffer, FALSE otherwise.
So it would be smarter to call read() after fstatus...
Are you sure that that '1' pressed is not displayed also in your terminal? No way to explain such '1'...
UART is initialized in init_bsp.c file and the init function takes parameters from init_uartX.c.
After that, your UART is initialized and ready to use. The standard output stream is, usually*, mapped to UART0- so you can printf() to the UART.
UART driver code actually support two drivers: interrupt driven and polled. Use user_config.h to select ttyX: (polled) or ittyX: (interrupt).
Use file search function in Windows to search for these files within MQX tree.
*this can be configured in user_config.h
I will write this to the next reply.
If you want to read RX register and so on, you can do it- the best start is to look how does UART driver (mqx/source/io/serial) handle with registers. You will not open driver in init_bsp.h, but initialize and read UART in your own procedures.
In my point of view, I dont see any benefit in writing what was already written and is tested, but you are a customer.