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.

Mixing Assembly code with C code serious problem

Other Parts Discussed in Thread: MSP430F2274

Hello,

I am programming for the MSP430F2274 and I started to notice a serious problem. I am using "Real Time Clock Library" provided by TI which uses assembly code (RTC_Calendar.asm). 

The assembly file declares several global variables that sit in the following section of the memory:

000003c8   TI_year

000003ca   TI_second

000003cb   TI_minute

000003cc   TI_hour

000003cd   TI_day

000003ce   TI_month

000003cf   TI_PM

000003d0   TI_FebDays

000003d1   TI_dayOfWeek

000003d2   TI_dayLightZone

000003d3   TI_dayLightSavings

 

The problem is that my C code global variables are starting to use the above locations resulting in bad things. Any idea how to prevent the C variables code from using certain sections of memory?

  • The Real Time Clock library defines these variables to be placed in the .bss section.  This section is also used by C/C++ compiler for global and static variables.

    What C variables are you concerned about using the .bss section?  Or is it the C stack and system memory (for dynamic memory allocation) is spilling over into these variables?

    You do have the option of placing these variables into their own named section and then allocating that section to an area of RAM which would not be impacted by the stack.

  • It seem to me that your problem is caused by the linking process.

    Which tool chain (IAR or CCE) are you using? Which version?

    IAR and CCE are not the same. Different versions of IAR are not the same. Different versions of CCE are not the same. What is the RTC Library you use intended? A mismatch can cause the linker to do silly things.

     

  • BrandonAzbell said:

    What C variables are you concerned about using the .bss section?  Or is it the C stack and system memory (for dynamic memory allocation) is spilling over into these variables?

    I had declared a char global variable and it always had the value of TI_year.  The memory map tool had &my_variable always pointing to TI_year location, which is bad.

    BrandonAzbell said:

    You do have the option of placing these variables into their own named section and then allocating that section to an area of RAM which would not be impacted by the stack.

    This would be great however I looked through the several pdfs regarding coding for MSP430 (like the C optimization guide and the assembly guide) and found no concrete information. Is there a place that explains this?

  • I am using Version: CCE v3.1   Build: 3.2.3.6.4 

    I use the RTC library to implement software real time clock. Been working great so far until this issue showed up.

  • Raf said:

    I had declared a char global variable and it always had the value of TI_year.  The memory map tool had &my_variable always pointing to TI_year location, which is bad.

    That is interesting.  The linker should allocate a space for each variable unless you explicitly do something to have it create a union or similar.

     

    Raf said:

    This would be great however I looked through the several pdfs regarding coding for MSP430 (like the C optimization guide and the assembly guide) and found no concrete information. Is there a place that explains this?

    The MSP430 Optimizing C/C++ Compiler v3.1 User's Guide (SLAU132) in Section 5.8 talks about pragma directives that you can use to create data sections in C code.  However, since the RTC library is in assembly, the MSP430 Assembly Language Tools v3.1 User's Guide (SLAU131) in Section 4.2 talks about section directives.

    For assembly files, you can specifiy a named section and redefine the RTC variables as .data.

         .sect "my_section"
    TI_year       .word 0x0000
    TI_second .byte 0x00
    etc.

    If you have a named section, you will then need to modify the linker command file to include this section and allocate it to a memory in the SECTIONS directive statement.

    SECTIONS
    {
       ...
       my_section : {} > RAM
       ...
    }

     

  • Thank you for the help so far. I tried following the instructions but I'm still having a problem

    This is what I have so far:

     

     

    .sect "rtc" ; rtc section

    TI_year .word 0x0000

    TI_second .byte 0x00

    TI_minute .byte 0x00

    TI_hour .byte 0x00

    TI_day .byte 0x00

    TI_month .byte 0x00

    TI_PM .byte 0x00

    TI_FebDays .byte 0x00

    TI_dayOfWeek .byte 0x00

    TI_dayLightZone .byte 0x00

    TI_dayLightSavings .byte 0x00

     

     

    The lnk_msp430f2274.cmd has been modified to include the following

     

    SECTIONS

    {

        .bss     : {} > RAM                /* GLOBAL & STATIC VARS              */

        .sysmem   : {} > RAM                /* DYNAMIC MEMORY ALLOCATION AREA    */

        .rtc     : {} > RAM                /* RTC VAR               */

        .stack   : {} > RAM (HIGH)         /* SOFTWARE SYSTEM STACK             */

    ...

     

     

    The problem is the compiler gives me a warning creating output section "rtc" without SECTIONS specification.  

    Because of this warning it assigns the RTC variables to:

     

     

    00000008   TI_FebDays

    00000007   TI_PM

    00000005   TI_day

    0000000b   TI_dayLightSavings

    0000000a   TI_dayLightZone

    00000009   TI_dayOfWeek

    00000004   TI_hour

    00000003   TI_minute

    00000006   TI_month

    00000002   TI_second

    00000000   TI_year

     

     

    Which isnt RAM. Any ideas?

     

  • Raf said:

    .sect "rtc" ; rtc section

    SECTIONS

    {

        .bss     : {} > RAM                /* GLOBAL & STATIC VARS              */

        .sysmem   : {} > RAM                /* DYNAMIC MEMORY ALLOCATION AREA    */

        .rtc     : {} > RAM                /* RTC VAR               */

        .stack   : {} > RAM (HIGH)         /* SOFTWARE SYSTEM STACK             */

    ...

    The problem is the compiler gives me a warning creating output section "rtc" without SECTIONS specification.  

    That is right.  The linker does not find an output section "rtc" specification, but it does find a ".rtc" specification.  Remove the . in front of .rtc, such as below.

    SECTIONS
    {
        .bss     : {} > RAM                /* GLOBAL & STATIC VARS              */
        .sysmem   : {} > RAM                /* DYNAMIC MEMORY ALLOCATION AREA    */
        rtc     : {} > RAM                /* RTC VAR               */
        .stack   : {} > RAM (HIGH)         /* SOFTWARE SYSTEM STACK             */
    ...

  • This fixed the problem. Thanks a lot.

  • I know this thread is kind of old, but I thought you should know that if you are using the RTC_Calendar.s43 module, you will need to modify the "setDate (y, m, d)" function. It is erroneous.

    In particular, there is an instruction that modifies R10 ("add.w   r10,     r11"), but R10 is never pushed. To fix this, add a "Push R10" at the beginning and a "Pop R10" at the end.

    It's the kind of bug that gives me giggles!

  • Err...oops..

    The offending instruction is actually "            mov.b   TI_mNumbers(r7), r10". I forgot that MSP430 assembly is written "inst src, dst".

**Attention** This is a public forum