diff --git a/avr/devices/n16/defs.asm b/avr/devices/n16/defs.asm index 12d4e10..0340b5f 100644 --- a/avr/devices/n16/defs.asm +++ b/avr/devices/n16/defs.asm @@ -17,7 +17,7 @@ ; PIR PB1 3 12 PA1 COM-DATA ; /RESET PB3 4 11 PA2 TCRT1000_Input ; PB2 5 10 PA3 LED -; COM_CLK PA7 6 9 PA4 TWI-SCL +; COM_CLK PA7 6 9 PA4 TWI-SCL ; TWI-SDA PA6 7 8 PA5 ; -------- ; diff --git a/avr/devices/n16/main/main.asm b/avr/devices/n16/main/main.asm index 4354219..531361f 100644 --- a/avr/devices/n16/main/main.asm +++ b/avr/devices/n16/main/main.asm @@ -59,7 +59,7 @@ #define MODULES_CLOCK #define MODULES_LED_SIMPLE #define MODULES_NETWORK -#define MODULES_UART_BITBANG +#define MODULES_COM2W #define MODULES_TWI_MASTER ;#define MODULES_LCD ;#define LCD_MINIMAL_FONT @@ -109,7 +109,7 @@ ; rjmp main ; Reset vector rjmp BOOTLOADER_ADDR ; Reset vector ; use this for flashed system reti ; EXT_INT0 - rjmp UART_BitBang_PcintIsr ; PCI0 + rjmp com2wPcintIsr ; PCI0 reti ; PCI1 reti ; WATCHDOG reti ; ICP1 @@ -204,7 +204,7 @@ onEveryLoop: ; --------------------------------------------------------------------------- ; defines for network interface -.equ netInterfaceData = uart_bitbang_iface +.equ netInterfaceData = com2w_iface diff --git a/avr/devices/n21/defs.asm b/avr/devices/n21/defs.asm index 2f5b3ae..67bed89 100644 --- a/avr/devices/n21/defs.asm +++ b/avr/devices/n21/defs.asm @@ -17,7 +17,7 @@ ; PIR PB1 3 12 PA1 COM-DATA ; /RESET PB3 4 11 PA2 TCRT1000_Input ; AUX-B2 PB2 5 10 PA3 LED -; COM_CLK PA7 6 9 PA4 TWI-SCL +; COM_CLK PA7 6 9 PA4 TWI-SCL ; TWI-SDA PA6 7 8 PA5 ; -------- ; diff --git a/avr/devices/n24/defs.asm b/avr/devices/n24/defs.asm index 0bdeb74..1e7d5cb 100644 --- a/avr/devices/n24/defs.asm +++ b/avr/devices/n24/defs.asm @@ -17,7 +17,7 @@ ; PIR PB1 3 12 PA1 COM-DATA ; /RESET PB3 4 11 PA2 AUX-PA2 ; PB2 5 10 PA3 LED -; COM_CLK PA7 6 9 PA4 TWI-SCL +; COM_CLK PA7 6 9 PA4 TWI-SCL ; TWI-SDA PA6 7 8 PA5 ; -------- ; diff --git a/avr/devices/n25/defs.asm b/avr/devices/n25/defs.asm index 2a9b1e3..fa508c6 100644 --- a/avr/devices/n25/defs.asm +++ b/avr/devices/n25/defs.asm @@ -16,7 +16,7 @@ ; PB1 3 12 PA1 COM-DATA ; PB3 4 11 PA2 ; PB2 5 10 PA3 LED -; COM_CLK PA7 6 9 PA4 LED_CLK +; COM_CLK PA7 6 9 PA4 LED_CLK ; LED_DATA PA6 7 8 PA5 LED_Mode (0 for SPI) ; -------- ; diff --git a/avr/devices/n26/defs.asm b/avr/devices/n26/defs.asm index 904a131..0b4a2fd 100644 --- a/avr/devices/n26/defs.asm +++ b/avr/devices/n26/defs.asm @@ -17,7 +17,7 @@ ; PB1 3 12 PA1 COM-DATA ; /RESET PB3 4 11 PA2 ; PB2 5 10 PA3 LED -; COM_CLK PA7 6 9 PA4 TWI-SCL +; COM_CLK PA7 6 9 PA4 TWI-SCL ; TWI-SDA PA6 7 8 PA5 ; -------- ; diff --git a/avr/devices/n27/defs.asm b/avr/devices/n27/defs.asm index 7fc3e86..c1646cc 100644 --- a/avr/devices/n27/defs.asm +++ b/avr/devices/n27/defs.asm @@ -17,7 +17,7 @@ ; PIR PB1 3 12 PA1 COM-DATA ; /RESET PB3 4 11 PA2 AUX-PA2 ; PB2 5 10 PA3 LED -; COM_CLK PA7 6 9 PA4 TWI-SCL +; COM_CLK PA7 6 9 PA4 TWI-SCL ; TWI-SDA PA6 7 8 PA5 ; -------- ; diff --git a/avr/devices/t03/defs.asm b/avr/devices/t03/defs.asm index cf5f970..14b690a 100644 --- a/avr/devices/t03/defs.asm +++ b/avr/devices/t03/defs.asm @@ -13,9 +13,9 @@ ; AtTiny 841 ; ------- ; VCC 1 14 GND -; PB0 2 13 PA0 ATTN0 -; PB1 3 12 PA1 TXD0 (UART0) -; /RESET PB3 4 11 PA2 RXD0 (UART0) +; PB0 2 13 PA0 COM-CLK +; PB1 3 12 PA1 +; /RESET PB3 4 11 PA2 COM-DATA ; LED PB2 5 10 PA3 ; 1-wire PA7 6 9 PA4 RXD1 (UART1) [SCK, PRG] ; [MOSI,PRG] SDA (I2C) PA6 7 8 PA5 TXD1 (UART1) [MISO, PRG] diff --git a/avr/devices/t03/main/main.asm b/avr/devices/t03/main/main.asm index c817180..9580fc8 100644 --- a/avr/devices/t03/main/main.asm +++ b/avr/devices/t03/main/main.asm @@ -83,9 +83,9 @@ .equ NET_BUFFERS_NUM = 10 +.equ NET_MSGNUMINBUF_SIZE = 8 ; max buffer nums in ringbuffer (global incoming) +.equ NET_IFACE_OUTMSGBUF_SIZE = 8 ; max buffer nums in ringbuffer (per interface outbound) -.equ UART_HW_MSGNUMINBUF_SIZE = 8 -.equ UART_HW_MSGNUMOUTBUF_SIZE = 8 @@ -110,7 +110,6 @@ rjmp BOOTLOADER_ADDR ; 1: RESET Reset vector use this for flashed system reti ; 2: INT0 External Interrupt Request 0 -; rjmp COM2W1_ClkChangeIsr ; 3: PCINT0 Pin Change Interrupt 0 rjmp com2wPcintIsr ; 3: PCINT0 Pin Change Interrupt 0 reti ; 4: PCINT1 Pin Change Interrupt 1 reti ; 5: WDT Watchdog Time-out @@ -131,7 +130,6 @@ reti ; 20: TIM2_OVF (OVF2) Timer/Counter2 Overflow reti ; 21: SPI SPI Serial Transfer Complete reti ; 22: USART0_RXS USART0 Rx Start -; rjmp ComOnUart0_RxCharIsr ; 23: USART0_RXC USART0 Rx Complete reti ; 23: USART0_RXC USART0 Rx Complete reti ; 24: USART0_DRE USART0 Data Register Empty reti ; 25: USART0_TXC USART0 Tx Complete @@ -335,6 +333,5 @@ addMsgToInterface_toUart1: ; defines for network interface .equ netInterfaceData = ttyOnUart1_iface -;.equ netInterfaceData2 = comOnUart0_iface .equ netInterfaceData2 = com2w_iface diff --git a/avr/devices/t03/test/main.asm b/avr/devices/t03/test/main.asm index 1154fbb..79b7a06 100644 --- a/avr/devices/t03/test/main.asm +++ b/avr/devices/t03/test/main.asm @@ -75,7 +75,7 @@ ;#define MODULES_DS18B20 ;#define MODULES_MOTION #define MODULES_NETWORK -#define MODULES_COM2W0 +#define MODULES_COM2W ;#define MODULES_COMONUART0 #define MODULES_TTYONUART1 #define APPS_STATS @@ -85,6 +85,8 @@ .equ NET_BUFFERS_NUM = 10 +.equ NET_MSGNUMINBUF_SIZE = 8 ; max buffer nums in ringbuffer (global incoming) +.equ NET_IFACE_OUTMSGBUF_SIZE = 8 ; max buffer nums in ringbuffer (per interface outbound) .equ UART_HW_MSGNUMINBUF_SIZE = 8 .equ UART_HW_MSGNUMOUTBUF_SIZE = 8 @@ -132,7 +134,7 @@ rjmp BOOTLOADER_ADDR ; 1: RESET Reset vector use this for flashed system reti ; 2: INT0 External Interrupt Request 0 - rjmp COM2W0_ClkChangeIsr ; 3: PCINT0 Pin Change Interrupt 0 + rjmp com2wPcintIsr ; 3: PCINT0 Pin Change Interrupt 0 reti ; 4: PCINT1 Pin Change Interrupt 1 reti ; 5: WDT Watchdog Time-out reti ; 6: TIM1_CAPT Timer/Counter1 Capture Event @@ -244,8 +246,7 @@ onMessageReceived: ; defines for network interface .equ netInterfaceData = ttyOnUart1_iface -;.equ netInterfaceData2 = comOnUart0_iface -.equ netInterfaceData2 = com2w0_iface +.equ netInterfaceData2 = com2w_iface ; debug diff --git a/avr/modules/com2w/com2w.asm b/avr/modules/com2w/com2w.asm index d2fe8b2..56988d3 100644 --- a/avr/modules/com2w/com2w.asm +++ b/avr/modules/com2w/com2w.asm @@ -170,6 +170,7 @@ com2wReceiveNextPkg_end: +#if 0 ; --------------------------------------------------------------------------- ; @routine com2wReceiveAndCheckMsg ; @@ -208,6 +209,7 @@ com2wReceiveAndCheckMsg_msgOk: sec ret ; @end +#endif @@ -247,9 +249,9 @@ com2wSendNextPkg_end: ; @clobbers R16, R18 (R20, R22, R24, R25, X) com2wSendMsg: - ldi r20, 11 ; wait for about 55us for clock low - rcall com2wWaitForClockLowMulti5Us - brcs com2wSendMsg_busy ; CLK got low while waiting, so line is busy + ldi r20, 6 ; wait for about 60us for clock low + rcall com2wWaitForClockLowMulti10Us ; (R16, R20, R22) + brcs com2wSendMsg_busy ; CLK got low while waiting, so line is busy push r15 in r15, SREG cli ; atomic disable irq and set CLK low @@ -313,7 +315,7 @@ com2wSendByte: ldi r17, 8 com2wSendByte_loop: rcall com2wClkSetLow - rcall com2wWaitTime1 ; longer wait period (R22) + rcall com2wWaitTime2 ; shorter wait period (R22) lsr r16 brcs com2wSendByte_send1 rcall com2wDataSetLow @@ -321,12 +323,12 @@ com2wSendByte_loop: com2wSendByte_send1: rcall com2wDataSetHigh com2wSendByte_sent: - rcall com2wWaitTime2 ; shorter wait period (R22) + Utils_WaitNanoSecs 5000, 0, r22 ; wait for very short time to ensure data is stable when clock rises push r15 in r15, SREG cli ; ensure time period by disabling irqs rcall com2wClkSetHigh - rcall com2wWaitTime1 ; longer wait period (R22) + rcall com2wWaitTime2 ; shorter wait period (R22) out SREG, r15 pop r15 dec r17 @@ -416,21 +418,12 @@ com2wRecvByte: ldi r17, 8 clr r16 com2wRecvByte_loop: - ldi r20, 31 ; wait up to 155us for clock low - rcall com2wWaitForClockLowMulti5Us ; (R20, R22) - brcs com2wRecvByte_waitForClkHigh - ldi r20, 31 ; wait up to 155us for clock low - rcall com2wWaitForClockLowMulti5Us ; (R20, R22) + ldi r20, 50 ; wait up to 500us for clock low + rcall com2wWaitForClockLowMulti10Us ; (R16, R20, R22) brcc com2wRecvByte_end -com2wRecvByte_waitForClkHigh: - ldi r20, 31 ; wait up to 155us for clock high - rcall com2wWaitForClockHighMulti5Us ; (R20, R22) - brcs com2wRecvByte_readBit - ldi r20, 31 ; wait up to 155us for clock high - rcall com2wWaitForClockHighMulti5Us ; (R20, R22) + ldi r20, 50 ; wait up to 500us for clock high + rcall com2wWaitForClockHighMulti10Us ; (R16, R20, R22) brcc com2wRecvByte_end - -com2wRecvByte_readBit: ; handle received bit inr r18, COM_DATA_INPUT andi r18, (1< 1000000 +; begin version for > 1000000 Hz + ldi r22, clock/1000000 +com2wWaitForClockHighMulti10Us_loop0: + push r20 +com2wWaitForClockHighMulti10Us_loop: ; 10 cycles per loop + sbic COM_CLK_INPUT, COM_CLK_PIN ; +2 if skipped, +1 if taken + rjmp com2wWaitForClockHighMulti10Us_stateReached ; (+2 if taken) + sbic COM_CLK_INPUT, COM_CLK_PIN ; +2 if skipped, +1 if taken + rjmp com2wWaitForClockHighMulti10Us_stateReached ; (+2 if taken) + sbic COM_CLK_INPUT, COM_CLK_PIN ; +2 if skipped, +1 if taken + rjmp com2wWaitForClockHighMulti10Us_stateReached ; (+2 if taken) + nop ; +1 + dec r20 ; +1 + brne com2wWaitForClockHighMulti10Us_loop ; +2 if TRUE, +1 if FALSE + pop r20 + dec r22 ; +1 + brne com2wWaitForClockHighMulti10Us_loop0 ; +1 if FALSE, +2 if TRUE + clc ; +1 + ret ; +4 +com2wWaitForClockHighMulti10Us_stateReached: + pop r20 + sec ; +1 + ret ; +4 +; end version for > 1000000 Hz +.elif clock < 1000000 + .error "Clock speed too low" .else - .error "Unhandled clock speed" +; begin version for 1000000 Hz +com2wWaitForClockHighMulti10Us_loop: ; 10 cycles per loop + sbic COM_CLK_INPUT, COM_CLK_PIN ; +2 if skipped, +1 if taken + rjmp com2wWaitForClockHighMulti10Us_stateReached ; (+2 if taken) + sbic COM_CLK_INPUT, COM_CLK_PIN ; +2 if skipped, +1 if taken + rjmp com2wWaitForClockHighMulti10Us_stateReached ; (+2 if taken) + sbic COM_CLK_INPUT, COM_CLK_PIN ; +2 if skipped, +1 if taken + rjmp com2wWaitForClockHighMulti10Us_stateReached ; (+2 if taken) + nop ; +1 + dec r20 ; +1 + brne com2wWaitForClockHighMulti10Us_loop ; +2 if TRUE, +1 if FALSE + clc ; +1 + ret ; +4 +com2wWaitForClockHighMulti10Us_stateReached: + sec ; +1 + ret ; +4 +; end version for 1000000 Hz .endif - -com2wWaitForClockHighMulti5Us_loop: ; 5 cycles per loop - sbic COM_CLK_INPUT, COM_CLK_PIN ; +2 if skipped, +1 if not - rjmp com2wWaitForClockHighMulti5Us_stateReached ; +2 - dec r20 ; +1 - brne com2wWaitForClockHighMulti5Us_loop ; +2 - clc ; +1 - ret ; +4 -com2wWaitForClockHighMulti5Us_stateReached: - sec ; +1 - ret ; +4 ; @end ; --------------------------------------------------------------------------- -; @routine com2wWaitForClockLowMulti5Us +; @routine com2wWaitForClockLowMulti10Us ; ; Wait for low CLK ; -; @param R20 multiple of 5us to wait (e.g. "2" for "10" us, max: 64) +; @param R20 multiple of 10us to wait (e.g. "2" for "20" us) ; @return CFLAG set if okay (state reached), cleared on error ; @clobbers: r20, r22 -com2wWaitForClockLowMulti5Us: -.if clock == 8000000 - add r20, r20 ; *2 - add r20, r20 ; *4 - add r20, r20 ; *8 -.endif -.elif clock == 1000000 -; nothing to do +com2wWaitForClockLowMulti10Us: +.if clock > 1000000 +; begin version for > 1000000 Hz + ldi r22, clock/1000000 +com2wWaitForClockLowMulti10Us_loop0: + push r20 +com2wWaitForClockLowMulti10Us_loop: ; 10 cycles per loop + sbis COM_CLK_INPUT, COM_CLK_PIN ; +2 if skipped, +1 if taken + rjmp com2wWaitForClockLowMulti10Us_stateReached ; (+2 if taken) + sbis COM_CLK_INPUT, COM_CLK_PIN ; +2 if skipped, +1 if taken + rjmp com2wWaitForClockLowMulti10Us_stateReached ; (+2 if taken) + sbis COM_CLK_INPUT, COM_CLK_PIN ; +2 if skipped, +1 if taken + rjmp com2wWaitForClockLowMulti10Us_stateReached ; (+2 if taken) + nop ; +1 + dec r20 ; +1 + brne com2wWaitForClockLowMulti10Us_loop ; +2 if TRUE, +1 if FALSE + pop r20 + dec r22 ; +1 + brne com2wWaitForClockLowMulti10Us_loop0 ; +1 if FALSE, +2 if TRUE + clc ; +1 + ret ; +4 +com2wWaitForClockLowMulti10Us_stateReached: + pop r20 + sec ; +1 + ret ; +4 +; end version for > 1000000 Hz +.elif clock < 1000000 + .error "Clock speed too low" .else - .error "Unhandled clock speed" +; begin version for 1000000 Hz +com2wWaitForClockLowMulti10Us_loop: ; 10 cycles per loop + sbis COM_CLK_INPUT, COM_CLK_PIN ; +2 if skipped, +1 if taken + rjmp com2wWaitForClockLowMulti10Us_stateReached ; (+2 if taken) + sbis COM_CLK_INPUT, COM_CLK_PIN ; +2 if skipped, +1 if taken + rjmp com2wWaitForClockLowMulti10Us_stateReached ; (+2 if taken) + sbis COM_CLK_INPUT, COM_CLK_PIN ; +2 if skipped, +1 if taken + rjmp com2wWaitForClockLowMulti10Us_stateReached ; (+2 if taken) + nop ; +1 + dec r20 ; +1 + brne com2wWaitForClockLowMulti10Us_loop ; +2 if TRUE, +1 if FALSE + clc ; +1 + ret ; +4 +com2wWaitForClockLowMulti10Us_stateReached: + sec ; +1 + ret ; +4 +; end version for 1000000 Hz .endif - -com2wWaitForClockLowMulti5Us_loop: ; 5 cycles per loop - sbis COM_CLK_INPUT, COM_CLK_PIN ; +2 if skipped, +1 if not - rjmp com2wWaitForClockLowMulti5Us_stateReached ; +2 - dec r20 ; +1 - brne com2wWaitForClockLowMulti5Us_loop ; +2 - clc ; +1 - ret ; +4 -com2wWaitForClockLowMulti5Us_stateReached: - sec ; +1 - ret ; +4 ; @end diff --git a/avr/modules/com2w/com2wn_send.asm b/avr/modules/com2w/com2wn_send.asm index 3faee47..137abbc 100644 --- a/avr/modules/com2w/com2wn_send.asm +++ b/avr/modules/com2w/com2wn_send.asm @@ -29,13 +29,13 @@ com2wnSendMsg: ldi r20, 6 ; wait for about 60us for clock low - rcall com2wWaitForClockLowMulti10Us ; (R17, R20, R22) - brcs com2wnSendMsg_busy ; CLK got low while waiting, so line is busy + rcall com2wWaitForClockLowMulti10Us ; (R16, R20, R22) + brcs com2wnSendMsg_busy ; CLK got low while waiting, so line is busy push r15 in r15, SREG cli ; atomic disable irq and set CLK low rcall com2wnDisableClkIrq ; (none) - rcall com2wnClkSetLow ; reserve bus (none) + rcall com2wnClkSetLow ; reserve bus (none) out SREG, r15 pop r15 adiw xh:xl, NETMSG_OFFS_MSGLEN diff --git a/avr/modules/com2w/com2wn_wait.asm b/avr/modules/com2w/com2wn_wait.asm index 0f95350..365589f 100644 --- a/avr/modules/com2w/com2wn_wait.asm +++ b/avr/modules/com2w/com2wn_wait.asm @@ -31,30 +31,33 @@ com2wWaitForClockHighMulti10Us: .if clock > 1000000 ; begin version for > 1000000 Hz - ldi r20, LOW(clock/1000000) - ldd r22, Y+COM2W_IFACE_OFFS_PINMASK_CLK ; +2 + push r19 + ldi r19, LOW(clock/1000000) + ldd r22, Y+COM2W_IFACE_OFFS_PINMASK_CLK ; +2 com2wWaitForClockHighMulti10Us_loop0: - push r20 ; +2 + push r20 com2wWaitForClockHighMulti10Us_loop: ; 10 cycles per loop - inr r16, COM_CLK_INPUT ; +1 (if low port, +2 if high port) - and r16, r22 ; +1 - breq com2wWaitForClockHighMulti10Us_stateReached ; +1 if FALSE, +2 if TRUE - - inr r16, COM_CLK_INPUT ; +1 (if low port, +2 if high port) - and r16, r22 ; +1 - breq com2wWaitForClockHighMulti10Us_stateReached ; +1 if FALSE, +2 if TRUE - - nop ; +1 + inr r16, COM_CLK_INPUT ; +1 (if low port, +2 if high port) + and r16, r22 ; +1 + brne com2wWaitForClockHighMulti10Us_stateReached ; +1 if FALSE, +2 if TRUE - dec r20 ; +1 - brne com2wWaitForClockHighMulti10Us_loop ; +2 if TRUE, +1 if FALSE - pop r20 ; +2 - dec r20 ; +1 - brne com2wWaitForClockHighMulti10Us_loop0 ; +1 if FALSE, +2 if TRUE + inr r16, COM_CLK_INPUT ; +1 (if low port, +2 if high port) + and r16, r22 ; +1 + brne com2wWaitForClockHighMulti10Us_stateReached ; +1 if FALSE, +2 if TRUE + + nop ; +1 + + dec r20 ; +1 + brne com2wWaitForClockHighMulti10Us_loop ; +2 if TRUE, +1 if FALSE + pop r20 + dec r19 ; +1 + brne com2wWaitForClockHighMulti10Us_loop0 ; +1 if FALSE, +2 if TRUE + pop r19 clc ; +1 ret ; +4 com2wWaitForClockHighMulti10Us_stateReached: - pop r20 + pop r20 + pop r19 sec ; +1 ret ; +4 ; end version for > 1000000 Hz @@ -66,11 +69,11 @@ com2wWaitForClockHighMulti10Us_stateReached: com2wWaitForClockHighMulti10Us_loop: ; 10 cycles per loop inr r16, COM_CLK_INPUT ; +1 (if low port, +2 if high port) and r16, r22 ; +1 - breq com2wWaitForClockHighMulti10Us_stateReached ; +1 if FALSE, +2 if TRUE + brne com2wWaitForClockHighMulti10Us_stateReached ; +1 if FALSE, +2 if TRUE inr r16, COM_CLK_INPUT ; +1 (if low port, +2 if high port) and r16, r22 ; +1 - brne com2wWaitForClockHighMulti10Us_stateReached ; +1 if FALSE, +2 if TRUE + brne com2wWaitForClockHighMulti10Us_stateReached ; +1 if FALSE, +2 if TRUE nop ; +1 @@ -97,6 +100,79 @@ com2wWaitForClockHighMulti10Us_stateReached: ; @clobbers: r16, r20, r22 com2wWaitForClockLowMulti10Us: +.if clock > 1000000 +; begin version for > 1000000 Hz + push r19 + ldi r19, LOW(clock/1000000) + ldd r22, Y+COM2W_IFACE_OFFS_PINMASK_CLK ; +2 +com2wWaitForClockLowMulti10Us_loop0: + push r20 +com2wWaitForClockLowMulti10Us_loop: ; 10 cycles per loop + inr r16, COM_CLK_INPUT ; +1 (if low port, +2 if high port) + and r16, r22 ; +1 + breq com2wWaitForClockLowMulti10Us_stateReached ; +1 if FALSE, +2 if TRUE + + inr r16, COM_CLK_INPUT ; +1 (if low port, +2 if high port) + and r16, r22 ; +1 + breq com2wWaitForClockLowMulti10Us_stateReached ; +1 if FALSE, +2 if TRUE + + nop ; +1 + + dec r20 ; +1 + brne com2wWaitForClockLowMulti10Us_loop ; +2 if TRUE, +1 if FALSE + pop r20 + dec r19 ; +1 + brne com2wWaitForClockLowMulti10Us_loop0 ; +1 if FALSE, +2 if TRUE + pop r19 + clc ; +1 + ret ; +4 +com2wWaitForClockLowMulti10Us_stateReached: + pop r20 + pop r19 + sec ; +1 + ret ; +4 +; end version for > 1000000 Hz +.elif clock < 1000000 + .error "Clock speed too low" +.else +; begin version for 1000000 Hz + ldd r22, Y+COM2W_IFACE_OFFS_PINMASK_CLK ; +2 +com2wWaitForClockLowMulti10Us_loop: ; 10 cycles per loop + inr r16, COM_CLK_INPUT ; +1 (if low port, +2 if high port) + and r16, r22 ; +1 + breq com2wWaitForClockLowMulti10Us_stateReached ; +1 if FALSE, +2 if TRUE + + inr r16, COM_CLK_INPUT ; +1 (if low port, +2 if high port) + and r16, r22 ; +1 + breq com2wWaitForClockLowMulti10Us_stateReached ; +1 if FALSE, +2 if TRUE + + nop ; +1 + + dec r20 ; +1 + brne com2wWaitForClockLowMulti10Us_loop ; +2 if TRUE, +1 if FALSE + clc ; +1 + ret ; +4 +com2wWaitForClockLowMulti10Us_stateReached: + sec ; +1 + ret ; +4 +; end version for 1000000 Hz +.endif + + + + + + + + + + + + + + + + .if clock > 1000000 ; begin version for > 1000000 Hz ldi r20, LOW(clock/1000000) diff --git a/avr/modules/com2w/defs.asm b/avr/modules/com2w/defs.asm index 5837e1d..7e644ea 100644 --- a/avr/modules/com2w/defs.asm +++ b/avr/modules/com2w/defs.asm @@ -11,7 +11,7 @@ #define AVR_MODULES_COM2W_DEFS_H -.equ COM2W_WAITTIME1 = 30000 +.equ COM2W_WAITTIME1 = 17000 .equ COM2W_WAITTIME2 = 10000 .equ COM2W_SKIPPING_MAXREADCOUNTER = 1 diff --git a/avr/modules/flash/io_com2w.asm b/avr/modules/flash/io_com2w.asm index 0343e7e..fe92966 100644 --- a/avr/modules/flash/io_com2w.asm +++ b/avr/modules/flash/io_com2w.asm @@ -13,7 +13,7 @@ -.equ COM2W_WAITTIME1 = 30000 +.equ COM2W_WAITTIME1 = 17000 .equ COM2W_WAITTIME2 = 10000 @@ -85,21 +85,17 @@ ioRawSendMsg_loop: ioRawWaitForValidMsg: ; wait for CLK low - ldi r19, 20 ; wait for up to 10s -ioRawWaitForValidMsg_waitLowLoop1: + ldi r17, 20 ; wait for up to 10s +ioRawWaitForValidMsg_waitLowLoop: .ifdef LED_PIN - sbi LED_PIN, LED_PINNUM ; toggle + sbi LED_PIN, LED_PINNUM ; toggle .endif - ldi r18, 5 -ioRawWaitForValidMsg_waitLowLoop2: - ldi r20, 10 ; wait for about 100us for clock low - rcall com2wWaitForClockLowMulti10Us ; R20, R22 - brcs ioRawWaitForValidMsg_recvMsg - dec r18 - brne ioRawWaitForValidMsg_waitLowLoop2 - dec r19 - brne ioRawWaitForValidMsg_waitLowLoop1 - clc ; no msg received + ldi r18, 5 ; wait up to 500ms + rcall com2wWaitForClockLowMulti100ms ; (r18, r19, r20, r22) + brcs ioRawWaitForValidMsg_recvMsg + dec r17 + brne ioRawWaitForValidMsg_waitLowLoop + clc ; no msg received rjmp ioRawWaitForValidMsg_end ioRawWaitForValidMsg_recvMsg: ; receive message @@ -112,13 +108,38 @@ ioRawWaitForValidMsg_recvMsg: ; validate CRC ldi xl, LOW(flashRecvBuffer) ldi xh, HIGH(flashRecvBuffer) - rcall NETMSG_CheckMessageInBuffer ; (R16, R17, R18, R19, R20, X) + rcall NETMSG_CheckMessageInBuffer ; (R16, R17, R18, R19, R20, X) ioRawWaitForValidMsg_end: ret ; @end +; --------------------------------------------------------------------------- +; @routine com2wWaitForClockLowMulti100ms +; +; wait for multiple of 100ms +; +; @param R18 number of 100ms to wait +; @return CFLAG set if okay (state reached), cleared on error +; @clobbers r18, r19, r20, r22 + +com2wWaitForClockLowMulti100ms: +com2wWaitForClockLowMulti100ms_loop1: + ldi r19, 100 +com2wWaitForClockLowMulti100ms_loop2: + ldi r20, 100 ; wait for 1ms for clock low + rcall com2wWaitForClockLowMulti10Us ; (R20, R22) + brcs com2wWaitForClockLowMulti100ms_ret + dec r19 + brne com2wWaitForClockLowMulti100ms_loop2 + dec r18 + brne com2wWaitForClockLowMulti100ms_loop1 + clc +com2wWaitForClockLowMulti100ms_ret: + ret +; @end + @@ -185,7 +206,7 @@ com2wSendByte: ldi r17, 8 com2wSendByte_loop: rcall com2wClkSetLow - rcall com2wWaitTime1 ; longer wait period (R22) + rcall com2wWaitTime2 ; shorter wait period (R22) lsr r16 brcs com2wSendByte_send1 rcall com2wDataSetLow @@ -193,9 +214,9 @@ com2wSendByte_loop: com2wSendByte_send1: rcall com2wDataSetHigh com2wSendByte_sent: - rcall com2wWaitTime2 ; shorter wait period (R22) + Utils_WaitNanoSecs 5000, 0, r22 ; wait for very short time to ensure data is stable when clock rises rcall com2wClkSetHigh - rcall com2wWaitTime1 ; longer wait period (R22) + rcall com2wWaitTime2 ; shorter wait period (R22) dec r17 brne com2wSendByte_loop ret @@ -267,10 +288,10 @@ com2wRecvByte: ldi r17, 8 clr r16 com2wRecvByte_loop: - ldi r20, 30 ; wait up to 300us for clock low + ldi r20, 70 ; wait up to 700us for clock low rcall com2wWaitForClockLowMulti10Us ; (R20, R22) brcc com2wRecvByte_end - ldi r20, 30 ; wait up to 300us for clock high + ldi r20, 70 ; wait up to 700us for clock high rcall com2wWaitForClockHighMulti10Us ; (R20, R22) brcc com2wRecvByte_end ; handle received bit @@ -357,30 +378,26 @@ com2wDataSetLow: ; ; @param R20 multiple of 10us to wait (e.g. "2" for "20" us) ; @return CFLAG set if okay (state reached), cleared on error -; @clobbers: r16, r20, r22 +; @clobbers: r20, r22 com2wWaitForClockLowMulti10Us: .if clock > 1000000 ; begin version for > 1000000 Hz - ldi r20, LOW(clock/1000000) - ldi r22, (1< 1000000 ; begin version for > 1000000 Hz - ldi r20, LOW(clock/1000000) - ldi r22, (1<