Beispiel 1: Konstrukte für STM32F1xx ==================================== /* Hilfsbezeichnungen zum Port-Konfigurieren */ /* Beispiel: #define wert_fuer_pa0 (mode?+conf?) */ #define input 0 /* Pin ist Input */ #define out10 1 /* Pin ist Output, max. 10 MHz schnell */ #define out2 2 /* Pin ist Output, max. 2 MHz schnell */ #define out50 3 /* Pin ist Output, max. 50 MHz schnell */ #define conf0 0 /* in:analog, out:gpio */ #define conf1 4 /* in:hi-z, out:open drain */ #define conf2 8 /* in:pullup/dn, out:alternate function */ #define conf3 12 /* in:verboten, out:alternate fkt mit open drain */ /* aufgedröselte Port-Konfiguationen: */ /* CNF=0 */ #define in_analog 0 #define out10_gpo 1 #define out2_gpo 2 #define out50_gpo 3 /* CNF=1 */ #define in_gpi 4 #define out10_od 5 #define out2_od 6 #define out50_od 7 /* CNF=2 */ #define in_pupd 8 #define out10_altfu 9 #define out2_altfu 10 #define out50_altfu 11 /* CNF=3 */ #define verboten 12 #define out10_altfu_od 13 #define out2_altfu_od 14 #define out50_altfu_od 15 /* Port A: */ #define wf_PA0 (in_gpi) /* in wakeup von Seiko-Epson-RTC oder extern */ #define wf_PA1 (in_pupd) /* ? an Testpunkt */ #define wf_PA2 (out2_altfu) /* TXD2 normaler Kommunikationsport */ #define wf_PA3 (in_gpi) /* RXD2 9600 Bd 8N1 an DSUB9, */ #define wf_PA4 (out2_altfu) /* ? (DAC) */ #define wf_PA5 (out2_altfu) /* ? (DAC) */ #define wf_PA6 (in_pupd) /* ? */ #define wf_PA7 (in_pupd) /* ? an Messerleiste */ #define wf_PA8 (out50_altfu) /* out MasterClock Out */ #define wf_PA9 (out2_altfu) /* TXD1 */ #define wf_PA10 (in_gpi) /* RXD1 an Programmierport */ #define wf_PA11 (out50_altfu) /* USB D- */ #define wf_PA12 (out50_altfu) /* USB D+ */ #define wf_PA13 (out50_altfu) /* JTAG TMS */ #define wf_PA14 (out50_altfu) /* JTAG TCK */ #define wf_PA15 (out50_altfu) /* JTAG TDI */ /* Port B: */ #define wf_PB0 (in_pupd) /* ? an Testpunkt */ #define wf_PB1 (in_pupd) /* ? an Testpunkt */ #define wf_PB2 (out50) /* out Enable für USB-rstout ?? */ #define wf_PB3 (out50_altfu) /* JTAG TDO */ #define wf_PB4 (out50_altfu) /* JTAG RST */ #define wf_PB5 (out50_altfu) /* interrupt CLK vom PS/2 Mini-Din */ #define wf_PB6 (out2_altfu_od) /* I2C1 SCL (RTC8564)*/ #define wf_PB7 (out2_altfu_od) /* I2C1 SDA (RTC8564)*/ #define wf_PB8 (in_gpi) /* in DATA vom PS/2 Mini-Din */ #define wf_PB9 (in_pupd) /* ? an Testpunkt */ #define wf_PB10 (out2_altfu) /* TXD3 */ #define wf_PB11 (out2_altfu) /* RXD3 an Messerleiste TTL-Pegel */ #define wf_PB12 (input+conf2) /* SPI2=I2S Audio: L/R WCLK */ #define wf_PB13 (input+conf2) /* SPI2=I2S SCK */ #define wf_PB14 (input+conf2) /* SPI2=I2S MISO */ #define wf_PB15 (input+conf2) /* SPI2=I2S MOSI */ und so weiter mit den anderen Ports /* Pin-Funktionen aufsetzen */ GPIOA->CRL = wf_GPIOA_CRL; GPIOA->CRH = wf_GPIOA_CRH; GPIOB->CRL = wf_GPIOB_CRL; GPIOB->CRH = wf_GPIOB_CRH; GPIOC->CRL = wf_GPIOC_CRL; GPIOC->CRH = wf_GPIOC_CRH; GPIOD->CRL = wf_GPIOD_CRL; GPIOD->CRH = wf_GPIOD_CRH; GPIOE->CRL = wf_GPIOE_CRL; GPIOE->CRH = wf_GPIOE_CRH; GPIOF->CRL = wf_GPIOF_CRL; GPIOF->CRH = wf_GPIOF_CRH; GPIOG->CRL = wf_GPIOG_CRL; GPIOG->CRH = wf_GPIOG_CRH; /* Pin-Remap */ AFIO->MAPR = wf_AFIO_MAPR; Beispiel 2: Konstrukte für STM32F3xx ==================================== /****** symbolische Port-Pin Eigenschaften ******/ /* ein array of word pro Port */ /* bits: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 */ /* np - - - - |pu,pd| |speed| od |mode| |--Alt-Fu--| */ // Alternativfunktionen, kommt in GPIOx_AFRL/AFRH #define AF0 0 #define AF1 1 #define AF2 2 #define AF3 3 #define AF4 4 #define AF5 5 #define AF6 6 #define AF7 7 #define AF8 8 #define AF9 9 #define AF10 10 #define AF11 11 #define AF12 12 #define AF13 13 #define AF14 14 #define AF15 15 // mode, kommt in GPIOx_MODER #define IN (0<<4) // GPIO IN #define OUT (1<<4) // GPIO OUT #define ALT (2<<4) // Alternative Fkt. #define ANA (3<<4) // analog // Open Drain, kommt in GPIOx_OTYPER #define OD (1<<6) // open drain // Portgeschwindigkeit, kommt in GPIOx_OSPEED #define LoSpeed (0<<7) #define MedSpeed (1<<7) #define HiSpeed (3<<7) // Pullup, Pulldown oder nix, kommt in GPIOx_PUPDR (nur Ports A und B) #define PullUP (1<<9) #define PullDN (2<<9) // vorhanden, unbelegt, oder nicht #define nopin (1<<15) #define unconnected (1<<15) const word wfPortA[16] = { /* P.0 */ OUT, // Dir-Motor /* P.1 */ ALT + MedSpeed + AF1, // TIM2_CH2 -> Takt A /* P.2 */ unconnected, /* P.3 */ ALT + MedSpeed + AF9, // TIM15_CH2 -> Takt B /* P.4 */ OUT, // Relais 2 /* P.5 */ OUT, // Relais 5 /* P.6 */ ALT + MedSpeed + AF2, // TIM3_CH1 -> Takt-Chopper /* P.7 */ OUT, // Strombegrenzung E/A /* P.8 */ ALT + AF0 + HiSpeed, // Systemtakt zum Test, unconnected /* P.9 */ ALT + MedSpeed + AF7, // USART1_TX /* P.10 */ ALT + MedSpeed + AF7, // USART1_RX /* P.11 */ ALT + HiSpeed + AF14, // USB_DM /* P.12 */ ALT + HiSpeed + AF14, // USB_DP /* P.13 */ ALT + HiSpeed + AF0, // SWDIO /* P.14 */ ALT + HiSpeed + AF0, // SWCLK /* P.15 */ OUT, // USB_ON }; const word wfPortB[16] = { /* P.0 */ unconnected, /* P.1 */ unconnected, /* P.2 */ unconnected, /* P.3 */ ALT + MedSpeed + AF7, // USART2_TX /* P.4 */ ALT + MedSpeed + PullUP + AF7, // USART2_RX /* P.5 */ unconnected, /* P.6 */ IN, // von SENSOR 2 /* P.7 */ ALT + MedSpeed + AF2, // TIM4_CH2 -> Sensepuls-Takt /* P.8 */ ALT + AF4, // I2C1_SCL /* P.9 */ ALT + AF4, // I2C1_SDA /* P.10 */ unconnected, /* P.11 */ unconnected, /* P.12 */ unconnected, /* P.13 */ unconnected, /* P.14 */ unconnected, /* P.15 */ unconnected, }; und so weiter für die restlichen Ports struct pinmode { dword moder; dword otyper; dword ospeedr; dword pupdr; qword afrhl; }; // portweise Pinmode Setup void PimoSetup (struct pinmode* Pim, const word tafl[] ) { struct pinmode iPim; int i; word w; iPim.moder = 0; iPim.otyper = 0; iPim.ospeedr = 0; iPim.pupdr = 0; iPim.afrhl = 0; i = 15; while (i >= 0) { w = tafl[i]; iPim.moder = iPim.moder << 2; iPim.otyper = iPim.otyper << 1; iPim.ospeedr = iPim.ospeedr << 2; iPim.pupdr = iPim.pupdr << 2; iPim.afrhl = iPim.afrhl << 4; if ((w & (1<<15)) == 0) { iPim.moder |= ((w>>4) & 3); iPim.otyper |= ((w>>6) & 1); iPim.ospeedr |= ((w>>7) & 3); iPim.pupdr |= ((w>>9) & 3); iPim.afrhl |= w & 15; } --i; } *Pim = iPim; } void sysconfig (void) { struct pinmode PMD; PimoSetup(&PMD, wfPortA); GPIOA_MODER = PMD.moder; GPIOA_OTYPER = PMD.otyper; GPIOA_OSPEEDR = PMD.ospeedr; GPIOA_PUPDR = PMD.pupdr; GPIOA_AFRL = PMD.afrhl; GPIOA_AFRH = PMD.afrhl >> 32; PimoSetup(&PMD, wfPortB); GPIOB_MODER = PMD.moder; GPIOB_OTYPER = PMD.otyper; GPIOB_OSPEEDR = PMD.ospeedr; GPIOB_PUPDR = PMD.pupdr; GPIOB_AFRL = PMD.afrhl; GPIOB_AFRH = PMD.afrhl >> 32; und so weiter...