Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
Guillaume Vlieghe
11/04/08 06:02
Read: 2061 times
Mouscron
Belgium


 
#159624 - flash memeory reading
Hello,
I program a code to write and read the flash memory in my MCU (C8051F020) in C ?
The writing is ok but not the reading. After reading, i send the value by UART. This function isn't ok.
can i help me ?

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "C8051F020.h"                 // SFR declarations
#include "stdio.h"
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F02x
//-----------------------------------------------------------------------------
sfr16 DP       = 0x82;                 // data pointer
sfr16 TMR3RL   = 0x92;                 // Timer3 reload value
sfr16 TMR3     = 0x94;                 // Timer3 counter
sfr16 ADC0     = 0xbe;                 // ADC0 data
sfr16 ADC0GT   = 0xc4;                 // ADC0 greater than window
sfr16 ADC0LT   = 0xc6;                 // ADC0 less than window
sfr16 RCAP2    = 0xca;                 // Timer2 capture/reload
sfr16 T2       = 0xcc;                 // Timer2
sfr16 RCAP4    = 0xe4;                 // Timer4 capture/reload
sfr16 T4       = 0xf4;                 // Timer4
sfr16 DAC0     = 0xd2;                 // DAC0 data
sfr16 DAC1     = 0xd5;                 // DAC1 data
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define     SYSCLK   22118400          // SYSCLK frequency in Hz
#define     BAUDRATE 115200            // Baud rate of UART in bps
sbit        LED = P1^6;                // LED='1' means ON
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);
void Timer0_ms (unsigned ms);
//unsigned char EE_Read (unsigned Addr);
//void EE_Write (unsigned Addr, unsigned char value);
//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------
char eedata [6];
unsigned int ii;
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void) {
   unsigned char xdata * idata pwrite; // pointer to FLASH used for writes
                                       // NOTE: this pointer must be located
                                       // in <data> or <idata> space!

   unsigned char code *pread;          // pointer to FLASH used for reads
   
   char EA_save;                       // saves the current state of the 
                                       // interrupt enable bit.

   // test string stored in FLASH
   unsigned char code test_string[] = "Howdy!";
  
   // disable watchdog timer
   WDTCN = 0xde;                       
   WDTCN = 0xad;
   
   SYSCLK_Init ();                     // initialize oscillator
   PORT_Init ();                       // initialize crossbar and GPIO
   UART0_Init ();                      // initialize UART0
   // PART A -- erase the FLASH page at 0x1000
   
   // initialize write/erase pointer
   pwrite = (unsigned char xdata *) 0x1000;   

   EA_save = EA;                       // save interrupt status   
   EA = 0;                             // disable interrupts (precautionary)
   
   FLSCL |= 0x01;                      // enable FLASH writes/erases from
                                       // user software
   
   PSCTL = 0x03;                       // MOVX writes erase FLASH page

   *pwrite = 0;                        // initiate page erase

   PSCTL = 0;                          // MOVX writes target XRAM
   
   FLSCL &= ~0x01;                     // disable FLASH writes/erases from
                                       // user software  
   
   EA = EA_save;                       // re-enable interrupts
   
   // PART B -- copy test string to FLASH memory at address 0x1000
   
   // initialize FLASH read pointer
   pread = (unsigned char code *) test_string;

   EA_save = EA;                       // save interrupt status
   EA = 0;                             // disable interrupts (precautionary)
   pwrite = 0x1000;                    // initialize FLASH write pointer
   
   FLSCL |= 0x01;                      // enable FLASH writes/erases from
                                       // user software
      
   PSCTL = 0x01;                       // MOVX writes target FLASH memory

   while (*pread != '\0') {            // copy until NULL is detected

      *pwrite = *pread;                // copy byte

      pread++;                         // advance pointers
      pwrite++;
   }

   *pwrite = '\0';

   PSCTL = 0x00;                       // MOVX writes target XRAM
   
   FLSCL &= ~0x01;                     // disable FLASH writes/erases from
                                       // user software   

   EA = EA_save;                       // re-enable interrupts
   
// PART C -- read test string to FLASH memory at address 0x1000 and copy in tab eedata

   EA_save = EA;
   EA = 0;
   pwrite = 0x1000;
   FLSCL |= 0x40;
   PSCTL = 0x00;

   while (*pread !='\0') {

     *eedata = *pread;                // copy byte

      pread++;                         // advance pointers
	  pwrite++;
   }

   PSCTL = 0x00;                       // MOVX writes target XRAM
   
   FLSCL &= ~0x40;                     // disable FLASH writes/erases from
                                       // user software   

   EA = EA_save;                       // re-enable interrupts


   while (1)                          // spin forever
   {                          
     for (ii=0;ii<6; ii++) 
	 {
	   LED = 1;
       printf("%c",eedata[ii]);
	   printf("@13");
     }
	 printf("\x0d");
	 printf("\x0a");
	 LED = 0;
   }

}
//-----------------------------------------------------------------------------
// Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use an 22.1184 MHz crystal
// as its clock source.
//
void SYSCLK_Init (void)
{
   int i;                              // delay counter

   OSCXCN = 0x67;                      // start external oscillator with
                                       // 22.1184 MHz crystal

   for (i=0; i < 256; i++) ;           // Wait for osc. to start up

   while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle

   OSCICN = 0x88;                      // select external oscillator as SYSCLK
                                       // source and enable missing clock
                                       // detector
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports
//
void PORT_Init (void)
{
   XBR0   |= 0x06;                     // Enable SPI0 and UART0
   XBR1    = 0x00;
   XBR2    = 0x40;                     // Enable crossbar and weak pull-ups
   P0MDOUT |= 0x15;                    // enable P0.0 (TX), P0.2 (SCK), and 
                                       // P0.4 (MOSI) as push-pull outputs 
   P1MDOUT |= 0x40;                    // enable P1.6 (LED) as push-pull outputs
                                      
   //P74OUT |= 0x01;                     //pins 4.0 and 4.1 as push-pull outputs
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
//
void UART0_Init (void)
{
   SCON0  = 0x50;                      // SCON0: mode 1, 8-bit UART, enable RX
   TMOD   = 0x20;                      // TMOD: timer 1, mode 2, 8-bit reload
   TH1    = -(SYSCLK/BAUDRATE/16);     // set Timer1 reload value for baudrate
   TR1    = 1;                         // start Timer1
   CKCON |= 0x10;                      // Timer1 uses SYSCLK as time base
   PCON  |= 0x80;                      // SMOD00 = 1 (disable baud rate 
                                       // divide-by-two)
   TI0    = 1;                         // Indicate TX0 ready
}


//-----------------------------------------------------------------------------
// Timer0_ms
//-----------------------------------------------------------------------------
//
// Configure Timer0 to delay <ms> milliseconds before returning.
//
void Timer0_ms (unsigned ms)
{
   unsigned i;                         // millisecond counter

   TCON  &= ~0x30;                     // STOP Timer0 and clear overflow flag
   TMOD  &= ~0x0f;                     // configure Timer0 to 16-bit mode
   TMOD  |=  0x01;
   CKCON |=  0x08;                     // Timer0 counts SYSCLKs

   for (i = 0; i < ms; i++) {          // count milliseconds
      TR0 = 0;                         // STOP Timer0
      TH0 = (-SYSCLK/1000) >> 8;       // set Timer0 to overflow in 1ms
      TL0 = -SYSCLK/1000;
      TR0 = 1;                         // START Timer0
      while (TF0 == 0);                // wait for overflow
      TF0 = 0;                         // clear overflow indicator
   }
}

thanks Regards

List of 31 messages in thread
TopicAuthorDate
flash memeory reading      Guillaume Vlieghe      11/04/08 06:02      
   How do you know      Per Westermark      11/04/08 06:08      
      flash memory reading      Guillaume Vlieghe      11/04/08 06:21      
   Cross-posted on Keil      Per Westermark      11/04/08 06:11      
   pread and pwrite...      Per Westermark      11/04/08 06:36      
      my problem      Guillaume Vlieghe      11/04/08 06:59      
         No, you ignored my notes in my previous post      Per Westermark      11/04/08 07:17      
            answers      Guillaume Vlieghe      11/04/08 07:38      
               Still no answers      Per Westermark      11/04/08 08:00      
   first      Erik Malund      11/04/08 06:56      
      a quick glance seems to revceal ...      Erik Malund      11/04/08 07:00      
         /      Guillaume Vlieghe      11/04/08 07:13      
         MOVX During FLASH Write      Michael Karas      11/05/08 05:49      
   and now he crossed it on the SILabs forum      Erik Malund      11/04/08 11:23      
   my problem      Guillaume Vlieghe      11/04/08 13:51      
      RTFM      Erik Malund      11/04/08 15:28      
         not xdata      Per Westermark      11/04/08 15:50      
            reding flash      Guillaume Vlieghe      11/05/08 01:01      
               So fix the loop then      Per Westermark      11/05/08 01:07      
            Not ok      Maarten Brock      11/08/08 10:32      
         reading flash problem      Guillaume Vlieghe      11/05/08 01:12      
            Yet one more post with broken read loop      Per Westermark      11/05/08 01:52      
               correcting my code      Guillaume Vlieghe      11/05/08 01:57      
                  Then specify exactly what you don't understand      Per Westermark      11/05/08 02:17      
                     i correct my code      Guillaume Vlieghe      11/05/08 02:33      
                        it's working      Guillaume Vlieghe      11/05/08 02:41      
                           Report feedback      Per Westermark      11/05/08 03:07      
                              write cycle time      Guillaume Vlieghe      11/05/08 03:12      
                                 New Question, New Thread.      Andy Neil      11/05/08 03:53      
                           So what fixed it?      Andy Neil      11/05/08 03:50      
   correct registers ?      Guillaume Vlieghe      11/05/08 01:44      

Back to Subject List