;****************************************************************************** ; MSP430G2x21/G2x31 Demo - I2C Slave Receiver, single byte ; ; Description: I2C Slave communicates with I2C Master using ; the USI Master data should increment from 0x00 with each transmitted byte ; which is verified by the slave ; LED off for address or data Ack; LED on for address or data NAck.d by the slave ; ACLK = n/a, MCLK = SMCLK = Calibrated 1MHz ; ; ***THIS IS THE SLAVE CODE*** ; ; Slave Master ; (msp430g2x21_usi_07.asm) ; MSP430G2x21/G2x31 MSP430G2x21/G2x31 ; ; /|\| XIN|/|\| XIN|; | | | | | | ; |RST XOUT| |RST XOUT|; | | | | ; LED LED ; | SDA/P1.7|< -|P1.7/SDA | ; | SCL/P1.6|< -|P1.6/SCL | ; ; Note: internal pull-ups are used in this example for SDA & SCL ; ; D Dang ; Texas Instruments Inc ; October 2010 ; Built with Code Composer Essentials Version: 4.2.0 ;****************************************************************************** I2CState MST_data slav_add equ equ equ R4 R5 R6 cdecls C,LIST, "msp430g2221.h" ; -.text ; Program Start ; -RESET mov.w #0280h,SP ; Initialize stackpointer StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; Stop watchdog timer CheckCal cmp.b #0FFh,&CALBC1_1MHZ ; Calibration constants erased? jeq Trap cmp.b #0FFh,&CALDCO_1MHZ jne Load Trap jmp $ ; Trap CPU!! Load mov.b &CALBC1_1MHZ,&BCSCTL1 ; Set DCO to 1MHz mov.b &CALDCO_1MHZ,&DCOCTL SetupP1 mov.b #0xC0,&P1OUT ; P1.6 &P1.7 pullups bis.b #0xC0,&P1REN ; P1.6 &P1.7 pullups mov.b #0xFF,&P1DIR ; unused pins output direction SetupP2 clr.b &P2OUT ; mov.b #0xFF,&P2DIR SetupUSI mov.b #USIPE6+USIPE7+USISWRST,&USICTL0; Port, I2C slave mov.b #USIIE+USII2C+USISTTIE,&USICTL1 ;Enable I2C mode,interrupts mov.b #USICKPL,&USICKCTL ; Setup clock polarity bis.b #USIIFGCC,&USICNT bic.b #USISWRST,&USICTL0 bic.b #USIIFG,&USICTL1 clr.w I2CState Mainloop clr.w mov.b MST_data #0x90,slav_add bis.w jmp #LPM0+GIE,SR Mainloop ; Enter LPM0, enable interrupts ; USI_ISR ; ; bit.b #USISTTIFG,&USICTL1 ; Start entry? jnc Check_State bis.b #0x01,&P1OUT ; turn on LED, sequence start mov.w #2,I2CState ; First I2C state, Rx address Check_State add.w I2CState,PC ; I2C State Machine jmp STATE0 jmp STATE2 jmp STATE4 jmp STATE6 jmp STATE8 jmp STATE10 STATE0 STATE2 STATE4 Chk_Add Add_NACK STATE6 STATE8 nop bic.b reti ; Idle, should not get here #USIIFG,&USICTL1 ; Rx address ; Bit counter = 8, Rx address mov.b and.b add.b mov.b bic.b mov.w bic.b reti &USICNT,R8 #0xE0,R8 #0x8,R8 R8,&USICNT #USISTTIFG,&USICTL1 #4,I2CState #USIIFG,&USICTL1 bit.b jnc inc.b #0X01,&USISRL Chk_Add slav_add bis.b cmp.b jnz clr.b bic.b mov.w bis.b bic.b reti #USIOE,&USICTL0 slav_add,&USISRL Add_NACK &USISRL #0x01,&P1OUT #8,I2CState #0x01,&USICNT #USIIFG,&USICTL1 ; SDA = output ; address match? mov.b bis.b mov.w bis.b bic.b reti #0xFF,&USISRL #0x1,&P1OUT #6,I2CState #0x01,&USICNT #USIIFG,&USICTL1 ; ; ; ; Send NACK LED on:error go to next state, prep next start Bit counter = 1, Send Nack bic.b mov.b clr.w bic.b reti #USIOE,&USICTL0 #0x90,slav_add I2CState #USIIFG,&USICTL1 ; ; ; ; Prep for Re-start condition SDA = input Reset Slave address Reset state machine ; Clear Start flag ; Go to next state, chk address ; Process address and send (N)Ack ; If read ; Save R/W bit ; LED off ; Go to next state: Rx data ; Bit counter = 1, send Ack bit ; Receive data byte STATE10 Data_NACK STATE10_Exit bic.b bis.b mov.w bic.b reti #USIOE,&USICTL0 #0x8,&USICNT #10,I2CState #USIIFG,&USICTL1 ; SDA =input ; Bit counter = 8, Rx data ; Go to next state,test data,(N)Ack bis.b cmp.b jne clr.b inc.b bic.b jmp #USIOE,&USICTL0 MST_data,&USISRL Data_NACK &USISRL MST_data #0x1,&P1OUT STATE10_Exit mov.b bis.b #0xFF,&USISRL #0x1,&P1OUT ; Send Nack ; LED on : error bis.b mov.w bic.b reti #0x1,&USICNT #6,I2CState #USIIFG,&USICTL1 ; Bit counter = 1, Send Nack ; next state, prep for next start ; Check Data and Tx (N)Ack ; SDA = output ; If data valid ; Send Ack ; Increment master data ; LED off ; ; Interrupt Vectors ; .sect ".reset" ; MSP430 RESET Vector short RESET ; sect ".int04" ; USI Vector short USI_ISR ; end