Files
BLV_C1F_Module/BasicCode/Drive/SPI SRAM/SPI_SRAM.c
caocong 95916b9995 fix:修改UDP通讯中,取电变化上报机制
1、问题点:当RCU网络状态异常的情况下,网络还处于协商状态下,还未进入正常通讯环节时,取电变化不会进行判断。这会导致取电变化上报与实际产生取电状态时间点对不上。
2、将BLV_C1F_Module代码上传至Gitea,之前代码修改记录请查看 .\BasicCode\Readme.txt
2026-01-23 09:23:12 +08:00

291 lines
8.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*******************************************************************************
* Function Name : APS6404L-3SQR SRAM -- 8M*8bit
* Description : SRAM驱动程序
*******************************************************************************/
#include "includes.h"
/*******************************************************************************
* Function Name : SPI_SRAM_Init
* Description : SRAM初始化
* Input : None
* Return : None
*******************************************************************************/
void SPI_SRAM_Init(void)
{
#if (USE_CORE_TYPE == 1) //使用C1F核心板
//设置SPI GPIO
GPIOA_ModeCfg(GPIO_Pin_12, GPIO_ModeOut_PP_20mA); //CS
GPIOA_SetBits( GPIO_Pin_13 );
GPIOA_ModeCfg(GPIO_Pin_13|GPIO_Pin_14, GPIO_ModeOut_PP_5mA);
GPIOA_ModeCfg(GPIO_Pin_15, GPIO_ModeIN_PU); //MISO
//SPI 主机模式
SPI0_MasterDefInit( );
SPI0_DataMode(Mode0_HighBitINFront);
SRAM_CE_L; //SRAM和flash使用同一片选脚默认选SRAM
#elif (USE_CORE_TYPE == 2) //使用C1核心板
//设置SPI GPIO
GPIOA_ModeCfg(GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14, GPIO_ModeOut_PP_5mA);
//SPI 主机模式
SPI0_MasterDefInit( );
SPI0_DataMode(Mode0_HighBitINFront);
SRAM_CE_H;
#endif //USE_CORE_TYPE == CORE_TYPE_C1F
}
/*******************************************************************************
* Function Name : SRAM_Write_Byte
* Description : SRAM写字节
* Input :
wdate : 需要写入的字节
add :字节写入的地址
* Return : None
*******************************************************************************/
void SRAM_Write_Byte(uint8_t wdate,uint32_t add)
{
uint8_t Hadd16=0x00,Hadd8=0x00,Ladd=0x00;
Ladd=add;
Hadd8=add>>8;
Hadd16=add>>16;
if(add >= SRAM_ADDRESS_MAX) return ;
SRAM_CE_L;
SPI0_MasterSendByte(SRAM_CMD_Write);
SPI0_MasterSendByte(Hadd16);
SPI0_MasterSendByte(Hadd8);
SPI0_MasterSendByte(Ladd);
SPI0_MasterSendByte(wdate);
SRAM_CE_H;
DelayUs(10);
}
/*******************************************************************************
* Function Name : SRAM_Read_Byte
* Description : SRAM读字节
* Input :
add :读取字节的地址
* Return : 返回读取字节
*******************************************************************************/
uint8_t SRAM_Read_Byte(uint32_t add)
{
uint8_t Hadd8=0x00,Hadd16=0x00,Ladd=0x00,rdate=0x00;
Ladd=add;
Hadd8=add>>8;
Hadd16=add>>16;
if(add >= SRAM_ADDRESS_MAX) return 0x00;
SRAM_CE_L;
SPI0_MasterSendByte(SRAM_CMD_Read);
SPI0_MasterSendByte(Hadd16);
SPI0_MasterSendByte(Hadd8);
SPI0_MasterSendByte(Ladd);
rdate = SPI0_MasterRecvByte();
SRAM_CE_H;
// if(rdate == 0xFF)
// {
// if(GPIOB_ReadPortPin(GPIO_Pin_3) == 0x00)
// {
// GPIOB_SetBits(GPIO_Pin_3);
// }else{
// GPIOB_ResetBits(GPIO_Pin_3);
// }
// }
DelayUs(10);
return rdate;
}
/*******************************************************************************
* Function Name : SRAM_Write_Word
* Description : SRAM写uint16_t数据 -- 采用小端模式保存数据
* Input :
wdate : 需要写入的字节
add :字节写入的地址
* Return : 返回读取字节
*******************************************************************************/
void SRAM_Write_Word(uint16_t wdate,uint32_t add)
{
SRAM_Write_Byte((uint8_t)(wdate & 0xFF),add);
SRAM_Write_Byte((uint8_t)((wdate >> 8) & 0xFF),add + 1);
}
/*******************************************************************************
* Function Name : SRAM_Read_Word
* Description : SRAM写uint16_t数据 -- 采用小端模式保存数据
* Input :
add :读取字的地址
* Return : 返回读取字
*******************************************************************************/
uint16_t SRAM_Read_Word(uint32_t add)
{
uint16_t rev = 0;
rev = SRAM_Read_Byte(add + 1);
rev <<= 8;
rev |= SRAM_Read_Byte(add);
return rev;
}
/*******************************************************************************
* Function Name : SRAM_Write_DW
* Description : SRAM写uint32_t数据 -- 采用小端模式保存数据
* Input :
wdate : 需要写入的双字
add :双字写入的地址
* Return : 返回读取双字
*******************************************************************************/
void SRAM_Write_DW(uint32_t wdate,uint32_t add)
{
SRAM_Write_Byte((uint8_t)(wdate & 0xFF),add);
SRAM_Write_Byte((uint8_t)((wdate >> 8) & 0xFF),add + 1);
SRAM_Write_Byte((uint8_t)((wdate >> 16) & 0xFF),add + 2);
SRAM_Write_Byte((uint8_t)((wdate >> 24) & 0xFF),add + 3);
}
/*******************************************************************************
* Function Name : SRAM_Read_DW
* Description : SRAM写uint32_t数据 -- 采用小端模式保存数据
* Input :
add :读取双字的地址
* Return : 返回读取双字
*******************************************************************************/
uint32_t SRAM_Read_DW(uint32_t add)
{
uint32_t rev = 0;
rev = SRAM_Read_Byte(add + 3);
rev <<= 8;
rev |= SRAM_Read_Byte(add + 2);
rev <<= 8;
rev |= SRAM_Read_Byte(add + 1);
rev <<= 8;
rev |= SRAM_Read_Byte(add);
return rev;
}
/*******************************************************************************
* Function Name : SRAM_Write_Buff
* Description : SRAM FIFO方式写入数据
* Input :
wbuff : 需要写入的数据
len : 写入数据的长度 -- 最大4095字节长度
add :字节写入的地址
* Return : None
*******************************************************************************/
void SRAM_Write_Buff(uint8_t* wbuff,uint16_t len,uint32_t add)
{
uint8_t Hadd16=0x00,Hadd8=0x00,Ladd=0x00;
Ladd=add;
Hadd8=add>>8;
Hadd16=add>>16;
//超过范围
if(add + len >= SRAM_ADDRESS_MAX) return ;
SRAM_CE_L;
SPI0_MasterSendByte(SRAM_CMD_Write);
SPI0_MasterSendByte(Hadd16);
SPI0_MasterSendByte(Hadd8);
SPI0_MasterSendByte(Ladd);
SPI0_MasterTrans(wbuff,len);
SRAM_CE_H;
DelayUs(10);
}
/*******************************************************************************
* Function Name : SRAM_Read_Buff
* Description : SRAM FIFO方式读取数据
* Input :
rbuff : 需要读取的数据
len : 读取数据的长度 -- 最大4095字节长度
add :字节写入的地址
* Return : None
*******************************************************************************/
void SRAM_Read_Buff(uint8_t* rbuff,uint16_t len,uint32_t add)
{
uint8_t Hadd16=0x00,Hadd8=0x00,Ladd=0x00;
Ladd=add;
Hadd8=add>>8;
Hadd16=add>>16;
if(add + len >= SRAM_ADDRESS_MAX) return ;
SRAM_CE_L;
SPI0_MasterSendByte(SRAM_CMD_Read);
SPI0_MasterSendByte(Hadd16);
SPI0_MasterSendByte(Hadd8);
SPI0_MasterSendByte(Ladd);
SPI0_MasterRecv(rbuff,len);
SRAM_CE_H;
DelayUs(10);
}
/*******************************************************************************
* Function Name : SRAM_DMA_Write_Buff
* Description : SRAM DMA方式写入数据
* Input :
wbuff : 需要写入的数据
len : 写入数据的长度 -- 最大4095字节长度
add :字节写入的地址
* Return : None
*******************************************************************************/
void SRAM_DMA_Write_Buff(uint8_t* wbuff,uint16_t len,uint32_t add)
{
uint8_t Hadd16=0x00,Hadd8=0x00,Ladd=0x00;
Ladd=add;
Hadd8=add>>8;
Hadd16=add>>16;
if(add + len >= SRAM_ADDRESS_MAX) return ;
SRAM_CE_L;
SPI0_MasterSendByte(SRAM_CMD_Write);
SPI0_MasterSendByte(Hadd16);
SPI0_MasterSendByte(Hadd8);
SPI0_MasterSendByte(Ladd);
SPI0_MasterDMATrans(wbuff,len);
SRAM_CE_H;
DelayUs(10);
}
/*******************************************************************************
* Function Name : SRAM_DMA_Read_Buff
* Description : SRAM DMA方式读取数据
* Input :
rbuff : 需要读取的数据
len : 读取数据的长度 -- 最大4095字节长度
add :字节写入的地址
* Return : None
*******************************************************************************/
void SRAM_DMA_Read_Buff(uint8_t* rbuff,uint16_t len,uint32_t add)
{
uint8_t Hadd16=0x00,Hadd8=0x00,Ladd=0x00;
Ladd=add;
Hadd8=add>>8;
Hadd16=add>>16;
if(add + len >= SRAM_ADDRESS_MAX) return ;
SRAM_CE_L;
SPI0_MasterSendByte(SRAM_CMD_Read);
SPI0_MasterSendByte(Hadd16);
SPI0_MasterSendByte(Hadd8);
SPI0_MasterSendByte(Ladd);
SPI0_MasterDMARecv(rbuff,len);
SRAM_CE_H;
DelayUs(10);
}