A repository for snippets of working code

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

When I was learning C all those years ago, before the invention of C++, I found the library of C "snippets".  The online library was well stocked with short examples of working code that did almost anything you could imagine, and was freely downloadable as PDF's (or were they Word documents back then - I really don't remember).  It is still on SourceForge.  I found that repository hugely valuable, and although it wasn't the only means by which I learned C, I learned a great deal by using sections of code from there, seeing how "proper" engineers did things, and modifying them to suit my needs.  I went on to produce hundreds of thousands of lines of C.

 

I have just reached a milestone in my little Assembler project on the ATtiny85, and I must say it has been a learning experience that I have found pretty difficult.  If there is a repository of AVR assembler snippets out there somewhere, then I never found it - and it's not from want of trying.  I found a lot of example C-code, but not much assembler.  If a Snippets library does not already exist, I would like to suggest making one, in which some distinction is made between languages - principally assembler and C - so that people learning these chips for the first time will have an easier time than I just had.  I couldn't get my INT0 interrupt working, and for various reasons, I was convinced that it was because of the way I was declaring the vectors.  It finally turned out to be an "R16" that had magically morphed into an "R1", but when the program wasn't working, everything was suspect, and I spent a lot of time trying to verify whether it was right to have an instruction instead of a vector at the vector address (my previous understanding of the term "vector" was more synonymous with "pointer" than with "instruction"), and how to use .org, and exactly what it did.  I read tutorials and searched the net for examples, but I found very little, and what I did find, I could not make compile.  It always popped up a Disassembler window in AS-7 with errors.  All of this uncertainty, and consequent waste of time, would have been greatly alleviated if I had had some working code snippets to look at.

 

As for myself, I am retired and working on a little project for my own interest and satisfaction.  I wanted to learn the AVR instruction set because I have other projects in mind - but none of them is going to set the world on fire.  On the other hand, there are many young engineers, setting out on life's highways and byways, and if the way was made easier for them to learn these chips, they might get into them that much quicker, and stick with them for some time into their careers - and who knows what world-beating devices they may produce?  Consequently, a repository of working code examples would serve the company as well as the community of enthusiasts.  A very good place for such a collection of code snippets would be in this group of forums - perhaps a new forum, with enforced tags of "C", "ASM", "ATtiny", "AVR", "8-bit", "32-bit", and any other divisions that others more experienced than I might propose.  Mixed code could have tags of C and ASM.  It should be made clear in the rules that code snippets should be fully commented as an aid to those who are trying to get to grips with the technology.  Although my project is not yet finished, the parts I have done are working, and while they may not be the slickest code, they illustrate some aspects of AVR programming and I would be very happy to donate it for the benefit of others.  I'm sure many other enthusiasts are proud of their efforts and would be very happy to show them off in a public forum.

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

armstack wrote:
I found a lot of example C-code, but not much assembler.
C compilers will show you the Asm code they are writing if you ask them nicely ;-)

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

The general push with the AVR was to use C - the designers collaborated with the IAR compiler writers to optimise the architecture to support it. Also, early on in it's life there were some low cost compilers then along came a port of gcc.
Similarly, if you try looking for asm examples for the ARM, they too are pretty thin on the ground. The 8bit PICs are a different story - the architecture is hostile to the implementation of a C compiler, so the general push with them is asm.

Last Edited: Wed. Mar 29, 2017 - 02:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
C compilers will show you the Asm code they are writing if you ask them nicely ;-)

Yes, clawson, I discovered that, but as an example of difficulties I was having that are not answered by the Disassembly window, it doesn't show you how to use .org in the declaration of interrupt vectors.

However, my intention in opening this thread was to propose a snippets repository.  What do you think of the suggestion?  It might take some of the workload off moderators such as yourself if people are able to solve their own queries.

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

I'd suggest AVRs reached their zenith a few years ago, so the requirement for asm examples is declining methinks.

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

armstack wrote:
Yes, clawson, I discovered that, but as an example of difficulties I was having that are not answered by the Disassembly window, it doesn't show you how to use .org in the declaration of interrupt vectors.
It does if you look at the RIGHT version of the source. Assuming avr-gcc then try -save-temps then study the .s file. As the lack of C annotation is a bit disappointing I wrote this to help...

https://spaces.atmel.com/gf/proj...

That won't immediately help your specific question about .org to vectors as that is not how you place code in GCC/binutils.

 

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

Kartman wrote:
The general push with the AVR was to use C - the designers collaborated with the IAR compiler writers to optimise the architecture to support it.

Thanks Kartman, I didn't know that.  I've only recently come to AVR processors, so I don't know anything about the history - and you could well be right (you'd know better than I) - asm may be a dead-end street for avr development - but I'm not suggesting a snippets collection of only assembler.  I'm not using C in my project, but snippets of C that relate in some way to features of the AVR would probably be very useful to a lot of budding developers - people who are attacking ADC for the first time, or trying to get to grips with any of the other internal peripherals of the chip.  Besides, I believe that assembler is a very useful means of understanding the underlying hardware, and has a real place in the embedded market.  Even in C code, there seems to be a fair bit of assembler in functions.

I was interested in your comment about asm on the Pic.  I saw a comment from somebody who was developing a Forth kernel for an ATMega chip, that the Pic didn't lend itself to that either.

Anyway - I'm wandering off-topic in my own thread!

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

