I see strange behavior trying to combine C ++ and C code. I am using a C ++ class in C code using static_cast with void* for the class. This is done as follows.
//C++ code void* newCSPI() { return static_cast<void*>(new XSpi); }
This function is declared in the header as follows.
//C++ code extern "C" void* newCSPI(void);
Then I can call the C ++ functions in the C code. An example of the implementation of other functions is given below.
This function is also declared as extern "C" in the header.
This casting function is implemented as follows.
I am trying to execute the following block of code. All this is successful, except for the last line.
//C code void* spi = newCSPI(); /* Select device. */ selectCSlave(spi); /* Transfer data over SPI*/ transferC(spi, MOSI, MISO, ByteNum); // It breaks here // /* Transfer data over SPI*/ transferC(spi, MOSI, MISO, ByteNum); /* Un-select device. */ deselectCSlave(spi);
During the second call to transferC(spi) pointer somehow changes the value. Inside the translation function, the pointer still has the same meaning. Inside the function that it distinguishes, the value changes. The implementation is exactly the same as the first transferC(spi) , which works. There is no code between these two calls.
I do not understand why the meaning will suddenly change. What am I missing here?
Code deselectCSlave() and SPI::deselectSlave(void) :
void deselectCSlave(void* Cspi) { static_cast<SPI*>(Cspi)->deselectSlave(); } void SPI::deselectSlave(void) {
0x00 and 0xFF are the values ββthat are written.