Shop OBEX P1 Docs P2 Docs Learn Events
flexspin compiler for P2: Assembly, Spin, BASIC, and C in one compiler - Page 63 — Parallax Forums

flexspin compiler for P2: Assembly, Spin, BASIC, and C in one compiler

16061636566122

Comments

  • garryj wrote: »
    I'm trying to implement multiple spin2 child objects as an array and have run into a snag:
      ser.fstr3(@szP2Vers, @tmp, usb[idx].getVersion(), nlPtr)
    2xKbMObjDemo.spin2:157: error: syntax error, unexpected ')'
    
    The error goes away if the empty parens are dropped from the getVersion method. Unfortunately, PNut requires that () must be there.
    There is no complaint if the non-array usb1.getVersion() syntax is used.
    Tested with fastspin 4.3.1 and b4.3.1-14.

    Thanks, Garry. I'll look into it.
  • RaymanRayman Posts: 14,646
    ersmith wrote: »
    Not sure what's going on with the SD card timing. The low-level code for driving the card is in include/filesys/fatfs/sdmm.cc, but I pretty much just took that from the default bit-banging example that came with FatFs.

    Yes, I saw that. Title of code is "Foolproof MMCv3/SDv1/SDv2 (in SPI mode) control module".
    Well, apparently you can fool it with certain cards (hopefully a very small subset) when pins are shared with flash and/or boot code...

  • Cluso99Cluso99 Posts: 18,069
    Rayman wrote: »
    Ok, I tested out the codes above. But, I still have the same issue with my SDHC card...
    uSD mounts the first time I run it, but won't mount the second time, unless I physically remove the card and stick it back in.
    I think this must have something to do with the way these pins are shared with flash and also used for booting.
    No problem with my regular 2 GB SD card (non- SDHC).
    I used to have more cards for testing around...

    I'll have to try out @Cluso99 code and see if it does any better.

    BTW: Your code seems to create an empty hello.txt file if none exists. Is that what you intended? I think there are some flags if FatFs to prevent this...
    The problem with DO causes the Flash to fail, not the SD card. To be specific, SD fails to release (ie not drive) the DO pin which causes problems with anything attached to the DO pin. But it does not cause any problem with the SD card itself.

    However, if you have other things attached to the SD pins, then this can affect the loading on those pins, and could be the cause of a timing issue. I was very careful with timing issues in the ROM code (and other code) to ensure that the inherent pin delays within the P2 did not cause any problems. I've posted timing diagrams previously.
  • RaymanRayman Posts: 14,646
    @Cluso99 I just tried this card with your CPM code and it shows the same behavior... Works the first time you load, but you have to physically remove the card to get it to work a second time...
  • RaymanRayman Posts: 14,646
    @ersmith What are the chances of getting exFat enabled in FatFs? Compiler doesn't seem to approve of that at the moment...
  • RaymanRayman Posts: 14,646
    To allow easy switch between the Eval board and my custom boards, I created a file called "PropPins.h" that I guess I'll include in source files that needs to use Prop pins... Is there a better way?

    For the FatFS program I then have this, with the attached included.
    #include "PropPins.h"  //RJA defining P2 pin usage here for sd, rtc, etc...
    
    //RJA relocating pin definitions to PropPins.h file
    #define PIN_SS   pin_sd_cs
    #define PIN_MOSI pin_sd_mosi
    #define PIN_MISO pin_sd_miso
    #define PIN_CLK  pin_sd_clk
    
    h
    h
    465B
  • I do something similar in spin (a child object though, rather than an include). It'll have clock setup (in the rare case of a board design out in the wild that has something other than a 5MHz xtal), SD pins, LED pins, etc
  • Wuerfel_21Wuerfel_21 Posts: 5,053
    edited 2020-09-06 23:16
    Yeah, I like the pins-in-child-object style. (Usually I call it platform.spin...) The main upside is that it works in compilers without a preprocessor, but the downside is that you can't do conditional compilation based on it (like linking different objects for different input devices or smth like that)
    With fastspin, child objects also have the advantage of being cross-language compatible...

    For the big game™, I've actually written a very overkill system where the build script generates a platform.spin from a target definition (where the common definitions for related targets (such as VGA vs. TV builds for the same board) can be grouped). Looks a bit like this:
    VENTILATOR_COMMON = {
        audiotype: :stereo,
        input: 'in_ps2_snes',
        player2:true,
        pins: {
            # SPI pins
            SPI_MISO: 21, #DO
            SPI_CLK:  24,
            SPI_MOSI: 20, #DI
            SD_CS:    25,
            RAM_CS:   26,
            # Video pins
            TVPINS: 0b001_0101,
            VGAPINS: 0b10_00001_10_00000_000000_000_0_11111100,
            # Audio pins
            AUDIO_LEFT:  11,
            AUDIO_RIGHT: 10,
            # Input pins
            PS2_DATA: 8,
            PS2_CLOCK: 9,
    
            SNES_CLOCK: 16,
            SNES_LATCH: 17,
            SNES_PLAYER1: 19,
            SNES_PLAYER2: 18,
        },
    }.freeze
    
    target_p1("ventilator-debug",debug: true,
        **VENTILATOR_COMMON,
        tvtype: :tv,
        svideo: true,
    )
    target_p1("ventilator-vga-debug",debug: true,
        **VENTILATOR_COMMON,
        tvtype: :rgb64,
    )
    

    ...and ends up generating a spin file like this: (as you can see, not all settings are reflected here; some are given as preproccessor defines on the command line ("-D TVTYPE_TV -D AUDIOTYPE_STEREO -D HAVE_SVIDEO" for this example, if you care). Some settings also weren't given and thus defaulted (notably, the clock settings))
    OBJ mmap : "memmap"
    '' Platform define file for ventilator-debug
    '' THIS FILE IS AUTO-GENERATED, DO NOT EDIT!!!
    PUB whyisthisevenrequiredaaaaaohwhyohwhy
    CON
    
    '' Clock
    _CLKMODE = XTAL1 + PLL16X
    _XINFREQ = 5000000
    '' Pins
    SPI_MISO             = 21
    SPI_CLK              = 24
    SPI_MOSI             = 20
    SD_CS                = 25
    RAM_CS               = 26
    TVPINS               = 21
    VGAPINS              = 2197815548
    AUDIO_LEFT           = 11
    AUDIO_RIGHT          = 10
    PS2_DATA             =  8
    PS2_CLOCK            =  9
    SNES_CLOCK           = 16
    SNES_LATCH           = 17
    SNES_PLAYER1         = 19
    SNES_PLAYER2         = 18
    '' Other
    PLATFLAGS_VAL = mmap#PLATFLAGS_HAS_PLAYER2
    
    It generates one of these for every defined target in its build directory (which is included as a library dir when compiling spin code). Again, very overkill if you don't already have a very complex build script (which I had, so adding this functionality was trivial)
  • Anyone know how to change the heap size from its default of 4kB in Fastspin C?

    Included documentation below only mentions the HEAPSIZE constant in a top level object file, and I am running a single C file, no Spin2 objects, at least not directly. In any case I've tried adding this to my C file:
    #define HEAPSIZE 100000
    
    and also this
    const HEAPSIZE=100000;
    

    however neither seems to help. What else would I need to do to increase the default heap size?

    Documentation only has this:
    ## Memory Management
    
    There are some built in functions for doing memory allocation. These are intended for C or BASIC, but may be used by Spin programs as well.
    
    ### Heap allocation
    
    The main function is `_gc_alloc_managed(siz)`, which allocates `siz` bytes of memory managed by the garbage collector. It returns 0 if not enough memory is avilable, otherwise returns a pointer to the start of the memory (like C's `malloc`). As long as there is some reference in COG or HUB memory to the pointer which got returned, the memory will be considered "in use". If there is no more such reference then the garbage collector will feel free to reclaim it. There's also `_gc_alloc(siz)` which is similar but marks the memory so it will never be reclaimed, and `_gc_free(ptr)` which explicitly frees a pointer previously allocated by `_gc_alloc` or `_gc_alloc_managed`.
    
     The size of the heap is determined by a constant `HEAPSIZE` declared in the top level object. If none is given then a (small) default value is used.
    
    Example:
    ```
    ' put this CON in the top level object to specify how much memory should be provided for
    ' memory allocation (the "heap"). The default is 4K on P2, 256 bytes on P1
    CON
       HEAPSIZE = 32768 ' or however much memory you want to provide for the allocator
    
    ' here's a function to allocate memory
    ' "siz" is the size in bytes
    PUB allocmem(size) : ptr
      ptr := _gc_alloc_managed(size)
    ```
    
  • Rayman wrote: »
    @ersmith What are the chances of getting exFat enabled in FatFs? Compiler doesn't seem to approve of that at the moment...

    EXFAT seems to require 64 bit integers, so that's not going to happen for a while.
  • ersmithersmith Posts: 6,053
    edited 2020-09-07 11:11
    rogloh wrote: »
    Anyone know how to change the heap size from its default of 4kB in Fastspin C?

    Included documentation below only mentions the HEAPSIZE constant in a top level object file, and I am running a single C file, no Spin2 objects, at least not directly. In any case I've tried adding this to my C file:
    #define HEAPSIZE 100000
    
    and also this
    const HEAPSIZE=100000;
    

    Try:
    enum { heapsize=100000 };
    
    I'd like to get either the #define or const form to work too, but for technical reasons these are hard for the compiler to recognize :(. Also note that in C the constant must be lower case. That'll be fixed in github soon so that either all upper or all lower will work.

  • RaymanRayman Posts: 14,646
    I was wondering about heapsize too ...
    Thanks. Looked all over but couldn’t find anything...

    How do you manage garbage collection in FlexC?
  • ersmithersmith Posts: 6,053
    edited 2020-09-07 11:32
    Rayman wrote: »
    I was wondering about heapsize too ...
    Thanks. Looked all over but couldn’t find anything...

    How do you manage garbage collection in FlexC?

    If malloc() is unable to allocate memory, then garbage collection is triggered and any managed pointers previously allocated but no longer referenced are freed. Then the malloc() is re-tried.

    By default memory returned by malloc() is *not* managed, so in ordinary C code the garbage collection will do nothing. However, you can explicitly mark a pointer returned by malloc() as manged by calling _gc_manage(ptr). Or, you can allocate the memory as managed from the start by using _gc_alloc_managed(n) instead of malloc(n). In both of those cases the pointers may be freed by garbage collection if no references to them are found (there are no 32 bit values in the heap or stack that point to the start of the blocks).

  • ersmith wrote: »
    Try:
    enum { heapsize=100000 };
    

    Thanks Eric that worked. :smile: One thing I found is that the resulting binary size using the heap method is still large. I was hoping if I was to allocate a large frame buffer structure on the heap instead of statically via an array it would not contribute to the binary image size, but it still seems to. I guess you need to include this heap space in the binary for some reason (to guarantee it will be available?).
  • RaymanRayman Posts: 14,646
    I started looking at the warning list and fixing those up...

    Noticed that I redefined the same int several times...
    Shouldn't that be error? In Visual C++ that gives an error...
  • RaymanRayman Posts: 14,646
    Are true and false not defined in FlexC? Is that a C++ only thing?
  • Rayman wrote: »
    Are true and false not defined in FlexC? Is that a C++ only thing?

    I think you need: #include <stdbool.h>
  • rogloh wrote: »
    ersmith wrote: »
    Try:
    enum { heapsize=100000 };
    

    Thanks Eric that worked. :smile: One thing I found is that the resulting binary size using the heap method is still large. I was hoping if I was to allocate a large frame buffer structure on the heap instead of statically via an array it would not contribute to the binary image size, but it still seems to.

    Yes, the current implementation just allocates a static array which is part of the binary image. Eventually I'd like to remove this, but it's not a big priority: it won't change the memory footprint, just the disk storage footprint.
  • Rayman wrote: »
    I started looking at the warning list and fixing those up...

    Noticed that I redefined the same int several times...
    Shouldn't that be error? In Visual C++ that gives an error...

    It depends on exactly what you're defining. It could be a bug in fastspin, but it could also just be hitting parts of the standard that are compiler dependent. You can always use -Werror to turn those warnings into errors.
  • RaymanRayman Posts: 14,646
    I had three instances of versions of lines like this:
    	int pr = f_puts("First string in my file.  What great text for a file!\n", &Fil);
    

    So, pr was being defined 3 times in the same function.
    Seemed to work fine anyway, but should probably be an error...
  • RaymanRayman Posts: 14,646
    Minor question:

    Visual Studio is telling me that defines like this:
    #define pin_sd_miso 44
    

    Should be changed to constant expressions. But, that doesn't seem to work...
    Is that possible in FastSpin?
  • Wuerfel_21Wuerfel_21 Posts: 5,053
    edited 2020-09-08 14:46
    Rayman wrote: »
    Minor question:

    Visual Studio is telling me that defines like this:
    #define pin_sd_miso 44
    

    Should be changed to constant expressions. But, that doesn't seem to work...
    Is that possible in FastSpin?

    a) constexpr is a C++ feature (roughly equivalent to CON in spin (unlike const, which does not guarantee compile-time evaluation)). I don't think fastspin supports it, but constexpr variables should(tm) be trivial for Eric to add. (as opposed to constexpr functions)
    b) WTF Visual Studio? Why is it doing code style suggestions like that by default?
    c) You should definitely change it to
    #define pin_sd_miso (44)
    
    to avoid weird syntax issues
  • Rayman wrote: »
    Minor question:

    Visual Studio is telling me that defines like this:
    #define pin_sd_miso 44
    

    Should be changed to constant expressions. But, that doesn't seem to work...
    Is that possible in FastSpin?

    No, as @Wuerfel_21 said that's a C++ only feature and not implemented in fastspin. Anyway, Visual Studio's suggestion is rather dubious, so I wouldn't worry about that.
  • RaymanRayman Posts: 14,646
    edited 2020-09-09 21:43
    sd reading with FatFs using sdmm.cc was too slow to playback stereo 44.1 kHz 16 bit PCM files.

    Surprisingly, I was able to change the delay to make it work:
    #define PAUSE() (_waitx(16))//80))
    #define SHORTPAUSE() (_waitx(8))//40))
    

    You'd think this is way too fast, but it works...
    Also, this should be linked to the clock speed, but this is with the clock at 300 MHz
    Might have to get the scope to see how fast it's actually going at...

    Actually, I can tell by the assembly that this is going to be <15 MHz at 300 MHz clock.
    Also, assembly looks very similar to fsrw for P2 where there's a note that that pause() delay can be as low as 14 with 320 MHz clock.
  • Thanks, @Rayman, I'll change the default delays for sdmm.cc in the fastspin source code to match what you've found.
  • evanhevanh Posts: 15,916
    edited 2020-09-10 11:42
    Hmm, // usually means a comment to the compiler but what does the preprocessor do with that? Is it still a comment and not part of the #define ?

    Never mind, I googled it and found // is still a comment in this case. Heh, last time I looked up any C language, Google didn't even exist.

  • evanhevanh Posts: 15,916
    Next question: What does the 80)) and 40)) mean?

  • RaymanRayman Posts: 14,646
    That is the original values, now commented out
  • RaymanRayman Posts: 14,646
    I just dug up a jpeg decoder that worked under GCC on P1...

    It seems to choke on initializing the 2D int array in the attached...

    Gives this error: >fft.c:3: error: wrong kind of initializer for array type

    Should one be able to declare a 2D array like this:
    int fft[64][64]={
    
       +8192,   +8192, ...  (lots of more integers here)
    
    C
    C
    37K
    FFT.C 37.1K
  • It's probably expecting braces around every group of 64 values. If you specify -Wall with GCC it generates a warning about this. Try the attached file.
    c
    c
    37K
    fft1.c 37.3K
Sign In or Register to comment.