Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Includes ------------------------------------------------------------------*/
- #include "GPIO_mapping.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- // Pin Mode and Speed
- #define OUT (0) // Output
- #define IN (1) // Input
- #define GPPP (0) // General purpose Push-Pull (Output)
- #define GPOD (1) // General purpose Open-Drain (Output)
- #define AFPP (2) // Alternate function Push-Pull (Output)
- #define AFOD (3) // Alternate function Open-Drain (Output)
- #define FL (0) // Floating (Input)
- #define AN (1) // Analog (Input)
- #define PD (2) // Pull-Down (Input)
- #define PU (3) // Pull-Up (Input)
- #define S2M (2) // 2MHz
- #define S10M (10) // 10MHz
- #define S50M (50) // 50MHz
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Config a pin mode and speed.
- * @param PortPin: select a pin to set.
- * This parameter should be: 0 ~ 79
- * 0~15:PA0~PA15; 16~31:PB0~PB15; 32~47:PC0~PC15;
- * 48~63:PD0~PD15; 64~79:PE0~PE15
- * @param INout: Input or Output.
- * This parameter should be: 0(Output) or 1(Input).
- * @param Mode: Pin mode.
- * This parameter should be: 0~3.
- * 0: GPPP or FL.
- * 1: GPOD or AN.
- * 2: AFPP or PD.
- * 3: AFOD or PU.
- * @param Speed: Pin speed.
- * This parameter should be: 2, 10 or 50.
- * 2: 2MHz.
- * 10: 10MHz.
- * 50: 50MHz.
- * @retval None
- */
- void Pin_Mod(u8 PortPin, u8 INout, u8 Mode, u8 Speed)
- {
- /* Structure Declarations */
- GPIO_InitTypeDef GPIO_InitStructure;
- // GPIO_Speed
- switch(Speed)
- {
- case S2M: // S2M:2
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- break;
- case S10M: // S10M:10
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- break;
- case S50M: // S50M:50
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- break;
- default:
- break;
- }
- // GPIO_Mode
- if(INout == OUT) // OUT:0
- {
- switch(Mode)
- {
- case GPPP: // GPPP:0
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- break;
- case GPOD: // GPOD:1
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
- break;
- case AFPP: // AFPP:2
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- break;
- case AFOD: // AFOD:3
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
- break;
- default:
- break;
- }
- }
- else if(INout == IN) // IN:1
- {
- switch(Mode)
- {
- case FL: // FL:0
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- break;
- case AN: // AN:1
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- break;
- case PD: // PD:2
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- break;
- case PU: // PU:3
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- break;
- default:
- break;
- }
- }
- // GPIO_Pin & GPIO_Init() function.
- if(PortPin <= 15) // Port-A: 0~15
- {
- GPIO_InitStructure.GPIO_Pin = ((uint16_t)(0x0001 << PortPin));
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- else if(PortPin <= 31) // Port-B: 16~31
- {
- GPIO_InitStructure.GPIO_Pin = ((uint16_t)(0x0001 << (PortPin - 16)));
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- else if(PortPin <= 47) // Port-C: 32~47
- {
- GPIO_InitStructure.GPIO_Pin = ((uint16_t)(0x0001 << (PortPin - 32)));
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- }
- else if(PortPin <= 63) // Port-D: 48~63
- {
- GPIO_InitStructure.GPIO_Pin = ((uint16_t)(0x0001 << (PortPin - 48)));
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- }
- else if(PortPin <= 79) // Port-E: 64~79
- {
- GPIO_InitStructure.GPIO_Pin = ((uint16_t)(0x0001 << (PortPin - 64)));
- GPIO_Init(GPIOE, &GPIO_InitStructure);
- }
- else /* Null */; // Out of range(0~79)
- }
- #undef OUT
- #undef IN
- #undef GPPP
- #undef GPOD
- #undef AFPP
- #undef AFOD
- #undef FL
- #undef AN
- #undef PD
- #undef PU
- #undef S2M
- #undef S10M
- #undef S50M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement