Checking volatile value of address in C++ - c++

I'm trying to implement a mailbox write for the Raspberry Pi. According to the info I found, I can write to address 0x2000B8A0 when mailbox is empty, meaning 0x2000B898 has not the last bit set. I wrote it like this:
uint32_t *mailbox = reinterpret_cast<uint32_t*>(0x2000B880);
while((mailbox[6] & 0x80000000) != 0);
mailbox[8] = value + channel;
But the disassembly shows that the value at mailbox[6] is only loaded once, before the loop, then it just repeats the check with that one value.
I could not find a solution because I don't even know the proper words for this problem. I'm sure it's simple but googling brought nothing for this special case.

Answer lies in title of your question.
You should use the following:
volatile uint32_t *mailbox = const_cast<volatile uint32_t *>(reinterpret_cast<uint32_t*>(0x2000B880));
This will make sure the value is loaded each time in your loop. If you see any application not responding, consider adding some sleep or delay or yield in while.

Related

Change variable value of the already running program

I think it is impossible, but I should try.
I have a program, that is doing a physical simulation and recording frames to files. It is recording until the breakup value is not achieved, like:
int counter=0; //global variable
void SomeFunction()
{
...
if(counter == 400) //written exactly by this way, i.e. 400 is not a variable, just a number
PostQuitMessage(0);
else
MakeScreenshot();
counter++;
...
}
The problem is that I forgot to change if(counter == 400) to if(counter == 1000) and now program will be finished with 400 frames, although I need exactly 1000.
I can’t just recompile the program because calculations are very heavy and the program is already running for 2 days, I can’t wait.
It is very important for me, is there any way to change the if statement, or exactly the variable value during the program running?
The only hope I have, is, as far as I remember there was programs that could like change money/health/another stuff in games, and there user exactly could search a variable by value, and change it
Currently it is on about 200-300 frame, I have so little time to fix it.
You can use the Windows functions ReadProcessMemory and WriteProcessMemory.
But you need to determine the position of the value 400. Looking for the value in the running program can be done with the above functions. Depending on the size of the constant, it should be either a USHORT or an UINT.
So you can use the following steps:
Look for the value in a second instance
Isolate the surrounding OpCode sequence.
For example, a cmp xxx, 400
Look for this OpCode sequence in the running executable to find the unique location and replace the crucial value by 1000.
This can be called "hot-patch". It's how simple in-memory cheats do work and requires root privileges.
I downloaded Cheat engine and found address of the counter variable by its value. Through all addresses, the address I need was colored in green.
I changed the value stored at that address to 401, and now all is ok.

Arduino substring doesn't work

I have a static method that searches (and returns) into String msg the value between a TAG
this is the code function:
static String genericCutterMessage(String TAG, String msg){
Serial.print("a-----");
Serial.println(msg);
Serial.print("b-----");
Serial.println(TAG);
if(msg.indexOf(TAG) >= 0){
Serial.print("msg ");
Serial.println(msg);
int startTx = msg.indexOf(TAG)+3;
int endTx = msg.indexOf(TAG,startTx)-2;
Serial.print("startTx ");
Serial.println(startTx);
Serial.print("endTx ");
Serial.println(endTx);
String newMsg = msg.substring(startTx,endTx);
Serial.print("d-----");
Serial.println(newMsg);
Serial.println("END");
Serial.println(newMsg.length());
return newMsg;
} else {
Serial.println("d-----TAG NOT FOUND");
return "";
}
}
and this is output
a-----[HS][TS]5132[/TS][TO]5000[/TO][/HS]
b-----HS
msg [HS][TS]5132[/TS][TO]5000[/TO][/HS]
startTx 4
endTx 30
d-----
END
0
fake -_-'....go on! <-- print out of genericCutterMessage
in that case I want return the string between HS tag, so my expected output is
[TS]5132[/TS][TO]5000[/TO]
but I don't know why I receive a void string.
to understand how substring works I just followed tutorial on official Arduino site
http://www.arduino.cc/en/Tutorial/StringSubstring
I'm not an expert in C++ and Arduino but this looks like a flushing or buffering problem, isn't it?
Any idea?
Your code is correct, this should not happen. Which forces you to consider the unexpected ways that this could possibly fail. There is really only one candidate mishap I can think of, your Arduino is running out of RAM. It has very little, the Uno only has 2 kilobytes for example. It doesn't take a lot of string munching to fill that up.
This is not reported in a smooth way. All I can do is point you to the relevant company page. Quoting:
If you run out of SRAM, your program may fail in unexpected ways; it will appear to upload successfully, but not run, or run strangely. To check if this is happening, you can try commenting out or shortening the strings or other data structures in your sketch (without changing the code). If it then runs successfully, you're probably running out of SRAM. There are a few things you can do to address this problem:
If your sketch talks to a program running on a (desktop/laptop) computer, you can try shifting data or calculations to the computer, reducing the load on the Arduino.
If you have lookup tables or other large arrays, use the smallest data type necessary to store the values you need; for example, an int takes up two bytes, while a byte uses only one (but can store a smaller range of values).
If you don't need to modify the strings or data while your sketch is running, you can store them in flash (program) memory instead of SRAM; to do this, use the PROGMEM keyword.
That's not very helpful in your specific case, you'll have to look at the rest of the program for candidates. Or upgrade your hardware, StackExchange has a dedicated site for Arduino enthusiasts, surely the best place to get advice.

How can I read undefined number of bytes from com port, using boost?

I'm trying to write and read data from com port. I' using
write(*ioboard.port, buffer(cpayload, cpayload.size()));
for writing and i have some troubles with receiving answer.
I tried different variants
int m = read(*ioboard.port, buffer(answer, 1));
int m = read(*ioboard.port, buffer(answer, 1024));
int m = ioboard.port->read_some(buffer(answer));
In first case I received answer after first call of write-read pair and no answer after second: fanction read glitching. In second case I didn't receive answer after first call: again due to the read function. Third works the same way as first. So now I need reboot my devise to send it two commands!
How should I read, if I know maximum data size, but do not know how many bytes, I'll get in particular way.
It happend because nothing was in that port. I have no deep understanding of what happend, but I solved this problem for now by creating symblink to /dev/ttyACM0 and connecting to it. Don't understand why so happened - seems that only discriptor was changed. – voronwe
If you are okay with using Qt tool kit try doc.qt.io/qt-5/qserialport.html

Infinite loop during debugging

I am working with a STM32 eval2 board and trying to debug it. It used to work fine, and I haven´t changed anything, but for the last week or so I am always getting stuck in this loop while I am in debugger mode, but when I am not, the program runs fine.
while(!__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))
{
if(__HAL_SD_SDIO_GET_FLAG(hsd, SDIO_FLAG_RXDAVL))
{
*(tempscr + index) = SDIO_ReadFIFO(hsd->Instance);
index++;
}
}
I even tried running the sample project code provided for the board by ST, did not change anything about it, and I am stuck in the same while loop in their code as well.
Does anybody know what I am doing wrong here? It doesn´t make sense because nothing changed.
The errors that are defined by the variables in the while loop are (respectively):
Received FIFO overrun error
Data block sent/received (CRC check failed)
Data timeout
Data block sent/received (CRC check passed)
Start bit not detected on all data signals in wide bus mode
and it looks like in this while loop it is getting stuck in the if statement for a "Data available in receive FIFO" flag, if that makes sense. I cannot step over that if statement.
I am using keil v5 and programming in c++
Well, I have been struggling with this for a week and almost right after I posted this I figured it out.
I had the SD card in, and for some reason taking it out fixed it. So I will leave this in case anyone else ever has this stupid problem.
Well, I have been struggling with this for a week and almost right after I posted this I figured it out.
I had the SD card in, and for some reason taking it out fixed it. So I will leave this in case somebody else has this stupid problem.

Using SetKeyboardState along with GetKeyboardState in C++

I don't know how to write a good question here, but, basically, does anyone know where I can possibly find some C++ source code using these to actually set keyboard state? For some reason using it the way MSDN does on Windows 7 doesn't do...anything at all.
Basic code:
PBYTE keyState;
GetKeyboardState(keyState);
...
// Later on when I need to set the keyboard state (key pressed etc) back to original:
SetKeyboardState(keyState);
and ... nothing happens :(
From:
http://www.gamedev.net/community/forums/topic.asp?topic_id=43463
First off, GetKeyboardState() would be the wrong function to use because as Windows has a chance to process keyboard messages (whether you want it too or not) it updates the results of the keyboard's state for the next call to GetKeyboardState().
Here's a little function that I use to get the status of the keyboard's keys. Be carefull though, depending on how fast your main loop is, it may cause problems if you aren't expecting it.
You need to keep track of whether or not a specific key was pressed the last time you called the ReadKeyboard() function. If your loop polls the keyboard 30 times a second, then pressing a key once probably causes the key to be flagged 3 or 4 calls in a row. Rather confusing sometimes. Just thought I'd mention it.
void ReadKeyboard( char* keys )
{
for (int x = 0; x < 256; x++)
keys[x] = (char) (GetAsyncKeyState(x) >> 8);
}