int flash_vs_erase(loff_t offs, uint32_t len) { uint8_t tx[8] = { FLASH_CMD_WESR, FLASH_CMD_WRSR, 0, FLASH_CMD_ERAS, FLASH_CMD_WREN, 0, 0, 0}; struct spi_transfer td[] = { {NULL, &tx[1], 2 }, {NULL, &tx[3], 1 }, {NULL, &tx[0], 1 }, }; /* clear sr */ // printf("clear sr\n"); flash_wren(); spi_exchange(&td[0], 1); flash_poll(FLASH_MOD_POLLREDY); /* clear sr: alt */ // printf("clear sr: alt\n"); spi_exchange(&td[2], 1); spi_exchange(&td[0], 1); flash_poll(FLASH_MOD_POLLREDY); /* erase */ flash_wren(); // printf("erase\n"); spi_exchange(&td[1], 1); flash_poll(FLASH_MOD_POLLREDY); return 0; }
int flash_vs_read(uint8_t *buf, loff_t offs, int len, int extra) { uint8_t tx[8] = {0, 0, 0, FLASH_CMD_FARD }; int remain = len, *d = (int *)&tx[4]; struct spi_transfer td[] = { {NULL, &tx[3], 5 }, {buf, NULL, 0 } }, *p; // printf("spi: r (b=%p, o=0x%llx, l=0x%x)\n", buf, offs, len); for(p = &td[1]; remain;) { *d = offs; *d = __swab32(*d); *d >>= 8; p->len = (remain < FLASH_READ_BLOCK)? remain: FLASH_READ_BLOCK; spi_exchange(td, ARRAY_SIZE(td)); remain -= p->len; offs += p->len; p->in += p->len; } return len; }
int flash_vs_write(uint8_t *buf, loff_t offs, int len, int extra) { uint8_t tx[8] = { FLASH_CMD_WREN, 0, 0, FLASH_CMD_WRIT }; int remain = len, *d = (int *)&tx[4]; struct spi_transfer td[] = { {NULL, &tx[4], 4 }, {NULL, buf, 0 }, }, *p; // printf("spi: w (b=%p, o=0x%llx, l=0x%x)\n", buf, offs, len); for(p = &td[1]; remain;) { *d = offs; *d = __swab32(*d); *d &= ~0xff; *d |= FLASH_CMD_WRIT; p->len = (remain < spibytes) ? remain: spibytes; flash_wren(); spi_exchange(td, ARRAY_SIZE(td)); flash_poll(FLASH_MOD_POLLREDY); remain -= p->len; offs += p->len; p->out += p->len; } return len; }
//////////////////////////////////////////////////////////// // Write command to the MCP2515 //////////////////////////////////////////////////////////// void can_write_cmd(char cmd){ spi_init_buffer(); spi_load_byte(cmd); spi_exchange(); spi_wait_for_completion(); }
void flash_wren(void) { uint8_t tx[8] = { FLASH_CMD_WREN, FLASH_CMD_WRDI }; struct spi_transfer td[] = { {NULL, &tx[0], 1 }, {NULL, &tx[1], 1} }; // spi_exchange(&td[1], 1); spi_exchange(&td[0], 1); flash_poll(FLASH_MOD_POLLWREN); }
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; }
//////////////////////////////////////////////////////////// // Write byte <data> to register with address <reg> //////////////////////////////////////////////////////////// void can_write_reg(char address, char data){ spi_init_buffer(); spi_load_byte(c2515Write); spi_load_byte(address); spi_load_byte(data); spi_exchange(); spi_wait_for_completion(); }
//////////////////////////////////////////////////////////// // Software reset in case the reset pin is not available //////////////////////////////////////////////////////////// void can_reset(){ // Reset the SPI buffer spi_init_buffer(); // Load and TX command spi_load_byte(c2515Reset); spi_exchange(); spi_wait_for_completion(); return; }
void flash_poll(int mod) { uint8_t buf[8] = { FLASH_CMD_RDSR, 0 }; struct spi_transfer td[] = { {NULL, &buf[0], 1}, {&buf[1], NULL, 1} }; do { spi_exchange(td, ARRAY_SIZE(td)); // printf("stat: 0x%02x, 0x%02x, 0x%02x\n", buf[0], buf[1], buf[2]); } while ((mod)? !(buf[1] & (1 << 1)): !!(buf[1] & 1)); }
//////////////////////////////////////////////////////////// // Write <data> in <address> using <mask> //////////////////////////////////////////////////////////// void can_write_bits(char address, char data, char mask){ spi_init_buffer(); spi_load_byte(c2515BitMod); spi_load_byte(address); spi_load_byte(mask); spi_load_byte(data); spi_exchange(); spi_wait_for_completion(); }
void can_read_status(){ // Reset the SPI buffer spi_init_buffer(); // Load and TX command spi_load_byte(c2515Status); spi_load_byte(0x00); spi_exchange(); spi_wait_for_completion(); serial_printf("Status = "); serial_print_hex(spi_get_byte(1)); return; }
//////////////////////////////////////////////////////////// // Read the contents of the register at <address> //////////////////////////////////////////////////////////// char can_read_reg(char address){ spi_init_buffer(); // Read command spi_load_byte(c2515Read); // Register address spi_load_byte(address); // Expect one byte answer spi_load_zeros(0x01); // Start the transfer spi_exchange(); spi_wait_for_completion(); return spi_get_byte(2); }
//////////////////////////////////////////////////////////// // Read several bytes from the other device into the // data buffer. Used when you're talking through a // one-wire SPI interface (e.g. for nRF2401). // ! The data output pin should be disabled when this // ! routine is called. //////////////////////////////////////////////////////////// void spi_read(char nr_of_bytes){ // Buffer init spi_init_buffer(); // Set the number of bytes we want to read spi_load_zeros(nr_of_bytes); // Disable the output pin //spi_do_tris = 1; // Do the exchange spi_exchange(); // Wait for completion spi_wait_for_completion(); // Enable the output pin again //spi_do_tris = 0; return; }
static uint16_t spi_send(FAR struct spi_dev_s *dev, uint16_t wd) { uint16_t buf = wd; spi_exchange(dev, &buf, &buf, 1); return buf; }