/*---------------------------------------------------------------------------*/ static const char * program_page(unsigned long offset, const unsigned char *p, int nbytes) { const unsigned char *end = p + nbytes; int s; wait_ready(); write_enable(); s = splhigh(); SPI_FLASH_ENABLE(); spi_tx(SPI_FLASH_INS_PP); spi_tx(offset >> 16); /* MSB */ spi_tx(offset >> 8); spi_tx(offset >> 0); /* LSB */ for(; p < end; p++) { spi_tx(~*p); } SPI_FLASH_DISABLE(); splx(s); return p; }
/*---------------------------------------------------------------------------*/ int xmem_pread(void *_p, int size, unsigned long offset) { unsigned char *p = _p; const unsigned char *end = p + size; int s; wait_ready(); ENERGEST_ON(ENERGEST_TYPE_FLASH_READ); s = splhigh(); SPI_FLASH_ENABLE(); SPI_WRITE_FAST(SPI_FLASH_INS_READ); SPI_WRITE_FAST(offset >> 16); /* MSB */ SPI_WRITE_FAST(offset >> 8); SPI_WRITE_FAST(offset >> 0); /* LSB */ SPI_WAITFORTx_ENDED(); SPI_FLUSH(); for(; p < end; p++) { unsigned char u; SPI_READ(u); *p = ~u; } SPI_FLASH_DISABLE(); splx(s); ENERGEST_OFF(ENERGEST_TYPE_FLASH_READ); return size; }
/*---------------------------------------------------------------------------*/ int xmem_pread(void *_p, int size, unsigned long offset) { unsigned char *p = _p; const unsigned char *end = p + size; int s; wait_ready(); ENERGEST_ON(ENERGEST_TYPE_FLASH_READ); s = splhigh(); SPI_FLASH_ENABLE(); spi_tx(SPI_FLASH_INS_READ); spi_tx(offset >> 16); /* MSB */ spi_tx(offset >> 8); spi_tx(offset >> 0); /* LSB */ FASTSPI_CLEAR_RX(); for(; p < end; p++) { unsigned char u; FASTSPI_RX(u); *p = ~u; } SPI_FLASH_DISABLE(); splx(s); ENERGEST_OFF(ENERGEST_TYPE_FLASH_READ); return size; }
/* * Initialize external flash *and* SPI bus! */ void xmem_init(void) { spi_init(); P4DIR |= BV(FLASH_CS) | BV(FLASH_HOLD) | BV(FLASH_PWR); P4OUT |= BV(FLASH_PWR); /* P4.3 Output, turn on power! */ SPI_FLASH_DISABLE(); /* Unselect flash. */ SPI_FLASH_UNHOLD(); }
/* * Initialize external flash *and* SPI bus! */ void xmem_init(void) { spi_init(); P4DIR |= BV(FLASH_CS); // Unnecessary for Zolertia Z1 | BV(FLASH_PWR); P5DIR |= BV(FLASH_HOLD); // In P5 for Z1 SPI_FLASH_DISABLE(); /* Unselect flash. */ SPI_FLASH_UNHOLD(); }
/*---------------------------------------------------------------------------*/ static void write_enable(void) { int s; s = splhigh(); SPI_FLASH_ENABLE(); spi_tx(SPI_FLASH_INS_WREN); SPI_FLASH_DISABLE(); splx(s); }
/*---------------------------------------------------------------------------*/ static void write_enable(void) { int s; s = splhigh(); SPI_FLASH_ENABLE(); FASTSPI_TX(SPI_FLASH_INS_WREN); SPI_WAITFORTx_ENDED(); SPI_FLASH_DISABLE(); splx(s); }
/* * Erase 64k bytes of data. It takes about 1s before WIP goes low! */ static void erase_sector(unsigned long offset) { int s; wait_ready(); write_enable(); s = splhigh(); SPI_FLASH_ENABLE(); spi_tx(SPI_FLASH_INS_SE); spi_tx(offset >> 16); /* MSB */ spi_tx(offset >> 8); spi_tx(offset >> 0); /* LSB */ SPI_FLASH_DISABLE(); splx(s); }
/* * Initialize external flash *and* SPI bus! */ void xmem_init(void) { int s; spi_init(); P4DIR |= BV(FLASH_CS) | BV(FLASH_HOLD) | BV(FLASH_PWR); P4OUT |= BV(FLASH_PWR); /* P4.3 Output, turn on power! */ /* Release from Deep Power-down */ s = splhigh(); SPI_FLASH_ENABLE(); SPI_WRITE_FAST(SPI_FLASH_INS_RES); SPI_WAITFORTx_ENDED(); SPI_FLASH_DISABLE(); /* Unselect flash. */ splx(s); SPI_FLASH_UNHOLD(); }
/*---------------------------------------------------------------------------*/ static unsigned read_status_register(void) { unsigned char u; int s; s = splhigh(); SPI_FLASH_ENABLE(); spi_tx(SPI_FLASH_INS_RDSR); FASTSPI_CLEAR_RX(); FASTSPI_RX(u); SPI_FLASH_DISABLE(); splx(s); return u; }
/*---------------------------------------------------------------------------*/ static unsigned read_status_register(void) { unsigned char u; int s; s = splhigh(); SPI_FLASH_ENABLE(); SPI_WRITE(SPI_FLASH_INS_RDSR); SPI_FLUSH(); SPI_READ(u); SPI_FLASH_DISABLE(); splx(s); return u; }
/* * Erase 64k bytes of data. It takes about 1s before WIP goes low! */ static void erase_sector(unsigned long offset) { int s; wait_ready(); write_enable(); s = splhigh(); SPI_FLASH_ENABLE(); FASTSPI_TX(SPI_FLASH_INS_SE); FASTSPI_TX(offset >> 16); /* MSB */ FASTSPI_TX(offset >> 8); FASTSPI_TX(offset >> 0); /* LSB */ SPI_WAITFORTx_ENDED(); SPI_FLASH_DISABLE(); splx(s); }