Z8 IrDA Flash Loader Dustin Graves March 28, 2006 Goal • Z8 IrDA Flash Loader: Modify a flash loader to use IrDA and write a communication program (in Java) that sends Z8 programs to the Z8. You will need a computer with a working IrDA port (or purchase a IrDA dongle). Dongle must include a driver that enables access as a COM port so you can use the Java COMM API as I don't think the Java USB API is complete yet or use Linux (maybe this will work under cygwin) and the HP Labs Java IrDA API or the JIR API might work. This will be mostly Java programming. • references: – – – – HP Labs Java IrDA API JIR API Z8 IrDA appnote AN0184 Z8 flash loader appnote AN0118, appnote AN0164 Overview • IR Communication – – – – – IrDA Background Z8 Support for IrDA Windows Support for IrDA Linux Support for IrDA IR Communication Options • Flash Loader – Z8 Flash Loader Characteristics – Z8 Program Relocation • Implementation – Pico-IrDA, Z8 Server, WIN32 Client IrDA Background • Infrared Data Association – An organization that creates, promotes, and standardizes IrDA technology. • A protocol suite designed to provide wireless, line of sight connectivity between devices – Secure, reliable, and low cost point-to-point communication among devices such as PDAs, digital cameras, pagers, set-top boxes, machinery, and computer peripherals – Faulty cables and incompatible connectors/wiring are not an issue. – Devices negotiate connection speed and properties, no user configuration required – High speed IrDA devices are compatible with low speed IrDA devices (All connections start at 9600 bps) • Consists of two main subsets – IrDA Data – Data Exchange – IrDA Control (IrDA-C) – HCI (Keyboard, Mouse, etc) IrDA Data Properties • Three main transfer rates – Serial Infrared (SIR) – Asynchronous, 9600 – 115200 bps • Resembles standard UART with one start bit, 8 data bits, and one stop bit – Fast Infrared (FIR) – Synchronous, up to 4 Mb/s – Very Fast Infrared (VFIR) – Synchronous, up to 16 Mb/s • Two way communication – – – – Half-duplex Automatic device discovery Error correction Primary and secondary modes of operation • Primary device is similar to a network client – establishes connections with other devices with which to transmit data and commands • Secondary device is similar to a network server – waits for connections from other devices from which to accept data and commands • Multi-layer protocol stack – Support for multiple modes of communication with various devices. IrDA Protocol Stack • Required Layers – – – – • Optional Layers – – – – – – • IrPHY – The physical layer IrLAP – Link Access Protocol IrLMP – Link Management Protocol IAS – Information Access Service TinyTP – Tiny Transport Protocol IrOBEX – Object Exchange IrCOMM – Serial and Parallel Port Emulation IrLAN – Local Area Network Access IrTran-P – Transfer Picture Protocol IrMC – Infrared Mobile Communications New Additions – IrFM – Financial Management Transactions – IrWW – Wrist Watch Communication IrDA Protocols of Interest IAS IrOBEX IrCOMM TinyTP IrLMP IrLAP Physical Layer • IrOBEX – Transmitting small programs – One-way communication (no feedback from Z8) – Protocol handles flow control and error checking • IrCOMM – Managed transmission of programs – Two-way communication – Z8 client responds to each data block received with a send next block notification or transfer complete notification Z8 Support for IrDA • Z8F6403 support for the IrDA Protocol includes: – The Physical Layer (SIR – 9600 baud to 115.2 kbaud) – An IrLAP Framer (IrDA Lite) • AN0152 – IrDA Framer Implementation – Creates data frames for IrLAP – Performs data checksum • AN0184 – Using the IrDA Port – Provides a cleaner, simpler, and more efficient IrLAP framer BOF Address (A) IrLAP Payload Control (C) FCS Information (I) EOF IrDA Discovery Frame • Windows XP IrDA Frame captured with IrDA Framer FF3F01AB080000FFFFFFFF010000239D • What does it mean? FF 3F Address Field – IRLAP_ADDR_BROADCAST | IRLAP_ADDR_C_TEST Control Field – IRLAP_CTRL_U_XID_CMD | IRLAP_CTRL_P_F_MASK 01 AB 08 00 00 FF FF FF FF 01 00 00 Information Field - IRLAP_XID_FMT_DISCOVERY Source address Destination address Flags - IRLAP_XID_FLAGS_SLOTS_6 Slot IRLAP version 23 9D Checksum Pico-IrDA • Minimalist IrDA stack for microcontrollers – http://blaulogic.com/pico_irda.shtml • Implements IrLAP, IrLMP, IAS, and TinyTP as lower level transport layers • Implements IrOBEX for primary and secondary devices – Send and receive files • Implements IrCOMM for secondary devices – Receive data and optionally respond with data • Requires implementation of IrPHY • Source code available from project website is not ANSI C compliant – Uses zero length arrays such as ‘char info[0]’ – ANSI C compliant version is available from the download section of the Pico-IrDA Yahoo! Group • http://groups.yahoo.com/group/pico_irda Windows Support for IrDA • Windows support for IrDA varies by version – Windows 2000/XP/Server 2003 • TinyTP based TCP like support for non-serialized data transmission at VFIR rates through Windows Sockets API • IrOBEX functionality available at OS level, not exposed through Winsock API • Limited IrCOMM support (9-wire cooked) exposed through Winsock API • Communication with non-Windows devices that implement the core IrDA protocol is possible – Suggested that devices implement Winsock like client/server functionality – Windows 95/98/ME • Support for serial communication with the IrCOMM programming model – Windows CE • Support for Serial Communication • IrOBEX functionality is exposed through Winsock API • Supplemental information for Winsock IrDA programming – MSDN documentation is limited, possibly inaccurate http://www.alanjmcf.me.uk/comms/infrared/Microsoft%20Windows%20IrDA%20programming.html Communication with a Non-Windows Device IrCOMM2K Serial Port Emulator • Driver for Windows 2000/XP that provides a virtual IR COM port • Also provides irda2kdump IrDA traffic monitor – Similar to a utility provided with Linux – Available from http://www.ircomm2k.de/English Linux Support for IrDA • Low level and high level access to IR device – Support for SIR and FIR devices – Access directly as serial port – Access through IrDA module with socket API • TCP like connection oriented communication • UDP like connectionless communication – Ultra Protocol for IrWW – Multiple utilities and libraries for IrDA interaction • irdadump – IrDA traffic monitor • OpenOBEX – Open source IrOBEX implementation (may work with Windows) • Java Infrared Sockets API – More information from Linux IR HOWTO http://tuxmobile.org/Infrared-HOWTO/Infrared-HOWTO.html Linux IrDA Setup • Install/compile irda-utils and irda kernel modules • Determine the name of the infrared device – Use ‘dmesg | grep ttyS’ to list available serial devices: • ttyS0 at I/O 0x3f8 (irq = 4) 16550A • ttyS1 at I/O 0x2f8 (irq = 3) 16550A • ttyS2 at I/O 0x3f8 (irq = 4) 16550A Probably COM port Probably IrDA device Probably Modem • Load the appropriate kernel modules – ‘modprobe irda’ – ‘modprobe irtty-sir’ – ‘modprobe ircomm-tty’ • Attach the infrared device with the IrDA module and initiate device discovery – ‘irattach /dev/ttyS1 –s’ • IrDA can be started as a service with Fedora – Edit ‘/etc/modprobe.conf’ – Edit ‘/etc/sysconfig/irda’ – Enable service with ‘chkconfig irda on’ Linux irdadump Utility • Useful for debugging – Decodes and displays contents of IrDA Frames IR Communication Options • Z8 communication with Windows XP – – – – IrOBEX from Windows XP to Z8 Pico IrDA implementation IrCOMM from Winsock API to Z8 Pico IrDA implementation Possibly use IrCOMM2K for basic serial communication with Z8? Possibly use IrCOMM2K and a Windows XP Pico IrDA implementation to communicate with Z8 Pico IrDA implementation? • Z8 communication with Windows XP – Basic serial communication with Linux and Z8 – IrOBEX from Linux implementation of OpenOBEX to Z8 Pico IrDA implementation – IrCOMM from Linux to Z8 Pico IrDA implementation – Linux Pico IrDA implementation to Z8 Pico IrDA implementation One more option… • Hardware solution - Microchip MCP2140 – IrDA standard protocol stack controller with fixed 9600 baud communication rate – Implements IrLAP, IrLMP, IAS, TinyTP, and IrCOMM (9-wire cooked) • Similar to functionality provided by Winsock API • More information at – http://ww1.microchip.com/downloads/en/devicedoc/21790a.pdf Z8 Flash Loader • A program that permanently resides in Flash memory to load a users application to Flash memory when signaled at program start or to execute the user application residing in Flash memory when not signaled at program start – – – – Startup code – Execute flash loader or resident user application Communication initialization – Initialize communication device Flash I/O utilities – Basic Flash and RAM read/write utilities Data processor / Flash loader – Receive, process, and load data frames for user program – Communication interface – Interaction with Flash Loader • AN0164 – Boot Loader for Z8 Encore – Assembly implementation of a boot loader – Requires 1000 bytes of flash memory • AN0118 – Flash Loader Utility for the Z8 Encore – C implementation of a flash loader – Requires 16 kilobytes of flash memory Z8 Flash Loader Characteristics • Wait to receive signal to enter Flash Loader Mode – Monitor GPIO pin, button, UART, etc • Load User Program to Flash – Support for communication with UART – Support for Flash I/O operations – Support for Intel Hex16 format • Define start of user program – Application program start address must relocate to address required by Flash loader • Prevent user program from overwriting Flash writer – Carefully check the address for each record loaded from Intel Hex16 file • Execute user program – Jump to start of user code Z8 Flash Loader Relocation • Z8 Flash loader must be moved to a safe Flash memory location – Loading a user program requires addresses 0x00-0x37 (Flash Option Bits, Reset Vector, Interrupt Vectors) to be erased for programming – Flash memory is erased by 512 byte pages – Flash loader must start at address 0x200 to avoid being damaged by erase of first page – Relocation performed through ZDS II Project Settings dialog Z8 Flash Loader Target Settings • Target settings for Flash Loader – Target Settings specified through Address Space category of Linker tab with ZDS II 4.9.6 Z8 Flash Loader Linker Directives • Special input directive settings to place vector code before startup code – ORDER __VECTORS,STARTUP User Program Relocation • Target settings for User Program – AN0118 specified 0x4000 as start address – The IrDA Flash loader specifies 0x5000 as start address User Program Linker Directives User Program Relocation with Assembly include "ez8.inc" SEGMENT code VECTOR RESET = main ORG %5000 ; add the entry point to the reset vector ; start AFTER the interrupt vector main: CLR LDX LDX ... R0 4064,#1 4065,R0 ; PEDD = 0x00; Z8 Flash Loader Implementation • Implement IrPHY layer for Pico-IrDA – Accept programs with IrOBEX • No feedback is transmitted to sender – Accept programs with IrCOMM • Feedback is transmitted to sender indicating success/failure • Create Flash loader based on program defined by AN0118 – Similar program location strategy – Only useable component is the module for processing Intel Hex16 data and loading Intel Hex16 data to flash • Create a Windows XP program for transmitting IrCOMM data through Winsock API – Send Intel Hex16 file one line (record) at a time as 32 byte increments – Provide progress indicator – Provide final summary Pico-IrDA IrPHY Implementation • Implement four functions to initialize IrDA device, transmit data, receive data, and wait for data – void irphy_reset(void) – Initialize UART0 and enable IrDA – void irphy_send(uint8_t) – Transmit a byte – uint8_t irphy_wait(int16_t timeout) – Wait specified number of seconds for data to arrive; infinite wait for -1 – uint8_t itphy_receive(void) – Receive a byte • Requires definition of types that are typically found in inttypes.h on UNIX systems – typedef unsigned char uint8_t – … Reset, Receive, and Transmit #define BAUDRATE (18432000UL / (9600UL * 16UL)) void irphy_reset(void) { U0BRH = (unsigned char)((0xFF00&BAUDRATE)>>0x08); U0BRL = (unsigned char) (0x00FF&BAUDRATE); PAAF |= 0x30; /* Enable UART0 alternate function */ U0CTL0 = 0x40; /* Enable UART0 receive */ U0CTL1 = 0x01; /* Enable UART IR */ } uint8_t irphy_receive(void) { return U0RXD; } void irphy_send(uint8_t v) { U0CTL0 = 0x80; while((U0STAT0 & 0x04) == 0); U0TXD = v; while((U0STAT0 & 0x02) == 0); U0CTL0 = 0x40; } /* Receive byte */ /* /* /* /* /* Set UART0 to transmit */ Wait for tx buffer to be available */ Transmit byte */ Wait for tx complete */ Set UART0 to receive */ Waiting uint8_t irphy_wait(int16_t timeout) { unsigned char done = 0; if(timeout != -1) { enable_timer3(); while(!(U0STAT0 & 0x80) && !done) { if(T3H > 0x48) { if(--timeout == 0) done = 1; else { T3H = 0x00; T3L = 0x01; } } } disable_timer3(); } else { while(!(U0STAT0 & 0x80)); } return (U0STAT0 & 0x80) ? 1 : 0; } /* /* /* /* /* Timed wait */ Initialize timer */ Until read ready or timeout */ Count for 1 millisecond */ Done waiting */ /* Reset timer */ /* Disable timer */ /* Infinite wait */ Debugging Pico-IrDA for the Z8 • Time critical transactions and constant state changes make PicoIrDA difficult to debug • Pico-IrDA comes with a Linux based IrPHY Implementation – Intended for Actisys IR220L serial IrDA dongle – Will work with any serial based IrDA device – Compare Linux implementation with Z8 implementation • Differences most likely due to problems with the compiler • Problems with the Z8 compiler – The following expression is always true: #define IRLMP_DLSAP_C_MASK 0x80 if(req_p->head.dlsap_sel & ~IRLMP_DLSAP_C_MASK) – This prevents the Z8 from connecting to other devices – Replace all references to ~IRLMP_DLSAP_C_MASK with uint8_t inv = ~IRLMP_DLSAP_C_MASK if(req_p->head.dlsap_sel & inv) Windows XP Program Loader • Open Intel Hex16 file and Initialize Winsock • “Enumerate” available IrDA devices – Returns address of devices within range • Set 9-Wire mode for IrCOMM – Could be unnecessary • Select Z8 from list of devices and establish connection • Read one line from file • Transmit 32 bytes of data to Z8 and wait for response – FLASH_RESULT_DONE – Transmission is complete; print summary to console and exit – FLASH_RESULT_RESEND – Checksum error for line; print an ‘x’ to console and resend line – FLASH_RESULT_NEXT_LINE – Line has been loaded; print a ‘.’ to the console and read next line from file – FLASH_RESULT_NEXT_PART – Send next 32 byte block from current line Demonstration • IrOBEX and IrCOMM program transmission References • • • • • • • • • • http://www.zilog.com/docs/z8encore/appnotes/an0164.pdf http://www.zilog.com/docs/z8encore/appnotes/an0118.pdf http://www.zilog.com/docs/z8encore/appnotes/an0152.pdf http://www.zilog.com/docs/z8encore/appnotes/an0184.pdf http://www.web-ee.com/primers/files/irda.pdf http://www.commsdesign.com/main/9803art2.htm http://www.eix.co.uk/Articles/IrDA/Welcome.htm http://msdn.microsoft.com/library/default.asp?url=/library/enus/irda/irda/irda_start_page.asp http://www.alanjmcf.me.uk/comms/infrared/Microsoft%20Windows%20IrDA %20programming.html http://windowssdk.msdn.microsoft.com/library/default.asp?url=/library/enus/irda/irda/ircomm_client_sample_code.asp