imx8mq gpio

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

imx8mq gpio

837 Views
gaobo
Contributor I

HI:

When i operation gpio109(GPIO04_IO13) , I use sysfs and memtool  to succeed and see the results,but when i use code,i don not see results ,follow:(DTS has been modified,This is because we need to adapt to the GPIO Library of raspberryPi)

pastedImage_27.png

#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <poll.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <asm/ioctl.h>
#include "wiringPi.h"


static unsigned int usingGpioMem = FALSE ;
static int wiringPiSetuped = FALSE ;
#define BLOCK_SIZE (4*1024)
#define ENV_DEBUG "WIRINGPI_DEBUG"
#define ENV_CODES "WIRINGPI_CODES"
#define ENV_GPIOMEM "WIRINGPI_GPIOMEM"
int wiringPiDebug = FALSE ;
int wiringPiReturnCodes = FALSE ;
static volatile unsigned int GPIO_PADS ;
static volatile unsigned int GPIO1_GDIR ;
static volatile unsigned int GPIO_BASE ;
static volatile unsigned int GPIO1_DR ;
static volatile unsigned int piGpioBase = 0 ;
static volatile unsigned int *gpio ;
static volatile unsigned int *gpio1_dr ;
static volatile unsigned int *gpio1_gdir ;
struct wiringPiNodeStruct *wiringPiNodes = NULL ;
static int wiringPiMode = WPI_MODE_UNINITIALISED ;

/*
* digitalWrite:
* Set an output bit
*********************************************************************************
*/

void digitalWrite (int pin, int value) //25,1
{
struct wiringPiNodeStruct *node = wiringPiNodes ;

if ((pin & PI_GPIO_MASK) == 0) // On-Board Pin
{
if (value == LOW)
*(gpio1_dr) = (*(gpio1_dr) & (0 << 13)) ;
else
*(gpio1_dr) = (*(gpio1_dr) | (1 << 13 ));
}
else
{
return ;
}
}


/*
* setupCheck
* Another sanity check because some users forget to call the setup
* function. Mosty because they need feeding C drip by drip )-:
*********************************************************************************
*/

static void setupCheck (const char *fName)
{
if (!wiringPiSetuped)
{
fprintf (stderr, "%s: You have not called one of the wiringPiSetup\n"
" functions, so I'm aborting your program before it crashes anyway.\n", fName) ;
exit (EXIT_FAILURE) ;
}
}


/*
* pinMode:
* Sets the mode of a pin to be input, output or PWM output
*********************************************************************************
*/

void pinMode (int pin, int mode)//25,output
{
if (mode == INPUT)
{
*(gpio1_dr + 4) = (*(gpio1_dr + 4) & ~(0 << 13)) ; // Sets bits to zero = input
}
else if (mode == OUTPUT)
{
*(gpio1_dr + 4) = (*(gpio1_dr + 4) & ~(0 << 13)) | (1 << 13) ;
}

}


int wiringPiFailure (int fatal, const char *message, ...)
{
va_list argp ;
char buffer [1024] ;

if (!fatal && wiringPiReturnCodes)
return -1 ;

va_start (argp, message) ;
vsnprintf (buffer, 1023, message, argp) ;
va_end (argp) ;

fprintf (stderr, "%s", buffer) ;
exit (EXIT_FAILURE) ;

return 0 ;
}

int wiringPiSetup (void)
{
int fd ;

if (wiringPiSetuped)
return 0 ;

wiringPiSetuped = TRUE ;

if (getenv (ENV_DEBUG) != NULL)
wiringPiDebug = TRUE ;

if (getenv (ENV_CODES) != NULL)
wiringPiReturnCodes = TRUE ;

if (wiringPiDebug)
printf ("wiringPi: wiringPiSetup called\n") ;

GPIO_PADS = 0x303303F8 ; //MUX PAD reg

GPIO_BASE = 0x30330190 ; //MUX reg
GPIO1_DR = 0x30220000 ;
GPIO1_GDIR = 0x30220004 ;

if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC)) < 0)
{
printf("Unable to open /dev/mem: %s\n", strerror(errno));
return -1;
}

gpio1_dr = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO1_DR) ;
if (gpio1_dr == MAP_FAILED)
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap gpio1_dr failed: %s\n", strerror (errno)) ;

return 0 ;
}
int main (void)
{

if(wiringPiSetup() < 0)return 1;
pinMode (13,OUTPUT) ;

digitalWrite(13, 1) ;

return 0 ;
}

0 Kudos
2 Replies

768 Views
igorpadykov
NXP Employee
NXP Employee

Hi gao

example of reading register can be found in memtool:

memtool.c\memtool\test - imx-test - i.MX Driver Test Application Software 

Also one can try linux gpio apis described in sect.2.1.6.1.1 API for GPIO

attached Linux Manual.

Best regards
igor
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

768 Views
gaobo
Contributor I

Can't you see the change with memtool and "cat /sys/class/gpio/gpio109/value(or direction)"in the above way!

0 Kudos