static void init(void) { /* set modes before to enable the port */ gpio_dir(PROG_BUTTON, GPIO_INPUT); pin_mode(PROG_BUTTON, PIN_MODE_PULLUP); gpio_dir(TARGET_RESET, GPIO_INPUT); gpio_dir(TARGET_LED, GPIO_INPUT); /* set digital debounce/filter */ pin_physport_from_pin(PROG_BUTTON)->dfcr.cs = PORT_CS_LPO; pin_physport_from_pin(PROG_BUTTON)->dfwr.filt = 31; /* button interrupt */ pin_physport_from_pin(PROG_BUTTON)->dfer |= 1 << pin_physpin_from_pin(PROG_BUTTON); pin_physport_from_pin(PROG_BUTTON)->pcr[pin_physpin_from_pin(PROG_BUTTON)].irqc = PCR_IRQC_INT_FALLING; /* reset interrupt */ pin_physport_from_pin(TARGET_RESET)->pcr[pin_physpin_from_pin(TARGET_RESET)].irqc = PCR_IRQC_INT_RISING; /* LED interrupt */ pin_physport_from_pin(TARGET_LED)->pcr[pin_physpin_from_pin(TARGET_LED)].irqc = PCR_IRQC_INT_FALLING; int_enable(IRQ_PORTD); gpio_dir(LED_SUCCESS, GPIO_OUTPUT); gpio_dir(LED_FAIL, GPIO_OUTPUT); timeout_init(); }
void pin_change_handler(volatile struct PORT_t *port, const struct pin_change_handler *handlers, const struct pin_change_handler *end) { const struct pin_change_handler *i; for (i = handlers; i < end; i++) { uint32_t bit = 1 << pin_physpin_from_pin(i->pin_id); if (port->isfr & bit) { port->isfr = bit; i->cb(i->cbdata); } } }
void config_pins() { // REMOTE_EN pin pin_mode(PIN_PTD3, PIN_MODE_MUX_GPIO); gpio_dir(PIN_PTD3, GPIO_OUTPUT); // UART pin_mode(PIN_PTA1, PIN_MODE_MUX_ALT2); pin_mode(PIN_PTA2, PIN_MODE_MUX_ALT2); // for I2C devices i2c_init(I2C_RATE_100); PORTB.pcr[pin_physpin_from_pin(PIN_PTB0)].raw = ((struct PCR_t) {.mux=2,.ode=1}).raw;
void gpio_dir(enum gpio_pin_id pin, enum gpio_dir dir) { int pinnum = pin_physpin_from_pin(pin); volatile struct GPIO_t *pinp = gpio_physgpio_from_pin(pin); switch (dir) { case GPIO_OUTPUT: pinp->pddr |= 1 << pinnum; goto set_mux; case GPIO_INPUT: pinp->pddr &= ~(1 << pinnum); set_mux: pin_mode(pin, PIN_MODE_MUX_ALT1); break; case GPIO_DISABLE: pin_mode(pin, PIN_MODE_MUX_ANALOG); break; } }
void PORTD_Handler(void) { if (pin_physport_from_pin(PROG_BUTTON)->pcr[pin_physpin_from_pin(PROG_BUTTON)].isf) { pin_physport_from_pin(PROG_BUTTON)->pcr[pin_physpin_from_pin(PROG_BUTTON)].raw |= 0; /* clear isf */ statemachine(ev_button); } if (pin_physport_from_pin(TARGET_RESET)->pcr[pin_physpin_from_pin(TARGET_RESET)].isf) { pin_physport_from_pin(TARGET_RESET)->pcr[pin_physpin_from_pin(TARGET_RESET)].raw |= 0; /* clear isf */ statemachine(ev_reset); } if (pin_physport_from_pin(TARGET_LED)->pcr[pin_physpin_from_pin(TARGET_LED)].isf) { pin_physport_from_pin(TARGET_LED)->pcr[pin_physpin_from_pin(TARGET_LED)].raw |= 0; /* clear isf */ statemachine(ev_led); } }
enum gpio_pin_value gpio_read(enum gpio_pin_id pin) { return (BITBAND_BIT(gpio_physgpio_from_pin(pin)->pdir, pin_physpin_from_pin(pin))); }
void gpio_toggle(enum gpio_pin_id pin) { gpio_physgpio_from_pin(pin)->ptor = 1 << pin_physpin_from_pin(pin); }
void gpio_write(enum gpio_pin_id pin, enum gpio_pin_value val) { BITBAND_BIT(gpio_physgpio_from_pin(pin)->pdor, pin_physpin_from_pin(pin)) = val; }
void pin_mode(enum pin_id pin, enum pin_mode mode) { int pinnum = pin_physpin_from_pin(pin); /* enable port clock */ SIM.scgc5.raw |= 1 << (pin_portnum_from_pin(pin) + 8); struct PCR_t pcr = pin_physport_from_pin(pin)->pcr[pinnum]; if (mode & PIN_MODE_RESET) { pcr.raw &= ~0xff; pcr.mux = 0; } if (mode & PIN_MODE__SLEW) { switch (mode & PIN_MODE__SLEW_MASK) { case PIN_MODE_SLEW_FAST: pcr.sre = 0; break; case PIN_MODE_SLEW_SLOW: pcr.sre = 1; break; } } if (mode & PIN_MODE__PULL) { switch (mode & PIN_MODE__PULL_MASK) { case PIN_MODE_PULL_OFF: pcr.pe = 0; break; case PIN_MODE_PULLDOWN: pcr.pe = 1; pcr.ps = PCR_PULLDOWN; break; case PIN_MODE_PULLUP: pcr.pe = 1; pcr.ps = PCR_PULLUP; break; } } if (mode & PIN_MODE__DRIVE) { switch (mode & PIN_MODE__DRIVE_MASK) { case PIN_MODE_DRIVE_LOW: pcr.dse = 0; break; case PIN_MODE_DRIVE_HIGH: pcr.dse = 1; break; } } if (mode & PIN_MODE__FILTER) { switch (mode & PIN_MODE__FILTER_MASK) { case PIN_MODE_FILTER_OFF: pcr.pfe = 0; break; case PIN_MODE_FILTER_ON: pcr.pfe = 1; break; } } if (mode & PIN_MODE__OPEN_DRAIN) { switch (mode & PIN_MODE__OPEN_DRAIN_MASK) { case PIN_MODE_OPEN_DRAIN_OFF: pcr.ode = 0; break; case PIN_MODE_OPEN_DRAIN_ON: pcr.ode = 1; break; } } if (mode & PIN_MODE__MUX) { pcr.mux = (mode & PIN_MODE__MUX_MASK) >> 13; /* XXX */ }