+static void dataflash_sector_protect(DWORD addr) {
+ wait_for_ready();
+
+ addr &= ~(PAGE_SIZE - 1);
+
+ CS_LOW();
+ xmit_spi(OP_WRITE_ENABLE);
+ CS_HIGH();
+
+ CS_LOW();
+ xmit_spi(OP_SECTOR_PROTECT);
+ xmit_spi((BYTE)(addr >> 16));
+ xmit_spi((BYTE)(addr >> 8));
+ xmit_spi((BYTE) addr );
+ CS_HIGH();
+
+ wait_for_ready();
+}
+
+static void dataflash_sector_unprotect(DWORD addr) {
+ wait_for_ready();
+
+ addr &= ~(PAGE_SIZE - 1);
+
+ CS_LOW();
+ xmit_spi(OP_WRITE_ENABLE);
+ CS_HIGH();
+
+ CS_LOW();
+ xmit_spi(OP_SECTOR_UNPROTECT);
+ xmit_spi((BYTE)(addr >> 16));
+ xmit_spi((BYTE)(addr >> 8));
+ xmit_spi((BYTE) addr );
+ CS_HIGH();
+
+ wait_for_ready();
+}
+
+static DRESULT dataflash_write_4k(const BYTE *buff, DWORD addr) {
+ addr &= ~4095u;
+
+ CS_LOW();
+ xmit_spi(OP_WRITE_ENABLE);
+ CS_HIGH();
+
+ CS_LOW();
+ xmit_spi(OP_ERASE_BLOCK_4K);
+ xmit_spi((BYTE)(addr >> 16));
+ xmit_spi((BYTE)(addr >> 8));
+ xmit_spi((BYTE) addr );
+ CS_HIGH();
+
+ CS_LOW();
+ xmit_spi(OP_WRITE_ENABLE);
+ CS_HIGH();
+
+ CS_LOW();
+ xmit_spi(OP_PROGRAM_PAGE);
+ xmit_spi((BYTE)(addr >> 16));
+ xmit_spi((BYTE)(addr >> 8));
+ xmit_spi((BYTE) addr );
+ for(int i = 0; i < 4096; ++i) {
+ xmit_spi(buff[i]);
+ }
+ CS_HIGH();
+}
+