Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

prob. in accessing UART

Altera_Forum
Honored Contributor II
1,262 Views

Hello ... 

i have written the following prog. to access my uart in my design 

it is named as "uart_0" in the SOPC builder.... 

while running this prog. in NIOSII IDE i was simulataneously running 

a c-prog in the PC which continuosly transmits character 't'. 

 

i even chkd the o/p from the com port to which it was sending the character……it was correctly sending the data 't'. 

but the prog. On the board is giving the wrong results... 

can anyone plz tell what is wrong in my prog.... 

# include <stdio.h># include <string.h># include "unistd.h"# include "nios2.h"# include "system.h"# include "io.h"# include "altera_avalon_uart.h" 

int main () 

 

int i,j,k=0; 

char *ch; 

 

i = open("/dev/uart_0",2); //i==file descriptor 

printf("file opened ::%d",i); 

 

if (i!=-1) 

 

while(k!=20) //i am just reading continuosly for 20times 

j=read(i,ch,2); 

printf("char read::%c and j=%d\n",*ch,j); 

k++; 

}  

if (j==-1) 

printf("did not read\t"); 

else 

printf("char read::%c ",*ch); 

else 

printf("Can’t open uart device\n"); 

close(i); 

 

return 0; 

 

the o/p for the prog. was as follows........ 

 

file opened with descriptor::3 

char read::. char read::à char read::à char read::. char read::à char read: 

:à char read::. char read::à char read::à char read::. char read::à char r 

ead::à char read::. char read::à char read::à char read::. char read::à ch 

ar read::à char read::. char read::à char read::à
0 Kudos
12 Replies
Altera_Forum
Honored Contributor II
526 Views

Hi ramesh_8051, 

 

> can anyone plz tell what is wrong in my prog 

 

You&#39;re writing through an uninitialized pointer (ch). <div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

int main () 

    int i,j,k=0; 

-    char *ch; 

+    char cbuf[1]; 

+    char *ch = cbuf; 

[/b] 

--- Quote End ---  

Regards, 

--Scott
0 Kudos
Altera_Forum
Honored Contributor II
526 Views

 

--- Quote Start ---  

originally posted by smcnutt@Nov 17 2005, 11:36 AM 

hi ramesh_8051, 

 

> can anyone plz tell what is wrong in my prog 

 

you&#39;re writing through an uninitialized pointer (ch). <div class='quotetop'>quote  

--- quote end ---  

 

--- quote start ---  

int main () 

    int i,j,k=0; 

-    char *ch; 

+    char cbuf[1]; 

+    char *ch = cbuf; 

 

--- Quote End ---  

Regards, 

--Scott 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=11036)</div> 

[/b] 

--- Quote End ---  

 

 

hi Mr. WOLF i tried as u told but the same result comes.. 

do i need to take care of any extra UART regs or INTERRUPTS... 

i have gone thry the pdfs u asked to and proceeded accordingly.. 

they were very useful but i am not getting o/ps.. 

plz help..........
0 Kudos
Altera_Forum
Honored Contributor II
526 Views

 

--- Quote Start ---  

originally posted by smcnutt@Nov 17 2005, 11:36 AM 

hi ramesh_8051, 

 

> can anyone plz tell what is wrong in my prog 

 

you&#39;re writing through an uninitialized pointer (ch). <div class='quotetop'>quote  

--- quote end ---  

 

--- quote start ---  

int main () 

    int i,j,k=0; 

-    char *ch; 

+    char cbuf[1]; 

+    char *ch = cbuf; 

 

--- Quote End ---  

Regards, 

--Scott 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=11036)</div> 

[/b] 

--- Quote End ---  

 

hi mr. SCOTT... 

i have tried as u told but the same result keep coming.. 

is there a need to chk any UARTregs of interrrupts... 

plz help
0 Kudos
Altera_Forum
Honored Contributor II
526 Views

I have tested your program and it works fine. 

 

Please give some more information about the hardware you are runing on and the project properties. 

 

Some things you could try in the meanwhile: 

 

1. check the system.h file for the uart baud rate and make sure it matches the terminals baud rate. 

 

2. have a clean start for your project coming from the "hello_world" example provided by altera. just replace the main() function with your code and only include stdio.h and unistd.h, these are the only header files you will need. 

 

hope this will help, 

--wolf
0 Kudos
Altera_Forum
Honored Contributor II
526 Views

 

--- Quote Start ---  

originally posted by pfab@Nov 21 2005, 12:02 PM 

i have tested your program and it works fine. 

 

please give some more information about the hardware you are runing on and the project properties. 

 

some things you could try in the meanwhile: 

 

1. check the system.h file for the uart baud rate and make sure it matches the terminals baud rate. 

 

2. have a clean start for your project coming from the "hello_world" example provided by altera. just replace the main() function with your code and only include stdio.h and unistd.h, these are the only header files you will need. 

 

hope this will help, 

--wolf 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=11071) 

--- quote end ---  

 

--- Quote End ---  

 

 

 

hello mr.Wolf.. 

thanks for taking pains to execute my prog.. 

i am really happy to know that my prog. works but  

