APPLICATION NOTE Atmel AT01244: DALI Slave Reference Design Atmel 8-bit Microcontrollers Features • • • Compliant with international standard IEC62386-101, 102, 207 Reference firmware design includes application, service and drivers Reference hardware made of three main parts: • DALI interface, which bridges the voltage between DALI bus and microcontroller • Microcontroller, which communicates with DALI master and controls the LEDs • LED driver, which drives the LED controlled by microcontroller Description DALI (Digital Addressable Lighting Interface) is an international standard created for lighting control and now becomes one of the more popular lighting interfaces today. DALI protocol is a digital communication interface with master-slave structure, aiming as a control gear between power supply and LED. DALI slave communicates with DALI master based on half duplex signal with baud rate of 1200 bit/s. Frame is defined as the sequence of bits used to transmit data. Forward frame is from master to slave and backward from slave to master. The definition of bit and frame can be found in IEC62386-102. Figure 1 shows the DALI slave system block diagram based on Atmel® megaAVR® device. Figure 1. DALI slave system block diagram. For this reference design, the hardware design files (schematic, BoM and PCB gerber) and software source code can be downloaded from Atmel website. The provided hardware documentation can be used with not limitations to manufacture the reference hardware solution for the design. 42071B−AVR−07/2013 Table of Contents 1. Related Items ....................................................................................... 3 2. Hardware Modules ............................................................................... 4 2.1 DALI hardware interface ................................................................................... 4 2.2 Microcontroller................................................................................................... 4 2.3 LED driver ......................................................................................................... 4 3. Firmware Modules ............................................................................... 6 3.1 Drivers ............................................................................................................... 6 3.2 Service .............................................................................................................. 6 3.2.1 Frame State Convert........................................................................... 6 3.2.2 Frame message process .................................................................... 7 3.3 Application ........................................................................................................ 8 3.4 Logarithm dimming table ................................................................................... 9 3.4.1 high_PWM_val and low_PWM_val ..................................................... 9 3.4.2 flash_fade_rate_val and flash_inv_fadetime_val .............................. 10 Appendix A. Command Sets .............................................................. 13 Appendix B. Schematic ...................................................................... 15 Appendix C. Revision History ............................................................ 18 Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 2 1. Related Items The following list contains links to the most relevant documents, software, and tools for DALI slave: • IEC 62386-101, IEC 62386-102, IEC 62386-207 The IEC standards define the specific of DALI slave. • Atmel ATmega88PA datasheet ATmega88PA is the microcontroller used in this solution. • Atmel LED Drivers-MSL2041/MSL2042 datasheet MSL2041 is used as LED driver. Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 3 2. Hardware Modules As previously described, there are mainly three hardware modules. 2.1 DALI hardware interface DALI hardware interface bridges the voltage level between DALI bus and microcontroller. According to the DALI protocol, the HIGH level is 9.5V to 22.5V, and the LOW level is -6.5V to 6.5V. The special logic level should be converted to the logic which MCU can accept. The interface logic should be 0 to 5V for the MCU (Atmel ATmega88PA) used in this design. Figure 2-1. DALI hardware interface. Current Source Optocoupler MCU Rx Comparator DALI Bus Rectify Bridge Optocoupler MCU Tx Two optocouplers are used to isolate the voltage level between the DALI bus and microcontroller. Using the comparator in above circuit has advantage over other simplified design in: • • • 2.2 Suitable for all working temperature range Trim the input waveform of signal for MCU Tolerant the difference of components for mass production Microcontroller ATmega88PA is one of the most popular devices in the Atmel MCU family. It is used as the main microcontroller in this reference design. ATmega88PA handles the commands and data which comes from the DALI interface part. 2.3 LED driver Atmel MSL2041 is used as the LED driver in this design. It drives and controls the LEDs based on the input PWM signal from MCU. It also monitors the current and voltage of LEDs and feeds back to MCU the error status if error occurs. Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 4 Figure 2-2. DALI LED driver. Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 5 3. Firmware Modules The firmware is developed based on layer structure as Atmel ASF that makes it more convenient to port between different Atmel device platforms. From bottom to top, the structure includes: • Drivers Drivers are used to transmit DALI bits between top layer and afford timer. Timer0 is defined for DALI encode/decode and DALI frame timing. PCINT is used for DALI decode. Timer1 is used for fade timing and other system timing. • Service Service processes frame sequence timing and implement DALI commands. To make migration feasible between different MCU device series, the service should be hardware irrelevant. • Application Application realizes the DALI LED slave function. Besides DALI service, it also process power on, interface state detection, etc. Figure 3-1. DALI firmware structure. 3.1 Drivers The driver layer aims at timer and encode/decode of DALI bits. For Atmel Atmega88PA in this solution, PCINT and timer0/1 are chosen as the driver components. It is free to use other components if different method is used. PCINT detects the logic change in the input signal pin after initialization. The signal change triggers the PCINT interrupt and makes ISR handle the decode process. Timer0 is configured as the time base for DALI encode/decode. It needs to count fast enough to measure the 1200 bit/s bit rate in DALI data stream. Timer0 needs to work together with PCINT to decode DALI signal. Timer1 is mainly used as time base for LED power output dimming. 3.2 Service 3.2.1 Frame State Convert Firstly DALI slave goes into idle state and does state initialization. After this, the slave shall go to forward frame state immediately and wait for receiving DALI frame. If received, DALI frame should be processed. According to different conditions, it goes to next repeat state, backward state or idle state. There is time delay between state conversions according to the IEC62386-102 standard specification. Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 6 In DALI standard definition, every configuration command shall be received twice within 100ms to reduce the probability of incorrect reception. If received correctly twice, the configuration command should be executed and then go to idle state. Otherwise it goes to idle state. A backward frame shall only be sent after the reception of a query command or a write memory command. If backward frame is NO, the DALI slave should not react. Otherwise it should send the DALI answer signal to master. After this is done, it should go to idle mode with a delay. Figure 3-2. Frame State Convert diagram. next repeat frame need repeat new frame Idle 3.2.2 Forward frame need answer Backward frame Frame message process After DALI slave receives forward frame, it should decode the two bytes of address and data. The address type includes short address, group address and broadcast. Address and data compose the command code. For every command code, the corresponding defined function should be executed. In this module, the slave shall decode the address and call the command function. Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 7 Figure 3-3. Message process flowchart. Y Short address? N Y Group address? N Y Broadcast? N N N Special commands? Y Is me? Y N Y Commands 256-275? Y Process commands 256275 Direct ARC? Process direct power level N Process normal commands 0-255 Return 3.3 Application The whole system process is an infinite loop. After start up, it initializes the parameters and configures microcontroller blocks. In the first power on period, if no command affecting power level is received, it shall go to power on level immediately without fading. The system also processes frame, interface state detection sequentially. Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 8 Figure 3-4. Message process flowchart. Start up Initialize param eters, drivers N Power on state? Y Process power on Process fram e Process interface state 3.4 Logarithm dimming table Here describes the dimming lookup table used in firmware. 3.4.1 high_PWM_val and low_PWM_val DALI standards define the logarithmic dimming curve between power value and power level. The formula below defines their relation. where X denotes power value and n denotes power level. Power level ranges from 1 to 254, while corresponding power value ranges from 0.1% to 100%. Figure 3-5 shows their relationship. Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 9 Figure 3-5. Logarithm dimming curve. In this example, 12-bit PWM of the MCU is used to output power. PWM provides a duty ratio from 0 to 4095 which corresponds to a precision of min. 0.1% power value. The PWM duty ratio lookup table is stored in arrays high_PWM_val and low_PWM_val. The tables are stored in EEPROM to save FLASH space. Timer1 is used as the time base for 12-bit PWM. The initialization is done in function dali_tc_init(). /*Timer1 clock with 8 prescaling */ tc_write_clock_source(TC1, TC_CLKSEL_DIV8_gc); /* OC1B in Compare Match, PB2 output, non-inverting, Fast PWM, * ICR1 is top*/ TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (1 << COM1B1) | (0 << COM1B0) | (1 << WGM11) | (0 << WGM10); TCCR1B |= (1 << WGM13) | (1 << WGM12); /* Used as top value for PWM, 0x0fff makes this a 12-bit PWM */ ICR1 = TC1_TOP; /* Clear counter */ TCNT1 = 0; 3.4.2 flash_fade_rate_val and flash_inv_fadetime_val DALI standard defines FADE TIME and FADE RATE as light dim parameters. FADE TIME is the time taken for the light to change from the current dim level to the target dim level. FADE RATE is the speed of the light change. There are sixteen levels in the formula that: • • FADE TIME = 2(X/2-1) FADE RATE = 253 / (FADE TIME) Their value relationship is given in Table 3-1. Table 3-1. FADE TIME and FADE RATE. X FADE TIME [s] FADE RATE [steps/s] 0 1 2 3 No fade 0.7 1.0 1.4 Not applicable 358 253 179 Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 10 4 5 6 7 8 9 10 11 12 13 14 15 2.0 2.8 4.0 5.7 8.0 11.3 16.0 22.6 32.0 45.3 64.0 90.5 127 89.4 63.3 44.7 31.6 22.4 15.8 11.2 7.9 5.6 4.0 2.8 In this design, Timer1 is used as timer of FADE TIME and FADE RATE, and its overflow interrupt is used as time base for light dimming. 32-bit variable dali.long_level is used to store power level whose lower 24 bits is the fractional value and upper 8 bits is the actual DALI level. union daliunion { //dali.long_level is used during fade - allows smoother fading between fixed DALI levels. uint32_t long_level; struct { // Lower three bytes store the fractional value of DALI level uint8_t byte0; uint8_t byte1; uint8_t byte2; // Upper 8 bits dali.level is the actual DALI level. uint8_t level; }; }dali; For DALI commands UP and DOWN, FADE RATE is used for light dimming. In every timer period, the change of DALI level is FADE RATE divided by Timer1 overflow frequency. Thus a 25-bits result is got with lower 24 bits used as fractional. To save space while keep precision, the result is right-shifted by 9 bits and the remainder 16-bits value is stored in lookup table flash_fade_rate_val. And accordingly in application, the value from lookup table should be leftshifted by 9 bits before being used. Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 11 For DALI direct power control command, FADE TIME is used for light dimming. In every timer period, the change of DALI level is (1/FADE TIME) divided by Timer1 overflow frequency. Thus a 17-bits result is got. Also to save space, the result is right-shifted by 1 bit and the remainder 16-bits value is stored in lookup table flash_inv_fadetime_val. And accordingly in application, the value from lookup table should be left-shifted by 1 bit before being used. To dim N levels in power control command, the above result needs to be multiplied by N. Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 12 Appendix A. Command Sets Command number Command name – 0 1 2 3 4 5 6 7 8 9 10 – 15 16 – 31 32 33 34 – 41 42 43 44 45 46 47 48 – 63 64 – 79 80 – 95 96 – 111 112 – 127 128 129 130 – 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 DIRECT ARC POWER CONTROL OFF UP DOWN STEP UP STEP DOWN RECALL MAX LEVEL RECALL MIN LEVEL STEP DOWN AND OFF ON AND STEP UP ENABLE DAPC SEQUENCE Reserved GO TO SCENE RESET STORE ACTUAL LEVEL IN THE DTR Reserved STORE THE DTR AS MAX LEVEL STORE THE DTR AS MIN LEVEL STORE THE DTR AS SYSTEM FAILURE LEVEL STORE THE DTR AS POWER ON LEVEL STORE THE DTR AS FADE TIME STORE THE DTR AS FADE RATE Reserved STORE THE DTR AS SCENE REMOVE FROM SCENE ADD TO GROUP REMOVE FROM GROUP STORE DTR AS SHORT ADDRESS ENABLE WRITE MEMORY Reserved QUERY STATUS QUERY CONTROL GEAR QUERY LAMP FAILURE QUERY LAMP POWER ON QUERY LIMIT ERROR QUERY RESET STATE QUERY MISSING SHORT ADDRESS QUERY VERSION NUMBER QUERY CONTENT DTR QUERY DEVICE TYPE QUERY PHYSICAL MINIMUM LEVEL QUERY POWER FAILURE QUERY CONTENT DTR1 QUERY CONTENT DTR2 Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 13 158 – 159 160 161 162 163 164 165 166 – 175 176 – 191 192 193 194 195 196 197 198 – 223 224 – 254 255 256 257 258 259 260 261 262 – 263 264 265 266 267 268 269 270 271 272 273 274 275 276 – 349 Reserved QUERY ACTUAL LEVEL QUERY MAX LEVEL QUERY MIN LEVEL QUERY POWER ON LEVEL QUERY SYSTEM FAILURE LEVEL QUERY FADE TIME/FADE RATE Reserved QUERY SCENE LEVEL (SCENES 0-15) QUERY GROUPS 0-7 QUERY GROUPS 8-15 QUERY RANDOM ADDRESS (H) QUERY RANDOM ADDRESS (M) QUERY RANDOM ADDRESS (L) READ MEMORY LOCATION Reserved See parts 207 of this standard QUERY EXTENDED VERSION NUMBER TERMINATE DATA TRANSFER REGISTER (DTR) INITIALISE RANDOMISE COMPARE WITHDRAW Reserved SEARCHADDRH SEARCHADDRM SEARCHADDRL PROGRAM SHORT ADDRESS VERIFY SHORT ADDRESS QUERY SHORT ADDRESS PHYSICAL SELECTION Reserved ENABLE DEVICE TYPE X DATA TRANSFER REGISTER 1 (DTR1) DATA TRANSFER REGISTER 2 (DTR2) WRITE MEMORY LOCATION Reserved Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 14 Schematic 1 Q101A 6 R101 GND_DALI 5 Appendix B. 150k 4 3 BC857BS Q101B BC857BS 1 1 BAT54XV2T1G R103 1.0M C101 4.7uF U101 2 R105 1.0M GND_DALI D102 MMSZ4682-V 4 VCC_5V 3 SFH6156-3 1 7.5k D101 2 2 R102 DALI_Input R104 8.2k 2 C102 1n GND GND_DALI GND_DALI GND_DALI U102 R106 1.0M 1 2 GND_DALI 3 R107 240k C103 OUT V+ 5 VIN+ IN- 4 TLV3491 TLV3491AIDBVR 100p 4 Q102 BC847B J101 D103 GND_DALI TP101 GND_DALI + R108 1.5k DALI_VP 1 ~ 2 - GND_DALI 3 2 DALI connector GND_DALI ~ 1 VCC_5V R109 1R TP102 DALI_GND R110 360R Q103 BC817-25 120R GND_DALI U103 4 R111 3 1 2 R112 100R SFH6156-3 GND_DALI DALI_Output GND_DALI Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 15 C301 U301 VIN 3 EN OUT BYPASS TP301 J301 VCC_5V GND 4.7uF C304 10n TP302 C303 33n C302 4 GND GND GND GND GND GND GND_TEST J313 GND GND TP303 5V 5 2 4.7uF LP2985-50DBVR 1 2 1 GND VIN_12V VIN_12V INPUT VCC_5V DALI_Input VCC_5V TP304 TP305 OUTPUT R301 10k SCL Driver_SCL R303 100k DALI_Output R302 10k TP306 Driver_SCL SDA Mega_RST Driver_SDA Driver_SDA 180k NCP18WF104J03RB GND C305 33n VCC_5V GND C307 33n VCC_5V 1 2 3 4 5 6 7 8 PD3(OC2B/INT1/...) PD4(XCK/T0/...) GND VCC GND VCC PB6(XTAL1/...) PB7(XTAL2/...) VCC_5V D301 620R 3 R306 R 1 G 2 LED2 SML-020MLT R307 620R ATmega88PA-AU TP307 V_LED LED_voltage (.../ADC1)PC1 (.../ADC0)PC0 ADC7 GND AREF ADC6 AVCC (.../SCK)PB5 TP308 24 23 22 21 20 19 18 17 V_S0 C309 100nF R308 Driver_S0 100k GND Mega_AREF VCC_5V J310 C308 33n GND TP309 2 4 6 HEADER 2x3 VCC_5V Driver_S0 C306 33n PWM Driver_PWM 1 3 5 LED_voltage GND 9 10 11 12 13 14 15 16 4 (.../INT0)PD2 (.../TXD)PD1 (.../RXD)PD0 (.../RESET)PC6 (.../ADC5/SCL)PC5 (.../ADC4/SDA)PC4 (.../ADC3)PC3 (.../ADC2)PC2 U302 R305 2 PD5(OC0B/T1/...) PD6(OC0A/AIN0/...) PD7(AIN1/...) PB0(CLKO/ICP1/...) PB1(OC1A/...) PB2(SS/OC1B/...) PB3(OC2A/MOSI/...) PB4(MISO/...) R304 1 GND_TEST 32 31 30 29 28 27 26 25 The thermistor need placed close to the Power LED Mega_MOSI TP310 Driver_PWM FLTB Driver_FLTB Driver_FLTB GND Mega_SCK Mega_MISO TP311 EN Driver_EN Driver_EN Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 16 2 GND 5 C202 TDC-021H-1 4.7uF R201 18k 4 GND R202 10k GND EN SW FB C201 6 JS201 D201 MSS2P3 100nF L201 LQH44PN2R2MP0 C203 10u/25V 3 R203 R204 2.7k 150R 2 D203 Driver_PWM Driver_FLTB Driver_SCL Driver_SDA GND_DRIVER FBO1 EN PWM3 PWM2 PWM1 PWM0 FLTB SCL SDA D0 G0 S0 D1 G1 S1 NC FBI1 FBO2 FBI2 GND VIN VCC CVDD VDD NC D3 G3 S3 S2 G2 D2 NC R207 10k 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 Driver_S0 LED_voltage 2 1 D205 LED_12 Short test C204 1u GND_DRIVER Driver_VIN Driver_VCC GND_TEST C205 2.2u Q201 Si4168DY Si4168DY GND_DRIVER Driver_VDD 4 C206 2.2u GND_DRIVER R209 360mR GND_DRIVER Driver_S0 LED_voltage D204 LED_12 J203 8 7 6 5 GND_DRIVER Driver_PWM Driver_FLTB Driver_SCL Driver_SDA 330R R205 120k GND GND_DRIVER 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Current Test 3 2 1 Driver_EN R206 MSL2041GU R208 100k Driver_EN 1 FDLL4148 VIN_12V U202 J202 GND GND GND VIN_12V 1 2 2 MSS2P3 U201 MP2451DT 1 VIN BST 12 1 1 D202 1 2 GND 3 2 1 2 J201 Driver_D0 Driver_G0 R210 360mR GND_DRIVER Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 17 Appendix C. Revision History Doc. Rev. Date Comments 42071B 07/2013 Add hardware information in “Description” chapter 42071A 02/2013 Initial document release Atmel AT01244: DALI Slave Reference Design [APPLICATION NOTE] 42071B−AVR−07/2013 18 Atmel Corporation Atmel Asia Limited Atmel Munich GmbH Atmel Japan G.K. 1600 Technology Drive Unit 01-5 & 16, 19F Business Campus 16F Shin-Osaki Kangyo Building San Jose, CA 95110 BEA Tower, Millennium City 5 Parkring 4 1-6-4 Osaki USA 418 Kwun Tong Road D-85748 Garching b. Munich Shinagawa-ku, Tokyo 141-0032 Tel: (+1)(408) 441-0311 Kwun Tong, Kowloon GERMANY JAPAN Fax: (+1)(408) 487-2600 HONG KONG Tel: (+49) 89-31970-0 Tel: (+81)(3) 6417-0300 www.atmel.com Tel: (+852) 2245-6100 Fax: (+49) 89-3194621 Fax: (+81)(3) 6417-0370 Fax: (+852) 2722-1369 © 2013 Atmel Corporation. All rights reserved. / Rev.: 42071B−AVR−07/2013 Atmel®, Atmel logo and combinations thereof, AVR®, Enabling Unlimited Possibilities®, megaAVR®, and others are registered trademarks or trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others. Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN THE ATMEL TERMS AND CONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS AND PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and products descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life.