www.pudn.com > 25xx256-EEPROM-programming-reference.rar > c_spi_bb_def.c, change:2007-06-19,size:15472b


//                   SPI_BB_DEF.C  - FUNCTIONS  DEFINITION  
 //**************************************************************************** 
 // File Name    : c_spi_bb_def.c  
 // Dependencies : spi_bb_dec.h = functions declaration   
 //                REG952.h     = SFRs definitions offered by "Keil"  
 //                STARTUP950   = start-up code for LPC952 ( "Keil" )   
 // Processor    : P89LPC952   
 // Hardware     : MicroChip's SPI EEPROM = 25xx256 on MCB950 EVB .  
 // I.D.E.       : uVision3 - Keil  
 // Company      : MicroChip Technology , Inc.  
 //........................................................................... 
 //                       SOFTWARE  LICENSE AGREEMENT  
 //........................................................................... 
 // "Microchip Technology Inc. (“Microchip”) licenses this software to you  
 // solely for use with Microchip Serial EEPROM products.  
 // The software is owned by Microchip and/or its licensors,and is protected  
 // under applicable copyright laws.  All rights reserved. 
 // SOFTWARE IS PROVIDED “AS IS.”  MICROCHIP AND ITS LICENSOR EXPRESSLY  
 // DISCLAIM ANY WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED, INCLUDING  
 // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS  
 // FOR A PARTICULAR PURPOSE,OR NON-INFRINGEMENT. IN NO EVENT SHALL MICROCHIP  
 // AND ITS LICENSORS BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR  
 // CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, HARM TO YOUR EQUIPMENT,  
 // COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY  
 // CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE  
 // THEREOF),ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER SIMILAR COSTS." 
 //*************************************************************************** 
 // History      :  V1.0 - Initial Release  
 //........................................................................... 
 // File Description : This is the file declaring all the necesary functions 
 //                    for the application note ANxxxx .  
 //                    Functions are declared in "spi_bb_dec.h" and defined in 
 // the below file . As described , there are 3 types of  functions :  
 // initialization , spi_access , auxiliary . Moreover , the function defines  
 // the public variables ( byte & bit ) and the slave_address constants : 
 // for write and read .                      
 //............................................................................. 
    #include "REG952.h" 
    #include "c_spi_bb_dec.h"  
     sfr S1CONX = 0xB6  ;    // correct definition of UART1 register       
//............................................................................. 
//                         GLOBAL CONSTANTS  
//............................................................................ 
    #define READ    03       // READ       instruction code   
    #define WRITE   02       // WRITE      instruction code  
    #define WRDI    04       // WR DISBALE instruction code  
    #define WREN    06       // WR ENABLE  instruction code 
    #define RDSR    05       // RD STATUS  instruction code  
    #define WRSR    01       // WR STATUS  instruction code  
    #define NOPROT  00       // NO WRITE PROTECTION  
//............................................................................  
//                        GLOBAL  VARIABLES 
//............................................................................       
    unsigned char bdata eep_buf         ;      //   eeprom's data buffer   
          sbit shift7 = eep_buf^7       ;      //   bit7 of  data buffer 
          sbit shift0 = eep_buf^0       ;      //   bit0 of  data buffer  
