Sun Microsystems V2.0 manual Writing a device driver

Page 40

2.Of all threads executing a Thread.sleep() it determines which will resume soonest. If the sleep interval is less than the minimum deep sleep time it shallow sleeps.

3.If the sleep interval is at least the minimum deep sleep time and deep sleeping is enabled, it sends a request to the SleepManager thread.

4.The SleepManager checks whether the Sun SPOT is connected to USB. If it is, a shallow sleep is performed.

5.The SleepManager requests each driver to “tear down”, saving any state that must be preserved into RAM and releasing any resources such as interrupt lines and PIO pins it has acquired from other drivers. Finally, each driver returns a status indicating whether it was able to do this successfully. If any driver failed, all drivers are reinitialized and a shallow sleep is performed.

6.If all drivers succeed, a deep sleep is performed. The low level firmware is programmed to wake the Sun SPOT up at the requested time and main power is turned off.

When the firmware restores main power to the Sun SPOT it resumes execution of the Java VM. The SleepManager then requests each driver to reinitialize its hardware using the state it stored before deep sleeping. Finally, the SleepManager does a brief shallow sleep until the end of the allotted driver wake up time allowance and resumes execution of the user program. The deep sleep appears to be transparent, except that external events such as radio packets arriving may have been missed during the deep sleep.

Writing a device driver

In deep sleep mode the CPU will not be able to receive interrupts from peripherals. Furthermore, the peripherals themselves are switched off, so they cannot detect external events or trigger interrupts. Only the low level firmware's real time clock continues to operate, allowing the Sun SPOT to wake up after a predetermined interval. Therefore, if the Sun SPOT deep sleeps it could miss external events unless drivers are written appropriately.

If you write your own device driver and you want to support deep sleep your driver must implement the IDriver interface. This has two methods “tearDown” and “setUp” which will be called indirectly by the SleepManager when the Sun SPOT deep sleeps and wakes, respectively. The tearDown method should only return true if the driver approves the request to deep sleep. A driver also has a method “name” that is used to identify the driver in messages to the user. The Sun SPOT has a DriverRegistry with which the driver should register itself with in order to participate in the tear down/set up sequence. A simple example of a confirming driver can be found in the class com.sun.spot.peripheral.Led. Note that this class is not public and hence in not described in the javadoc but its source can be found in <sdk>/src/spotlib_source.jar.

One constraint on device driver authors is that device drivers may receive interrupts while processing tearDown and setUp. The radio driver handles this by vetoing deep sleep unless an application has previously disabled the radio and consequently its interrupts. An alternative strategy is to turn off interrupts at the beginning of tearDown, and reenable them at the end of setUp.

It can prove difficult to debug tearDown() and setup() methods as these are only normally invoked when the USB port is not connected to a host computer, which means that diagnostic messages cannot be seen. To overcome this method, you can execute code like this:

Spot.getInstance().getSleepManager().enableDiagnosticMode();

The effect of this will be that the SleepManager will go through the full process of tearing down even though USB is enumerated. It will then enter a shallow sleep for the requisite period, and then set up the drivers as though returning from deep sleep.

40

Image 40
Contents Page Page Contents Http protocol support Introduction Building and deploying Sun Spot applications Deploying and running a sample applicationBuild Successful Total time 3 seconds \MyApplication Ant -Dport=COM2 info Total time 4 seconds \MyApplication Total time 0 seconds \MyApplication Deploying a pre-existing jar Incorporating utility classes into your applicationExcluding files from the compilation Manifest and resourcesOther user properties Built-in properties Using the BasestationOverview Set up Remote operation Base Station configurationIntroduction Connect a Sun Spot base station Using short names for SPOTs Managing keys and sharing Sun SPOTsTake suitable actions during over-the-air downloads BackgroundChanging the owner of a Sun Spot Sharing Sun SPOTsWhat is protected? Deploying and running a host applicationGenerating a new key-pair LimitationsConfiguring network features Your own host applicationIncorporating pre-existing jars into your host application Mesh routingTrace route Hardware configurations and USB powerLogging Page Threads Overview of an applicationThread priorities Sun Spot device library Sun Spot device librariesDevice Interface Persistent properties Accessing flash memory Overriding the Ieee addressUsart Radio communication library Program Radiogram protocol Client end Server endYou can open server radiogram connections in a similar way Broadcasting Radio properties Turning the receiver off and on Monitoring radio activity Conserving power using deep sleep modeShallow Sleep Activating deep sleep mode Deep SleepUSB inhibits deep sleep Preconditions for deep sleepingDeep sleep behaviour of the standard drivers Device Condition to permit deep sleepWriting a device driver Configuring the http protocol Http protocol supportConfiguring projects in an IDE Socket Proxy GUI modeJavadoc/source configuration Classpath configurationDebugging Ant selectapplication Configuring NetBeans as a debug client Configuring Eclipse as a debug clientUsing library suites Advanced topicsWith adderlib as your current directory, execute the command Use the command Using the spot client SpotSerialPortException other exception in serial port comms Persistent system properties ReferenceProperty name Meaning Contents of the arm directory Contents of the lib directoryMemory usage Start address Space UseContents of the bin directory preverify.exe Contents of the upgrade directory Contents of the tests directory