<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<META NAME="GENERATOR" CONTENT="GtkHTML/3.12.2">
</HEAD>
<BODY>
Hi Guillaume,<BR>
<BR>
Try using channel 1. It may be set up where the first channel is 1 and not 0.<BR>
<BR>
- John<BR>
<BR>
On Mon, 2008-05-26 at 17:43 +0200, Guillaume Dargaud wrote:
<BLOCKQUOTE TYPE=CITE>
<PRE>
<FONT COLOR="#000000">I still haven't managed to figure out how to use the Xilinx GPIO from </FONT>
<FONT COLOR="#000000">usermode.</FONT>
<FONT COLOR="#000000">The program gpio_test found at </FONT>
<FONT COLOR="#000000"><A HREF="http://www.itee.uq.edu.au/~listarch/microblaze-uclinux/archive/2004/05/msg00004.html">http://www.itee.uq.edu.au/~listarch/microblaze-uclinux/archive/2004/05/msg00004.html</A> </FONT>
<FONT COLOR="#000000">and slightly modified as this:</FONT>
<FONT COLOR="#000000">#include <stdio.h></FONT>
<FONT COLOR="#000000">#include <fcntl.h></FONT>
<FONT COLOR="#000000">#include <errno.h></FONT>
<FONT COLOR="#000000">#include <string.h></FONT>
<FONT COLOR="#000000">#include <linux/types.h></FONT>
<FONT COLOR="#000000">#include <sys/ioctl.h></FONT>
<FONT COLOR="#000000">#include <xgpio_ioctl.h></FONT>
<FONT COLOR="#000000">#define GPIO_DEV "/dev/xgpio"</FONT>
<FONT COLOR="#000000">void usage(int argc, char *argv[])</FONT>
<FONT COLOR="#000000">{</FONT>
<FONT COLOR="#000000"> fprintf(stderr,"usage: %s hexval\n\n", argv[0]);</FONT>
<FONT COLOR="#000000"> fprintf(stderr,"\n");</FONT>
<FONT COLOR="#000000"> fprintf(stderr,"Puts the input hexval onto the LEDs, and\n");</FONT>
<FONT COLOR="#000000"> fprintf(stderr,"displays the current value on the DIP switches\n");</FONT>
<FONT COLOR="#000000"> fprintf(stderr,"\n");</FONT>
<FONT COLOR="#000000"> fprintf(stderr,"try gpio_test FC\n\n");</FONT>
<FONT COLOR="#000000">}</FONT>
<FONT COLOR="#000000">void dump_dipsw(int val)</FONT>
<FONT COLOR="#000000">{</FONT>
<FONT COLOR="#000000"> int i;</FONT>
<FONT COLOR="#000000"> val = (val & 0x000000FF);</FONT>
<FONT COLOR="#000000"> printf("DIP SW: ");</FONT>
<FONT COLOR="#000000"> for(i=0;i<8;i++)</FONT>
<FONT COLOR="#000000"> {</FONT>
<FONT COLOR="#000000"> printf("%i",val & 0x01);</FONT>
<FONT COLOR="#000000"> val >>= 1;</FONT>
<FONT COLOR="#000000"> }</FONT>
<FONT COLOR="#000000"> printf("\n");</FONT>
<FONT COLOR="#000000">}</FONT>
<FONT COLOR="#000000">/* Turn 4 LSB of X into 8 bit LED segment code */</FONT>
<FONT COLOR="#000000">unsigned char nyb_hex2led(int x)</FONT>
<FONT COLOR="#000000">{</FONT>
<FONT COLOR="#000000"> unsigned char led_tab[] = {</FONT>
<FONT COLOR="#000000"> 0xFC, 0x60, 0xDA, 0xF2,</FONT>
<FONT COLOR="#000000"> 0x66, 0xB6, 0xBE, 0xE0,</FONT>
<FONT COLOR="#000000"> 0xFE, 0xF6, 0xEE, 0x3E,</FONT>
<FONT COLOR="#000000"> 0x9C, 0x7A, 0x9E, 0x8E};</FONT>
<FONT COLOR="#000000"> return led_tab[x & 0xF];</FONT>
<FONT COLOR="#000000">}</FONT>
<FONT COLOR="#000000">/* Turn 8 LSB of x into 2 times 8 bit LED segment codes */</FONT>
<FONT COLOR="#000000">unsigned short hex2led(int x)</FONT>
<FONT COLOR="#000000">{</FONT>
<FONT COLOR="#000000"> return (nyb_hex2led(x & 0xF) << 8) | (nyb_hex2led((x >> 4) & 0xF));</FONT>
<FONT COLOR="#000000">}</FONT>
<FONT COLOR="#000000">int main(int argc, char *argv[])</FONT>
<FONT COLOR="#000000">{</FONT>
<FONT COLOR="#000000"> /* Open the device */</FONT>
<FONT COLOR="#000000"> int fd = open(GPIO_DEV, O_RDWR);</FONT>
<FONT COLOR="#000000"> struct xgpio_ioctl_data gpio_ioctl;</FONT>
<FONT COLOR="#000000"> // struct ibm_gpio_ioctl_data gpio_ioctl;</FONT>
<FONT COLOR="#000000">// int result;</FONT>
<FONT COLOR="#000000">// int command;</FONT>
<FONT COLOR="#000000"> if(fd==-1) {</FONT>
<FONT COLOR="#000000"> fprintf(stderr,"Unable to open %s\n", GPIO_DEV);</FONT>
<FONT COLOR="#000000"> return -1;</FONT>
<FONT COLOR="#000000"> }</FONT>
<FONT COLOR="#000000">/*</FONT>
<FONT COLOR="#000000"> if(argc!=2)</FONT>
<FONT COLOR="#000000"> {</FONT>
<FONT COLOR="#000000"> usage();</FONT>
<FONT COLOR="#000000"> exit(1);</FONT>
<FONT COLOR="#000000"> }</FONT>
<FONT COLOR="#000000">*/</FONT>
<FONT COLOR="#000000"> gpio_ioctl.chan=0;</FONT>
<FONT COLOR="#000000"> /* Set the tristates */</FONT>
<FONT COLOR="#000000"> gpio_ioctl.mask=0x000000FF;</FONT>
<FONT COLOR="#000000"> ioctl(fd, XGPIO_TRISTATE,(void *)&gpio_ioctl);</FONT>
<FONT COLOR="#000000"> /* Get output data from command line if provided */</FONT>
<FONT COLOR="#000000"> if(argc==2)</FONT>
<FONT COLOR="#000000"> sscanf(argv[1],"%x",&(gpio_ioctl.data));</FONT>
<FONT COLOR="#000000"> else</FONT>
<FONT COLOR="#000000"> gpio_ioctl.data=time(NULL);</FONT>
<FONT COLOR="#000000"> /* Convert binary (16 LSB) into LED segment codes and shift into</FONT>
<FONT COLOR="#000000"> position on gpio */</FONT>
<FONT COLOR="#000000"> gpio_ioctl.data=hex2led(gpio_ioctl.data & 0xFF)<<8;</FONT>
<FONT COLOR="#000000"> ioctl(fd, XGPIO_OUT,(void *)&gpio_ioctl);</FONT>
<FONT COLOR="#000000"> /* Read some data */</FONT>
<FONT COLOR="#000000"> ioctl(fd, XGPIO_IN,(void *)&gpio_ioctl);</FONT>
<FONT COLOR="#000000"> dump_dipsw(gpio_ioctl.data);</FONT>
<FONT COLOR="#000000"> return 0;</FONT>
<FONT COLOR="#000000">}</FONT>
<FONT COLOR="#000000">...crashes in flames when run:</FONT>
<FONT COLOR="#000000"># gpio_test FC</FONT>
<FONT COLOR="#000000">[ 1366.864955] Oops: kernel access of bad area, sig: 11 [#5]</FONT>
<FONT COLOR="#000000">[ 1366.867560] NIP: c0108d50 LR: c00e68c4 CTR: 00000000</FONT>
<FONT COLOR="#000000">[ 1366.867560] REGS: c752fde0 TRAP: 0300 Tainted: G D (2.6.25-rc9)</FONT>
<FONT COLOR="#000000">[ 1366.867560] MSR: 00029030 <EE,ME,IR,DR> CR: 93000033 XER: e000007f</FONT>
<FONT COLOR="#000000">[ 1366.867560] DEAR: c8fffffc, ESR: 00000000</FONT>
<FONT COLOR="#000000">[ 1366.867560] TASK = c7c37030[202] 'gpio_test' THREAD: c752e000</FONT>
<FONT COLOR="#000000">[ 1366.867560] GPR00: 00000000 c752fe90 c7c37030 c8fffffc 00000056 0000000c </FONT>
<FONT COLOR="#000000">c752feb0 00000000</FONT>
<FONT COLOR="#000000">[ 1366.867560] GPR08: 00000000 11111111 ffffffe7 c7c5866c 00000000 10018dec </FONT>
<FONT COLOR="#000000">ffff8432 ffdf1d0f</FONT>
<FONT COLOR="#000000">[ 1366.867560] GPR16: ffffffff ffffffff ffffffff ffffffff ffffffff 00000000 </FONT>
<FONT COLOR="#000000">100af244 10000858</FONT>
<FONT COLOR="#000000">[ 1366.867560] GPR24: 10000c00 00000002 10000478 800c5a03 c7c58000 c7c5866c </FONT>
<FONT COLOR="#000000">800c5a03 c0200000</FONT>
<FONT COLOR="#000000">[ 1366.867560] NIP [c0108d50] XIo_In32+0x4/0xc</FONT>
<FONT COLOR="#000000">[ 1366.867560] LR [c00e68c4] XGpio_GetDataDirection+0x84/0xac</FONT>
<FONT COLOR="#000000">[ 1366.867560] Call Trace:</FONT>
<FONT COLOR="#000000">[ 1366.867560] [c752fe90] [c00e66a8] xgpio_getinst+0x48/0xb0 (unreliable)</FONT>
<FONT COLOR="#000000">[ 1366.867560] [c752fea0] [c00e6a8c] xgpio_ioctl+0x1a0/0x1fc</FONT>
<FONT COLOR="#000000">[ 1366.867560] [c752fed0] [c006740c] vfs_ioctl+0x6c/0x84</FONT>
<FONT COLOR="#000000">[ 1366.867560] [c752fee0] [c006778c] do_vfs_ioctl+0x368/0x39c</FONT>
<FONT COLOR="#000000">[ 1366.867560] [c752ff10] [c0067800] sys_ioctl+0x40/0x70</FONT>
<FONT COLOR="#000000">[ 1366.867560] [c752ff40] [c0002770] ret_from_syscall+0x0/0x3c</FONT>
<FONT COLOR="#000000">[ 1366.867560] Instruction dump:</FONT>
<FONT COLOR="#000000">[ 1366.867560] 7c634b78 7c031b78 4e800020 7c0006ac 88630000 5463063e </FONT>
<FONT COLOR="#000000">4e800020 7c0006ac</FONT>
<FONT COLOR="#000000">[ 1366.867560] a0630000 5463043e 4e800020 7c0006ac <80630000> 4e800020 </FONT>
<FONT COLOR="#000000">7c0006ac 7c601e2c</FONT>
<FONT COLOR="#000000">Segmentation fault</FONT>
<FONT COLOR="#000000"># dmesg | grep gpio</FONT>
<FONT COLOR="#000000">[ 0.120666] Registering device xilinx_gpio:0</FONT>
<FONT COLOR="#000000">[ 0.121672] Registering device xilinx_gpio:1</FONT>
<FONT COLOR="#000000">[ 0.122682] Registering device xilinx_gpio:2</FONT>
<FONT COLOR="#000000">[ 0.292352] xgpio0 #0 at 0x81400000 mapped to 0xC9000000 device: 10,185 </FONT>
<FONT COLOR="#000000">using IRQ#7</FONT>
<FONT COLOR="#000000">[ 0.293805] xgpio1 #1 at 0x81420000 mapped to 0xC9020000 device: 10,186 </FONT>
<FONT COLOR="#000000">using IRQ#6</FONT>
<FONT COLOR="#000000">[ 0.295062] xgpio2 #2 at 0x81440000 mapped to 0xC9040000 device: 10,187 </FONT>
<FONT COLOR="#000000">using IRQ#5</FONT>
<FONT COLOR="#000000">[ 1366.867560] TASK = c7c37030[202] 'gpio_test' THREAD: c752e000</FONT>
<FONT COLOR="#000000">[ 1366.867560] [c752fe90] [c00e66a8] xgpio_getinst+0x48/0xb0 (unreliable)</FONT>
<FONT COLOR="#000000">[ 1366.867560] [c752fea0] [c00e6a8c] xgpio_ioctl+0x1a0/0x1fc</FONT>
<FONT COLOR="#000000">Another program using a different strategy also fails:</FONT>
<FONT COLOR="#000000">#include <fcntl.h></FONT>
<FONT COLOR="#000000">#include <stdio.h></FONT>
<FONT COLOR="#000000">int main (int argc, char *argv[]) {</FONT>
<FONT COLOR="#000000"> if (argc==2 || argc==3) {</FONT>
<FONT COLOR="#000000"> int fd = open(argv[1], O_RDWR | O_NDELAY );</FONT>
<FONT COLOR="#000000"> if (fd < 0) {</FONT>
<FONT COLOR="#000000"> fprintf(stderr, "GPIO OPEN FAIL\n");</FONT>
<FONT COLOR="#000000"> return -1;</FONT>
<FONT COLOR="#000000"> }</FONT>
<FONT COLOR="#000000"> unsigned long buff = 0;</FONT>
<FONT COLOR="#000000"> if (argc==2) {</FONT>
<FONT COLOR="#000000"> if (read(fd, &buff, 4) != 4)</FONT>
<FONT COLOR="#000000"> fprintf(stderr, "GPIO READ ERROR\n");</FONT>
<FONT COLOR="#000000"> else printf( "%x\n", buff);</FONT>
<FONT COLOR="#000000"> } else {</FONT>
<FONT COLOR="#000000"> buff=atoi(argv[2]);</FONT>
<FONT COLOR="#000000"> if (write(fd, &buff, 4) != 4)</FONT>
<FONT COLOR="#000000"> fprintf(stderr, "GPIO WRITE ERROR value %x\n", buff);</FONT>
<FONT COLOR="#000000"> else printf( "Written %x successfully\n", buff);</FONT>
<FONT COLOR="#000000"> }</FONT>
<FONT COLOR="#000000"> close(fd);</FONT>
<FONT COLOR="#000000"> } else {</FONT>
<FONT COLOR="#000000"> fprintf(stderr, "Usage: %s device [value] to read or write data to gpio </FONT>
<FONT COLOR="#000000">device\n", argv[0]);</FONT>
<FONT COLOR="#000000"> return 1;</FONT>
<FONT COLOR="#000000"> }</FONT>
<FONT COLOR="#000000"> return 0;</FONT>
<FONT COLOR="#000000">}</FONT>
<FONT COLOR="#000000"># GpioReadWrite /dev/xgpio</FONT>
<FONT COLOR="#000000">GPIO READ ERROR</FONT>
<FONT COLOR="#000000"># GpioReadWrite /dev/xgpio 7</FONT>
<FONT COLOR="#000000">GPIO WRITE ERROR value 0</FONT>
<FONT COLOR="#000000">I wanted to avoid writing a kernel module, but apparently I've ran out of </FONT>
<FONT COLOR="#000000">options...</FONT>
</PRE>
</BLOCKQUOTE>
<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<HR SIZE="1">
<BR>
<BR>
John Bonesio<BR>
Commercial Linux Solutions<BR>
john.bonesio@xilinx.com<BR>
(408) 879-5569
</TD>
</TR>
</TABLE>
<br clear=all> This email and any attachments are intended for the sole use of the named recipient(s) and contain(s) confidential information that may be proprietary, privileged or copyrighted under applicable law. If you are not the intended recipient, do not read, copy, or forward this email message or any attachments. Delete this email message and any attachments immediately.
</BODY>
</HTML>