Example #1
0
void SPI_Init(void)
{
	//MOSI --> J17-12 --> GPIO-115
	//SCK --> J17-11 -->GPIO-109
	spi = mraa_spi_init(1);

	if (spi == NULL)
	{
		printf("SPI initialization failed, check syslog for details, exit...\n");
		exit(1);
	}
	//printf("SPI initialized successfully\n");

	mraa_spi_frequency (spi, fSCLK);
	//printf("SPI clock frequency set to %iHz\n", fSCLK);

	/*
	MRAA_SPI_MODE0
	CPOL = 0, CPHA = 0,
	Clock idle low, data is clocked in on rising edge, output data (change) on falling edge
	*/
	mraa_spi_mode(spi, MRAA_SPI_MODE0);
	//printf("SPI set to MODE0\n");

	//LSB Transmission
	mraa_spi_lsbmode(spi, 0);
}
Example #2
0
int
main(int argc, char** argv)
{
    //! [Interesting]
    mraa_spi_context spi;
    spi = mraa_spi_init(1);
    if (spi == NULL) {
        printf("Initialization of spi failed, check syslog for details, exit...\n");
        exit(1);
    }

    printf("SPI initialised successfully\n");

    mraa_spi_frequency(spi, 400000);
    mraa_spi_lsbmode(spi, 0);

    // The MAX7219/21 Chip needs the data in word size
    if (mraa_spi_bit_per_word(spi, 16) != MRAA_SUCCESS) {
        printf("Could not set SPI Device to 16Bit mode, exit...\n");
        exit(1);
    };

    mraa_spi_write_word(spi, 0x0900); // Do not decode bits
    mraa_spi_write_word(spi, 0x0a05); // Brightness of LEDs
    mraa_spi_write_word(spi, 0x0b07); // Show all Scan Lines
    mraa_spi_write_word(spi, 0x0c01); // Display on
    mraa_spi_write_word(spi, 0x0f00); // Testmode off

    // Display Pattern on the display
    uint16_t dataAA55[] = { 0x01aa, 0x0255, 0x03aa, 0x0455, 0x05aa, 0x0655, 0x07aa, 0x0855 };
    mraa_spi_write_buf_word(spi, dataAA55, 16);

    sleep(2);

    // Display inverted Pattern
    uint16_t data55AA[] = { 0x0155, 0x02aa, 0x0355, 0x04aa, 0x0555, 0x06aa, 0x0755, 0x08aa };
    mraa_spi_write_buf_word(spi, data55AA, 16);

    sleep(2);

    // Clear the display
    uint16_t data[] = { 0x0100, 0x0200, 0x0300, 0x0400, 0x0500, 0x0600, 0x0700, 0x0800 };
    mraa_spi_write_buf_word(spi, data, 16);

    int i;
    int j;
    // cycle through all LED's
    for (i = 1; i <= 8; i++) {
        for (j = 0; j < 8; j++) {
            mraa_spi_write_word(spi, (i << 8) + (1 << j));
            sleep(1);
        }
        mraa_spi_write_word(spi, i << 8);
    }

    mraa_spi_stop(spi);

    //! [Interesting]
}
Example #3
0
    /**
     * Initialise SPI object using the board mapping to set muxes
     *
     * @param bus to use, as listed in the platform definition, normally 0
     */
    Spi(int bus)
    {
        m_spi = mraa_spi_init(bus);

        if (m_spi == NULL) {
            throw std::invalid_argument("Error initialising SPI bus");
        }
    }
