31
Application Development Using .Net MF on The LPC2000 Pankaj Shrivastava Product Line Microcontrollers Business Line Standard IC’s

Application Development Using .Net MF on The LPC2000rtcgroup.com/arm/2007/presentations/246 - Application Development... · – .Net 2.0 Software Development Kit ... Object-oriented

Embed Size (px)

Citation preview

Application Development Using .Net MF on The LPC2000

Pankaj Shrivastava

Product Line MicrocontrollersBusiness Line Standard IC’s

http://www.nxp.com/microcontrollers2

Agenda

Introduction– .NET Micro Framework

Getting Started

C# Introduction

Accessing Hardware Peripherals

I2C Real Time Clock Application Demonstration

References

http://www.nxp.com/microcontrollers3

What is .NET?

Microsoft's application development platform

Easily create Windows and Web applications

Supports Multiple languages– CSharp– C++– Visual Basic

Executes MSIL (Microsoft Intermediate Language)

Just-in-Time Compilation

Automatic memory management

http://www.nxp.com/microcontrollers4

.Net Micro Framework

Small .NET runtime for embedded devices – Minimum 256K RAM and 512K Flash/ROM

Implements a subset of the .Net libraries

Only supports CSharp (C#) language

Adds support for common hardware peripherals– GPIO– UART– SPI– I2C

Supports ARM7 and ARM9 CPUs

Extensible Hardware emulator– Simulates target hardware

http://www.nxp.com/microcontrollers5

Architecture

http://www.nxp.com/microcontrollers6

.Net Micro FrameworkSystem.WebSystem.Web System.Windows.FormsSystem.Windows.Forms

System.DataSystem.Data System.XMLSystem.XML

SystemSystem

Services• Description• Discovery• Protocols

UI• HTML controls• Web controls

Design

ConfigurationCache

Session stateSecurity

ImagingDrawing 2D

TextPrinting

DesignADO.NET

SQL ServerCESQL Client

Xslt/XPathXML Document

Reader/writersSerialization

Service processConfiguration

ThreadingThreadingDiagnosticsDiagnostics

NetNetIOIO

ResourcesResourcesReflectionReflection

SecurityCollectionsCollections

GlobalizationGlobalizationText

Component model

Runtime

System.DrawingSystem.Drawing

Interop servicesRemoting

Serialization

http://www.nxp.com/microcontrollers7

Getting started

Host Computer– Windows XP– .Net 2.0 Software Development Kit– .NET Micro framework SDK ( samples and emulator )– Visual Studio 2005 ( optional for HW target )– Serial or USB port for Target Device connection– MFDeploy or FlashLite (program HW Target device)

Target Device– ARM7 or ARM9 hardware platform with .Net MF port

• E.g. NXP LPC2294 Phytec Development boardOr– .Net Micro Framework device emulator ( requires VS 2005 )

CSharp application

http://www.nxp.com/microcontrollers8

C# to ARM Machine Code

Intermediate Language Code

C# Source code

C# Compiler or VisualStudio2005

VS Debugger or FlashLite or MFDeploy

ARM7/ARM9 specific instructions

Common Language Runtime (CLR)

Target Hardware or Emulator

UART or USB

http://www.nxp.com/microcontrollers9

CSharpNXP

http://www.nxp.com/microcontrollers10

First C# Program - HelloWorldusing System;

namespace HelloWorld

{

public class HelloWorldExample

{

public static void Main()

{

while(true)

{

Debug.Print( "Hello, World!" );

}

}

}

}

http://www.nxp.com/microcontrollers11

CSharp

Object-oriented language

C# application is a collection of classes

A class contains data, methods ( or member functions )– Needs to be instantiated– new keyword instantiates an object

One class should implement the Main() method– Entrypoint of the program

Namespaces– Provide unique identifiers for collection of classes

http://www.nxp.com/microcontrollers12

Frequently used variable types

bool or System.Boolean– 1 byte– true (1) or false (0)

byte or System.Byte– 1 byte– 0 to 255

ushort or System.Uint16– 2 bytes– 0 - 65535

uint or System.UInt32– 4 bytes– 0 to 4294967295

http://www.nxp.com/microcontrollers13

Flow Control & Operators

Same syntax as C for flow control– while (condition) { execute statements}– for( i= 0 ; i < xx ; i++ ) {execute statements }– do { execute statements } while (condition)– if ( condition ) { statements } else { statements }

Same syntax as C for various operators– Assignments

=, += , -=, *=, /=, %=, &=, |=– Logical comparisons

==, >, >=, <, <=,!=, &&, ||– Bitwise

&, |

http://www.nxp.com/microcontrollers14

Csharp common build errors

The name ‘xyz’ does not exists in the class or namespace– Forgot to declare a variable or Typo– variable declared in different scope

Use of unassigned local variable ‘x’– Initialize variable before use

‘class_name.member_name’ is inaccessible due to its protection– Forgot public keyword or trying to access private member

Cannot implicitly convert type ‘x’ in to ‘y’– Using two different variable types in the same expression

‘method_name’ : not all code paths return a value- void method declared as non void- forgot to return a value or if statement returns without value

http://www.nxp.com/microcontrollers15

Accessing Hardware PeripheralsNXP

http://www.nxp.com/microcontrollers16

Hardware peripherals as objects.Net Micro Framework provides classes which represent hardware

– Microsoft.SPOT.Hardware

Pin6 Pin5 Pin4 Pin3 Pin2 Pin0Pin1

LED Switch EEPROM RTC

OutputPortwrite(bool)

HAL/PAL Native code

InputPortbool read()

HAL/PAL Native code

SPI

HAL/PAL Native code

write(wr[]) read(rd[])

I2C

HAL/PAL Native code

execute transaction

http://www.nxp.com/microcontrollers17

SPI - 1

Create SPI Object

spiconfig = new SPI.Configuration( (Cpu.Pin)10, // GPIO port pin used for CS

false, // CS Active state LOW

1, // CS Setup time in uS

1, // CS Hold time in uS

false, // Clock idle state

true, // Sampling Clock Edge

Clk_KHz, // SPI Clock rate in KHz

SPI.SPI_module.SPI1 // SPI Peripheral

);

eeprom = new SPI(spiconfig);

http://www.nxp.com/microcontrollers18

SPI - 2

Write data to SPI Device

eeprom.Write( byte [ ] wdata); // 8 bit write

eeprom.Write( ushort [ ] wdata); // 16 bit write

Write/Read data to/from SPI Device

eeprom.WriteRead( byte [ ] wdata, byte [ ] rdata, rdoffset ); // 8bit write/read

eeprom.WriteRead(ushort [ ] wdata, ushort [ ] rdata, rdoffset);// 16bit write/read

http://www.nxp.com/microcontrollers19

I2C

Create an I2C object i2cfg = new I2CDevice.Configuration( I2CAdr, Clk_KHz );

i2rtc = new I2CDevice( i2cfg );

Write data to I2C deviceI2CDevice.I2CTransaction xAction = i2rtc.CreateWriteTransaction( byte [ ] data );

transferred = i2rtc.Execute( xAction , Timeout );

Read data from I2C DeviceI2CDevice.I2CTransaction xAction = i2rtc.CreateReadTransaction( byte [ ] data );

transferred = i2rtc.Execute( xAction ,Timeout );

http://www.nxp.com/microcontrollers20

Serial Port

Create serial port object

serialport = new ComPort( COM1,

BaudRate,

true // Flow control

);

Write to Serial Portserialport.Write( byte [ ] wdata, offset, count );

Read From Serial Portcount = serialport.Read(byte [ ] rdata , offset , rdata.Length, Timeout);

http://www.nxp.com/microcontrollers21

GPIO

Create GPIO output objectoutputport = new OutputPort( (Cpu.Pin)8, true );

Write to GPIO pinoutputport.Write( true or false );

Create GPIO input objectinputport = new InputPort((Cpu.Pin)8), GlitchFilter, ResistorMode)

