MSP430 Kopierfunktion (Flash in RAM)

In diesem Beispiel-Programm wird eine einfache Kopierfunktion in C und ASM geschrieben. Die Funktion/Unterprogramm soll Daten aus dem Flash in den RAM kopieren bis ein bestimmtes Zeichen bzw. ein bestimmter Wert gefunden wurde (in diesem Beispiel wird ein String kopiert bis das Stringende-Zeichen gefunden wurde). Sind die Daten für den reservierten Buffer zu groß, wird eine 0 zurückgegeben, bei einem erfolgreichen Kopiervorgang eine 1.

ASM-Programm

FILE: main.asm

;***********************************************
;     ___       _             _     
;    |   |_ _ _| |___ ___   _| |___
;    | | |_'_| . | -_|  _|_| . | -_|
;    |___|_,_|___|___|___|_|___|___|
;
; FILE:     main.asm
; Author:   declis (xdec.de)
;***********************************************

    .cdecls "msp430g2231.h"
    .cdecls "lib_func.h"
    .bss buffer,size               ;allocate 16 bytes in RAM
    .sect .const
data: .cstring "omg ASM rox kthx"  ;define string with '�' (in flash)
    .ref copy_data_ram
    .global main
    .text

main:
    mov.w #0x280,SP                ;initialize stack pointer
    mov.w #WDTPW+WDTHOLD,&WDTCTL   ;stop watchdog timer

    mov.w #data,R4                 ;sourcepointer for data in R4
    mov.w #buffer,R5               ;sourcepointer for buffer in R5

    call #copy_data_ram

    jmp $

    .end

FILE: lib_func.asm

;***********************************************
;     ___       _             _     
;    |   |_ _ _| |___ ___   _| |___
;    | | |_'_| . | -_|  _|_| . | -_|
;    |___|_,_|___|___|___|_|___|___|
;
; FILE:     lib_func.asm
; Author:   declis (xdec.de)
;***********************************************

    .cdecls "msp430g2231.h"
    .cdecls "lib_func.h"
    .def copy_data_ram         ;global entry of function
    .text

;------------------------------------------
; Name:         copy_data_ram
; Description:  copy data into RAM/Buffer till
;               termination value found
; Input:        R4 - sourcepointer of source
;               R5 - sourcepointer of buffer
; Returns:      R8 - 1 = success, 0 = overflow
; Destroys:     R8
;------------------------------------------
copy_data_ram:
    push.w SR
    push.w R4
    push.w R5
    push.w R6

    mov.w #size,R8         ;copy size into R8
    mov.w #0,R6            ;index=0
loop:
    cmp.b #value,0(R4)     ;termination value found?
    jz success

    cmp.w R8,R6            ;overflow?
    jz over_error

    mov.b @R4+,0(R5)       ;copy value of "source" into buffer and increment
                            ;sourcepointer of "source"
                            ;C: buffer[index]=source[index]; index++;
    inc.w R5               ;increment sourcepointer of "buffer"
    inc.w R6               ;increment index
    jmp loop

over_error:
    mov.w #0,R8            ;return 0
    jmp done

success:
    mov.w #1,R8            ;return 1

done:
    pop.w R6
    pop.w R5
    pop.w R4
    pop.w SR
    ret

    .end

FILE: lib_func.h

/*************************************************
 *     ___       _             _     
 *    |   |_ _ _| |___ ___   _| |___
 *    | | |_'_| . | -_|  _|_| . | -_|
 *    |___|_,_|___|___|___|_|___|___|
 *
 * FILE:     lib_func.h
 * Author:   declis (xdec.de)
 ************************************************/

#ifndef LIB_FUNC_H_
#define LIB_FUNC_H_

#define size 16         /* define size=16 */
#define value 0         /* define termination value */

#endif /*LIB_FUNC_H_*/

C-Programm

FILE: main.c

  1. /*************************************************
  2.  *     ___       _             _    
  3.  *    |   |_ _ _| |___ ___   _| |___
  4.  *    | | |_'_| . | -_|  _|_| . | -_|
  5.  *    |___|_,_|___|___|___|_|___|___|
  6.  *
  7.  * FILE:     main.c
  8.  * Author:   declis (xdec.de)
  9.  ************************************************/
  10.  
  11. #include <msp430g2231.h>
  12. #include "lib_func_c.h"
  13.  
  14. void main(void)
  15. {
  16.     const char data[]={"omg ASM rox kthx"}; /* data in flash (const char) */
  17.     char buffer[size];                      /* allocate 16 bytes in RAM */
  18.  
  19.     WDTCTL=WDTPW+WDTHOLD;                   /* stop watchdog timer */
  20.  
  21.     copy_data_ram(data,buffer);
  22.  
  23.     for(;;);
  24. }
