Note that there are some explanatory texts on larger screens.

plurals
  1. POAvoid creating multiple copies of code in memory
    primarykey
    data
    text
    <p>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 <em>only existing in one place</em> 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. <code>#include "spi.h"</code>).</p> <p>What is the best approach for what I'm trying to do?</p> <p>Here is an example class. From what I understand, variables such as <code>_callback</code> and function definitions like <code>call()</code> in the .cpp will only appear in spi.o so they will appear only once in memory, but I may be mixed up.</p> <p><strong>spi.h:</strong></p> <pre><code>#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_ */ </code></pre> <p><strong>spi.cpp:</strong></p> <pre><code>#include "spi.h" SPI::voidCallback SPI::_callback = 0; char SPI::largeArray[] = /* data */ ; void SPI::configure() { UCA0MCTL = 0; UCA0CTL1 &amp;= ~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 &amp; UCBUSY); SPI::call(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload