历史上的今天
今天是:2025年04月25日(星期五)
2019年04月25日 | STM8S105K4 SPI相关代码
2019-04-25 来源:eefocus
/* 初始化函数 */
void SPI_FLASH_Init(void)
{
SPI_Init(SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_2, SPI_MODE_MASTER,
SPI_CLOCKPOLARITY_HIGH, SPI_CLOCKPHASE_2EDGE,
SPI_DATADIRECTION_2LINES_FULLDUPLEX, SPI_NSS_SOFT, 0x07);
SPI_Cmd(ENABLE);
GPIO_Init(SPI_CS , SPI_Pin_CS, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_WriteHigh(SPI_CS, SPI_Pin_CS)
}
/*
写使能
*/
void SPI_FLASH_WriteEnable(void)
{
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
/* Send "Write Enable" instruction */
SPI_FLASH_SendByte(WREN);
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
}
/**
发送一个字节的数据
*/
u8 SPI_FLASH_SendByte(u8 byte)
{
while (SPI_GetFlagStatus( SPI_FLAG_TXE) == RESET);
SPI_SendData(byte);
while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET);
return SPI_ReceiveData();
}
/*
等待写数据结束
*/
void SPI_FLASH_WaitForWriteEnd(void)
{
u8 FLASH_Status = 0;
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(RDSR);
do
{
/* Send a dummy byte to generate the clock needed by the FLASH
FLASH_Status = SPI_FLASH_SendByte(Dummy_Byte);
}
while ((FLASH_Status & WIP_Flag) == SET); /* Write in progress */
SPI_FLASH_CS_HIGH();
}
/*
写一页数据
*/
void SPI_FLASH_PageWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
{
SPI_FLASH_WriteEnable();
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(WRITE);
/****************************************************************/
/* Send WriteAddr high nibble address byte to write to */
SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
/* Send WriteAddr medium nibble address byte to write to */
SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);
/* Send WriteAddr low nibble address byte to write to */
SPI_FLASH_SendByte(WriteAddr & 0xFF);
/*****************************************************************/
/* while there is data to be written on the FLASH */
while (NumByteToWrite--)
{
/* Send the current byte */
SPI_FLASH_SendByte(*pBuffer);
/* Point on the next byte to be written */
pBuffer++;
}
SPI_FLASH_CS_HIGH();
SPI_FLASH_WaitForWriteEnd();
}
/*
写BUFFER数据
*/
void SPI_FLASH_BufferWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
{
u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0;
Addr = WriteAddr % SPI_FLASH_PageSize;// 判断是否是页对齐
count = SPI_FLASH_PageSize - Addr; //一页里的第几个
NumOfPage = NumByteToWrite / SPI_FLASH_PageSize;
NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
if (Addr == 0) /* WriteAddr is SPI_FLASH_PageSize aligned */
{
if (NumOfPage == 0) /* NumByteToWrite < SPI_FLASH_PageSize */
{
SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
}
else /* NumByteToWrite > SPI_FLASH_PageSize */
{
while (NumOfPage--)
{
SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
WriteAddr += SPI_FLASH_PageSize;
pBuffer += SPI_FLASH_PageSize;
}
SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
}
}
else /* WriteAddr is not SPI_FLASH_PageSize aligned */
{
if (NumOfPage == 0) /* NumByteToWrite < SPI_FLASH_PageSize */
{
if (NumOfSingle > count)
{ /* (NumByteToWrite + WriteAddr) > SPI_FLASH_PageSize */
temp = NumOfSingle - count;
SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
WriteAddr += count;
pBuffer += count;
SPI_FLASH_PageWrite(pBuffer, WriteAddr, temp);
}
else
{
SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
}
}
else /* NumByteToWrite > SPI_FLASH_PageSize */
{
NumByteToWrite -= count;
NumOfPage = NumByteToWrite / SPI_FLASH_PageSize;
NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
WriteAddr += count;
pBuffer += count;
while (NumOfPage--)
{
SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
WriteAddr += SPI_FLASH_PageSize;
pBuffer += SPI_FLASH_PageSize;
}
if (NumOfSingle != 0)
{
SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
}
}
}
}
/*
读一个buffer数据
*/
void SPI_FLASH_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)
{
/* Select the FLASH: Chip Select low */
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(READ);
/* Send ReadAddr high nibble address byte to read from */
SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
/* Send ReadAddr medium nibble address byte to read from */
SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
/* Send ReadAddr low nibble address byte to read from */
SPI_FLASH_SendByte(ReadAddr & 0xFF);
while (NumByteToRead--) /* while there is data to be read */
{
/* Read a byte from the FLASH */
*pBuffer = SPI_FLASH_SendByte(Dummy_Byte);
/* Point to the next location where the byte read will be saved */
pBuffer++;
}
SPI_FLASH_CS_HIGH();
}
史海拾趣
|
本帖最后由 jameswangsynnex 于 2015-3-3 20:01 编辑 非接触式IC卡核心技术被破解 涉及高校公交及其他公共领域 日前,工业和信息部发布了《关于做好应对部分IC卡出现严重安全漏洞工作的通知》,要求各地各机关和部门开展对IC卡使用情 ...… 查看全部问答> |
|
如上图 请教一下红圈子中的所有元件及参数的意义。 包括R6\\C3 ,R8\\C5 ,R5\\C2 ,R7\\C4 ,它们在整个电路所起的具体作用是什么 BISS0001说明书我看不懂,所以别叫我再去看那个东西… 查看全部问答> |
|
使用VS2005编译TCPMP 0.72RC1出错。 安装了CE6.0的SDK. 设置如下: Properties->C/C++->Advanced->Compile For Architecture->ARM4(/QRarch4) Properties->Linker->Advanced->Targer Machine->MachineTHUMB 首先编译的的common,可是提示出错: ...… 查看全部问答> |
|
本人有6410开发板刚刚上市,宇宙最低价1300元,联系方式QQ:767800652 本人有6410开发板刚刚上市,宇宙最低价1300元,联系方式QQ:767800652 QQ: 767800652 手机:15914387571 Email:LongFei_Y@163.com 地址:广州科学城香山路17号B栋6楼 idea6410是一款高端ARM11处理器开发板,专为消费类电子、工业控制、车载导 ...… 查看全部问答> |




