diff --git a/doc/doc.html b/doc/doc.html new file mode 100644 index 0000000..8cdd701 --- /dev/null +++ b/doc/doc.html @@ -0,0 +1,132 @@ + + +RingLock for AVR Microcontrollers + + + +

RingLock

+

How it started

+After reading about PIClock4 +a inter-microcontroller mutex on dangerousprototypes.com, +I got curious about the actual implementation.
+It uses shift registers to pass a token in form of just a single bit around.
+Once the token arrives at a mcu that needs access to the resource, the mcu can stop the shifting.
+After having done whatever it needs the resource for, it can start the shifting again.
+Unfortunately the schematic files were down, so I had to ask for them.
+The marker of piclock told me that his project was just for fun and never intended for actual usage.
+Because of the huge amount of logic chips needed he suggested rather using a arduino/atmega328 to emulate the hardware in software.
+That's what got me thinking initially.
+Using a 32k flash mcu to emulate 9 logic ics, sounded terribly wasteful to me.
+First idea : I could use a Attiny instead.
+Advantages : I would not waste that much flash space.
+Disadvantage : Attinys have only few IO-pins and therefore the amount of mcus that can be attached to the lock is limited.
+
+So I was looking for a solution that allows a huge number of mcus, while needing no/not much additional hardware.
+At some point I got reminded of token ring networks, something I've never seen myself,
+because it's a rather old technique and at least for computer networks totally replaced by Ethernet.
+It's basic ideas is that the hosts pass a token from one to the next in a ring like topology.
+Once a host gets a token he is allowed to attach data to it while passing it on.
+This is very similar to the bit that gets shifted around in the piclock system,
+but it does not require a central piece of hardware controlling the whole process.
+That sounded just like the thing I wanted.
+
+After googling for a while I found a few implementations for token passing on microcontrollers,
+but the majority of them were integrated in other projects and designed for a single usage scenario.
+At that point I started experimenting with an own implementation.
+It had easily configurable for different AVR mcus and applicable to various scenarios where multiple mcus access the same resource.
+
+ +It's important to notice that I don't claim this to be a full featured library.
+Let's be honest, there not even enough lines of code to call this a program.
+I rather wanted to present the idea behind it, as well as providing a little reference implementation.
+So just think of this as a request for comments.
+
+ +

How it works

+The basic idea is to implement a decentralized token passing mechanism,
+allowing multiple mcus to share access to a resource like some kind of memory, a sensor or a communication interface of some kind.
+Each mcu has one of its IO Pins connected to the external interrupt pin e.g. INT0 of the next mcu.
+The last mcu in this chain has one of its IOs connected to the interrupt pin of the first one.
+This results in a ring topology as shown below.
+
+ +
+When everything is powered up somebody has to have the token and the access initially.
+I called this special mcu master.
+It does whatever it needs to do with the resource and then pulls his IO-pin (called PYX in the drawing) high for 1us.
+Slave 1 will get interrupted because of the rising signal at his INT0.
+In his ISR he will check whether the rest of his program has requested access to the resource.
+This is done by simply checking a flag. If the flag is not set it will pass the token to slave 2 immediately in his ISR.
+Otherwise it will set a flag, signalling the program that it can now access the resource and leave the ISR.
+It's now up to the program to work with the shared resource for a while and to pass on the token.
+The next mcus will do exactly the same thing and the token will circulate in the ring infinitely.
+
+At least theory.
+In praxis you'll sooner or later lose the token, simply because of murphys law.
+Imagine a mcu crashes, there is a power failure or a bad connection.
+Having lost the token, the system will get stuck since no one is allowed to access the shared resource.
+Every mcu waits for the token, that will never arrive.
+That's the point where a time out might come handy.
+Using a timer of the master mcu it's easy to implement one.
+The timer gets reset every time the master gets the token back from the last mcu in the ring.
+If the token doesn't arrive in time, the master assumes that it was lost and pretends to have the token.
+The new token will be passed on and everything is hopefully working again.
+Still there is one pitfall in this approach, the timeout has to be chosen carefully.
+If the master sends a new token while another is still on the ring, to mcu will try to work with the resource,
+which is in fact the situation we are trying to avoid using lock.
+The best idea is to use a timeout that is at least 1,5 times as long, as the longest time a token need back to the master.
+
+ +

