STAR Firmware by JonnyC - Source Code and Explanation

No ESD problems yet, and I've programmed a few.

If I want to change the switch pin to Star 3 and the temperature sensor to Star 4 of MTN_momentary_temp, should I simply modify these lines:

#define SWITCH_PIN PB3 to #define SWITCH_PIN PB4

#ifdef VOLTAGE_MON
volatile uint8_t adc_channel = 1;
#else
volatile uint8_t adc_channel = 2;
#endif

to

#ifdef VOLTAGE_MON
volatile uint8_t adc_channel = 1;
#else
volatile uint8_t adc_channel = 3;
#endif

also modify the adc_channel to 0x03 (PB3)

// Switch ADC to temp monitoring
adc_channel = 0x03;
ADMUX = ((ADMUX & 0b11111100) | adc_channel);
} else if (adc_channel == 0x03) {

Thanks for your comments.

Okay, I made a version of STAR which uses brownout detection instead of an offtime capacitor. It’s just STAR_off_time 1.3 with that one thing changed; otherwise it’s identical.

http://bazaar.launchpad.net/~toykeeper/flashlight-firmware/trunk/files/head:/ToyKeeper/STAR_noinit/

Not guaranteed to work on every attiny13-based driver, but it works on all the ones I tried. I hear the brownout timing can vary depending on some of the other driver components.

Everytime I read “Brownout” my mind immediately thinks “dust storm.”

I may just have to try that sometime, but I have a lifetime supply of OTC’s, so we’ll see if I ever get around to it.

There isn’t really any advantage to it if you already have an OTC installed. It might save a few bytes, I guess, but mostly it’s just to get an offtime-based UI on lights which don’t have (or can’t fit) the capacitor. I use this trick on a couple qlite/nanjg drivers.

I use a USBasp 2.0 programmer and grew tired of the annoying “cannot set sck period” error message every time I used it, so I fixed it (updated the firmware) for $3—and ended up with a backup programmer in the process. I gleaned the following info from a few sites including the programmer’s site. This only works if you’re currently using a USBasp 2.0 programmer.

1. Buy a USBasp 2.0 with 10-wire ribbon from Ebay, Fasttech or wherever.
2. Choose which programmer you’re going to update (the target) and mark it with a permanent marker or something,
3. Document your clip ribbon connections to your existing programmer and remove them.
4. Optional: Make sure your new programmer works. Connect to a clip, clip to an attiny13a, and test the connection. I use “avrdude –p t13 –c usbasp –n” (without the quotes).
5. Jump JP2 on the target USBasp to put it in programming mode.
6. Connect the two USBasps with the 10-wire ribbon. It can only connect one way.
7. Get the new firmware either from the developer (needs to be compiled) or get the hex file here . Lazy arse that I am, I got the hex file.
8. Put the hex file in the correct directory.
9. Plug the non-target USBasp into the USB port
10. The new firmware requires different fuse settings on the target programmer’s processor. I used the command line “avrdude –c usbasp –p atmega8 –u –U hfuse:w:0xc9:m –U lfuse:w:0xef:m” (without the quotes).
11. Flash the hex file to the target USBASP to update the firmware. I used the command line “avrdude –c usbasp –p atmega8 –U flash:w:usbasp.atmega8.2011-05-28.hex” (without the quotes). If you made your own hex file, use it in the command line.
12. Disconnect the 10-wire ribbon and remove the jumper from the target.
13. Connect the clip ribbon to the target and repeat #4 above. The “cannot set sck period” error message is now a thing of the past.

You should now be able to flash error free. Oh, I had both programmers set to 5 volts.

I have only flashed one chip with the new firmware, and it works, but I am leaving the non-target with the old firmware just in case there is some issue down the road.

Hope this helps someone and good luck!

I think you're the only person who has ever cared to update their firmware. It's never caused any problems for me or anyone else that I'm aware of, so my opinion has always been "if it isn't broken, don't waste time trying to fix it".

That said, I applaud you for figuring out a fix for something that isn't really broken just because it bugs you. It sounds like you'll fit in really well here (we've all been known to do things like this).

TK - I tried this firmware on a single sided Nanjg 101-AK-A1 (from IOS) with 2 added 7135's (2.1A total). This driver really has no space for the OTC cap I could find for it. Mostly it worked great:

  • quick click detection had excellent timing and works reliably
  • LVP kicks in at just about exactly 3.0v, but with an issue:

--> It blinks 3 times ok, but immediately goes into moon mode after that, instead of ramping down. I'm using your recommended fuse settings (not sure what those settings mean: -Ulfuse:w:0x79:m -Uhfuse:w:0xed:m). Everything else is untouched, your original code, but set my own mode PWM values, including 5 for moon mode assuming that's the minimum for 7135's.

I carefully reviewed the LVP code and didn't see any differences, and the logic appears correct, using the ALT_PWM port value. Looks like it should cut output in half each time, then decrement modes when the PWM value falls below the next, etc...

Any ideas why this is happening? Does LVP work ok for you in this firmware? Almost seems like reading ALT_PWM is not working well, or something...

Edit: This is a pretty good fuse setup web app for the ATTiny13A: engbedded.com fuse app

Interesting. It could be a fuse value, perhaps, since I didn’t change anything except the memory method and fuse settings. Or it could be a bug inherited from STAR_off_time. The fuse values were copied from alexvh’s posts, but I’m not sure why some bit changed. I suspect that 0x75 and 0xfd should work, but I’ve only actually tried 0x79 and 0xed. Perhaps I can test LVP with various fuses soon…

Ok - I fixed it now. First, I re-produced it on another Nanjg 101-AK-A1 (short story: 1st light sold as soon as I showed it to someone...). This was a 2.45A build, but same exact firmware of STAR_NoInit. It has the same exact problem, but this time I notice after the 3 blinks, it actually turns off for a couple secs, then turns on at a moon mode level -- weird.

So again, I studied the code and only possible issue I could see is the manipulations of ALT_PWM_LVL, which is really the I/O register OCR0A. So, I added a variable called: 'setPwmOut' that mirror images the PWM output, and referenced it instead of ALT_PWM_LVL in the LVP code. Then viola!! Re-built and works perfectly. You can see it now go thru 3 blinks, then drop, repeatedly, which is how it should work. Also I fixed a potential bug in the Main LVP code where mode_idx is decremented where it could potentially be 0 before the decrement.

The decrement is below this comment (code inserting sux in a thread post), add an if " if (mode_idx > 0)" before "mode_idx++":

// See if we should change the current mode level if we've gone under the current mode.

Ohhh - I use AtmelStudio 6.2 with the optimized for size setting - all same compile/build settings as my eswitch and STAROffTime projects which work well, including LVP.

Awesome. Could you send me a patch, or the source you’re now using? I’d like to fix it in my repo too.

Sure.. My version has some additional formatting changes, always backs off the mode index by 1 when a turbo timeout occurs (so 1 click restores turbo), and disables the moon mode STAR setting (commented out). @work now with no access to it, but I'll try this eve. You probably should grab all my latest to update your repository with. I'll try to throw together some readme text files for each to list the changes/enhancements.

Really love the quick click mode change of this noinit version. The OTC cap versions always seem to have issues of variation of timing, and so far, this one has been a rock. From what I can see, the 2 main changes to the fuses is the 64 msec delay option vs. the 4 msec delays option, and the brown-out detection enabling. Thinking the 64 msecs give it more time for the memory to change, so the timing of the mode change detection is better/shorter. The brown-out detection enabling is the key to it working at all, I would think.

When you say "mirrored" do you mean reversed? or do you mean copied?

Smile - in CS/IT, never heard of mirror imaging doing anything reverse - interesting analogy.. I mean copy, a local copy of the I/O register.

It's an old trick (I'm old too) to have a local copy of registers, specially when they are write-only. These registers (OCR0A and OCR0B) are doc'd as R/W, so either there's something quirky with them, or the C compiler has some bug/issue with doing manipulations on registers, dunno, or could be some flaky bug in our software that somehow corrupts something, and my change simply moved things around so the effect is minimized. I've seen it all from chip bugs to SW tool bugs, and got burned a few times on C compiler problems. We've had a few C compiler bugs on our current project at work using a TI DSP and the development tools that support them. Back in the mid-late 80's and 90's, I believe the compilers were better - they were developed in the US either directly by the chip manufacturer or direct contractor. Now everything goes overseas and the rigorous quality and testing standards have diminished. It's all bout saving money now...

Sorry - That's my engineer curse throwing doubt in my interpretations. Anyone else would know exactly what you meant.

I figured you meant 'copy', and now upon further refection, I can't imagine why you would 'reverse' it, but I asked first and thought second.

I did it because I can :stuck_out_tongue: . And now I have a spare programmer in case one goes Tango Uniform.

…And what did it cost me? $3 and maybe 2 hours of net surfing/zapping.

I had to go byte hunting again, both in program space and available RAM, to fit in a function in a light that has a 13A MCU. I still had some code left from the original Star and found a way to free up some much needed space.

The eeprom_read_block funtion eats a fair chunk of programming space and the eep array eats half of the 13A’s RAM capacity.

eeprom_read_block(&eep, 0, 32);
while((eep[eepos]==0xff) && (eepos<32)) eepos++;

Instead of reading a 32 byte block, read one byte at a time.

while (modeidx==0xff && eepos<64) {
    while (EECR & 2);
    EEARL = eepos;  EECR = 1; modeidx = EEDR;
    while (EECR & 2);
    if (modeidx==0xff) eepos++;
}

There are only advantages to this. Not only did it save me 32 bytes of programming space, it frees up the 32 byte eep array just hanging around eating up half of the 13A’s RAM capacity, and it also allows using the full 64 bytes of the EEPROM for wear leveling. I can’t notice any difference in mode reading time either, and I tested with the full 64 byte wrap around.

I probably don’t have to do the second wait after reading, but I need to be sure that no EEPROM reading or writing is going on before continuing. If I have to hunt down a few more bytes I’ll take a closer look at that…

Mike C, I did the same thing in the blf-a6 firmware. :slight_smile:

So, the blf-a6 code can be used as a reference for handling eeprom more space-efficiently. It also shows how to store multiple values, if desired, or how to store just the mode index. (full blf-a6 uses multiple values, trimmed-down version just stores one value)

BTW, there are some library functions which can be used instead of accessing the eeprom registers directly. This should make the code a little more portable to different MCUs, and might also make the code smaller or less likely to break.

BTW, I’m not really sure where to announce this, but I added a tagging system and an auto-generated index to the firmware repository. I hope this will make it easier for people to find code relevant to their needs.

The latest index should always be here:

http://bazaar.launchpad.net/~toykeeper/flashlight-firmware/trunk/view/head:/INDEX

Nice. I usually end up throwing a few features out the window (Star functionality; LVP blinks, etc.) whenever I need more space, but I'm too lazy and ignorant to do anything more advanced!