我爱嵌入式系统

当前位置:首页 > 软件天地 > 业内资讯 > 详细内容
串行flash操作函数(X5045/93LC66B)
发布时间:2009/11/13  阅读次数:1143  字体大小: 【】 【】【

//**********************************************************************************
//杨屹       2002/08/20       第一版
//串行flash操作函数(X5045/93LC66B)
//联系方法:gdtyy@ri.gdt.com.cn(2003/07/31以前有效)
//**********************************************************************************
//使用方法:
//       接口函数屏蔽了底层硬件操作,用户只需给出操作命令、起始地址、数据长度、数据指针
//即可操作硬件。
//**********************************************************************************
#include <general.h>
//#include <myconfig51.h>
//#include <SerEEROM.h>

bit Isbit7(unsigned char x)//内部使用,对用户透明
{
  x=x&0x80;
  if(x==0) return 0;
  else return 1;
}

bit Isbit15(unsigned int x)//内部使用,对用户透明
{
  x=x&0x8000;
  if(x==0) return 0;
  else return 1;
}

//X5045
bit opx5045(unsigned char cmd,unsigned int addr,unsigned int len,unsigned char *dat)//X5045操作函数
{
  unsigned char i;
  unsigned int j;
  unsigned char x;
  unsigned char adr,a8;

  adr=addr;
  a8=(addr>>5)&0x8;

  switch(cmd){
    case WREN:
    case WRDI:{
      while(sck==0);
      while(sck==1);
      xcs=0;
      for(i=0;i<8;i++){
        si=Isbit7(cmd);
        cmd=cmd<<1;
        while(sck==0);
        while(sck==1);
      }
      xcs=1;
      break;}
//    case WRDI:{
//      break;}
    case RDSR:{
      while(sck==0);
      while(sck==1);
      xcs=0;
      for(i=0;i<8;i++){
        si=Isbit7(cmd);
        cmd=cmd<<1;
        while(sck==0);
        while(sck==1);
      }
      for(i=0;i<8;i++){
        while(sck==0);
        x=x<<1;
        x=x|so;
        while(sck==1);
      }      
      xcs=1;
      *dat=x;
      break;}
    case WRSR:{
      x=*dat;
      while(sck==0);
      while(sck==1);
      xcs=0;
      for(i=0;i<8;i++){
        si=Isbit7(cmd);
        cmd=cmd<<1;
        while(sck==0);
        while(sck==1);
      }
      for(i=0;i<8;i++){        
        si=Isbit7(x);
        x=x<<1;
        while(sck==0);
        while(sck==1);
      }      
      xcs=1;      
      break;}
    case READ:{      
      cmd=cmd&0xF7;
      cmd=cmd|a8;
      xcs=1;
      while(sck==0);
      while(sck==1);
      xcs=0;
      for(i=0;i<8;i++){
        si=Isbit7(cmd);
        cmd=cmd<<1;
        while(sck==0);
        while(sck==1);
      }        
      for(i=0;i<8;i++){
        si=Isbit7(adr);    
        adr=adr<<1;
        while(sck==0);
        while(sck==1);
      }
      for(j=0;j<len;j++){
        for(i=0;i<8;i++){
          while(sck==0);
          x=x<<1;
          x=x|so;
          while(sck==1);
        }
        *(dat+j)=x;
      }
      xcs=1;      
      break;}
    case WRITE:{
      if(len>4) return 0;  
      
      cmd=cmd&0xF7;
      cmd=cmd|a8;

      while(sck==0);
      while(sck==1);
      xcs=0;
      for(i=0;i<8;i++){
        si=Isbit7(cmd);
        cmd=cmd<<1;
        while(sck==0);
        while(sck==1);
      }
      for(i=0;i<8;i++){
        si=Isbit7(adr);
        adr=adr<<1;
        while(sck==0);
        while(sck==1);
      }
      for(j=0;j<len;j++){
        x=*(dat+j);
        for(i=0;i<8;i++){        
          si=Isbit7(x);
          x=x<<1;
          while(sck==0);
          while(sck==1);
        }
      }      
      xcs=1;
      break;}
    default:{
      return 0;
      break;}
  }
  return 1;
}

//93LC66B
bit op93LC66B(unsigned char cmd,unsigned int addr,unsigned int len,unsigned int *dat)//93LC66B操作函数
{
  unsigned char i;
  unsigned int j;
  unsigned int x;
  unsigned char adr,a8;

  adr=addr;
  a8=(addr>>1)&0x80;

  xcs=0;
  switch(cmd){
    case ERASE:{
      while(sck==0);
      while(sck==1);
      xcs=1;
      for(i=0;i<3;i++){
        si=Isbit7(cmd);
        cmd=cmd<<1;
        while(sck==0);
        while(sck==1);
      }
      //si=Isbit7(a8);//9366A
      //while(sck==0);//9366A
      //while(sck==1);//9366A
      for(i=0;i<8;i++){
        si=Isbit7(adr);
        adr=adr<<1;
        while(sck==0);
        while(sck==1);
      }
      xcs=0;
      while(sck==0);
      while(sck==1);
      xcs=1;
      while(sck==0);
      while(sck==1);
      while(so==0);////////
      while(sck==0);
      while(sck==1);
      xcs=0;
      break;}
    case ERAL:{
      while(sck==0);
      while(sck==1);
      xcs=1;
      for(i=0;i<11;i++){//i<12 for 9366A
        si=Isbit7(cmd);
        cmd=cmd<<1;
        while(sck==0);
        while(sck==1);
      }
      xcs=0;
      while(sck==0);
      while(sck==1);
      xcs=1;
      while(sck==0);
      while(sck==1);
      while(so==0);////////
      while(sck==0);
      while(sck==1);
      xcs=0;
      break;}  
    case EWDS:
    case EWEN:{
      while(sck==0);
      while(sck==1);
      xcs=1;
      for(i=0;i<11;i++){//i<12 for 9366A
        si=Isbit7(cmd);
        cmd=cmd<<1;
        while(sck==0);
        while(sck==1);
      }      
      xcs=0;
      break;}    
    case READB:{
      while(sck==0);
      while(sck==1);
      xcs=1;
      for(i=0;i<3;i++){
        si=Isbit7(cmd);
        cmd=cmd<<1;
        while(sck==0);
        while(sck==1);
      }    
      //si=Isbit7(a8);//9366A
      //while(sck==0);//9366A
      //while(sck==1);//9366A
      for(i=0;i<8;i++){
        si=Isbit7(adr);
        adr=adr<<1;
        while(sck==0);
        while(sck==1);
      }
      for(j=0;j<len;j++){
        for(i=0;i<16;i++){//i<8 for 9366A
          while(sck==0);
          x=x<<1;
          x=x|so;
          while(sck==1);
        }
        *(dat+j)=x;
      }
      xcs=0;
      break;}
    case WRITEB:{
      x=*dat;
      while(sck==0);
      while(sck==1);
      xcs=1;
      for(i=0;i<3;i++){
        si=Isbit7(cmd);
        cmd=cmd<<1;
        while(sck==0);
        while(sck==1);
      }    
      //si=Isbit7(a8);//9366A
      //while(sck==0);//9366A
      //while(sck==1);//9366A
      for(i=0;i<8;i++){
        si=Isbit7(adr);
        adr=adr<<1;
        while(sck==0);
        while(sck==1);
      }
      for(i=0;i<16;i++){//i<8 for 9366A
        si=Isbit15(x);//Isbit7 for 9366A
        x=x<<1;
        while(sck==0);
        while(sck==1);
      }
      xcs=0;
      while(sck==0);
      while(sck==1);      
      xcs=1;
      while(sck==0);
      while(sck==1);
      while(so==0);////////
      while(sck==0);
      while(sck==1);
      xcs=0;
      break;}
    case WRAL:{
      x=*dat;
      while(sck==0);
      while(sck==1);
      xcs=1;
      for(i=0;i<11;i++){//i<12 for 9366A
        si=Isbit7(cmd);
        cmd=cmd<<1;
        while(sck==0);
        while(sck==1);
      }      
      for(i=0;i<16;i++){//i<8 for 9366A
        si=Isbit15(x);//Isbit7 for 9366A
        x=x<<1;
        while(sck==0);
        while(sck==1);
      }
      xcs=0;
      while(sck==0);
      while(sck==1);      
      xcs=1;
      while(sck==0);
      while(sck==1);
      while(so==0);////////
      while(sck==0);
      while(sck==1);
      xcs=0;
      break;}
    default:{
      return 0;
      break;}
  }
  return 1;
}

//extra:init IO port and convert 1bit to 4bit.
void initport(void)//初始化IO端口
{
  isin=1;linkok=1;txspeed=1;pow0=1;pow1=1;
}

void opBoardSel(unsigned char num)//板选输出
{
  unsigned char i;

  bcs=0;
  num=num<<4;
  while(sck==0);  
  while(sck==1);
  bcs=1;
  for(i=0;i<4;i++){
    bas=Isbit7(num);
    num=num<<1;
    while(sck==0);
    while(sck==1);
  }
  bcs=0;
}

我要评论
  • 匿名发表
  • [添加到收藏夹]
  • 发表评论:(匿名发表无需登录,已登录用户可直接发表。) 登录状态:未登录
最新评论
所有评论[2]
  • 评论人:[匿名] 时间: [2019/9/4 12:40:32] IP:[5.188.84.13*]
  • Achat Tadalis Sx Homme Propecia Increased Libido a href=http//elc4sa.comviagra/a Cialis Online Bologna Vardenafil In Osterreich Erhaltlich Vega H
  • 评论人:[匿名] 时间: [2019/3/30 6:56:16] IP:[95.215.0.14*]
  • Where Can I Buy Cheap Flagyl In Carrollton a href=http//drugslr.comcialis/a Venta Viagra Barata

51RTOS.com 版权所有  

Copyright 20006-2009 我爱嵌入式 ( 51RTOS.com ) All rights reserved 沪ICP备09080633号