LedMatrix::LedMatrix(int m) { int i; moduleNum = m; _width = 32*moduleNum; //cout << "Modules: " << int(moduleNum) << ", width " << int(_width) << endl; _height = 8; modules = new LedModule[m]; fontWidth = 8; if (wiringPiSPISetup(0, 256000) <0) cout << "SPI Setup Failed for channel 0: " << strerror(errno) << endl; if (wiringPiSPISetup(1, 256000) <0) cout << "SPI Setup Failed for channel 1: " << strerror(errno) << endl; if (wiringPiSetup() == -1) exit(1); pinMode(0, OUTPUT); pinMode(1, OUTPUT); pinMode(2, OUTPUT); pinMode(3, OUTPUT); //cout << "Initializing " << int(m) << " modules" << endl; for (i=0; i < m; i++) { modules[i].setChip(i); modules[i].init(); } //cout << "Done initializing " << int(m) << " modules" << endl; }
/* * Function: ioBoardSpiTest * Description: be used to test SPI interface on the board * * host slave * MISO < - > MISO * MOSI < - > MOSI * CLK < - > CLK * CE0/1 < - > CS */ void ioBoardSpiTest() { int fd; unsigned char wData = 0x65; //write value unsigned char retv; #if 0 system("gpio load spi"); //load spi driver fd = wiringPiSPISetup(0, 5000000); //channel:0 5M #else fd = wiringPiSPISetup(0, 500000); //channel:0 500k #endif if(fd < 0) { printf("Open the SPI device failed!\n"); return; } at45BufWrite(wData); delay(100); //delay 100ms retv = at45BufRead(); printf("SPI: read data: 0x%02x\n", retv); if(wData == retv) { printf("SPI: the spi interface is OK!\n"); } else { printf("SPI: the spi interface is not OK! Please check out!\n"); } close(fd); }
static ERL_NIF_TERM setup_2(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { unsigned int channel; unsigned long speed; int error; if (argc != 2 || !enif_is_number(env, argv[0]) || !enif_is_number(env, argv[1])) { return enif_make_badarg(env); } if (!enif_get_uint(env, argv[0], &channel)) { return enif_make_badarg(env); } if (!enif_get_ulong(env, argv[1], &speed)) { return enif_make_badarg(env); } if (speed < 500000 || speed > 32000000) { return enif_make_badarg(env); } switch (channel) { case 0: if (state0.fd != 0) { return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_string(env, "channel already opened", ERL_NIF_LATIN1)); } else { state0.env = env; state0.fd = wiringPiSPISetup(channel, speed); if (state0.fd == 0) { error = errno; return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_int(env, error)); } else { return enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_int(env, channel)); } } break; case 1: if (state1.fd != 0) { return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_string(env, "channel already opened", ERL_NIF_LATIN1)); } else { state1.env = env; state1.fd = wiringPiSPISetup(channel, speed); if (state1.fd == 0) { error = errno; return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_int(env, error)); } else { return enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_int(env, channel)); } } break; default: return enif_make_badarg(env); } }
int main(int argc, char *argv[]){ if( argc > 1 ){ sscanf(argv[1], "%i", &frame_time); } signal(SIGINT, sigintHandler); wiringPiSetupGpio(); pinMode(PIN_CS, OUTPUT); pinMode(PIN_RST, OUTPUT); pinMode(PIN_A, OUTPUT); //digitalWrite(PIN_CS, LOW); memset(buf, 0, BUF_SIZE); if(wiringPiSPISetup(0, SPI_SPEED_HZ)==-1){ printf("Could not initialize SPI\n"); return; }; lcd_reset(); lcd_command((unsigned char[8]){ 0xA2, 0xA0, 0xC8, 0xA4, 0xA6, 0x2F, 0x60, 0x27 },8);
extern int EX_setup() { int result = OK; int fd = 0; LG_logEntry(__func__, NULL); /* Set data directions on a number of pins. * * SS - Slave select is output for PI * REQEXC - Request exchange is output for PI * ACKEXC - Acknowledge exchange request is input for PI. */ pinMode(SS, OUTPUT); digitalWrite(SS, HIGH); pinMode(REQEXC, OUTPUT); digitalWrite(REQEXC, LOW); pinMode(ACKEXC, INPUT); /* Setup SPI channel and speed */ fd = wiringPiSPISetup(SPICHANNEL, SPISPEED); result = (fd == -1) ? RV_EXCHANGE_SETUP_FAILED : OK; LG_logExit(__func__, result, NULL); return result; }
void init(){ int byte; unsigned char command[] = { 0xAE, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0xA1, 0xC8, 0xA6, 0xD5, 0x80, 0xDA, 0x12, 0x81, 0x00, 0xB0, 0xA4, 0xDB, 0x40, 0x20, 0x00, 0x00, 0x10, 0x8D, 0x14, 0x2E, 0xA6, 0xAF }; for(byte=0;byte<1024;byte++){ frame[byte] = 0x00; } wiringPiSetup(); pinMode (5, OUTPUT) ; pinMode (6, OUTPUT) ; wiringPiSPISetup(0, 32*1000*1000); digitalWrite(6, LOW) ; delay(50); digitalWrite(6, HIGH) ; digitalWrite(5, LOW); wiringPiSPIDataRW(0, command, 28); }
int main (int argc, char** argv) { int status = wiringPiSetupGpio(); if (status < 0) { printf ("Fail to setup wiringPi. Are you root?"); exit(1); } pinMode (24,INPUT); wiringPiISR(24, INT_EDGE_FALLING, irq_handler); status = wiringPiSPISetup(0,500000); if (status < 0) { printf("Failed to configure SPI"); exit (1); } unsigned char buf [256]; pause(); while (1) { buf[0] = 0x07; buf[1] = 0xff; wiringPiSPIDataRW(0, buf, 2); printf ("Output %d \n",buf[1]); delay (1000); } }
int main (void) { uint8_t spiData [10] ; // set interface speed if (wiringPiSPISetup (0,100000) < 0) { fprintf (stderr, "Unable to open SPI device 0: %s\n", strerror (errno)) ; exit (1) ; } wiringPiSetupSys() ; wiringPiSetup() ; /* setup output ports */ pinMode(8, OUTPUT); /* reset */ pinMode(9, OUTPUT); /* rs */ /* reset LCD display */ digitalWrite(8,0); delayMicroseconds (100) ; digitalWrite(8,1); void write_cmd(int c) { digitalWrite(9,0); // set rs bit for cmd spiData[0]=c; wiringPiSPIDataRW (0, spiData, 1) ; }
MCP2510::MCP2510(uint8_t csPin):_csPin(csPin){ // system("gpio load spi"); wiringPiSetup(); wiringPiSPISetup(_csPin,SPI_CLOCK_SPEED); }
void setup (void) { int status = wiringPiSetupGpio(); if(status < 0) { perror ("Failed to configure GPIO"); exit (1); } pinMode (IRQ_PIN, INPUT); pinMode (CE_PIN, OUTPUT); status = wiringPiSPISetup (CHANNEL,SPEED); if (status < 0) { perror ("Failed to configure SPI"); exit(1); } configure_address(); //TODO dynamic length nrf24l01_set_dynamic_payload_length(0, ENABLE); configure_isr(); nrf24l01_set_rx_payload_length(1,32); //nrf24l01_set_en_aa(1,ENABLE); nrf24l01_set_dynamic_payload_length(1, ENABLE); nrf24l01_power_up(ENABLE); nrf24l01_mode(MODE_RX); printf ("Configured\n"); }
bool ofApp::setupSensors() { if (wiringPiSPISetup (0, 1000000) < 0) return false; else return true; }
int main(int argc, char **argv) { if(wiringPiSetup() == -1) { printf("wiringPiSetup() failed\n"); return EXIT_FAILURE; } // 540kHz speed - recomended by ST7920 spec //if ((lcd_fd = wiringPiSPISetupMode(0, 540000, 0x07)) < 0) { if ((lcd_fd = wiringPiSPISetup(0, 540000)) < 0) { printf("Can't open the SPI bus\n"); return EXIT_FAILURE; } char mode = 0x07; ioctl(lcd_fd, SPI_IOC_WR_MODE, &mode); init_gpio(); reset_lcd(); init_lcd(); set_extended_mode(0, 0, 0); set_extended_mode(0, 1, 1); clear_lcd(); show_image(raspberry_pix); return EXIT_SUCCESS; }
void main(void){ struct tm *tm_ptr; time_t the_time; (void) time(&the_time); tm_ptr = gmtime(&the_time); printf("The Raw time is %ld\n", the_time); /* fast timeb function for milliseconds */ struct timeb tmb; struct timeb starttime; struct timeb stoptime; unsigned char buffer[4096]; int i; for(i=0;i<4096;i++){ buffer[i]=0xAA; } /* buffer[0]=0; buffer[1]=1; buffer[2]=2; buffer[3]=3; buffer[4]=4; buffer[5]=0xAA; buffer[6]=0x55; */ int channel=0; if (wiringPiSPISetup (channel, 500000) < 0) fprintf (stderr, "SPI Setup failed!\n"); printf("\n\nEntering SPI Comms:\n"); ftime(&starttime); int j=0; for(j=0; j<1; j++){ wiringPiSPIDataRW ( channel, &buffer, 4096) ; } ftime(&stoptime); printf("seconds difference: %ld.%ld\n", stoptime.time-starttime.time); printf("millis difference: %ld\n" , stoptime.millitm-starttime.millitm); // Show each character as recieved for(i=0;i<4096;i++){ printf("%d,0x%X\n", i, buffer[i]); } }
/** * \brief Receive data from an EEPROM chip * * This receives data from an EEPROM using SPI BUS protocol * \param[out] sg_serial Sansgrid data received * \param[out] size Size of data received * \param[in] ts Stub configuration to use */ int eepromReceive(SansgridSerial **sg_serial, uint32_t *size, TalkStub *ts) { // Read from an EEPROM chip over SPI int fd; uint8_t buffer[sizeof(SansgridSerial)]; uint8_t newbuffer[sizeof(SansgridSerial)+3]; int i; mark_point(); if (!ts->valid_read) return 1; mark_point(); if (!eeprom_lock_initd) { pthread_mutex_init(&eeprom_lock, NULL); eeprom_lock_initd = 1; } mark_point(); mark_point(); *size = sizeof(SansgridSerial); mark_point(); sem_wait(&ts->writelock); if (ts->eeprom_address == -1) { return -1; } mark_point(); pthread_mutex_lock(&eeprom_lock); // Set up reading from EEPROM mark_point(); if ((fd = wiringPiSPISetup (0, MHZ(SPI_SPEED_MHZ))) < 0) fprintf(stderr, "SPI Setup failed: %s\n", strerror (errno)); // Prepend command and address to buffer newbuffer[0] = READ; newbuffer[1] = ts->eeprom_address >> 8; newbuffer[2] = ts->eeprom_address & 0xff; // Send command/address, read data wiringPiSPIDataRW(0, newbuffer, (*size)+3); // shift the command and address out of buffer for (i=0; i<*size; i++) buffer[i] = newbuffer[i+3]; *sg_serial = (SansgridSerial*)malloc(sizeof(SansgridSerial)); memcpy(*sg_serial, buffer, sizeof(SansgridSerial)); // finish up close(fd); pthread_mutex_unlock(&eeprom_lock); sem_post(&ts->readlock); return 0; }
void spiSetup (int spiChannel) { if ((myFd = wiringPiSPISetup (spiChannel, 1000000)) < 0) { fprintf (stderr, "Can't open the SPI bus: %s\n", strerror (errno)) ; exit (EXIT_FAILURE) ; } }
void main(int argc, char** argv) { if (wiringPiSPISetup(CHANNEL, 4000000) < 0) { fprintf (stderr, "SPI Setup failed: %s\n", strerror (errno)); exit(errno); } printf("sample=%04x\n", sample(0)); }
void spiSetup (int speed) { if ((myFd = wiringPiSPISetup (SPI_CHAN, speed)) < 0) { fprintf (stderr, "Can't open the SPI bus: %s\n", strerror (errno)) ; exit (EXIT_FAILURE) ; } }
static int remote_monitoring_init(void) { int result; Lock_fd = open_lockfile(LOCKFILE); if (setuid(getuid()) < 0) { perror("Dropping privileges failed. (did you use sudo?)n"); result = EXIT_FAILURE; } else { result = wiringPiSetup(); if (result != 0) { perror("Wiring Pi setup failed."); } else { result = wiringPiSPISetup(Spi_channel, Spi_clock); if (result < 0) { printf("Can't setup SPI, error %i calling wiringPiSPISetup(%i, %i) %sn", result, Spi_channel, Spi_clock, strerror(result)); } else { int sensorResult = bme280_init(Spi_channel); if (sensorResult != 1) { printf("It appears that no BMP280 module on Chip Enable %i is attached. Aborting.\n", Spi_channel); result = 1; } else { // Read the Temp & Pressure module. float tempC = -300.0; float pressurePa = -300; float humidityPct = -300; sensorResult = bme280_read_sensors(&tempC, &pressurePa, &humidityPct); if (sensorResult == 1) { printf("Temperature = %.1f *C Pressure = %.1f Pa Humidity = %1f %%\n", tempC, pressurePa, humidityPct); result = 0; } else { printf("Unable to read BME280 on pin %i. Aborting.\n", Spi_channel); result = 1; } } } } } return result; }
void* run_spi(void* arg){ dp_exchange_t* p = (dp_exchange_t*)(arg); // Initialize SPI Device int channel = 1; int speed = 500000; int fd = wiringPiSPISetup(channel, speed); if (fd == -1) { printf( "SPI Setup Error: %s\n", strerror(errno)); return NULL; } printf("SPI Device FD:\t%d\n", fd); // Data Exchange Loop while(1) { // Commit Status pthread_mutex_lock(&p->mutex); // Consume Command if (p->command.is_not_empty) { // Debug Output: Say, what we got here printf("\n\nSPI: Write to Pad %d\n", p->padnr); if (p->command.is_rgb) { printf(" Type:\tRGB data\n"); printf(" Data:\trgb(%d,%d,%d)\n", p->command.rgb.r * 8, p->command.rgb.g * 8, p->command.rgb.b * 8); } else { printf(" Type:\tCommand\n"); printf(" Data:\tTODO"); } printf(" Size:\t%d Bytes\n\n", sizeof(p->command.bytes)); // SPI Read and Write unsigned char* data = p->command.bytes; int ret = wiringPiSPIDataRW(channel, data, ARRAY_SIZE(data)); if (ret == -1) { printf( "Error at SpiRW: %s\n", strerror(errno)); } else { printf("Bytes Written:\t%d\n", ret); printf("Resonse Data:\t%d, %d\n", data[0], data[1]); // TODO Status entgegennehmen } // TODO Status Output // Actually consume command by setting everything back to zero p->command.is_not_empty = 0; } pthread_mutex_unlock(&p->mutex); usleep(1000000); } return NULL; }
int main(int argc, char **argv) /////////////////////////////// { unsigned char data, new_data; int count, ok; printf("poweroff version 1.0\n"); usleep(1000000); // let other processes finish int spi = wiringPiSPISetup(1, 9600); if (spi < 0) { printf("Cannot initialize spi driver\n"); return 1; } // printf("spi handle = %d\n", spi); // send 0xFF and receive current status of pi-top-hub count = 0; data = 0xff; printf("Sending: 0x%X\n", data); do { data = 0xff; ok = wiringPiSPIDataRW(1, &data, 1); if (ok) { ok &= analyze(data); } } while ((!ok) && (count++ < MAXCOUNT)); if (ok) { printf("Receiving: 0x%X\n", data); printf("Current brightness = %d\n", brightness); // check whether current brightness s within acceptable range if (brightness > 10) brightness = 10; if (brightness < 3) brightness = 3; // calculate data to send shutdown = 1; screenoffbit = 0; new_data = calculate(); // send new data twice data = new_data; printf("Sending: 0x%X\n", data); wiringPiSPIDataRW(1, &data, 1); data = new_data; wiringPiSPIDataRW(1, &data, 1); } else printf("reading current brightness not successful\n"); return 0; }
/** * \brief Prepare for SPI transfer (API) * * Note that to use this, you must have an int g_fd file descriptor * that this can write to. You must close that file descriptor when * transfer is done. You should use spiOpen(), which is a wrapper * for this function that returns the file descriptor directly. * \returns * This function returns 0 on success, and -1 on failure. \n * A global file descriptor is stored in the int g_fd field. */ int8_t sgSerialOpen(void) { // Set up SPI if ((g_fd = wiringPiSPISetup (0, KHZ(SPI_SPEED_KHZ))) < 0) { syslog(LOG_ERR, "SPI Setup failed: %s\n", strerror (errno)); return -1; } else { return 0; } return 0; }
int spi_openadapter(uint8_t spi_bus) { // spi_buses[spi_bus] = mraa_spi_init (spi_bus); // return spi_bus; int fd; int id = getSPIId(); spi_freq[id] = SPI_DEFAULT_FREQ; spi_channels[id] = spi_bus; fd = wiringPiSPISetup (spi_bus, SPI_DEFAULT_FREQ); spi_buses[id] = fd; return id; }
static ERL_NIF_TERM spi_setup_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { int channel, speed; if (!enif_get_int(env, argv[0], &channel) || !enif_get_int(env, argv[1], &speed)) { return enif_make_badarg(env); } int result = wiringPiSPISetup(channel, speed); return enif_make_int(env, result); }
void SPIListener::Setup() { // Get Events EventSubMap eventsSPI = Daemon::EventList.find("SPI")->second; EventSubMap::iterator it; FromKeyMap events = TIEVoxInfo::Events.find("SPI")->second; int code; for (it = eventsSPI.begin(); it != eventsSPI.end(); ++it) { code = events.find(it->second.EventName)->second; // Add event to list by code Events[code] = it->second; // Setup car bus (16MHz) wiringPiSPISetup(SPI_BUS_CAR, 16000000); // Setup nRF24 bus (16MHz) wiringPiSPISetup(SPI_BUS_RF, 16000000); } }
/** * \brief Send data to an EEPROM chip * * This sends data to an EEPROM using SPI BUS protocol * \param[in] buffer Data to send * \param[in] address EEPROM address to write to * \param[in] size Number of bytes to write * \returns * On error, returns -1. \n * Otherwise returns 0 */ static int eepromSendData(uint8_t *buffer, uint16_t address, int size) { // Set up SPI int i; int fd; // only 32 bytes can be written at a time; see below int bounded_size = (size > 32 ? 32 : size); // prepend the command and address to the data uint8_t newbuffer[bounded_size+3]; struct timespec required, remaining; int excode; if ((fd = wiringPiSPISetup (0, MHZ(SPI_SPEED_MHZ))) < 0) fprintf(stderr, "SPI Setup failed: %s\n", strerror (errno)); // Have to make room for command and address for (i=3; i<bounded_size+3; i++) newbuffer[i] = buffer[i-3]; // Allow writes to the EEPROM // Has to be done before every write cycle newbuffer[0] = WRITE_ENABLE; write(fd, newbuffer, 1); // Write to specified address newbuffer[0] = WRITE; newbuffer[1] = address >> 8; newbuffer[2] = address & 0xff; //printf("Writing to %x\n", address); write(fd, newbuffer, bounded_size+3); close(fd); // Wait for the write to cycle required.tv_sec = 0; required.tv_nsec = 1000L*WRITE_CYCLE; do { if ((excode = nanosleep(&required, &remaining)) == -1) { if (errno == EINTR) required.tv_nsec = remaining.tv_nsec; else return -1; } } while (excode); if (size > 32) { // Only one page (of 32 bytes) can be written // at a time. If more than 32 bytes are being written, // break the line into multiple pages return eepromSendData(&buffer[32], address+0x0020, size-32); } return 0; }
uint8_t u8g_com_raspberrypi_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch(msg) { case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_INIT: // check wiringPi setup if (wiringPiSetup() == -1) { printf("wiringPi-Error\n"); exit(1); } if (wiringPiSPISetup (0, 100000) < 0) { printf ("Unable to open SPI device 0: %s\n", strerror (errno)) ; exit (1) ; } u8g_SetPIOutput(u8g, U8G_PI_RESET); u8g_SetPIOutput(u8g, U8G_PI_A0); break; case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ u8g_SetPILevel(u8g, U8G_PI_A0, arg_val); break; case U8G_COM_MSG_CHIP_SELECT: /* Done by the SPI hardware */ break; case U8G_COM_MSG_RESET: u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val); break; case U8G_COM_MSG_WRITE_BYTE: wiringPiSPIDataRW (0, &arg_val, 1) ; break; case U8G_COM_MSG_WRITE_SEQ: wiringPiSPIDataRW (0, arg_ptr, arg_val); break; case U8G_COM_MSG_WRITE_SEQ_P: wiringPiSPIDataRW (0, arg_ptr, arg_val); break; } return 1; }
void init(){ rd_data.code = RD_CODE; wr_data.code = WR_CODE; pinMode(REQ, OUTPUT);digitalWrite(REQ, LOW); pinMode(WR, OUTPUT);digitalWrite(WR, LOW); pinMode(PER, INPUT); pinMode(INFO, INPUT); pinMode(REQ, OUTPUT);digitalWrite(REQ, LOW); pinMode(WR, OUTPUT);digitalWrite(WR, LOW); // pinMode(CS, OUTPUT);digitalWrite(CS, HIGH); wiringPiSPISetup (CHANNEL, SPI_SPEED); }
int main(int argc, char *argv[]) { int i, delay_ms; uint32_t x1; int print_output; printf ("SPI test program\n") ; // initialize the WiringPi API if (wiringPiSPISetup (0, 1000000) < 0) return -1 ; if (argc>2) print_output = 0; else print_output = 1; // get the channel to read, default to 0 if (argc>1) delay_ms = atoi(argv[1]); else delay_ms = 1; //we don't use this here... int counter_wrap = 100; if (delay_ms != 0) counter_wrap = 500/delay_ms; int counter = 0; // run until killed with Ctrl-C while (1) { counter++; // read data and add to total int chan; for (chan = 0; chan<8; chan++) { x1 = readadc(chan); if (print_output) { if (counter > counter_wrap) { printf("CH %i val= %d ", chan, x1); } } } if (counter > counter_wrap) { if (print_output) printf("\n"); counter = 0; } delay(delay_ms); } return 0 ; }
int main(int argc, char** argv) { if (fd = wiringPiSPISetup (CHANNEL, 1000000) < 0) { fprintf (stderr, "SPI Setup failed: %s\n", strerror (errno)); return 1; } while(1) { fd = wiringPiSPIDataRW(CHANNEL, buff, 1); printf("%i\n", *buff); delay(1000); } return (EXIT_SUCCESS); }
int mcp3004Setup (const int pinBase, int spiChannel) { struct wiringPiNodeStruct *node ; if (wiringPiSPISetup (spiChannel, 1000000) < 0) return -1 ; node = wiringPiNewNode (pinBase, 8) ; node->fd = spiChannel ; node->analogRead = myAnalogRead ; return 0 ; }