Shop OBEX P1 Docs P2 Docs Learn Events
spin2cpp: missing volatile (bug?) — Parallax Forums

spin2cpp: missing volatile (bug?)

SRLMSRLM Posts: 5,045
edited 2012-11-28 04:57 in Propeller 1
I tried running spin2cpp on FullDulpexSerial.spin, and created my own main.cpp file:
#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

  • ersmithersmith Posts: 6,054
    edited 2012-11-26 17:59
    This is a bug of sorts, since the code compiled with -Os doesn't work, but as you say it's very difficult to determine which variables should be marked as volatile. The definition of the Yield() macro works around the volatile issue in -O1 (which I believe works correctly) but for -Os and -O2 something more is needed. I'll keep trying :-).

    Thanks for the report!

    Eric
  • SRLMSRLM Posts: 5,045
    edited 2012-11-26 21:28
    I'll also mention another small bug here: spin2cpp is missing support for the spin "=>=" operator. It gives a syntax error. I found this in the FFDS1.spin object: http://forums.parallax.com/showthread.php?143514-Fast-Full-Duplex-Serial-1-Cog-a.k.a.-FFDS1

    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;
    };
    
  • SRLMSRLM Posts: 5,045
    edited 2012-11-26 21:31
    Also, the --gas option isn't mentioned in the help of spin2cpp, but it seems to work correctly.
  • ersmithersmith Posts: 6,054
    edited 2012-11-28 04:57
    Thanks for the bug reports! I'll be uploading a new version of spin2cpp (1.01) that fixes the operator problem and uses symbolic names instead of literal constants in more places.

    Eric
Sign In or Register to comment.