Global Var Set (or not set) w/o Me Knowing
DavidZemon
Posts: 2,973
I have a multi-threaded app running with two devices using my SPI routine as written in PropWare. Something seems to be wrong (maybe I'm just missing a keyword?) because despite having never yet called 'SPIStart(...)', my global variable g_spiCog is not equal to -1. At the top of spi.c is a line:
I can only think of two possible things: 1) the variable is never initialized, and 2) Some pointer has gone haywire and is overwriting g_spiCog. But I use pointers sparingly and carefully (and never malloc) so I find that hard to believe.
If more context is needed, let me know. I can upload the full project to github or just snippets.
Thanks,
David
static int8_t g_spiCog = -1;Here is a link directly to spi.c. I've searched the file; there are exactly 5 occurrences of the variable:
- Line 28: Initialization
- Line 60: Assignment by cognew(...)
- Line 88: Stopping the cog
- Line 89: Assigning -1 after stopping the cog
- Line 96: Checking value in comparison for SPIIsRunning()
I can only think of two possible things: 1) the variable is never initialized, and 2) Some pointer has gone haywire and is overwriting g_spiCog. But I use pointers sparingly and carefully (and never malloc) so I find that hard to believe.
If more context is needed, let me know. I can upload the full project to github or just snippets.
Thanks,
David
Comments
Edit: Never mind. I see you aren't even calling the start function. I guess seeing more of the code might help. Can you post a zip file?
In the mean time, I'll post a short overview of the program:
There are three cogs
Cog 1 - Segway.c, Motors.c
Runs main(), PID loop, and outputs PWM signal to two GPIOs
Cog 2 - Segway_backend.c, spi.c, mcp300x.c, l3g.c, PropWare.c... I think that's it
controls SPI assembly cog, reads values from two devices via SPI, runs a weighted, rolling average filter on the inputs
Cog 3 - spi_as.S
GAS cog running SPI routines
All of the SPI code (along with mcp300x.c, l3g.c, PropWare.c are in PropWare here - same git repo as spi.c posted above)
The first step in Segway_backend.c is calling the setup() function. The first step of setup() is to initialize the L3G module with L3GStart(). First step of L3G is to check if SPI has already been initialized and, if not, initialize it. Therefore, if we ignore the long string of function calls, the first thing that this cog (Segway_backend.c) does is check if SPI is running, which can be done by calling "SPIIsRunning". Source code for which is very simple:
Since printf/__simple_printf is acting very bizarre, I'm printing values to my 16x2 LCD such as: It should return "false" and print out as '0'... but it doesn't. '1' every time.
To be absolutely sure that something weird isn't calling SPIStart() by accident, I've thrown a simple snippet at the top of the SPI assembly cog: With the lights not blinking, I know SPIStart() has not [successfully] run.
Thanks. I'm pretty fuzzy on the various keywords. I know what they do but am not 100% sure where they should and should not be placed.
But I was wrong: you have the variable declared as a SIGNED 8-bit integer (int8_t -- really a char) of value -1 which gets sign-extended to an integer with value -1 so the comparison should be correct and should have made the function return 0 (false, not running).
I don't have SimpleIDE on the system I'm working on at this moment but you might want to try casting the -1 in the comparison to the same type as the variable just to make sure. Try:
You may also want to make the g_SPIcog variable a volatile but I don't think that should make a difference: volatile only prevents consecutive fetches of the same variable in the same function to be optimized away and that doesn't happen at all in your code as far as I can see.
If the above modification makes it work, we may need to take a good look at the assembler output of the original version, I wonder if there's something wrong with the sign-extension code. I can't really think of anything else.
A simple test to see if the sign-extension code in the compiler works correctly would be:
===Jac
I tried increasing the stack size to 1024 and it's making no difference. I'll leave it there for now to be sure. And to answer your question David, not quite. There are four arrays, each are currently 32 vars long. Two of them are 2-byte vars and two are 1-byte. 32*(2*2 + 2*1) / 4 = 48.
Yes, it works.
Function sprintf is part of the standard C library. This is a good reference: http://publications.gbdirect.co.uk/c_book/chapter9/formatted_io.html
Of course it will grow just like standard printf. Use the alternative Simple Library print, scan, and friends functions for manageable code size.
or could it be in the makefile???
Example please?
What Propeller-GCC distribution are you running? I.E. $ propeller-elf-gcc -v
sprintf yields a bunch of jargon, as if it's dropping a null-terminator or something. I've made the buffer very long but it doesn't matter. Again, I'll post full details when I get back.
Thanks,
David
The error has something to do with multi-core. I'll continue trying to narrow it down some more.
From this:
To this: