at_flash.c

Go to the documentation of this file.
00001 /*
00002  * File:        at_flash.c
00003  * Purpose:     Atmel flash handler functions
00004  * Author:      Peter Ivanov, Olimex Ltd.
00005  * Modified by:
00006  * Created:     2007-05-19 11:29:32
00007  * Last modify: 2007-10-14 15:39:42 ivanovp {Time-stamp}
00008  * Copyright:   (C) Peter Ivanov, 2007
00009  * Licence:     GPL
00010  */
00016 #include <msp430xG461x.h>
00017 #include "at_flash.h"
00018 
00019 #define BUFFER_1_WRITE                      0x84    // buffer 1 write
00020 #define BUFFER_2_WRITE                      0x87    // buffer 2 write
00021 #define BUFFER_1_READ                       0x54    // buffer 1 read
00022 #define BUFFER_2_READ                       0x56    // buffer 2 read        
00023 #define B1_TO_MM_PAGE_PROG_WITH_ERASE       0x83    // buffer 1 to main memory page program with built-in erase
00024 #define B2_TO_MM_PAGE_PROG_WITH_ERASE       0x86    // buffer 2 to main memory page program with built-in erase
00025 #define B1_TO_MM_PAGE_PROG_WITHOUT_ERASE    0x88    // buffer 1 to main memory page program without built-in erase
00026 #define B2_TO_MM_PAGE_PROG_WITHOUT_ERASE    0x89    // buffer 2 to main memory page program without built-in erase
00027 #define MM_PAGE_PROG_THROUGH_B1             0x82    // main memory page program through buffer 1
00028 #define MM_PAGE_PROG_THROUGH_B2             0x85    // main memory page program through buffer 2
00029 #define AUTO_PAGE_REWRITE_THROUGH_B1        0x58    // auto page rewrite through buffer 1
00030 #define AUTO_PAGE_REWRITE_THROUGH_B2        0x59    // auto page rewrite through buffer 2
00031 #define MM_PAGE_TO_B1_COMP                  0x60    // main memory page compare to buffer 1
00032 #define MM_PAGE_TO_B2_COMP                  0x61    // main memory page compare to buffer 2
00033 #define MM_PAGE_TO_B1_XFER                  0x53    // main memory page to buffer 1 transfer
00034 #define MM_PAGE_TO_B2_XFER                  0x55    // main memory page to buffer 2 transfer
00035 #define READ_STATUS_REGISTER                0xD7    // read status register
00036 #define CONTINUOUS_ARRAY_READ               0xE8    // continuous read
00037 #define MAIN_MEMORY_PAGE_READ               0x52    // main page read
00038 #define PAGE_ERASE                          0x81    // page erase
00039 
00040 // buffer for test
00041 unsigned char WriteBuffer[]   = { 1, 2, 3, 4, 5, 6, 5, 8, 9, 0 };
00042 unsigned char ReadBuffer[]    = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
00043 
00044 void InitUCA_SPI(void) 
00045 {
00046     // master mode, data valid on rising edge, msb first
00047     UCA0CTL0 = UCCKPH + UCMST + UCMSB + UCSYNC;
00048 
00049     // clock -> SMCLK
00050     UCA0CTL1 = UCSSEL1;
00051 
00052     // Baud Rate
00053     UCA0BR0 = 0x80;        
00054     UCA0BR0 = 0x00; // BR1 ??? XXX
00055 
00056     // second functionality of pin
00057     P7SEL |= 0x0e;        // P7.1-3 SPI option select
00058     P7DIR |= 0x0b;        // P7.0 output direction
00059     P7OUT |= 0x01;        // P7.0 HIGH
00060 
00061 }
00062 
00063 void FlashEnable(void) 
00064 {
00065     // Set P7.0 to LOW
00066     P7OUT &= ~0x01;
00067 }
00068 
00069 void FlashDisable(void) 
00070 {
00071     // Set P7.0 to HIGH
00072     P7OUT |= 0x01;
00073 }
00074 
00075 unsigned char WriteByte(unsigned char data) 
00076 {
00077     while ((IFG2&UCA0TXIFG)==0);    // wait while not ready / for RX
00078     UCA0TXBUF = data;        // write
00079     while ((IFG2&UCA0RXIFG)==0);    // wait for RX buffer (full)
00080     return (UCA0RXBUF);
00081 }
00082 
00083 unsigned char FlashBusy(void)
00084 {
00085 
00086     unsigned char result;
00087 
00088     FlashEnable();
00089 
00090     // Send RDSR - Read Status Register opcode
00091     WriteByte(READ_STATUS_REGISTER);
00092 
00093     // Get register contents
00094     result = WriteByte(0x0);
00095 
00096     FlashDisable();
00097 
00098     if (result&0x80)
00099     {
00100         return 0;
00101     }
00102     else
00103     {
00104         return 1;
00105     }
00106 
00107 }
00108 
00109 void ByteToBuffer(unsigned int byte, unsigned int address) 
00110 {
00111 
00112     unsigned int addr    = 0;
00113     //unsigned int page    = 0;
00114 
00115     //page = address/264;
00116     addr = address%264;    
00117 
00118     // Wait for write to complete
00119     while( FlashBusy() );    
00120 
00121     // perform to transfer
00122 
00123     // Set the Write Enable latch
00124     FlashEnable();
00125 
00126     WriteByte(BUFFER_1_WRITE);
00127 
00128     WriteByte(0x00);
00129 
00130     WriteByte((*((unsigned char*)&addr +1)));
00131 
00132     WriteByte(((unsigned char)addr));
00133 
00134     // Send the byte to write
00135     WriteByte(byte);
00136 
00137     FlashDisable();
00138 
00139     // Wait for write to complete
00140     while( FlashBusy() );
00141 }
00142 
00143 void FlashReadArray(unsigned long address, unsigned char *buffer, unsigned char length) 
00144 {
00145 
00146     unsigned int addr    = 0;
00147     unsigned int page    = 0;
00148 
00149     page = address/264;
00150     addr = address%264;
00151 
00152     page<<=1;    
00153 
00154     FlashEnable();
00155 
00156     // Send READ opcode
00157     WriteByte(CONTINUOUS_ARRAY_READ);
00158     
00159     // 24 bit page and address
00160     WriteByte((*((unsigned char*)&page+1)));
00161 
00162     WriteByte((((unsigned char)page&0xFE)|(*((unsigned char*)&addr+1) & 0x01)));
00163 
00164     WriteByte((unsigned char)addr);
00165 
00166     // 32 don't care bits
00167     WriteByte(0x00);
00168     WriteByte(0x00);
00169     WriteByte(0x00);
00170     WriteByte(0x00);
00171 
00172     while(length--)
00173     {
00174        *buffer++ = WriteByte(0x0);
00175     };
00176     
00177 
00178 
00179     FlashDisable();
00180 }
00181 
00182 
00183 void WriteBufferToMainMemory(unsigned int address) 
00184 {
00185     
00186     //unsigned int addr    = 0;
00187     unsigned int page    = 0;
00188 
00189     page = address/264;
00190     //addr = address%264;    
00191 
00192     page<<=1;
00193 
00194     // Wait for write to complete
00195     while( FlashBusy() );    
00196 
00197     FlashEnable();
00198 
00199     //command
00200     WriteByte(B1_TO_MM_PAGE_PROG_WITH_ERASE);
00201 
00202     //6 reserved + 2 high address
00203     WriteByte((*((unsigned char*)&page+1)));
00204 
00205     //7 low address bits + 1 don't care
00206     WriteByte(((unsigned char)page));
00207 
00208     //don't care 8 bits
00209     WriteByte(0x00);
00210 
00211     FlashDisable();
00212 
00213     // Wait for write to complete
00214     while( FlashBusy() );
00215 }
00216 
00217 void ReadBufferFromMainMemory(unsigned int address) 
00218 {
00219 
00220     //unsigned int addr    = 0;
00221     unsigned int page    = 0;
00222 
00223     page = address/264;
00224     //addr = address%264;    
00225 
00226     page<<=1;
00227 
00228     // Wait for write to complete
00229     while( FlashBusy() );    
00230 
00231     FlashEnable();
00232 
00233     //command
00234     WriteByte(MM_PAGE_TO_B1_XFER);
00235 
00236     //6 reserved + 2 high address
00237     WriteByte((*((unsigned char*)&page+1)));
00238 
00239     //7 low address bits + 1 don't care
00240     WriteByte(((unsigned char)page));
00241 
00242     //don't care 8 bits
00243     WriteByte(0x00);
00244 
00245     FlashDisable();
00246 
00247     // Wait for write to complete
00248     while( FlashBusy() );
00249 }
00250 
00251 
00252 char TestATFlash(void) 
00253 {
00254     char i;
00255 
00256     // Test led and flash memory
00257     InitUCA_SPI();
00258 
00259     // Write WriteBuffer
00260     for(i=0; i<10; i++) 
00261     {
00262         ByteToBuffer(WriteBuffer[i], i);
00263     }
00264     WriteBufferToMainMemory(0);
00265 
00266     // Read ReadBuffer
00267     FlashReadArray(0, ReadBuffer, 10);
00268 
00269     // check for Correct Read
00270     for (i=0; i<10; i++) 
00271     {
00272         if(WriteBuffer[i]!=ReadBuffer[i]) 
00273         {
00274             return 0;
00275         }
00276     }
00277 
00278     return 1;
00279 }
00280 

Generated on Sun Dec 9 17:17:10 2007 for Sample MSP430-4619LCD Project by  doxygen 1.5.1