UC3B @ High Speed

From time to time I forget how to set the UC3B CPU to run at full speed. The EVK1101 comes with a 12MHz crystal, and I use the same crystal for my board because it is a nice number that let me easily reach the 48Mhz needed for driving the USB port correctly. So I will not change it, but I will try to get the most of the CPU.
The UC3B comes with the following oscilators:

  • 2 PLL [80MHz – 240MHz]
  • Crystal Osc. [0.4MHz – 20MHz] (but I will let it fixed at 12MHz)
  • Crystal Osc. [32KHz] (The RTC clock)
  • RC Osc. [115KHz] (May be useful for low power consumption apps, but not this case.)

The Power Management unit handles all clock generators. A close look at the part of the unit responsible for driving CPU, HSB, PBA and PBB clocks is shown here:

So if we want to run our UC3B as fast as cars do in “The Fast & The Furious” (but without Nitro), we have to use PLL. The rules for setting PLL propertly are:

  1. Crystal must be between 0.4MHz and 16MHz
  2. PLL multiplication factor goes from 3 to 15
  3. PLL frequency must go from 80MHz to 240MHz

PLL output frecuency is the result of  ((Crystal frequency/ OSC_DIVIDE) *  PLL_MUL_FACTOR). As the datasheet states the maximum frequency for the UC3B is 60MHz. So the following should configuration can be applied in order to get 60MHz for CPU and 30MHz for PBA (USB will use PBB and PLL1 in order to get 48Mhz from the 12Mhz source)

Crystal Osc: 12MHz
PLL multiplication factor: 9
PLL divisor: 1
PLL0 = (12MHZ / 1) * (9+1) = 120MHz (and we are safe at this frequency)
PLL0 output divided by two = 1 (yes)
CPU frequency = PLL0 output /2 = 60MHz
PBA frequency = 30MHz (Pheriperial Bus A clock)

 
In order to get this configuration we have to choices. We can do it manually, or we can tell ASF (AVR Software Framework) to set the registers based on our needs.
The old way:

And the simplest way of configuring the frequency we want is by doing the following.

 
 
 
 
 
 
 
Of course, both ways do the same thing. But this lastest one, lets the ASF do the maths and the configuration for you.
It is important not to forget to set the CPU frequency for NewLib and to initialize the USB clock after setting the main clock source.
 

First contact with AVR Studio 5 (beta)

After years of being the lastest (and only) official IDE for AVR microcontrollers, Atmel decided to launch a new version of the famous AVR Studio. Of course between this AVR Studio 5 release (which is still beta) and AVR Studio 4, Atmel tried a new IDE for AVR32 based on Eclipse.
From my point of view the AVR32 Studio based on Eclipse was the best decision Atmel did. It brought something new all the developers that love and use AVR products. The fact is that having an IDE based on Eclipse (which is based on Java) let users run the IDE in several platforms and not only Windows (as AVR Studio 4 did). Moreover, Atmel delivered an AVR32 Toolchain for Windows and Linux, and the lastest one was easily ported to OS X.
The new version of AVR Studio (5) is based on…. Visual Studio! Yes… it can only runs on Windows (don’t try Wine, it’s like trying to drive a new BMW X5 with an old FIAT 600 engine, you will not go very far). But, let’s judge it by  how it works, and not how it was constructed.

As you might image when listening that it was based on Visual Studio, the ASM/C/C++ compiler is integrated with the instaler. And also the software framework, even though this last one is only available for AVR32 and the XMega series. There is no ASF for the 8bit family (yet).

Once the project is created, everything is very similar to AVR Studio 4. May be this is one of the reason why Atmel choose Visual Studio instead of Eclipse for the new IDE.

At a glance there are a few interesting things that were necessary to have. For example, you can import an AVR Studio 4 project into this new IDE.

As it happens with AVR32 Studio (based on Eclipse) you have a few predefined code snippets, and can add yours.

The project properties panel is more complete, you can choose different microcontrollers, and configure the toolchain in the same way you do it with Eclipse (for AVR32)

