boolean Adafruit_STMPE610::begin() { // hardware SPI InitTransaction(); // SPI = mraa_spi_init(0); // which buss? will experment here... SPI = mraa_spi_init_software_cs(0); // which buss? will experment here... mraa_spi_frequency(SPI, 1000000); mraa_spi_lsbmode(SPI, false); mraa_spi_mode(SPI, MRAA_SPI_MODE0); m_spiMode = MRAA_SPI_MODE0; _gpioCS = mraa_gpio_init(_cs); mraa_gpio_dir(_gpioCS, MRAA_GPIO_OUT); CSHigh(); mraa_spi_write(SPI, 0x00); // try mode0 if (getVersion() != 0x811) { //Serial.println("try MODE1"); mraa_spi_frequency(SPI, 1000000); mraa_spi_lsbmode(SPI, false); mraa_spi_mode(SPI, MRAA_SPI_MODE1); m_spiMode = MRAA_SPI_MODE1; if (getVersion() != 0x811) { return false; } } writeRegister8(STMPE_SYS_CTRL1, STMPE_SYS_CTRL1_RESET); delay(10); for (uint8_t i=0; i<65; i++) { readRegister8(i); } writeRegister8(STMPE_SYS_CTRL2, 0x0); // turn on clocks! writeRegister8(STMPE_TSC_CTRL, STMPE_TSC_CTRL_XYZ | STMPE_TSC_CTRL_EN); // XYZ and enable! //Serial.println(readRegister8(STMPE_TSC_CTRL), HEX); writeRegister8(STMPE_INT_EN, STMPE_INT_EN_TOUCHDET); writeRegister8(STMPE_ADC_CTRL1, STMPE_ADC_CTRL1_10BIT | (0x6 << 4)); // 96 clocks per conversion writeRegister8(STMPE_ADC_CTRL2, STMPE_ADC_CTRL2_6_5MHZ); writeRegister8(STMPE_TSC_CFG, STMPE_TSC_CFG_4SAMPLE | STMPE_TSC_CFG_DELAY_1MS | STMPE_TSC_CFG_SETTLE_5MS); writeRegister8(STMPE_TSC_FRACTION_Z, 0x6); writeRegister8(STMPE_FIFO_TH, 1); writeRegister8(STMPE_FIFO_STA, STMPE_FIFO_STA_RESET); writeRegister8(STMPE_FIFO_STA, 0); // unreset writeRegister8(STMPE_TSC_I_DRIVE, STMPE_TSC_I_DRIVE_50MA); writeRegister8(STMPE_INT_STA, 0xFF); // reset all ints writeRegister8(STMPE_INT_CTRL, STMPE_INT_CTRL_POL_HIGH | STMPE_INT_CTRL_ENABLE); return true; }
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); }
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] }
mraa_spi_context mraa_spi_init_raw(unsigned int bus, unsigned int cs) { mraa_spi_context dev = mraa_spi_init_internal(plat == NULL ? NULL : plat->adv_func); if (dev == NULL) { syslog(LOG_CRIT, "spi: Failed to allocate memory for context"); return NULL; } char path[MAX_SIZE]; sprintf(path, "/dev/spidev%u.%u", bus, cs); dev->devfd = open(path, O_RDWR); if (dev->devfd < 0) { syslog(LOG_ERR, "spi: Failed opening SPI Device. bus:%s", path); free(dev); return NULL; } int speed = 0; if (ioctl(dev->devfd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) != -1) { dev->clock = speed; } else { // We had this on Galileo Gen1, so let it be a fallback value dev->clock = 4000000; syslog(LOG_WARNING, "spi: Max speed query failed, setting %d", dev->clock); } if (mraa_spi_mode(dev, MRAA_SPI_MODE0) != MRAA_SUCCESS) { free(dev); return NULL; } if (mraa_spi_lsbmode(dev, 0) != MRAA_SUCCESS) { free(dev); return NULL; } if (mraa_spi_bit_per_word(dev, 8) != MRAA_SUCCESS) { free(dev); return NULL; } return dev; }
mraa_spi_context mraa_spi_init_raw(unsigned int bus, unsigned int cs) { mraa_spi_context dev = mraa_spi_init_internal(plat == NULL ? NULL : plat->adv_func); if (dev == NULL) { syslog(LOG_CRIT, "spi: Failed to allocate memory for context"); return NULL; } char path[MAX_SIZE]; sprintf(path, "/dev/spidev%u.%u", bus, cs); dev->devfd = open(path, O_RDWR); if (dev->devfd < 0) { syslog(LOG_ERR, "spi: Failed opening SPI Device. bus:%s", path); free(dev); return NULL; } int speed = 0; if ((ioctl(dev->devfd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) != -1) && (speed < 4000000)) { dev->clock = speed; } else { dev->clock = 4000000; } if (mraa_spi_mode(dev, MRAA_SPI_MODE0) != MRAA_SUCCESS) { free(dev); return NULL; } if (mraa_spi_lsbmode(dev, 0) != MRAA_SUCCESS) { free(dev); return NULL; } if (mraa_spi_bit_per_word(dev, 8) != MRAA_SUCCESS) { free(dev); return NULL; } return dev; }
mraa_spi_context mraa_spi_init_raw(unsigned int bus, unsigned int cs) { mraa_result_t status = MRAA_SUCCESS; mraa_spi_context dev = mraa_spi_init_internal(plat == NULL ? NULL : plat->adv_func); if (dev == NULL) { syslog(LOG_CRIT, "spi: Failed to allocate memory for context"); status = MRAA_ERROR_NO_RESOURCES; goto init_raw_cleanup; } if (IS_FUNC_DEFINED(dev, spi_init_raw_replace)) { status = dev->advance_func->spi_init_raw_replace(dev, bus, cs); if (status == MRAA_SUCCESS) { return dev; } else { goto init_raw_cleanup; } } char path[MAX_SIZE]; snprintf(path, MAX_SIZE, "/dev/spidev%u.%u", bus, cs); dev->devfd = open(path, O_RDWR); if (dev->devfd < 0) { syslog(LOG_ERR, "spi: Failed opening SPI Device. bus:%s. Error %d %s", path, errno, strerror(errno)); status = MRAA_ERROR_INVALID_RESOURCE; goto init_raw_cleanup; } int speed = 0; if (ioctl(dev->devfd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) != -1) { dev->clock = speed; } else { // We had this on Galileo Gen1, so let it be a fallback value dev->clock = 4000000; syslog(LOG_WARNING, "spi: Max speed query failed, setting %d", dev->clock); } status = mraa_spi_mode(dev, MRAA_SPI_MODE0); if (status != MRAA_SUCCESS) { goto init_raw_cleanup; } status = mraa_spi_lsbmode(dev, 0); if (status != MRAA_SUCCESS) { goto init_raw_cleanup; } status = mraa_spi_bit_per_word(dev, 8); if (status != MRAA_SUCCESS) { goto init_raw_cleanup; } init_raw_cleanup: if (status != MRAA_SUCCESS) { if (dev != NULL) { free(dev); } return NULL; } return dev; }
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; }
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) { printf ("[ERROR] SPI failed to initilize\n"); } mraa_spi_frequency (a_pins->m_spi, 2000000); mraa_spi_mode (a_pins->m_spi, MRAA_SPI_MODE0); mraa_spi_lsbmode (a_pins->m_spi, 1); /* 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) { printf ("[ERROR] GPIO failed to initilize \n"); } a_pins->m_req_ctx = mraa_gpio_init (a_pins->reqn_pin); if (a_pins->m_req_ctx == NULL) { printf ("[ERROR] GPIO failed to initilize \n"); } a_pins->m_rst_ctx = mraa_gpio_init (a_pins->reset_pin); if (a_pins->m_rst_ctx == NULL) { printf ("[ERROR] GPIO failed to initilize \n"); } 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); } }
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; }
/** * Change the SPI lsb mode * * @param lsb Use least significant bit transmission - 0 for msbi * @return Result of operation */ Result lsbmode(bool lsb) { return (Result) mraa_spi_lsbmode(m_spi, (mraa_boolean_t) lsb); }