int main(void) { clock_setup(); spi2.begin(); configure_as_output({PB, 0}); set_pin({PB, 0}, true); etk::sleep_ms(300); //configure chip select pin auto cs = gpio_pin(PA, 8); configure_as_output(cs); set_pin(cs, true); //start LCD screen lcd.begin(); etk::sleep_ms(500); while(true) { lcd.set_cursor(0, 0); lcd.print("Temp: "); set_pin(cs, false); union spidata { char bytes[4]; int32 data; }; spidata data; data.bytes[3] = spi2.read(); data.bytes[2] = spi2.read(); data.bytes[1] = spi2.read(); data.bytes[0] = spi2.read(); set_pin(cs, true); lcd.set_cursor(2, 6); int32 v = data.data; if (v & 0x80000000) v = 0xFFFFC000 | ((v >> 18) & 0x00003FFFF); else v >>= 18; real_t deg_c = v; deg_c *= 0.25; lcd.print(deg_c, " C"); etk::sleep_ms(500); }
int board_init_io() { #ifndef SIMULATOR // analog inputs ANSELA = 0x0000; // all analog inputs of port A as digital buffer ANSELB = 0x0000; // all analog inputs of port B as digital buffer ANSELC = 0x0000; // all analog inputs of port C as digital buffer ANSELD = 0x0000; // all analog inputs of port D as digital buffer #endif board_leds[0] = gpio_pin(GPIO_PORTD, 3); gpio_setBitConfig(board_leds[0], GPIO_OUTPUT); board_leds[1] = gpio_pin(GPIO_PORTC, 13); gpio_setBitConfig(board_leds[1], GPIO_OUTPUT); board_buttons[0] = gpio_pin(GPIO_PORTB, 9); gpio_setBitConfig(board_buttons[0], GPIO_INPUT); board_buttons[1] = gpio_pin(GPIO_PORTC, 10); gpio_setBitConfig(board_buttons[1], GPIO_INPUT); board_buttons[2] = gpio_pin(GPIO_PORTC, 4); gpio_setBitConfig(board_buttons[2], GPIO_INPUT); return 0; }
int board_init_io() { #ifndef SIMULATOR // analog inputs ANSELA = 0x0000; // all analog inputs of port A as digital buffer ANSELB = 0x0000; // all analog inputs of port B as digital buffer ANSELC = 0x0000; // all analog inputs of port C as digital buffer ANSELD = 0x0000; // all analog inputs of port D as digital buffer ANSELE = 0x0000; // all analog inputs of port E as digital buffer ANSELG = 0x0000; // all analog inputs of port G as digital buffer // remappable pins // Unlock configuration pin unlockIoConfig(); U3RXR = 0b0101; // RX3 ==> RPC7 RPC6R = 0b00001; // TX3 ==> RPC6 U4RXR = 0b1101; // RX4 ==> RPD3 RPA12R = 0b00010; // TX4 ==> RPA12 C1RXR = 0b1000; // CAN1RX ==> RE14 RPE0R = 0b01100; // CAN1TX ==> RE0 C2RXR = 0b1100; // CAN2RX ==> RE1 RPE15R = 0b01100; // CAN2TX ==> RE15 C3RXR = 0b1010; // CAN3RX ==> RG6 RPC15R = 0b01100; // CAN3TX ==> RC15 C4RXR = 0b0110; // CAN4RX ==> RC2 RPB1R = 0b01100; // CAN4TX ==> RB1 lockIoConfig(); #endif board_leds[0] = gpio_pin(GPIO_PORTG, 12); gpio_setBitConfig(board_leds[0], GPIO_OUTPUT); board_leds[1] = gpio_pin(GPIO_PORTG, 13); gpio_setBitConfig(board_leds[1], GPIO_OUTPUT); board_leds[2] = gpio_pin(GPIO_PORTG, 14); gpio_setBitConfig(board_leds[2], GPIO_OUTPUT); board_buttons[0] = gpio_pin(GPIO_PORTG, 11); gpio_setBitConfig(board_buttons[0], GPIO_INPUT); board_buttons[1] = gpio_pin(GPIO_PORTF, 13); gpio_setBitConfig(board_buttons[1], GPIO_INPUT); board_buttons[2] = gpio_pin(GPIO_PORTF, 12); gpio_setBitConfig(board_buttons[2], GPIO_INPUT); return 0; }
void gpio_irqdisable(int irq) { uint32_t base; int pin; if ((unsigned)irq < NR_GPIO_IRQS) { /* Get the base address of the GPIO module associated with this IRQ */ base = gpio_baseaddress(irq); /* Get the pin number associated with this IRQ. We made life difficult * here by choosing a sparse representation of IRQs on GPIO pins. */ pin = gpio_pin(irq); DEBUGASSERT(pin >= 0); /* Disable the GPIO interrupt. */ putreg32((1 << pin), base + AVR32_GPIO_IERC_OFFSET); } }
static int lua_gpio_index(lua_State *L) { gpio_t *gpio; const char *field; if (!lua_isstring(L, 2)) return lua_gpio_error(L, GPIO_ERROR_ARG, 0, "Error: unknown method or property"); field = lua_tostring(L, 2); /* Look up method in metatable */ lua_getmetatable(L, 1); lua_getfield(L, -1, field); if (!lua_isnil(L, -1)) return 1; gpio = luaL_checkudata(L, 1, "periphery.GPIO"); if (strcmp(field, "fd") == 0) { lua_pushinteger(L, gpio_fd(gpio)); return 1; } else if (strcmp(field, "pin") == 0) { lua_pushunsigned(L, gpio_pin(gpio)); return 1; } else if (strcmp(field, "supports_interrupts") == 0) { bool supported; int ret; if ((ret = gpio_supports_interrupts(gpio, &supported)) < 0) return lua_gpio_error(L, ret, gpio_errno(gpio), "Error: %s", gpio_errmsg(gpio)); lua_pushboolean(L, supported); return 1; } else if (strcmp(field, "direction") == 0) { gpio_direction_t direction; int ret; if ((ret = gpio_get_direction(gpio, &direction)) < 0) return lua_gpio_error(L, ret, gpio_errno(gpio), "Error: %s", gpio_errmsg(gpio)); switch (direction) { case GPIO_DIR_IN: lua_pushstring(L, "in"); break; case GPIO_DIR_OUT: lua_pushstring(L, "out"); break; default: lua_pushstring(L, "unknown"); break; } return 1; } else if (strcmp(field, "edge") == 0) { gpio_edge_t edge; int ret; if ((ret = gpio_get_edge(gpio, &edge)) < 0) return lua_gpio_error(L, ret, gpio_errno(gpio), "Error: %s", gpio_errmsg(gpio)); switch (edge) { case GPIO_EDGE_NONE: lua_pushstring(L, "none"); break; case GPIO_EDGE_RISING: lua_pushstring(L, "rising"); break; case GPIO_EDGE_FALLING: lua_pushstring(L, "falling"); break; case GPIO_EDGE_BOTH: lua_pushstring(L, "both"); break; default: lua_pushstring(L, "unknown"); break; } return 1; } return lua_gpio_error(L, GPIO_ERROR_ARG, 0, "Error: unknown property"); }