Exemplo n.º 1
0
void init(void) {
  /* Setup GPIO ports */
  /* *_LIMIT# on PB2, input and external interrupt */
  /*   NOP, DDRB already contains "0" for input mode */
  MCUCR |= _BV(ISC00);
  /* *_DIR on PB1, output */
  DDRB = _BV(PORTB1);
  /* *_STEP on PA7, output */
  DDRA = _BV(PORTA7);
  /* MOSI, MISO and SCK configured by SPI */
  spi_configure(SPI_INT_ENABLE, SPI_OFF, NULL, SPI_SLAVE, NULL, SPI_PHASE_LEADING, NULL);
  spi_hook = SPI_hook;
  /* CS_STEP# on PA3, input and pin change interrupt */
  /*  NOP, DDRA already contains "0" for input mode */
  PCMSK0 = _BV(PCINT3);
  GIFR |= _BV(INTF0) | _BV(PCIF0); /* Avoid spurious interrupts on startup */
  GIMSK |= _BV(INT0) | _BV(PCIE0);
  /* INT# on PA2, output, open-collector */
  /*  NOP, DDRA already contains "0" for High-Z and PORTA already contains "0" for open-drain */

  /* Initialize state */
  NewCommand = false;
  LimitInvert = true;
  BufferSize = 0;

  /* And off we go */
  sei();
}
Exemplo n.º 2
0
void main(void)
{
    struct spi_config config = { 0 };
    struct device *spi_mst_0 = device_get_binding("SPI_0");
    uint8_t id;
    int err;

    printk("SPI Example application\n");

    if (!spi_mst_0)
        return;

    config.config = SPI_MODE_CPOL | SPI_MODE_CPHA | SPI_WORD(16);
    config.max_sys_freq = 256;

    err = spi_configure(spi_mst_0, &config);
    if (err) {
        printk("Could not configure SPI device\n");
        return;
    }

    err = spi_slave_select(spi_mst_0, 1);
    if (err) {
        printk("Could not select SPI slave\n");
        return;
    }

    id = lsm9ds0_read_whoami_g(spi_mst_0);

    printk("LSM9DS0 Who Am I: 0x%x\n", id);
}
Exemplo n.º 3
0
static int
tmp121temp_match(device_t parent, cfdata_t cf, void *aux)
{
	struct spi_attach_args *sa = aux;

	/* configure for 10MHz */
	if (spi_configure(sa->sa_handle, SPI_MODE_0, 1000000))
		return 0;

	return 1;
}
Exemplo n.º 4
0
static int
m25p_match(device_t parent, cfdata_t cf, void *aux)
{
	struct spi_attach_args *sa = aux;

	/* configure for 20MHz, which is the max for normal reads */
	if (spi_configure(sa->sa_handle, SPI_MODE_0, 20000000))
		return 0;

	return 1;
}
Exemplo n.º 5
0
static int ti_adc108s102_read(struct device *dev,
					struct adc_seq_table *seq_table)
{
	struct ti_adc108s102_config *config = dev->config->config_info;
	struct ti_adc108s102_data *adc = dev->driver_data;
	struct spi_config spi_conf;
	uint32_t data[2] = {0, 0};
	struct nano_timer timer;
	int ret = 0;
	int32_t delay;

	spi_conf.config = config->spi_config_flags;
	spi_conf.max_sys_freq = config->spi_freq;

	nano_timer_init(&timer, data);

	if (spi_configure(adc->spi, &spi_conf)) {
		return -EIO;
	}

	if (spi_slave_select(adc->spi, config->spi_slave)) {
		return -EIO;
	}

	/* Resetting all internal channel data */
	memset(adc->chans, 0, ADC108S102_CHANNELS_SIZE);

	if (_verify_entries(seq_table) == 0) {
		return -EINVAL;
	}

	adc->seq_table = seq_table;

	/* Sampling */
	while (1) {
		delay = _ti_adc108s102_prepare(dev);
		if (delay == ADC108S102_DONE) {
			break;
		}

		nano_timer_start(&timer, delay);
		nano_task_timer_test(&timer, TICKS_UNLIMITED);

		ret = _ti_adc108s102_sampling(dev);
		if (ret != 0) {
			break;
		}

		_ti_adc108s102_handle_result(dev);
	}

