⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modem.asm

📁 Eversmith_-_AVRSMS for pdu mode
💻 ASM
字号:
;==========================================================
; This is a part of: Eversmith - AVRSMS
; Copyright (2003) Martin Thomas, Kaiserslautern,  Germany. 
; This  Software is  distributed  under  the Aladdin  Free 
; Public License (AFPL) Read the file license.txt included 
; in the distibution-package. See main-file for more infor-
; mation and license.
;==========================================================

; GSM-Modem handling - Modem is connected to UART1

; default timeouts:
;.equ MODDEFTO=5		; default modem timeout in 0.01 sec
.equ MODDEFTO=$FF		;;;; debugging

.CSEG

;----------------------------------------------------------
;;;; SayTimeout - send the Timeout-Message (debugging)
;----------------------------------------------------------
;ModemSayTimeout:
;	push ZL
;	push ZH
;	ldi ZL,LOW(2*TIMEOUTTXT)
;  	ldi ZH,HIGH(2*TIMEOUTTXT)
;	call MenuUartSendFlashSeq
;	pop ZH
;	pop ZL
;	ret
;TIMEOUTTXT:
;.db $0D,$0A,"Modem Timeout! ",$0D,$0A,$00

;----------------------------------------------------------
;;;;; SayOverrun - send the Overrun-Message (debugging)
;----------------------------------------------------------
;ModemSayOverrun:
;	push ZL
;	push ZH
;	ldi ZL,LOW(2*OVERRUNTXT)
;  	ldi ZH,HIGH(2*OVERRUNTXT)
;	call MenuUartSendFlashSeq
;	pop ZH
;	pop ZL
;	ret
;OVERRUNTXT:
;.db $0D,$0A,"UART Overrun!",$0D,$0A,$00


;----------------------------------------------------------
; ModemUartSendByte - Sends a byte to UART (GSM Modem)
;----------------------------------------------------------
; 
; Inputs:
;	r16 - Byte to send
;
; Ouputs:
;	none
ModemUartSendByte:
	rcall UART1SendByte
	ret

;-------------------------------------------------------------
; ModemUartReadByteFIFO - Get a byte from Modem-UART
;-------------------------------------------------------------
; 
; Inputs:
;	r18 timeout value (0=no timeout)
;	Routine loops 10msec*timeout times before returning with error
;	the timeout is a no data availabe timeout if the modem
;	sends garbage all the time this will never timeout
;
; Ouputs:
;  	r17 Byte read
;   carry flag set if timeout occured
;	carry flag cleared on normal read
;
; see UART.asm
ModemUartReadByteFIFO:
	call UART1ReadByteFIFO

	ret

; same with default timeout
ModemUartReadByteDTO:
	push r18
	ldi r18,MODDEFTO	; UART timout-value
	call UART1ReadByteFIFO
	pop r18
	ret



;----------------------------------------------------------
; ModemUartFlush - empty receive "buffer"
;----------------------------------------------------------
; Inputs/Ouputs: none
ModemUartFlush:
	call Uart1Flush			; in UART.asm
	ret

;----------------------------------------------------------
; ModemUartWaitOK - wait for OK from Modem use FIFO
;----------------------------------------------------------
; Inputs: none
;
; Ouputs: 
;	r20=$00 if no OK (timed out)
;	r20=$01 if answer was "OK\r\n"
ModemUartWaitOK:
	push r16
	push r17
	push r18

MWOKF_L1:
	ldi r18,MODDEFTO
	rcall ModemUartReadByteFIFO
	brcs MWOKF_tout
	cpi r17,'O'
	brne MWOKF_L1
;   debugging
;	rcall MenuUartSendByte ; to console UART
	rcall ModemUartReadByteFIFO
	brcs MWOKF_tout
	cpi r17,'K'
	brne MWOKF_L1
;   debugging
;	rcall MenuUartSendByte ; to console UART
	rcall ModemUartReadByteFIFO
	brcs MWOKF_tout
	cpi r17,$0d
	brne MWOKF_L1
;   debugging
;	ldi r17,'/'
;	rcall MenuUartSendByte ; to console UART
	rcall ModemUartReadByteFIFO
	brcs MWOKF_tout
	cpi r17,$0a
	brne MWOKF_L1
;   debugging
;	ldi r17,'|'
;	rcall MenuUartSendByte ; to console UART

	ldi r20,$01		; got all chars of "OK\r\n"
	rjmp MWOKF_END

MWOKF_tout:
	ldi r20,$00
	rjmp MWOKF_END

MWOKF_END:
	rcall ModemUartFlush	; remove everything after "OK"

	pop r18
	pop r17
	pop r16
	ret


