This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430G2553: Problem with non-volatile flash memory and mspflash.h library

Part Number: MSP430G2553
Other Parts Discussed in Thread: ENERGIA

Hello. I know, that this should better be posted on 43oh.com forum, but I got no answer from there.


I have wrote a simple Energia program for digital potentiometer control with incremental rotary encoder. They are connected to MSP430G2553 launchpad.

I want to save variable "counter" (it is an integer number from 0-255) in non-volatile memory using mspflash.h library in case of reset or power interrupt. But it doesn't saves it.
I am allmost sure there is something wrong with initialization of variables or pointers inside my code. Can anyone help? Any improvements in code?

My code:

#include <SPI.h>
#include "MspFlash.h"

#define outputA P2_1
#define outputB P2_2

#define flash SEGMENT_D

 int aState;
 int aLastState;  

 const int slaveSelectPin = SS;
 const int shutDownPin = P1_4;

int pos=0;
int counter=0;
int p=0;

void setup() {

    pinMode (outputA,INPUT_PULLUP);
    pinMode (outputB,INPUT_PULLUP);
    aLastState = digitalRead(outputA);
 
  pinMode (slaveSelectPin, OUTPUT);
  pinMode (shutDownPin, OUTPUT);
    SPI.begin();
  digitalWrite(shutDownPin, HIGH);
  digitalPotWrite(1, 0);
}

void loop() {
 
Flash.read(flash+(pos * sizeof(int)), (unsigned char*)&p, sizeof(int));

      aState = digitalRead(outputA);
   if (aState != aLastState){     
     if (digitalRead(outputB) != aState) {
        counter=++counter;
     } else {
        counter=--counter;
     }
   }
   if (counter <= 255) {
   if (counter < 0) {
   digitalPotWrite(1, 0);
   counter = 0;
   Flash.erase(flash);
   Flash.write(flash+(pos * sizeof(int)), (unsigned char*)&counter, sizeof(int));
   }
   else {
   digitalPotWrite(1, counter);
   Flash.erase(flash);
   Flash.write(flash+(pos * sizeof(int)), (unsigned char*)&counter, sizeof(int));
   }
   }
    if (counter > 255) {    
   digitalPotWrite(1, 255);
   counter = 255;
   Flash.erase(flash);
   Flash.write(flash+(pos * sizeof(int)), (unsigned char*)&counter, sizeof(int));  
   }
       aLastState = aState;
}
int digitalPotWrite(int address, int value) {
  digitalWrite(slaveSelectPin,LOW);
  SPI.transfer(address);
  SPI.transfer(value);
  digitalWrite(slaveSelectPin,HIGH);
}

 

Thank you in advance,

Superpanky

  • Hi Superpanky,
    sorry, but you're absolutely right. TI is not providing support for Energia.

    What I can offer you, is a working code example for FLASH write functionality, which you could use for debugging what's going wrong with yours.
    You can find this code example in our CCS TI Resource Explorer under code examples for MSP430G2553. It is called msp430g2xx3_flashwrite_01.c

    //******************************************************************************
    // MSP430G2xx3 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 *//
    //
    // MSP430G2xx3
    // -----------------
    // /|\| 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 <msp430.h>

    char value; // 8-bit value to write to segment A

    // Function prototypes
    void write_SegC (char value);
    void copy_C2D (void);

    int main(void)
    {
    WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
    if (CALBC1_1MHZ==0xFF) // If calibration constant erased
    {
    while(1); // do not load, trap CPU!!
    }
    DCOCTL = 0; // Select lowest DCOx and MODx settings
    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
    __no_operation(); // 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
    }

    Best regards
    Peter
  • Well that doesn't help a lot, because i'm not familiar with CCS.
    Maybe you can try to tell, at least, what could be wrong in these few lines? I don't understand, how these constructions with pointers and "unsigned char/int" dataytypes do work.


    int pos=0;
    int counter=0;
    int p=0;

    void loop() {

                Flash.read(flash+(pos * sizeof(int)), (unsigned char*)&p, sizeof(int));

                            if (.............) {
                              counter=++counter;
                              } else {
                               counter=--counter;

                             }

                 Flash.erase(flash);

                 Flash.write(flash+(pos * sizeof(int)), (unsigned char*)&counter, sizeof(int));

    }

    Regards, Superpanky

  • Hi Superpanky,
    I am sorry, but this is about C-coding and Energia support, and both are not the target of this forum. This forum is MSP430 product related support. Please consult respective forums, to obtain support on your topics. Many thanks in advance.

    Best regards
    Peter

**Attention** This is a public forum