Home

  • 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);
    }

  • Simple 2D Graphics functions

    So finally I have uploaded a first release of my 2D graphic engine for microcontrollers. Even though I would like to make it portable to several displays the code has some optimizations in order to improve rendering speed. The display used in this project is the Nokia 128×128 LCD that you can see in other projects in this page, and basically some functions write raw data to the display taking advantage of the memory layout and the fact that when you write a pixel, the pointer to the current position in LCD’s RAM moves automatically to the next one.
    Last but not least, there is a beta version of this engine that includes rotations, but it needs more testing and fixing. And there is a plan for a 3D Engine, but up to now my applications doesn’t require 3D.
    Geometry [Header]
    Geometry [Source]
    Engine 2D [Header]
    Engine 2D [Source]

  • Blackberry's Pearl controlled with ARM7

    I found really interesting a small protoboard created by Sparkfun that allows you to easily connect a Pearl (the one made famous by RIM / Blackberry devices) with a microcontroller. This time I choose the LPC2138 from NXP because I also have a complete 2D graphic engine for interfacing the LPC2138 with a Nokia 128×128 LCD with 16 bits per pixel. One issue with this device is that a small movement on the pearl is detected. And sometimes you want to move in a direction but this little ball moves in more than one direction at a time. So the big problem was designing a software able to detect the movements correctly, providing anti bouncing and a way to easily modify how many pulses of the pearl are detected as one movement.



    void pearl_Task() {
        if ((!(PEARL_INPUT_PIN & (1 << PEARL_LEFT_PIN))) && (pearl_state_left == 0)) {
            pearl_dir_left_changes++;
            pearl_state_left = 1;
        }
        else if (((PEARL_INPUT_PIN & (1 << PEARL_LEFT_PIN)))
                      && (pearl_state_left == 1)) {
            pearl_dir_left_changes++;
            pearl_state_left = 0;
        }
        if ((!(PEARL_INPUT_PIN & (1 << PEARL_RIGHT_PIN))) && (pearl_state_right == 0)) {
            pearl_dir_right_changes++;
            pearl_state_right = 1;
        }
        else if (((PEARL_INPUT_PIN & (1 << PEARL_RIGHT_PIN)))
                      && (pearl_state_right == 1)) {
            pearl_dir_right_changes++;
            pearl_state_right = 0;
        }
        if ((!(PEARL_INPUT_PIN & (1 << PEARL_DOWN_PIN))) && (pearl_state_down == 0)) {
            pearl_dir_down_changes++;
            pearl_state_down = 1;
        }
        else if (((PEARL_INPUT_PIN & (1 << PEARL_DOWN_PIN)))
                      && (pearl_state_down == 1)) {
            pearl_dir_down_changes++;
            pearl_state_down = 0;
        }
        if ((!(PEARL_INPUT_PIN & (1 << PEARL_UP_PIN))) && (pearl_state_up == 0)) {
            pearl_dir_up_changes++;
            pearl_state_up = 1;
        }
        else if (((PEARL_INPUT_PIN & (1 << PEARL_UP_PIN)))
                      && (pearl_state_up == 1)) {
            pearl_dir_up_changes++;
            pearl_state_up = 0;
        }
    }


    This task can be perform in a main loop, or can be called by a timer when it overflows (as I do in my code). The following piece of code is an example of how to ask if a movement in a particular direction was performed.



    unsigned char pearl_IsMovingLeft() {
    	if (pearl_dir_left_changes >= PEARL_DIR_LEFT_LIMIT) {
    		pearl_dir_left_changes = 0;
    		return 1;
    	}
    	return 0;
    }


    Where if PEARL_DIR_LEFT_LIMIT is between 3 or 5 and you run the pearl task every 50mSec the movement is quite smooth.

    Pearl (Blackberry’s Trackball) Driver [Header]

    Pearl (Blackberry’s Trackball) Driver [Source]

    Trackball schematic

  • iPhone App for AVR Development

    There is an infinite number of AVR website that has configuration wizards for UART, Timers and SPI, but I didn’t find an iPhone application that can do something similar. So I developed an iOS application that lets you setup the AVR SPI easily, email this configuration, and also understand what each bit of the SPI registers does. The application is called AVR ISP and soon it will be available in the App Store. Hopefully the next version (1.1) will have code generation in C and ASM, and not only for the setup, but also including functions for handling the SPI interface.
    Here I show some of the screenshots of the app. And as soon as Apple publishes the app in the Store I’ll upload the link.

    UPDATED: Apple has approved AVR SPI and now it’s ready for download from the App Store. You can find it by just looking for “AVR SPI” or using this link to iTunes

  • 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.

  • AVR32 Software Framework Drivers: USART

    As we get into AVR32 programming it’s unavoidable to use drivers from the software framework. First because we are not going to reinvent the wheel every time we want to start a new project, then because they are fully tested and documented. Moreover, they provide code-compatibility between several AVR32 families (UC3A, UC3B, UC3L, AP7…)
    I thought that a good first approach to AVR32 software framework was using and understanding the USART. The USART is essential for those who don´t have a JTAG for debugging (I know, it’s not too expensive, but may be for a student or somebody that just want to test the platform) because you can send debug content to your Mac/PC using the serial port (this means using the USART in UART mode).
    Our first program will be an UART echo program. This means that it will echo every character sent from the PC. We will learn a few things about AVR32 and how to code with this example, more than be can imagine at first.
    First of all, ensure you have the lastest version of AVR32Studio and AVR32 Software Framework. Both available and for free at http://www.atmel.com/products/AVR/. Inside AVR32Studio create a “New AVR32 C project from template”.

    In the template you will choose your board/mcu configuration. In this case, I will use EVK1101 as the board, and AT32UC3B0256 ad the MCU.

    Once the project is configured we will select the Software Framework drivers that we will use.

    Once the Software Framework drivers are selected, replace your main.c with main.c (USART)
    Let’s take a close look to the code:
    First of all we will include the header files from the framework that we will use, this includes the AVR32 IO definition, GPIO, USART and Power Manager.

    // Include Files
    #include <avr32/io.h>
    // EVK1101
    #include "board.h"
    // Drivers
    #include "power_clocks_lib.h"
    #include "gpio.h"
    #include "usart.h"

    then we will define the frequency for the PBA, which USART we will use, and which GPIO pins we will be connected to that UART

    #define TARGET_PBACLK_FREQ_HZ FOSC0  // PBA clock target frequency, in Hz
    // Usart Pins compatible with EVK1101
    #define MY_USART               (&AVR32_USART1)
    #define MY_USART_RX_PIN        AVR32_USART1_RXD_0_0_PIN
    #define MY_USART_RX_FUNCTION   AVR32_USART1_RXD_0_0_FUNCTION
    #define MY_USART_TX_PIN        AVR32_USART1_TXD_0_0_PIN
    #define MY_USART_TX_FUNCTION   AVR32_USART1_TXD_0_0_FUNCTION
    #define MY_USART_CLOCK_MASK    AVR32_USART1_CLK_PBA
    #define MY_PDCA_CLOCK_HSB      AVR32_PDCA_CLK_HSB
    #define MY_PDCA_CLOCK_PB       AVR32_PDCA_CLK_PBA

    in the main function we will configure the GPIO pins. this is done by creating a variable of type gpio_map_t which includes the pin and its function.

    	static const gpio_map_t MY_USART_GPIO_MAP = {
    			{MY_USART_RX_PIN, MY_USART_RX_FUNCTION},
    			{MY_USART_TX_PIN, MY_USART_TX_FUNCTION} };

    then we set up the UART parameters, this includes the baudrate, parity and stop among others.

    	static const usart_options_t MY_USART_OPTIONS = { .baudrate= 57600, .charlength= 8,
    			.paritytype= USART_NO_PARITY, .stopbits = USART_1_STOPBIT,
    			.channelmode = USART_NORMAL_CHMODE };

    next we configure the Power Clock in order to use the crystal connected to OSC0.

    	pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP);

    We need to tell the GPIO module that it must assign the GPIO pins to the USART

    	gpio_enable_module(MY_USART_GPIO_MAP,sizeof(MY_USART_GPIO_MAP)/sizeof(MY_USART_GPIO_MAP[0]));

    For enabling the UART the framework provides a function for initializing in RS232 mode

    	usart_init_rs232(MY_USART,&MY_USART_OPTIONS,TARGET_PBACLK_FREQ_HZ);

    And last, but not least, you can see in this few lines of code how to write and read from the UART

    	usart_write_line(MY_USART, "This is an echo program)n");
    	while(1) {
    		c = usart_getchar(MY_USART);
    		usart_putchar(MY_USART,c);
    	}
  • AVR32 UC3B Hello World!

    Inside AVR32 Studio I’ll create a new project (File -> New -> AVR32 Example). As I’m going to use the EVK1101 as my base development it may be a good idea to build my first app over a test program like “an example project”.  I have choosen the USART Example, which includes the necessary startup files for the MCU plus a small echo client, and modified it to only show “Hello World”.

    The good thing about developing over the AVR32 Software Framework is that is fully documented and you will find comments everywhere, what makes the learning curve smooth.

    After building this project, you can deploy it either by using an AVR32 Target configured inside AVR32Studio, or by calling the external tool BatchISP which cames with FLIP.
    This is a call to BatchISP for programming the “Hello World” example:

    batchisp -hardware usb -device at32uc3b0256 -operation erase f memory flash blankcheck loadbuffer HelloWorld.elf program verify start reset 0

    Running batchisp 1.2.4 on Mon Mar 29 22:56:02 2010
    AT32UC3B0256 - USB - USB/DFU
    Device selection....................... PASS
    Hardware selection..................... PASS
    Opening port........................... PASS
    Reading Bootloader version............. PASS    1.0.2
    Erasing................................ PASS
    Selecting FLASH........................ PASS
    Blank checking......................... PASS    0x00000 0x3ffff
    Parsing ELF file....................... PASS    HelloWorld.elf
    Programming memory..................... PASS    0x00000 0x02e9f
    Verifying memory....................... PASS    0x00000 0x02e9f
    Starting Application................... PASS    RESET   0
    Summary:  Total 11   Passed 11   Failed 0

  • The AVR32 UC3B Platform

    The AVR32 is not only a microcontroller, it’s a complete development platform that works smoothly on Linux and Windows, and may be OSX without too much pain. By development platform I mean having an IDE not only for building projects, but also for managing them (version control, plugins that support development, built-in programmer and debugger), a programmer that requires few external components, and a good compiler (if possible in C/C++).  If you found a platform with all these positive points, for free, and all the tools provided by the  manufacturer, then you will enjoy programming on this platform. Atmel provides all these things and a software framework that let you reach your time to market faster.
    Basically the fact that Eclipse was the IDE choosen by Atmel for developening over AVR32 plus the software framework where the things that motivated me to start learning about this new version of AVR. Moreover, the microcontroller is not a mid-range AVR, but a full 32bit MCU capable of running Linux, or a small RTOS, delivering nearly 83DMIPS at 60MHz using 3.3V and 23mA.
    My starting MCU is the AT32UC3B0256 which has the following features:

    • 256Kbytes of Flash
    • 32Kbytes of SRAM
    • 83 Dhrystone MIPS at 60MHz
    • 44 I/O pins in a 64 pins TQFT package
    • USB 2.0 Full Speed and On The Go

    A very basic diagram shows how things are connected in the AVR32 UC3B.

    Recently Atmel annouced a new member of the UC3B family, the AT32UC3B0512, which has 96Kbytes of SRAM and 512Kbytes of Flash in the same TFQF64 package.
    My first steps in this platform are guided by the ATEVK1101 which is an evalutation kit for the AT32UC3B. This kit allows a rapid and friendly development without the need of dealing with a SMD component and a custom PCB.

    The AVR32 Studio IDE can be found at: http://www.atmel.com/dyn/products/tools_card_mcu.asp?tool_id=4116
    For programming the AVR32 using the USB DFU (we need Flip: http://www.atmel.com/dyn/products/tools_card_mcu.asp?tool_id=3886
    And last but not least, Atmel provides a toolchain of GCC for compiling apps: http://www.atmel.com/dyn/products/tools_card_mcu.asp?tool_id=4118
    So let’s get started with a simple Hello World project…