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; }
/** * Set bits per mode on transaction, default is 8 * * @param bits bits per word * @return Result of operation */ Result bitPerWord(unsigned int bits) { return (Result) mraa_spi_bit_per_word(m_spi, bits); }