AN10369. UART/SPI/I 2 C code examples. Document information. Abstract Simple code examples are provided for UART0, SPI and I 2 C.



Similar documents
AN LPC1700 timer triggered memory to GPIO data transfer. Document information. LPC1700, GPIO, DMA, Timer0, Sleep Mode

Implementing SPI Master and Slave Functionality Using the Z8 Encore! F083A

DATA SHEET. BC875; BC879 NPN Darlington transistors DISCRETE SEMICONDUCTORS. Product specification Supersedes data of 1999 May 28.

4-bit binary full adder with fast carry CIN + (A1 + B1) + 2(A2 + B2) + 4(A3 + B3) + 8(A4 + B4) = = S1 + 2S2 + 4S3 + 8S4 + 16COUT

USER GUIDE EDBG. Description

AVR151: Setup and Use of the SPI. Introduction. Features. Atmel AVR 8-bit Microcontroller APPLICATION NOTE

AN10866 LPC1700 secondary USB bootloader

DATA SHEET. BST50; BST51; BST52 NPN Darlington transistors DISCRETE SEMICONDUCTORS. Product specification Supersedes data of 2001 Feb 20.

INTEGRATED CIRCUITS. NE558 Quad timer. Product data Supersedes data of 2001 Aug Feb 14

INTEGRATED CIRCUITS I CODE SLI. Smart Label IC SL2 ICS20. Functional Specification. Product Specification Revision 3.1 Public. Philips Semiconductors

AN10811 Programming SPI flash on EA3131 boards Rev May 2009 Application note Document information Info Content Keywords Abstract

PAC52XX Clock Control Firmware Design

Design and Implementation of Home Monitoring System Using RF Technology

PC Base Adapter Daughter Card UART GPIO. Figure 1. ToolStick Development Platform Block Diagram

DISCRETE SEMICONDUCTORS DATA SHEET

Embedded Systems Design Course Applying the mbed microcontroller

Develop a Dallas 1-Wire Master Using the Z8F1680 Series of MCUs

Serial Communications

ARM Thumb Microcontrollers. Application Note. Software ISO 7816 I/O Line Implementation. Features. Introduction

Software User Guide UG-461

AND8336. Design Examples of On Board Dual Supply Voltage Logic Translators. Prepared by: Jim Lepkowski ON Semiconductor.

AN11008 Flash based non-volatile storage

Freescale Semiconductor, Inc. Product Brief Integrated Portable System Processor DragonBall ΤΜ

AVR317: Using the Master SPI Mode of the USART module. 8-bit Microcontrollers. Application Note. Features. Introduction

How To Add A Usb Secondary Ipo Bootloader To An Lpc23Xx Flash Device To A Flash Device

Lab Experiment 1: The LPC 2148 Education Board

UART Boot Loader Design on the Kinetis E Series

AN3998 Application note

AVR1309: Using the XMEGA SPI. 8-bit Microcontrollers. Application Note. Features. 1 Introduction SCK MOSI MISO SS

Software development and debugging for NXP ARM7 MCUs

AN LPC1700 RTC hardware auto calibration. Document information. RTC, Hardware Auto Calibration, LPC1700, Graphic LCD

AN10860_1. Contact information. NXP Semiconductors. LPC313x NAND flash data and bad block management

AN3332 Application note

ES_LPC4357/53/37/33. Errata sheet LPC4357/53/37/33. Document information

RN-131-PICTAIL & RN-171-PICTAIL Evaluation Boards

AN141 SMBUS COMMUNICATION FOR SMALL FORM FACTOR DEVICE FAMILIES. 1. Introduction. 2. Overview of the SMBus Specification. 2.1.

Application Note: AN00141 xcore-xa - Application Development

76-77 GHz RF Transmitter Front-end for W-band Radar Applications

AVR1510: Xplain training - XMEGA USART. 8-bit Microcontrollers. Application Note. Prerequisites. 1 Introduction

Hello, and welcome to this presentation of the STM32L4 reset and clock controller.

APPLICATION NOTE. AT07175: SAM-BA Bootloader for SAM D21. Atmel SAM D21. Introduction. Features

