Page 1 of 1

Problem with raw-reading AUX register

Posted: Mon May 06, 2013 2:23 pm
by McGregor80
Hi

I'm trying to read data from AUX_MU_LSR_REG (0x7E215054).
I modified code for manipulation of GPIO ports but i only get 0 when reading the register.

What is it what i'm doing wrong?

Code: Select all

Here are all #include...

#define BCM2708		0x20000000
#define GPIO_BASE 	(BCM2708+0x200000)
#define UART_BASE	(BCM2708+0x215000) 
#define PAGE_SIZE	(4*1024)
#define BLOCK_SIZE	(4*1024)
#define TX_IDLE		*(uart+21)

void *uart_map;
volatile unsigned *uart;
int uart0_filestream;
struct termios options;

main()
{
if((mem_fd=open("/dev/mem",O_RDWR|O_SYNC))<0)
	{
	printf("can't open /dev/mem");
	ERROR();
	printf("\n");
	exit(-1);
	}
else 
	{
	printf("stream_file open ");
	OK();
	printf("\n");
	}

if((uart_map=malloc(BLOCK_SIZE))==0)
	printf("malloc fail\n");
else printf("UART_mallocation OK %d \n", (int)uart_map );

uart_map=mmap
	(
	NULL, 			//any address in our space
	BLOCK_SIZE, 		//map length
	PROT_READ|PROT_WRITE, 	//Enable reading and writing to maped memmory
	MAP_SHARED,		//Shares with other processes
	mem_fd,			//File to map
	UART_BASE		//Offset to UART
	);




if(uart_map==MAP_FAILED)
	{
	printf("UART mmap error %d", (int)uart_map);
    exit(-1);
	}
else printf("UART mapping OK \n");
uart=(volatile unsigned* )uart_map;

close(mem_fd);			//Close the file stream after maping

uart0_filestream=open("/dev/ttyAMA0",O_RDWR | O_NOCTTY | /*O_NDELAY|*/ O_NONBLOCK);
if(uart0_filestream==-1)
	{
	printf("Error with opening UART \n");
	return -1;	
	}
else printf("UART opening OK \n");	

fcntl(uart0_filestream, F_SETFL,O_NDELAY);

//Here i setup the termios options

// Write to the port
printf("%x \n",TX_IDLE); //HERE IT SHOWS 0
int n = write(uart0_filestream,"Hello Peripheral\n  ",16);

printf("%x \n", TX_IDLE); //HERE IT SHOWS 0
if (n < 0) 	{
			perror("Write failed - ");
			return -1;
			}
n=0;
//function checking if the data was send
usleep(16*1200);
printf("%x \n", TX_IDLE);  //HERE IT SHOWS 0


.... (some other code)

}
So the receiver works and sender is transmitting but i would like to check if the send is done and TX is IDLE.

Any idea why it is showing only 0? Or in general if it's correct method?
Thank You.

Re: Problem with raw-reading AUX register

Posted: Wed May 08, 2013 3:01 pm
by McGregor80
No answers. Maybe in this case someone can show me some example how to raw read a register value from the PI?

Re: Problem with raw-reading AUX register

Posted: Sat May 11, 2013 5:04 pm
by gordon@drogon.net
Your code maps the register, but you never actually read it.

So you want to check/wait when the send is done: so why not use the tcdrain() system function?

Don't read the hardware registers directly because Linux will be buffering your data and clocking it out under interrupt/dma ...

-Gordon

Re: Problem with raw-reading AUX register

Posted: Sun May 12, 2013 5:41 pm
by McGregor80
Thank You Gordon,

I'll try the tcdrain( I didn't had any idea about existence of this function).

But just to make me understand what i'm doing wrong can you please explain what i'm missing in reading the registers?
Your code maps the register, but you never actually read it.
I thought that mmap() does just the job and kernel keeps the memory area all the time updated with hardware.

gregor

Re: Problem with raw-reading AUX register

Posted: Sun May 12, 2013 5:51 pm
by gordon@drogon.net
McGregor80 wrote:Thank You Gordon,

I'll try the tcdrain( I didn't had any idea about existence of this function).

But just to make me understand what i'm doing wrong can you please explain what i'm missing in reading the registers?
Your code maps the register, but you never actually read it.
I thought that mmap() does just the job and kernel keeps the memory area all the time updated with hardware.

gregor
The mmap() call gives you access to it, but you still have to read it. There's no automatic monitoring of it for you. No hand-holding here. See (for example) the source code to wiringPi to see how some of the hardware registers are read via the mmap() mechanism, but I really don't think it's going to help you here at all, as Linux is also looking at it and its the Linux kernel that's handling sending characters out one by one...

-Gordon

Re: Problem with raw-reading AUX register

Posted: Thu May 16, 2013 5:51 pm
by McGregor80
Thanks again Gordon,
The tcdrain() worked exactly as you said.

Re: Problem with raw-reading AUX register

Posted: Tue May 21, 2013 11:13 am
by McGregor80
I have to take it back. It works in most cases but i just find out that often it's too slow so i miss half of a message send by other units if they respond instantaneously.

Do you have any other ideas?
The only thing i can think about is to calculate the delay for the TXenable basing on baudrate and number of bytes to be sent.

Re: Problem with raw-reading AUX register

Posted: Tue May 21, 2013 11:20 am
by gordon@drogon.net
McGregor80 wrote:I have to take it back. It works in most cases but i just find out that often it's too slow so i miss half of a message send by other units if they respond instantaneously.

Do you have any other ideas?
The only thing i can think about is to calculate the delay for the TXenable basing on baudrate and number of bytes to be sent.
You probably want to invent a nice little protocol for talking to the remote device. If you start to rely on timing like that, then you'll be prone to all sorts of issues.

Maybe post a better description of what you're aiming to achieve - what are you interfacing to, etc.

-Gordon

Re: Problem with raw-reading AUX register

Posted: Mon Jun 03, 2013 9:29 am
by McGregor80
Hi
Maybe post a better description of what you're aiming to achieve - what are you interfacing to, etc.
I'm Interfacing with UART/RS485 converter and i have a bus of devices on the RS485 line. It's a halfduplex bus with D+, D- and GND. Selecting direction is done by setting the TX_Enable pin to Low(RX) or High(TX).
The thing is when i send the command i can get answer directly or in Timeout which is ca 1000 ms.

Gregor