Advertisement
phillip_bourdon234

GPIO_Driver.h

Feb 28th, 2021
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.73 KB | None | 0 0
  1. #ifndef GPIO_Driver
  2. #define GPIO_Driver
  3.  
  4. #include "stm32f10x.h"
  5. #include <stdint.h>
  6.  
  7. //STATES
  8. #define LOW 0
  9. #define HIGH 1
  10.  
  11. //PORT NAMES
  12. #define PORTA           GPIOA
  13. #define PORTB           GPIOB
  14. #define PORTC           GPIOC
  15. #define PORTD           GPIOD
  16. #define PORTE           GPIOE
  17. #define PORTF           GPIOF
  18. #define PORTG           GPIOG
  19.  
  20. //PIN MODES
  21. #define OUTPUT                              ((int32_t)0x01)
  22. #define INPUT                                   ((uint32_t)0x00)
  23.  
  24. //INPUT TYPES
  25. #define INPUT_ANALOG                        ((uint32_t)0x00)
  26. #define INPUT_FLOATING                  ((uint32_t)0x01)
  27. #define INPUT_PU_PD                         ((uint32_t)0x02)
  28.  
  29. //OUTPUT TYPES
  30. #define OUTPUT_GEN_PURPOSE          ((uint32_t)0x00)
  31. #define OUTPUT_OD                       ((uint32_t)0x01)
  32. #define OUTPUT_ALT_FUNCTION         ((uint32_t)0x02)
  33. #define OUTPUT_ALT_FUNCTION_OD  ((uint32_t)0x03)
  34.  
  35. //PIN speed/slew rates
  36. #define SPEED_2MHZ                      ((uint32_t)0x02)
  37. #define SPEED_10MHZ                     ((uint32_t)0x01)
  38. #define SPEED_50MHZ                     ((uint32_t)0x03)
  39.  
  40. //CLOCK enables
  41. #define CLOCK_GPIO_EN_ALT_FUNC      (RCC->APB2ENR |= (1<<0))
  42. #define CLOCK_GPIO_EN_PORTA         (RCC->APB2ENR |= (1<<2))
  43. #define CLOCK_GPIO_EN_PORTB         (RCC->APB2ENR |= (1<<3))
  44. #define CLOCK_GPIO_EN_PORTC         (RCC->APB2ENR |= (1<<4))
  45. #define CLOCK_GPIO_EN_PORTD         (RCC->APB2ENR |= (1<<5))
  46.  
  47. //CONFIG REG CNF AND MODE
  48. #define MODE_POS_BIT1       (PINPOS[pinNumber])
  49. #define MODE_POS_BIT2           (PINPOS[pinNumber] + 1)
  50. #define CNF_POS_BIT1            (PINPOS[pinNumber] + 2)
  51. #define CNF_POS_BIT2            (PINPOS[pinNumber] + 3)
  52.  
  53. //CONFIG STRUCT
  54. typedef struct
  55. {
  56.     GPIO_TypeDef *port;
  57.     uint32_t pin;
  58.     uint32_t mode;
  59.     uint32_t mode_type;
  60.     uint32_t pull;
  61.     uint32_t speed;
  62.     uint32_t alt;
  63. }   GPIO_TYPE;
  64.  
  65. //EDGE ENUM
  66. typedef enum
  67. {
  68.     RISING_EDGE,
  69.     FALLING_EDGE,
  70.     RISING_FALLING_EDGE,
  71. }edge_select;
  72.  
  73. //FUNCTION PROTOTYPES
  74.  
  75. //====================================
  76. //       GPIO CONFIGURATIONS
  77. //====================================
  78.  
  79. void config_pin(GPIO_TypeDef *port, uint32_t pinNumber, uint32_t modeType, uint32_t pinSpeed, uint32_t mode);
  80. void init_gpio(GPIO_TYPE gpio_type);
  81.  
  82. //=====================================
  83. //         GPIO COMMANDS
  84. //=====================================
  85.  
  86. void pin_write(GPIO_TypeDef *port, uint32_t pinNumber, uint32_t state);
  87. void pin_toggle(GPIO_TypeDef *port, uint32_t pinNumber);
  88. uint32_t pin_read(GPIO_TypeDef *port, uint32_t pinNumber);
  89.  
  90. //=====================================
  91. //      GPIO INTERRUPT COMMANDS
  92. //=====================================
  93.  
  94. void config_gpio_interrupt(GPIO_TypeDef *port, uint32_t pinNumber, edge_select edge);
  95. void enable_gpio_interrupt(uint32_t pinNumber, IRQn_Type irqNumber);
  96. void clear_gpio_interrupt(uint32_t pinNumber);
  97.  
  98. //=====================================
  99. //        SLEEP FUNCTIONS
  100. //=====================================
  101.  
  102. void goToSleep(void);
  103.  
  104.  
  105. #endif
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement