Using MC683xx M-Bus Software To Communicate Between Processor Systems
Using MC683xx M-Bus Software To Communicate Between Processor Systems
Using MC683xx M-Bus Software To Communicate Between Processor Systems
AN496/D
Introduction
M-bus is an I2C-compatible bus interface used in the 683xx family. It is a serial interface comprising two open-drain, bidirectional signals, namely serial clock (SCL) and serial data (SDA). Multiple devices can be connected directly to these open-drain lines, and indeed this is good reason for the widespread adoption of the bus as an efcient IC communication method in end-systems. A typical scenario would consist of a processor with an M-bus master controlling the data ow between several slaves, such as LCDs, real-time clocks, keypads, A/D converters and memories. Moreover, a built-in bus collision mechanism supports multiple M-bus masters as well as multiple slaves. The M-bus module of the 683xx is exible enough to operate as either an M-bus master or a slave. This application note demonstrates control software for M-bus communication between two identical MC68307 systems, one congured with an M-bus master and the other an M-bus slave. Only a short piece of initialisation code needs to be changed to make the MC68307 code applicable to other 683xx devices with M-bus.
If a system has two or more M-bus masters which poll the bus free and start a transfer at the same time, then the collision detection arbitration, throughout the transfer of the slave address transfer and subsequent data bytes, decides which device gets charge of the bus. If the 683xx M-bus loses arbitration in this way, it stops driving data onto the bus, to prevent data corruption. Furthermore, it switches automatically into slave mode pre-empting the alternate master addressing it as a slave. If interrupts are enabled, an interrupt is generated on the completion of that byte, and a status bit indicates arbitration lost as the interrupt source. The rst data byte transmitted by the M-bus master is always the targeted slave address, with the least signicant bit determining whether the slave remains ready to receive or transmit subsequent bytes. The addressed slave can then acknowledge the received byte, or not, depending upon the software protocol and acknowledge capability of the slave devices used. Each acknowledge is like a 9th data bit, asserted by the receiver as a handshake to successfully transmitted data.
Freescale Semiconductor, Inc., 2004. All rights reserved.
9/95 1995. All trademarks are recognised.
A block transfer comprising a series of data bytes (and acknowledges, if used) follows as commanded by the software protocol. The bus remains busy throughout the block, precluding all other masters from starting transfers. At the end of the block, the bus master relinquishes the bus by software placing a STOP condition onto the bus. Ultimately, the M-bus master is responsible for starting and stopping transfers, but the number of bytes transferred can be dictated by either the master or slave depending upon the desired software protocol. For example, a slave may acknowledge all bytes received until it saturates, at which point the master STOPs the block transfer. Alternatively, the slave receiver may acknowledge received bytes until the master transmitter dictates there are no more bytes to send. Indeed, both master and slave can be charged with controlling the transfer block. For instance, the software protocol may transfer a byte count as part of the communication, or use a xed number of transfer bytes every time. For the best choice in software control, transfers can adopt either a status polling method, or interrupts at the end of each byte. The interrupt option is most commonly used to minimise the time the processor is tied up with the transfers (overhead). If enabled, the interrupts are generated on the completion of each 9 bits (8 data bits plus an acknowledge).
initiated by a dummy read of the data register. There is no sensible data read at this point, but having started the receive process, data register reads in subsequent end-of-byte interrupts read valid data and initiate the next byte receive. Again, the software protocol determines the use of acknowledges. For a fuller description of the M-bus software and hardware features, see the MC68307 users manual.
Description of Set-up
The hardware consists of two identical MC68307 systems connected together via the M-bus as shown in Figure 1. Both are master-mode processor systems, each with the MC68307 processor core executing instructions prefetched from ROM. This is not to be confused with the master and slave operation of the M-bus modules within each processor.
68307 System
5V 2. 2k 2. 2k SC L SD A
683 07 Syst em
Mast er MBus
SCL SDA
Sl a ve MBus
Figure 1 Hardware Setup Each MC68307 system has 128kB EPROM and 128kB SRAM and runs a debug monitor. A complete description of the system hardware is provided in AN490/D, Multiple Bus Interfaces using the MC68307. Using the monitor's download facility, an M-bus control program is downloaded into the SRAM of each board. The code allows one system to control its M-bus module as a master, while the other implements an M-bus slave. Together, the two software programs allow the MC68307 M-bus master to write data to the M-bus slave and later read it back for verication.
Software Flow
The MC68307 master M-bus controls the number of blocks transferred via START and STOP conditions. In this example, there are only two communication blocks, one transmitting data to the slave (master transmit block), and one receiving data back from the slave for verication (master receive block). The master/slave responsibilities during the master transmit block are outlined in Figure 2a and for the master receive block in Figure 3a. On these diagrams, note that for a given transfer byte, the end-of-byte interrupts on the master and slave occur at around the same time. The built-in M-bus transfer mechanism means it does not matter in which order they are serviced. The master and slave interrupt service order used in the owcharts of Figures 2a and 3a is purely for demonstration purposes. The interrupt handlers are shown such that the data ow is always from transmitter to receiver. It should be understood that the master and slave interrupt handlers are happening at the same time, as are the transmit and receive of a particular byte.
Transfer Blocks
The master M-bus controls the number of data bytes within each transmit/receive block. Observe Figures 2b and 3b, which give a summary of the activity on the M-bus during the master transmit and master receive blocks respectively. When the master is transmitting data (master transmit block), the slave acknowledges all bytes received, and the master decides when the transfer is completed by setting a STOP condition; see Figure 2b. When the master is
AN496/D
receiving data (master receive block), it decides when the transfer is complete by stopping acknowledges on the last received byte, (thereby stopping the slave transmitting) and setting a STOP condition; see Figure 3b.
Software Implementation
The software used is shown in Software listing 1 and 2. Only the method of enabling the M-bus and interrupts at the start of the software listings is specic to the MC68307. Thereafter, the code is generic for any 683xx device with an M-bus module. The 683xx M-bus slave software should always be set running before the master software, such that the prospective slave is initialised as a receiver before the master transmits the slave address. The software uses interrupts to control the byte transfers within each block. The M-bus master starts the transfer by transmitting the slave address. Thereafter interrupts are generated on both the master and slave M-bus to control the test. The M-bus hardware protocol does not care which order the interrupts are serviced by the master (transmitter or receiver) or slave (transmitter or receiver) at the end of each byte. Consider that the master is in charge of generating the SCL clocks to shift data out the transmitter and into the receiver, when a transmit/receive is initiated by writing/ reading the M-bus data register respectively. However, the clocks do not start until the slave has released the clock line on the bus by making its corresponding read/write of its M-bus data register. Therefore, both 683xx M-bus master and slave interrupts have to initiate the next data transfer. The slave frequency can be programmed as greater or less than that of the master. M-bus implements a clock synchronisation mechanism such that the clock with the shortest high time and longest low time dictates the opendrain clock. For example, if the programmed slave M-bus clock frequency is less than the master, the slave can stretch the clock as necessary. The number of transfer and receive blocks, and the number of data bytes within each block can be altered in the master software.The slave software remains the same throughout. If the user desires detailed crosschecks on the software ow, interrupt counts (for number of bytes transferred) or a ag passing mechanisms could be implemented. For simplicity this is not used in the example software.
-Rx slave address -Auto-Acknowledge address -Interrupt on Slave address match -Set Tx/Rx mode to Rx -Dummy read of MBDR, ready Rx 1st data byte
-Rx data -Auto-Acknowledge data -Interrupt at end of 1st data byte Rx -Read 1st byte of valid data from MBDR (AA), and ready for next Rx
-Interrupt at end of address Tx -Verify Acknowledge -Remain in Tx mode -Write 1st data byte (AA) to MBDR to initiate Tx -Tx data
-Interrupt at end of 1st data byte Tx -Verify Acknowledge -Write 2nd data byte (55) to MBDR to initiate Tx -Tx data
-Rx data -Auto-Acknowledge data -Interrupt at end of 2nd data byte Rx -Read 2nd byte of valid data from MBDR (55), and ready for next Rx
-Interrupt at end of 2nd data byte Tx -Verify Acknowledge - STOP block transfer
Master Activity
Start Block
Stop Block
M-bus
Start
Ack Ack
Ack Ack
Stop
Slave Activity
AN496/D
-Rx s lav e addr es s -Aut o- Ack nowledge addr ess -In ter rupt on Sl ave addr ess m atc h -S et T x/ Rx m ode t o T x -W rite 1st dat a by te (AA) to MBD R re ady to T x
-I nt er rupt at end of addr es s T x - Ver ify Ack nowledge - Set Tx /R x mode to Rx - Du mmy r e ad of M BDR t o i ni tiate R x of 1st dat a by te (AA)
-Tx d ata
-Rx dat a -A uto- Ac know ledge dat a -I nt er rupt at end of 1s t data byt e R x - Re ad of M BDR r eady t o R x 2nd dat a by te (55)
-In ter rupt at end of 1s t data byt e Tx -Ver ify Ac know ledge -W rite 2nd dat a by te (AA) to MBD R t o i ni tiate T x -Tx d ata
-Int er rupt at end of 2nd dat a by te Tx - No Ack nowledge, s o end Tx - Sw itc h t o s lave Rx m ode ready f or nex t s lave addr ess
Master Activity
Start Block
Ack
No Ack
Stop Block
M-bus
Start
Ack
No Ack
Stop
Slave Activity
*============================================================================ * Main Program *============================================================================ ORG $10000 RANDOM LOCATION FOR ASSEMBLY AND.L #$FFFFFEFF,SCR Clear SCR bit 8, MBUS CLock Active MOVE.B #$40,PIVR Vector = #$40, Vector @ address $100 OR.W #$000D,PICR MBUS Interrupt level = 5 MOVE.L #MBUSHAN,MBUSVEC Set up MBUS Interrupt Handler OR.W #$0003,PBCNT Enable MBUS Lines BSR INIT_MBM Initialise MBus as master * WRITE TO SLAVE 68307 MBus * Write Chip Address, and Two bytes of data START BSR MBBUSY Poll the MBUS, wait till bus free MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B MOVE.L BSR #0,V_DRXCNT #ATXCNT,V_ATXCNT #DTXCNT,V_DTXCNT #1,V_WRITE #S307_AD,V_CHIPAD #S307_DATA,A0 WRITE1 Data RECEIVE COUNT Address TRANSMIT COUNT Data TRANSMIT COUNT Set Write to slave var = TRUE Slave 68307 Mbus receiver Address Pointer to stored data for transfer Send out the Chip Address
* READ FROM SLAVE 68307 MBus * Write Chip Address, and READ Two bytes of data BSR MOVE.B MOVE.B MOVE.B MOVE.B MBBUSY #DRXCNT,V_DRXCNT #ATXCNT,V_ATXCNT #0,V_DTXCNT #0,V_WRITE Poll the MBUS, wait till bus free Data RECEIVE COUNT Address TRANSMIT COUNT Data TRANSMIT COUNT Set Write to slave var = FALSE
AN496/D
MOVE.B OR.B MOVE.B MOVE.L BSR * Test Complete BSR FOREVER BRA
Alter chip address lsb for slave transmit and write to chip address variable Pointer to data for memory 1 Send out the Chip Address Poll the MBUS, wait till bus free Test complete & passed, loop forever
********************************************************************************* * MBUS SETUP/INITIALISATION ********************************************************************************* INIT_MBM MOVE.W #$2700,SR DISABLE INTERRUPTS BY SETTING TO LEVEL 7 MOVE.B #0,MBSR CLEAR INTERRUPT PEND, ARBITRATION LOST MOVE.B #$0C,MFDR SET FREQUENCY MOVE.B #$00,MBCR DISABLE AND RESET MBUS MOVE.B #$80,MBCR ENABLE MBUS RTS * NOTE - By not writing MADR, the 68307 MBUS slave address = 0
********************************************************************************* * Poll the MBUS BUSY ********************************************************************************* MBBUSY BTST #5,MBSR TEST MBB BIT, BNE MBBUSY AND WAIT UNTIL IT IS CLEAR RTS ********************************************************************************* * GENERATION FIRST BYTE OF DATA TRANSFER ********************************************************************************* WRITE1 BTST #5,MBSR TEST MBB BIT, BNE WRITE1 AND WAIT UNTIL IT IS CLEAR TXSTART BSET #4,MBCR SET TRANSMIT MODE BSET #5,MBCR SET MASTER MODE (GENERATE START) BSET #6,MBCR Enable MBUS Interrupts MOVE.B (V_CHIPAD),MBDR TRANSMIT THE SLAVE CHIP ADDRESS MOVE.W #$2000,SR ENABLE INTERRUPTS BY SETTING TO LEVEL 0 MBFREE BTST BEQ RTS #5,MBSR MBFREE TEST MBB BIT, If bus is still free, wait until busy
********************************************************************************* * POST BYTE TRANSMISSION/RECEPTION SOFTWARE RESPONSE ********************************************************************************* ORG MBUSHAN Start of Interrupt Handler ISR BCLR #1,MBSR CLEAR THE MIF FLAG MOVE.L D0,-(A7) PUSH D0 REGISTER TO STACK MOVE.L D1,-(A7) PUSH D1 REGISTER TO STACK MOVE.L #0,D0 Clear general data reg MOVE.L #0,D1 Clear general data reg BTST BEQ BTST BEQ #5,MBCR SLAVE #4,MBCR MASTRX CHECK THE MSTA FLAG BRANCH IF SLAVE MODE CHECK THE MODE FLAG BRANCH IF RECEIVE MODE
********************************************************************************* * Master TRANSMIT caused Interrupt ********************************************************************************* MASTX BTST #0,MBSR CHECK ACK FROM RECEIVER, BNE ENDMASTX IF NO ACK, END TRANSMISSION TXADDR MOVE.B BEQ SUBQ.B MOVE.B BEQ MOVE.B BEQ SUBQ.B MOVE.B BRA ENDMASTX SETMASTRX BCLR BRA BCLR BCLR BSET V_ATXCNT,D1 TXDATA #1,V_ATXCNT V_WRITE,D1 SETMASTRX V_DTXCNT,D1 ENDMASTX #1,V_DTXCNT (A0)+,MBDR END #5,MBCR END #3,MBCR #4,MBCR #5,MBCR CHECK Address TX COUNT If address already Complete go to data Decrement Address Tx Count Check if writing or reading slave If reading set to Master receive CHECK Data TX COUNT IF NO MORE DATA THEN STOP BIT Reduce Tx Count Transmit next byte EXIT GENERATE STOP CONDITION EXIT Enable TXAK Set master Receive Mode SET MASTER MODE (GENERATE START)
TXDATA
********************************************************************************* * Master RECEIVE ********************************************************************************* MASTRX SUBQ.B #1,V_DRXCNT Decrement receive count MOVE.B V_DRXCNT,D1 CMP.B #DRXCNT-1,D1 First byte read Check BNE NOTFIRST If not first, read and compare as usual MOVE.B MBDR,D0 If first, DUMMY read only to start Rx BRA END NOTFIRST CMP.B BEQ SUBQ.B BNE BSET BRA BCLR MOVE.B CMP.B BEQ BRA MOVE.L MOVE.L RTE NOP BRA SLAVE #0,D1 ENMASR #1,D1 NXMAR #3,MBCR NXMAR #5,MBCR MBDR,D0 (A0)+,D0 END READERR (A7)+,D1 (A7)+,D0 LAST BYTE TO BE READ CHECK LAST SECOND BYTE TO BE READ CHECK NOT LAST ONE OR LAST SECOND,SO BRANCH LAST SECOND, DISABLE ACK TRANSMITTING LAST ONE, GENERATE STOP SIGNAL READ DATA COMPARE WITH WRITTEN DATA If data as expected o.k. Else ERROR loop forever. POP D1 REGISTER FROM STACK POP D0 REGISTER FROM STACK
READERR END
SLAVE
********************************************************************************* * Buffers and Variables ********************************************************************************* V_WRITE DC.B $1 Slave write = True V_CHIPAD DC.B S307_AD Chip Address variable = Slave 307 Add V_DRXCNT DC.B DRXCNT Set up variables - Data Receive Count V_ATXCNT DC.B ATXCNT - Addr Transmit Count V_DTXCNT DC.B DTXCNT - Data Transmit Count S307_DATA DC.B $AA,$55 Chip 1 Data END
AN496/D
*============================================================================ * Main Program *============================================================================ ORG $10000 RANDOM LOCATION FOR ASSEMBLY AND.L #$FFFFFEFF,SCR Clear SCR bit 8, MBUS CLock Active MOVE.B #$40,PIVR Vector = #$40, Vector @ address $100 OR.W #$000D,PICR MBUS Interrupt level = 5 MOVE.L #MBUSHAN,MBUSVEC Set up MBUS Interrupt Handler OR.W #$0003,PBCNT Enable MBUS Lines BSR INIT_MBS Initialise MBus as slave FINISH BRA FINISH Loop forever
********************************************************************************* * MBUS SETUP/INITIALISATION ********************************************************************************* INIT_MBS MOVE.W #$2700,SR DISABLE INTERRUPTS BY SETTING TO LEVEL 7 MOVE.B #0,MBSR CLEAR INTERRUPT PEND, ARBITRATION LOST MOVE.B #$10,MFDR Set FREQUENCY MOVE.B #S307_AD,MADR Set MBUS slave address MOVE.B #$00,MBCR DISABLE AND RESET MBUS OR.B #$C0,MBCR ENABLE MBUS, Ints, TXAK MOVE.W #$2300,SR Enable INTS BY SETTING TO LEVEL 3 RTS ********************************************************************************* * Poll the MBUS BUSY ********************************************************************************* MBBUSY BTST #5,MBSR TEST MBB BIT, BNE MBBUSY AND WAIT UNTIL IT IS CLEAR RTS
10
********************************************************************************* * POST BYTE TRANSMISSION/RECEPTION SOFTWARE RESPONSE ********************************************************************************* ORG MBUSHAN Start of Interrupt Handler ISR BCLR #1,MBSR CLEAR THE MIF FLAG MOVE.L D0,-(A7) PUSH D0 REGISTER TO STACK MOVE.L D1,-(A7) PUSH D1 REGISTER TO STACK MOVE.L #0,D0 Clear general data reg MOVE.L #0,D1 Clear general data reg * Interrupt Counter ADDQ.L #1,D3 (Not used, simply monitor) BTST BEQ MASTER BRA #5,MBCR SLAVE MASTER CHECK THE MSTA FLAG BRANCH IF SLAVE MODE Master not implemented, so error
********************************************************************************* SLAVE MOVE.B MBSR,D6 Read MBSR BTST.B #6,D6 Is it slave address byte? BEQ SLAVE_DATA If not, then data
********************************************************************************* * Addressed as SLAVE ********************************************************************************* SLAVE_ADD BTST #2,D6 Read SRW to verify slave Tx or Rx BEQ INIT_SRX If Rx, initialise SLAVE receive count INIT_STX OR.B MOVE.L MOVE.B BRA AND.B MOVE.L MOVE.B BRA #$10,MBCR #DATABUF,A0 (A0)+,MBDR END_SLAVE #$E7,MBCR #DATABUF,A0 MBDR,D0 END_SLAVE Set transmit mode Pointer to data storage buffer First data byte transmit Set receive mode and TXAK Pointer to data storage buffer Start receive via Dummy byte read
INIT_SRX
********************************************************************************* * Slave Data ********************************************************************************* SLAVE_DATA BTST #4,MBCR Read Tx or Rx mode BEQ SRX_DATA ********************************************************************************* * Post Slave data Transmit Control ********************************************************************************* STX_DATA BTST #0,MBSR CHECK ACK FROM RECEIVER, BEQ NXT_TX IF ACK, THEN TX NEXT DATA BYTE AND.B MOVE.B BRA NXT_TX MOVE.B BRA #$EF,MBCR MBDR,D0 END_SLAVE (A0)+,MBDR END_SLAVE TX complete so swap to Rx Dummy read to free bus (SCL) Finish and await Master Tx next data byte EXIT
********************************************************************************* * Post Slave data Receive Control ********************************************************************************* SRX_DATA MOVE.B MBDR,D0 READ DATA MOVE.B D0,(A0)+ Store data in next data buffer location END_SLAVE MOVE.L MOVE.L RTE (A7)+,D1 (A7)+,D0 POP D1 REGISTER FROM STACK POP D0 REGISTER FROM STACK
********************************************************************************* * Buffers and Variables ********************************************************************************* DATABUF DS.B 0 Slave data buffer between Rx and Tx END
AN496/D
11
Information in this document is provided solely to enable system and software implementers to use Freescale Semiconductor products. There are no express or implied copyright licenses granted hereunder to design or fabricate any integrated circuits or integrated circuits based on the information in this document. Freescale Semiconductor reserves the right to make changes without further notice to any products herein. Freescale Semiconductor makes no warranty, representation or guarantee regarding the suitability of its products for any particular purpose, nor does Freescale Semiconductor assume any liability arising out of the application or use of any product or circuit, and specifically disclaims any and all liability, including without limitation consequential or incidental damages. Typical parameters which may be provided in Freescale Semiconductor data sheets and/or specifications can and do vary in different applications and actual performance may vary over time. All operating parameters, including Typicals must be validated for each customer application by customers technical experts. Freescale Semiconductor does not convey any license under its patent rights nor the rights of others. Freescale Semiconductor products are not designed, intended, or authorized for use as components in systems intended for surgical implant into the body, or other applications intended to support or sustain life, or for any other application in which the failure of the Freescale Semiconductor product could create a situation where personal injury or death may occur. Should Buyer purchase or use Freescale Semiconductor products for any such unintended or unauthorized application, Buyer shall indemnify and hold Freescale Semiconductor and its officers, employees, subsidiaries, affiliates, and distributors harmless against all claims, costs, damages, and expenses, and reasonable attorney fees arising out of, directly or indirectly, any claim of personal injury or death associated with such unintended or unauthorized use, even if such claim alleges that Freescale Semiconductor was negligent regarding the design or manufacture of the part.