spin2cpp: missing volatile (bug?)
I tried running spin2cpp on FullDulpexSerial.spin, and created my own main.cpp file:
Running with the version of FDS that spin2cpp generates gives gibberish output:
However, compiling with -O0 (instead of -Os) gives the expected output. This lead to the addition of volatile to the fdsSpin class variables:
fds.h:
I don't know if this is a bug or not, since there isn't really any way to tell if a PASM snippet will modify a variable, but maybe a warning would be good.
#include "fds.h"
#include <stdint.h>
#include <propeller.h>
int main(void)
{
int32_t rxpin = 31;
int32_t txpin = 30;
int32_t mode = 0;
int32_t baud = 115200;
fdsSpin port;
port.Start(rxpin, txpin, mode, baud);
waitcnt(CLKFREQ*1 + CNT);
port.Tx('a');
port.Tx('b');
port.Tx('c');
port.Tx('d');
port.Tx('\n');
port.Tx('\r');
port.Str("Trouble\r\n");
waitcnt(CLKFREQ/2 + CNT);
for(;;)
{
// char output[] = "Hello, World!\r\n";
// port.Str(&output);
port.Str("XYZ\r\n");
waitcnt(CLKFREQ/2 + CNT);
port.Str("Afternoon\r\n");
waitcnt(CLKFREQ/2 + CNT);
port.Str("Boxing\r\n");
waitcnt(CLKFREQ/2 + CNT);
port.Str("Charlie\r\n");
waitcnt(CLKFREQ + CNT);
}
}
Running with the version of FDS that spin2cpp generates gives gibberish output:
Propeller Version 1 on /dev/ttyUSB0 Loading main.elf to hub memory 4596 bytes sent Verifying RAM ... OK [ Entering terminal mode. Type ESC or Control-C to exit. ] bcd Trouble YZ fternoon Xoxing eharlie oYZ fternoon Xoxing eharlie oYZ ...
However, compiling with -O0 (instead of -Os) gives the expected output. This lead to the addition of volatile to the fdsSpin class variables:
fds.h:
//
// automatically generated by spin2cpp on Mon Nov 26 13:40:54 2012
// ./spin2cpp.linux fds.spin
//
#ifndef fdsSpin_Class_Defined__
#define fdsSpin_Class_Defined__
#include <stdint.h>
class fdsSpin {
public:
...
private:
// int32_t Cog;
// int32_t Rx_head;
...
volatile int32_t Cog;
volatile int32_t Rx_head;
...
};
#endif
I don't know if this is a bug or not, since there isn't really any way to tell if a PASM snippet will modify a variable, but maybe a warning would be good.


Comments
Thanks for the report!
Eric
Another small issue, with the same object: the buffer size is specified with a constant in the Spin version, but replaced with a literal in the .h file (look for 987):
class FFDS1 { public: static const int Buf_len = 987; static const int Min_half_period = 86; static const int Xon = 17; static const int Xoff = 19; int32_t Start(int32_t Rx_pin, int32_t Tx_pin, int32_t Rate); int32_t Stop(void); int32_t Setbaud(int32_t Rate); int32_t Setbaudclock(int32_t Rate, int32_t Sysclock); int32_t Str(int32_t String_ptr); int32_t Tx(int32_t Char_val); int32_t Txbuf(int32_t Buf_ptr, int32_t Buffer_bytes); int32_t Txbufnowait(int32_t Buf_ptr, int32_t Buffer_bytes); int32_t Waitfortx(void); int32_t Rxflush(void); int32_t Rxcheck(void); int32_t Rxtime(int32_t Ms); int32_t Rx(void); int32_t Dec(int32_t Value); int32_t Hex(int32_t Value, int32_t Digits); int32_t Bin(int32_t Value, int32_t Digits); int32_t Atoi(int32_t Strptr); int32_t Htoi(int32_t Strptr); private: volatile int32_t Write_buf_ptr; volatile int32_t Send_temp; volatile int32_t Half_bit_period; volatile uint16_t Rx_head, Rx_tail; volatile uint8_t Rx_buffer[987]; volatile uint8_t Cog; };Eric