//........................................................................... 
//                        INITIALIZATION FUNCTIONS   
//........................................................................... 
        void  ini_intr(void)  // interrupts'   init                        
    {// IEN0 = 0x00 ;  
     // IEN1 = 0x00 ;  
     // IEN2 = 0x00 ;         // disable all interrupts                         
                           }  // reset value = 00 , disable intr 
     
 
       void  ini_wdt(void)     // W.D.T.'s init - WDT disabled in UCFG1  
    {  WDCON = 0xe0          ; // WDCON = PRE210-xx-WDRUN-WDTOF-WDCLK   
       WDL   = 0xff          ; // roll-over at maximum  
       WFEED1 = 0xa5         ; // stop WDT even it's underflow reset is dis  
       WFEED2 = 0x5a         ; // feed sequence after WDCON write-intr dis 
                           }  
              
 
       void  ini_osc(void)     // oscillator's init-TRIM=RCCLK-ENCLK-TRIM5,0  
    {  TRIM |= 0xc0        ;   // RCCLK=ENCLK=1 (trim value remains the same)                      
                               // enable CCLK:2 on XTAL2 = P3.0  
                           }  
     
 
       void  ini_gpio(void)    // GPIO's  init 
    {  P0M1 = 0xff          ;  // P0 = analog apps , all bits = inputs  
       P0M2 = 0x00          ;    
       P1M1 = 0xfe          ;  // P0=I2C->P1.3=SDA=open dr,P1.2=SCL=open dr    
       P1M2 = 0x0d          ;  // COM0->P1.1=RXD0=inp , P1.0=TXD0=push pull 
       P2M1 = 0x0b          ;  // MOSI=P2.2=push-pull ,   MISO=P2.3=input  
       P2M2 = 0x34          ;  // SS=P2.4=push pull , SPICLK=P2.5=push-pull  
       P3M1 = 0x02          ;  // P3.1 = XTAL1 = inp  
       P3M2 = 0x01          ;  // P3.0 = CLKOUT= out push pull  
       P4M1 = 0x0a          ;  // P4.3=RXD1=inp , P4.2=TXD1=push pull ,  
       P4M2 = 0x05          ;  // P4.1=TRIG=inp , P4.0=push pull .  
       P5M1 = 0x00          ;  // all P5's bits = bi - directional  
       P5M2 = 0x00          ;  //  
       P5   = 0x00          ;  // clear all LEDs  
                         }  
 
       void  ini_i2c(void)     // I2C bit-bang init  
    {  SDA = 1   ; SCL = 1  ;  // SDA = SCL = 1 , pre-start condition  
                         }                             
 
       void  ini_com1(void)    // UART1's      init 
    {   
//     S1CON    = 0x52      ;  //SM01=01=8bUART-REN=1-TB8/RB8=00,TI=1,RI=0 
       S1CONX   = 0x52      ;    
       BRG0_1   = 0xf0      ;  // BAUD=256*BRG1+BRG0+16  
       BRG1_1   = 0x02      ;  // 7.373Mhz : 9600 = 768 = 2*256+240+16  
       BRGCON_1 = 0x03      ;  // 9600 baud + 1b_start + 8b_date + 1b_stop   
                         }       
 
       void  ini_tim(void)     // Timers'  init 
    {  TMOD = 0x22          ;  // T0,1 = MODE2 (8b auto-reload)   
                               // GATE0,1=0(delay timer_0 will start at TR0)  
                         }       
 
       void ini_spi(void)  
    {  SPICLK=1 ;   SS=1 ;     // initialize SPI bits .   
         MOSI=1 ; MISO=1 ;  }  // they were programmed I/O in "ini_gpio()"  
 
       void ini_memspi(void) 
    {  spi_wren()           ;  // enable writes in the memory  
       spi_start()          ;  // SS=nCS=0 , start access   
   eep_buf=WRSR ; spi_wr()  ;  // program "write status reg" instruction  
 eep_buf=NOPROT ; spi_wr()  ;  // write "no protection" in the status reg 
       spi_stop()           ;  // SS=nCS=1 , stop access   
       dly5ms()         ;   }  // write cycle time  
//.............................................................................   
//                   SPI  ACCESS FUNCTIONS  DEFINITIONS  
//***************************************************************************** 
             void spi_start(void)  
           { SS=1 ; SS=0 ;  }  // assert nCS=0                                                                  
//............................................................................. 
             void spi_stop(void)  
           { SS=0 ; SS=1 ;  }  // assert nCS=1                       
//............................................................................. 
             void spi_wren(void)  
           { spi_start()            ;   // SS=nCS=0 , start access  
     eep_buf=WREN     ; spi_wr()    ;   // insert "write enable" command .  
             spi_stop()        ;    }   // SS=nCS=1 , stop access   
//............................................................................ 
             void spi_wr(void)           // write an 8b streaming  
      { unsigned char bit_count = 0 ;    // bit counter for the 8b streaming  
        while(bit_count<8)               // 8 bits cycle           
        { MOSI=shift7               ;    // MOSI=SI=bit_n of eeprom's data buffer  
          SPICLK=0                  ;    // pulse low SPICLK   
          SPICLK=1                  ;    // the memory will read on rising edge 
          eep_buf=eep_buf<<1        ;    // shift left 1bit the eep's data buf  
          bit_count++         ;  }  }    // increment bit counter(repeat for 8b) 
//............................................................................ 
         unsigned char spi_rd(void)      // read an 8b streaming  
       { unsigned char bit_count=0  ;    // bit counter of the 8b streaming  
         MISO = 1                   ;    // prepare MISO as input   
         while(bit_count<8)              // 8 bits cycle  
       { SPICLK=0                   ;    // fetch memory's SO 
         SPICLK=1                   ;    // prepare the read  
         eep_buf=eep_buf<<1         ;    // shift left 1b eeprom data buffer   
         shift0=MISO                ;    // read bit_n from eeprom     
         bit_count++      ;         }    // increment bit counter(repeat for 8b)  
         return eep_buf   ;         }    // return read data byte                                           