	return ret;
}
Exemplo n.º 6
0
static int
oj6sh_match(device_t parent, cfdata_t match, void *aux)
{
	struct spi_attach_args *sa = aux;

	if (strcmp(match->cf_name, "oj6sh"))
		return 0;
	if (spi_configure(sa->sa_handle, SPI_MODE_0, 2500000))
		return 0;

	return 2;
}
Exemplo n.º 7
0
static int bt_spi_open(void)
{
	/* Configure RST pin and hold BLE in Reset */
	gpio_pin_configure(rst_dev, GPIO_RESET_PIN,
			   GPIO_DIR_OUT | GPIO_PUD_PULL_UP);
	gpio_pin_write(rst_dev, GPIO_RESET_PIN, 0);

	spi_configure(spi_dev, &spi_conf);

#if defined(CONFIG_BLUETOOTH_SPI_BLUENRG)
	/* Configure the CS (Chip Select) pin */
	gpio_pin_configure(cs_dev, GPIO_CS_PIN,
			   GPIO_DIR_OUT | GPIO_PUD_PULL_UP);
	gpio_pin_write(cs_dev, GPIO_CS_PIN, 1);
#endif /* CONFIG_BLUETOOTH_SPI_BLUENRG */

	/* Configure IRQ pin and the IRQ call-back/handler */
	gpio_pin_configure(irq_dev, GPIO_IRQ_PIN,
			   GPIO_DIR_IN | GPIO_INT |
			   GPIO_INT_EDGE | GPIO_INT_ACTIVE_HIGH);

	gpio_init_callback(&gpio_cb, bt_spi_isr, BIT(GPIO_IRQ_PIN));

	if (gpio_add_callback(irq_dev, &gpio_cb)) {
		return -EINVAL;
	}

	if (gpio_pin_enable_callback(irq_dev, GPIO_IRQ_PIN)) {
		return -EINVAL;
	}

	/* Start RX thread */
	k_thread_spawn(rx_stack, sizeof(rx_stack),
		       (k_thread_entry_t)bt_spi_rx_thread,
		       NULL, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT);

	/* Take BLE out of reset */
	gpio_pin_write(rst_dev, GPIO_RESET_PIN, 1);

	/* Device will let us know when it's ready */
	k_sem_take(&sem_initialised, K_FOREVER);

	return 0;
}
Exemplo n.º 8
0
int main(void)
{
	struct spi_config config = { 0 };
	struct device *spi_mst_0 = device_get_binding("SPI_0");
	uint8_t manufacturer, device_id;
	int err;

	printk("SPI Example application\n");

	if (!spi_mst_0)
		return -EIO;

	config.config = SPI_MODE_CPOL | SPI_MODE_CPHA | SPI_WORD(8);
	config.max_sys_freq = 256;

	err = spi_configure(spi_mst_0, &config);
	if (err) {
		printk("Could not configure SPI device\n");
		return -EIO;
	}

	err = spi_slave_select(spi_mst_0, 1);
	if (err) {
		printk("Could not select SPI slave\n");
		return -EIO;
	}

	err = w25q80bl_read_id(spi_mst_0, &manufacturer, &device_id);
	if (err) {
		printk("Could not get Manufacturer and Device ID from SPI Flash\n");
		return -EIO;
	}

	printk("SPI Flash Manufacturer %x Device Id %x\n", manufacturer,
	       device_id);

	return 0;
}
Exemplo n.º 9
0
static int bmi160_transceive(struct device *dev, uint8_t *tx_buf,
			     uint8_t tx_buf_len, uint8_t *rx_buf,
			     uint8_t rx_buf_len)
{
	struct bmi160_device_config *dev_cfg = dev->config->config_info;
	struct bmi160_device_data *bmi160 = dev->driver_data;
	struct spi_config spi_cfg;

	spi_cfg.config = SPI_WORD(8);
	spi_cfg.max_sys_freq = dev_cfg->spi_freq;

	if (spi_configure(bmi160->spi, &spi_cfg) < 0) {
		SYS_LOG_DBG("Cannot configure SPI bus.");
		return -EIO;
	}

	if (spi_slave_select(bmi160->spi, dev_cfg->spi_slave) < 0) {
		SYS_LOG_DBG("Cannot select slave.");
		return -EIO;
	}

	return spi_transceive(bmi160->spi, tx_buf, tx_buf_len,
			      rx_buf, rx_buf_len);
}
Exemplo n.º 10
0
//! Initialise SPI for SWD
//!
//! @return BDM_RC_OK => success
//!
uint8_t initDSPI_SWD(void) {
   spi_configure(SPI_CTAR_LSBFE_MASK|SPI_CTAR_FMSZ(8-1),      // 8-bit transfer
                 SPI_CTAR_LSBFE_MASK|SPI_CTAR_FMSZ(16-1));    // 16-bit transfer 
   return spi_setSpeed(0);
}
Exemplo n.º 11
0
void main ()
{
  const unsigned int pwr_mask = (1U << pwr_pinmux.pin);
  const unsigned int csn_mask = (1U << csn_pinmux.pin);
  const sBSPACMdeviceEFM32periphUSARTdevcfg * spicfgp = (const sBSPACMdeviceEFM32periphUSARTdevcfg *)spi->devcfg.ptr;
  const sBSPACMdeviceEFM32pinmux * const gdo1_pinmuxp = &spicfgp->uart.common.rx_pinmux;
  GPIO_P_TypeDef * const pwr_port = pwr_pinmux.port;
  GPIO_P_TypeDef * const csn_port = csn_pinmux.port;
  GPIO_P_TypeDef * const gdo1_port = gdo1_pinmuxp->port;
  const unsigned int gdo1_mask = (1U << gdo1_pinmuxp->pin);
  USART_TypeDef * usart = (USART_TypeDef *)spi->uart;
  const sBSPACMperiphUARTconfiguration cfg = { .speed_baud = 0 };
  int rv;
  uint8_t rc = 0;
  (void)rv;

  vBSPACMledConfigure();
  SystemCoreClockUpdate();
  BSPACM_CORE_ENABLE_INTERRUPT();
  BSPACM_CORE_ENABLE_CYCCNT();

  printf("\n" __DATE__ " " __TIME__ "\n");
  printf("System clock %lu Hz\n", SystemCoreClock);
  spi = spi_configure(spi, &cfg);
  csn_port->DOUTSET = csn_mask;
  printf("SPI at %p usart at %p\n", spi, usart);
  printf("CTRL %lx\n", usart->CTRL);
  vBSPACMdeviceEFM32pinmuxConfigure(&pwr_pinmux, 1, 0);
  vBSPACMdeviceEFM32pinmuxConfigure(&csn_pinmux, 1, 1);

  printf("GDO1 %d\n", !!(gdo1_port->DIN & gdo1_mask));
  pwr_port->DOUTSET = pwr_mask;
  csn_port->DOUTCLR = csn_mask;
  while (! (gdo1_mask & gdo1_port->DIN)) {
  }

  do {
    rc = sendStrobe(0x30);
    printf("Reset got %x\n", rc);
  } while (0x0f != rc);
  printf("PARTNUM response %#02x\n", readRegister(0x30));
  printf("VERSION response %#02x\n", readRegister(0x31));
  printf("IOCFG2 read %#02x\n", readRegister(0x00));
  printf("IOCFG1 read %#02x\n", readRegister(0x01));
  printf("IOCFG0 read %#02x\n", readRegister(0x02));

  /* ChipCon radios consume 1.4mA when idle.  That goes down to
   * nominally 400 nA if the GDOs are configured to "HW to 0" and the
   * chip is told to power-down on loss of CSn.  On the EXP430F5438
   * the RF PWR header indicates that a CC1101 is using 40 nA in this
   * mode.*/
  rc = writeRegister(0x00, 0x2f);
  rc = writeRegister(0x01, 0x2f);
  rc = writeRegister(0x02, 0x2f);
  printf("Cleared IOCFG\n");
  printf("IOCFG2 read %#02x\n", readRegister(0x00));
  printf("IOCFG1 read %#02x\n", readRegister(0x01));
  printf("IOCFG0 read %#02x\n", readRegister(0x02));

  /* SPWD */
  rc = sendStrobe(0x39);
  csn_port->DOUTSET = csn_mask;
  printf("SPWD got %d\n", rc);

  fflush(stdout);
  ioctl(1, BSPACM_IOCTL_FLUSH, O_WRONLY);
}