Using SerialPort communication between PC and MSP430F5438A. It's C# on the PC side and Code Composer Studio (CCS) on the MSP430 side. I'm sending "Hello World!" to MCU. I set the flash pointer to address 0x20000:

unsigned long *Flash_ptr= 0x20000;               // Initialize Flash pointer

then I write to flash (This procedure repeat itself as the number of the chars in the string or the text file):

void write_SegC(char data)
 //__disable_interrupt();                  // 5xx Workaround: Disable global
                                        // interrupt while erasing. Re-Enable
                                        // GIE if needed
 FCTL3 = FWKEY;                            // Clear Lock bit
 FCTL1 = FWKEY+ERASE;                      // Set Erase bit
 *(unsigned int *)Flash_ptr = 0;           // Dummy write to erase Flash seg
 FCTL1 = FWKEY+WRT;                        // Set WRT bit for write operation
 while(!(WAIT & FCTL3));
 *Flash_ptr++ = data;                   // Write value to flash

 FCTL1 = FWKEY;                            // Clear WRT bit
 FCTL3 = FWKEY+LOCK;                       // Set LOCK bit

And then send it back to PC:

void read_SegC(void)
  unsigned long i;
  for(i = 0; i < 8; i++)
      while(!(WAIT & FCTL3));
      while(!(UCA1IFG & UCTXIFG));
      UCA1TXBUF=*Flash_ptr++;                 // Write long int to Flash

what I get in PC: 2d!Wo

instead of: Hello World! which means I just get some chars of the string! the rest are garbage. What I did wrong?

  • \$\begingroup\$ Are you sure "2|r" is garbage? If you calledwrite_SegC three times with 0x32, 0x7C, 0x72 as input, then your output wasn't garbage. Can you post an example of a few bytes of input with the corresponding output? \$\endgroup\$
    – skrrgwasme
    Jul 28, 2014 at 22:33
  • \$\begingroup\$ Can you use a debugger to set some breakpoints? It's hard to debug this as it stands- your issue could be in your assignment of "Heey", it could be in storing or retrieving from flash, or it could be in your transmission. Have you been able to trace "Heey" until it turns into "2|r"? \$\endgroup\$
    – skrrgwasme
    Jul 28, 2014 at 22:38
  • \$\begingroup\$ That's certainly a good hint that you're accessing flash incorrectly, but I'm not sure why. Your statements are valid in C, so check the manual of your chip to see if there are any peculiarities about assigning to pointers into Flash, and check for scope issues to make sure you're really referencing the same 'Flash_ptr' in both locations. \$\endgroup\$
    – skrrgwasme
    Jul 28, 2014 at 23:10
  • \$\begingroup\$ @OmayerGharra How are you connecting to the computer? Is it through USB or are you using some bridge to take a serial protocol to USB \$\endgroup\$
    – Funkyguy
    Jul 31, 2014 at 20:08
  • 1
    \$\begingroup\$ Renya Karasuma, I noticed your edit but it was not clear to me why you edited your post to remove all useful details. This question can still be useful to future visitors, so I rolled back the edit. If you have a new question, please use the 'Ask question' button. \$\endgroup\$
    – user17592
    Sep 13, 2014 at 11:30

1 Answer 1


You need to add a busy check prior to moving on and writing to the next location. Something along these lines should appear after writing at each location.

while(FCTL3 & BUSY){};

Where busy is defined as 0x01 - We're checking to see if FCTL3 Bit 0 is set.

Here is my flash write method in its entirety:

* \brief Writes an array of 8 bit unsigned integers to the flash.
* \param[in]     p_data  pointer to the data to write
* \param[in,out] p_flash starting address to write to
* \param[in]     count   size of the array to write
* \warning This function cannot be used to write to INFO A
void flash_write(const uint8_t * p_data, uint8_t * p_flash, uint16_t count)

    FCTL3 = FWKEY;                  // Clear Lock bit and set the
                                    // Flash write password of FCTL3
    FCTL1 = FWKEY + WRT;            // Enable byte/word write mode and set the
                                    // Flash write password of FCTL1
    while (count > 0u){
        while (FCTL3 & BUSY)        // Wait if the flash is busy
        *p_flash = *p_data; // Write to Flash
    FCTL1 = FWKEY;                  // Clear write bit
    FCTL3 = FWKEY + LOCK;           // Set LOCK bit

Not the answer you're looking for? Browse other questions tagged or ask your own question.