How to use it

+There are two ways of getting the source code :
+
    +
  1. Get the tarball here
  2. +
  3. Clone the hg repository: hg clone hg repo url here
  4. +
+
+Once you got the source, you'll want to adjust the configuration in include/ringlock.h.
+The config is documented there and changing it should be pretty straight forward.
+
+Using the makefile is a bit more complex then the usual make all.
+There are some variables that need to be set :
+ +So if you want to build a slave for an Atmega8 with 8mHz call make ROLE=slave AVRMCU=atmega8 F_CPU=8000000 all.
+Currently the source only supports Atmega32 as master or slave and Atmega8 as slave.
+This can be changed by extending the configs in include/ringlock.h.
+Once you have added a new mcu, it would be nice to send me patch so I can add it to the source.
+
+Using the ringlock in your source code is simple, it boils down to 4 functions :
+ + +

Example

+Since this was a solution to which I didn't have a Problem I made one up :
+One ATmega32 (Master) and one Atmega8 (Slave) share the TXD line of a RS232 connection.
+I used the OpenBench LogicSniffer to measure the signals.
+
+ +
+
+The and gate was in a HCF4081.
+If you set the TXD of the passive mcu to high-Z you won't need it, but I wanted the unmixed signals as well.
+The program used in this test is bundled with the sourcecode as main.c.
+
+Here are all the signals in the jawis OLS client:
+ +
+and a photo of my setup :
+
+
+
+So that's all so far.
+Have fun with this stuff and please don't waste an entire Atmega32 on locks.
+ + diff --git a/doc/photo.jpg b/doc/photo.jpg new file mode 100644 index 0000000..5774d8e Binary files /dev/null and b/doc/photo.jpg differ diff --git a/doc/ringlock_idea.png b/doc/ringlock_idea.png new file mode 100644 index 0000000..c11c783 Binary files /dev/null and b/doc/ringlock_idea.png differ diff --git a/doc/ringlock_idea.svg b/doc/ringlock_idea.svg new file mode 100644 index 0000000..1a6d831 --- /dev/null +++ b/doc/ringlock_idea.svg @@ -0,0 +1,393 @@ + + + + + + + + + + + + image/svg+xml + + + + + + + Openbench Logicsniffer + Master + + + MCU0 + INT0 + PYX + + + + + + MCU1 + INT0 + PYX + + + + + + MCU2 + INT0 + PYX + + + + + + MCUn + INT0 + PYX + + + + + Slave 1 + Slave 2 + Slave n + + + + + + + + + diff --git a/doc/ringlock_test.png b/doc/ringlock_test.png new file mode 100644 index 0000000..e2acdea Binary files /dev/null and b/doc/ringlock_test.png differ diff --git a/doc/ringlock_test.svg b/doc/ringlock_test.svg new file mode 100644 index 0000000..0abaa6c --- /dev/null +++ b/doc/ringlock_test.svg @@ -0,0 +1,314 @@ + + + + + + + + + + + + image/svg+xml + + + + + + + + + Atmega8 + + INT0 + TXD + PYX + + + + + + Atmega32 + + INT0 + TXD + PYX + + + + + + + + + + + + + + + Openbench Logicsniffer + + + + + + + diff --git a/doc/test.ols b/doc/test.ols new file mode 100644 index 0000000..6a755cc --- /dev/null +++ b/doc/test.ols @@ -0,0 +1,670 @@ +;Size: 662 +;Rate: 20000000 +;Channels: 32 +;EnabledChannels: 255 +;TriggerPosition: 7208 +;Compressed: true +;AbsoluteLength: 1223475 +;CursorEnabled: false +00000010@0 +00000018@7209 +0000001c@7210 +00000014@9240 +00000010@9241 +00000018@11270 +0000001c@11271 +0000001e@18207 +0000001c@18226 +0000000c@18539 +00000008@18540 +00000018@20568 +0000001c@20569 +0000000c@22598 +00000008@22599 +0000001c@32747 +0000000c@34776 +00000008@34777 +00000018@36806 +0000001c@36807 +0000001d@43740 +0000001c@43759 +00000014@43890 +00000010@43891 +00000018@47952 +0000001c@47953 +00000014@49983 +00000010@49984 +00000018@58106 +0000001c@58107 +00000014@60137 +00000010@60138 +00000018@62168 +0000001c@62169 +0000001e@69107 +0000001c@69126 +0000000c@69410 +00000008@69411 +00000018@71440 +0000001c@71441 +0000000c@73470 +00000008@73471 +00000018@83618 +0000001c@83619 +0000000c@85648 +00000008@85649 +00000018@87677 +0000001c@87678 +0000001d@94616 +0000001c@94635 +00000014@94788 +00000010@94789 +00000018@98850 +0000001c@98851 +00000014@100881 +00000010@100882 +00000018@109005 +0000001c@109006 +00000014@111036 +00000010@111037 +00000018@113067 +0000001c@113068 +0000001e@120006 +0000001c@120026 +0000000c@120282 +00000008@120283 +00000018@122312 +0000001c@122313 +0000000c@124341 +00000008@124343 +00000018@134490 +0000001c@134491 +0000000c@136519 +00000008@136521 +00000018@138549 +0000001c@138550 +0000001d@145483 +0000001c@145502 +00000014@145689 +00000010@145690 +00000018@149751 +0000001c@149752 +00000014@151782 +00000010@151783 +00000018@159907 +0000001c@159908 +00000014@161938 +00000010@161939 +00000018@163969 +0000001c@163970 +0000001e@170908 +0000001c@170928 +0000000c@171281 +00000008@171282 +00000018@173310 +0000001c@173311 +0000000c@175340 +00000008@175341 +00000018@185488 +0000001c@185489 +0000000c@187518 +00000008@187519 +00000018@189548 +0000001c@189549 +0000001d@196481 +0000001c@196501 +00000014@196718 +00000010@196719 +00000018@200780 +0000001c@200781 +00000014@202811 +00000010@202812 +00000018@210935 +0000001c@210936 +00000014@212966 +00000010@212967 +00000018@214997 +0000001c@214998 +0000001e@221936 +0000001c@221956 +0000000c@222279 +00000008@222280 +00000018@224309 +0000001c@224310 +0000000c@226338 +00000008@226340 +00000018@236487 +0000001c@236488 +0000000c@238516 +00000008@238518 +00000018@240546 +0000001c@240547 +0000001d@247480 +0000001c@247499 +00000014@247618 +00000010@247619 +00000018@251680 +0000001c@251681 +00000014@253711 +00000010@253712 +00000018@261835 +0000001c@261836 +00000014@263865 +00000010@263867 +00000018@265896 +0000001c@265897 +0000001e@272836 +0000001c@272855 +0000000c@273151 +00000008@273152 +00000018@275180 +0000001c@275181 +0000000c@277210 +00000008@277211 +00000018@287358 +0000001c@287359 +0000000c@289388 +00000008@289389 +00000018@291417 +0000001c@291418 +0000001d@298351 +0000001c@298371 +00000014@298517 +00000010@298518 +00000018@302579 +0000001c@302580 +00000014@304610 +00000010@304611 +00000018@312734 +0000001c@312735 +00000014@314765 +00000010@314766 +00000018@316796 +0000001c@316797 +0000001e@323732 +0000001c@323752 +0000000c@324022 +00000008@324023 +00000018@326052 +0000001c@326053 +0000000c@328082 +00000008@328083 +00000018@338230 +0000001c@338231 +0000000c@340259 +00000008@340260 +00000018@342289 +0000001c@342290 +0000001d@349225 +0000001c@349245 +00000014@349416 +00000010@349417 +0000001c@353478 +00000014@355508 +00000010@355510 +00000018@363632 +0000001c@363633 +00000014@365663 +00000010@365664 +00000018@367694 +0000001c@367695 +0000001e@374633 +0000001c@374652 +0000000c@374893 +00000008@374895 +00000018@376923 +0000001c@376924 +0000000c@378953 +00000008@378954 +00000018@389101 +0000001c@389102 +0000000c@391131 +00000008@391132 +0000001c@393161 +0000001d@400094 +0000001c@400114 +00000014@400314 +00000010@400315 +00000018@404376 +0000001c@404377 +00000014@406406 +00000010@406408 +00000018@414530 +0000001c@414531 +00000014@416561 +00000010@416562 +00000018@418592 +0000001c@418593 +0000001e@425533 +0000001c@425553 +0000000c@425892 +00000008@425893 +00000018@427922 +0000001c@427923 +0000000c@429951 +00000008@429952 +0000001c@440100 +0000000c@442129 +00000008@442130 +00000018@444159 +0000001c@444160 +0000001d@451098 +0000001c@451117 +00000014@451339 +00000010@451340 +00000018@455401 +0000001c@455402 +00000014@457431 +00000010@457433 +00000018@465555 +0000001c@465556 +00000014@467586 +00000010@467587 +00000018@469617 +0000001c@469618 +0000001e@476558 +0000001c@476578 +0000000c@476890 +00000008@476891 +00000018@478920 +0000001c@478921 +0000000c@480950 +00000008@480951 +00000018@491098 +0000001c@491099 +0000000c@493128 +00000008@493129 +00000018@495157 +0000001c@495158 +0000001d@502096 +0000001c@502115 +00000014@502237 +00000010@502238 +00000018@506299 +0000001c@506300 +00000014@508330 +00000010@508331 +00000018@516454 +0000001c@516455 +00000014@518485 +00000010@518486 +00000018@520516 +0000001c@520517 +0000001e@527453 +0000001c@527473 +0000000c@527762 +00000008@527763 +00000018@529791 +0000001c@529792 +0000000c@531821 +00000008@531822 +00000018@541969 +0000001c@541970 +0000000c@543999 +00000008@544000 +0000001c@546029 +0000001d@552967 +0000001c@552987 +00000014@553138 +00000010@553139 +00000018@557200 +0000001c@557201 +00000014@559231 +00000010@559232 +00000018@567355 +0000001c@567356 +00000014@569386 +00000010@569387 +00000018@571417 +0000001c@571418 +0000001e@578354 +0000001c@578374 +0000000c@578633 +00000008@578634 +0000001c@580663 +0000000c@582692 +00000008@582693 +00000018@592840 +0000001c@592841 +0000000c@594870 +00000008@594871 +00000018@596900 +0000001c@596901 +0000001d@603833 +0000001c@603853 +00000014@604039 +00000010@604040 +00000018@608101 +0000001c@608102 +00000014@610131 +00000010@610133 +0000001c@618256 +00000014@620286 +00000010@620288 +00000018@622317 +0000001c@622318 +0000001e@629259 +0000001c@629279 +0000000c@629631 +00000008@629632 +00000018@631661 +0000001c@631662 +0000000c@633691 +00000008@633692 +00000018@643839 +0000001c@643840 +0000000c@645868 +00000008@645869 +00000018@647898 +0000001c@647899 +0000001d@654831 +0000001c@654851 +00000014@655065 +00000010@655066 +00000018@659126 +0000001c@659127 +00000014@661157 +00000010@661158 +00000018@669281 +0000001c@669282 +00000014@671312 +00000010@671313 +00000018@673343 +0000001c@673344 +0000001e@680284 +0000001c@680304 +0000000c@680629 +00000008@680630 +0000001c@682659 +0000000c@684688 +00000008@684689 +0000001c@694837 +0000000c@696866 +00000008@696867 +00000018@698896 +0000001c@698897 +0000001d@705830 +0000001c@705849 +00000014@705963 +00000010@705964 +00000018@710025 +0000001c@710026 +00000014@712056 +00000010@712057 +00000018@720180 +0000001c@720181 +00000014@722210 +00000010@722212 +00000018@724241 +0000001c@724242 +0000001e@731183 +0000001c@731202 +0000000c@731501 +00000008@731502 +00000018@733530 +0000001c@733531 +0000000c@735560 +00000008@735561 +00000018@745708 +0000001c@745709 +0000000c@747738 +00000008@747739 +00000018@749767 +0000001c@749768 +0000001d@756704 +0000001c@756723 +00000014@756861 +00000010@756862 +00000018@760923 +0000001c@760924 +00000014@762954 +00000010@762955 +00000018@771078 +0000001c@771079 +00000014@773108 +00000010@773110 +00000018@775139 +0000001c@775140 +0000001e@782078 +0000001c@782098 +0000000c@782372 +00000008@782373 +00000018@784401 +0000001c@784402 +0000000c@786431 +00000008@786432 +00000018@796579 +0000001c@796580 +0000000c@798609 +00000008@798610 +00000018@800639 +0000001c@800640 +0000001d@807573 +0000001c@807592 +00000014@807759 +00000010@807760 +0000001c@811821 +00000014@813851 +00000010@813853 +00000018@821975 +0000001c@821976 +00000014@824006 +00000010@824007 +00000018@826037 +0000001c@826038 +0000001e@832978 +0000001c@832998 +0000000c@833243 +00000008@833245 +00000018@835273 +0000001c@835274 +0000000c@837303 +00000008@837304 +00000018@847451 +0000001c@847452 +0000000c@849481 +00000008@849482 +00000018@851510 +0000001c@851511 +0000001d@858449 +0000001c@858468 +00000014@858657 +00000010@858658 +00000018@862719 +0000001c@862720 +00000014@864749 +00000010@864751 +00000018@872873 +0000001c@872874 +00000014@874904 +00000010@874905 +00000018@876935 +0000001c@876936 +0000001e@883876 +0000001c@883896 +0000000c@884242 +00000008@884243 +00000018@886271 +0000001c@886272 +0000000c@888301 +00000008@888302 +00000018@898449 +0000001c@898450 +0000000c@900479 +00000008@900480 +00000018@902508 +0000001c@902509 +0000001d@909447 +0000001c@909467 +00000014@909682 +00000010@909683 +00000018@913744 +0000001c@913745 +00000014@915775 +00000010@915776 +00000018@923899 +0000001c@923900 +00000014@925930 +00000010@925931 +00000018@927961 +0000001c@927962 +0000001e@934903 +0000001c@934923 +0000000c@935240 +00000008@935241 +00000018@937269 +0000001c@937270 +0000000c@939299 +00000008@939300 +00000018@949447 +0000001c@949448 +0000000c@951477 +00000008@951478 +00000018@953507 +0000001c@953508 +0000001d@960445 +0000001c@960465 +00000014@960583 +00000010@960584 +00000018@964645 +0000001c@964646 +00000014@966676 +00000010@966677 +00000018@974800 +0000001c@974801 +00000014@976831 +00000010@976832 +00000018@978862 +0000001c@978863 +0000001e@985803 +0000001c@985823 +0000000c@986111 +00000008@986112 +00000018@988141 +0000001c@988142 +0000000c@990171 +00000008@990172 +00000018@1000319 +0000001c@1000320 +0000000c@1002348 +00000008@1002350 +00000018@1004378 +0000001c@1004379 +0000001d@1011317 +0000001c@1011336 +00000014@1011483 +00000010@1011484 +00000018@1015545 +0000001c@1015546 +00000014@1017576 +00000010@1017577 +00000018@1025700 +0000001c@1025701 +00000014@1027731 +00000010@1027732 +00000018@1029762 +0000001c@1029763 +0000001e@1036703 +0000001c@1036723 +0000000c@1036982 +00000008@1036983 +00000018@1039012 +0000001c@1039013 +0000000c@1041042 +00000008@1041043 +00000018@1051190 +0000001c@1051191 +0000000c@1053219 +00000008@1053221 +00000018@1055249 +0000001c@1055250 +0000001d@1062183 +0000001c@1062202 +00000014@1062383 +00000010@1062384 +00000018@1066445 +0000001c@1066446 +00000014@1068476 +00000010@1068477 +00000018@1076600 +0000001c@1076601 +00000014@1078631 +00000010@1078632 +0000001c@1080662 +0000001e@1087601 +0000001c@1087620 +0000000c@1087981 +00000008@1087982 +00000018@1090010 +0000001c@1090011 +0000000c@1092040 +00000008@1092041 +00000018@1102188 +0000001c@1102189 +0000000c@1104218 +00000008@1104219 +00000018@1106247 +0000001c@1106248 +0000001d@1113181 +0000001c@1113201 +00000014@1113408 +00000010@1113410 +00000018@1117470 +0000001c@1117471 +00000014@1119501 +00000010@1119502 +00000018@1127625 +0000001c@1127626 +00000014@1129655 +00000010@1129657 +00000018@1131686 +0000001c@1131687 +0000001e@1138623 +0000001c@1138642 +0000000c@1138979 +00000008@1138980 +00000018@1141008 +0000001c@1141009 +0000000c@1143038 +00000008@1143039 +00000018@1153186 +0000001c@1153187 +0000000c@1155216 +00000008@1155217 +00000018@1157245 +0000001c@1157246 +0000001d@1164182 +0000001c@1164201 +00000014@1164306 +00000010@1164307 +00000018@1168368 +0000001c@1168369 +00000014@1170399 +00000010@1170400 +00000018@1178522 +0000001c@1178523 +00000014@1180553 +00000010@1180554 +00000018@1182584 +0000001c@1182585 +0000001e@1189525 +0000001c@1189545 +0000000c@1189850 +00000008@1189852 +00000018@1191880 +0000001c@1191881 +0000000c@1193910 +00000008@1193911 +00000018@1204058 +0000001c@1204059 +0000000c@1206088 +00000008@1206089 +00000018@1208118 +0000001c@1208119 +0000001d@1215056 +0000001c@1215076 +00000014@1215204 +00000010@1215205 +00000018@1219266 +0000001c@1219267 +00000014@1221296 +00000010@1221298 diff --git a/doc/test.png b/doc/test.png new file mode 100644 index 0000000..d335738 Binary files /dev/null and b/doc/test.png differ diff --git a/doc/test_short.png b/doc/test_short.png new file mode 100644 index 0000000..b64b431 Binary files /dev/null and b/doc/test_short.png differ diff --git a/include/ringlock.c b/include/ringlock.c index a1d1f79..d551c41 100644 --- a/include/ringlock.c +++ b/include/ringlock.c @@ -1,5 +1,5 @@ /* - * ringlock.c + * ringlock.c * * Created on: Sep 28, 2011 * Author: sebastian diff --git a/include/ringlock.h b/include/ringlock.h index d4abd45..cd1b123 100644 --- a/include/ringlock.h +++ b/include/ringlock.h @@ -1,5 +1,5 @@ /* - * ringlock.h + * ringlock.h * * Created on: Sep 28, 2011 * Author: sebastian diff --git a/main.c b/main.c index f9f9358..3196dd8 100644 --- a/main.c +++ b/main.c @@ -1,12 +1,8 @@ /* - * main.c + * main.c * * Created on: Sep 28, 2011 - * Author: sebastian - * - * Makefile - * Created on: Sep 28, 2011 - * Author: sebastian + * Author: sebastian * * This file is part of the RingLock-library for Atmel AVR MCUS. *