#ifndef SPI_FLASH_H
#define SPI_FLASH_H

#include <stdbool.h>
#include <stdint.h>


#if defined ( __ICCARM__ )
#define ssize_t long
#else
#include <unistd.h>
#endif


#include "FreeRTOS.h"

#define LOG_FLASH_SECTOR_OFFSET	4

#define SPI_FLASH_SECTOR_SIZE		        4096
#define SPI_FLASH_SECTORS_IN_BLOCK_NUMBER   16
#define SPI_FLASH_BLOCK_SIZE                16
#define SPI_FLASH_BLOCK_NUMBER		        32

//extern SemaphoreHandle_t spi_mutex;

typedef struct {
	bool present;
	uint32_t sector_size;
	uint8_t sector_erase_op;
	uint16_t sector_count;
} spi_flash_desc_t;


bool spi_flash_init(void);

ssize_t spi_flash_read(int addr, void *buf, size_t len, uint32_t timeout);

ssize_t spi_flash_write(int addr, const void *buf, size_t len, uint32_t timeout);

int spi_flash_erase_sector(int addr, uint32_t timeout);

int spi_flash_chip_erase(uint32_t timeout);

unsigned int spi_flash_get_sector_size(void);

unsigned int spi_flash_get_sector_count(void);

unsigned int spi_flash_get_total_size(void);


bool spi_flash_is_init(void);

bool spi_flash_test(void);

void spi_flash_get_id(uint8_t *buf);

extern spi_flash_desc_t spi_flash_desc;


// -------------------------------------------------------------------------- //

#endif /* SPI_FLASH_H */
