// Lua: adc.force_init_mode(x) static int adc_init107( lua_State *L ) { uint8_t byte107 = luaL_checkinteger (L, 1); uint32 init_sector = flash_safe_get_sec_num () - 4; // Note 32bit alignment so we can safely cast to uint32 for the flash api char init_data[SPI_FLASH_SEC_SIZE] __attribute__((aligned(4))); if (SPI_FLASH_RESULT_OK != flash_safe_read ( init_sector * SPI_FLASH_SEC_SIZE, (uint32 *)init_data, sizeof(init_data))) return luaL_error(L, "flash read error"); // If it's already the correct value, we don't need to force it if (init_data[107] == byte107) { lua_pushboolean (L, false); return 1; } // Nope, it differs, we need to rewrite it init_data[107] = byte107; if (SPI_FLASH_RESULT_OK != flash_safe_erase_sector (init_sector)) return luaL_error(L, "flash erase error"); if (SPI_FLASH_RESULT_OK != flash_safe_write ( init_sector * SPI_FLASH_SEC_SIZE, (uint32 *)init_data, sizeof(init_data))) return luaL_error(L, "flash write error"); lua_pushboolean (L, true); return 1; }
uint32_t flash_detect_size_byte(void) { #define FLASH_BUFFER_SIZE_DETECT 32 uint32_t dummy_size = FLASH_SIZE_256KBYTE; uint8_t data_orig[FLASH_BUFFER_SIZE_DETECT] ICACHE_STORE_ATTR = {0}; uint8_t data_new[FLASH_BUFFER_SIZE_DETECT] ICACHE_STORE_ATTR = {0}; if (SPI_FLASH_RESULT_OK == flash_safe_read(0, (uint32 *)data_orig, FLASH_BUFFER_SIZE_DETECT)) { dummy_size = FLASH_SIZE_256KBYTE; while ((dummy_size < FLASH_SIZE_16MBYTE) && (SPI_FLASH_RESULT_OK == flash_safe_read(dummy_size, (uint32 *)data_new, FLASH_BUFFER_SIZE_DETECT)) && (0 != os_memcmp(data_orig, data_new, FLASH_BUFFER_SIZE_DETECT)) ) { dummy_size *= 2; } }; return dummy_size; #undef FLASH_BUFFER_SIZE_DETECT }
bool flash_init_data_written(void) { // FLASH SEC - 4 uint32_t data[2] ICACHE_STORE_ATTR; #if defined(FLASH_SAFE_API) if (SPI_FLASH_RESULT_OK == flash_safe_read((flash_safe_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)data, sizeof(data))) #else if (SPI_FLASH_RESULT_OK == spi_flash_read((flash_rom_get_sec_num() - 4) * SPI_FLASH_SEC_SIZE, (uint32 *)data, sizeof(data))) #endif // defined(FLASH_SAFE_API) { if (data[0] == 0xFFFFFFFF && data[1] == 0xFFFFFFFF) { return false; } } return true; }
static int flash_char_read(struct inode *inode, struct file *filp, char *buf, int count) { int rlen; struct flashpartition *part = (struct flashpartition *)filp->private_data; FDEBUG(printk("flash_char_read\n")); rlen = flash_safe_read(part, (unsigned char *)part->start + filp->f_pos, (unsigned char *)buf, count); /* advance file position pointer */ if(rlen >= 0) filp->f_pos += rlen; return rlen; }
static s32_t my_spiffs_read(u32_t addr, u32_t size, u8_t *dst) { flash_safe_read(addr, (void *)dst, size); return SPIFFS_OK; }