;------------------------------------------------------------
; ModemUartWaitChar - wait for a char from Modem 
;------------------------------------------------------------
; (esp. wait for '>' (PDU-Send Prompt) from Modem)
;
; Inputs: none
;	r16: char to wait for
;
; Ouputs: 
;	r20=$00 if char never occured (timed out)
;	r20=$01 if char was read from ModemUart
ModemUartWaitChar:
	push r16
	push r17
	push r18

MUWPP_L1:
	ldi r18,MODDEFTO
	rcall ModemUartReadByteFIFO
	brcs MUWPP_tout
	cp r17,r16		;;;; old code: cpi r17,'>'	
	brne MUWPP_L1
	ldi r20,$01		; got the char
	rjmp MUWPP_END

MUWPP_tout:
	ldi r20,$00
	rjmp MUWPP_END

MUWPP_END:
	;;rcall ModemUartFlush

	pop r18
	pop r17
	pop r16
	ret
	
;----------------------------------------------------------
; ModemSendCmdFlash - send a sequence from flash to modem
;----------------------------------------------------------
; Input:
;	Z-Register: Pointer to Sequence in FLASH (NULL teminated)
ModemSendCmdFlash:
	push r0
	push r16
	push ZL
	push ZH
ModemSendCmdFlash_L1:
	lpm				; defaults to lpm r0
	mov r16,r0
	tst r16			; test for Null
	breq ModemSendCmdFlash_End
	rcall ModemUartSendByte
	adiw ZH:ZL,$01 
	rjmp ModemSendCmdFlash_L1
ModemSendCmdFlash_End:
	pop ZH
	pop ZL
	pop r16
	pop r0
	ret

;---------------------------------------------------------------
; ModemSendItoA - Sends byte as decimal ASCII-Code (for AT+CGMS)
;---------------------------------------------------------------
;
; Inputs:
;  r16 - Byte to send
;
; Output:
;  goes to UART

ModemSendItoA:
	push r16
	push r17
	push r18
	push r19

	mov r17,r16
	ldi r18,$00 ; counter
	ldi r19,$00 ; flag - 1 = "must print"
	; hundrets
I2AC1:
	cpi r17,100
	brlo I2AC2
	inc r18
	subi r17,100
	rjmp I2AC1
I2AC2:
	cpi r18,$00
	breq I2AC3
	subi r18,-'0'
	mov r16,r18
	rcall ModemUartSendByte
	ldi r19,$01
	; tens
I2AC3:
	ldi r18,$00
I2AC4:
	cpi r17,10
	brlo I2AC5
	inc r18
	subi r17,10
	rjmp I2AC4
I2AC5:
	or r19,r18
	cpi r19,$00
	breq I2AC6
	subi r18,-'0'
	mov r16,r18
	rcall ModemUartSendByte
	; an finaly the ones (not that elegant...)
I2AC6:
	ldi r18,$00
I2AC7:
	cpi r17,1
	brlo I2AC8
	inc r18
	subi r17,1
	rjmp I2AC7
I2AC8:
	subi r18,-'0'
	mov r16,r18
	rcall ModemUartSendByte

	pop r19
	pop r18
	pop r17
	pop r16

	ret

;----------------------------------------------------------
; ModemReadATOI
;----------------------------------------------------------
; reads characters from to UART till a non digit occures
; stores digits as decimal in r16, eats leading blanks ($20)
;
; Output:
;   r16=result byte
;	r20=$00 timed out
;	r20=$01 ok

ModemReadATOI:
	push r17
	push r18
	push r19
	push r21
	push r22

	ldi r16,$00			; reset return value
	ldi r18,MODDEFTO	; load timout-value
	ldi r20,$01			; optimistic return

MUA2I_L1:
	rcall ModemUartReadByteFIFO ; read char from modem to r17
	brcs MUA2I_tout
	cpi r17,' '		; skip leading whitespaces 
	brne MUA2I_NOWS	; jump of no whitespace
	tst r16			; already read one char (=result!=0)?
	breq MUA2I_L1	; no -> jump to read more
MUA2I_NOWS:
	cpi r17,'0'		; check if digit
	brlo MUA2I_end	; invalid chars cause return from routine
	cpi r17,'9'+1
	brsh MUA2I_end
	subi r17,'0'	; char to number
	; multiply old value of r16 by 10
	; this is "old-fashioned" - ATmegas have a MUL instruction
	clr r21			; clear result
	clr r22			; clear counter
MUA2I_mul:
	add r21,r16		; add multiplicant to result
	inc r22			; increment counter
	cpi	r22,10		; compare counter 
	brlo MUA2I_mul	; add more if counter below 10
	add r21,r17		; add new dezimal
	mov r16,r21		; copy result to r16
	rjmp MUA2I_L1
	
MUA2I_tout:
	ldi r20,$00

MUA2I_end:
	pop r22
	pop r21
	pop r19
	pop r18
	pop r17
	ret
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -