Documentation:Linux/Serial
From AVRFreaks Wiki
Contents |
[edit] Using serial ports
Using the AVR32's serial ports under Linux is the same as using them under all Linux architectures. There's a great reference here
[edit] Getting access to a serial port
To get access to a serial port which isn't enabled by default you must change some kernel code.
[edit] Method 1 - Use Buildroot
If you are already doing AVR32 development you may not need this step but it is here for completeness (you can even move this to another section if you get excited). You will need a Linux host/development machine to do this. You can download the complete AVR32 development Virtual Machine image from http://www.atmel.no/beta_ware/ (http://www.atmel.no/beta_ware/avr32/AVR32_Linux_BSP_DVD_Image_3.0.0-beta.2.iso).
NOTE: I have no idea how you download a 3.4GB file with a normal browser. Please let me know if you figure it out. I actually created a virtual machine and installed Ubuntu 8.04 (LTS) from http://www.ubuntu.com (http://www.ubuntu.com/getubuntu/download) onto it. I then installed the "reduced" version of the BSP 858MB (http://www.atmel.no/beta_ware/avr32/AVR32_Linux_BSP_CD_Image_3.0.0-beta.2.iso).
NOTE: The following steps are applicable the the ATNGW100 device, Ubuntu 8.04 LTS Development OS and Linux version 2.6.25.10 for the target device (ATNGW100). These steps can be substituted for other valid devices (e.g. ATSTK1000, etc). I have not tried any other target Linux versions but the changes should be basically the same. For noobs I would recommend using the Atmel recommended AVR32 development OS (currently Ubuntu 8.04 LTS) even if you are familiar with another Linux flavour. Most AVR32 documentation references steps performed in Ubuntu and I actually found it really easy to use (and it boots quickly too!).
If you used "reduced" version you will need to install the BSP. You may even need the following steps if you downloaded the full virtual machine image, I haven't checked.
[edit] Initial Setup
I followed the instructions on the BSP CD under "Getting started with AVR32 Studio and AVR32 Linux development" (<BSP CD Path>/documentation/getting_started_with_avr32_studio_and_avr32_linux_development.html). Under the Installation subheading follow the four steps.
1. Install AVR32 GNU Toolchain (follow -> installing tools on Ubuntu Linux -> Installing packages from the AVR32 Linux BSP)
2. Download AVR32 Buildroot (you can skip this step as we are using the BSP)
3. Build AVR32 Buildroot (I used the Getting started section and copied the buildroot folder from the BSP cd to my home drive on my Ubuntu development virtual machine and entered that buildroot directory and ran:
make atngw100_defconfig make source make
This will produce a baseline copy of the project for your ATNGW100 under <path to buildroot>/project_build_avr32/atngw100/
4. Install AVR32 Buildroot (pretty straight-forward change to the PATH environment variable). However I used a slightly different format. I added the following line to THE END of my ~/.profile script. This step is mainly for AVR32 Studio:
PATH="<path to buildroot>/build_avr32/staging_dir/bin:$PATH".
You can complete the steps under "Getting started with AVR32 Studio and AVR32 Linux development" to install and use AVR32 Studio, but this is not necessary for adding Serial Ports.
[edit] Create Source Patch
Ok this is the tricky part. Buildroot is very automated and just simply adding the serial port code to the source code is not enough as Buildroot retrieves the source code from a kernel archive on each build and overwrites your changes. So you need to create a patch. These steps were retrieved from an avrfreaks post (http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59415) so appropriate credit should go to those involved in that post. So here goes:
1. Install quilt (you only need to do this once):
apt-get install quilt
2. cd into the kernel source directory:
cd <path to buildroot>/project_build_avr32/atngw100/linux-2.6.25.10/
3. Run:
quilt new enable-two-usarts.patch
NOTE: It is possible to make the quilt edit command below use another editor apart from vi by setting the EDITOR environment variable. See below how to change it to gedit. You can substitute gedit for your favourite editor:
EDITOR=gedit export EDITOR
4. Run:
quilt edit arch/avr32/boards/atngw100/setup.c
And make the following changes (I hope you know how to use vi :): Add the following two lines in the function setup_board(void) below "at32_map_usart(1, 0);":
at32_map_usart(2, 1); /* USART 2: /dev/ttyS1 */ at32_map_usart(3, 2); /* USART 3: /dev/ttyS2 */
Then add the following two lines in the function atngw100_init(void) below "at32_add_device_usart(0);":
at32_add_device_usart(1); at32_add_device_usart(2);
NOTE: At this point it is also possible to add two more SPI devices if required. The following was taken from another avrfreaks post (http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=64228) so due credit to those involved. These are a little more complex to configure however the following code configures two default 1MHz SPI devices using SPI0 chip select 1 and 2 using default polarity etc. You will need specific information on the SPI device before making these changes as you may need to specify more and or different parameters. Read <path to buildroot>/project_build_avr32/atngw100/linux-2.6.25.10/Documentation/spi/spidev for more information. NOTE: As mentioned before the following SPI change IS NOT REQUIRED for adding more serial ports, it is included if you need to add SPI devices as well. Add the following two entries after the closing curly bracket and comma (},) in the spi0_board_info[] structure, so the contents of the structure now looks like this (the first mtd_dataflash entry is existing):
{
.modalias = "mtd_dataflash",
.max_speed_hz = 8000000,
.chip_select = 0,
},
{
.modalias = "spidev", /* 1MHz Device */
.max_speed_hz = 1000000,
.chip_select = 1,
},
{
.modalias = "spidev", /* 1MHz Device */
.max_speed_hz = 1000000,
.chip_select = 2,
}
Then save and exit.
5. Run:
quilt refresh
6. Copy (and rename) the patch to the target patch device directory:
mkdir <path to buildroot>/target/device/Atmel/atngw100/kernel-patches cp patches/enable-two-usarts.patch <path to buildroot>/target/device/Atmel/atngw100/kernel-patches/linux-2.6.25.10-100-atngw100-enable-two-usarts.patch
7. Someone also noted uImage needed to be removed to trigger a kernel rebuild so remove it too:
rm <path to buildroot>/project_build_avr32/atngw100/linux-2.6.25.10/arch/avr32/boot/images/uImage
8. Return to buildroot and rebuild the kernel:
cd <path to buildroot>/ make
When the build is complete (check for errors), check if the changes to setup.c stuck (I like to use gedit):
gedit <path to buildroot>/project_build_avr32/atngw100/linux-2.6.25.10/arch/avr32/boards/atngw100/setup.c
If the code you entered above is shown then the kernel has been successfully built and the new kernel can be loaded onto the ATNGW100.
NOTE: I have had trouble with recompiling the kernel, as I am relatively new to this I don't fully understand how it works. If you run make help from the <buildroot path> it will give you some information however I found the easiest way to force a full rebuild including the new patch is to delete the project and perform make as below:
cd <path to buildroot>/ rm -r project_build_avr32/atngw100/ make
[edit] Upload Modified Kernel to Target
[edit] Update uBoot
When you run "make" in buildroot it also creates an updated uBoot binary. For whatever reason, you may want to update uBoot on the target. See Documentation:AVR32 Linux Development/Upgrading U-boot for good information on using flash-upgrade to create a uBoot boot image to safely update uBoot. I updated uBoot using a serial connection (kermit) as shown below.
1. Connect to the ATNGW100 via serial link and a host terminal program (I used Hyperterminal) that can support kermit transfer mode.
2. Boot the ATNGW100 and halt at the uBoot prompt (press <space>)
3. On the terminal, enter the following uBoot command to receive the file over kermit:
loady 0x10400000
4. On the host terminal locate the flash-upgrade.uimg file and select kermit transfer mode and press send. The ATNGW100 will show the file being received.
5. Once received, on the terminal enter the following commands to update uBoot:
bootm 0x10400000
6. Follow the instructions on the console.
[edit] Upload Kernel ***NOTE THESE STEPS ARE EXPERIMENTAL PROCEED WITH EXTREME CAUTION AND READ REFERENCED LINKS BELOW FOR EXTRA INFORMATION***
The compiled kernel files are located in <path to buildroot>/binaries/atngw100/. If you followed step 3. Build AVR32 Buildroot in the section above, you would have noticed at the bottom of the web page a section Deploy binares to target system. You should have all these files except for the ext2 ones, which doesn't appear to matter. If the target linux kernel created by buildroot differs from the version already on the target board (ATNGW100) then I recommend you do a full flash update (including uBoot). Remember to backup any config files or data files stored on flash before doing this. See Documentation:NGW/Firmware upgrade for various methods of doing this. I updated uBoot first then performed the following to update Linux using an SD card (a slight variation on the steps in Documentation:NGW/Firmware upgrade.
1. Remove all partitions from an SD card (at least 32MB). I use gparted.
2. Copy the NGW firmware image to the SD card. Assuming that sdb is the device of your SD card:
~# sudo dd if=ngw_fw_upgrade.img of=/dev/sdb
3. Remove and reinsert the SD card and ensure it is mounted as a 32MB disk.
4. Use gparted again to resize the partition to cover the full capacity of the SD card.
5. Remove and reinsert the SD card and ensure it is mounted as the full capacity of the SD card.
6. Rename all instances of the old flash filesystem image files to the new names:
~# gedit /media/disk/etc/init.d/copyfiles replace all instances of the text ngw_jffs_root.img with rootfs.avr32.jffs2-root replace all instances of the text ngw_jffs_usr.img with rootfs.avr32.jffs2-usr
7. Copy the newly created flash file system image files onto the SD card:
cp <path to buildroot>/binaries/rootfs.avr32.jffs2-root /media/disk/ cp <path to buildroot>/binaries/rootfs.avr32.jffs2-usr /media/disk/
You have now created a bootable kernel that will automatically update the ATNGW100 flash filesystem everytime you use the uBoot commands below. Note these files can be safely left on the SD card and it will not overwrite the filesystem unless the following uBoot commands are entered.
8. Install SD card into ATNGW100 and power it up and press <space> to display the uBoot prompt and enter the following:
Uboot> askenv bootcmd Please enter 'bootcmd': mmcinit; ext2load mmc 0:1 0x10300000 uImage; bootm 0x10300000 Uboot> set bootargs 'console=ttyS0 root=/dev/mmcblk0p1 ro' Uboot> boot
9. This will boot the ATNGW100 from the SD card and copy the new filesystem to the ATNGW100.
10. Reboot, press space to get a uBoot console and then type
~#setenv bootcmd 'fsload boot/uImage;bootm' ~$saveenv ~$boot
10. Type the following to ensure your new kernel is installed by verifying the date is correct:
uname -v
11. From now on if you make drastic changes to the ATNGW100 filesystem, you just need to repeat the steps from step 7 above.
12. If you modify the kernel and loadable modules you will only need to copy the new kernel image (uImage) to the ATNGW100 /boot directory and copy the kernel modules from [TBA] to [TBA]
13. If you only modify the kernel you will only need to copy the new kernel image (uImage) to the ATNGW100 /boot directory.