//........................................................................... 
      void spi_rndwr(unsigned int eep_adr, unsigned char eep_data)  
      {  spi_wren()                 ;    // enable future writes  
         spi_start()                ;    // START command  
         eep_buf = WRITE            ;    //   
         spi_wr()                   ;    // insert WRITE instruction   
         eep_buf = eep_adr>>8       ;  
         spi_wr()                   ;    // write the high_byte of the adr  
         eep_buf = eep_adr&0xff     ;       
         spi_wr()                   ;    // write the  low_byte of the adr 
         eep_buf = eep_data         ;  
         spi_wr()                   ;    // write the data_character  
         spi_stop()                 ;    // finally , STOP command 
         dly5ms()             ;     }    // write cycle time      
//............................................................................ 
      void spi_rndrd(unsigned int eep_adr,unsigned char *dst)  
      {   
	     spi_start()                ;  // START command  
         eep_buf = READ             ;         
         spi_wr()                   ;  // write instruction code = READ   
         eep_buf = eep_adr>>8       ;  
         spi_wr()                   ;  // write the high_byte of the adr  
         eep_buf = eep_adr&0xff     ;       
         spi_wr()                   ;  // write the  low_byte of the adr  
         spi_rd()                   ;  // read MISO line in eep_buf 
         *dst = eep_buf             ;  // read random byte  
         spi_stop()         ;       }  // STOP command  
                                       // WILL IT WORK WITH EEP_DATA ???  
//............................................................................ 
void spi_pgwr(unsigned char *source,unsigned int eep_adr,unsigned char lofsstr) 
   { // lofsstr = length of the source string  
     unsigned char k=0              ;  // char counter  
     spi_wren()                     ;  // enable future writes  
     spi_start()                    ;  // START command  
     eep_buf = WRITE ; spi_wr()     ;  // insert WRITE instruction  
     eep_buf = eep_adr>>8           ;   
     spi_wr()                       ;  // write high byte of the address  
     eep_buf = eep_adr & 0xff       ;   
     spi_wr()                       ;  // write low byte of the address  
     while( k < lofsstr )  
   { eep_buf = *source ; spi_wr()   ;  // write char from the source str  
     source++ ; k++ ;               }  // increment pointer & counter             
     spi_stop() ; dly5ms()  ;       }  // STOP + write cycle time  
//............................................................................ 
void spi_pgrd(unsigned char *dest,unsigned int eep_adr,unsigned char lofdstr)  
  { 
     // lofdstr = length of the destination string  
     unsigned char k=0              ;  // char counter   
     spi_start()                    ;  // START command  
     eep_buf = READ  ; spi_wr()     ;  // insert READ instruction  
     eep_buf = eep_adr>>8           ;   
     spi_wr()                       ;  // write high byte of the address  
     eep_buf = eep_adr & 0xff       ;   
     spi_wr()                       ;  // write low byte of the address  
     while( k < lofdstr )  
   { spi_rd()   ; *dest = eep_buf   ;  // read char in the destination str  
     dest++    ; k++  ;             }  // increment pointer & counter             
     spi_stop()       ;             }  // STOP command                                   
   
//**************************************************************************** 
//                    AUXILIARY  FUNCTIONS  DEFINITIONS  
//**************************************************************************** 
   void dly_usec(unsigned char num_usec) // delay in micro-seconds 
// Since 8051_Timers in mode2 work on 8 bits,the argument of the function 
// may not override "255" . Accordingly , "num_usec" may not override "64" .  
       { TH0 = ~(1+4*(num_usec-1))  ; // timers<-PCLK=CCLK:2=7.373Mhz:2-> 271ns   
         TF0 = 0 ;                    // clear overflow flag  
         TR0 = 1 ;                    // start T0  
         while(!TF0)  ;               // wait overflow  
                                    }  
//............................................................................ 
    void dly5ms(void)                 // 5 miliseconds delay  
  { unsigned char j = 100      ;      // 100 * 50usec = 5 msec  
    while(--j) { dly_usec(50)  ; }   
                                   }   
//............................................................................ 
    void dly250ms(void)               // 250 miliseconds delay  
  { unsigned char k = 50       ;      //  50 * 5 msec = 250 msec  
    while(--k) { dly5ms()      ; }    // usefull for messages' display 
                                   }   
//............................................................................. 
    void dly1s(void)                  // 1 second delay   
  { dly250ms() ; dly250ms()    ;      // 4 * 250 ms = 1 second  
    dly250ms() ; dly250ms()    ;   }  // usefull for messages' display 
//............................................................................. 
       void tx_com1(unsigned char com1_buf)  // sends on UART1 line com1_buf   
       {                              //  TI1 = set in S1CON init  
//       while(!(S1CON&0x02))  ;      //  wait for TI_1 = 1  
//       S1CON &= 0xfd         ;      //  clear TI_1  
         while(!(S1CONX&0x02)) ;  
		 S1CONX &=0xfd ;    
         S1BUF = com1_buf      ;      //  write data in serial data buffer                  
                                   }   
//*****************************************************************************