To sum up, AVR Studio 5 it’s a better IDE than Studio 4, it’s not perfect or portable to other platforms, but it gives me a good impression.

Software Framework comes to rescue: FAT32

Embedded applications make intense use of information these days, both for reading configurations and writing logs and process’ results. Developers tend to use built-in FLASH or small EEPROM memories to archive these tasks but as information gets complex, systems’ features grow and programming is done in high level languages, the need of a reliable and flexible solution takes more importance.
Is in this situation when embedded systems designers incorporates technologies available in personal computers, servers and mobile devices. Using mass storage devices like USB keys, USB-SATA disks and memory cards is a great solution for several problems including portability, capacity and reliability.
The Atmel’s AVR32 Software Framework incorporates low level drivers for SD/MMC cards, AT45DB memories and a USB mini Host driver capable of communicating with USB keys and disks. Moreover FAT12/16/32 is present as a high level API that can be connected to any low level driver.
Creating a file inside a directory, and writing something inside is as simple as this

For making use of these drivers and components the project must be configured with the following features:

  • Drivers
    • PM
    • SPI
    • USART
    • GPIO
    • INTC
  • Components
    • SD/MMC Memory Card
  • Services
    • FAT File System
    • Memory Control Access Services

Then it is necessary to set CONFIG/conf_access.h properly to activate the SD/MMC logic unit.

/*! name Activation of Logical Unit Numbers
*/
//! @{
#define LUN_0 DISABLE //!< On-Chip Virtual Memory.
#define LUN_1 DISABLE //!< AT45DBX Data Flash.
#define LUN_2 ENABLE //!< SD/MMC Card over SPI.
#define LUN_3 DISABLE
#define LUN_4 DISABLE
#define LUN_5 DISABLE
#define LUN_6 DISABLE
#define LUN_7 DISABLE
#define LUN_USB DISABLE //!< Host Mass-Storage Memory.
//! @}


/*! name Activation of Interface Features
*/
//! @{
#define ACCESS_USB DISABLED //!< MEM <-> USB interface.
#define ACCESS_MEM_TO_RAM ENABLED //!< MEM <-> RAM interface.
#define ACCESS_STREAM DISABLED //!< Streaming MEM <-> MEM interface.
#define ACCESS_STREAM_RECORD DISABLED //!< Streaming MEM <-> MEM interface in record mode.
#define ACCESS_MEM_TO_MEM DISABLED //!< MEM <-> MEM interface.
#define ACCESS_CODEC DISABLED //!< Codec interface.
//! @}

And then call this SPI initialization code from your main code:

/*! brief Initializes SD/MMC resources: GPIO, SPI and SD/MMC.
*/
static void sd_mmc_resources_init(void) {
// GPIO pins used for SD/MMC interface
static const gpio_map_t SD_MMC_SPI_GPIO_MAP = { { SD_MMC_SPI_SCK_PIN,
SD_MMC_SPI_SCK_FUNCTION }, // SPI Clock.
{ SD_MMC_SPI_MISO_PIN, SD_MMC_SPI_MISO_FUNCTION }, // MISO.
{ SD_MMC_SPI_MOSI_PIN, SD_MMC_SPI_MOSI_FUNCTION }, // MOSI.
{ SD_MMC_SPI_NPCS_PIN, SD_MMC_SPI_NPCS_FUNCTION } // Chip Select NPCS.
};
// SPI options.
spi_options_t spiOptions = {
.reg = SD_MMC_SPI_NPCS,
.baudrate = SD_MMC_SPI_MASTER_SPEED, // Defined in conf_sd_mmc_spi.h.
.bits = SD_MMC_SPI_BITS, // Defined in conf_sd_mmc_spi.h.
.spck_delay = 0, .trans_delay = 0, .stay_act = 1, .spi_mode = 0,
.modfdis = 1 };
// Assign I/Os to SPI.
gpio_enable_module(SD_MMC_SPI_GPIO_MAP, sizeof(SD_MMC_SPI_GPIO_MAP)
/ sizeof(SD_MMC_SPI_GPIO_MAP[0]));
// Initialize as master.
spi_initMaster(SD_MMC_SPI, &spiOptions);
// Set SPI selection mode: variable_ps, pcs_decode, delay.
spi_selectionMode(SD_MMC_SPI, 0, 0, 0);
// Enable SPI module.
spi_enable(SD_MMC_SPI);
// Initialize SD/MMC driver with SPI clock (PBA).
sd_mmc_spi_init(spiOptions, PBA_HZ);
}

