Home/Support/Support Forum/User Space LED/GPIO access through Digital I/O
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

User Space LED/GPIO access through Digital I/O

0 votes
I want to configure the Digital I/O as GPIO, So i did like this

At first i created a node for GPIO3_5
> 69 in /etc/makedevs.d/ccwmx51

/dev/gpio d 755 0 0
/dev/gpio/69 c 644 0 0 250 69
then
modprobe gpio

Then, from my application:

/* Open GPIO file descriptor */

int fd = open("/dev/gpio/69", O_RDWR);

/* Configure GPIO as output*/

ioctl(fd, GPIO_CONFIG_AS_OUT, 0);

/* control GPIO*/

int outvalve = 0; /* outvalve =0 ---> led "on", outvalve =1 ----> led"off"*/

write(fd, (char *)&outvalve, sizeof(char));

Here i am not getting any compilation error.

But my LED, which i connected in the Digital I/O pin(GPIO3_5) is not toggling.

where as the LED(GPIO3_10) is toggling. This GPIO3_10 not a Digital I/O, it is connected in User section of my board.

Can any one help why my Digital I/O is not Toggling?
Is there anything went wrong in my configuration?
asked Nov 8, 2010 in Linux by elu_digi New to the Community (6 points)
recategorized Dec 3, 2013 by tuxembb

Please log in or register to answer this question.

4 Answers

0 votes
Gpio_test shows the only supported way of working with GPIOs:
/usr/local/DigiEL-5.5/apps/gpio_test_c/gpio_test.c


/* Check if gpio kernel module is loaded */
if (!check_module_loaded()) {
printf("[ERROR] gpio driver not loaded, please run 'modprobe gpio'\n\n");
exit(EXIT_FAILURE);
}

snprintf(dev_button, sizeof(dev_button) - 1, "/dev/gpio/%d",
plat_list[platform].gpio_in);
if ((fd_button = open(dev_button, O_RDWR)) < 0) {
printf("[ERROR] open (%s): %s\n\n", dev_button, strerror(errno));
exit(EXIT_FAILURE);
}

snprintf(dev_led, sizeof(dev_led) - 1, "/dev/gpio/%d", plat_list[platform].gpio_out);
if ((fd_led = open(dev_led, O_RDWR)) < 0) {
printf("[ERROR] open (%s): %s\n\n", dev_led, strerror(errno));
close(fd_button);
exit(EXIT_FAILURE);
}

if (GPIO_UNDEFINED != plat_list[platform].pullup_in) {
snprintf(dev_pullup, sizeof(dev_led) - 1, "/dev/gpio/%d",
plat_list[platform].pullup_in);
if ((fd_pullup = open(dev_pullup, O_RDWR)) < 0) {
printf("[ERROR] open (%s): %s\n\n", dev_pullup, strerror(errno));
close(fd_led);
close(fd_button);
exit(EXIT_FAILURE);
}
}

fprintf(stdout, "Configuring %s as input and %s as output\n\n", dev_button, dev_led);

if ((ret_val = ioctl(fd_button, GPIO_CONFIG_AS_INP)) < 0) {
printf("[ERROR] ioctl (button): %s\n\n", strerror(errno));
goto err_close;
}

if ((ret_val = ioctl(fd_led, GPIO_CONFIG_AS_OUT)) < 0) {
printf("[ERROR] ioctl (led): %s\n\n", strerror(errno));
goto err_close;
}

/* Initialize outval with the current led value */
if ((ret_val = ioctl(fd_led, GPIO_READ_PIN_VAL, &amp;outval)) < 0) {
printf("[ERROR] ioctl (led): %s\n\n", strerror(errno));
goto err_close;
}

fprintf(stdout, "Using ioctl system call to control the GPIOs\n"
"Press the button %s 10 times and check the led %s\n\n",
plat_list[platform].button, plat_list[platform].led);

loops = 10;
while (loops) {
do {
lastinval = inval;
if ((ret_val = ioctl(fd_button, GPIO_READ_PIN_VAL, &amp;inval)) < 0) {
printf("[ERROR] ioctl (button): %s\n\n", strerror(errno));
goto err_close;
}
usleep(1000);
} while (!((lastinval == 1) &amp;&amp; (inval == 0)));

fprintf(stdout, "%s pressed\n", plat_list[platform].button);

outval = outval ? 0 : 1;

if ((ret_val = ioctl(fd_led, GPIO_WRITE_PIN_VAL, &amp;outval)) < 0) {
printf("[ERROR] ioctl (led): %s\n\n", strerror(errno));
goto err_close;
}
loops--;
}

fprintf(stdout, "\nUsing read/write system calls to control the GPIOs\n"
"Press the button %s 10 times and check the led %s\n\n",
plat_list[platform].button, plat_list[platform].led);

