예제 #1
0
파일: wpanusb.c 프로젝트: sunkaizhu/zephyr
int net_recv_data(struct net_if *iface, struct net_buf *buf)
{
	struct net_buf *frag;

	SYS_LOG_DBG("Got data, buf %p, len %d frags->len %d",
		    buf, buf->len, net_buf_frags_len(buf));

	frag = net_buf_frag_last(buf);

	/**
	 * Add length 1 byte, do not forget to reserve it
	 */
	net_buf_push_u8(frag, net_buf_frags_len(buf) - 1);

	hexdump("<", frag->data, net_buf_frags_len(buf));

	try_write(WPANUSB_ENDP_BULK_IN, frag->data, net_buf_frags_len(buf));

	net_nbuf_unref(buf);

	return 0;
}
예제 #2
0
파일: spi.c 프로젝트: bboozzoo/zephyr
static int bt_spi_send(struct net_buf *buf)
{
	u8_t header[5] = { SPI_WRITE, 0x00,  0x00,  0x00,  0x00 };
	u32_t pending;

	/* Buffer needs an additional byte for type */
	if (buf->len >= SPI_MAX_MSG_LEN) {
		BT_ERR("Message too long");
		return -EINVAL;
	}

	/* Allow time for the read thread to handle interrupt */
	while (true) {
		gpio_pin_read(irq_dev, GPIO_IRQ_PIN, &pending);
		if (!pending) {
			break;
		}
		k_sleep(1);
	}

	k_sem_take(&sem_busy, K_FOREVER);

	switch (bt_buf_get_type(buf)) {
	case BT_BUF_ACL_OUT:
		net_buf_push_u8(buf, HCI_ACL);
		break;
	case BT_BUF_CMD:
		net_buf_push_u8(buf, HCI_CMD);
		break;
	default:
		BT_ERR("Unsupported type");
		k_sem_give(&sem_busy);
		return -EINVAL;
	}

	/* Poll sanity values until device has woken-up */
	do {
#if defined(CONFIG_BLUETOOTH_SPI_BLUENRG)
		gpio_pin_write(cs_dev, GPIO_CS_PIN, 1);
		gpio_pin_write(cs_dev, GPIO_CS_PIN, 0);
#endif /* CONFIG_BLUETOOTH_SPI_BLUENRG */
		spi_transceive(spi_dev, header, 5, rxmsg, 5);

		/*
		 * RX Header (rxmsg) must contain a sanity check Byte and size
		 * information.  If it does not contain BOTH then it is
		 * sleeping or still in the initialisation stage (waking-up).
		 */
	} while (rxmsg[STATUS_HEADER_READY] != READY_NOW ||
		 (rxmsg[1] | rxmsg[2] | rxmsg[3] | rxmsg[4]) == 0);

	/* Transmit the message */
	do {
		spi_transceive(spi_dev, buf->data, buf->len, rxmsg, buf->len);
	} while (rxmsg[0] == 0);

#if defined(CONFIG_BLUETOOTH_SPI_BLUENRG)
	/* Deselect chip */
	gpio_pin_write(cs_dev, GPIO_CS_PIN, 1);
#endif /* CONFIG_BLUETOOTH_SPI_BLUENRG */
	k_sem_give(&sem_busy);

	spi_dump_message("TX:ed", buf->data, buf->len);

#if defined(CONFIG_BLUETOOTH_SPI_BLUENRG)
	/*
	 * Since a RESET has been requested, the chip will now restart.
	 * Unfortunately the BlueNRG will reply with "reset received" but
	 * since it does not send back a NOP, we have no way to tell when the
	 * RESET has actually taken palce.  Instead, we use the vendor command
	 * EVT_BLUE_INITIALIZED as an indication that it is safe to proceed.
	 */
	if (bt_spi_get_cmd(buf->data) == BT_HCI_OP_RESET) {
		k_sem_take(&sem_initialised, K_FOREVER);
	}
#endif /* CONFIG_BLUETOOTH_SPI_BLUENRG */

	net_buf_unref(buf);

	return 0;
}