/*************************************************
 *     ___       _             _     
 *    |   |_ _ _| |___ ___   _| |___
 *    | | |_'_| . | -_|  _|_| . | -_|
 *    |___|_,_|___|___|___|_|___|___|
 *
 * FILE:     main.c
 * Author:   declis (xdec.de)
 ************************************************/

#include <msp430g2231.h>
#include "lib_func_c.h"

void main(void)
{
    const char data[]={"omg ASM rox kthx"}; /* data in flash (const char) */
    char buffer[size];                      /* allocate 16 bytes in RAM */

    WDTCTL=WDTPW+WDTHOLD;                   /* stop watchdog timer */

    copy_data_ram(data,buffer);

    for(;;);
}

FILE: lib_func.c

  1. /*************************************************
  2.  *     ___       _             _    
  3.  *    |   |_ _ _| |___ ___   _| |___
  4.  *    | | |_'_| . | -_|  _|_| . | -_|
  5.  *    |___|_,_|___|___|___|_|___|___|
  6.  *
  7.  * FILE:     lib_func.c
  8.  * Author:   declis (xdec.de)
  9.  ************************************************/
  10.  
  11. #include <msp430g2231.h>
  12. #include "lib_func_c.h"
  13.  
  14. char copy_data_ram(const char *data,char *buffer)
  15. {
  16.     char index=0;
  17.  
  18.     for(;data[index]!=value;index++)    /* termination value found? */
  19.     {
  20.         if(index==size) return 0;       /* overflow occurred */
  21.         buffer[index]=data[index];      /* copy flash-data in RAM/Buffer */
  22.     }
  23.  
  24.     return 1;                           /* success */
  25. }
/*************************************************
 *     ___       _             _     
 *    |   |_ _ _| |___ ___   _| |___
 *    | | |_'_| . | -_|  _|_| . | -_|
 *    |___|_,_|___|___|___|_|___|___|
 *
 * FILE:     lib_func.c
 * Author:   declis (xdec.de)
 ************************************************/

#include <msp430g2231.h>
#include "lib_func_c.h"

char copy_data_ram(const char *data,char *buffer)
{
    char index=0;

    for(;data[index]!=value;index++)    /* termination value found? */
    {
        if(index==size) return 0;       /* overflow occurred */
        buffer[index]=data[index];      /* copy flash-data in RAM/Buffer */
    }

    return 1;                           /* success */
}

FILE: lib_func_c.h

  1. /*************************************************
  2.  *     ___       _             _    
  3.  *    |   |_ _ _| |___ ___   _| |___
  4.  *    | | |_'_| . | -_|  _|_| . | -_|
  5.  *    |___|_,_|___|___|___|_|___|___|
  6.  *
  7.  * FILE:     lib_func_c.h
  8.  * Author:   declis (xdec.de)
  9.  ************************************************/
  10.  
  11. #ifndef LIB_FUNC_C_H_
  12. #define LIB_FUNC_C_H_
  13.  
  14. #define size 16         /* define size=16 */
  15. #define value 0         /* define termination value */
  16.  
  17. char copy_data_ram(const char*,char*);
  18.  
  19. #endif /*LIB_FUNC_C_H_*/
/*************************************************
 *     ___       _             _     
 *    |   |_ _ _| |___ ___   _| |___
 *    | | |_'_| . | -_|  _|_| . | -_|
 *    |___|_,_|___|___|___|_|___|___|
 *
 * FILE:     lib_func_c.h
 * Author:   declis (xdec.de)
 ************************************************/

#ifndef LIB_FUNC_C_H_
#define LIB_FUNC_C_H_

#define size 16         /* define size=16 */
#define value 0         /* define termination value */

char copy_data_ram(const char*,char*);

#endif /*LIB_FUNC_C_H_*/

Leave a Reply

Your email address will not be published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

This site uses Akismet to reduce spam. Learn how your comment data is processed.