void loop() { int ret; if(inloop) { printf("Error: loop() task overrun\n"); inloop = 0; return; } inloop = 1; // ======================================================== // We should not have any SPI devices enabled at this point ret = spi_chip_select_status(); if(ret != 0xff) { printf("Error: loop() entered with spi_cs = %d\n",ret); spi_end(ret); return; } user_loop(); // We should not have any SPI devices enabled at this point ret = spi_chip_select_status(); if(ret != 0xff) { printf("Error: loop() entered with spi_cs = %d\n",ret); spi_end(ret); return; } inloop = 0; }
static void spi_write_reg(uint8_t reg, uint16_t value) { spi_begin(); spi_write(DEV_ID); spi_write(0); spi_write(reg); spi_end(); spi_begin(); spi_write(DEV_ID | RS); spi_write(value >> 8); spi_write(value & 0xff); spi_end(); }
void flash_erase_64kB(int addr) { spi_begin(); spi_xfer(0xd8, 8); spi_xfer(addr, 24); spi_end(); }
void Adafruit_STMPE610::writeRegister8(uint8_t reg, uint8_t val) { spi_begin(); CSLow(); spiOut(reg); spiOut(val); CSHigh(); spi_end(); }
/*! \fn void mpu60x0_get_reading(int device, struct mpu60x0_stateType mpu60x0_state, struct reading_memory_type *reading) * \brief Obtain a set of readings from the device * * A set of readings is a new set of data for each of accelerometers, gyrometers, temperature sensing and timestamps. * @param[in] device The spi device to enable interrupts on * @param[in] mpu60x0_state a pointer to a state of the sensor which is used to determine the conversion ratios * @param[out] reading a pointer to a sensor reading structure this function will update */ void mpu60x0_get_reading(int device, struct mpu60x0_stateType mpu60x0_state, struct reading_memory_type *reading) { uint8_t byte_H; uint8_t byte_L; int16_t conv; double conv_double; reading->timestamp = systimer_get_us_32bit(); // We start a SPI multibyte read of sensors spi_begin(device); spi_transfer(INV_MPU60x0_REG_RAW_ACCEL|0x80); // Read AccelX byte_H = spi_transfer(0); byte_L = spi_transfer(0); conv = ((int16_t)byte_H<<8)| byte_L; conv_double = conv; reading->a_x = conv_double/inv_mpu60x0_accl_conv_ratio[mpu60x0_state.accel_rate]; //FIXME: // Read AccelY byte_H = spi_transfer(0); byte_L = spi_transfer(0); conv = ((int16_t)byte_H<<8)| byte_L; conv_double = conv; reading->a_y = conv_double/inv_mpu60x0_accl_conv_ratio[mpu60x0_state.accel_rate]; // Read AccelZ byte_H = spi_transfer(0); byte_L = spi_transfer(0); conv = ((int16_t)byte_H<<8)| byte_L; conv_double = conv; reading->a_z = conv_double/inv_mpu60x0_accl_conv_ratio[mpu60x0_state.accel_rate]; // Read Temp byte_H = spi_transfer(0); byte_L = spi_transfer(0); conv = ((int16_t)byte_H<<8)| byte_L; conv_double = conv; reading->temp = conv_double/340.0 + 36.53; // Read GyroX byte_H = spi_transfer(0); byte_L = spi_transfer(0); conv = ((int16_t)byte_H<<8)| byte_L; conv_double = conv; reading->w_x = conv_double/inv_mpu60x0_gyro_conv_ratio[mpu60x0_state.gyro_rate]; //FIXME: // Read GyroY byte_H = spi_transfer(0); byte_L = spi_transfer(0); conv = ((int16_t)byte_H<<8)| byte_L; conv_double = conv; reading->w_y = conv_double/inv_mpu60x0_gyro_conv_ratio[mpu60x0_state.gyro_rate]; //fixme!! // Read GyroZ byte_H = spi_transfer(0); byte_L = spi_transfer(0); conv = ((int16_t)byte_H<<8)| byte_L; conv_double = conv; reading->w_z = conv_double/inv_mpu60x0_gyro_conv_ratio[mpu60x0_state.gyro_rate]; //fixme!! spi_end(); }
/*! \fn int spi_write(int pin, uint8_t reg, uint8_t data) * \brief Write 8 address bits to an spi device then writes 8 data bits to the spi device * @param[in] pin the chipselect pin to toggle * @param[in] reg the address bits to send to the spi device * @param[in] the data to send to the spi device * \return returns ERR_NOERR unless an attempt has been made to perform two simultaneous spi transactions, then an error is returned */ int spi_write(int pin, uint8_t reg, uint8_t data) { int ret = spi_begin(pin); if (ret < 0) return ret; spi_transfer(reg); spi_transfer(data); return spi_end(); }
/*! \fn int spi_read(int pin, uint8_t reg, uint8_t *data) * \brief Write 8 address bits to an spi device then read 8 bits from spi device * @param[in] pin the chipselect pin to toggle * @param[in] reg the address bits to send to the spi device * @param[out] the data the spi device sent back * \return returns ERR_NOERR unless an attempt has been made to perform two simultaneous spi transactions, then an error is returned */ int spi_read(int pin, uint8_t reg, uint8_t *data) { int ret = spi_begin(pin); if (ret < 0) return ret; spi_transfer(reg|0x80); *data = spi_transfer(0); return spi_end(); }
void flash_write(int addr, char *data, int n) { spi_begin(); spi_xfer(0x02, 8); spi_xfer(addr, 24); while (n--) spi_xfer(*(data++), 8); spi_end(); }
void fill(byte color) { int b, i; for (b=0; b < numboards; b++) { if (b) delay(INTER_BOARD_DELAY); spi_start(); for (i = 0; i < 64; i++) spi_put(color); spi_end(); } }
void drv8301_write_reg(int reg, int data) { uint16_t out = 0; out |= (reg & 0x0F) << 11; out |= data & 0x7FF; spi_begin(); spi_exchange(out); spi_end(); }
unsigned int drv8301_read_reg(int reg) { uint16_t out = 0; out |= (1 << 15); out |= (reg & 0x0F) << 11; out |= 0x807F; if (reg != 0) { spi_begin(); spi_exchange(out); spi_end(); } spi_begin(); uint16_t res = spi_exchange(0xFFFF); spi_end(); return res; }
static void atben_reg_write(void *handle, uint8_t reg, uint8_t v) { struct atben_dsc *dsc = handle; spi_begin(dsc); spi_send(dsc, AT86RF230_REG_WRITE | reg); spi_send(dsc, v); spi_end(dsc); }
void flash_read(int addr, char *data, int n) { spi_begin(); spi_xfer(0x03, 8); spi_xfer(addr, 24); while (n--) *(data++) = spi_xfer(0, 8); spi_end(); }
/** * Timer interrupt */ void encoder_tim_isr(void) { uint16_t pos; spi_begin(); spi_transfer(&pos, 0, 1); spi_end(); pos &= 0x3FFF; last_enc_angle = ((float)pos * 360.0) / 16384.0; }
static void atben_sram_write(void *handle, uint8_t addr, uint8_t v) { struct atben_dsc *dsc = handle; spi_begin(dsc); spi_send(dsc, AT86RF230_SRAM_WRITE); spi_send(dsc, addr); spi_send(dsc, v); spi_end(dsc); }
static uint8_t atben_reg_read(void *handle, uint8_t reg) { struct atben_dsc *dsc = handle; uint8_t res; spi_begin(dsc); spi_send(dsc, AT86RF230_REG_READ | reg); res = spi_recv(dsc); spi_end(dsc); return res; }
static void atben_buf_write(void *handle, const void *buf, int size) { struct atben_dsc *dsc = handle; spi_begin(dsc); spi_send(dsc, AT86RF230_BUF_WRITE); spi_send(dsc, size); while (size--) spi_send(dsc, *(uint8_t *) buf++); spi_end(dsc); }
void flash_read_id() { int i; spi_begin(); spi_xfer(0x9F, 8); printf("Flash ID:"); for (i = 1; i < 21; i++) printf(" %02X", spi_xfer(0, 8)); printf("\n"); spi_end(); }
uint8_t Adafruit_STMPE610::readRegister8(uint8_t reg) { uint8_t x ; spi_begin(); CSLow(); spiOut(0x80 | reg); spiOut(0x00); x = spiIn(); CSHigh(); spi_end(); return x; }
static uint8_t atben_sram_read(void *handle, uint8_t addr) { struct atben_dsc *dsc = handle; uint8_t res; spi_begin(dsc); spi_send(dsc, AT86RF230_SRAM_READ); spi_send(dsc, addr); res = spi_recv(dsc); spi_end(dsc); return res; }
static void cmd_device(uint8_t bus, uint8_t port) { if (spi_started) spi_end(); if (spi_begin(bus, port) != 0) { fprintf(stderr, "problem accessing device\n"); exit(EXIT_FAILURE); } spi_started = 1; }
/*! \fn void mpu60x0_get_reading_raw(int device, struct reading_memory_type *reading) * \brief Obtain a set of "raw" readings from the device * * The data returned from this function is actually the raw 16bit integer data converted to a double. This makes it easier when dealing with the serial protocol defined in the final report * @param[in] device The spi device to enable interrupts on * @param[out] reading a pointer to a sensor reading structure this function will update */ void mpu60x0_get_reading_raw(int device, struct reading_memory_type *reading) { uint8_t byte_H; uint8_t byte_L; int16_t conv; reading->timestamp = systimer_get_us_32bit(); // We start a SPI multibyte read of sensors spi_begin(device); spi_transfer(INV_MPU60x0_REG_RAW_ACCEL|0x80); // Read AccelX byte_H = spi_transfer(0); byte_L = spi_transfer(0); conv = ((int16_t)byte_H<<8)| byte_L; reading->a_x = (double) conv; // Read AccelY byte_H = spi_transfer(0); byte_L = spi_transfer(0); conv = ((int16_t)byte_H<<8)| byte_L; reading->a_y = (double) conv; // Read AccelZ byte_H = spi_transfer(0); byte_L = spi_transfer(0); conv = ((int16_t)byte_H<<8)| byte_L; reading->a_z = (double) conv; // Read Temp byte_H = spi_transfer(0); byte_L = spi_transfer(0); conv = ((int16_t)byte_H<<8)| byte_L; reading->temp = (double) conv; // Read GyroX byte_H = spi_transfer(0); byte_L = spi_transfer(0); conv = ((int16_t)byte_H<<8)| byte_L; reading->w_x = (double) conv; // Read GyroY byte_H = spi_transfer(0); byte_L = spi_transfer(0); conv = ((int16_t)byte_H<<8)| byte_L; reading->w_y = (double) conv; // Read GyroZ byte_H = spi_transfer(0); byte_L = spi_transfer(0); conv = ((int16_t)byte_H<<8)| byte_L; reading->w_z = (double) conv; spi_end(); }
uint16_t Adafruit_STMPE610::readRegister16(uint8_t reg) { uint16_t x; spi_begin(); CSLow(); spiOut(0x80 | reg); spiOut(0x00); x = spiIn(); x<<=8; x |= spiIn(); CSHigh(); spi_end(); //Serial.print("$"); Serial.print(reg, HEX); //Serial.print(": 0x"); Serial.println(x, HEX); return x; }
static int atben_buf_read(void *handle, void *buf, int size) { struct atben_dsc *dsc = handle; uint8_t len, i; spi_begin(dsc); spi_send(dsc, AT86RF230_BUF_READ); len = spi_recv(dsc); len++; /* LQI */ if (len > size) len = size; for (i = 0; i != len; i++) *(uint8_t *) buf++ = spi_recv(dsc); spi_end(dsc); return len; }
void flash_wait() { while (1) { spi_begin(); spi_xfer(0x05, 8); int status = spi_xfer(0, 8); spi_end(); if ((status & 0x01) == 0) break; // fprintf(stderr, "[wait]"); usleep(1000); } // fprintf(stderr, "[ok]\n"); }
void scrolling(void) { snooze(scrolldelay); uint8_t board_offset; for (board_offset = 0; board_offset < (8*numboards); board_offset = board_offset + 8) { if (board_offset) delay(10); // interframe delay, per data sheet spi_start(); uint8_t r; for (r=0; r < 8; r++) { //produce all the columns of one row int o = ho; // start with the global offset char *txt = t; uint8_t w = getCharWidth(*txt); uint8_t b = getCharData(*txt, r, ho); uint8_t c = 0; while (c < (board_offset + 8)) { if (o <= w) { // any bits left? if (c >= board_offset) { spi_put((b & 0x80) ? fgcolor : bgcolor); } c++; // next column o++; // ... using next bit of this one b = (b << 1); // ...teed up right here } else { // advance to next char if (*txt) ++txt; w = getCharWidth(*txt); b = getCharData(*txt, r, 0); o = 0; } } } spi_end(); } // update offset and char for next frame if (++ho > getCharWidth(*t)) { ho = 0; if (*t) ++t; // on to the next char } if (!(*t)) { snooze(0); // cancel the iso-snooze above set_state(dwell); } }
/// @brief Release SPI bus from TFT display, deassert chip select /// return: void void tft_spi_end() { spi_end(ILI9341_CS); }
/** * @ingroup SPI-DO * */ void bw_spi_relay_end(void) { FUNC_PREFIX(spi_end()); }
int main(int argc, char *argv[]) { uint8_t bus = 0, port = 0; struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"device", required_argument, 0, 'd'}, {"read-flash", required_argument, 0, 'r'}, {"write-flash", required_argument, 0, 'w'}, {"erase-flash", no_argument, 0, 'c'}, {"lock", no_argument, 0, 'x'}, {"fsr", optional_argument, 0, 1}, {"read-ip", required_argument, 0, 2}, {"write-ip", required_argument, 0, 3}, {"erase-all", no_argument, 0, 4}, {0, 0, 0, 0} }; while (1) { int c; c = getopt_long(argc, argv, "hd:r:w:cx", long_options, NULL); if (c == -1) break; switch (c) { case 'd': // device if (sscanf(optarg, "%hhu-%hhu", &bus, &port) != 2) { fprintf(stderr, "invalid USB device\n"); return EXIT_FAILURE; } cmd_device(bus, port); break; case 'r': // read flash if (!spi_started) cmd_device(0, 0); cmd_read_flash(optarg); break; case 'w': // write flash if (!spi_started) cmd_device(0, 0); cmd_write_flash(optarg); break; case 'c': // erase flash if (!spi_started) cmd_device(0, 0); cmd_erase_flash(); break; case 'x': // lock flash if (!spi_started) cmd_device(0, 0); cmd_lock(); break; case 1: // read/write fsr if (!spi_started) cmd_device(0, 0); if (optarg) { char *check = NULL; unsigned long int value; value = strtoul(optarg, &check, 16); if (strlen(check) || value > 0xff) { fprintf(stderr, "invalid FSR value\n"); return EXIT_FAILURE; } cmd_write_fsr(value); } else cmd_read_fsr(); break; case 2: // read info page if (!spi_started) cmd_device(0, 0); cmd_read_ip(optarg); break; case 3: // write info page if (!spi_started) cmd_device(0, 0); cmd_write_ip(optarg); break; case 4: // erase all if (!spi_started) cmd_device(0, 0); cmd_erase_all(); break; default: cmd_show_usage(argv[0]); return EXIT_SUCCESS; } } if (spi_started) spi_end(); return EXIT_SUCCESS; }
/** * @ingroup SPI-LCD * */ void bw_spi_lcd_end(void) { FUNC_PREFIX(spi_end()); }