|
I am programming regular C for an embedded ARM processor. I need to declare an array with volatile elements inside it. The location of the array is constant, it's only the elements inside the array that can change and they are, like I said, volatile. What is the correct way to declare it?
A. uint8_t volatile UARTrxBuffer[10];
or
B. volatile uint8_t UARTrxBuffer[10];
or
C: volatile uint8_t volatile UARTrxBuffer[10];
modified 7-Dec-16 9:37am.
|
|
|
|
|
The general answer is B but in your case there is no difference between A and B, and C would generate a compiler error.
It can be better explained when having a dynamically created array:
uint8_t* volatile my_data1 = (uint8_t* volatile)malloc(10);
volatile uint8_t* my_data2 = (uint8_t*)malloc(10);
volatile uint8_t* volatile my_data3 = (uint8_t* volatile)malloc(10);
With the volatile pointers, the casts must also contain the volatile keyword.
But with arrays, the pointer can't be re-assigned so that version C is not allwed and the position of the volatile keyword does not care.
|
|
|
|
|
I am confused what Jochen is saying to you but I will say nothing you nor Jochen have code will make the items within the array volatile.
First lets cover the basics volatile is an optimizer instruction it has very little to do with the C language as such it is also HIGHLY vendor sensitive. So the first thing you always need to do is check your vendor tools and specification.
My next warning is to always put the volatile between the type and the name not at the front as people love to do. Things like the arm compilers are notorious with volatile in the front of the type and if you use it in macros you will end up with it on the wrong thing. I covered this in a recent article on baremetal with the RPi but Arm covers this in there documentation
ARM Information Center[^]
You see the format, that is not just the arm standard it is the standard I suggest you use as most embedded compilers get it right
type volatile * const ptr;
| | | | |
| | | | +------> ptr is a
| | | +-----------> constant
| | +---------------> pointer to a
| +---------------------> volatile
+---------------------------> integer/long/etc
Almost all vendor tools will fail to do with volatile what the full C specification says because the specification isn't written by embedded processor vendors and they get little say in it. Some of the junk that is in the C specification around the word volatile is ignored because it doesn't make sense in micro-controllers. The scary stat is 96% of vendor tools fail the full C implementation (Testing result reference :[^])
So lets get down to specifics the volatile must also be on any item within the array putting a volatile on the array itself will not work almost any compiler. The compiler doesn't see "blocks" of things as volatile it only sees things it is accessing in the mode it is accessing them as volatile.
The fact you want a volatile inside a malloc would tend to indicate you are playing around with a DMA controller so I will reference hardware access here. If this is just shared multitask memory nothing changes it's just simpler to understand.
This is a basic hardware register definition example for something like a DMA controller
typedef uint32_t volatile rwreg32;
typedef uint32_t const volatile readreg32;
I can then define an array of them and the volatile will be on each and every array item because when the C compiler picks up an array entry they are a type which includes the word volatile. Note if you accessed the enteries as words or bytes those accesses would not be volatile, it is that specific.
rwreg32* SomeArray = (rwreg32*)malloc(10 * sizeof(rwreg32));
You notice there is no word volatile on the array itself that is pointless and won't work unless you were passing the whole array to something. The same situation exists with structs if you wish to use them as representations of hardware. The volatile must be on each item in the struct AS IT WOULD BE ACCESSED and even down to bit packs if that is how you access them. Here is a small random sample from an OTG chipset struct
volatile const struct {
volatile unsigned SpaceAvailable : 16;
volatile unsigned QueueSpaceAvailable : 8;
volatile unsigned Terminate : 1;
volatile enum {
InOut = 0,
ZeroLengthOut = 1,
PingCompleteSplit = 2,
ChannelHalt = 3,
} TokenType : 2;
volatile unsigned Channel : 4;
volatile unsigned Odd : 1;
} __attribute__ ((__packed__)) Status;
You see how they have put volatile on each entry in the struct as they would be accessed, and as the struct itself may be accessed as a whole it is put on the struct itself.
So basically however you access the hardware must be seen as a volatile type, just putting volatile on something doesn't make it volatile as a whole if you don't access it that way. So even declaring something like a register as volatile doesn't work if you access the register as bits. Many micro-controllers have bit operations they can do on registers and special hardware and that is what those who write the C specification seem to not understand time and time again. Some vendors will allow you just to mark the register as volatile but many more require you to mark each bit within the register as volatile if you use bit operations. That is why many vendors tools have a more simple definition called SFR for special function register where they do the volatile on everything for you.
So this is why the correct way to do this is however your C tool vendor says to do it because the C standard may be of little help on anything more than a simple shared memory use of the word. In 4 revisions of the C standard the committee hasn't even managed to give us a way to define a write only register which almost every micro-controller has, so don't expect them to deal with volatile anytime soon.
If you are on something like an ARM6+ it gets worse the C standard has no idea what to do with Out-Of-Order-Execution processors like the Arm Most of the new fancy phones run Weakly-Ordered CPU's and if you are coding for them you need to understand how to deal with it and a few more tricks you won't find in the C standard. I bring it up because on those you don't even write directly to the hardware (there is a cache between you an it) so the idea of volatile gets even more convoluted.
In vino veritas
modified 7-Dec-16 20:45pm.
|
|
|
|
|
created a Dialog base Application with Tree control..Now i had Selected some leaf item from my tree control ,And after that click on to open any dialog then the Tree Leaf unselected.
Now i want tree item should be selected after closing the dialog.
|
|
|
|
|
The selected item should remain selected after the second dialog terminates.
|
|
|
|
|
Thanks you so much for reply...
Now I tell u full story. when i drag the item into grid it also add into TreeView.Now suppose there are 3/4 item into editor and u select one the parallely tree item will also select.so now this time both the thing are selected but as u will click some where else like open an dialogbox and close it then tree item will be selected but partially it`s colour will be Gray..now my problem is that Gary colour. I need it Fully selected Item.
Please tell me what to do...
modified 5-Dec-16 7:40am.
|
|
|
|
|
If you want your item to be highlighted (blue) it must not only be selected, but the containing control must have the focus. Read up on "set focus".
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
I piece of code that is executed by 4 threads. I want to Serialize it one at a time
So I Create a Mutex handle = CreateMutex(NULL,FALSE,NULL);
I observe that the HANDLE its not NULL
Then I "dwWaitResult = WaitForSingleObject(sysblk.single_thread,INFINITE)"
(I am running this under VS debugger and have a few breakpoints before I do the ReleaseMutex)
Then finally the first thread gets to the end of the serialized code.
I do a "ReleaseMutex(sysblk.single_thread)" it returns 1 however
When I look back at the VS Threads Window and click on each the 3 threads there is an inverted arrow at the WaitForSingleObject indicating
that none of the threads were signaled
Thanks
|
|
|
|
|
|
Sorry that was a typo as I typed the code and didn't cut paste it was meant to be
WaitForSingleObject(handle,INFINITE); and ReleaseMutex(handle);
Let me ask question after the first thread goes thru the WaiForSingleObject it turns from
signaled to non-signaled then if more then one thread is waitting when the ReleaseMutex is
executed it lets only ONE thread at a time to process while the other remain waiting till
the next ReleaseMutex correct ?
|
|
|
|
|
That should be the way is works. Each thread should acquire the Mutex (i.e. lock it) in order to protect the data. When they finish the critical section they should Release it. The point being that you only want one thread through this path at any one time.
|
|
|
|
|
Sorry for the delayed response but I have been very busy at work
But can Multiple threads wait on the Mutex with the WaitForSingleObject
And then which is released with the ReleaseMutex the one waiting the longest
|
|
|
|
|
I'm not sure, you would need to run some tests to find out. But in a properly designed application it should not matter, mutexes are not supposed to be FIFO queues.
|
|
|
|
|
|
In the Microsoft example there are 2 threads so ..
1) thread gains ownership of the signaled object and the second waits
In may case they are 4 so If thread 1 gains ownership do threads 2 - 4 waits
if so When the ReleaseMutex are all released or only one at a time
if so Which one
|
|
|
|
|
ForNow wrote: do threads 2 - 4 waits
Yes.
ForNow wrote: When the ReleaseMutex are all released or only one at a time
It depends on the scheduler. Thread scheduling is a very interesting topic in OS design, and the rules can vary from platform. But a general rule is that the waiting thread with the highest priority runs. However some OSs throw in a bit of priority inversion on occasion to free up any deadlocks.
Within the same priority group you might decide on say, 'last thread run', or 'first thread waiting' to decide which to activate and give the CPU to. A bit like FIFO, LRU LIFO etc, there are a lot of designs.
The important point though is that the OS does this for you so no need to worry about it. Use the example from Microsoft and all will be well.
|
|
|
|
|
If all the threads are released it is a problem
|
|
|
|
|
they arent, so you are OK.
|
|
|
|
|
i need help for this project. I would glad if someone could help me with my concern.
Create in C/C++ client and server application for UNIX operating system using BSD
sockets (located at eva.fit.vutbr.cz or merlin.fit.vutbr.cz) which will provide directory
service (small LDAP) .
• Final programs should be compiled into filename called client and
server.
• Create makefile which can be used for automatic compilation of source
files by typing make statement in UNIX shell.
• All errors should be printed on stderr.
• Directory database will be presented as text file (CSV format) with
following syntax:
• “id,login,surname,firstname,faculty” . Each entry (line) is terminated by
\n character.
|
|
|
|
|
We will not do your homework.
|
|
|
|
|
|
We do not do homework, assignment help, because homework is assigned to check how much you have learnt and where you lag in programming.
All of those steps can be also easily answered if you just try to find them on Google.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
Is there someon that knows a good tutorial for a 2d game in c++.
I searched for manny tutorials vut didnt find a good one.
|
|
|
|
|
In any case, game development in C++ will be tough, and if you are a beginners don't even bother.
A good one, if you mean the one that builds everything for you, won't be available easily. You need to learn a lot of things them merge them. JavaScript, C# etc. have engines that can supports 2D game development easily and in simple code as compared to C++. So, look into them.
2D breakout game using pure JavaScript - Game development | MDN[^]
Unity - 2D Game Development Walkthrough[^]
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|