dritcey / optiboot

Automatically exported from code.google.com/p/optiboot
0 stars 0 forks source link

LED_DATA_FLASH possible issue #112

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I'm creating a custom bootloader based on optiboot but with the ability to 
drive a RGB WS2812B LED instead of a classic LED

Everything is working fine but I saw in the original code something I do not 
understand
I'm on ATMEGA328P so classic Arduino

on the function getch, at the begining there is the following code, ok sounds 
good to me :

#ifdef LED_DATA_FLASH
#ifdef __AVR_ATmega8__
  LED_PORT ^= _BV(LED);
#else
  LED_PIN |= _BV(LED); 
#endif
#endif

but at the end of getch there is 
#ifdef LED_DATA_FLASH
#ifdef __AVR_ATmega8__
  LED_PORT ^= _BV(LED);
#else
  LED_PIN |= _BV(LED); 
#endif
#endif

so I agree that LED_PORT ^= _BV(LED) will toggle the led state between start 
and end but LED_PIN |= _BV(LED); will always set LED port output to 1 no ?
At the end the function should not be LED_PIN &= ~_BV(LED); ?
May be I misunderstood something or any optimization done by the compiler.
If someone has an explanation on this case I will appreciate to understand and 
increase my knowledge ;-)

Thank you for your help

Original issue reported on code.google.com by ch.hall...@gmail.com on 16 Sep 2014 at 11:26

GoogleCodeExporter commented 8 years ago
On the more advanced chips (168, 328), writing a 1 to the PIN register (LED_PIN 
vs LED_PORT) causes the output state to toggle.  It's buried in the datasheet 
somewhere.

Original comment by wes...@gmail.com on 19 Sep 2014 at 5:37

GoogleCodeExporter commented 8 years ago
Got it on the datasheet thank you for your answer :

Three I/O memory address locations are for each port :
– PORTx, Data Direction Register 
– DDRx, and the Port Input Pins 
– PINx. The Port Input Pins I/O location is read only
While the Data Register and the Data Direction Register are read/write.

However, writing a logic one to a bit in the PINx Register, will result in a 
toggle in the corresponding bit in the Data Register.

This is what says the datasheet. I like the point :
"Is Read Only" but writing to it do an action (toggle) ;-)

Original comment by ch.hall...@gmail.com on 19 Sep 2014 at 4:49