Having customizable setting in your PIC program is easy. Just define it as a constant and let XC8 handle everything else. If you need to change it during runtime there is bunch of code out there dealing with EEPROM access. But what if you want to store something in PIC without EEPROM?
In my case I needed to store two settings: Counter and Unit. Hardware device was already built around PIC without EEPROM so there was no choice but to use self-programming feature that almost all newer PICs share.
Idea is simple. Reserve some space in program memory by making some data constant (e.g. const char mySetting = 3;). Every time you access this variable PIC will return what it had stored there at time of programming. Our trick is to change those values by reprogramming code as it runs.
First complication comes from fact that you cannot just write in flash willy-nilly. Before each write you need to erase whole block of (usually) 64 bytes. Unless you are willing to write very complicated code that also means that you will always need to occupy whole block regardless of how many bytes you want to actually store.
Second caveat is that you can only erase whole block. That means that you cannot let compiler decide upon location of your data. You must select fixed location and that might give you :0: warning: segment "__SETTINGS_PROGRAM_text" (200-23F) overlaps segment "intcode" (8-273). Fortunately solution is really easy - just move your constant somewhere else. Of course that implies that you have enough free flash memory laying around.
Third issue might be that this operation typically takes few milliseconds which is ages in microcontroller terms. To make things worse interrupts must be disabled during almost all that time. Any precise time keeping is out of question.
However, if you wiggle your way around all these obstacles, you get nice storage for times when you just cannot afford EEPROM for one reason or another.
It would be best if you would keep this code in separate file (e.g. settings.h) and just include it from your main program file instead of pasting all this directly.
PS: If your program does not use interrupts change _SETTINGS_REENABLE_GIE_AFTER_WRITE to 0.
PPS: Do notice that flash memory was not intended as this kind of storage medium. Writing is much slower than EEPROM (because of erase) and endurance is much lower. Use it only in dire straits and be gentle.
PPS: This code is for PIC18F* series of micro-controllers. Same general principle works on PIC16F* but names of some registers might differ and additional NOPs might be required. Do check instruction manual.
Starting December 6th 2012 there is no more free Google Apps. If someone wants Google to handle mail for his domain, he can now expect hefty yearly charge of $50 per user.
All current users of free Google Apps should probably start searching for new home since I don’t expect this status quo to last. My assumption is that existing users will start getting “pay up or get lost” mails pretty soon. For family of four this means $200 if you want to continue using it or dealing with account migration. Lets hope that Google’s “Don’t be evil” motto will prevent this extortion-style scenario from happening.
This unfortunate turn of events just comes to show how living in cloud leaves you in mercy of your hosting company. There is no such things as a free lunch.
If you are wondering what ITU can offer if it takes over control of Internet, wonder no more. Great example of moronic and damaging decisions is already here - Y.2770 Requirements for deep packet inspection in Next Generation Networks.
This standardizes the way how government can spy any traffic that goes over your favorite telco. If possible this standard also keeps door open to decrypt user’s traffic “in case of a local availability of the used encryption key(s).”
Whoever thinks that this is good idea and trusts his government is complete moron.
My own government was caught some time ago (translation) just adding unrelated phone numbers to existing warrants. End result is that this practice was deemed quite appropriate. USA government is no better with its whole NSA activities. Lets not even go into areas here warrant are not required. And there is plenty of other governments with even lower standards.
This standardization will allow them to use one kind of equipment in each telco thus reducing their cost of tracking a citizen. Forcing a company to give them SSL keys in order to stop users from encrypting traffic is next step. Hell, I can bet that some countries are already doing that.
Quite a lot of Internet security relies on encrypted things staying encrypted. Once that encryption is taken away all you think of private is not so private anymore. Each time you access your bank in order to make a transaction, someone else is peeking over your shoulders.
And “honest people have nothing to hide” is bullshit. How much time do you think it will pass until some security hole is discovered on those systems?
Same standardization that makes live easy for government will also make it a sweet delight for criminals. Just filter through minute of traffic and you will have enough information to get into thousands of accounts. I will not even cover what single rogue agent can do e.g. if he finds that his wife is cheating him. He can gather/falsify enough information to get other guy in real trouble. And I am being optimistic, real-life scenarios will be much darker.
And this is a mess that ITU is already capable of. Imagine ideas that will spring into their mind if they gain control over Internet. Orwell was right, he just missed the year.
For one PIC microcontroller program I had to use assembly code for some critical timing. Rest of code was just plain XC8 C code and everything compiled (and performed) without any issue. So I decided to share code with another project of mine.
Suddenly I got bunch of warnings all saying almost same thing: wait.h:19: error: fixup overflow referencing psect bssBANK1 (0xA2) into 1 byte at 0xB3E/0x2 -> 0x59F (dist/default/production\Source.production.obj 136/0x3E). Program did work (fixup did what its name says) but each compilation would pepper output with warnings - unacceptable.
It was easy to find a culprit:
unsignedchar wait_asm_W;//copy of W...#asm
BANKSEL _wait_asm_W
MOVWF _wait_asm_W
...#endasm
Address of an variable was used inside assembly code. Original program had this variable in bank0 and thus no warning were issues. This project had slightly different allocations and variable ended up in bank1 (e.g. 0xA2). Since assembly instruction can only deal with 7-bit registers, address was outside of allowed range.
Once solution is forcing variable into bank0:
bank0 unsignedchar wait_asm_W;//copy of W...#asm
BANKSEL _wait_asm_W
MOVWF _wait_asm_W
...#endasm
This works but only after change to project’s compiler flags (Properties, XC8 global options, XC8 compiler, Option categories: Optimizations, Address qualifiers: Require). Just thought of having to remember to change project properties each time I reuse this code made me wince. Yes, it was a solution but not what I wanted.
What I wanted is way to transform offending value 0xA2 to non-problematic 0x22. Quick bit-masking solved that issue and warnings were gone:
unsignedchar wait_asm_W;//copy of W...#asm
BANKSEL _wait_asm_W
MOVWF _wait_asm_W &0x7F...#endasm
This December International Telecommunication Union is asking for keys to the Internet. Finally there will be central body to control and help expand the network we all use.
And I am scared.
When I look the trend of various countries locking down on Internet privacy and usability in the name of “security” I can just imagine ITU making it mandatory for all countries to implement switch in order to prevent cyber attacks. And then some content filtering of naughty material “for the sake of children”. One tiny step removed I can see countries suppressing criticism and all content that they do not care about.
I do not trust my (or any other government for that matter) in making wise choice. Government will by design try to keep it self in power regardless of what it takes. And each government having same input in ITU is just recipe to allow for Great wall of [insert country name here].
Huge concern is also that ITU is actually quite telco-heavy. That means that all decisions would be very much influenced by what powerful telcom operators think about it. And I can just see how they would like to limit (already low) speeds in order not to invest in their network anymore. Telcos do not care about people, nor should they. Telcos are corporations and they only care about profit for their shareholders.
Yes, currently USA has all keys to the Internet and situation is not ideal. However, even with this there is lot of freedom left to engineers to do what they need. And “freedom talk” that they are all fond of does keep most of censorship and death switches away for now. Having someone like China determine how open Internet will be is not a thought I cherish.
Maybe I am just paranoid, but I cannot see how this can end up well.