i wonder why it is not the same with my setup.... 

i am using  

->NIOSII IDE to run the code in CYCLONE KIT 

->using the project "ACCESSING MEMORY" present in SLS corporation site 

->it didnot have any uart component in it and so i added a component to it 

and gave pin 42(UART IN) and PIN 47(UART OUT) in the .bdf document... 

->i haven&#39;t kept the option in IDE that uart to be my stdout and stdin.. 

->IN the PC i am running a c-prog which continuously transmits a character using the command - 

"outportb(<COM port no of pc>,&#39;t&#39;)";(i gave the correct port no. i have chkeed it also the data was coming....) 

-> i have chked that the baud rate is 11200bps in both pc and uart setup... 

plz help me out....
0 Kudos
Altera_Forum
Honored Contributor II
526 Views

->NIOSII IDE to run the code in CYCLONE KIT 

is this a development board from Altera? 

 

->using the project "ACCESSING MEMORY" present in SLS corporation site 

I am not familiar with sls corporation. If you give a link to their webpage I can check on the project you mentioned. 

 

--wolf
0 Kudos
Altera_Forum
Honored Contributor II
526 Views

hi ramesh 

 

We have noticed similar behaviour on some of our boards. 

Try these two measures 

 

(1) Try using fopen instead of open with r and r+ modes 

(2) mask the most significant bit of ch 

 

 

hope this helps. http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/smile.gif  

 

regards 

Pavan 

 

PS: Using Altera NDK Stratix 1s10 & 2s60
0 Kudos
Altera_Forum
Honored Contributor II
526 Views

 

--- Quote Start ---  

originally posted by pfab@Nov 29 2005, 05:54 AM 

->niosii ide to run the code in cyclone kit 

is this a development board from altera? 

 

->using the project "accessing memory" present in sls corporation site 

i am not familiar with sls corporation. if you give a link to their webpage i can check on the project you mentioned. 

 

--wolf 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=11233) 

--- quote end ---  

 

--- Quote End ---  

 

hello Mr.WOLF 

thanks again for ur patient following of my mails .. 

yes i am using the ALTERA NIOSII CYCLONE kit ... 

here is the link for SLS corporation which has the ACCESSING MEMORY project under the quatus 5 section..plz find it at the end of the page... 

 

http://www.slscorp.com/up3support/pages/documents.php (http://www.slscorp.com/up3support/pages/documents.php

plz help me out....byeeeeeeee
0 Kudos
Altera_Forum
Honored Contributor II
526 Views

Unless it is absolutly necessary to use the sls example wich is, if I am not wrong, an example for the up3 development kit, you should use the altera examples for your development kit. 

Using the altera examples you will have a FPGA configuration that is tested to work with your board, including the uart. 

 

I am quite sure that the problem is not your code but the fpga design.  

 

 

--wolf
0 Kudos
Altera_Forum
Honored Contributor II
526 Views

hello mr wolf 

i am using EP1C6 UP3 BOARD supplied by altera(i.e sls corp. in turn) 

so the cd given by altera did not include any examples related to ep1c6 board so i had to go to sls example... 

plz help me out ..or can u give some info about ur bdf file(i,e ur example ) where this uart prog worked... 

plz help me 

http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/sad.gif  

 

 

--- Quote Start ---  

originally posted by pfab@Dec 1 2005, 04:03 AM 

unless it is absolutly necessary to use the sls example wich is, if i am not wrong, an example for the up3 development kit, you should use the altera examples for your development kit. 

using the altera examples you will have a fpga configuration that is tested to work with your board, including the uart. 

 

i am quite sure that the problem is not your code but the fpga design.  

 

 

--wolf 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=11284) 

--- quote end ---  

 

--- Quote End ---  

0 Kudos
Altera_Forum
Honored Contributor II
526 Views

hello mr wolf 

i am using EP1C6 UP3 BOARD supplied by altera(i.e sls corp. in turn) 

so the cd given by altera did not include any examples related to ep1c6 board so i had to go to sls example... 

plz help me out ..or can u give some info about ur bdf file(i,e ur example ) where this uart prog worked... 

plz help me 

http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/sad.gif  

 

 

 

 

--- Quote Start ---  

originally posted by pfab@Dec 1 2005, 04:03 AM 

unless it is absolutly necessary to use the sls example wich is, if i am not wrong, an example for the up3 development kit, you should use the altera examples for your development kit. 

using the altera examples you will have a fpga configuration that is tested to work with your board, including the uart. 

 

i am quite sure that the problem is not your code but the fpga design.  

 

 

--wolf 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=11284) 

--- quote end ---  

 

--- Quote End ---  

0 Kudos
Altera_Forum
Honored Contributor II
526 Views

hi ramesh, 

 

I used the standard NIOS II design for a stratix development kit that comes with the NIOS II IDE. You can find a demo version on the altera homepage. 

 

In the next few weeks I will not have time to get into the sls example you are using. 

You might want to start a new topic that asks for assistant with the up3 board. 

There might be somebody who is familiar with it. 

 

SORRY, 

--wolf
0 Kudos
Reply