clawson, I just tried your link, and I got "The owner of spaces.atmel.com has configured their website improperly. To protect your information from being stolen, Firefox has not connected to this website."

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

The best collection of AVR code these days are almost certainly all the library code that has been written to support Arudino. I think you will find "driver" code there for almost every internal or external peripheral you may ever try to use with AVR.

 

Sure the code is in C++ which is not to everyone's taste but for other languages it can be used as a template to see what needs to be done.

 

So I guess you could start to rewrite some of the popular Arduino libraries in Asm?

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

Kartman wrote:
I'd suggest AVRs reached their zenith a few years ago, so the requirement for asm examples is declining methinks.

... and that may be the clincher.   A snippets repository for these chips may be yesterday's idea.

Signed - Yesterday's Man.

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

In case you haven't heard - there's Arduino that hides all the evil. That's what the kiddies are using these days.
If there's a chip half useful to a hobbyist then there's most likely an Arduino library for it. It's generally my first stop.

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

Kartman wrote:
Arduino[...]'s generally my first stop.

Just to save me time, can you give a URL?

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

armstack wrote:
Signed - Yesterday's Man.
Does anyone else see the irony in the name of your ID here?

 

ARM is where it's at these days. For those that are so inclined I could see why there may be a requirement for a software stack in Asm to support ARM.

 

The AVR boat may have already sailed ;-)

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

Not wanting to sound rude, but google anything with Arduino and you're going to get hits. Your question is almost akin to asking for the url of Google!

I just tried googling arduino nova
Alas nothing specific. Google arduino pdp11 - there's a few. Even someone who has emulated a pdp11 running unix on an Arduino.

Last Edited: Wed. Mar 29, 2017 - 03:43 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Does anyone else see the irony in the name of your ID here?

Yes, I'm aware of it too.  I'm subscribed to lots of forums for everything from woodwork to finance to legal, and I have a different user/password for every one in the interests of security.  When joining a forum, I dream up a new user name - and that was the one that presented itself when I had very little idea of what I was joining.

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

I as you am in a similar situation, being retired and of the same interest. However, there are only two reasons I share code;

 

  1. To be critiqued by others of similar interest.
  2. To give insight as to train of though when asking a question and code may be undocumented or incomplete.

 

Beyond that is where the Achiles heel of low level programming rears its head because it is architecture specific. As an example, Atmel has a master transmitter example on page 216 of 8271 I-AVR datasheet that depicts using OUT TWCR, R16. I think it's safe to assume that on some devices this is addressable somewhere in IO space, but on the 328P it is not, as STS needs to be used. In C however;

TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);

There need be no more qualification than that, because the compiler will look after the idiosyncrasies of whichever device was selected. As much as a proponent I am of assembly, I'm not disillusioned why I'm on an island by myself. This little bubble that I'm in, is my bubble, but if your a serious developer, you want to make sure you have a big needle to pop this thing. Next year, "C" will be 50 yrs old and there is a very good reason for its longevity.

 

If you'd like to collaborate with me directly, I'd be into that, but I'm not sure anything we'd have to share would have a very broad audience. There are things I've shared on other sites that have been there for a couple of years, only to have maybe 100 views. That's pretty much in a nutshell how popular low level programming is.

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

RES

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

That looks like a potentially very useful site, Res.  TYVM!  Unfortunately, every link that I tried on the page gave me a timeout, and wouldn't load.  I guess something is down somewhere.  I'll try again another day.

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

AVR_Coder_1 - Thanks for the offer.  I'm not sure about collaboration, but only because our interests in projects is probably very different - though it's undoubted that projects are more enjoyable if one has another person to bounce ideas off, and to 'enthuse' with over successes.  I looked to see if I could send you a personal message, but this forum doesn't seem to have that facility.  I could ask a moderator to send you my e-mail address, or otherwise, we could simply open a thread in this AVR discussion forum - and other asm coders might join too.  Surely, we aren't the only ones?  I don't have a FaceBook or Twitter account - and don't want one - though here are plenty of chat media - Skype messaging, for example.  If you're serious about taking it forward, how do you propose we do it?

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

I looked to see if I could send you a personal message, but this forum doesn't seem to have that facility.

Top right hand side next to the envelope and just above the search box.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

js wrote:
Top right hand side next to the envelope and just above the search box.

Thanks, John.  That works.

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

Did you think of

a) avr-libc ?

 parts of it, which are not hardware dependant, can be tested on a PC on cygwin or GNU linux, a Rpi/nanoPi (with glibc) and their doc is available offline with man strdup (say)

This is a large number of chunks of well written, carefully tested , with easy to read structured  documentation http://www.nongnu.org/avr-libc/u... and examples.

 

b) avr-freaks tutorials (taught me a lot); is much easier to understand than looking at Arduino's source, which makes an heavy use of preprocessor directive. As my old head does not countain a C preprocessor ,https://en.wikipedia.org/wiki/C_... it takes time understanding their working code....

 

Edited : tried to fix huge typos

 

BTW : you can post working (or seeming to work) code on this very forum :

* chidden bugs (string overflows, say) are more likely to be detected by 10*2 eyes than by 2...

* people who read this forum can find it nice to see well written, carefully indented and working code (it was an error of mine using this forum ... to learn C : I realised that I had to be very careful and at least wait till it was fixed not to learn .... bad C, waiting to be repaired)

Last Edited: Thu. Mar 30, 2017 - 11:52 AM