Exception and Interrupt Handling in ARM

Old Company Name in Catalogs and Other Documents

MFRD52x. Mifare Contactless Smart Card Reader Reference Design. Document information

ELAN DIGITAL SYSTEMS LTD. SL232 PC- CARD USER S GUIDE

QorIQ espi Controller Register Setting Considerations and Programming Examples

AN3155 Application note

Scanning Comparator (ScanComp) Features. General Description. Input/Output Connections. When to Use a Scanning Comparator. clock - Digital Input* 1.

Chapter 13. PIC Family Microcontroller

Serial port interface for microcontroller embedded into integrated power meter

Floating Point C Compiler: Tips and Tricks Part I

In-System Programmer USER MANUAL RN-ISP-UM RN-WIFLYCR-UM

Introduction the Serial Communications Huang Sections 9.2, 10.2 SCI Block User Guide SPI Block User Guide

USB2.0 <=> I2C V4.4. Konverter Kabel und Box mit Galvanischetrennung

AT15007: Differences between ATmega328/P and ATmega328PB. Introduction. Features. Atmel AVR 8-bit Microcontrollers APPLICATION NOTE

How to design and implement firmware for embedded systems

TURBO PROGRAMMER USB, MMC, SIM DEVELOPMENT KIT

Application Note AN_299. FT800_FT801_Internal_Clock _Trimming

SC16IS740/750/760. Single UART with I 2 C-bus/SPI interface, 64 bytes of transmit and receive FIFOs, IrDA SIR built-in support

Software Real Time Clock Implementation on MC9S08LG32

Block 3 Size 0 KB 0 KB 16KB 32KB. Start Address N/A N/A F4000H F0000H. Start Address FA000H F8000H F8000H F8000H. Block 2 Size 8KB 16KB 16KB 16KB

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

StrongARM** SA-110 Microprocessor Instruction Timing

Command Processor for MPSSE and MCU Host Bus Emulation Modes

AN3265 Application note

ZL30136 GbE and Telecom Rate Network Interface Synchronizer

Programming Audio Applications in the i.mx21 MC9328MX21

APPLICATION. si32library. Callback CMSIS HARDWARE. Figure 1. Firmware Layer Block Diagram

Using the RS232 serial evaluation boards on a USB port

AN3252 Application note

Using the HCS12 Serial Monitor on Wytec Dragon-12 boards. Using Motorola s HCS12 Serial Monitor on Wytec s Dragon-12 boards

Freescale Semiconductor, I

HEF4021B. 1. General description. 2. Features and benefits. 3. Ordering information. 8-bit static shift register

DISCRETE SEMICONDUCTORS DATA SHEET M3D848. CGD MHz, 20 db gain power doubler amplifier. Product specification 2002 Oct 08

Monitoring TMS320C240 Peripheral Registers in the Debugger Software

Internal Oscillator. I 2 C Interface. SPI Interface. Controller. Internal Registers Port Controller. Eight I/O Pins. Figure 1.

AN1754 APPLICATION NOTE

Using the HT46R46 I/O Ports to Implement Half-Duplex SPI Communication

SKP16C62P Tutorial 1 Software Development Process using HEW. Renesas Technology America Inc.

Quad 2-input NAND Schmitt trigger

Elettronica dei Sistemi Digitali Costantino Giaconia SERIAL I/O COMMON PROTOCOLS

SMD version of BUK118-50DL

TWR-KV31F120M Sample Code Guide for IAR Board configuration, software, and development tools Rev.0

AN Boot mode jumper settings for LPC1800 and LPC4300. Document information

14-stage ripple-carry binary counter/divider and oscillator

S/PDIF Software Component

Definitions and Documents

Bootloader with AES Encryption

Low-Jitter I 2 C/SPI Programmable Dual CMOS Oscillator

SPI I2C LIN Ethernet. u Today: Wired embedded networks. u Next lecture: CAN bus u Then: wireless embedded network

AD9125-EBZ Quick Start Guide

AN1229. Class B Safety Software Library for PIC MCUs and dspic DSCs OVERVIEW OF THE IEC STANDARD INTRODUCTION

