Microcontroller Firmware Hints

From RepRap
Jump to: navigation, search

Compiling and downloading Arduino Firmware using Windows

  • Fresh Install Arduino version 018 software.
  • Copy sub directories in reprap-arduino-firmware-1.3/library directory to Arduino hardware/libraries directory.
  • With reprap-arduino-firmware-1.3 you should end up with 14 sub directories below /libraries.
  • Open the appropriate Single_Arduino_SNAP.pde in Arduino.
  • Compile.
    • You will see an warning regarding obsolete header file.
      • BUT it will compile without error.
  • If you change a file in libraries which you invariably have to do to match your thermistor/thermocouple delete the object file ?????.o
    • Otherwise it will not recompile using the changes
  • This is the firmware link for the files to work under Arduino version 1.0 (I modified it for Arduino UNO board)

http://reprap.org/wiki/File:Reprap-arduino-firmware-1.3for-arduino1.0.rar#file

Sanguino Firmware

The Sanguino is an Arduino clone using the Atmega644p chip instead of the Atmeg168. The clone is software-compatible except that the pins are moved around a bit.

  • First, install the arduino software. Version 12 is noted as being somewhat buggy, but can be used with these steps. Copy files into the arduino directory from the sanguino 1.3 folder as explained on the Sanguino website under the section "use it". Follow the instructions to the letter.
  • if an error saying something about an undefined "byte" error, then go into the parameters.h tab and add the following early in the file
// from [www.arduino.cc]

// and [publications.gbdirect.co.uk]

typedef uint8_t byte; 

This should solve the problem of undefined type "byte".

Note: the first 2 lines aren't really needed, but include the places where the solver of the problem learnt how to fix the issue.

  • Make any changes in the code that you need to as described on the software page. The most common change is removing the double slash in front of #use thermistor in the thermistor tale section if you are using the standard (i.e. cheaper) thermistor method of temperature analysis.
  • Assuming your code compiles correctly, attempt to upload the code to your Sanguino, if it works first time, then you're very lucky. But most likely you'll get an error something like.
avrdude: stk500_getsync(): not in sync: resp=0x30
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51

One workaround is as follows.

  1. Get one finger ready to press the reset button on the Sanguino
  2. Use the other hand to hit File->Upload to I/O Board (or Ctrl-U).
  3. Now count to 10 and press the reset button.

After a few seconds, the light on the Sanguino should start flashing like crazy (most likely a bit of flashing at first, then a period of flashing so fast the light is almost permanently lit up). If the same error comes back, do it again but count to 10 a bit slower. This should allow the code to upload.

  • Permanent fix

I couldn't upload at all to my atmega644 until I changed the upload speed in my boards.txt. Open up arduino-0022/hardware/arduino/boards.txt, find the section for your board and look for a line similar to the following:

sanguino.upload.speed=38400

My original speed was set to 57600; lowering it to 38400 like the above fixed the problem permanently. --Pelrun 13:39, 26 June 2011 (UTC)

Initial testing on a Sanguino

If you've just soldered up your Sanguino a good first program to run is Blink. You can find it from the menu in the Arduino host software at File => Sketchbook => Examples => Digital => Blink

The program will toggle “digital pin 13” which is pin 19 on the Atmega644p chip and the leg labeled 13 on the Sanguino board. This pin does not have an LED attached so you will see nothing flash.

If you want to see something flash change the line “int ledPin = 13;” to “int ledPin = 0;”. Compile and upload this and it will toggle digital pin 0 causing the red debug LED to flash.

Refer to the schematic and the pin-out diagram and review.

Compiling and downloading Arduino Firmware using OSX

( Based on the research papers, some information from here: http://reprap.org/bin/view/Main/Generation2Electronics#Program_the_Arduino )

  • Fresh Install Arduino version 012 software, including USB to serial drivers. follow this page: http://arduino.cc/en/Guide/MacOSX We suggest installing the software to the /Applications/arduino-0012/ folder.
  • Download the most recent RepRap Arduino firmware source: http://sourceforge.net/project/showfiles.php?group_id=159590&package_id=256565
  • Copy sub directories in reprap-arduino-firmware-1.3/library (or any later version downloaded from sourceforge https://reprap.svn.sourceforge.net/svnroot/reprap) directory to Arduino hardware/libraries directory.
  • With reprap-arduino-firmware-1.3 you should end up with 18 sub directories below /libraries.
  • If you were to try to "build" your sketch now, it would error out with math.h or stdlib.h errors.
  • To fix, edit the arduino-0012/hardware/cores/arduino/wiring.h and comment out the 7 defines listed here ( or surround them in a #ifdef like below, which is equivalent) :
#ifdef USE_PESKY_MACROS
#define int(x)     ((int)(x))
#define char(x)    ((char)(x))
#define long(x)    ((long)(x))
#define byte(x)    ((uint8_t)(x))
#define float(x)   ((float)(x))
#define abs(x) ((x)>0?(x):-(x))
#define round(x)     ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
#endif
  • start the Arduino 12.app
  • Open the appropriate Single_Arduino_SNAP.pde in arduino.
  • Compile.
  • You will see an error regarding obsolete header file.
  • BUT it will compile without error.
  • If you change a file in libraries which you invariably have to do to match your thermistor/thermocouple delete the object file ?????.o
  • Otherwise it will not recompile using the changes

Sanguino Firmware on Ubuntu Linux

You can't use the Ubuntu arduino package with Sanguino, so forget about it.

  • Assuming you want to run the latest and greatest software (as of March 2010) (you do) copy/paste this into a shell:
sudo apt-get install -y gcc-avr avr-libc avrdude default-jre librxtx-java
wget http://reprap.svn.sourceforge.net/viewvc/reprap/trunk/mendel/firmware/FiveD_GCode.tar.gz?view=tar -OFiveD_GCode.tgz
tar -zxvf FiveD*
wget http://arduino.googlecode.com/files/arduino-0018.tgz
tar -zxvf arduino-0018.tgz
wget http://sanguino.googlecode.com/files/Sanguino-0018r1_1_4.zip
unzip Sanguino*
cp Sanguino/cores/arduino/ arduino-0018/hardware/arduino/cores/sanguino
cp Sanguino/bootloaders/atmega644p/ arduino-0018/hardware/arduino/bootloaders/
cd arduino-0018
./arduino &
  • file->open
  • now laboriously double-click through directories until you find FiveD_Gcode_Interpreter.pde
  • tools->boards->Sanguino
  • hit CTRL-R (compile) and you should see no errors and a message like: Binary sketch size: 17688 bytes (of a 63488 byte maximum)

arduino deletes the compiled binary when you quit, so: find the last firmware compiled in arduino and put it somewhere safe

cp -i `ls -drt /tmp/build*|tail -n1`/FiveD_GCode_Interpreter.cpp.hex ./

now edit the file /etc/udev/rules.d/45-atmel.rules to give you permission to use the libusb so you can upload a bootloader to the chip

sudo nano /etc/udev/rules.d/45-atmel.rules

put this crap in it. it probably won't work anyway

# udev rules file for atmel usb devices (for udev 0.98 version)
#
SUBSYSTEM!="usb_device", GOTO="atmel_rules_end"
ACTION!="add", GOTO="atmel_rules_end" 
ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="2103", MODE="0660", GROUP="plugdev"
ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="2104", MODE="0660", GROUP="plugdev"
ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="2107", MODE="0660", GROUP="plugdev"
LABEL="atmel_rules_end"


  • Connect your chip programmer to the sanguino ICSP header (3x2 pins) -

note that the Sanguino usb/external power switch may be labeled backwards. you should have a green LED lit on the device before attempting to program it.

  • now in arduino tools->burn bootloader
  • did it work? yes? if not, see below.

note: now would be a good time to make sure you're about to plug in the serial cable the right way (green and black are marked on the board)

  • now in arduino click 'upload'

arduino sucks

if arduino hates you or maybe you just didn't pray hard enough to the fluffy IDE gods, this didn't work. well, we can upload the bootloader with avrdude instead.

sudo avrdude -patmega644P -cavrispmkII -Pusb -U flash:w:hardware/arduino/bootloaders/atmega644p/ATmegaBOOT_644P.hex

if you're using usbtiny:

sudo avrdude -patmega644P -cusbtiny -U flash:w:hardware/arduino/bootloaders/atmega644p/ATmegaBOOT_644P.hex

now try 'upload' again in arduino.

oh look arduino sucks and the bootloader doesn't work anyway, imagine that. here's how you install just the firmware, with no bootloader:

sudo avrdude -patmega644P -cavrispmkII -Pusb -U flash:w:FiveD_GCode_Interpreter.cpp.hex

if you keep getting device ID="0x0000" the chip is probably not connected correctly. make sure it's powered up :)

soon to come

testing the software, tips for hunting gremlins, yak shaving recipes, and more!

Arduino Firmware using Ubuntu Linux

After downloading the linux firmware from the arduino site, If you cannot select the ttyUSB0 port from the arduino software menu - the entire tools/serial port menu is greyed out - the it could be a permission problem for /dev/ttyUSB0 If you run as root

 sudo ./arduino

then the USB port can be selected and it can be uploaded.

You can also add your normal user to the 'dialout' group (through the System>Administration>Users and Groups menu). After rebooting, you can then run the arduino software as your normal user.

Note this also causes problems for the RepRap host software (can't open port /dev/usb0) and also for any G-Code uploaders (like ReplicatorG)


Driving Steppers with the Extruder Controller V2.2 (Arduino inside...)

extruder controller and stepper; Blu-tack so you can see it turning...

This short Arduino program shows how to use the two H-bridge chips in the Extruder Controller V2.2 to drive a stepper. Note that it deliberately messes up on-board Timer 0 in order to drive the PWM (i.e. analogWrite(...) ) at very high frequency. This means that calls to delay(N) will no longer wait N milliseconds, but will come back 64 times faster.

The program uses the potentiometer on the board to turn the current to the motor up and down.

If you want full stepping, just use the even-numbered cases in the switch statement.


/*
 * How to drive a stepper with the RepRap Extruder controller v2.2
 *
 * Adrian Bowyer 1 July 2009
 *
 */
 
// Control pins for the A3949 chips

#define H1D 7
#define H1E 5
#define H2D 8
#define H2E 6
#define POT 0

// We will half-step;  coilPosition will take values between 0 and 7 inclusive

byte coilPosition;

// This variable stores the value (0..255) of the on-board potentiometer.  This is used 
// to vary the PWM mark-space values in analogWrites() to the enable pins, hence 
// controlling the effective motor current.

byte potValue;
bool h1Enable;
bool h2Enable;


void setup()
{
  pinMode(H1D, OUTPUT);
  pinMode(H1E, OUTPUT);  
  pinMode(H2D, OUTPUT);
  pinMode(H2E, OUTPUT);
  pinMode(POT, INPUT);
  
  // Change the frequency of Timer 0 so that PWM on pins H1E and H2E goes at
  // a very high frequency (64kHz see: 
  // http://tzechienchu.typepad.com/tc_chus_point/2009/05/changing-pwm-frequency-on-the-arduino-diecimila.html)
  // ****NOTE  This will mess up timings in the delay() and similar functions; they will no longer work in
  // milliseconds, but 64 times faster.
  
  //First clear all three prescaler bits:
  
  TCCR0B &= ~(0x07); //AND the value in TCCR0B with binary number "11111000"
  TCCR0B |= 1; //OR the value in TCCR0B with binary number "00000001"
  
  coilPosition = 0;  
}

// Demo program just spins the motor.  

void loop()                     // run over and over again
{
  motor_click();
  coilPosition++;
  coilPosition &= 7;
  
  // Remember the value fed to delay is not 1000 milliseconds, but about
  // 15.6 ms (= 1000/64).
  
  delay(1000);
}

// This writes the appropriate pattern to the output pins for
// the value of coilPosition.

void motor_click()
{
  // Disable the coils
  
  digitalWrite(H1E, 0);
  digitalWrite(H2E, 0);
  
  // Which of the 8 possible patterns do we want?
  // The commented out setPower(...) lines could
  // be used to run the coils at constant power (with
  // half-stepping not each step is equal).  Just
  // use the value of the argument to setPower(...) to
  // scale the PWM values.
  
  switch(coilPosition) 
  {
  case 7:
    //setPower((stepPower >> 1) + (stepPower >> 3));
    digitalWrite(H1D, 1);    
    digitalWrite(H2D, 1);
    h1Enable = true;
    h2Enable = true;    
    break;
    
  case 6:
    //setPower(stepPower);
    digitalWrite(H1D, 1);    
    digitalWrite(H2D, 1);
    h1Enable = true;
    h2Enable = false;   
    break; 
    
  case 5:
    //setPower((stepPower >> 1) + (stepPower >> 3));
    digitalWrite(H1D, 1);
    digitalWrite(H2D, 0);
    h1Enable = true;
    h2Enable = true; 
    break;
    
  case 4:
    //setPower(stepPower);
    digitalWrite(H1D, 1);
    digitalWrite(H2D, 0);
    h1Enable = false;
    h2Enable = true; 
    break;
    
  case 3:
    //setPower((stepPower >> 1) + (stepPower >> 3));
    digitalWrite(H1D, 0);
    digitalWrite(H2D, 0);
    h1Enable = true;
    h2Enable = true; 
    break; 
    
  case 2:
    //setPower(stepPower);
    digitalWrite(H1D, 0);
    digitalWrite(H2D, 0);
    h1Enable = true;
    h2Enable = false; 
    break;
    
  case 1:
    //setPower((stepPower >> 1) + (stepPower >> 3));
    digitalWrite(H1D, 0);
    digitalWrite(H2D, 1);
    h1Enable = true;
    h2Enable = true; 
    break;
    
  case 0:
    //setPower(stepPower);
    digitalWrite(H1D, 0);
    digitalWrite(H2D, 1);
    h1Enable = false;
    h2Enable = true; 
    break; 
    
  }
  
  // How much is the pot turned up?
  // Divide it by 4 to spread the valid readings out a bit.
  // This is about right for a 1A 3-ohm/coil stepper.
  
  potValue = analogRead(POT)>>2;
  
  // Send the appropriate PWM values
  
  if(h1Enable)
    analogWrite(H1E, potValue);
  else
    analogWrite(H1E, 0);
    
  if(h2Enable)
    analogWrite(H2E, potValue);
  else
    analogWrite(H2E, 0);
}

Using 5D Firmware with 3D GCode generation tools

There's a compatibility script to use older GCode files (with M101 to start extrusion) with the newer firmware interpreter. Please see 3D-to-5D-Gcode-php. It also has many other nifty features.

Further reading