S3C2451_GPIO初始化、置位、读取源码

以下是博主自己写的基于三星的S3C2451mini开发板的GPIO编程代码。 供大家参考学习

/*GPIO宏定义*/
#define GPACON (*(volatile unsigned long *)0x56000000)
#define GPADAT (*(volatile unsigned long *)0x56000004)

#define GPBCON (*(volatile unsigned long *)0x56000010)
#define GPBDAT (*(volatile unsigned long *)0x56000014)
#define GPBUDP (*(volatile unsigned long *)0x56000018)
#define GPBSEL (*(volatile unsigned long *)0x5600001c)

#define GPCCON (*(volatile unsigned long *)0x56000020)
#define GPCDAT (*(volatile unsigned long *)0x56000024)
#define GPCUDP (*(volatile unsigned long *)0x56000028)

#define GPDCON (*(volatile unsigned long *)0x56000030)
#define GPDDAT (*(volatile unsigned long *)0x56000034)
#define GPDUDP (*(volatile unsigned long *)0x56000038)

#define GPECON (*(volatile unsigned long *)0x56000040)
#define GPEDAT (*(volatile unsigned long *)0x56000044)
#define GPEUDP (*(volatile unsigned long *)0x56000048)
#define GPESEL (*(volatile unsigned long *)0x5600004c)

#define GPFCON (*(volatile unsigned long *)0x56000050)
#define GPFDAT (*(volatile unsigned long *)0x56000054)
#define GPFUDP (*(volatile unsigned long *)0x56000058)


#define GPGCON (*(volatile unsigned long *)0x56000060)
#define GPGUDP (*(volatile unsigned long *)0x56000064)
#define GPGDAT (*(volatile unsigned long *)0x56000068)

#define GPHCON (*(volatile unsigned long *)0x56000070)
#define GPHUDP (*(volatile unsigned long *)0x56000074)
#define GPHDAT (*(volatile unsigned long *)0x56000078)

#define GPJCON (*(volatile unsigned long *)0x560000d0)
#define GPJUDP (*(volatile unsigned long *)0x560000d4)
#define GPJDAT (*(volatile unsigned long *)0x560000d8)

#define GPKCON (*(volatile unsigned long *)0x560000e0)
#define GPKUDP (*(volatile unsigned long *)0x560000e4)
#define GPKDAT (*(volatile unsigned long *)0x560000e8)

#define GPLCON (*(volatile unsigned long *)0x560000f0)
#define GPLUDP (*(volatile unsigned long *)0x560000f4)
#define GPLDAT (*(volatile unsigned long *)0x560000f8)

#define GPMCON (*(volatile unsigned long *)0x56000100)
#define GPMUDP (*(volatile unsigned long *)0x56000104)
#define GPMDAT (*(volatile unsigned long *)0x56000108)

#define UTXH0 (*(volatile unsigned long *)0x50000020)
#define URXH0 (*(volatile unsigned long *)0x50000024)
#define MISCCR (*(volatile unsigned *)(0x56000080))
/*GPIO2451.h*/
#ifndef _GPIO2451_H_
#define _GPIO2451_H_

#include "typedef.h"

typedef enum
{
    Bit_RESET = 0,
    Bit_SET = !Bit_RESET
}BitAction;


typedef enum
{
    GPIOA = 0,
    GPIOB,
    GPIOC,
    GPIOD,
    GPIOE,
    GPIOF,
    GPIOG,
    GPIOH,
    GPIOJ,
    GPIOK,
    GPIOL,
    GPIOM   
}GPIO_TypeDef;

typedef enum
{
    GPIO_MODE_IN = 0,
    GPIO_MODE_OUT,
    GPIO_MODE_ALT1,
    GPIO_MODE_ALT2
}GPIO_MODE_TypeDef;

typedef enum
{
    GPIO_UDP_DISABLE = 0,
    GPIO_UDP_DOWN,
    GPIO_UDP_UP,
}GPIO_UDP_TypeDef;


extern void GPIOx_Init(GPIO_TypeDef gpiox, U32 pin, GPIO_MODE_TypeDef mode, GPIO_UDP_TypeDef udp);
extern void GPIO_SetBit(GPIO_TypeDef gpiox, U32 pin, BitAction bit);
extern BitAction GPIO_GetBit(GPIO_TypeDef gpiox, U32 pin);

#endif
/*typedef.h*/
#ifndef _TYPEDEF_H_
#define _TYPEDEF_H_

#define U32 unsigned int
#define U16 unsigned short
#define S32 int
#define S16 short int
#define U8 unsigned char
#define S8 char

#define TRUE 1
#define FALSE 0

#endif
#include "Main.h"
#include "typedef.h"

/* 对它进行初始化 GPIO_TypeDef gpiox = 哪一个GPIO U32 pins = 哪一个引脚 GPIO_MODE_TypeDef mode = 那一种模式 GPIO_TypeDef udp = 使能选择 */
void GPIOx_Init(GPIO_TypeDef gpiox, U32 pin, GPIO_MODE_TypeDef mode, GPIO_UDP_TypeDef udp)
{
    switch(gpiox)
    {
        case GPIOA:
        {
            //IO复用
            GPACON &= ~(1 << pin); /* one pin corresponding one bit*/
            GPACON |= mode << pin; /* set mode*/

            break;
        }
        case GPIOB:
        {
            /* mode elect */
            /* one pin corrsponding two bit*/
            GPBCON &= ~(3 << (pin * 2));
            GPBCON |= (mode << (pin * 2));
            //up/down action set
            GPBUDP &= ~(3 << (pin * 2));
            GPBUDP |= (udp << (pin * 2));

            break;
        }
        case GPIOC:
        {
            /* mode elect */
            /* one pin corrsponding two bit*/
            GPCCON &= ~(3 << (pin * 2));
            GPCCON |= (mode << (pin * 2));
            //up/down action set
            GPCUDP &= ~(3 << (pin * 2));
            GPCUDP |= (udp << (pin * 2));
            break;
        }
        case GPIOD:
        {
            /* mode elect */
            /* one pin corrsponding two bit*/
            GPDCON &= ~(3 << (pin * 2));
            GPDCON |= (mode << (pin * 2));
            //up/down action set
            GPDUDP &= ~(3 << (pin*2));
            GPDUDP |= (udp << (pin*2));
            break;
        }
        case GPIOE:
        {
            /* mode elect */
            /* one pin corrsponding two bit*/
            GPECON &= ~(3 << (pin * 2));
            GPECON |= (mode << (pin * 2));
            //up/down action set
            GPEUDP &= ~(3 << (pin * 2));
            GPEUDP |= (udp << (pin *2));
            break;
        }
        case GPIOF:
        {
            /* mode elect */
            /* one pin corrsponding two bit*/
            GPFCON &= ~(3 << (pin * 2));
            GPFCON |= (mode << (pin * 2));
            //up/down action set
            GPFUDP &= ~(3 << (pin * 2));
            GPFUDP |= (udp << (pin * 2));
            break;
        }
        case GPIOG:
        {
            /* mode elect */
            /* one pin corrsponding two bit*/
            GPGCON &= ~(3 << (pin * 2));
            GPGCON |= (mode << (pin * 2));
            //up/down action set
            GPGUDP &= ~(3 << (pin * 2));
            GPGUDP |= (udp << (pin * 2));
            break;
        }
        case GPIOH:
        {
            /* mode elect */
            /* one pin corrsponding two bit*/
            GPHCON &= ~(3 << (pin * 2));
            GPHCON |= (mode << (pin * 2));
            //up/down action set
            GPHUDP &= ~(3 << (pin * 2));
            GPHUDP |= (udp << (pin *2));
            break;
        }
        case GPIOJ:
        {
            /* mode elect */
            /* one pin corrsponding two bit*/
            GPJCON &= ~(3 << (pin * 2));
            GPJCON |= (mode << (pin * 2));
            //up/down action set
            GPJUDP &= ~(3 << (pin * 2));
            GPJUDP |= (udp << (pin * 2));
            break;
        }
        case GPIOK:
        {
            /* mode elect */
            /* one pin corrsponding two bit*/
            GPKCON &= ~(3 << (pin * 2));
            GPKCON |= (mode << (pin * 2));
            //up/down action set
            GPKUDP &= ~(3 << (pin * 2));
            GPKUDP |= (udp << (pin * 2));
            break;
        }
        case GPIOL:
        {
            /* mode elect */
            /* one pin corrsponding two bit*/
            GPLCON &= ~(3 << (pin * 2));
            GPLCON |= (mode << (pin * 2));
            //up/down action set
            GPLUDP &= ~(3 << (pin * 2));
            GPLUDP |= (udp << (pin * 2));
            break;
        }
        case GPIOM:
        {
            /* mode elect */
            /* one pin corrsponding two bit*/
            GPMCON &= ~(3 << (pin * 2));
            GPMCON |= (mode << (pin * 2));
            //up/down action set
            GPMUDP &= ~(3 << (pin * 2));
            GPMUDP |= (udp << (pin * 2));
            break;
        }
        default:
        {

        }
    }
}
/*对某一位进行置位*/
void GPIO_SetBit(GPIO_TypeDef gpiox, U32 pin, BitAction bit)
{
    switch(gpiox)
    {
        case GPIOA:
        {
            if (bit != Bit_RESET)
            {
                GPADAT &= ~(1 << pin);
            }   
            else
            {
                GPADAT |= (1 << pin);
            }
            break;
        }
        case GPIOB:
        {
            if (bit != Bit_RESET)
            {
                GPBDAT &= ~(1 << pin);
            }   
            else
            {
                GPBDAT |= (1 << pin);
            }
            break;
        }
        case GPIOC:
        {
            if (bit != Bit_RESET)
            {
                GPCDAT &= ~(1 << pin);
            }   
            else
            {
                GPCDAT |= (1 << pin);
            }
            break;
        }
        case GPIOD:
        {
            if (bit != Bit_RESET)
            {
                GPDDAT &= ~(1 << pin);
            }   
            else
            {
                GPDDAT |= (1 << pin);
            }
            break;
        }
        case GPIOE:
        {
            if (bit != Bit_RESET)
            {
                GPEDAT &= ~(1 << pin);
            }   
            else
            {
                GPEDAT |= (1 << pin);
            }
            break;
        }
        case GPIOF:
        {
            if (bit != Bit_RESET)
            {
                GPFDAT &= ~(1 << pin);
            }   
            else
            {
                GPFDAT |= (1 << pin);
            }
            break;
        }
        case GPIOG:
        {
            if (bit != Bit_RESET)
            {
                GPGDAT &= ~(1 << pin);
            }   
            else
            {
                GPGDAT |= (1 << pin);
            }
            break;
        }
        case GPIOH:
        {
            if (bit != Bit_RESET)
            {
                GPHDAT &= ~(1 << pin);
            }   
            else
            {
                GPHDAT |= (1 << pin);
            }
            break;
        }
        case GPIOJ:
        {
            if (bit != Bit_RESET)
            {
                GPJDAT &= ~(1 << pin);
            }   
            else
            {
                GPJDAT |= (1 << pin);
            }
            break;
        }
        case GPIOK:
        {
            if (bit != Bit_RESET)
            {
                GPKDAT &= ~(1 << (pin * 2));
            }   
            else
            {
                GPKDAT |= (1 << (pin * 2));
            }
            break;
        }
        case GPIOL:
        {
            if (bit != Bit_RESET)
            {
                GPLDAT &= ~(1 << pin);
            }   
            else
            {
                GPLDAT |= (1 << pin);
            }
            break;
        }
        case GPIOM:
        {
            if (bit != Bit_RESET)
            {
                GPMDAT &= ~(1 << pin);
            }   
            else
            {
                GPMDAT |= (1 << pin);
            }
            break;
        }
        default:
        {

        }
    }
}
/*对某一位进行读取*/
BitAction GPIO_GetBit(GPIO_TypeDef gpiox, U32 pin)
{
    switch(gpiox)

    {
        case GPIOA:
        {
            if ((GPADAT & (1 << pin)) == (1 << pin))
            {
                return Bit_SET;
            }   
            else
            {
                return Bit_RESET;
            }
            //break;
        }
        case GPIOB:
        {
            if ((GPBDAT & (1 << pin)) == (1 << pin))
            {
                return Bit_SET;
            }   
            else
            {
                return Bit_RESET;
            }
            //break;
        }
        case GPIOC:
        {
            if ((GPCDAT & (1 << pin)) == (1 << pin))
            {
                return Bit_SET;
            }   
            else
            {
                return Bit_RESET;
            }
            //break;
        }
        case GPIOD:
        {
            if ((GPDDAT & (1 << pin)) == (1 << pin))
            {
                return Bit_SET;
            }   
            else
            {
                return Bit_RESET;
            }
            //break;
        }
        case GPIOE:
        {
            if ((GPEDAT & (1 << pin)) == (1 << pin))
            {
                return Bit_SET;
            }   
            else
            {
                return Bit_RESET;
            }
            //break;
        }
        case GPIOF:
        {
            if ((GPFDAT & (1 << pin)) == (1 << pin))
            {
                return Bit_SET;
            }   
            else
            {
                return Bit_RESET;
            }
            //break;
        }
        case GPIOG:
        {
            if ((GPGDAT & (1 << pin)) == (1 << pin))
            {
                return Bit_SET;
            }   
            else
            {
                return Bit_RESET;
            }
            //break;
        }
        case GPIOH:
        {
            if ((GPHDAT & (1 << pin)) == (1 << pin))
            {
                return Bit_SET;
            }   
            else
            {
                return Bit_RESET;
            }
            //break;
        }
        case GPIOJ:
        {
            if ((GPJDAT & (1 << pin)) == (1 << pin))
            {
                return Bit_SET;
            }   
            else
            {
                return Bit_RESET;
            }
            //break;
        }
        case GPIOK:
        {
            if ((GPKDAT & (1 << (pin * 2))) == (1 << (pin * 2)))
            {
                return Bit_SET;
            }   
            else
            {
                return Bit_RESET;
            }
            //break;
        }
        case GPIOL:
        {
            if ((GPLDAT & (1 << pin)) == (1 << pin))
            {
                return Bit_SET;
            }   
            else
            {
                return Bit_RESET;
            }
            //break;
        }
        case GPIOM:
        {
            if ((GPMDAT & (1 << pin)) == (1 << pin))
            {
                return Bit_SET;
            }   
            else
            {
                return Bit_RESET;
            }
            //break;
        }
        default:
        {
            break;
        }
    }
    return Bit_RESET;
}

你可能感兴趣的:(ARM,S3C2451,GPIO编程)