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.

First word on RAM not set to zero

Other Parts Discussed in Thread: MSP430G2513

Hi,

I am using MSP430G2513 and I have a problem the the first two bytes on RAM not being set to zero at initiation.

I tried setting the global variable that is mapped first in RAM to zero manually, and it was moved forward and replaced by another uninitialized global variable and now the value of that variable is the same as the other one had before.

As I understand it from the map file, RAM starts at 0x200. The first word is set to 0x930C (instead of zero) no matter which variable is mapped to that address. During the run of the program, the values in that address change without the mapped variable being accessed

When the variable is accessed directly, the value will temporarily change to the expected value, only to revert to being junk.

Any idea what can cause this?

Thanks.

  • If you are using IAR Compiler, it will initialize global variables to zero unless you told it otherwise. I do not use CCS, I think it does not do the same.

    But you must have misunderstood something. Try this:

    #include <msp430g2513.h>
    int xyz;   // alternatively: int xyz = 0;
    int main (void)
    {
      return xyz; // access that global variable
    }

  • With the test code the variable is initialized to zero (as CCS does in my experience). So it's not a hardware or IDE problem.

    It still does not explain why it should happen with the code I'm running.

    The value seems to change spontaneously when I do a division once while in the initiation stage of the program. Although, I don't see any use of the variable or address in the disassembly of the division part.

    I tried disabling optimization, which only change the value it was initialized to, but it wasn't zero. Even though it shouldn't really matter as the variable in question is volatile.

    Also, when I wrote that the value changes temporarily when accessed, I really meant it changes when the code changes it manually (my bad).

    Anyways, thanks for the reply.

  • The code you are running is a mystery to others.Obviously, the IDE you use did not do what you wanted. But that is between you two alone, others have no way of knowing or helping.

  • old_cow_yellow said:
    The code you are running is a mystery to others.Obviously, the IDE you use did not do what you wanted. But that is between you two alone, others have no way of knowing or helping.

    In other words: if your code is doing something weird and you want someone to tell you what's going on, you should post your code.
    A rather vague description of what you think is happening won't show the true culprit (if your thoughts were right and complete, you'd already have solved the problem)

  • Sorry I wasn't around. I was working on something else.

    You are both right, of course. I am porting FreeModbus to the MSP430G2513. The file in question is /rtu/mbrtu.c (attached

    /* 
     * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
     * Copyright (c) 2006 Christian Walter <wolti@sil.at>
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     * 1. Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     * 2. Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     * 3. The name of the author may not be used to endorse or promote products
     *    derived from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     * File: $Id: mbrtu.c,v 1.18 2007/09/12 10:15:56 wolti Exp $
     */
    
    /* ----------------------- System includes ----------------------------------*/
    #include "stdlib.h"
    #include "string.h"
    
    /* ----------------------- Platform includes --------------------------------*/
    #include "port.h"
    
    /* ----------------------- Modbus includes ----------------------------------*/
    #include "mb.h"
    #include "mbrtu.h"
    #include "mbframe.h"
    
    #include "mbcrc.h"
    #include "mbport.h"
    
    /* ----------------------- Defines ------------------------------------------*/
    #define MB_SER_PDU_SIZE_MIN     4       /*!< Minimum size of a Modbus RTU frame. */
    #define MB_SER_PDU_SIZE_MAX     256     /*!< Maximum size of a Modbus RTU frame. */
    #define MB_SER_PDU_SIZE_CRC     2       /*!< Size of CRC field in PDU. */
    #define MB_SER_PDU_ADDR_OFF     0       /*!< Offset of slave address in Ser-PDU. */
    #define MB_SER_PDU_PDU_OFF      1       /*!< Offset of Modbus-PDU in Ser-PDU. */
    
    /* ----------------------- Type definitions ---------------------------------*/
    typedef enum
    {
        STATE_RX_INIT,              /*!< Receiver is in initial state. */
        STATE_RX_IDLE,              /*!< Receiver is in idle state. */
        STATE_RX_RCV,               /*!< Frame is beeing received. */
        STATE_RX_ERROR              /*!< If the frame is invalid. */
    } eMBRcvState;
    
    typedef enum
    {
        STATE_TX_IDLE,              /*!< Transmitter is in idle state. */
        STATE_TX_XMIT               /*!< Transmitter is in transfer state. */
    } eMBSndState;
    
    /* ----------------------- Static variables ---------------------------------*/
    static volatile eMBSndState eSndState;
    static volatile eMBRcvState eRcvState;
    
    volatile UCHAR  ucRTUBuf[MB_SER_PDU_SIZE_MAX];
    
    static volatile UCHAR *pucSndBufferCur;
    static volatile USHORT usSndBufferCount;
    
    static volatile USHORT usRcvBufferPos;
    
    /* ----------------------- Start implementation -----------------------------*/
    eMBErrorCode
    eMBRTUInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
    {
        eMBErrorCode    eStatus = MB_ENOERR;
        ULONG           usTimerT35_50us;
    
        ( void )ucSlaveAddress;
        ENTER_CRITICAL_SECTION(  );
    
        /* Modbus RTU uses 8 Databits. */
        if( xMBPortSerialInit( ucPort, ulBaudRate, 8, eParity ) != TRUE )
        {
            eStatus = MB_EPORTERR;
        }
        else
        {
            /* If baudrate > 19200 then we should use the fixed timer values
             * t35 = 1750us. Otherwise t35 must be 3.5 times the character time.
             */
            if( ulBaudRate > 19200 )
            {
                usTimerT35_50us = 35;       /* 1800us. */
            }
            else
            {
                /* The timer reload value for a character is given by:
                 *
                 * ChTimeValue = Ticks_per_1s / ( Baudrate / 11 )
                 *             = 11 * Ticks_per_1s / Baudrate
                 *             = 220000 / Baudrate
                 * The reload for t3.5 is 1.5 times this value and similary
                 * for t3.5.
                 */
                usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate );
            }
            if( xMBPortTimersInit( ( USHORT ) usTimerT35_50us ) != TRUE )
            {
                eStatus = MB_EPORTERR;
            }
        }
        EXIT_CRITICAL_SECTION(  );
    
        return eStatus;
    }
    
    void
    eMBRTUStart( void )
    {
        ENTER_CRITICAL_SECTION(  );
        /* Initially the receiver is in the state STATE_RX_INIT. we start
         * the timer and if no character is received within t3.5 we change
         * to STATE_RX_IDLE. This makes sure that we delay startup of the
         * modbus protocol stack until the bus is free.
         */
        eRcvState = STATE_RX_INIT;
        vMBPortSerialEnable( TRUE, FALSE );
        vMBPortTimersEnable(  );
    
        EXIT_CRITICAL_SECTION(  );
    }
    
    void
    eMBRTUStop( void )
    {
        ENTER_CRITICAL_SECTION(  );
        vMBPortSerialEnable( FALSE, FALSE );
        vMBPortTimersDisable(  );
        EXIT_CRITICAL_SECTION(  );
    }
    
    eMBErrorCode
    eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
    {
        BOOL            xFrameReceived = FALSE;
        eMBErrorCode    eStatus = MB_ENOERR;
    
        ENTER_CRITICAL_SECTION(  );
        assert( usRcvBufferPos < MB_SER_PDU_SIZE_MAX );
    
        /* Length and CRC check */
        if( ( usRcvBufferPos >= MB_SER_PDU_SIZE_MIN )
            && ( usMBCRC16( ( UCHAR * ) ucRTUBuf, usRcvBufferPos ) == 0 ) )
        {
            /* Save the address field. All frames are passed to the upper layed
             * and the decision if a frame is used is done there.
             */
            *pucRcvAddress = ucRTUBuf[MB_SER_PDU_ADDR_OFF];
    
            /* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
             * size of address field and CRC checksum.
             */
            *pusLength = ( USHORT )( usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_CRC );
    
            /* Return the start of the Modbus PDU to the caller. */
            *pucFrame = ( UCHAR * ) & ucRTUBuf[MB_SER_PDU_PDU_OFF];
            xFrameReceived = TRUE;
        }
        else
        {
            eStatus = MB_EIO;
        }
    
        EXIT_CRITICAL_SECTION(  );
        return eStatus;
    }
    
    eMBErrorCode
    eMBRTUSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
    {
        eMBErrorCode    eStatus = MB_ENOERR;
        USHORT          usCRC16;
    
        ENTER_CRITICAL_SECTION(  );
    
        /* Check if the receiver is still in idle state. If not we where to
         * slow with processing the received frame and the master sent another
         * frame on the network. We have to abort sending the frame.
         */
        if( eRcvState == STATE_RX_IDLE )
        {
            /* First byte before the Modbus-PDU is the slave address. */
            pucSndBufferCur = ( UCHAR * ) pucFrame - 1;
            usSndBufferCount = 1;
    
            /* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
            pucSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucSlaveAddress;
            usSndBufferCount += usLength;
    
            /* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */
            usCRC16 = usMBCRC16( ( UCHAR * ) pucSndBufferCur, usSndBufferCount );
            ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 & 0xFF );
            ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 >> 8 );
    
            /* Activate the transmitter. */
            eSndState = STATE_TX_XMIT;
            vMBPortSerialEnable( FALSE, TRUE );
        }
        else
        {
            eStatus = MB_EIO;
        }
        EXIT_CRITICAL_SECTION(  );
        return eStatus;
    }
    
    BOOL
    xMBRTUReceiveFSM( void )
    {
        BOOL            xTaskNeedSwitch = FALSE;
        UCHAR           ucByte;
    
        assert( eSndState == STATE_TX_IDLE );
    
        /* Always read the character. */
        ( void )xMBPortSerialGetByte( ( CHAR * ) & ucByte );
    
        switch ( eRcvState )
        {
            /* If we have received a character in the init state we have to
             * wait until the frame is finished.
             */
        case STATE_RX_INIT:
            vMBPortTimersEnable(  );
            break;
    
            /* In the error state we wait until all characters in the
             * damaged frame are transmitted.
             */
        case STATE_RX_ERROR:
            vMBPortTimersEnable(  );
            break;
    
            /* In the idle state we wait for a new character. If a character
             * is received the t1.5 and t3.5 timers are started and the
             * receiver is in the state STATE_RX_RECEIVCE.
             */
        case STATE_RX_IDLE:
            usRcvBufferPos = 0;
            ucRTUBuf[usRcvBufferPos++] = ucByte;
            eRcvState = STATE_RX_RCV;
    
            /* Enable t3.5 timers. */
            vMBPortTimersEnable(  );
            break;
    
            /* We are currently receiving a frame. Reset the timer after
             * every character received. If more than the maximum possible
             * number of bytes in a modbus frame is received the frame is
             * ignored.
             */
        case STATE_RX_RCV:
            if( usRcvBufferPos < MB_SER_PDU_SIZE_MAX )
            {
                ucRTUBuf[usRcvBufferPos++] = ucByte;
            }
            else
            {
                eRcvState = STATE_RX_ERROR;
            }
            vMBPortTimersEnable(  );
            break;
        }
        return xTaskNeedSwitch;
    }
    
    BOOL
    xMBRTUTransmitFSM( void )
    {
        BOOL            xNeedPoll = FALSE;
    
        assert( eRcvState == STATE_RX_IDLE );
    
        switch ( eSndState )
        {
            /* We should not get a transmitter event if the transmitter is in
             * idle state.  */
        case STATE_TX_IDLE:
            /* enable receiver/disable transmitter. */
            vMBPortSerialEnable( TRUE, FALSE );
            break;
    
        case STATE_TX_XMIT:
            /* check if we are finished. */
            if( usSndBufferCount != 0 )
            {
                xMBPortSerialPutByte( ( CHAR )*pucSndBufferCur );
                pucSndBufferCur++;  /* next byte in sendbuffer. */
                usSndBufferCount--;
            }
            else
            {
                xNeedPoll = xMBPortEventPost( EV_FRAME_SENT );
                /* Disable transmitter. This prevents another transmit buffer
                 * empty interrupt. */
                vMBPortSerialEnable( TRUE, FALSE );
                eSndState = STATE_TX_IDLE;
            }
            break;
        }
    
        return xNeedPoll;
    }
    
    BOOL
    xMBRTUTimerT35Expired( void )
    {
        BOOL            xNeedPoll = FALSE;
    
        switch ( eRcvState )
        {
            /* Timer t35 expired. Startup phase is finished. */
        case STATE_RX_INIT:
            xNeedPoll = xMBPortEventPost( EV_READY );
            break;
    
            /* A frame was received and t35 expired. Notify the listener that
             * a new frame was received. */
        case STATE_RX_RCV:
            xNeedPoll = xMBPortEventPost( EV_FRAME_RECEIVED );
            break;
    
            /* An error occured while receiving the frame. */
        case STATE_RX_ERROR:
            break;
    
            /* Function called in an illegal state. */
        default:
            assert( ( eRcvState == STATE_RX_INIT ) ||
                    ( eRcvState == STATE_RX_RCV ) || ( eRcvState == STATE_RX_ERROR ) );
        }
    
        vMBPortTimersDisable(  );
        eRcvState = STATE_RX_IDLE;
    
        return xNeedPoll;
    }
    
    ) which I made no changes to except some #ifdef's for a different device that will act as a master. The device in question is the slave so it needs no changes made to it. I attached the original file (without the #ifdef's).

    The variable is eRcvState, which is a global static volatile enumeration, typedef'ed as eMBRcvState. It governs the various receiving stages.

    Things to note: While debugging, when stepping over certain (it changes) functions, the value of eRcvState changes spontaneously; but not when stepping into them. Also, as I stated, when doing a division operation (without a hardware division module), the value will change even though the division has nothing to do with this variable.

    The values it most often changes to are 0x12B0, 0x430B and 0x43D2. These are CALL, MOV and CLR asm commands (not respectively of even surely correct). Could I have somehow created this problem in the project settings or something of this nature?

    Hopefully, by the time you wake up (time zones) I will have already fixed this problem (doubtful). But any help or insight will be appreciated.

  • I noticed that if I initialized this variable, it would get mapped to another address, and the variable that gets its old address will be having the same issues. So it's an address problem and not anything with this variable.

    What I did is declare a volatile int before the variable, so that now it is mapped to 0x200. Now this integer is changed but it doesn't prevent the program from running correctly.

    It's still a waste of space that I would rather avoid, though. And it's something I shouldn't have, because I think this solution is compiler specific.

  • Does your code soemwhere use 0x200 as a fixed address for something? I've seen user code where the DTC was programmed to put ADc sampling results to 0x200 and following locations, ignoring the fact that the linekr has placed global variables there.

    Do you use DMA, DTC, or do you use any pointers of any kind for storing data?

    It is possible that the boot code uses some ram at the start of the ram area when the MSP is starting up. But this happens before the user code is started and would be undone by the C startup code that initializes your variables.

  • This device doesn't have an ADC or DMA (it's the 16KB Flash model).

    I think the main clue here is that the changes happen when stepping over functions with the debugger, but not when stepping into and out of them. It also  happens when I assembly step over a division but not when I assembly step into it and go over all the division loop.

    It's as if when I'm stepping into the functions, I'm preventing an interrupt or something that isn't prevented when I'm stepping over. But It happens even before I initiate the UART and clock, and enable the interrupts.

  • The ISR code will be executed if interrupt is enabled and the hardware flag is set (for any reason).

    The mysterious changes of the global variable are caused by your ISR. The compiler probably did its job correctly. The Debugger does not and cannot do what you expect it to do.

    The Debugger cannot stop the world, it can only stop the CPU and some of the clocks.

    If you disable GIE before you "step over", ISR will not be executed during that step over, and the the global variable will not be changed by it.

  • Omer said:
    It's as if when I'm stepping into the functions, I'm preventing an interrupt or something that isn't prevented when I'm stepping over. But It happens even before I initiate the UART and clock, and enable the interrupts.

    Can you post your complete project which demonstrates the problem?

    (Looking at the MSP430 MODBUS demo the demo was written using GCC or Rowley Crossworks specific compiler instrinsics and assembler, and so I had to make some changes to get the demo compiled under CCS)

    Also, which version of CCS are you using, and which debug interface is being used (e.g. Launchpad or MSP-FET430UIF)? There have been bugs in the debugger where the target memory got corrupted during single stepping.

**Attention** This is a public forum