Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid creating multiple copies of code in memory

I'm new to developing on embedded systems and am not used to having very little program memory (16kB in this case) to play with. I would like to be able to create global variables, arrays, and functions that I can access from anywhere in the program while only existing in one place in memory. My current approach is to use static class members and methods that I can use by simply including the header file (e.g. #include "spi.h").

What is the best approach for what I'm trying to do?

Here is an example class. From what I understand, variables such as _callback and function definitions like call() in the .cpp will only appear in spi.o so they will appear only once in memory, but I may be mixed up.

spi.h:

#ifndef SPI_H_
#define SPI_H_

#include "msp430g2553.h"

class SPI {
public:
    typedef void (*voidCallback)(void);

    static voidCallback _callback;
    static char largeArray[1000];
    static __interrupt void USCIA0TX_ISR();
    static void call();

    static void configure();
    static void transmitByte(unsigned char byte, voidCallback callback);
};

#endif /* SPI_H_ */

spi.cpp:

#include "spi.h"

SPI::voidCallback SPI::_callback = 0;
char SPI::largeArray[] = /* data */ ;

void SPI::configure() {
    UCA0MCTL = 0;
    UCA0CTL1 &= ~UCSWRST;                   
    IE2 |= UCA0TXIE;

}

void SPI::transmitByte(unsigned char byte, voidCallback callback) {
    _callback = callback;
    UCA0TXBUF = byte;
}

void SPI::call() {
    SPI::_callback();
}

#pragma vector=USCIAB0TX_VECTOR
__interrupt void SPI::USCIA0TX_ISR()
{
    volatile unsigned int i;
    while (UCA0STAT & UCBUSY);
    SPI::call();
}
like image 204
JustcallmeDrago Avatar asked Jun 17 '13 20:06

JustcallmeDrago


1 Answers

The data members and the member functions of the class you wrote will only be defined once in memory. And if they're not marked static, the member functions will still only be defined once in memory. Non-static data members will be created in memory once for each object that you create, so if you only create one SPI object you only get one copy of its non-static data members. Short version: you're solving a non-problem.

like image 143
Pete Becker Avatar answered Sep 28 '22 22:09

Pete Becker