Bad Atmel Start code for IO toggle

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm using a TinyAVR 0-series micro. I have an LED on PA3 and a test output on PA5. When I start using the toggle function created by Atmel Start my LED goes crazy. After digging into the code I see this is what is used.

 

VPORTA.IN |= 0x20;

 

I think this is the wrong way to use this feature. What I believe happens is if any outputs are on they will show as a 1 if you read VPORTA.IN.  So when you do the OR you are effectively reading out and resetting those bits. This causes those IOs to toggle as well. What the code should have done is this

 

VPORTA.IN = 0x20;

 

This will only toggle the one bit in question as it will write the other bits to 0.

 

Am I right with this?  I'm new to these micros so I'd like to be sure.  If so can I report the bug somehow?

Last Edited: Sat. Mar 14, 2020 - 02:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looks like you are right. Usually when you use an AVRs IN-toggle feature it's = not |=

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


I tried creating a project on LED Toggle using ATTiny1617 device.

 

On debugging the code, as part of .lss file i could observe that the existing implementation of VPORTB.IN |= 1 << pin (where pin is '4') consumes only one instruction of SBI whose purpose is to set the specified bit in an IO register.

 

 

whereas for similar scenario using VPORTB.IN = 1<<pin, it consumes couple of instructions of LDI and OUT where a single instruction of LDI is responsible to load 8-bit constant to register and OUT is responsible to store data from register in the file to IO space.

 

 

Hence with respect to the instruction cycles, i feel the existing implementation serves its purpose to toggle the respective pins by setting the specified pin in the register. 

gud day

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

It's true that if only ONE bit is being toggled and the compiler can be persuaded to emit SBI to do the bit write then that's OK. The issue would be if you tried to toggle (say) 3 bits at a time so it did IN, OR, OUT - that would be when the problems arise.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I agree that the problem would arise in case of toggling multiple bits at a time, however, as the current START code supports the toggle mode with respect to only single pin, the existing implementation seems to be convincing.

gud day

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sounds like another example of stuff that "works" by accident rather than design ... ?

 

https://www.avrfreaks.net/commen...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why has no-one looked to see what code is generated when multiple bits are toggled? Seems to be the easiest way to see if START is smart or not.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It seems that the PORTB_toggle_pin_level() is a START function, and it toggles only one pin - so there is no way for it to (attempt to) do multiple pins ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...