maxeen
Member level 1
- Joined
- Jul 26, 2012
- Messages
- 36
- Helped
- 3
- Reputation
- 6
- Reaction score
- 3
- Trophy points
- 1,288
- Activity points
- 1,559
unsigned char buffer_flash[64]
unsigned int i
int *p_flash = (int *)0x1000;
for (i=0; i<64; i++) buffer_flash[i] = *p_flash++;
p_flash = (int *)0x1000;
buffer_flash[0] = *p_flash;
p_flash = (int *)0x1001;
buffer_flash[0] = *p_flash;
After doing some research on the web, I have found that whenever I try to read the address 0x1000, the MCU reads both 0x1000 and 0x1001, but discards the latter. It only works with even address, so if I read the address 0x1002, the MCU reads both 0x1002 and 0x1003. What I could not find is how to read the address 0x1001 (or any odd address).
[COLOR="#FF0000"]int[/COLOR] *p_flash = ([COLOR="#FF0000"]int[/COLOR] *)0x1000;
unsigned char * p_flash = (unsigned char *) 0x1000;
The data located is a password that opens a door. But even when I erase the flash, I read zero..
void write_flash(char data, int *addr){ //writes data in addr
while(BUSY & FCTL3); //wait for flash memory to be ready
FCTL1 = 0xA500 | WRT; //enable write
FCTL2 = 0xA500 | FSSEL1 | 0x0D; //select proper clock and divider
FCTL3 = 0xA500 | LOCKA; //locks segment A, which contain important information
while(BUSY & FCTL3); //wait for flash memory to be ready
*addr = data; //write data in addr
addr++; //increment pointer (which isnt really necessary)
while(BUSY & FCTL3); //wait for flash memory to be ready
FCTL1 = 0xA500; //lock writing
FCTL3 = 0xA500 | LOCK;
}
void inicializa_flash(void){
int *p = (int *)0x103A; //points to 0x103A (which is the number o passwords)
char aux; //"work" variable
save_flash((int*)0x1000); //saves the content of the flash (for debugging purposes)
// erase_flash((int*)0x1000); //erase flash
aux = *p;
if(aux == 0xFF) write_password(); //if aux == 0xFF, then flash memory is empty and
//password is saved
}
void write_password(void){
write_flash(0x01, (int *)0x101A);
write_flash(0x02, (int *)0x101C);
write_flash(0x03, (int *)0x101E); //saves the master password
write_flash(0x04, (int *)0x1020);
write_flash(0x01, (int *)0x103A); //saves the number of passwords (1 now)
write_flash(0x00, (int *)0x103C); //saves the number of magnetic cards (0)
}
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 //****************************************************************************** // MSP430G2xx2 Demo - Flash In-System Programming, Copy SegC to SegD // // Description: This program first erases flash seg C, then it increments all // values in seg C, then it erases seg D, then copies seg C to seg D. // Assumed MCLK 771kHz - 1428kHz. // //* Set Breakpoint on NOP in the Mainloop to avoid Stressing Flash *// // // MSP430G2xx2 // ----------------- // /|\| XIN|- // | | | // --|RST XOUT|- // | | // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include <msp430g2452.h> char value; // 8-bit value to write to segment A // Function prototypes void write_SegC (char value); void copy_C2D (void); void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF) { while(1); // If calibration constants erased // do not load, trap CPU!! } BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz DCOCTL = CALDCO_1MHZ; FCTL2 = FWKEY + FSSEL0 + FN1; // MCLK/3 for Flash Timing Generator value = 0; // initialize value while(1) // Repeat forever { write_SegC(value++); // Write segment C, increment value copy_C2D(); // Copy segment C to D _NOP(); // SET BREAKPOINT HERE } } void write_SegC (char value) { char *Flash_ptr; // Flash pointer unsigned int i; Flash_ptr = (char *) 0x1040; // Initialize Flash pointer FCTL1 = FWKEY + ERASE; // Set Erase bit FCTL3 = FWKEY; // Clear Lock bit *Flash_ptr = 0; // Dummy write to erase Flash segment FCTL1 = FWKEY + WRT; // Set WRT bit for write operation for (i=0; i<64; i++) { *Flash_ptr++ = value; // Write value to flash } FCTL1 = FWKEY; // Clear WRT bit FCTL3 = FWKEY + LOCK; // Set LOCK bit } void copy_C2D (void) { char *Flash_ptrC; // Segment C pointer char *Flash_ptrD; // Segment D pointer unsigned int i; Flash_ptrC = (char *) 0x1040; // Initialize Flash segment C pointer Flash_ptrD = (char *) 0x1000; // Initialize Flash segment D pointer FCTL1 = FWKEY + ERASE; // Set Erase bit FCTL3 = FWKEY; // Clear Lock bit *Flash_ptrD = 0; // Dummy write to erase Flash segment D FCTL1 = FWKEY + WRT; // Set WRT bit for write operation for (i=0; i<64; i++) { *Flash_ptrD++ = *Flash_ptrC++; // copy value segment C to segment D } FCTL1 = FWKEY; // Clear WRT bit FCTL3 = FWKEY + LOCK; // Set LOCK bit }
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?