Saturday, 24 December 2011

OverviewI use my Ubuntu 11.10 based Beagleboard as a low-power home server, which mainly stores my subversion repositories for my mobile apps development business. The server is headless and remains plugged in 24/7.So I wanted to have some sort of cloud backup system, where my repositories and some parts of the Linux filesystem (mainly /etc) were backed-up to a remote location. At most I wanted to backup about 150MB of data.After much searching around, I came up with the following two lists of possible solutions for backing up a home linux server:Free:

Dropbox - Obvious choice given that your backups would also be accesible on various different platforms, but unfortunately they don't officially support an ARM CLI client (there have been some people who have been able to hack their packages to get them working, but I didn't want the hassle). You can login to Dropbox and vote for ARM support here.

UbuntuOne - This is available for ARM, but requires a GUI interface to manage, see here. There are ways around this, as described here, but I want a simple solution.

Using the standard backup-manager tool to backup to your own online server (unfortunately I don't have access to one). For those that do, you can use either of the following methods for transferring your archive: FTP, SSH, S3 and RSYNC. From the user guide: "The FTP, SSH and S3 methods are dedicated to upload archives, using those method depends on the use of at least one backup method. On the opposite, the RSYNC method uploads a directory to remote locations, this directory can be your repository or whatever other location of your ﬁle system".

Paid:

Amazon Simple Storage Service (Amazon S3), which the standard backup-manager tool can upload to. The pricing for this is flexible, and very very cheap for small amounts of data! To upload and store 1GB of data per month, the cost is less than $1.00!!! You can play around with the calculator here, be sure to select the "Amazon S3" tab along the left hand side. (Instructions for how to do this here.)

Many other paid services, some with their own clients and others that can be used with the backup-manager package.

Which Solution?Based on the lists above and the drawbacks of each possible solution, I decided to go with using backup-manager with Amazon S3. The backups are for my business's subversion repositories, so I don't mind spending a little each month.You can follow the instructions I have created to setup backups from your Ubuntu based Beagleboard to Amazon S3 here.

These are the steps to setup a periodic backup directories on an Ubuntu 11.10 server to the Amazon S3 service:WARNING: Some irony here, but make sure you backup your data before attempting this, there *shouldn't* be much risk, but I won't be responsible if you loose any data or incur any financial loss from following any of this.1. Create Amazon S3 account & storage bucket

2. Install the necessary software packages on UbuntuRun the following to install the packages:

apt-get install backup-manager libnet-amazon-s3-perl

3. Configure backup-managerYou configure backup manager by editing the /etc/backup-manager.conf configuration file. There are settings I have modified from the default, I haven't given the values for all of the config parameters as I recommend you think about these and use your own:

export BM_ARCHIVE_TTL="..."

export BM_ARCHIVE_METHOD="tarball-incremental" - Note, this is recommended to reduce the amount of data transferred and stored on the S3 servers.

export BM_TARBALL_DIRECTORIES="..." - space separated list of the directories you would like to backup.

export BM_TARBALLINC_MASTERDATETYPE="..."

export BM_TARBALLINC_MASTERDATEVALUE="..."

export BM_UPLOAD_METHOD="s3"

Amazon S3 specific parameters of the config file, you will get the access and secret keys from the Security Credentials section of the Amazon S3 website:

export BM_UPLOAD_S3_DESTINATION="your_bucket_name"

export BM_UPLOAD_S3_ACCESS_KEY="..."

export BM_UPLOAD_S3_SECRET_KEY="..."

export BM_UPLOAD_S3_PURGE="false"

4. Test your settingsYou can test your settings by running a manual backup (this will cost money, as you will be uploading to the Amazon S3 server):

backup-manager -v

You can verify you backups were uploaded by looking at your Amazon S3 console.

5. Add the backup task to CRONI decided to have my incremental backups run daily. So created a backup-manager script in /etc/cron.daily/backup_manager, containing with the following:

#!/bin/sh
/usr/sbin/backup-manager -v

Don't forget to make the script executable.ConclusionThat's it, you should now have your backups setup and backing up to the bucket you created on the Amazon S3 service.DisclaimerI take no responsibility what-so-ever for any loss of data or financial costs incurred by someone following the info in this blog! Remember, each time you backup to the S3 service, it will cost you a certain amount of money.

Sunday, 27 November 2011

OverviewWelcome to the fifth and final part of the "Sleeping Arduino" series, where we will cover how to wake the Arduino from sleep mode using the Watchdog Timer (WDT). When waking your Arduino from sleep, you could use one of the standard internal timers of an Arduino as I have detailed in Part 4, but if you are looking for the maximum sleep time and/or minimum sleep power consumption, you have the use the WDT;

As I have mentioned in this table, the WDT can give us a sleep time of 8 seconds, whereas the 'longest' 8/18bit timer will only give us a sleep time of ~4 seconds.

Watchdog Timer (WDT)The Watchdog Timer on the Arduino's microprocessor only has one source to drive it: it's own separate internal 128kHz oscillator (as opposed to the 8/16bit internal timers, which can use either the 16Mhz system clock or an external clock). It is this separate oscillator that enables the WDT to function in the lowest power mode: SLEEP_MODE_PWR_DOWN (see here for more detailed info on the Arduino's power modes).

The WDT also has a prescaler, which is used to configure the timeout period. It supports timeout periods from 16ms to 8 seconds:

Also it has three modes of operation:

Interrupt - The WDT_vect interrupt sub-routing will be called when the WDT times out. Can be used to wake the micro from sleep modes, including the lowest power sleep mode (SLEEP_MODE_PWR_DOWN), where other timers are not available.

System Reset - A watchdog time-out reset will occur, i.e. the micro-controller will be restarted. For use in handling code lockups.

Interrupt and System Reset - First the WDT_vect interrupt sub-routing will be called, when completed a watchdog time-out reset will occur.

Please refer to my post herefor the other operation modes of the WDT. The post is for the Atmega1281, but the functionality is pretty much the same.

Code

So, we will be using interrupt mode in our code to wake the Arduino from sleep every 8 seconds to toggle the state of the LED:

Saturday, 26 November 2011

OverviewWelcome to part 4 of the Sleeping Arduino Series. In this entry we will cover how to get the Arduino to wake from sleep using one of the micro-controller's internal timers. This does not require any external hardware, all is implemented in code.Arduino TimersSo,we need to go into a little detail on the micro controller's internal timers. The Atmega168 in the Arduino Diecimila has three internal timers:

Timer/Counter 0 - 8 bit (Max timer duration: 16.4ms)

Timer/Counter 1 - 16 bit (Max timer duration: 4.1s)

Timer/Counter 2 - 8 Bit (Max timer duration: 16.4ms)

Whether the timer is 8 bit or 16 bit defines how much the timer can count up to: an 8 bit timer counting to 256 and 16 bit to 65536. What drives the counter to count can be configured to be either the internal 16Mhz clock or an external clock source on T1 pin. We will be using the internal timer in this tutorial.

Once a timer's counter has reached its maximum value and increments once more, it will overflow and the counter will reset to zero. This overflow event can be configured to trigger an overflow interrupt, which we will use to wake the Arduino from sleep mode. Note you can also modify the value within a timer's counter from your code to tune the overflow period.

Figure 1. Timer source

One thing I haven't mentioned yet is the prescaler, this is another counter that is used in conjunction with the the timer counter to extend the time between timer overflows. The prescaler can be set the the following values: 1:1, 1:8, 1:64, 1:256, 1:1024, these are the ratios of prescaler overflow to clock cycles.The following formula shows the calculation of the timeout period.

Figure 2. Timeout period formula

Take the following examples:16 bit Timer1 no prescaler (1:1)

Overflow period = 1/16Mhz x 1 x 2^16 = 4.09 milliseconds

16 bit Timer1 with prescaler of 1:1024

Overflow period = 1/16Mhz x 1024 x 2^16 = 4.09 seconds

So we can see that using the maximum prescaler value of 1:1024, we can get maximum Timer1 overflow period of 4.09 seconds, this is the maximum time we can put the Arduino to sleep for using Timer1. If you want a longer sleep period than this, you can use the Watch Dog Timer, which can provide a sleep time of about 8 seconds (it can also be used in a lower power/sleep mode than Timer1, see Arduino/Atmega168 Timers For Sleeping). Using the WDT for waking from sleep is described in the 5th part of this series.

Timers and Power Modes

As I have mentioned in part 1 of this series, not all hardware peripherals of the micro-controller are available in all power modes. Looking at our table here, we can see that timer1's lowest running power mode is IDLE, so when we put the micro-controller to sleep, we need to make sure we don't enter a sleep mode below this, or the timer will be disabled.

Control RegistersThe following are the timer related control registers we will use to configure Timer1

TCNT1 - 16bit counter register. This is the actual register that will count up each timer tick. When the value in this register rolls over from 65536 to 0, the overflow interrupt will fire.

TIMSK1 - TOIE1 - Interrupt mask register and overflow interrupt enable bit. This register contains the control bits to enable the various interrupts available to timer1, including TOIE1 which we will set to enable the overflow interrupt.

For more information on these registers, see section 16.11 of the datasheet here.

OperationOur code will operate as follows:

Configure the serial port and LED pin.

Configure Timer1's prescaler so the timer will expire every 4.09 seconds (TCCR1B).

Clear Timer1's counter TCNT1

Enable Timer1 overflow interrupt (TIMSK1).

The main loop will then:

Enter IDLE sleep mode.

When the Timer1 overflow occurs, the interrupt will wake the processor

The LED state will be toggled.

Re-enter IDLE sleep mode.

Code

In the following code the Arduino board will go to sleep (SLEEP_MODE_IDLE) for 4.09 seconds, wake up and toggle the LED and go back to sleep again.

Wednesday, 23 November 2011

The following is a table outlining the available timers of the Arduino, their max timeout period (how long you can put your Arduino to sleep for) and the lowest power saving mode that the timer can be used in. I hope it will help you in your choice of sleep timer.

Friday, 18 November 2011

OopsI recently had a problem with root access on my Beagleboard running Ubuntu 11.10 from an SD card. Basically I had accidentally removed my user account from the admin group. As it was the only user account I had set-up when installing Ubuntu: I was stuck!

SolutionThe only option that I was aware of that would allow me to recover root access, was to boot Ubuntu in single user mode and add my user account back into the admin group.I tried this by editing the UBoot bootargs parameter so I could enter Single user mode, setting it to:

Ahh, so the boot.scr script was being loaded from the boot partition on the SD card. I had a look at that file by mounting the SD card on my MAc and found that that script was overriding the bootargs parameter that I had manually set in UBoot.

Simple solution was to run the following commands in UBoot that bypassed the script on the boot partition of the SD card:

Tuesday, 27 September 2011

OverviewIn this post I will be describing how to use the Watchdog Timer of the ATmega1281 in order to detect and handle code lock-ups and at the same time, as a timer to wake the device from low power sleep mode.Note this same approach should work with the ATmega640, ATmega1280, ATmega2560 and ATmega2561 micro-controllers.This entry assumes the reader has some experience developing for the ATmega1281 micro-controller, understands how a typical Watchdog Timer works and understands the sleep modes of he ATmega1281.

Atmega1281 Watchdog Timer (WDT)FunctionalityThe WDT has three modes of operation:

Interrupt - The WDT_vect interrupt sub-routing will be called when the WDT times out. Can be used to wake the micro from sleep modes, including the lowest power sleep mode (SLEEP_MODE_PWR_DOWN), where other timers are not available.

System Reset - A watchdog time-out reset will occur, i.e. the micro-controller will be restarted. For use in handling code lockups.

Interrupt and System Reset - First the WDT_vect interrupt sub-routing will be called, when completed a watchdog time-out reset will occur.

The WDT has it's own internal clock, that gives us time-out resolutions from 16ms to 8s.

Note, after a System Reset the WDT is still enabled, also the configuration will have reverted to default, i.e. 16ms time-out, If the user doesn't re-configure or disable the WDT before that time-out you can end up with the micro in a continuous System Reset loop. The following will disable the WDT before user code is executed:

Control & RegistersThere are two sets of registers to be used when controlling the WDT:

WDTCSR - Watchdog timer Control Register, used for configuring the time-out, mode of operation, etc.

MCUSR - MCU Status Register, used to tell the cause of the last reset, such as brown-out reset, watchdog reset, etc. NOTE: for security reasons, there is a timed sequence for clearing the WDE and changing the time-out configuration. If you don't use this sequence properly, you'll get unexpected results.

AVR LibrariesAtmel provide a set of macros for controlling the WDT. If you simply want to use the WDT to detect code lock-ups, that library will be fine. If you want to do anything more advanced, you will also have to manually configure the above registers.

CodeWe want to use the WDT for two purposes simultaneously; detect/handle code lock-ups and as a timer to bring us out of sleep mode.For this we will use the WDT in interrupt mode only. Depending on why the interrupt has entered, we will either force a watchdog system reset or wake the main application from it's sleep mode.