00001
00002
00003
00004
00005
00006
00007
00008
00009
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
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
00047 UCA0CTL0 = UCCKPH + UCMST + UCMSB + UCSYNC;
00048
00049
00050 UCA0CTL1 = UCSSEL1;
00051
00052
00053 UCA0BR0 = 0x80;
00054 UCA0BR0 = 0x00;
00055
00056
00057 P7SEL |= 0x0e;
00058 P7DIR |= 0x0b;
00059 P7OUT |= 0x01;
00060
00061 }
00062
00063 void FlashEnable(void)
00064 {
00065
00066 P7OUT &= ~0x01;
00067 }
00068
00069 void FlashDisable(void)
00070 {
00071
00072 P7OUT |= 0x01;
00073 }
00074
00075 unsigned char WriteByte(unsigned char data)
00076 {
00077 while ((IFG2&UCA0TXIFG)==0);
00078 UCA0TXBUF = data;
00079 while ((IFG2&UCA0RXIFG)==0);
00080 return (UCA0RXBUF);
00081 }
00082
00083 unsigned char FlashBusy(void)
00084 {
00085
00086 unsigned char result;
00087
00088 FlashEnable();
00089
00090
00091 WriteByte(READ_STATUS_REGISTER);
00092
00093
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
00114
00115
00116 addr = address%264;
00117
00118
00119 while( FlashBusy() );
00120
00121
00122
00123
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
00135 WriteByte(byte);
00136
00137 FlashDisable();
00138
00139
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
00157 WriteByte(CONTINUOUS_ARRAY_READ);
00158
00159
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
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
00187 unsigned int page = 0;
00188
00189 page = address/264;
00190
00191
00192 page<<=1;
00193
00194
00195 while( FlashBusy() );
00196
00197 FlashEnable();
00198
00199
00200 WriteByte(B1_TO_MM_PAGE_PROG_WITH_ERASE);
00201
00202
00203 WriteByte((*((unsigned char*)&page+1)));
00204
00205
00206 WriteByte(((unsigned char)page));
00207
00208
00209 WriteByte(0x00);
00210
00211 FlashDisable();
00212
00213
00214 while( FlashBusy() );
00215 }
00216
00217 void ReadBufferFromMainMemory(unsigned int address)
00218 {
00219
00220
00221 unsigned int page = 0;
00222
00223 page = address/264;
00224
00225
00226 page<<=1;
00227
00228
00229 while( FlashBusy() );
00230
00231 FlashEnable();
00232
00233
00234 WriteByte(MM_PAGE_TO_B1_XFER);
00235
00236
00237 WriteByte((*((unsigned char*)&page+1)));
00238
00239
00240 WriteByte(((unsigned char)page));
00241
00242
00243 WriteByte(0x00);
00244
00245 FlashDisable();
00246
00247
00248 while( FlashBusy() );
00249 }
00250
00251
00252 char TestATFlash(void)
00253 {
00254 char i;
00255
00256
00257 InitUCA_SPI();
00258
00259
00260 for(i=0; i<10; i++)
00261 {
00262 ByteToBuffer(WriteBuffer[i], i);
00263 }
00264 WriteBufferToMainMemory(0);
00265
00266
00267 FlashReadArray(0, ReadBuffer, 10);
00268
00269
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