Example #4
0
DW1000::DW1000()
{
    spi = mraa_spi_init(5);
    mraa_spi_frequency(spi, DW1000_SPI_CLOCK_SPEED);

    cs = mraa_gpio_init(DW_CS);
    mraa_gpio_dir(cs, MRAA_GPIO_OUT);
    mraa_gpio_write(cs, 0x1);

    resetAll();                         // we do a soft reset of the DW1000 everytime the driver starts

    //Those values are for the 110kbps mode (5, 16MHz, 1024 Symbols) and are quite complete
    writeRegister16(DW1000_AGC_CTRL, 0x04, 0x8870);             //AGC_TUNE1 for 16MHz PRF
    writeRegister32(DW1000_AGC_CTRL, 0x0C, 0x2502A907);         //AGC_TUNE2 (Universal)
    writeRegister16(DW1000_AGC_CTRL, 0x12, 0x0055);             //AGC_TUNE3 (Universal)

    writeRegister16(DW1000_DRX_CONF, 0x02, 0x000A);             //DRX_TUNE0b for 110kbps
    writeRegister16(DW1000_DRX_CONF, 0x04, 0x0087);             //DRX_TUNE1a for 16MHz PRF
    writeRegister16(DW1000_DRX_CONF, 0x06, 0x0064);             //DRX_TUNE1b for 110kbps & > 1024 symbols
    writeRegister32(DW1000_DRX_CONF, 0x08, 0x351A009A);         //PAC size for 1024 symbols preamble & 16MHz PRF
    //writeRegister32(DW1000_DRX_CONF, 0x08, 0x371A011D);               //PAC size for 2048 symbols preamble

    writeRegister8 (DW1000_LDE_CTRL, 0x0806, 0xD);              //LDE_CFG1
    writeRegister16(DW1000_LDE_CTRL, 0x1806, 0x1607);           //LDE_CFG2 for 16MHz PRF

    writeRegister32(DW1000_TX_POWER, 0, 0x28282828);            //Power for channel 5

    writeRegister8(DW1000_RF_CONF, 0x0B, 0xD8);                 //RF_RXCTRLH for channel 5
    writeRegister32(DW1000_RF_CONF, 0x0C, 0x001E3FE0);          //RF_TXCTRL for channel 5

    writeRegister8 (DW1000_TX_CAL, 0x0B, 0xC0);                 //TC_PGDELAY for channel 5

    writeRegister32 (DW1000_FS_CTRL, 0x07, 0x0800041D);         //FS_PLLCFG for channel 5
    writeRegister8 (DW1000_FS_CTRL, 0x0B, 0xA6);                //FS_PLLTUNE for channel 5

    loadLDE();                          // important everytime DW1000 initialises/awakes otherwise the LDE algorithm must be turned off or there's receiving malfunction see User Manual LDELOAD on p22 & p158

    // 110kbps CAUTION: a lot of other registers have to be set for an optimized operation on 110kbps
    writeRegister16(DW1000_TX_FCTRL, 1, 0x0800 | 0x0100 | 0x0080); // use 1024 symbols preamble (0x0800) (previously 2048 - 0x2800), 16MHz pulse repetition frequency (0x0100), 110kbps bit rate (0x0080) see p.69 of DW1000 User Manual
    writeRegister8(DW1000_SYS_CFG, 2, 0x44);    // enable special receiving option for 110kbps (disable smartTxPower)!! (0x44) see p.64 of DW1000 User Manual [DO NOT enable 1024 byte frames (0x03) becuase it generates disturbance of ranging don't know why...]

    writeRegister16(DW1000_TX_ANTD, 0, 16384); // set TX and RX Antenna delay to neutral because we calibrate afterwards
    writeRegister16(DW1000_LDE_CTRL, 0x1804, 16384); // = 2^14 a quarter of the range of the 16-Bit register which corresponds to zero calibration in a round trip (TX1+RX2+TX2+RX1)

    writeRegister8(DW1000_SYS_CFG, 3, 0x20);    // enable auto reenabling receiver after error
}
Example #5
0
File: lis3dh.c Project: g-vidal/upm
// Init
lis3dh_context
lis3dh_init(int bus, int addr, int cs)
{
    lis3dh_context dev = (lis3dh_context) malloc(sizeof(struct _lis3dh_context));

    if (!dev) {
        return NULL;
    }

    // Zero out context
    memset((void*) dev, 0, sizeof(struct _lis3dh_context));

    // Make sure MRAA is initialized
    if (mraa_init() != MRAA_SUCCESS) {
        printf("%s: mraa_init() failed\n", __FUNCTION__);
        lis3dh_close(dev);
        return NULL;
    }

    if (addr < 0) {
        // SPI
        if (!(dev->spi = mraa_spi_init(bus))) {
            printf("%s: mraa_spi_init() for bus %d failed\n", __FUNCTION__, bus);
            lis3dh_close(dev);
            return NULL;
        }

        // Only create CS context if we are actually using a valid pin.
        // A hardware controlled pin should specify CS as -1.
        if (cs >= 0) {
            if (!(dev->gpioCS = mraa_gpio_init(cs))) {
                printf("%s: mraa_gpio_init() for CS pin %d failed\n", __FUNCTION__, cs);
                lis3dh_close(dev);
                return NULL;
            }
            mraa_gpio_dir(dev->gpioCS, MRAA_GPIO_OUT);
        }

        mraa_spi_mode(dev->spi, MRAA_SPI_MODE0);
        if (mraa_spi_frequency(dev->spi, 5000000)) {
            printf("%s: mraa_spi_frequency() failed\n", __FUNCTION__);
            lis3dh_close(dev);
            return NULL;
        }
    } else {
        // I2C
        if (!(dev->i2c = mraa_i2c_init(bus))) {
            printf("%s: mraa_i2c_init() for bus %d failed\n", __FUNCTION__, bus);
            lis3dh_close(dev);
            return NULL;
        }

        if (mraa_i2c_address(dev->i2c, addr)) {
            printf("%s: mraa_i2c_address() for address 0x%x failed\n", __FUNCTION__, addr);
            lis3dh_close(dev);
            return NULL;
        }
    }

    // Check the chip id
    uint8_t chipID = lis3dh_get_chip_id(dev);
    if (chipID != LIS3DH_CHIPID) {
        printf("%s: invalid chip id: %02x, expected %02x\n", __FUNCTION__, chipID, LIS3DH_CHIPID);
        lis3dh_close(dev);
        return NULL;
    }

    // Call devinit with default options
    if (lis3dh_devinit(dev, LIS3DH_ODR_100HZ, LIS3DH_FS_2G, true)) {
        printf("%s: lis3dh_devinit() failed\n", __FUNCTION__);
        lis3dh_close(dev);
        return NULL;
    }

    return dev;
}
Example #6
0
void hal_aci_tl_init(aci_pins_t *a_pins, bool debug)
{
    mraa_result_t error = MRAA_SUCCESS;
    aci_debug_print = debug;

    /* Needs to be called as the first thing for proper intialization*/
    m_aci_pins_set(a_pins);

    /*
     * Init SPI
     */
    a_pins->m_spi = mraa_spi_init (0);
    if (a_pins->m_spi == NULL) {
        throw std::invalid_argument(std::string(__FUNCTION__) +
                                    ": mraa_spi_init() failed");
    }

    mraa_spi_frequency (a_pins->m_spi, 2000000);
    mraa_spi_mode (a_pins->m_spi, MRAA_SPI_MODE0);

    /* Initialize the ACI Command queue. This must be called after the delay above. */
    aci_queue_init(&aci_tx_q);
    aci_queue_init(&aci_rx_q);

    // Configure the IO lines
    a_pins->m_rdy_ctx = mraa_gpio_init (a_pins->rdyn_pin);
    if (a_pins->m_rdy_ctx == NULL) {
        throw std::invalid_argument(std::string(__FUNCTION__) +
                                    ": mraa_gpio_init(rdyn) failed, invalid pin?");
    }

    a_pins->m_req_ctx = mraa_gpio_init (a_pins->reqn_pin);
    if (a_pins->m_req_ctx == NULL) {
        throw std::invalid_argument(std::string(__FUNCTION__) +
                                    ": mraa_gpio_init(reqn) failed, invalid pin?");
    }

    a_pins->m_rst_ctx = mraa_gpio_init (a_pins->reset_pin);
    if (a_pins->m_rst_ctx == NULL) {
        throw std::invalid_argument(std::string(__FUNCTION__) +
                                    ": mraa_gpio_init(reset) failed, invalid pin?");
    }

    error = mraa_gpio_dir (a_pins->m_rdy_ctx, MRAA_GPIO_IN);
    if (error != MRAA_SUCCESS) {
        printf ("[ERROR] GPIO failed to initilize \n");
    }

    error = mraa_gpio_dir (a_pins->m_req_ctx, MRAA_GPIO_OUT);
    if (error != MRAA_SUCCESS) {
        printf ("[ERROR] GPIO failed to initilize \n");
    }

    error = mraa_gpio_dir (a_pins->m_rst_ctx, MRAA_GPIO_OUT);
    if (error != MRAA_SUCCESS) {
        printf ("[ERROR] GPIO failed to initilize \n");
    }

    if (UNUSED != a_pins->active_pin) {
    }

  /* Pin reset the nRF8001, required when the nRF8001 setup is being changed */
  hal_aci_tl_pin_reset();

  /* Set the nRF8001 to a known state as required by the datasheet*/
  mraa_gpio_write (a_pins->m_req_ctx, LOW);

  usleep(30000); //Wait for the nRF8001 to get hold of its lines - the lines float for a few ms after the reset

    /* Attach the interrupt to the RDYN line as requested by the caller */
    if (a_pins->interface_is_interrupt) {
        // We use the LOW level of the RDYN line as the atmega328 can wakeup from sleep only on LOW
        // attachInterrupt(a_pins->interrupt_number, m_aci_isr, LOW);
    }
}
Example #7
0
File: spi.c Project: KurtE/mraa
int
main(int argc, char** argv)
{
    mraa_result_t status = MRAA_SUCCESS;
    mraa_spi_context spi;
    int i, j;

    /* initialize mraa for the platform (not needed most of the times) */
    mraa_init();

    //! [Interesting]
    /* initialize SPI bus */
    spi = mraa_spi_init(SPI_BUS);
    if (spi == NULL) {
        fprintf(stderr, "Failed to initialize SPI\n");
        mraa_deinit();
        return EXIT_FAILURE;
    }

    /* set SPI frequency */
    status = mraa_spi_frequency(spi, SPI_FREQ);
    if (status != MRAA_SUCCESS)
        goto err_exit;

    /* set big endian mode */
    status = mraa_spi_lsbmode(spi, 0);
    if (status != MRAA_SUCCESS) {
        goto err_exit;
    }

    /* MAX7219/21 chip needs the data in word size */
    status = mraa_spi_bit_per_word(spi, 16);
    if (status != MRAA_SUCCESS) {
        fprintf(stdout, "Failed to set SPI Device to 16Bit mode\n");
        goto err_exit;
    }

    /* do not decode bits */
    mraa_spi_write_word(spi, 0x0900);

    /* brightness of LEDs */
    mraa_spi_write_word(spi, 0x0a05);

    /* show all scan lines */
    mraa_spi_write_word(spi, 0x0b07);

    /* set display on */
    mraa_spi_write_word(spi, 0x0c01);

    /* testmode off */
    mraa_spi_write_word(spi, 0x0f00);

    while (flag) {
        /* set display pattern */
        mraa_spi_write_buf_word(spi, pat, 16);

        sleep(2);

        /* set inverted display pattern */
        mraa_spi_write_buf_word(spi, pat_inv, 16);

        sleep(2);

        /* clear the LED's */
        mraa_spi_write_buf_word(spi, pat_clear, 16);

        /* cycle through all LED's */
        for (i = 1; i <= 8; i++) {
            for (j = 0; j < 8; j++) {
                mraa_spi_write_word(spi, (i << 8) + (1 << j));
                sleep(1);
            }
            mraa_spi_write_word(spi, i << 8);
        }
    }

    /* stop spi */
    mraa_spi_stop(spi);

    //! [Interesting]
    /* deinitialize mraa for the platform (not needed most of the times) */
    mraa_deinit();

    return EXIT_SUCCESS;

err_exit:
    mraa_result_print(status);

    /* stop spi */
    mraa_spi_stop(spi);

    /* deinitialize mraa for the platform (not needed most of the times) */
    mraa_deinit();

    return EXIT_FAILURE;
}
Example #8
0
uint8_t RF22::init()
{
    // Wait for RF22 POR (up to 16msec)
    usleep (16);

    // Initialise the slave select pin    
    _cs = mraa_gpio_init(_slaveSelectPin);
	mraa_gpio_dir(_cs, MRAA_GPIO_OUT);
	mraa_gpio_write(_cs, 0x1);

    // start the SPI library:
    // Note the RF22 wants mode 0, MSB first and default to 1 Mbps
    _spi = mraa_spi_init(0);
    mraa_spi_mode (_spi, MRAA_SPI_MODE0);
    mraa_spi_lsbmode(_spi, 0);
    mraa_spi_frequency(_spi, 1000000); // 1Mhz
    usleep (100);

    // Software reset the device
    reset();

    // Get the device type and check it
    // This also tests whether we are really connected to a device
    _deviceType = spiRead(RF22_REG_00_DEVICE_TYPE);
    if (   _deviceType != RF22_DEVICE_TYPE_RX_TRX
        && _deviceType != RF22_DEVICE_TYPE_TX)
	return 0;
 
    _irq = mraa_gpio_init(_interrupt + 2);
    mraa_gpio_dir(_irq, MRAA_GPIO_IN);
    gpio_edge_t edge = MRAA_GPIO_EDGE_FALLING;
	// Set up interrupt handler
    if (_interrupt == 0)
    {
		_RF22ForInterrupt[0] = this;
    	mraa_gpio_isr(_irq, edge, &RF22::isr0, NULL);
    }
    else if (_interrupt == 1)
    {
		_RF22ForInterrupt[1] = this;
    	mraa_gpio_isr(_irq, edge, &RF22::isr1, NULL);
    }
    else
	return 0;

    clearTxBuf();
    clearRxBuf();
  
    // Most of these are the POR default
    spiWrite(RF22_REG_7D_TX_FIFO_CONTROL2, RF22_TXFFAEM_THRESHOLD);
    spiWrite(RF22_REG_7E_RX_FIFO_CONTROL,  RF22_RXFFAFULL_THRESHOLD);
    spiWrite(RF22_REG_30_DATA_ACCESS_CONTROL, RF22_ENPACRX | RF22_ENPACTX | RF22_ENCRC | RF22_CRC_CRC_16_IBM);
    // Configure the message headers
    // Here we set up the standard packet format for use by the RF22 library
    // 8 nibbles preamble
    // 2 SYNC words 2d, d4
    // Header length 4 (to, from, id, flags)
    // 1 octet of data length (0 to 255)
    // 0 to 255 octets data
    // 2 CRC octets as CRC16(IBM), computed on the header, length and data
    // On reception the to address is check for validity against RF22_REG_3F_CHECK_HEADER3
    // or the broadcast address of 0xff
    // If no changes are made after this, the transmitted
    // to address will be 0xff, the from address will be 0xff
    // and all such messages will be accepted. This permits the out-of the box
    // RF22 config to act as an unaddresed, unreliable datagram service
    spiWrite(RF22_REG_32_HEADER_CONTROL1, RF22_BCEN_HEADER3 | RF22_HDCH_HEADER3);
    spiWrite(RF22_REG_33_HEADER_CONTROL2, RF22_HDLEN_4 | RF22_SYNCLEN_2);
    setPreambleLength(8);
    uint8_t syncwords[] = { 0x2d, 0xd4 };
    setSyncWords(syncwords, sizeof(syncwords));
    setPromiscuous(0); 
    // Check the TO header against RF22_DEFAULT_NODE_ADDRESS
    spiWrite(RF22_REG_3F_CHECK_HEADER3, RF22_DEFAULT_NODE_ADDRESS);
    // Set the default transmit header values
    setHeaderTo(RF22_DEFAULT_NODE_ADDRESS);
    setHeaderFrom(RF22_DEFAULT_NODE_ADDRESS);
    setHeaderId(0);
    setHeaderFlags(0);

    // Ensure the antenna can be switched automatically according to transmit and receive
    // This assumes GPIO0(out) is connected to TX_ANT(in) to enable tx antenna during transmit
    // This assumes GPIO1(out) is connected to RX_ANT(in) to enable rx antenna during receive
    spiWrite (RF22_REG_0B_GPIO_CONFIGURATION0, 0x12) ; // TX state
    spiWrite (RF22_REG_0C_GPIO_CONFIGURATION1, 0x15) ; // RX state

    // Enable interrupts
    spiWrite(RF22_REG_05_INTERRUPT_ENABLE1, RF22_ENTXFFAEM | RF22_ENRXFFAFULL | RF22_ENPKSENT | RF22_ENPKVALID | RF22_ENCRCERROR | RF22_ENFFERR);
    spiWrite(RF22_REG_06_INTERRUPT_ENABLE2, RF22_ENPREAVAL);

    // Set some defaults. An innocuous ISM frequency, and reasonable pull-in
    setFrequency(434.0, 0.05);
//    setFrequency(900.0);
    // Some slow, reliable default speed and modulation
    setModemConfig(FSK_Rb2_4Fd36);
//    setModemConfig(FSK_Rb125Fd125);
    // Minimum power
    setTxPower(RF22_TXPOW_8DBM);
//    setTxPower(RF22_TXPOW_17DBM);

    return 1;
}
Example #9
0
File: kx122.c Project: g-vidal/upm
kx122_context kx122_init(int bus, int addr, int chip_select_pin, int spi_bus_frequency)
{
  kx122_context dev = (kx122_context)malloc(sizeof(struct _kx122_context));

  if(!dev){
    return NULL;
  }

  dev->using_spi = false;

  dev->i2c = NULL;
  dev->spi = NULL;
  dev->chip_select = NULL;

  dev->gpio1 = NULL;
  dev->gpio2 = NULL;

  if(mraa_init() != MRAA_SUCCESS){
    printf("%s: mraa_init() failed.\n", __FUNCTION__);
    kx122_close(dev);
    return NULL;
  }

  if(addr == -1){
    dev->using_spi = true;
  }

  if(dev->using_spi){

    if (spi_bus_frequency > 10000000){	// KX122 has a maximum SPI bus speed of 10MHz
      printf("%s: bus frequency too high - KX122 has a maximum SPI bus speed of 10MHz.\n", __FUNCTION__);
      kx122_close(dev);
      return NULL;
    }

    if (!(dev->spi = mraa_spi_init(bus))){
      printf("%s: mraa_spi_init() failed.\n", __FUNCTION__);
      kx122_close(dev);
      return NULL;
    }

    if (!(dev->chip_select = mraa_gpio_init(chip_select_pin))){
      printf("%s: mraa_gpio_init() failed.\n", __FUNCTION__);
      kx122_close(dev);
      return NULL;
    }

    mraa_gpio_dir(dev->chip_select,MRAA_GPIO_OUT);
    mraa_spi_mode(dev->spi,MRAA_SPI_MODE0);

    if (mraa_spi_frequency(dev->spi, spi_bus_frequency)){
      printf("%s: mraa_spi_frequency() failed.\n", __FUNCTION__);
      kx122_close(dev);
      return NULL;
    }
  }
  else{ //Using I2C
    if (!(dev->i2c = mraa_i2c_init(bus))){
      printf("%s: mraa_i2c_init() failed, used bus: %d\n", __FUNCTION__,bus);
      kx122_close(dev);
      return NULL;
    }

    if (mraa_i2c_address(dev->i2c, addr)){
      printf("%s: mraa_i2c_address() failed.\n", __FUNCTION__);
      kx122_close(dev);
      return NULL;
    }
  }

  uint8_t who_am_i;
  kx122_get_who_am_i(dev,&who_am_i);

  if(who_am_i != KX122_WHO_AM_I_WIA_ID){
    printf("%s: Wrong WHO AM I received, expected: 0x%x | got: 0x%x\n", __FUNCTION__,KX122_WHO_AM_I_WIA_ID,who_am_i);
    kx122_close(dev);
    return NULL;
  }

  kx122_set_default_values(dev);

  kx122_device_init(dev,KX122_ODR_50,HIGH_RES,KX122_RANGE_2G);
  return dev;
}