Quick Start Guide. MRB-KW01 Development Platform Radio Utility Application Demo MODULAR REFERENCE BOARD

8051 MICROCONTROLLER COURSE

Lesson 16 Analog-to-Digital Converter (ADC)

AVR319: Using the USI module for SPI communication. 8-bit Microcontrollers. Application Note. Features. Introduction

Keil C51 Cross Compiler

How To Fix An Lmx9838 Bluetooth Serial Port Module With Bluetooth (Bluetooth 2) From A Bluetooth Bluetooth 4.2 Device With A Bluembee 2.2 Module

D-PAK version of BUK117-50DL

Transcription:

UART/SPI/I 2 C code examples Rev. 01 06 April 2005 Application note Document information Info Keywords Content UART, SPI, I 2 C Abstract Simple code examples are provided for UART0, SPI and I 2 C.

Revision history Rev Date Description 01 20050406 Initial version Contact information For additional information, please visit: http://www.semiconductors.philips.com For sales office addresses, please send an email to: sales.addresses@www.semiconductors.philips.com Application note Rev. 01 06 April 2005 2 of 17

1. Introduction 2. UART0 This application note provides code samples, which will enable the user to get a jumpstart into using some of the serial communication interfaces of the LPC2000 family. In this application note code samples are provided for UART0, SPI and I 2 C-bus. For detailed description on the peripheral please refer to the User manual of the respective device. The basic startup assembly code is only shown for the I 2 C-bus peripheral. For UART0 and SPI, the basic startup code should setup the stack pointer for the Supervisor mode, which could be done using the LDR (Load Register) instruction. LDR SP, =0x4.. The code was tested on a LPC2106 evaluation board, which uses a 10 MHz crystal (system clock). The on-chip PLL has not been used for the sample code examples given below. If the user uses the code for a different crystal setting then be sure to set the baud rate/speed of the peripheral accordingly before running the example code. Speed calculations equations are provided for each peripheral. All the code samples are configured to run from SRAM and they were compiled using the ADS (ARM Development Suite) v1.2 compiler. Though the below code samples have been successfully tested on the LPC2106 it should work fine on rest of the Philips LPC2000 family devices after some minor modifications. Minor modifications could be setting the Stack pointer correctly (depending upon the SRAM present on chip), using the correct header files etc. If the end user wishes to run the code from the on-chip Flash then a signature is to be placed at 0x14 and the code has to be linked differently in addition to other changes. The below code sample configures UART0 to interface to a Terminal program running on a host machine (maybe Tera Term Or HyperTerminal) at a baud rate of 9600. The code simply prints Philips LPC on the host machine terminal program forever since it is included in a while (1) loop. VPB Divider value is at its reset settings and hence the peripheral clock is one fourth of the system clock. The VPB clock would then be 2.5MHz. Steps on calculating the divisor value are shown below. 2.1 Calculating Baud rate Baud rate is calculated using the following formula: Required baud rate= VPB clock/ (16 * Divisor value) 9600 = 2.5MHz /16 * x x = 16.2 = 0x10(after discarding the decimal part) Application note Rev. 01 06 April 2005 3 of 17

This value needs to be entered into the U0DLL register as shown below. 2.2 C Code /* Include header file depending upon device been used */ #include"lpc2.h" void Initialize(void); /* Macro Definitions */ #define TEMT (1<<6) #define LINE_FEED 0xA #define CARRIAGE_RET 0xD /************************* MAIN *************************/ int main() int i; char c[]="philips LPC"; Initialize() /* Print forever */ while(1) i=0; /* Keep Transmitting until Null character('\0') is reached */ while(c[i]) U0THR=c[i]; i++; U0THR=LINE_FEED; U0THR=CARRAIGE_RET; /* Wait till U0THR and U0TSR are both empty */ while(!(u0lsr & TEMT)) /*************** System Initialization ***************/ void Initialize() /* Initialize Pin Select Block for Tx and Rx */ PINSEL0=0x5; /* Enable FIFO's and reset them */ U0FCR=0x7; /* Set DLAB and word length set to 8bits */ U0LCR=0x83; Application note Rev. 01 06 April 2005 4 of 17

