コード例 #1
0
ファイル: nrf905_driver.c プロジェクト: ts37140/nrf905
int set_nrf905_op_mode(struct nrf905_dev_data *dev, enum nrf905_op_mode mode)
{
	int retval = 0;
	unsigned int mode_switch_delay = 0;

	if (mode < 0 || mode > NRF905_TX) {
		dev_err(dev->char_dev, "Invalid Nrf905 mode: %#x\n", mode);
		retval = -EINVAL;
		goto out;
	}

	if (dev->chip.mode == mode)
		goto out;

	/* Delay for nRF905 mode changes */
	if (dev->chip.mode == NRF905_POWER_DOWN)
		mode_switch_delay = 3000; /* 3ms */
	else
		mode_switch_delay = 700; /* 700us */

	if (mode == NRF905_TX) {
		memcpy(dev->chip.tx_payload, dev->dev_send_buf,
			NRF905_PAYLOAD_LEN);

		set_nrf905_op_mode(dev, NRF905_SPI_READY);

		nrf905_set_tx_addr(dev);

		nrf905_write_tx_payload(dev);

		/* check the channel is free before starting tx */
		retval = nrf905_wait_free_channel(dev);
		if (retval < 0) {
			set_nrf905_op_mode(dev, NRF905_POWER_DOWN);
			goto out;
		}
	}

	dev->chip.mode = mode;

	nrf905_set_gpio(dev, mode);

	/* wait chip to change the mode */
	usleep_range(mode_switch_delay, mode_switch_delay + 100);

out:
	return retval;
}
コード例 #2
0
ファイル: main.c プロジェクト: Drooids/nrf
int main(void)
{
  uint8_t tx_addr[4] = { 0xaa, 0xab, 0xac, 0xad };
  uint8_t rx_addr[4] = { 0xa1, 0xa2, 0xa3, 0xa4 };

  sys_setup();

  uart_setup();

  /* setup spi first */
  spi_setup_master();
  spi_set_sck_freq(SPI_SCK_FREQ_FOSC2);

  nrf905_setup();
  nrf905_set_tx_addr(tx_addr, 4);
  nrf905_set_rx_addr(rx_addr, 4);
  nrf905_set_payload_width(2);
  nrf905_commit_config();

#if 0
  {
    uint8_t i;
    dump_config();
    for (i = 0; i != sizeof(nrf905_config); ++i)
      nrf905_config[i] = 0x2a;
    nrf905_cmd_rc();
    dump_config();
  }
#endif

#if 0
  {
    uint8_t x;
    uart_write((uint8_t*)"rx side\r\n", 9);
    uart_write((uint8_t*)"press space\r\n", 13);
    uart_read_uint8(&x);
    uart_write((uint8_t*)"starting\r\n", 10);
  }
#endif

  led_setup();

  nrf905_set_rx();

  while (1)
  {
  wait_cd:
    if (nrf905_is_cd() == 0)
    {
      led_set(LED_RED);
      while (nrf905_is_cd() == 0) ;
      /* uart_write((uint8_t*)"cd\r\n", 4); */
    }

#if 0
    while (nrf905_is_am() == 0) ;
    uart_write((uint8_t*)"am\r\n", 4);
    led_mask = LED_GREEN;
#endif

    led_set(LED_GREEN);

  wait_dr:
    while (nrf905_is_dr() == 0)
    {
      if (nrf905_is_cd() == 0) goto wait_cd;
    }
    uart_write((uint8_t*)"dr\r\n", 4);

    reset_pattern();

    /* wait(); */
    nrf905_read_payload();

    if (check_pattern() == 0)
    {
      uart_write((uint8_t*)"ok\r\n", 4);
      led_set(LED_BLUE);
    }
    else led_set(LED_GREEN);

    goto wait_dr;
  }

  return 0;
}