Read GPIO inputportstatus = inputport.Read();

http://www.nxp.com/microcontrollers22

GPIO Interrupts

Create GPIO interrupt object

IntrPort = new InterruptPort((Cpu.Pin)8

, GlitchFilter

, ResistorMode

, InterruptMode

);

Connect interrupt handller with GPIO pin

IntrPort.OnInterrupt += MyInterruptHandler;

http://www.nxp.com/microcontrollers23

I2C RTC Application DemonstrationNXP

http://www.nxp.com/microcontrollers24

I2C Real Time Clock ApplicationMain.cs (CSharp source file)

RTC8564

BCD

SetTime(Time time)

GetTime(Time ref time)

SetDate(Date date)

GetDate(Date ref date)

toBCD(byte bin)

toBin(byte bcd)

I2C_DEMO

Main()

Create RTC8564 object

Create Blinky Thread

Print Demo Message

Set Time & Date

Print Time & Date

http://www.nxp.com/microcontrollers25

Hardware

Single Board Computer (53mm x 62mm)

Memory: • SRAM : 1MB to 8MB• FLASH : 1MB to 16MB• EEPROM : 1KB to 8KB

I2C Real Time Clock with calendar & alarm function

Two UART interfaces with RS232 transceivers

10/100Mbit SMSC LAN91C111 Ethernet Controller

Phytec phyCORE-ARM7/LPC229x

http://www.nxp.com/microcontrollers26

Building I2C RTC Demo

http://www.nxp.com/microcontrollers27

Flashing RTC Demo using Flashlite

http://www.nxp.com/microcontrollers28

I2C RTC Demo

http://www.nxp.com/microcontrollers29

References

NXP LPC2000 Microcontrollers– http://www.standardics.nxp.com/products/lpc2000/

Microsoft .Net Micro Framework website– http://msdn2.microsoft.com/en-us/embedded/bb267253.aspx

Phytec NXP LPC2294 Board– http://www.phytec.com/products/sbc/ARM-XScale/phyCORE-ARM7-

LPC229x.html

Books– Embedded Programming with the Microsoft .NET Micro Framework -

Donald Thompson and Rob S. Miles– C# 2005 for Dummies - Stephen Randy Davis & Chuck Sphar

http://www.nxp.com/microcontrollers30

I2C RTC Application Code Walkthrough

Main.cs

http://www.nxp.com/microcontrollers31