Inline void set_cr_cnf(uint32_t reg, uint_t p, int_t v) { if (p < 8) { sil_andw((void*)GPIO_CRL(reg), ~CR_CNF_MASK(p)); sil_orw((void*)GPIO_CRL(reg), CR_CNF(p, v)); } else if (8 <= p && p < 16) { sil_andw((void*)GPIO_CRH(reg), ~CR_CNF_MASK(p - 8)); sil_orw((void*)GPIO_CRH(reg), CR_CNF(p - 8, v)); } }
static void setupBUTTON (void) { // todo, swap out hardcoded pin/bank with macro u32 rwmVal; /* read-write-modify place holder var */ /* Setup APB2 (GPIOC) */ rwmVal = GET_REG(RCC_APB2ENR); rwmVal |= 0x00000010; SET_REG(RCC_APB2ENR,rwmVal); /* Setup GPIOC Pin 9 as PP Out */ rwmVal = GET_REG(GPIO_CRH(GPIOC)); rwmVal &= 0xFFFFFF0F; rwmVal |= 0x00000040; SET_REG(GPIO_CRH(GPIOC),rwmVal); }
static void setupUSB (void) { u32 rwmVal; /* read-write-modify place holder var */ /* Setup the USB DISC Pin */ rwmVal = GET_REG(RCC_APB2ENR); rwmVal |= 0x00000010; SET_REG(RCC_APB2ENR,rwmVal); // todo, macroize usb_disc pin /* Setup GPIOC Pin 12 as OD out */ rwmVal = GET_REG(GPIO_CRH(GPIOC)); rwmVal &= 0xFFF0FFFF; rwmVal |= 0x00050000; setPin (GPIOC,12); SET_REG(GPIO_CRH(GPIOC),rwmVal); pRCC->APB1ENR |= 0x00800000; /* initialize the usb application */ resetPin (GPIOC,12); /* present ourselves to the host */ }
void gpio_set_mode(uint32_t gpioport, uint8_t mode, uint8_t cnf, uint16_t gpios) { uint16_t i, offset = 0; uint32_t crl = 0, crh = 0, tmp32 = 0; /* * We want to set the config only for the pins mentioned in gpios, * but keeping the others, so read out the actual config first. */ crl = GPIO_CRL(gpioport); crh = GPIO_CRH(gpioport); /* Iterate over all bits, use i as the bitnumber. */ for (i = 0; i < 16; i++) { /* Only set the config if the bit is set in gpios. */ if (!((1 << i) & gpios)) { continue; } /* Calculate bit offset. */ offset = (i < 8) ? (i * 4) : ((i - 8) * 4); /* Use tmp32 to either modify crl or crh. */ tmp32 = (i < 8) ? crl : crh; /* Modify bits are needed. */ tmp32 &= ~(0xf << offset); /* Clear the bits first. */ tmp32 |= (mode << offset) | (cnf << (offset + 2)); /* Write tmp32 into crl or crh, leave the other unchanged. */ crl = (i < 8) ? tmp32 : crl; crh = (i >= 8) ? tmp32 : crh; } GPIO_CRL(gpioport) = crl; GPIO_CRH(gpioport) = crh; }