PaulStoffregen / OneWire

Library for Dallas/Maxim 1-Wire Chips
http://www.pjrc.com/teensy/td_libs_OneWire.html
579 stars 382 forks source link

Add support for the rest of the megaAVR 0-series, the tinyAVR 0/1-series, and the new AVR Dx-series, make interrupt safe. #94

Open SpenceKonde opened 3 years ago

SpenceKonde commented 3 years ago

These all have the same patterns in register locations, so can use the same formulae for them.

This also switches to using the VPORT registers instead of the PORT registers, which makes setting or clearing a single bit into an atomic, single word, single cycle operation (SBI/CBI), as opposed to requiring a read-modify-write cycle like the PORT.OUT and PORT.DIR registers do, making those operations interrupt safe like they were on classic AVRs, as well as reducing sketch size and executing faster. Taking advantage of the consistent numbering of ports returned by digitalPinToPort() among the cores supporting these parts to get the base register address shrinks the compiled sketch size a touch too.

Non-pins passed to digitalPinToPort() will get back NOT_A_PORT, which is defined as 255, would give base reg of 0x03FC, which falls into one of the many gaps in the register space on these parts as well - so it won't fiddle with some random other peripheral somewhere (as it happens, it's right before where the PORT registers start at 0x0400, which seems fitting for "port -1")

SpenceKonde commented 3 years ago

Just discovered something: __AVR_ARCH__ is 102, 103, or 104 for the new Dx series parts, not always 104. 103 which is what it is on the tinyAVR/megaAVR 0/1-series as well as 32k Dx parts; these can map all of their flash in the data space at once. 102 is for 64k (two sections), and 104 is for 128k parts (which have to split their flash into 4 sections)

Updated pr to account for this discovery.

BigGene commented 3 years ago

Please add this support for the new AVRs in the next version. Thanks!