loops = 10;
while (loops) {
do {
lastinval = inval;
if ((ret_val =
read(fd_button, (char *)&amp;inval, sizeof(char))) != sizeof(char)) {
printf("[ERROR] read (button): %s\n\n", strerror(errno));
goto err_close;
}
usleep(1000);
} while (!((lastinval == 1) &amp;&amp; (inval == 0)));

fprintf(stdout, "%s pressed\n", plat_list[platform].button);

outval = outval ? 0 : 1;

if ((ret_val = write(fd_led, (char *)&amp;outval, sizeof(char))) != sizeof(char)) {
printf("[ERROR] write (led): %s\n\n", strerror(errno));
goto err_close;
}
loops--;
}
answered Nov 11, 2010 by bobthomas13 Seasoned Professional (182 points)
0 votes
Thanks for your reply THOMAS BOB.

I used the example program of gpio(/usr/local/DigiEL-5.5/apps/gpio_test_c) what you mentioned is working fine only for GPIO3_10, GPIO3_9 and GPIO1_8. It is not working for other than these GPIO's.

I am trying to toggle GPIO3_5, it is one of the Digital I/O's pin. When i do this i am not getting any compilation error also it is returning correct return value for open,read and write.
answered Nov 12, 2010 by elu_digi New to the Community (6 points)
0 votes
Enclosed a patch for the digital IO to be applied to DEL-5.5-1P-SP1.

cd /usr/local/DigiEL-5.5/kernel/linux-2.6.31/

patch -p1 < 0001-ccwmx51-Configure-the-digital-IO-pins-as-GPIO.patch

diff --git a/arch/arm/mach-mx5/mx51_ccwmx51js_gpio.c b/arch/arm/mach-mx5/mx51_ccwmx51js_gpio.c
index 7d127a4..4184387 100644
--- a/arch/arm/mach-mx5/mx51_ccwmx51js_gpio.c
+++ b/arch/arm/mach-mx5/mx51_ccwmx51js_gpio.c
@@ -709,6 +709,23 @@ void __init ccwmx51_io_init(void)
/* For the wireless module */
ccwmx51_mmc2_gpio_active();
#endif
+
+ /* Configure Digital IO as GPIO */
+ mxc_config_iomux(MX51_PIN_NANDF_CS4,IOMUX_CONFIG_ALT3 | IOMUX_CONFIG_SION);
+ mxc_config_iomux(MX51_PIN_NANDF_CS5,IOMUX_CONFIG_ALT3 | IOMUX_CONFIG_SION);
+ mxc_config_iomux(MX51_PIN_NANDF_CS6,IOMUX_CONFIG_ALT3 | IOMUX_CONFIG_SION);
+#if !defined(CONFIG_MMC_IMX_ESDHCI) &amp;&amp; !defined(CONFIG_MMC_IMX_ESDHCI_MODULE)
+ mxc_config_iomux(MX51_PIN_NANDF_CS7,IOMUX_CONFIG_ALT3 | IOMUX_CONFIG_SION);
+#endif
+
+ mxc_config_iomux(MX51_PIN_DISPB2_SER_DIN,IOMUX_CONFIG_ALT4 | IOMUX_CONFIG_SION);
+ mxc_iomux_set_input(MUX_IN_GPIO3_IPP_IND_G_IN_5_SELECT_INPUT,INPUT_CTL_PATH1);
+ mxc_config_iomux(MX51_PIN_DISPB2_SER_DIO,IOMUX_CONFIG_ALT4 | IOMUX_CONFIG_SION);
+ mxc_iomux_set_input(MUX_IN_GPIO3_IPP_IND_G_IN_6_SELECT_INPUT,INPUT_CTL_PATH1);
+ mxc_config_iomux(MX51_PIN_DISPB2_SER_CLK,IOMUX_CONFIG_ALT4 | IOMUX_CONFIG_SION);
+ mxc_iomux_set_input(MUX_IN_GPIO3_IPP_IND_G_IN_7_SELECT_INPUT,INPUT_CTL_PATH1);
+ mxc_config_iomux(MX51_PIN_DISPB2_SER_RS,IOMUX_CONFIG_ALT4 | IOMUX_CONFIG_SION);
+ mxc_iomux_set_input(MUX_IN_GPIO3_IPP_IND_G_IN_8_SELECT_INPUT,INPUT_CTL_PATH1);
}

#if defined(CONFIG_MMC_IMX_ESDHCI) || defined(CONFIG_MMC_IMX_ESDHCI_MODULE)
answered Nov 12, 2010 by User143 Community Contributor (132 points)
0 votes
Hi, Thanks again
I applied the patch, when i recompile the kernel i got an error like this

/usr/local/DigiEL-5.5/kernel/linux-2.6.31/arch/arm/mach-mx51/mx51_ccwmx51js_gpio.c implicit declaration of function 'mxc_config_iomux' kernelproject 733 C/C++ Problem


There is no mxc_config_iomux function in iomux.h file

How to declare this function.
answered Nov 12, 2010 by elu_digi New to the Community (6 points)
...