0

I'm using the Teensy 2.0 (atmega32u4) and decided to switch from Arduino IDE with Wiring to pure C. I think it's much more useful to master programming microcontrollers on that level. Wiring seems to be quite limited at some point.

It was really easy to use some basic libraries such as avr/io, or util/delay and write simple programs like "arduino hello world".

My problem comes with more complex tasks such as acting as usb mouse. On http://pjrc.com are references just to use with arduino ide/wiring. I couldn't find anything useful on google. Is there any good reference for pure C programming tennsy/arduino and/or using libraries such as "usb_mouse.h" (from pjrc.com) with pure C?


EDIT 1

Let me add some details (based of @st2000 answer).. Yes, Tennsy (and Arduino) are microcontrollers, not microprocessors. Programming avr chips in pure C doesn't require so much work as programming microprocessors. This is the simplest avr C program I could come with (typical blinky):

#include <avr/io.h>
#include <util/delay.h>

int main(void) {
        (CLKPR = 0x80, CLKPR = 0x00);
        DDRD |= (1<<6);
        PORTD &= ~(1<<6);

        int sviti = 0;

        while (1) {
                if (sviti)
                        PORTD |= (1<<6);
                else
                        PORTD &= ~(1<<6);
                sviti ^= 1;
                _delay_ms(40000);
        }
}

As you can see (@st2000), there are no dozens of setting up registers. It does not require any expensive equipment to upload resultant .HEX file - for teensy the only thing you need is a USB cable.

If you want a concrete example, I want to use this library with pure C, but it's obviously meant for wiring and arduino IDE primary. So how to use this library with pure C? And back to my main question, how to generally use libraries of this kind with pure C avr programming?

  • Datasheets? Atmel application notes? Anyway, USB programming is not an easy task - which is why people use libraries. – Majenko Sep 25 '16 at 22:09
  • You can use pure C with libraries. Just search "atmega32u4 HID Mouse" that should pop up some githubs/forums/resources/blogs. – Paul Sep 26 '16 at 6:01
  • Please try make your question more specific. Are you actually after a library so that your Teensy behaves like a mouse? What do you plan to do with it? That will help the community determine what answers to give. arduino.stackexchange.com/tour – sa_leinad Sep 26 '16 at 6:34
  • If you want to do USB on an AVR with USB hardware capability outside of the Arduino realm, you might look at LUFA. – Chris Stratton Sep 26 '16 at 13:20
  • The library you are referring to is meant to work in plain C. It does not depend on any Arduino libraries, nor does it require C++. Look at the provided example to see how to use it. To compile the example, just type make at the command line. With one caveat: all PROGMEM arrays should be made const in order to be compatible with recent releases of gcc. You can of course tune the Makefile to your taste. – Edgar Bonet Sep 27 '16 at 17:54
1

Refer to the actual processors used on the platforms to find reference material for ordinary C or C++ programming. There are different processors used on various Arduino boards. Be aware that this approach normally requires programming hardware which range from a few dollars to hundreds of dollars. Also be aware that "bare metal programming" requires the programmer to setup the processor's resources of interest. This normally involves setting dozens (as in the Arduino Uno AVR processors) if not thousands (as in Arduino Zero's ARM processors) of registers which can deal with everything from the processor's clock to interrupt vectors.

USB programming is very complex. Consider the complete description of RS232 could be contained in a white paper. The equivalent for USB would be at least a book. To simplify your efforts, we can eliminate all USB Host features as a mouse is a peripheral or USB Device. Further, you will probably want to limit your USB Device to a USB HID or Human Interface Device. This allows most OSs to easily recognize and use the mouse as the HID definition is more complete / restrictive. Also note, embedded processors which happen to have a USB port usually only support USB Devices as USB Host requires additional features & resources. Now that the scope of USB support has been narrowed, you can start searching through the different processor's libraries for what you need to create a USB HID Mouse.

  • You seem to be confusing microcontrollers and processors (literally in your text). But also, "bare metal programming" really doesn't take that much registers on Arduino/AVR, you'll only have to set up what you need, and you can use libraries or the datasheet. – Paul Sep 26 '16 at 6:05
  • I will gladly change "processor" to "micro-controllers" in the answer if you explain why. Now that I think about it, I believe "embedded processor" is the more commonly used term. As for initializing the embedded processor's registers, I agree, if you do not rely on the feature you do not need to initialize the register. Regardless, the point of the answer was to make the OP realize that he / she was giving up a key feature of the Arduino platform. The abstraction from the embedded processor's own hardware. – st2000 Sep 26 '16 at 12:58
  • Getting a "bare metal" program into this sort of chip requires no tools different from those used to get an Arduino sketch in. The same compiler can be used (even literally as installed by the Arduino IDE), and the chip can't tell the difference. – Chris Stratton Sep 26 '16 at 13:17
  • I had assumed the OP wanted to remove the Arduino boot program and do all his / her own C programming. In such a case, at minimum, you would need a 2nd Arduino to act like a programmer. Or, perhaps, pick up this or that AVR programmer. – st2000 Sep 26 '16 at 13:28
1

And back to my main question, how to generally use libraries of this kind with pure C avr programming?

just as how you would write under arduino ide -> arduino ide is nothing but pure c/c++ plus a set of canned libraries. so what you are trying to do is no different from what you are doing with arduino right now, maybe with your own libraries or libraries from folks other than the arduino folks.

nothing magic about going in "pure c".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.