Esempio n. 1
0
/*!
 * This function configures the hardware SPI for the currrent client.
 * Lock must be taken
 *
 * @param        mod              the module number
 * @param        client_config    client hardware configuration.
 * @return       This function returns 0 if successful, -EPERM otherwise.
 */
static int spi_hard_config(module_nb_t mod, spi_config * client_config)
{
	int error = 0;
	error = spi_set_baudrate(mod, client_config->bit_rate);
	if (error < 0) {
		return error;
	}
	spi_set_transfer_length(mod, client_config->bit_count);
	error = spi_select_ss(mod, client_config->ss_asserted);
	if (error < 0) {
		return error;
	}

	if (client_config->master_mode == true) {
		spi_set_mode(mod, SPI_MASTER);
	} else {
		spi_set_mode(mod, SPI_SLAVE);
	}

	if (client_config->active_high_ss_polarity == true) {
		spi_set_ss_polarity(mod, SPI_SS_ACTIVE_HIGH);
	} else {
		spi_set_ss_polarity(mod, SPI_SS_ACTIVE_LOW);
	}

	if (client_config->ss_low_between_bursts == true) {
		spi_set_ss_waveform(mod, SPI_LOW_BTN_BURST);
	} else {
		spi_set_ss_waveform(mod, SPI_PULSE_BTN_BURST);
	}

	if (client_config->phase == true) {
		spi_set_phase(mod, SPI_PHASE_1);
	} else {
		spi_set_phase(mod, SPI_PHASE_0);
	}

	if (client_config->active_high_polarity == true) {
		spi_set_polarity(mod, SPI_POLARITY_ACTIVE_HIGH);
	} else {
		spi_set_polarity(mod, SPI_POLARITY_ACTIVE_LOW);
	}
	return 0;
}
Esempio n. 2
0
File: rf233.c Progetto: Idolf/tock
/**
 * \brief      Init the radio
 * \return     Returns success/fail
 * \retval 0   Success
 */
int rf233_init(void) {
  volatile uint8_t regtemp;
  volatile uint8_t radio_state;  /* don't optimize this away, it's important */
  PRINTF("RF233: init.\n");

  /* init SPI and GPIOs, wake up from sleep/power up. */

  spi_init();
  // RF233 expects line low for CS, this is default SAM4L behavior
  //spi_set_chip_select(3);
  // POL = 0 means idle is low
  spi_set_chip_select(3);
  spi_set_polarity(0);
  // PHASE = 0 means sample leading edge
  spi_set_phase(0);
  spi_set_rate(400000);

    /* reset will put us into TRX_OFF state */
  /* reset the radio core */
  gpio_enable_output(RST_PIN);
  gpio_enable_output(SLP_PIN);
  gpio_clear(RST_PIN);
  delay_ms(1);
  gpio_set(RST_PIN);
  gpio_clear(SLP_PIN); /* be awake from sleep*/

  
  /* Read the PART_NUM register to verify that the radio is
   * working/responding. Could check in software, I just look at
   * the bus. If this is working, the first write should be 0x9C x00
   * and the return bytes should be 0x00 0x0B. - pal*/
  regtemp = trx_reg_read(RF233_REG_PART_NUM);

  /* before enabling interrupts, make sure we have cleared IRQ status */
  regtemp = trx_reg_read(RF233_REG_IRQ_STATUS);
  PRINTF("RF233: After wake from sleep\n");
  radio_state = rf233_status();
  PRINTF("RF233: Radio state 0x%04x\n", radio_state);
  calibrate_filters();
  if (radio_state == STATE_P_ON) {
    trx_reg_write(RF233_REG_TRX_STATE, TRXCMD_TRX_OFF);
  } 
  /* Assign regtemp to regtemp to avoid compiler warnings */
  regtemp = regtemp;
  // Set up interrupts
  gpio_interrupt_callback(interrupt_callback, NULL);
  gpio_enable_input(RADIO_IRQ, PullNone);
  gpio_clear(RADIO_IRQ);
  gpio_enable_interrupt(RADIO_IRQ, PullNone, RisingEdge);

  /* Configure the radio using the default values except these. */
  trx_reg_write(RF233_REG_TRX_CTRL_1,      RF233_REG_TRX_CTRL_1_CONF);
  trx_reg_write(RF233_REG_PHY_CC_CCA,      RF233_REG_PHY_CC_CCA_CONF);
  trx_reg_write(RF233_REG_PHY_TX_PWR, RF233_REG_PHY_TX_PWR_CONF);
  trx_reg_write(RF233_REG_TRX_CTRL_2,      RF233_REG_TRX_CTRL_2_CONF);
  trx_reg_write(RF233_REG_IRQ_MASK,        RF233_REG_IRQ_MASK_CONF);
  trx_reg_write(RF233_REG_XAH_CTRL_1,      0x02);
  trx_bit_write(SR_MAX_FRAME_RETRIES, 0);
  trx_bit_write(SR_MAX_CSMA_RETRIES, 0);
  PRINTF("RF233: Configured transciever.\n");
  {
    uint8_t addr[8];
    addr[0] = 0x22;
    addr[1] = 0x22;
    addr[2] = 0x22;
    addr[3] = 0x22;
    addr[4] = 0x22;
    addr[5] = 0x22;
    addr[6] = 0x22;
    addr[7] = 0x22;
    SetPanId(IEEE802154_CONF_PANID);
    
    SetIEEEAddr(addr);
    SetShortAddr(0x2222);
  }
  rf_generate_random_seed();
  
  for (uint8_t i = 0; i < 8; i++)   {
    regtemp = trx_reg_read(0x24 + i);
  }

  /* 11_09_rel */
  trx_reg_write(RF233_REG_TRX_RPC, 0xFF); /* Enable RPC feature by default */
  PRINTF("RF233: Installed addresses. Turning on radio.");
  rf233_on();
  return 0;
}