📄 ta_uart.asm
字号:
;*******************************************************************************
; Code for application report - "Timer_A UART Library"
;*******************************************************************************
; THIS PROGRAM IS PROVIDED "AS IS". TI MAKES NO WARRANTIES OR
; REPRESENTATIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
; INCLUDING ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
; FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR
; COMPLETENESS OF RESPONSES, RESULTS AND LACK OF NEGLIGENCE.
; TI DISCLAIMS ANY WARRANTY OF TITLE, QUIET ENJOYMENT, QUIET
; POSSESSION, AND NON-INFRINGEMENT OF ANY THIRD PARTY
; INTELLECTUAL PROPERTY RIGHTS WITH REGARD TO THE PROGRAM OR
; YOUR USE OF THE PROGRAM.
;
; IN NO EVENT SHALL TI BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
; CONSEQUENTIAL OR INDIRECT DAMAGES, HOWEVER CAUSED, ON ANY
; THEORY OF LIABILITY AND WHETHER OR NOT TI HAS BEEN ADVISED
; OF THE POSSIBILITY OF SUCH DAMAGES, ARISING IN ANY WAY OUT
; OF THIS AGREEMENT, THE PROGRAM, OR YOUR USE OF THE PROGRAM.
; EXCLUDED DAMAGES INCLUDE, BUT ARE NOT LIMITED TO, COST OF
; REMOVAL OR REINSTALLATION, COMPUTER TIME, LABOR COSTS, LOSS
; OF GOODWILL, LOSS OF PROFITS, LOSS OF SAVINGS, OR LOSS OF
; USE OR INTERRUPTION OF BUSINESS. IN NO EVENT WILL TI'S
; AGGREGATE LIABILITY UNDER THIS AGREEMENT OR ARISING OUT OF
; YOUR USE OF THE PROGRAM EXCEED FIVE HUNDRED DOLLARS
; (U.S.$500).
;
; Unless otherwise stated, the Program written and copyrighted
; by Texas Instruments is distributed as "freeware". You may,
; only under TI's copyright in the Program, use and modify the
; Program without any charge or restriction. You may
; distribute to third parties, provided that you transfer a
; copy of this license to the third party and the third party
; agrees to these terms by its first use of the Program. You
; must reproduce the copyright notice and any other legend of
; ownership on each copy or partial copy, of the Program.
;
; You acknowledge and agree that the Program contains
; copyrighted material, trade secrets and other TI proprietary
; information and is protected by copyright laws,
; international copyright treaties, and trade secret laws, as
; well as other intellectual property laws. To protect TI's
; rights in the Program, you agree not to decompile, reverse
; engineer, disassemble or otherwise translate any object code
; versions of the Program to a human-readable form. You agree
; that in no event will you alter, remove or destroy any
; copyright notice included in the Program. TI reserves all
; rights not specifically granted under this license. Except
; as specifically provided herein, nothing in this agreement
; shall be construed as conferring by implication, estoppel,
; or otherwise, upon you, any license or other right under any
; TI patents, copyrights or trade secrets.
;
; You may not use the Program in non-TI devices.
;
;*******************************************************************************
; TA UART Library
;
; Description; Allows Timer_A to communicate through UART
;
; Typical Cycle Count per function:
; TI_initTimer 59
; TI_setCallbackFunction 13
; TI_TX_Byte 64
;
; Memory Usage: <296 bytes
;
; L. Westlund
; Version 1.1
; Texas Instruments, Inc
; August 2007
; Built with Code Composer Essentials 2.0
;*******************************************************************************
;Change log
;
;1.0 - Inital version - L.Westlund
;1.1 - Misc. updates - A.Dannenberg
; * Code now preserves registers used in callback function
; * Fixed issue with possible SR corruption
; * Removed TACCR0_UNUSED feature
;*******************************************************************************
;*******************************************************************************
; Timer_A UART Library - does not assume 32Khz xtal
;
;
; MSP430
; -----------------
; /|\| XIN|-
; | | | 32kHz (optional)
; --|RST XOUT|-
; | |
; | P1.2/TA1|<----------
; | |
; | P1.3/TA2|---------->
;
;*******************************************************************************
.cdecls C,LIST, "msp430x11x1.h"
; The USE_C_CONTEXT_SAVE macro is used when C functions are called from the
; library. The macro preserves registers R11, R13 - R15 which may be destroyed
; during a C function call. If the calling function is in assembler the
; macro need not be used.
USE_C_CONTEXT_SAVE .set 1
RXD .set 004h ; RXD on P1.2
TXD .set 008h ; TXD on P1.3
TACLK_MCLK_SYNC .set 1
TI_TA_TX_READY .set 0x01
TI_TA_RX_RECEIVED .set 0x02
;Variables
.def TI_RXData
.def TI_FuncPtr
.def TI_TA_UART_StatusFlags
;Functions
.def TI_initTimer
.def TI_setCallbackFunction
.def TI_TX_Byte
.bss TI_FuncPtr, 2
.bss TI_Bitime, 2
.bss TI_Bitime_5, 2
.bss TI_TXData, 2
.bss OLD_LPM, 1
.bss TI_RXData, 1
.bss TI_TXBitCnt, 1
.bss TI_RXBitCnt, 1
.bss TI_TA_UART_StatusFlags, 1
.text
;============================================================================
; TI_initTimer (func ptr, bitime, bitime_2, clock_source)
;============================================================================
TI_initTimer mov.w #MC_2,&TACTL ; Continuous mode
mov.w #OUT,&TACCTL2 ; TXD Idle as Mark
SetupP1_2 bis.b #TXD+RXD,&P1SEL ; P1.2/3 Timer func
bis.b #TXD,&P1DIR ; TXD output on P1
bic.b #RXD,&P1DIR ; RXD input on P1
mov.w r13, &TI_Bitime ; Bitime, 2nd param
clrc
rrc.w r13 ; get half bit time
mov.w r13, &TI_Bitime_5 ; set Bitime/2
bis.w r14, &TACTL ; Select Clock Source, 3th param
mov.b #TI_TA_TX_READY, &TI_TA_UART_StatusFlags
;-------------------------------FALL-THROUGH----------------------------------
TI_setCallbackFunction; Subroutine that sets function to be called after RX
;-----------------------------------------------------------------------------
mov.w r12, &TI_FuncPtr ; move function ptr, 1st param
;-------------------------------FALL-THROUGH----------------------------------
RX_Ready ; Subroutine that Readies UART to RX Byte into RXData Buffer
;-----------------------------------------------------------------------------
mov.b #8,&TI_RXBitCnt ; Load Bit Counter, 8 data bits
mov.w #CM1+CAP+CCIE,&TACCTL1 ; Sync, Neg Edge, Capture, Int
ret ; Return from subroutine
;-----------------------------------------------------------------------------
TI_TX_Byte ; Subroutine that Transmit One Byte from TXData Buffer
;-----------------------------------------------------------------------------
bit.b #TI_TA_TX_READY, &TI_TA_UART_StatusFlags
jz TX_Return
bic.b #TI_TA_TX_READY, &TI_TA_UART_StatusFlags
.if TACLK_MCLK_SYNC
mov.w &TAR, &TACCR2
.else
mov.w #CM_1+CCIS_2+CAP+SCS+OUT, &TACCTL2;
bic.w #CCIFG, &TACCTL2
bis.w #CCIS0, &TACCTL2
Test_Cap bit.w #CCIFG, &TACCTL2
jz Test_Cap
.endif
add.w &TI_Bitime,&TACCR2 ; Some time till first bit
mov.w r12, &TI_TXData ; put character into TXData
bis.w #0100h, &TI_TXData ; Add mark stop bit to TX Data
rla.w &TI_TXData ; Add space start bit
mov.b #10,&TI_TXBitCnt ; Load Bit Counter, 8data + ST/SP
mov.w #OUTMOD0+CCIE,&TACCTL2 ; TXD = mark = idle
TX_Return ret ;
;------------------------------------------------------------------------------
TA1_ISR ; RXTXData Buffer holds UART Data for RX, R12 for TX
;------------------------------------------------------------------------------
add.w &TAIV, PC
nop ; only here as a filler
jmp RX_byte ; TAIV case 2: RX byte
jmp TX_byte ; TAIV case 4: TX byte
nop ; filler
reti ; TAIV case 10: overflow
TX_byte add.w &TI_Bitime, &TACCR2
cmp.b #00h,&TI_TXBitCnt ;
jne TX_Next ; Next bit?
bic.w #CCIE,&TACCTL2 ; All Bits TX or RX, Disable Int.
bis.b #TI_TA_TX_READY, &TI_TA_UART_StatusFlags; All bits TX'ed set flag
reti ;
TX_Next bic.w #OUTMOD2,&TACCTL2 ; TX Mark
rra.w &TI_TXData ; LSB is shifted to carry
jc TX_Test ; Jump --> bit = 1
TX_Spac bis.w #OUTMOD2,&TACCTL2 ; TX Space
TX_Test dec.b &TI_TXBitCnt ; All bits sent (or received)?
reti ;
RX_byte
add.w &TI_Bitime, &TACCR1
bit.w #CAP,&TACCTL1 ; Capture mode = start bit edge
jz RX_Bit
bit.b #TI_TA_RX_RECEIVED, &TI_TA_UART_StatusFlags
jnz RX_Next
bic.w #CAP, &TACCTL1
add.w &TI_Bitime_5, &TACCR1
mov.b @SP, &OLD_LPM ; make copy of LPMx bits
bic.w #SCG0+SCG1, 0(SP) ; ensure device is atleast LPM0
reti ;
RX_Bit bit.w #SCCI,&TACCTL1 ; Get bit waiting in receive latch
rrc.b &TI_RXData ; Store received bit
RX_Test dec.b &TI_RXBitCnt ; All bits sent (or received)?
jnz RX_Next ; Next bit?
; Decode of Received Byte Here
RX_Comp bis.b #TI_TA_RX_RECEIVED, &TI_TA_UART_StatusFlags
call #RX_Ready
push.w R12
.if USE_C_CONTEXT_SAVE ; Full context save?
push.w R11 ;
push.w R13 ; (needed as a C-callback function
push.w R14 ; can be used. May be optimized
push.w R15 ; in case of an ASM callback fn.)
mov.b &OLD_LPM, R12 ; Retrieve original LPMx...
and.b #SCG0+SCG1,R12 ; ...isolate modified bits...
bis.w R12,10(SP) ; and return original LPMx to stack
.else
mov.b &OLD_LPM,R12 ; Retrieve original LPMx...
and.b #SCG0+SCG1,R12 ; ...isolate modified bits...
bis.w R12,2(SP) ; and return original LPMx to stack
.endif
mov.b &TI_RXData, R12 ; Pass value to callback func
call &TI_FuncPtr
tst.w R12 ; Exit LPMx?
jz RX_Comp2
.if USE_C_CONTEXT_SAVE
bic.w #LPM4,10(SP) ; Exit LPMx
RX_Comp2 pop.w R15 ; Full context restore
pop.w R14
pop.w R13
pop.w R11
.else
bic.w #LPM4, 2(SP) ; Exit LPMx
RX_Comp2
.endif
pop.w R12
RX_Next reti
.sect ".int08" ; Timer_A1 Vector
.short TA1_ISR ;
.end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -