gpio.c

来自「基于Arm Developer suite 1.2开发」· C语言 代码 · 共 194 行

C
194
字号
/******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
* File Name          : gpio.c
* Author             : MCD Application Team
* Date First Issued  : 06/08/2003
* Description        : This file provides all the GPIO software functions
********************************************************************************
* History:
*  01/01/2004 : V1.2
*  14/07/2004 : V1.3
*******************************************************************************/

#include "gpio.h"

/*******************************************************************************
* Function Name  : GPIO_Config
* Description    : Configure the GPIO port pins
* Input 1        : GPIOx (x can be 0,1 or 2) the desired port
* Input 2        : Port_Pins : pins placements
* Input 3        : Pins Mode
* Output         : None
* Return         : None
*******************************************************************************/
/* 1+4+3+21=29
GPIO_Config	[0xe3520007]   cmp      r2,#7            1s
20000338	[0x908ff102]   addls    pc,pc,r2,lsl #2  2s and 3s+1N=4s
2000033c	[0xea000010]   b        0x20000384  ; (GPIO_Config + 0x50) 3s
20000340	[0xea000006]   b        0x20000360  ; (GPIO_Config + 0x2c)
20000344	[0xea00000f]   b        0x20000388  ; (GPIO_Config + 0x54)
20000348	[0xea000018]   b        0x200003b0  ; (GPIO_Config + 0x7c)
2000034c	[0xea000021]   b        0x200003d8  ; (GPIO_Config + 0xa4) 3s
20000350	[0xea00002a]   b        0x20000400  ; (GPIO_Config + 0xcc)
20000354	[0xea000033]   b        0x20000428  ; (GPIO_Config + 0xf4)
20000358	[0xea00003c]   b        0x20000450  ; (GPIO_Config + 0x11c)
2000035c	[0xea000045]   b        0x20000478  ; (GPIO_Config + 0x144)
20000360	[0xe1d020b0]   ldrh     r2,[r0,#0]
20000364	[0xe1c22001]   bic      r2,r2,r1
20000368	[0xe1c020b0]   strh     r2,[r0,#0]
2000036c	[0xe1d020b4]   ldrh     r2,[r0,#4]
20000370	[0xe1c22001]   bic      r2,r2,r1
20000374	[0xe1c020b4]   strh     r2,[r0,#4]
20000378	[0xe1d020b8]   ldrh     r2,[r0,#8]
2000037c	[0xe1c21001]   bic      r1,r2,r1
20000380	[0xe1c010b8]   strh     r1,[r0,#8]
20000384	[0xe1a0f00e]   mov      pc,r14
20000388	[0xe1d020b0]   ldrh     r2,[r0,#0]
2000038c	[0xe1822001]   orr      r2,r2,r1
20000390	[0xe1c020b0]   strh     r2,[r0,#0]
20000394	[0xe1d020b4]   ldrh     r2,[r0,#4]
20000398	[0xe1c22001]   bic      r2,r2,r1
2000039c	[0xe1c020b4]   strh     r2,[r0,#4]
200003a0	[0xe1d020b8]   ldrh     r2,[r0,#8]
200003a4	[0xe1c21001]   bic      r1,r2,r1
200003a8	[0xe1c010b8]   strh     r1,[r0,#8]
200003ac	[0xeafffff4]   b        0x20000384  ; (GPIO_Config + 0x50)
200003b0	[0xe1d020b0]   ldrh     r2,[r0,#0]
200003b4	[0xe1c22001]   bic      r2,r2,r1
200003b8	[0xe1c020b0]   strh     r2,[r0,#0]
200003bc	[0xe1d020b4]   ldrh     r2,[r0,#4]
200003c0	[0xe1822001]   orr      r2,r2,r1
200003c4	[0xe1c020b4]   strh     r2,[r0,#4]
200003c8	[0xe1d020b8]   ldrh     r2,[r0,#8]
200003cc	[0xe1c21001]   bic      r1,r2,r1
200003d0	[0xe1c010b8]   strh     r1,[r0,#8]
200003d4	[0xeaffffea]   b        0x20000384  ; (GPIO_Config + 0x50)
200003d8	[0xe1d020b0]   ldrh     r2,[r0,#0]  1s_1n+1i=3s
200003dc	[0xe1822001]   orr      r2,r2,r1    1s
200003e0	[0xe1c020b0]   strh     r2,[r0,#0]  2n=2s
200003e4	[0xe1d020b4]   ldrh     r2,[r0,#4]  3s
200003e8	[0xe1822001]   orr      r2,r2,r1    1s
200003ec	[0xe1c020b4]   strh     r2,[r0,#4]  2s
200003f0	[0xe1d020b8]   ldrh     r2,[r0,#8]  3s 
200003f4	[0xe1c21001]   bic      r1,r2,r1    1s
200003f8	[0xe1c010b8]   strh     r1,[r0,#8]  2s
200003fc	[0xeaffffe0]   b        0x20000384  ; (GPIO_Config + 0x50) 3s
/////21s
20000400	[0xe1d020b0]   ldrh     r2,[r0,#0]
20000404	[0xe1c22001]   bic      r2,r2,r1
20000408	[0xe1c020b0]   strh     r2,[r0,#0]
2000040c	[0xe1d020b4]   ldrh     r2,[r0,#4]
20000410	[0xe1c22001]   bic      r2,r2,r1
20000414	[0xe1c020b4]   strh     r2,[r0,#4]
20000418	[0xe1d020b8]   ldrh     r2,[r0,#8]
2000041c	[0xe1821001]   orr      r1,r2,r1
20000420	[0xe1c010b8]   strh     r1,[r0,#8]
20000424	[0xeaffffd6]   b        0x20000384  ; (GPIO_Config + 0x50)
20000428	[0xe1d020b0]   ldrh     r2,[r0,#0]
2000042c	[0xe1822001]   orr      r2,r2,r1
20000430	[0xe1c020b0]   strh     r2,[r0,#0]
20000434	[0xe1d020b4]   ldrh     r2,[r0,#4]
20000438	[0xe1c22001]   bic      r2,r2,r1
2000043c	[0xe1c020b4]   strh     r2,[r0,#4]
20000440	[0xe1d020b8]   ldrh     r2,[r0,#8]
20000444	[0xe1821001]   orr      r1,r2,r1
20000448	[0xe1c010b8]   strh     r1,[r0,#8]
2000044c	[0xeaffffcc]   b        0x20000384  ; (GPIO_Config + 0x50)
20000450	[0xe1d020b0]   ldrh     r2,[r0,#0]
20000454	[0xe1c22001]   bic      r2,r2,r1
20000458	[0xe1c020b0]   strh     r2,[r0,#0]
2000045c	[0xe1d020b4]   ldrh     r2,[r0,#4]
20000460	[0xe1822001]   orr      r2,r2,r1
20000464	[0xe1c020b4]   strh     r2,[r0,#4]
20000468	[0xe1d020b8]   ldrh     r2,[r0,#8]
2000046c	[0xe1821001]   orr      r1,r2,r1
20000470	[0xe1c010b8]   strh     r1,[r0,#8]
20000474	[0xeaffffc2]   b        0x20000384  ; (GPIO_Config + 0x50)
20000478	[0xe1d020b0]   ldrh     r2,[r0,#0]

*/
//1+4+3+21=29
void GPIO_Config (GPIO_TypeDef *GPIOx, u16 Port_Pins, GpioPinMode_TypeDef GPIO_Mode)
{
  switch (GPIO_Mode)  //cmp      r2,#7  addls    pc,pc,r2,lsl #2 
  {//b        0x20000384  b 0x20000360 
    case GPIO_HI_AIN_TRI:  
      GPIOx->PC0&=~Port_Pins; //20000360:ldrh     r2,[r0,#0]
      GPIOx->PC1&=~Port_Pins;
      GPIOx->PC2&=~Port_Pins;
      break;

    case GPIO_IN_TRI_TTL:  
      GPIOx->PC0|=Port_Pins;
      GPIOx->PC1&=~Port_Pins;
      GPIOx->PC2&=~Port_Pins;
      break;  ///b        0x20000384

    case GPIO_IN_TRI_CMOS:
      GPIOx->PC0&=~Port_Pins;
      GPIOx->PC1|=Port_Pins;
      GPIOx->PC2&=~Port_Pins;
      break;

    case GPIO_INOUT_WP:
      GPIOx->PC0|=Port_Pins;
      GPIOx->PC1|=Port_Pins;
      GPIOx->PC2&=~Port_Pins;
      break;

    case GPIO_OUT_OD:
      GPIOx->PC0&=~Port_Pins;
      GPIOx->PC1&=~Port_Pins;
      GPIOx->PC2|=Port_Pins;
      break;

    case GPIO_OUT_PP:
      GPIOx->PC0|=Port_Pins;
      GPIOx->PC1&=~Port_Pins;
      GPIOx->PC2|=Port_Pins;
      break;

    case GPIO_AF_OD:
      GPIOx->PC0&=~Port_Pins;
      GPIOx->PC1|=Port_Pins;
      GPIOx->PC2|=Port_Pins;
      break;

    case GPIO_AF_PP:
      GPIOx->PC0|=Port_Pins;
      GPIOx->PC1|=Port_Pins;
      GPIOx->PC2|=Port_Pins;
      break;
  }
}

/*******************************************************************************
* Function Name  : GPIO_BitWrite
* Description    : Set or reset the selected port pin
* Input 1        : Selected GPIO port
* Input 2        : Pin number
* Input 3        : bit value
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_BitWrite(GPIO_TypeDef *GPIOx, u8 Port_Pin, u8 Port_Val)
{
  if (Port_Val&0x01) GPIOx->PD |= 1<<Port_Pin; else GPIOx->PD &= ~(1<<Port_Pin);
}

/*******************************************************************************
* Function Name  : GPIO_ByteWrite
* Description    : Write byte value to the selected PD register
* Input 1        : Selected GPIO port
* Input 2        : GPIO_MSB or GPIO_LSB
* Input 3        : Byte value
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_ByteWrite(GPIO_TypeDef *GPIOx, u8 Port_Byte, u8 Port_Val)
{
  GPIOx->PD = Port_Byte ? (GPIOx->PD&0x00FF) | ((u16)Port_Val<<8)
                        : (GPIOx->PD&0xFF00) | Port_Val;
}

/******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?