int xbee_init(netdev_t *dev) { uint8_t tmp[2]; xbee_t *xbee = (xbee_t *)dev; /* set default options */ xbee->addr_flags = XBEE_ADDR_FLAGS_LONG; xbee->options = 0; /* initialize buffers and locks*/ mutex_init(&(xbee->tx_lock)); mutex_init(&(xbee->resp_lock)); xbee->resp_limit = 1; /* needs to be greater then 0 initially */ xbee->rx_count = 0; /* initialize UART and GPIO pins */ if (uart_init(xbee->p.uart, xbee->p.br, _rx_cb, xbee) != UART_OK) { DEBUG("[xbee] init: error initializing UART\n"); return -ENXIO; } /* if reset pin is connected, do a hardware reset */ if (xbee->p.pin_reset != GPIO_UNDEF) { gpio_clear(xbee->p.pin_reset); xtimer_usleep(RESET_DELAY); gpio_set(xbee->p.pin_reset); } /* put the XBee device into command mode */ xtimer_usleep(ENTER_CMD_MODE_DELAY); _at_cmd(xbee, "+++"); xtimer_usleep(ENTER_CMD_MODE_DELAY); /* disable non IEEE802.15.4 extensions */ _at_cmd(xbee, "ATMM2\r"); /* put XBee module in "API mode without escaped characters" */ _at_cmd(xbee, "ATAP1\r"); /* apply AT commands */ _at_cmd(xbee, "ATAC\r"); /* exit command mode */ _at_cmd(xbee, "ATCN\r"); /* load long address (we can not set it, its read only for Xbee devices) */ if (_get_addr_long(xbee, xbee->addr_long.uint8, IEEE802154_LONG_ADDRESS_LEN) < 0) { DEBUG("[xbee] init: error getting address\n"); return -EIO; } if (_set_addr(xbee, &((xbee->addr_long).uint8[6]), IEEE802154_SHORT_ADDRESS_LEN) < 0) { DEBUG("[xbee] init: error setting short address\n"); return -EIO; } /* set default channel */ tmp[1] = 0; tmp[0] = XBEE_DEFAULT_CHANNEL; if (_set_channel(xbee, tmp, 2) < 0) { DEBUG("[xbee] init: error setting channel\n"); return -EIO; } /* set default PAN ID */ tmp[1] = (uint8_t)(XBEE_DEFAULT_PANID >> 8); tmp[0] = (uint8_t)(XBEE_DEFAULT_PANID & 0xff); if (_set_panid(xbee, tmp, 2) < 0) { DEBUG("[xbee] init: error setting PAN ID\n"); return -EIO; } DEBUG("[xbee] init: Initialization successful\n"); return 0; }
/* * Driver's "public" functions */ int xbee_init(xbee_t *dev, uart_t uart, uint32_t baudrate, gpio_t reset_pin, gpio_t sleep_pin) { uint8_t tmp[2]; /* check device and bus parameters */ if (dev == NULL) { return -ENODEV; } if (uart >= UART_NUMOF) { return -ENXIO; } /* set device driver */ dev->driver = &xbee_driver; /* set peripherals to use */ dev->uart = uart; dev->reset_pin = reset_pin; dev->sleep_pin = sleep_pin; /* set default options */ dev->addr_flags = 0; dev->proto = XBEE_DEFAULT_PROTOCOL; dev->options = 0; /* initialize buffers and locks*/ mutex_init(&(dev->tx_lock)); mutex_init(&(dev->resp_lock)); dev->resp_limit = 1; /* needs to be greater then 0 initially */ dev->rx_count = 0; /* initialize UART and GPIO pins */ if (uart_init(uart, baudrate, _rx_cb, dev) < 0) { DEBUG("xbee: Error initializing UART\n"); return -ENXIO; } if (reset_pin != GPIO_UNDEF) { if (gpio_init(reset_pin, GPIO_DIR_OUT, GPIO_NOPULL) < 0) { DEBUG("xbee: Error initializing RESET pin\n"); return -ENXIO; } gpio_set(reset_pin); } if (sleep_pin != GPIO_UNDEF) { if (gpio_init(sleep_pin, GPIO_DIR_OUT, GPIO_NOPULL) < 0) { DEBUG("xbee: Error initializing SLEEP pin\n"); return -ENXIO; } gpio_clear(sleep_pin); } /* if reset pin is connected, do a hardware reset */ if (reset_pin != GPIO_UNDEF) { gpio_clear(reset_pin); xtimer_usleep(RESET_DELAY); gpio_set(reset_pin); } /* put the XBee device into command mode */ xtimer_usleep(ENTER_CMD_MODE_DELAY); _at_cmd(dev, "+++"); xtimer_usleep(ENTER_CMD_MODE_DELAY); /* disable non IEEE802.15.4 extensions */ _at_cmd(dev, "ATMM2\r"); /* put XBee module in "API mode without escaped characters" */ _at_cmd(dev, "ATAP1\r"); /* apply AT commands */ _at_cmd(dev, "ATAC\r"); /* exit command mode */ _at_cmd(dev, "ATCN\r"); /* load long address (we can not set it, its read only for Xbee devices) */ _get_addr_long(dev, dev->addr_long.uint8, 8); /* set default channel */ _set_addr(dev, &((dev->addr_long).uint8[6]), 2); tmp[1] = 0; tmp[0] = XBEE_DEFAULT_CHANNEL; _set_channel(dev, tmp, 2); /* set default PAN ID */ tmp[1] = (uint8_t)(XBEE_DEFAULT_PANID >> 8); tmp[0] = (uint8_t)(XBEE_DEFAULT_PANID & 0xff); _set_panid(dev, tmp, 2); DEBUG("xbee: Initialization successful\n"); return 0; }