Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.10 KB | None | 0 0
  1.  
  2. /* Includes ------------------------------------------------------------------*/
  3. #include "GPIO_mapping.h"
  4.  
  5. /* Private typedef -----------------------------------------------------------*/
  6. /* Private define ------------------------------------------------------------*/
  7. // Pin Mode and Speed
  8. #define OUT     (0) // Output
  9. #define IN      (1) // Input
  10.  
  11. #define GPPP    (0) // General purpose Push-Pull (Output)
  12. #define GPOD    (1) // General purpose Open-Drain (Output)
  13. #define AFPP    (2) // Alternate function Push-Pull (Output)
  14. #define AFOD    (3) // Alternate function Open-Drain (Output)
  15.  
  16. #define FL      (0) // Floating (Input)
  17. #define AN      (1) // Analog (Input)
  18. #define PD      (2) // Pull-Down (Input)
  19. #define PU      (3) // Pull-Up (Input)
  20.  
  21. #define S2M     (2) // 2MHz
  22. #define S10M    (10)    // 10MHz
  23. #define S50M    (50)    // 50MHz
  24.  
  25. /* Private macro -------------------------------------------------------------*/
  26. /* Private variables ---------------------------------------------------------*/
  27. /* Private function prototypes -----------------------------------------------*/
  28. /* Private functions ---------------------------------------------------------*/
  29.  
  30. /**
  31.   * @brief  Config a pin mode and speed.
  32.   * @param  PortPin: select a pin to set.
  33.   *         This parameter should be: 0 ~ 79
  34.   *          0~15:PA0~PA15; 16~31:PB0~PB15; 32~47:PC0~PC15;
  35.   *         48~63:PD0~PD15; 64~79:PE0~PE15
  36.   * @param  INout: Input or Output.
  37.   *         This parameter should be: 0(Output) or 1(Input).
  38.   * @param  Mode: Pin mode.
  39.   *         This parameter should be: 0~3.
  40.   *         0: GPPP or FL.
  41.   *         1: GPOD or AN.
  42.   *         2: AFPP or PD.
  43.   *         3: AFOD or PU.
  44.   * @param  Speed: Pin speed.
  45.   *         This parameter should be: 2, 10 or 50.
  46.   *          2:  2MHz.
  47.   *         10: 10MHz.
  48.   *         50: 50MHz.
  49.   * @retval None
  50.   */
  51. void Pin_Mod(u8 PortPin, u8 INout, u8 Mode, u8 Speed)
  52. {
  53.     /* Structure Declarations */
  54.     GPIO_InitTypeDef GPIO_InitStructure;
  55.  
  56.     // GPIO_Speed
  57.     switch(Speed)
  58.     {
  59.         case S2M:   // S2M:2
  60.             GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  61.             break;
  62.         case S10M:  // S10M:10
  63.             GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  64.             break;
  65.         case S50M:  // S50M:50
  66.             GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  67.             break;
  68.         default:
  69.             break;
  70.     }
  71.  
  72.     // GPIO_Mode
  73.     if(INout == OUT)    // OUT:0
  74.     {
  75.         switch(Mode)
  76.         {
  77.             case GPPP:  // GPPP:0
  78.                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  79.                 break;
  80.             case GPOD:  // GPOD:1
  81.                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  82.                 break;
  83.             case AFPP:  // AFPP:2
  84.                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  85.                 break;
  86.             case AFOD:  // AFOD:3
  87.                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  88.                 break;
  89.             default:
  90.                 break;
  91.         }
  92.     }
  93.     else if(INout == IN)    // IN:1
  94.     {
  95.         switch(Mode)
  96.         {
  97.             case FL:    // FL:0
  98.                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  99.                 break;
  100.             case AN:    // AN:1
  101.                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  102.                 break;
  103.             case PD:    // PD:2
  104.                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  105.                 break;
  106.             case PU:    // PU:3
  107.                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  108.                 break;
  109.             default:
  110.                 break;
  111.         }
  112.     }
  113.  
  114.     // GPIO_Pin & GPIO_Init() function.
  115.     if(PortPin <= 15)       // Port-A:  0~15
  116.     {
  117.         GPIO_InitStructure.GPIO_Pin = ((uint16_t)(0x0001 << PortPin));
  118.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  119.     }
  120.     else if(PortPin <= 31)  // Port-B: 16~31
  121.     {
  122.         GPIO_InitStructure.GPIO_Pin = ((uint16_t)(0x0001 << (PortPin - 16)));
  123.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  124.     }
  125.     else if(PortPin <= 47)  // Port-C: 32~47
  126.     {
  127.         GPIO_InitStructure.GPIO_Pin = ((uint16_t)(0x0001 << (PortPin - 32)));
  128.         GPIO_Init(GPIOC, &GPIO_InitStructure);
  129.     }
  130.     else if(PortPin <= 63)  // Port-D: 48~63
  131.     {
  132.         GPIO_InitStructure.GPIO_Pin = ((uint16_t)(0x0001 << (PortPin - 48)));
  133.         GPIO_Init(GPIOD, &GPIO_InitStructure);
  134.     }
  135.     else if(PortPin <= 79)  // Port-E: 64~79
  136.     {
  137.         GPIO_InitStructure.GPIO_Pin = ((uint16_t)(0x0001 << (PortPin - 64)));
  138.         GPIO_Init(GPIOE, &GPIO_InitStructure);
  139.     }
  140.     else /* Null */;        // Out of range(0~79)
  141. }
  142.  
  143. #undef OUT
  144. #undef IN
  145.  
  146. #undef GPPP
  147. #undef GPOD
  148. #undef AFPP
  149. #undef AFOD
  150.  
  151. #undef FL
  152. #undef AN
  153. #undef PD
  154. #undef PU
  155.  
  156. #undef S2M
  157. #undef S10M
  158. #undef S50M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement