电子大神的日记本,供应链专家的功夫茶盘,在这里记录、分享与共鸣。

登录以开始

DSP5402读写K9F1G08之C++源代码

FlashObj::FlashObj(void)
{

  McBSP1Init();
  FlashInit();
}

void FlashObj::::McBSP1Init(void)//SPI配置
{
  McBSP1->SPSA = SPCR1;
  McBSP1->SPSD = (0x01 << SPCR1_RJUST);//接收符号扩展及判别模式13~14
  McBSP1->SPSA = SPCR2;
  McBSP1->SPSD = (1 << SPCR2_FRST)  //帧同步发生器复位
               | (1 << SPCR2_GRST)  //采样率发生器复位
               | (1 << SPCR2_XEMPTY)//发送移位寄存器空
               | (1 << SPCR2_XRDY); //发送准备好
  McBSP1->SPSA = PCR;
  McBSP1->SPSD = (1 << PCR_XIOEN)   //发送通用I/O模式位
               | (1 << PCR_RIOEN)   //接收通用I/O模式位
               | (1 << PCR_FSXM)    //发送帧同步模式位(FLASH的片选信号)
               | (1 << PCR_CLKXM)   //发送器时钟模式位
               | (1 << PCR_FSXP);   //关闭FLASH的片选信号(FSX="1")

void FlashObj::FlashInit(void)
{
  FlashDisable();
  for(int i = 0; i < 256; i ++) {
    FlashBuffers[i] = 0;
  }
  MakerId = 0;
  DeviceId = 0;
  ThirdCyc = 0;
  FourthCyc = 0;
}

void FlashObj::FlashEnable(void)
{
  McBSP1->SPSA = PCR;
  McBSP1->SPSD &= ~(1 << PCR_FSXP);//打开FLASH的片选信号(FSX="0")
}

void FlashObj::FlashDisable(void)
{
  McBSP1->SPSA = PCR;
  McBSP1->SPSD |= (1 << PCR_FSXP);//关闭FLASH的片选信号(FSX="1")
}

void FlashObj::FlashWait(void)
{
  McBSP1->SPSA = PCR;
  do {
    __nop();
    __nop();
  }
  while((McBSP1->SPSD & (1 << PCR_FSRP)) == 0);//读FSR信号
}

void FlashObj::FlashSck(void)
{
//  McBSP1->SPSA = PCR;
//  __nop();
//  __nop();
  McBSP1->SPSD |= (1 << PCR_FSXP);//FSX="1"
  __nop();
//  __nop();
  McBSP1->SPSD &= ~(1 << PCR_FSXP);//FSX="0"
  __nop();
//  __nop();
}

/*-----------------------------------------
  K9F1G08--128M*8bit

-----------------------------------------*/
bool FlashObj::FlashId(void)
{
bool flag;
  FlashEnable();//使能FLASH
  Port_Cle = 0x90;
  __nop();//此处绝对不能少于2个nop()
  __nop();
  __nop();
  __nop();
  __nop();
  __nop();
  Port_Cle = 0x00;
//  __nop();//此处绝容不下1个nop()
  MakerId = Port_Data;
  FlashSck();//发送时钟信号
  DeviceId = Port_Data;
  FlashSck();//发送时钟信号
  ThirdCyc = Port_Data;
  FlashSck();//发送时钟信号
  FourthCyc = Port_Data;
  FlashDisable();//禁止FLASH
  flag = (MakerId == 0xec) && (DeviceId == 0xf1) && (ThirdCyc == 0x80) && (FourthCyc == 0x15);
  return flag;//K9F1G08的ID={0xec,0xec,0xf1,0x80,0x15}
}

/*---------------------------------------------
  块擦除
---------------------------------------------*/

bool FlashObj::BlockErase(unsigned int Sector)
{
bool flag = false;
  FlashEnable();//使能FLASH
  Port_Cle = 0x60;
  __nop();
  __nop();
  Port_Ale = Sector & 0xff;
  __nop();
  __nop();
  Port_Ale = (Sector >> 8) & 0xff;
  __nop();
  __nop();
  Port_Cle = 0xd0;
  FlashWait();
  Port_Cle = 0x70;
  flag = (Port_Data & 0x01) == 0;
  FlashDisable();//禁止FLASH
  return flag;
}

void FlashObj::PageRead(unsigned long Sector, unsigned int Length, unsigned int * DataBuff)
{
  FlashEnable();//使能FLASH
  Port_Cle = 0;
  __nop();
  __nop();
  Port_Ale = Sector & 0xff;
  __nop();
  __nop();
  Port_Ale = (Sector >> 8) & 0xff;
  __nop();
  __nop();
  Port_Ale = (Sector >> 16) & 0xff;
  __nop();
  __nop();
  Port_Ale = (Sector >> 24) & 0xff;
  __nop();
  __nop();
  Port_Cle = 0x30;
  FlashWait();
  McBSP1->SPSA = PCR;
  for(int i = 0; i < Length; i ++) {
    FlashSck();//发送时钟信号
    DataBuff[i] = Port_Data;
  }
  FlashDisable();//禁止FLASH
}

bool FlashObj::PageWrite(unsigned long Sector, unsigned int Length, unsigned int * DataBuff)
{
bool flag;
  FlashEnable();//使能FLASH
  Port_Cle = 0x80;
  __nop();
  __nop();
  Port_Ale = Sector & 0xff;
  __nop();
  __nop();
  Port_Ale = (Sector >> 8) & 0xff;
  __nop();
  __nop();
  Port_Ale = (Sector >> 16) & 0xff;
  __nop();
  __nop();
  Port_Ale = (Sector >> 24) & 0xff;
  __nop();
  __nop();
  McBSP1->SPSA = PCR;
  for(int i = 0; i < Length; i ++) {
    Port_Data = DataBuff[i] & 0xff;
    __nop();
    __nop();
  }
  Port_Cle = 0x10;
  FlashWait();
  Port_Cle = 0x70;
  flag = (Port_Data & 0x01) == 0;
  FlashDisable();//禁止FLASH
  return flag;
}
 

博主
sgh1255
星梦之家
树的方向,由风决定;人的方向,自己决定
点击跳转