/* Baud rate set to 9600 */ U0DLL=0x10; U0DLM=0x0; /* Clear DLAB */ U0LCR=0x3; /*********************************************************/ 2.3 Terminal Program settings This code was tested on Tera Term Pro v2.3 and the settings for the serial port are shown below. Fig 1. Tera Term serial port settings 2.4 Output The output of the above code should be similar to the screen shown in Fig 2. Application note Rev. 01 06 April 2005 5 of 17

Fig 2. Code output 3. SPI The following code sample configures the SPI as a master and transmits data bytes on the MOSI pin. Output waveforms are captured on the oscilloscope and shown below. Since no slave device is physically connected to the master, SSEL should be driven high (does not apply to the LPC213x family). MISO is not being used in this example. Also the VPB clock is set to system clock (10MHz) and SPI is run at maximum speed (SPCCR=0x8). CPOL and CPHA both are set to 0. 3.1 Speed Calculation Speed of SPI= VPB clock/ SPCCR value= 10MHz/8= 1.25 MHz. 3.2 C Code /* Include header file depending upon part used */ #include"lpc2.h" void Initialize(void); /* Macro Definitions */ #define SPIF (1<<7) #define DATA 0xC1 /************************* MAIN *************************/ int main() Application note Rev. 01 06 April 2005 6 of 17

Initialize(); /* Do forever */ while(1) /* Write data out */ SPDR=DATA; /* Wait for transfer to be completed */ while(!(spsr & SPIF)) /*************** System Initialization ***************/ void Initialize() /* Configure Pin Connect Block */ PINSEL0=0x5500; /* Set pclk to same as cclk */ VPBDIV=0x1; /* Set to highest speed for SPI at 10 MHz- > 1.25 MHz */ SPCCR=0x8; /* Device selected as master */ SPCR=0x20; /*********************************************************/ 3.3 Output Waveforms from Oscilloscope are shown in Fig 3. Application note Rev. 01 06 April 2005 7 of 17

0xC1 Fig 3. Waveforms 4. I 2 C In the above code examples for UART and SPI, interrupts have not been used but they have been used for this example. I 2 C has been classified as an IRQ interrupt. LPC2106 is being used as a Master Transmitter and a Philips port expander PCF8574 is used as a slave device. Waveforms are shown to help the user to understand the communication better. VPB Divider value is at its reset settings and hence the peripheral clock is one fourth of the system clock (10 MHz). 4.1 Calculation of Bit frequency Bit Frequency = pclk/ (I 2 CSCLH + I 2 CSCLL) Since the maximum speed the PCF8574 could interface to the LPC2106 is100 KHz 100KHz= 2.5MHz/(I 2 CSCLH + I 2 CSCLL) Therefore I 2 CSCLH + I 2 CSCLL=25 We select I 2 CSCLH=13 & I 2 CSCLL=12 Application note Rev. 01 06 April 2005 8 of 17

4.2 Setting of the slave address PCF8574A has the following address 0 1 1 1 A2 A1 A0 In the test setup, A1 was driven high. A2 and A0 are driven low. 4.3 State Diagram (with regard to I 2 C states) Only three I 2 C states are considered in this example namely 0x8, 0x18 and 0x28. The code flow is shown below. For detailed description on the I 2 C states, please refer to the User Manual of the respective device. The section describing I 2 C has been recently updated for the LPC213x User Manual and this section will be updated for all LPC 2000 Family devices in future revisions of the User Manual. Start condition transmitted I 2 C master enters State 8H and transmits slave address + Write bit Slave acknowledges I 2 C master enters State 18H and transmits data byte 55H Slave acknowledges I 2 C master enters State 28H and transmits Stop condition Fig 4. State diagram code flow 4.4 Code The files used here are as follows: 1. Interrupt Vector table Application note Rev. 01 06 April 2005 9 of 17

