Monday Apr 15, 2013

Getting Started with Java ME Embedded 3.3 on Keil Board


Today we have a great news in the embedded site: we announced the availability of Java ME Embedded 3.3 for ARM Cortex M3!

In this post I want you to show you how to create an application that turns on and off some of the LEDs available on the Keil board.  We will be using the Device Access API that provides interfaces and classes for communicating with and controlling peripheral devices attached to the board. 

Join me in this exciting trip, even if you don't have a board yourself, you can still use the emulator to run this program.

What do you need:
  1. Software:
  2. Hardware
    • Desktop computer:
      • Windows XP or later with two USB ports
    • Keil MCBSTM32F200 Cortex Board
    • Hardware debugger
    • ULINK ME or ULINK 2
    • MicroSD card SD adapter, for connecting to the desktop computer
    • Networking LAN cable with RJ-45 interface

For full instructions on how to configure your system, please check out the two videos I've created at our Java Channel in YouTube  (videos Part I, Part II).


Locating the LEDs

If you pay attention to the board, you can locate at the left hand side of the screen (bottom of the screen from the image bellow), a line of 8 small leds, 4 buttons and a small joystick.  These present a great opportunity for us to test the capabilities of the Java ME Embedded software before we connect any external device.
Keil Board

Some of these peripherals are preconfigured and ready to use. The LEDs and Buttons are part of the GPIO preconfigured pins and the joystick is part of the I2C preconfigured devices.  (Full details available at the Device Access API guide.)

For this particular demo, I'm going to use the first 4 LEDs:


Peripheral ID
Peripheral
Name
Mapped To
Configuration
1
LED1
LED PH3
portNumber = 7
pinNumber = 3
direction = GPIOPinConfig.DIR_OUTPUT_ONLY
mode = GPIOPinConfig.MODE_OUTPUT_PUSH_PULL
trigger = GPIOPinConfig.TRIGGER_NONE
initValue = false
2
LED2
LED PH6
portNumber = 7
pinNumber = 6
direction = GPIOPinConfig.DIR_OUTPUT_ONLY
mode = GPIOPinConfig.MODE_OUTPUT_PUSH_PULL
trigger = GPIOPinConfig.TRIGGER_NONE
initValue = false
3
LED3
LED PH7
portNumber = 7
pinNumber = 7
direction = GPIOPinConfig.DIR_OUTPUT_ONLY
mode = GPIOPinConfig.MODE_OUTPUT_PUSH_PULL
trigger = GPIOPinConfig.TRIGGER_NONE
initValue = false
4
LED4
LED PH10
portNumber = 8
pinNumber = 10
direction = GPIOPinConfig.DIR_OUTPUT_ONLY
mode = GPIOPinConfig.MODE_OUTPUT_PUSH_PULL
trigger = GPIOPinConfig.TRIGGER_NONE
initValue = false

The code

One important class in the Device Access API is the PeripheralManager.   This class provides methods for opening and registering peripheral devices that can be handled as Peripheral instances.  In our sample, we want open the GPIO pin associated with each LED.

The simplest way to open the GPIOPin is by calling the static method open from PeripheralManager, and provide the platform-specific numerical ID of this pin.  For example:

        GPIOPin pin1 = (GPIOPin) PeripheralManager.open(1);

If you need to specify more detailed  information about the GPIOPin you are trying to open, you can use a GPIOPinConfig object.  The GPIOPinConfig class encapsulates the hardware addressing information, and static and dynamic configuration parameters of a GPIO pin.

        GPIOPinConfig led3 = new GPIOPinConfig(
               7,            // Port Number
               7,            // Pin Number
               GPIOPinConfig.DIR_OUTPUT_ONLY,               //  Direction
               GPIOPinConfig.MODE_OUTPUT_PUSH_PULL,        
//  Mode
               GPIOPinConfig.TRIGGER_NONE,                 
//  Trigger
               true);     
// Init value

        GPIOPin pin3 = (GPIOPin) PeripheralManager.open(led3);


A PeripheralConfigInvalidException is thrown when attempting to open a peripheral device with an invalid or unsupported configuration.

Now, the following code will simply get the GPIO pin associated with the first 4 LEDs, and switch their status to on and off every second.

package keilleds;

import com.oracle.deviceaccess.PeripheralManager;
import com.oracle.deviceaccess.gpio.GPIOPin;
import com.oracle.deviceaccess.gpio.GPIOPinConfig;
import javax.microedition.midlet.*;

/**
 * @author acaicedo
 */
public class IMlet extends MIDlet {

       public void startApp() {
        System.out.println("GPIO test");

        //Configure pin 7
        GPIOPinConfig led1 = new GPIOPinConfig(
                7,
                3,
                GPIOPinConfig.DIR_OUTPUT_ONLY, 
                GPIOPinConfig.MODE_OUTPUT_PUSH_PULL,
                GPIOPinConfig.TRIGGER_NONE,
                true);


        GPIOPinConfig led2 = new GPIOPinConfig(
                7,
                6,
                GPIOPinConfig.DIR_OUTPUT_ONLY,
                GPIOPinConfig.MODE_OUTPUT_PUSH_PULL,
                GPIOPinConfig.TRIGGER_NONE,
                false);
        GPIOPinConfig led3 = new GPIOPinConfig(
                7,
                7,
                GPIOPinConfig.DIR_OUTPUT_ONLY,
                GPIOPinConfig.MODE_OUTPUT_PUSH_PULL,
                GPIOPinConfig.TRIGGER_NONE,
                true);

        GPIOPinConfig led4 = new GPIOPinConfig(
                8,
                10,
                GPIOPinConfig.DIR_OUTPUT_ONLY,
                GPIOPinConfig.MODE_OUTPUT_PUSH_PULL,
                GPIOPinConfig.TRIGGER_NONE,
                false);
       
        GPIOPin pin1 = null;
        GPIOPin pin2 = null;
        GPIOPin pin3 = null;
        GPIOPin pin4 = null;
       
        try {
            pin1 = (GPIOPin) PeripheralManager.open(led1);
            pin2 = (GPIOPin) PeripheralManager.open(led2);
            pin3 = (GPIOPin) PeripheralManager.open(led3);
            pin4 = (GPIOPin) PeripheralManager.open(led4);
           
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        if (pin1 == null || pin2 == null |
| pin3 == null || pin4 == null) {
            System.out.println("Error connecting to Pins");
        } else {
            try {
                for(int i=0; i<10; i++){
                    pin1.setValue(!pin1.getValue());// If the LED was off,
                                       //
turn it on, otherwise turn it on.
                    pin2.setValue(!pin2.getValue());
                    pin3.setValue(!pin3.getValue());
                    pin4.setValue(!pin4.getValue());
                    Thread.sleep(1000);                 
                }
                // turn all LEDs off at the end
                pin1.setValue(false);
                pin2.setValue(false);
                pin3.setValue(false);
                pin4.setValue(false);
            } catch (Exception e) {
                System.out.println("Noo");
            }

        }
    }
   
   
    public void pauseApp() {
    }
   
    public void destroyApp(boolean unconditional) {
    }
}


You can find the NebBeans project in here.

Running the code

Now lets run the code.  If you are lucky and have a Keil board lets have it connected to your computer and added to NetBeans.

  • Start NetBeans and verify that you have the Java ME SDK 3.3 plugin installed. 
  • Open KeilLEDs project.
  • Click on the phone icon, under IMlet.java Navigator to add your Keil board.  If you don't see this navigator, you probably forgot to install the plugin.  Go back to the top of this blog and make sure you install it before you continue.
    • Enter the ip address for your device, in my case I'm using static ip address 10.0.0.11
    • Click Next, make sure it's detected, and finally click on Finish.
Adding your
device
  • Select the execution model for the project:
    • Right click to your project, and go to Properties.
    • Select Platform from Category
    • Select IMPNGExternalDevice1 from the device list.
    • Click Ok.
Selecting your
device 
  • Run your project.
    • You should see the LEDs going on and off.  See sample here.

Congratulations, you just run your first Java ME Embedded application on your Keil board.

Now, if you are not so lucky to have a board with you, this is the way you can test your application using the Java ME SDK emulators.

  • Go to Tools -> Java ME -> Custom Device Editor

Adding an
emulator
  • Now you can create your own Devices, with the configuration you want.
    • First,  select IMP-NG and select New...
Adding device emulator
    • In the new screen you can:
      • Provide a name to your new device
      • Add the Pin and Ports you want to simulate.
      • The following image is an example that maps the code we previously described.  I've created two ports, 7 and 8 that maps the description of the Keil board.  Then I've created 4 Pins, again that maps to the LED description provided by Keil.
      • You could use any pin you want, as we are only running the code on an emulator, I just wanted to map this emulator as much as possible to the real device, in our case the Keil board.
Configuring your
device
  • Now, select the emulator as your new platform
    • Right-click on your project and select Properties.
    • Select Platform from Category
    • Select your new device, from the device list.  In our case MyVirtualKeilBoard.
Selecting your emulator
  • Run your project
    • Now that we are running on an emulator, you will see the emulator screen been displayed.
    • Click on the GPIO Pins tab, and see the LEDs change your value from High <-> Low.
Running on my
device

Now that we have a basic idea of the Device Access API you can start creating more interesting demos, connecting real devices to your board. Lets have FUN!






About

Angela Caicedo

Search

Categories
Archives
« April 2013 »
SunMonTueWedThuFriSat
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today