У меня другой подход. #ifndef _PORTS_HEADER_
#define _PORTS_HEADER_
#include "test.h"
#include "stm32f0xx.h"
#define INPUT_MODE 0
#define OUTPUT_MODE 1
#define ALTER_MODE 2
#define ANALOG_MODE 3
#define PUSH_PULL_OTYPE 0
#define OPEN_DRAIN_OTYPE 1
#define LOW_SPEED 0
#define MED_SPEED 1
#define HIGH_SPEED 3
#define PULL_NOT 0
#define PULL_UP 1
#define PULL_DOWN 2
#define SET_MODER(PORT, IO, MODE) do { \
GPIO##PORT->MODER = \
(GPIO##PORT->MODER & ~(3<<(IO*2))) | \
(MODE<<(IO*2) ) ; } while (0)
#define SET_OTYPE(PORT, IO, OTYPE) do { \
GPIO##PORT->OTYPER = \
(GPIO##PORT->OTYPER & ~(1<<IO)) | \
(OTYPE<<IO); } while (0)
#define SET_OSPEED(PORT, IO, SPEED) do { \
GPIO##PORT->OSPEEDR = \
(GPIO##PORT->OSPEEDR & ~(3<<(IO*2)))|\
(SPEED<<(IO*2)); } while (0)
#define SET_PULL(PORT, IO, PULL) do { \
GPIO##PORT->PUPDR = \
(GPIO##PORT->PUPDR & ~(1<<IO)) | \
(PULL<<IO); } while (0)
#define SET_OD(PORT, IO) do { \
GPIO##PORT->BSRR = \
(1<<IO); } while (0)
#define RESET_OD(PORT, IO) do { \
GPIO##PORT->BSRR = \
(1<<(IO+16)); } while (0)
#define GET_ID(PORT, IO) (GPIO##PORT->IDR&(1<<IO))
#define TOGGLE_OD(PORT, IO) do { \
if (GET_ID(PORT,IO)) \
RESET_OD(PORT,IO); \
else \
SET_OD(PORT,IO); } while(0)
#define SET_AFR(PORT, IO, AFR) do { \
if (IO<=7) GPIO##PORT->AFRL= \
(GPIO##PORT->AFRL &~(0xfUL<<(IO*4))) | \
AFR<<(IO*4); \
else GPIO##PORT->AFRH= \
(GPIO##PORT->AFRH&~(0xfUL<<((IO-8)*4)))|\
AFR<<((IO-8)*4); } while (0)
#define AF0_EVENTOUT (0x00UL) /*!< AF0: EVENTOUT Alternate Function mapping */
#define AF0_SWDIO (0x00UL) /*!< AF0: SWDIO Alternate Function mapping */
#define AF0_SWCLK (0x00UL) /*!< AF0: SWCLK Alternate Function mapping */
#define AF0_MCO (0x00UL) /*!< AF0: MCO Alternate Function mapping */
#define AF0_CEC (0x00UL) /*!< AF0: CEC Alternate Function mapping */
#define AF0_CRS (0x00UL) /*!< AF0: CRS Alternate Function mapping */
#define AF0_IR (0x00UL) /*!< AF0: IR Alternate Function mapping */
#define AF0_SPI1 (0x00UL) /*!< AF0: SPI1/I2S1 Alternate Function mapping */
#define AF0_SPI2 (0x00UL) /*!< AF0: SPI2/I2S2 Alternate Function mapping */
#define AF0_TIM1 (0x00UL) /*!< AF0: TIM1 Alternate Function mapping */
#define AF0_TIM3 (0x00UL) /*!< AF0: TIM3 Alternate Function mapping */
#define AF0_TIM14 (0x00UL) /*!< AF0: TIM14 Alternate Function mapping */
#define AF0_TIM15 (0x00UL) /*!< AF0: TIM15 Alternate Function mapping */
#define AF0_TIM16 (0x00UL) /*!< AF0: TIM16 Alternate Function mapping */
#define AF0_TIM17 (0x00UL) /*!< AF0: TIM17 Alternate Function mapping */
#define AF0_TSC (0x00UL) /*!< AF0: TSC Alternate Function mapping */
#define AF0_USART1 (0x00UL) /*!< AF0: USART1 Alternate Function mapping */
#define AF0_USART2 (0x00UL) /*!< AF0: USART2 Alternate Function mapping */
#define AF0_USART3 (0x00UL) /*!< AF0: USART3 Alternate Function mapping */
#define AF0_USART4 (0x00UL) /*!< AF0: USART4 Alternate Function mapping */
#define AF0_USART8 (0x00UL) /*!< AF0: USART8 Alternate Function mapping */
#define AF0_CAN (0x00UL) /*!< AF0: CAN Alternate Function mapping */
#define AF1_TIM3 (0x01UL) /*!< AF1: TIM3 Alternate Function mapping */
#define AF1_TIM15 (0x01UL) /*!< AF1: TIM15 Alternate Function mapping */
#define AF1_USART1 (0x01UL) /*!< AF1: USART1 Alternate Function mapping */
#define AF1_USART2 (0x01UL) /*!< AF1: USART2 Alternate Function mapping */
#define AF1_USART3 (0x01UL) /*!< AF1: USART3 Alternate Function mapping */
#define AF1_USART4 (0x01UL) /*!< AF1: USART4 Alternate Function mapping */
#define AF1_USART5 (0x01UL) /*!< AF1: USART5 Alternate Function mapping */
#define AF1_USART6 (0x01UL) /*!< AF1: USART6 Alternate Function mapping */
#define AF1_USART7 (0x01UL) /*!< AF1: USART7 Alternate Function mapping */
#define AF1_USART8 (0x01UL) /*!< AF1: USART8 Alternate Function mapping */
#define AF1_IR (0x01UL) /*!< AF1: IR Alternate Function mapping */
#define AF1_CEC (0x01UL) /*!< AF1: CEC Alternate Function mapping */
#define AF1_EVENTOUT (0x01UL) /*!< AF1: EVENTOUT Alternate Function mapping */
#define AF1_I2C1 (0x01UL) /*!< AF1: I2C1 Alternate Function mapping */
#define AF1_I2C2 (0x01UL) /*!< AF1: I2C2 Alternate Function mapping */
#define AF1_TSC (0x01UL) /*!< AF1: TSC Alternate Function mapping */
#define AF1_SPI1 (0x01UL) /*!< AF1: SPI1 Alternate Function mapping */
#define AF1_SPI2 (0x01UL) /*!< AF1: SPI2 Alternate Function mapping */
#define AF2_TIM1 (0x02UL) /*!< AF2: TIM1 Alternate Function mapping */
#define AF2_TIM2 (0x02UL) /*!< AF2: TIM2 Alternate Function mapping */
#define AF2_TIM16 (0x02UL) /*!< AF2: TIM16 Alternate Function mapping */
#define AF2_TIM17 (0x02UL) /*!< AF2: TIM17 Alternate Function mapping */
#define AF2_EVENTOUT (0x02UL) /*!< AF2: EVENTOUT Alternate Function mapping */
#define AF2_USART5 (0x02UL) /*!< AF2: USART5 Alternate Function mapping */
#define AF2_USART6 (0x02UL) /*!< AF2: USART6 Alternate Function mapping */
#define AF2_USART7 (0x02UL) /*!< AF2: USART7 Alternate Function mapping */
#define AF2_USART8 (0x02UL) /*!< AF2: USART8 Alternate Function mapping */
#define AF3_EVENTOUT (0x03UL) /*!< AF3: EVENTOUT Alternate Function mapping */
#define AF3_TSC (0x03UL) /*!< AF3: TSC Alternate Function mapping */
#define AF3_TIM15 (0x03UL) /*!< AF3: TIM15 Alternate Function mapping */
#define AF3_I2C1 (0x03UL) /*!< AF3: I2C1 Alternate Function mapping */
#define AF4_TIM14 (0x04UL) /*!< AF4: TIM14 Alternate Function mapping */
#define AF4_USART4 (0x04UL) /*!< AF4: USART4 Alternate Function mapping */
#define AF4_USART3 (0x04UL) /*!< AF4: USART3 Alternate Function mapping */
#define AF4_CRS (0x04UL) /*!< AF4: CRS Alternate Function mapping */
#define AF4_CAN (0x04UL) /*!< AF4: CAN Alternate Function mapping */
#define AF4_I2C1 (0x04UL) /*!< AF4: I2C1 Alternate Function mapping */
#define AF4_USART5 (0x04UL) /*!< AF4: USART5 Alternate Function mapping */
#define AF5_TIM15 (0x05UL) /*!< AF5: TIM15 Alternate Function mapping */
#define AF5_TIM16 (0x05UL) /*!< AF5: TIM16 Alternate Function mapping */
#define AF5_TIM17 (0x05UL) /*!< AF5: TIM17 Alternate Function mapping */
#define AF5_SPI2 (0x05UL) /*!< AF5: SPI2 Alternate Function mapping */
#define AF5_I2C2 (0x05UL) /*!< AF5: I2C2 Alternate Function mapping */
#define AF5_MCO (0x05UL) /*!< AF5: MCO Alternate Function mapping */
#define AF5_USART6 (0x05UL) /*!< AF5: USART6 Alternate Function mapping */
#define AF6_EVENTOUT (0x06UL) /*!< AF6: EVENTOUT Alternate Function mapping */
#define AF7_COMP1 (0x07UL) /*!< AF7: COMP1 Alternate Function mapping */
#define AF7_COMP2 (0x07UL) /*!< AF7: COMP2 Alternate Function mapping */
// 1 : VDD
// 2: PC13 - not used
// 3: PC14 - not used
// 4: PC15 - not used
// 5: OSC_IN - not changed
// 6: OSC_OUT - not changed
// 7: NRST
// 8: VSSA
// 9: VDDA
// 10: PA0/USART4_TX (USB)
// 11: PA1/USART4_RX (USB)
#define INIT_USB_PINS() do { \
SET_MODER(A,0,ALTER_MODE); \
SET_OTYPE(A,0,PUSH_PULL_OTYPE); \
SET_OSPEED(A,0,HIGH_SPEED); \
SET_PULL(A,0,PULL_UP); \
SET_AFR(A,0,AF1_USART1); \
SET_MODER(A,1,INPUT_MODE); \
SET_PULL(A,1,PULL_UP); \
SET_AFR(A,1,AF1_USART1); \
} while(0)
#define DEINIT_USB_PINS() do { \
} while (0)
// 12: PA2/USART2_TX (BT)
// 13: PA3/USART2_RX (BT)
#define INIT_BT_PINS() do { \
SET_MODER(A,2,ALTER_MODE); \
SET_OTYPE(A,2,PUSH_PULL_OTYPE); \
SET_OSPEED(A,2,HIGH_SPEED); \
SET_PULL(A,2,PULL_UP); \
SET_AFR(A,2,AF1_USART1); \
SET_MODER(A,3,INPUT_MODE); \
SET_PULL(A,3,PULL_UP); \
SET_AFR(A,3,AF1_USART1); \
} while(0)
#define DEINIT_BT_PINS() do { \
} while (0)
// 14: PA4/ADC_IN4 (3,3V)
// 15: PA5/ADC_IN5 (5V)
// 16: PA6/ADC_IN6 (BAT)
// 17: PA7 (EN_BAT_ADC)
// 18: PB0 (BT_KEY)
// 19: PB1 (DCDC_ENABLE)
// возможные варианты:
// выход высокий - разрешение ON
// выход низкий - запрет OFF
#define INIT_DCDC_OFF() do { \
SET_MODER(B,1,OUTPUT_MODE); \
SET_OTYPE(B,1,PUSH_PULL_OTYPE); \
SET_OSPEED(B,1,LOW_SPEED); \
SET_PULL(B,1,PULL_NOT); \
RESET_OD(B,1); \
} while(0)
#define ON_DCDC() SET_OD(B,1)
#define OFF_DCDC() RESET_OD(B,1)
// 20: PB2 (CHARGE_ENABLE)
// 21: PB10 /USART3_TX (opto)
// 22: PB11 /USART3_RX (opto)
#define INIT_OPTO_PINS() do { \
SET_MODER(B,10,ALTER_MODE); \
SET_OTYPE(B,10,PUSH_PULL_OTYPE); \
SET_OSPEED(B,10,HIGH_SPEED); \
SET_PULL(B,10,PULL_UP); \
SET_AFR(B,10,AF1_USART1); \
SET_MODER(B,11,INPUT_MODE); \
SET_PULL(B,11,PULL_UP); \
SET_AFR(B,11,AF1_USART1); \
} while(0)
#define DEINIT_OPTO_PINS() do { \
} while (0)
// 23: VSS
// 24: VDD
// 25: PB12 ( LED_BT_G )
#define INIT_LEDBT_G_OFF() do { \
SET_MODER(B,12,OUTPUT_MODE); \
SET_OTYPE(B,12,PUSH_PULL_OTYPE); \
SET_OSPEED(B,12,LOW_SPEED); \
SET_PULL(B,12,PULL_NOT); \
RESET_OD(B,12); \
} while(0)
#define ON_LEDBT_G() SET_OD(B,12)
#define OFF_LEDBT_G() RESET_OD(B,12)
// 26: PB13 ( LED_BT_R )
#define INIT_LEDBT_R_OFF() do { \
SET_MODER(B,13,OUTPUT_MODE); \
SET_OTYPE(B,13,PUSH_PULL_OTYPE); \
SET_OSPEED(B,13,LOW_SPEED); \
SET_PULL(B,13,PULL_NOT); \
RESET_OD(B,13); \
} while(0)
#define ON_LEDBT_R() SET_OD(B,13)
#define OFF_LEDBT_R() RESET_OD(B,13)
// 27: PB14 ( LED_RAD_G )
#define INIT_LEDRAD_G_OFF() do { \
SET_MODER(B,14,OUTPUT_MODE); \
SET_OTYPE(B,14,PUSH_PULL_OTYPE); \
SET_OSPEED(B,14,LOW_SPEED); \
SET_PULL(B,14,PULL_NOT); \
RESET_OD(B,14); \
} while(0)
#define ON_LEDRAD_G() SET_OD(B,14)
#define OFF_LEDRAD_G() RESET_OD(B,14)
// 28: PB15 ( LED_RAD_R )
#define INIT_LEDRAD_R_OFF() do { \
SET_MODER(B,15,OUTPUT_MODE); \
SET_OTYPE(B,15,PUSH_PULL_OTYPE); \
SET_OSPEED(B,15,LOW_SPEED); \
SET_PULL(B,15,PULL_NOT); \
RESET_OD(B,15); \
} while(0)
#define ON_LEDRAD_R() SET_OD(B,15)
#define OFF_LEDRAD_R() RESET_OD(B,15)
// 29: ---
// 30: PA9 / USART1_TX (radio)
// 31: PA10 / USART1_ RX (radio)
#define INIT_RAD_PINS() do { \
SET_MODER(A,9,ALTER_MODE); \
SET_OTYPE(A,9,PUSH_PULL_OTYPE); \
SET_OSPEED(A,9,HIGH_SPEED); \
SET_PULL(A,9,PULL_UP); \
SET_AFR(A,9,AF1_USART1); \
SET_MODER(A,10,INPUT_MODE); \
SET_PULL(A,10,PULL_UP); \
SET_AFR(A,10,AF1_USART1); \
} while(0)
#define DEINIT_RAD_PINS() do { \
} while (0)
// 32: ---
// 33: PA12 ( BT_ RESET)
// 34: SW_DIO
// 35: VSS
// 36: VDD
// 37: SW_CLK
// 38: ----
// 39: PB3 (LED_OPTO_G)
#define INIT_LEDOPTO_G_OFF() do { \
SET_MODER(B,3,OUTPUT_MODE); \
SET_OTYPE(B,3,PUSH_PULL_OTYPE); \
SET_OSPEED(B,3,LOW_SPEED); \
SET_PULL(B,3,PULL_NOT); \
RESET_OD(B,3); \
} while(0)
#define ON_LEDOPTO_G() SET_OD(B,3)
#define OFF_LEDOPTO_G() RESET_OD(B,3)
// 40: PB4 ( LED_OPTO_R)
#define INIT_LEDOPTO_R_OFF() do { \
SET_MODER(B,4,OUTPUT_MODE); \
SET_OTYPE(B,4,PUSH_PULL_OTYPE); \
SET_OSPEED(B,4,LOW_SPEED); \
SET_PULL(B,4,PULL_NOT); \
RESET_OD(B,4); \
} while(0)
#define ON_LEDOPTO_R() SET_OD(B,4)
#define OFF_LEDOPTO_R() RESET_OD(B,4)
// 41: PB5 (RUN)
#define INIT_LEDRUN_ON() do { \
SET_MODER(B,5,OUTPUT_MODE); \
SET_OTYPE(B,5,PUSH_PULL_OTYPE); \
SET_OSPEED(B,5,LOW_SPEED); \
SET_PULL(B,5,PULL_NOT); \
RESET_OD(B,5); \
} while(0)
#define OFF_LEDRUN() SET_OD(B,5)
#define ON_LEDRUN() RESET_OD(B,5)
#define TOGGLE_LEDRUN() TOGGLE_OD(B,5)
// 42: PB6 (SCL_MEM)
// 43: PB7 (SDA_MEM)
#define INIT_GPIO_I2C() do { \
SET_OTYPE(B,6,OPEN_DRAIN_OTYPE); \
SET_OTYPE(B,7,OPEN_DRAIN_OTYPE); \
SET_OD(B,6); \
SET_OD(B,7); \
SET_OSPEED(B,6,HIGH_SPEED); \
SET_OSPEED(B,7,HIGH_SPEED); \
SET_PULL(B,6,PULL_NOT); \
SET_PULL(B,7,PULL_NOT); \
SET_MODER(B,6,OUTPUT_MODE); \
SET_MODER(B,7,OUTPUT_MODE); \
} while (0)
#define LI2C_SCL_0() RESET_OD(B,6)
#define LI2C_SCL_1() SET_OD(B,6)
#define LI2C_SDA_0() RESET_OD(B,7)
#define LI2C_SDA_1() SET_OD(B,7)
#define LI2C_SDA_RD() GET_ID(B,7)
#define LI2C_SCL_RD() GET_ID(B,6)
// 44: BOOT0
// 45: PB8 (LED_USB_G)
#define INIT_LEDUSB_G_OFF() do { \
SET_MODER(B,8,OUTPUT_MODE); \
SET_OTYPE(B,8,PUSH_PULL_OTYPE); \
SET_OSPEED(B,8,LOW_SPEED); \
SET_PULL(B,8,PULL_NOT); \
RESET_OD(B,8); \
} while(0)
#define ON_LEDUSB_G() SET_OD(B,8)
#define OFF_LEDUSB_G() RESET_OD(B,8)
// 46: PB9 (LED_USB_R
#define INIT_LEDUSB_R_OFF() do { \
SET_MODER(B,9,OUTPUT_MODE); \
SET_OTYPE(B,9,PUSH_PULL_OTYPE); \
SET_OSPEED(B,9,LOW_SPEED); \
SET_PULL(B,9,PULL_NOT); \
RESET_OD(B,9); \
} while(0)
#define ON_LEDUSB_R() SET_OD(B,9)
#define OFF_LEDUSB_R() RESET_OD(B,9)
// 47: VSS
// 48: VDD
#if 0
// 42: PB6 UART1_TX 43: PB7 UART1_RX
#define INIT_OPTO_PINS() do { \
SET_MODER(B,6,ALTER_MODE); \
SET_OTYPE(B,6,PUSH_PULL_OTYPE); \
SET_OSPEED(B,6,HIGH_SPEED); \
SET_PULL(B,6,PULL_UP); \
SET_AFR(B,6,AF1_USART1); \
SET_MODER(B,7,INPUT_MODE); \
SET_PULL(B,7,PULL_UP); \
SET_AFR(B,7,AF1_USART1); \
} while(0)
#define DEINIT_OPTO_PINS() do { \
} while (0)
//------------------------------------------------------------------
// BL PB13
// возможные варианты:
// выход высокий - включен подсвет ON
// выход низкий - выключен подсвет OFF
#define INIT_BL_OFF() do { \
SET_MODER(B,13,OUTPUT_MODE); \
SET_OTYPE(B,13,PUSH_PULL_OTYPE); \
SET_OSPEED(B,13,LOW_SPEED); \
SET_PULL(B,13,PULL_NOT); \
RESET_OD(B,13); \
} while(0)
#define ON_BL() SET_OD(B,13)
#define OFF_BL() RESET_OD(B,13)
//------------------------------------------------------------------
// 42: PA9, SCL 43: PA10, SDA
#define INIT_GPIO_I2C() do { \
SET_OTYPE(A,9,OPEN_DRAIN_OTYPE); \
SET_OTYPE(A,10,OPEN_DRAIN_OTYPE); \
SET_OD(A,9); \
SET_OD(A,10); \
SET_OSPEED(A,9,HIGH_SPEED); \
SET_OSPEED(A,10,HIGH_SPEED); \
SET_PULL(A,9,PULL_NOT); \
SET_PULL(A,10,PULL_NOT); \
SET_MODER(A,9,OUTPUT_MODE); \
SET_MODER(A,10,OUTPUT_MODE); \
} while (0)
#define LI2C_SCL_0() RESET_OD(A,9)
#define LI2C_SCL_1() SET_OD(A,9)
#define LI2C_SDA_0() RESET_OD(A,10)
#define LI2C_SDA_1() SET_OD(A,10)
#define LI2C_SDA_RD() GET_ID(A,10)
#define LI2C_SCL_RD() GET_ID(A,9)
//------------------------------------------------------------------------------
// оптопорт
#define UART(x) UART1_##x
#define UART_ENABLE_CLOCK() do {} while(0)
/* SIM_SCGC4|=SIM_SCGC4_UART1_MASK
*/
#define UART_DISABLE_CLOCK() do {} while(0)
/* SIM_SCGC4 &= ~SIM_SCGC4_UART1_MASK;
*/
//---------------------------------------------------
// ADC
/*
do { if (SIM_SCGC6 & SIM_SCGC6_ADC0_MASK) \
ADC0_SC1A = ADC_SC1_ADCH_MASK; \
SIM_SCGC6 &= ~SIM_SCGC6_ADC0_MASK; \
} while(0)
*/
#endif
/* Enable the peripheral clock of GPIOA and GPIOC */
#define ENABLE_CLOCK_ALL_PORTS() RCC->AHBENR |= \
RCC_AHBENR_GPIOAEN | \
RCC_AHBENR_GPIOBEN | \
RCC_AHBENR_GPIOCEN | \
RCC_AHBENR_GPIOFEN;
#define ADC_STOP() do {} while(0)
#define UART_ENABLE_CLOCK() do {} while(0)
#endif
-
- Понял, что меня смущает. Много текста, который тяжело охватить сразу. Dingo(549 знак., 10.02.2017 11:09)
- Неплохо! Но объём больше. С другой стороны - у вас не только GPIO здесь. - Dingo(10.02.2017 10:43)