ARM7 Programming under Mac OS X: Part 2 – GCC and Eclipse

Even though OS X default IDE is XCode (for C, Objective C, Java) and the fact that we can use XCode for programming an ARM7 MCU, using Eclipse is great because you can share your project with Linux (and Windows) users, use Mercurial, CVS or SVN as version control, among others interesting things that you can do with Eclipse.
Before going into the IDE, we must install a GCC toolchain that let us compile and generate ARM7 compatible binary files.  And of course, there is a port of GCC for ARM7 architecture for Mac. Between all the toolchains available I like Yagarto (www.yagarto.de), it’s available for Windows and Mac, and in SourceForge you can find a DMG for OS X (http://sourceforge.net/projects/yagarto/files/). The lastest release of Yagarto for Mac includes GCC 4.4.2 and Binutils 2.20, while the Windows release includes GCC 4.5.0 and the same version of Binutils. Despite of this “small” difference, it works pretty well. So, download this release, and following the Readme file. Basically you need to unpack “yagarto-4.4.2” and include two paths into the System Path (everything is very well explained in the Readme file).
Once you have installed Yagarto, open a Terminal and type “arm-elg-gcc -v”. You should see something like:
If this is what you see, then everything goes right. If not, check Yagarto documentation, it’s very clear.
As I have mentioned before, Eclipse will be the base IDE, not only because of compatibility, but also because there is a great plugin called GNUARM that help us dealing with configurations and Makefile. Download Eclipse for C/C++ Developers (http://www.eclipse.org/downloads/) and copy into your home folder. Launch it and select a workspace. Once there, go to “Help” -> “Install New Software” and configure a plugin site with the following configuration:

Disable “Group items by category” as the plugin doesn’t have a category and appears hidden. Install the plugin and restart Eclipse.
Now if you create a C or C++ project, you will see a similar window that let you create an empty project for ARM. Simply type a name for the project, select “ARM Cross Target Application” -> “Empty project”  and click “Finish”.
In the project’s property you will see all the parameters related with compiling, linking, and generating the binary file.

ARM7 Programming under Mac OS X: Part 1 – ISP

Programming on a Mac is quite similar as you do on a Linux PC, and developing for ARM7 is not the exception. In this first post I’ll show you how to setup the In System Programming feature of the LPC2138/01 in your Mac, and check that the MCU is communicating successfully with your Mac.
The first thing you have to download  is lpc21isp, this minimalistic piece of software is a programming tool that communicates with the ARM7 bootloader. You can find it at Yahoo Groups (http://tech.groups.yahoo.com/group/lpc21isp/). The lastest version (lpc21isp_180) is OS-X compatible.
Once you have it in your disk, unpack (yes, this is done automatically once downloaded) and open a Terminal. Locate the unpacked folder and type ‘make -f Makefile clean all’. This  should produce an output as you see in this screenshot

The result of the build process produces a file called “lpc21isp” which is executable. You have to invoce this file specifying the operations to perform, the serial port, the speed of the serial port, and the xtal frecuency in KHz.

For locating the serial port on a Mac, just look into /dev. In my enviroment I was using a custom USB-RS232 I have created a long time ago, which OS X identifies as /dev/tty.usbserial-A800cwuH

The following image shows how to invoke a detection and its result.

While lpc21isp says “Synchronizing” you should enter bootloader mode by holding P0.14 in low and performing a reset.

Last, but not least, here there are some pictures of the LPC2138/01 board and the Mac. The LPC2138/01 is mounted on an adapter I made, and near that adapter you can see the RS232 to USB adapter. Everything is powered with a 3.3V power supply.