/* * Minimal example for Wi-Fi connectivity using SPB800. This example compiles * for the AVR XMEGA atxmega128a1 * * Put this file and the directory wlp_api from the reference design sources * in a directory and compile with the following command line: * * avr-gcc -mmcu=atxmega128a1 -Os -DBYTE_ORDER=LITTLE_ENDIAN -Iwlp_api * wlp_api/wlp_api.c wlp_api/wlp_inet.c tutorial.c -o tutorial.elf * * avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature * tutorial.elf tutorial.hex * * Programming with avrdude and avr dragon: * * avrdude -p x128a1 -c dragon_jtag -P usb -U flash:w:tutorial.hex * */ #include /**************************************************************************** * Platform-dependent code starts here. The functions, defines and includes * in this section must be adapted to the specific platform used. * * **************************************************************************** */ #define F_CPU 2000000UL #include #include /* * Error printing. * * NOTE: This function must be modified for the specific platform used. * */ void board_err(const char *str, int err) { return; } /* * Initalize the UART port to the specified baudrate, 8N1 * * NOTE: This function must be modified for the specific platform used. * */ void board_uart_init(uint32_t baudrate) { USART_t *port = &USARTD0; uint16_t bsel = 0; uint8_t bscale = 0; port->CTRLB = 0; /* disable transceiver while confiuring */ PORTD_DIRSET = (1 << 3); /* PD3 is PICO_UART TX */ PORTD_DIRCLR = (1 << 2); /* PD2 is PICO_UART RX */ /* Pull UART TX high during initialization to avoid a potential * risk of transmitting garbage on the UART */ PORTD_OUTSET = (1 << 3); if (baudrate == 57600) { bsel = 19; bscale = 12; /* 0b1100 -> -4 in two-complemet repr */ } else { board_err("baudrate not supported", baudrate); return; } port->BAUDCTRLA = bsel & 0xff; port->BAUDCTRLB = (bsel >> 8) | (bscale << 4); port->CTRLC = USART_CHSIZE_8BIT_gc | USART_PMODE_DISABLED_gc; port->CTRLB = USART_TXEN_bm | USART_RXEN_bm; } /* * Read len bytes from the UART. This function block until len bytes * have been read. * * NOTE: This function must be modified for the specific platform used. * */ int board_uart_read_f(void *ctx, void *buf, int len) { int i; char *in = buf; USART_t *uart = &USARTD0; for (i = 0; i < len; i++) { /* loop until all bytes are read */ /* wait for a char */ while (!(uart->STATUS & USART_RXCIF_bm)); /* read the char into the provided buffer */ in[i] = uart->DATA; } return len; } /* * Write len bytes to the UART. This function should block until len bytes * have been written. * * NOTE: This function must be modified for the specific platform used. * */ int board_uart_write_f(void *ctx, const void *buf, int len) { int i; const char *out = buf; USART_t *uart = &USARTD0; for (i = 0; i < len; i++) { /* loop until all bytes are written */ /* wait until we are ready to transmit next char */ while (!(uart->STATUS & USART_DREIF_bm)); /* transmit the char */ uart->DATA = out[i]; } return len; } /* * Busy wait for the specified number of ms * * NOTE: This function must be modified for the specific platform used. * */ void board_delay(uint32_t ms) { _delay_ms(ms); } /**************************************************************************** * Platform independent code starts here. * * * **************************************************************************** */ #include #include #include #include #include #include /* Will be called when Wi-Fi link is up */ static void link_f(void *ctx, int link) { return; } /* Will called when an IP address is configured */ static void addr_f(void *ctx, const struct ip_addr *ip) { return; } /* Initialize the UART and the SPB800. Then connect to the access point with * SSID: "hdwireless". * */ int main(void) { struct wl_ssid_t ssid; int err; /* wait until device is booted */ _delay_ms(200); /* configure host UART */ board_uart_init(57600); /* initialize the oWL Pico API */ if ((err = wlp_init( 57600, /* baudrate */ 0, /* no hardware flow control */ board_uart_read_f, /* read buf from uart */ board_uart_write_f, /* write buf to uart */ NULL /* context passed to uart_*_f */)) < 0) { board_err("init failed", err); } /* wait until device is reconfigued */ _delay_ms(100); /* register a function that will be called when the link status * changes */ wlp_set_link_cb(link_f, NULL); /* register a function that will be called when the IP address is * assigned or changes */ wlp_set_ipaddr_cb(addr_f, NULL); /* specifiy the ssid ... */ strcpy(ssid.ssid, "hdwireless"); /* ... and the number of valid bytes in the ssid */ ssid.len = strlen("hdwireless"); /* start linkup process */ if ((err = wlp_linkup(&ssid, NULL, 0)) < 0) board_err("linkup failed", err); /* configure the device to use dhcp */ if ((err = wlp_set_dhcp(1)) < 0) board_err("set_dhcp failed", err); for (;;) wlp_poll(); /* make forward progress */ }