2. Startup Assembly code 3. Main C file 4. Header file 5. Tool specific file (not shown here) Only the first three files are discussed and shown below. 4.4.1 Interrupt Vector table ; --------------------------------------------------------- ; Assembler Directives ; --------------------------------------------------------- AREA IVT, CODE ; New Code section CODE32 ; ARM code IMPORT start ; start symbol not ; defined in this ; section Entry ; Defines entry point ; --------------------------------------------------------- LDR PC, =start LDR PC, Undefined_Addr LDR PC, SWI_Addr LDR PC, Prefetch_Addr LDR PC, Abort_Addr NOP LDR PC, [PC, #-0xFF0] LDR PC, FIQ_Addr Undefined_Addr DCD Undefined_Handler SWI_Addr DCD SWI_Handler Prefetch_Addr DCD Prefetch_Handler Abort_Addr DCD Abort_Handler FIQ_Addr DCD FIQ_Handler ; --------------------------------------------------------- ; Exception Handlers ; --------------------------------------------------------- ; The following dummy handlers do not do anything useful in ; this example. They are set up here for completeness. Undefined_Handler B Undefined_Handler SWI_Handler B SWI_Handler Prefetch_Handler B Prefetch_Handler Abort_Handler B Abort_Handler Application note Rev. 01 06 April 2005 10 of 17

FIQ_Handler B FIQ_Handler END Startup Assembly code: ; --------------------------------------------------------- ; Assembler Directives ; --------------------------------------------------------- AREA asm_code, CODE ; New Code section CODE32 ; ARM code IMPORT main ; in this section EXPORT start ; main not defined ; global symbol ; referenced in ; ivt.s ; --------------------------------------------------------- start ; Enable interrupts MSR cpsr_c,#0x13 ; Set SP for Supervisor mode. Depending upon ; the available memory the application needs to set ; the SP accordingly LDR SP,=0x4. ; Setting up SP for IRQ mode. Change mode to ; IRQ before setting SP_irq and then ; switch back to Supervisor mode MRS R0, CPSR BIC R1, R0,#0x1F ORR R1, R1,#0x12 MSR cpsr_c, R1 LDR SP, =0x4. MSR cpsr_c, R0 ; Jump to C code LDR lr, = main MOV pc, lr END 4.4.2 C code #include"lpc210x.h" void Initialize(void); Application note Rev. 01 06 April 2005 11 of 17

/* I 2 C ISR */ irq void I 2 C _ISR(void); /* Master Transmitter states */ void ISR_8(void); void ISR_18(void); void ISR_28(void); /*************************** MAIN ************************/ int main() /* Initialize system */ Initialize (); /* Send start bit */ I 2 C ONSET=0x60; /* Do forever */ while(1) IOCLR=0x40; IOSET=0x40; /*************** System Initialization ***************/ void Initialize() /* Remap interrupt vectors to SRAM */ MEMMAP=0x2; /* Initialize GPIO ports to be used as indicators */ IODIR=0xF0; IOSET=0xF0; /* Initialize Pin Connect Block */ PINSEL0=0x50; /* Initialize I 2 C */ I2CONCLR=0x6c; /* clearing all flags */ I2CONSET=0x40; /* enabling I 2 C */ I2SCLH=0xC; /* 100 KHz */ I2SCLL=0xD; /* Initialize VIC for I 2 C use */ VICINTSEL=0x0; /* selecting IRQ */ VICINTEN= 0x200; /* enabling I 2 C */ VICCNTL0= 0x29; /* highest priority and enabled */ VICVADDR0=(unsigned long) I2C_ISR; /* ISR address written to the respective address register*/ Application note Rev. 01 06 April 2005 12 of 17

/********************** I 2 C ISR **************************/ irq void I2C_ISR() int temp=0; temp=i2stat; switch(temp) case 8: ISR_8(); break; case 24: ISR_18(); break; case 40: ISR_28(); break; default : break; VICVADDR=0xFF; /* I 2 C states*/ /* Start condition transmitted */ void ISR_8() /* Port Indicator */ IOCLR=0x10; /* Slave address + write */ I2DAT=0x74; /* Clear SI and Start flag */ I2CONCLR=0x28; /* Port Indicator */ IOSET=0x10; Application note Rev. 01 06 April 2005 13 of 17

/* Acknowledgement received from slave for slave address */ void ISR_18() /* Port Indicator */ IOCLR=0x20; /* Data to be transmitted */ I2DAT=0x55; /* clear SI */ I2CONCLR=0x8; /* Port Indicator */ IOSET=0x20; /* Acknowledgement received from slave for byte transmitted from master. Stop condition is transmitted in this state signaling the end of transmission */ void ISR_28() /* Port Indicator */ IOCLR=0x80; /* Transmit stop condition */ I2CONSET=0x10; /* clear SI */ I2CONCLR=0x8; /* Port Indicator */ IOSET=0x80; /********************************************************/ 4.4.3 Linking notes for I 2 C code: Since the interrupt vectors need to be remapped to SRAM hence the interrupt vector table should be linked to 0x40000000. Remaining files could be linked after the interrupt vector table. The first instruction to be executed will be the instruction located at 0x40000000, which would be LDR PC, start PC gets transferred to the assembly code and from there to the main C code. On an IRQ interrupt, PC will execute the instruction located at the IRQ interrupt vector in the interrupt vector table. LDR PC, [PC, #-0xFF0] On execution of this instruction, PC will start executing the I 2 C ISR located in the C file. 4.5 Output waveforms In the output waveform the following are shown Start condition Stop condition Application note Rev. 01 06 April 2005 14 of 17

Slave address Data byte In the code three port indicators were used which are shown in the waveforms below. For instance consider I 2 C state 8H. void ISR_8() /* Port Indicator */ IOCLR=0x10;. IOSET=0x10; The results of the two statements are shown in the oscilloscope as channel D3 (labeled S_8). Similarly, D4 indicates state 18H and D5 indicates state 28H. Channel D2 shows all the instances when the IRQ interrupt is triggered and normal program flow (i.e. while(1) loop in C main()) is interrupted and IRQ interrupts are serviced. Channel D1 and channel D0 indicate SDA and SCLK respectively. Fig 5. Waveforms Application note Rev. 01 06 April 2005 15 of 17

5. Disclaimers Life support These products are not designed for use in life support appliances, devices, or systems where malfunction of these products can reasonably be expected to result in personal injury. Philips Semiconductors customers using or selling these products for use in such applications do so at their own risk and agree to fully indemnify Philips Semiconductors for any damages resulting from such application. Right to make changes Philips Semiconductors reserves the right to make changes in the products - including circuits, standard cells, and/or software - described or contained herein in order to improve design and/or performance. When the product is in full production (status Production ), relevant changes will be communicated via a Customer Product/Process Change Notification (CPCN). Philips Semiconductors assumes no responsibility or liability for the use of any of these products, conveys no licence or title under any patent, copyright, or mask work right to these products, and makes no representations or warranties that these products are free from patent, copyright, or mask work right infringement, unless otherwise specified. Application information Applications that are described herein for any of these products are for illustrative purposes only. Philips Semiconductors make no representation or warranty that such applications will be suitable for the specified use without further testing or modification. Application note Rev. 01 06 April 2005 16 of 17

6. Contents 1. Introduction...3 2. UART0...3 2.1 Calculating Baud rate...3 2.2 C Code...4 2.3 Terminal Program settings...5 2.4 Output...5 3. SPI...6 3.1 Speed Calculation...6 3.2 C Code...6 3.3 Output...7 4. I 2 C...8 4.1 Calculation of Bit frequency...8 4.2 Setting of the slave address...9 4.3 State Diagram (with regard to I 2 C states)...9 4.4 Code...9 4.4.1 Interrupt Vector table...10 4.4.2 C code...11 4.4.3 Linking notes for I 2 C code:...14 4.5 Output waveforms...14 5. Disclaimers...16 6. Contents...17 Koninklijke Philips Electronics N.V. 2005 All rights are reserved. Reproduction in whole or in part is prohibited without the prior written consent of the copyright owner. The information presented in this document does not form part of any quotation or contract, is believed to be accurate and reliable and may be changed without notice. No liability will be accepted by the publisher for any consequence of its use. Publication thereof does not convey nor imply any license under patent- or other industrial or intellectual property rights. Date of release: 06 April 2005 Published in The Netherlands