#include "M25L.h"
#include <nrf51.h>
#include "../Broad_Pin_config.h"
#include "spi_master_config.h"
#include "nrf_gpio.h"
#define FLASH_ENABLE nrf_gpio_pin_clear(SPI_PSELSS1)
#define FLASH_DISABLE nrf_gpio_pin_set(SPI_PSELSS1)
#define SPI_BUFSIZE 8
uint8_t M25MasterBuffer[SPI_BUFSIZE];
uint8_t M25SlaveBuffer[SPI_BUFSIZE];
uint8_t M25ReadLength, M25WriteLength;
#define SET 0x01
#define RESET 0x00
uint8_t Flash_Init(void)
{
uint32_t *spi_base_address = spi_master_init(SPI1, SPI_MODE0, false);
if (spi_base_address == 0) {
return false;
}
return true;
}
u8 SPI1_WriteRead_Data(u8 dat)
{
uint32_t counter = 0;
NRF_SPI_Type *spi_base = (NRF_SPI_Type *)NRF_SPI1;
spi_base->TXD = (uint32_t)(dat);
while ((spi_base->EVENTS_READY == 0U) && (counter < TIMEOUT_COUNTER))
{
counter++;
}
if (counter == TIMEOUT_COUNTER)
{
FLASH_DISABLE;
return false;
}
else { /* clear the event to be ready to receive next messages */
spi_base->EVENTS_READY = 0U;
}
return (uint8_t)spi_base->RXD;
}
FLASH_ID Flash_Read_ID(void)
{
FLASH_ID Flash_id;
FLASH_ENABLE;
SPI1_WriteRead_Data(SPI_Flash_Read_ID);
Flash_id.MXIC_ID = SPI1_WriteRead_Data(DummyData);
Flash_id.MemType_ID = SPI1_WriteRead_Data(DummyData);
Flash_id.MemDensity_ID = SPI1_WriteRead_Data(DummyData);
FLASH_DISABLE;
return Flash_id;
}
void Flash_Write_Enable(void)
{
FLASH_ENABLE;
SPI1_WriteRead_Data(SPI_Flash_Write_Enable);
FLASH_DISABLE;
}
void Flash_Write_Disable(void)
{
FLASH_ENABLE;
SPI1_WriteRead_Data(SPI_Flash_Write_Disable);
FLASH_DISABLE;
}
u8 Flash_Read_Status_Register(void)
{
u8 data;
FLASH_ENABLE;
SPI1_WriteRead_Data(SPI_Flash_Read_Status);
data = SPI1_WriteRead_Data(DummyData);
FLASH_DISABLE;
return data;
}
void Flash_Write_Status_Register(u8 dat)
{
FLASH_ENABLE;
SPI1_WriteRead_Data(SPI_Flash_Write_Status);
SPI1_WriteRead_Data(dat);
FLASH_DISABLE;
}
void Flash_Sector_Erase(u16 Sector_idx)
{
u8 Sector_Addr_H;
u8 Sector_Addr_M;
u8 Sector_Addr_L;
Sector_Addr_H = (u8)(((Sector_idx * 0x1000) & 0x00ff0000) >> 16);
Sector_Addr_M = (u8)(((Sector_idx * 0x1000) & 0x0000ff00) >> 8);
Sector_Addr_L = 0x00;
Flash_Write_Enable();
FLASH_ENABLE;
SPI1_WriteRead_Data(SPI_Flash_Sector_Erase);
SPI1_WriteRead_Data(Sector_Addr_H);
SPI1_WriteRead_Data(Sector_Addr_M);
SPI1_WriteRead_Data(Sector_Addr_L);
FLASH_DISABLE;
while (Flash_Read_Status_Register()&C_Flash_Busy != RESET);
}
void Flash_Block_Erase(u16 Block_idx)
{
u8 Block_Addr_H;
u8 Block_Addr_M;
u8 Block_Addr_L;
Block_Addr_H = (u8)(((Block_idx * 0x10000) & 0x00ff0000) >> 16);
Block_Addr_M = 0x00;
Block_Addr_L = 0x00;
Flash_Write_Enable();
FLASH_ENABLE;
SPI1_WriteRead_Data(SPI_Flash_Block_Erase);
SPI1_WriteRead_Data(Block_Addr_H);
SPI1_WriteRead_Data(Block_Addr_M);
SPI1_WriteRead_Data(Block_Addr_L);
FLASH_DISABLE;
while (Flash_Read_Status_Register()&C_Flash_Busy != RESET);
}
void Flash_Chip_Erase(void)
{
Flash_Write_Enable();
FLASH_ENABLE;
SPI1_WriteRead_Data(SPI_Flash_Chip_Erase);
FLASH_DISABLE;
while (Flash_Read_Status_Register()&C_Flash_Busy != RESET);
}
void Flash_WriteByte(uint32_t data_addr, u8 data)
{
u8 Data_Addr_H;
u8 Data_Addr_M;
u8 Data_Addr_L;
Data_Addr_H = (u8)(((data_addr * 0x10000) & 0x00ff0000) >> 16);
Data_Addr_M = (u8)(((data_addr * 0x10000) & 0x0000ff00) >> 8);
Data_Addr_L = (u8)((data_addr * 0x10000) & 0x000000ff);
Flash_Write_Enable();
FLASH_ENABLE;
SPI1_WriteRead_Data(SPI_Flash_Page_Program);
SPI1_WriteRead_Data(Data_Addr_H);
SPI1_WriteRead_Data(Data_Addr_M);
SPI1_WriteRead_Data(Data_Addr_L);
SPI1_WriteRead_Data(data);
FLASH_DISABLE;
while (Flash_Read_Status_Register()&C_Flash_Busy != RESET);
}
void Flash_WriteBytes(uint32_t data_addr, u8* ptdata, u16 count)
{
u8 Data_Addr_H;
u8 Data_Addr_M;
u8 Data_Addr_L;
Data_Addr_H = (u8)(((data_addr * 0x10000) & 0x00ff0000) >> 16);
Data_Addr_M = (u8)(((data_addr * 0x10000) & 0x0000ff00) >> 8);
Data_Addr_L = (u8)((data_addr * 0x10000) & 0x000000ff);
Flash_Write_Enable();
FLASH_ENABLE;
SPI1_WriteRead_Data(SPI_Flash_Page_Program);
SPI1_WriteRead_Data(Data_Addr_H);
SPI1_WriteRead_Data(Data_Addr_M);
SPI1_WriteRead_Data(Data_Addr_L);
for (; count != 0; count--)
{
SPI1_WriteRead_Data(*(ptdata++));
}
FLASH_DISABLE;
while (Flash_Read_Status_Register()&C_Flash_Busy != RESET);
}
void Flash_ReadBytes(uint32_t data_addr, u8* ptdata, u16 count)
{
u8 Data_Addr_H;
u8 Data_Addr_M;
u8 Data_Addr_L;
Data_Addr_H = (u8)(((data_addr * 0x10000) & 0x00ff0000) >> 16);
Data_Addr_M = (u8)(((data_addr * 0x10000) & 0x0000ff00) >> 8);
Data_Addr_L = (u8)((data_addr * 0x10000) & 0x000000ff);
FLASH_ENABLE;
SPI1_WriteRead_Data(SPI_Flash_Fast_Read);
SPI1_WriteRead_Data(Data_Addr_H);
SPI1_WriteRead_Data(Data_Addr_M);
SPI1_WriteRead_Data(Data_Addr_L);
SPI1_WriteRead_Data(DummyData);
for (; count != 0; count--)
{
*(ptdata++) = SPI1_WriteRead_Data(DummyData);
}
FLASH_DISABLE;
}
u8 Flash_ReadByte(uint32_t data_addr)
{
u8 data;
u8 Data_Addr_H;
u8 Data_Addr_M;
u8 Data_Addr_L;
Data_Addr_H = (u8)(((data_addr * 0x10000) & 0x00ff0000) >> 16);
Data_Addr_M = (u8)(((data_addr * 0x10000) & 0x0000ff00) >> 8);
Data_Addr_L = (u8)((data_addr * 0x10000) & 0x000000ff);
FLASH_ENABLE;
SPI1_WriteRead_Data(SPI_Flash_Read);
SPI1_WriteRead_Data(Data_Addr_H);
SPI1_WriteRead_Data(Data_Addr_M);
SPI1_WriteRead_Data(Data_Addr_L);
data = SPI1_WriteRead_Data(DummyData);
FLASH_DISABLE;
return data;
}