stdio scanf

New to XMOS and XCore? Get started here.
User avatar
rubenc
Active Member
Posts: 40
Joined: Fri Jul 22, 2011 2:31 pm

stdio scanf

Postby rubenc » Wed Sep 21, 2011 4:17 pm

Hi, I am having some trouble with the following code

int i
scanf ("%d", &i);

-- Throws an error--> parse error before '&'token

Since somehow it is not recognizing the pointer, I tried
int i
scanf ("%d", i);

but then
xrun: Program received signal ET_LOAD_STORE, Memory access exception.
[Switching to stdcore[1] hwthread 0]

wich is the correct way of using it?

Thankyou
User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm

Postby Bianco » Wed Sep 21, 2011 4:27 pm

Did you save it as a C file? XC does not support pointers.
User avatar
rubenc
Active Member
Posts: 40
Joined: Fri Jul 22, 2011 2:31 pm

Postby rubenc » Wed Sep 21, 2011 4:31 pm

nope.. it was xc

but thankyou for the tip I will try it out..
on the mean time I found this workaround which gives me the same result

printstr ("Enter your family name: ");
scanf ("%s",str);
printstr ("Enter your age: ");
scanf ("%s", str_d);
i = atoi(str_d);
printf ("Mr. %s , %d years old.\n",str,i);
printf ("You have entered %#x (%d).\n",i,i);

Enter your family name: ruben
Enter your age: 30
Mr. ruben , 30 years old.
You have entered 0x1e (30).

Thankyou!
User avatar
rubenc
Active Member
Posts: 40
Joined: Fri Jul 22, 2011 2:31 pm

Postby rubenc » Mon Sep 26, 2011 1:00 pm

Thankyou for the previous info.
Now I am experiencing another trouble eith scanf

scanf is blocking somehow the mac_rx function.

I test this behaviour in a default example so that anyone can reproduce it.
sc_ethernet --> app_ethernet_demo3

In that example it is running as:
on stdcore[2]: ethernet_server
on stdcore[0]: demo

while adding:

on stdcore[1]: menu

with menuu defined as:
void menu()
{
char option[20];
while(1)
{
scanf ("%s",option);
printf ("Echo: %s",option);
}
}



Notice that menu function is running in another core and another thread... the question is why is it affecting the other mac_rx thread?

I am pinging the board and by adding this scanf function. it will just respond to one ping everythime something is entered + <enter>

is there some other way to do this that it is not blocking? however it shold not be like that right?
User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm

Postby Bianco » Mon Sep 26, 2011 1:06 pm

The default stdout/stdin uses JTAG to communicate with the host.
That means that all threads in the system can be halted at for the ethernet stuff inconvenient times.
You can redirect stdout/in to a serial interface to avoid that.
I believe this is your issue.
User avatar
rubenc
Active Member
Posts: 40
Joined: Fri Jul 22, 2011 2:31 pm

Postby rubenc » Mon Sep 26, 2011 1:42 pm

Then I would need to build some hardware to use a serial port right? :-( I wanted to avoid that. but if there is no other option I will give it a try.

Then I will add a UART to the project and...
Bianco wrote: You can redirect stdout/in to a serial interface to avoid that.
how can I redirect stdin/out to that serial interface? :-S

Thankyou!
User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm

Postby Bianco » Mon Sep 26, 2011 1:50 pm

User avatar
rubenc
Active Member
Posts: 40
Joined: Fri Jul 22, 2011 2:31 pm

Postby rubenc » Mon Sep 26, 2011 2:40 pm

ok then. so I need to redefine

int _write(int fd, const char buf[], ___size_t count); // syscall.h
for printstr and printf I guess.

and then I asume that for scanf
it will be calling
int _read(int fd, char buf[], unsigned count); // syscall.h

am I right? But what about how should the new function behave?
I think that fd = filedescriptor is not required that the buff array has the content and size at size. but
I am concern on the return values? what is it expected?

Has anyone implemented something like this? because I havent find any project or example using it, is there another way? am I kind of crazy trying to do this? or is it really that nobody else finds it usefull enough to do it?
am I not going in the right direction?
User avatar
rubenc
Active Member
Posts: 40
Joined: Fri Jul 22, 2011 2:31 pm

Postby rubenc » Mon Sep 26, 2011 2:45 pm

will something like this do the trick?

int _write(int fd, const char buf[], ___size_t count)
{
send_uart(buff,count);
// udp_send(buff,count); -- or ethernet redirection
}

int _read(int fd, char buf[], unsigned count)
{
while(uart_empty);
count = receive_uart(buff);
return count;
}

## pseudocode. I still need to find the HW to test the stdout redirection to uart.
User avatar
rubenc
Active Member
Posts: 40
Joined: Fri Jul 22, 2011 2:31 pm

Postby rubenc » Tue Sep 27, 2011 5:49 pm

I received the recomendation of using sc_xlog instead. I think I will give it a try.
It seems that for printstr is working correctly. only the scanf is missing, but for the moment I will continue in a different way.
since I am using XC-3 I will use the additional ethernet connector for debuging purposes.

But it will be nice to know if someone completes the xlog for input also.

Thankyou.

Who is online

Users browsing this forum: No registered users and 4 guests