Very Simple Uart Debug
tonyp12
Posts: 1,951
Sometimes you just want to keep a tab on a hub variable, to debug pasm make your cog wrlong to this hub location.
Once started it will keep resending the status of this variable forever, you never have to tell your spin code to send it each time.
If you want burst of data due to some pin state etc then add this at the end:
test _mypin,ina wz
if_z add cnt,_second 'restart main loop with 1sec delay
if_nz add cnt,#500 'restart main loop nearly right away
Save this to you PropTool library folder as: debug_uart
Here is an example how to use it, just add two lines to your code you want to debug
Under OBJ> debug : "debug_uart"
In Main> debug.start(@symbol)
Once started it will keep resending the status of this variable forever, you never have to tell your spin code to send it each time.
If you want burst of data due to some pin state etc then add this at the end:
test _mypin,ina wz
if_z add cnt,_second 'restart main loop with 1sec delay
if_nz add cnt,#500 'restart main loop nearly right away
Save this to you PropTool library folder as: debug_uart
{ Sends a 8 character HEX of a HUB Address value, at a fixed interval }
PUB start(address)
cognew (@entry,address)
DAT org 0
entry mov DIRA,_bit30
mov cnt,#7 '7 cycle overhead
add cnt,cnt 'add current cnt
loop1 waitcnt cnt,#100 'wait and then add 100 for below overhead
rdlong temp,PAR
mov charcnt,#9 '8 chars in a long + space
loop2 rol temp,#4 'visually send msb hex first
mov uartTx,temp
and uartTx,#%1111 'get nibble
add uartTx,#48 'start at ASCII for "0"
cmp uartTx,#58 wc 'over letter 9?
if_nc add uartTx,#7 'if so add for letters A-F
cmp charcnt,#1 wz 'last char?
if_z mov uartTX,#32 'replace with space
or uartTx,#$100 'set stopbit
shl uartTx,#1 'merge in startbit
mov bitcnt,#10 '10bit uart
loop3 waitcnt cnt,_baud
shr UartTx,#1 wc
muxc OUTA,_bit30
djnz bitcnt,#loop3
djnz charcnt,#loop2
add cnt,_second 'restart main loop with 1sec delay
jmp #loop1
_second long 80_000_000 '/2 or /4 if you want shorter delay
_baud long 80_000_000/115200
_bit30 long |<30
temp res 1
charcnt res 1
bitcnt res 1
uartTx res 1
Here is an example how to use it, just add two lines to your code you want to debug
Under OBJ> debug : "debug_uart"
In Main> debug.start(@symbol)
{Demo for debug_uart}
CON
_clkmode = xtal1 + pll16x 'Standard clock mode * crystal frequency = 80 MHz
_xinfreq = 5_000_000
VAR
long symbol
OBJ
debug : "debug_uart"
PUB main
debug.start(@symbol) ' start it by pointing to long hub variable
repeat
symbol +=1 ' forever loop that increase a variable

Comments
Save this to you PropTool library folder as: debug_uart_dec
{ Sends a 10 character DEC of a HUB Address value, at a fixed interval } PUB start(address) cognew (@entry,address) DAT org 0 entry mov DIRA,_bit30 mov cnt,#7 '7 cycle overhead add cnt,cnt 'add current cnt loop1 waitcnt cnt,#200 'wait and then add 200 for below overhead rdlong temp,PAR movs decimal,#table mov charcnt,#11 '10 dec chars in a long + space loop2 mov uartTx,#48 'start at ASCII for "0" decimal cmpsub temp,0-0 wc 'try 1 Billion first if_c add uartTx,#1 if_c jmp #decimal cmp charcnt,#1 wz 'last charter one? if_z mov uartTX,#32 'replace with space or uartTx,#$100 'set stopbit shl uartTx,#1 'merge in startbit mov bitcnt,#10 '10bit uart loop3 waitcnt cnt,_baud shr UartTx,#1 wc muxc OUTA,_bit30 djnz bitcnt,#loop3 add decimal,#1 'try 100mill next djnz charcnt,#loop2 add cnt,_second 'restart main loop with 1sec delay jmp #loop1 _second long 80_000_000 '/2 or /4 if you want shorter delay _baud long 80_000_000/115200 _bit30 long |<30 table long 1_000_000_000, 100_000_000, 10_000_000, 1_000_000, 100_000, 10_000, 1_000, 100, 10, 1,1 temp res 1 charcnt res 1 bitcnt res 1 uartTx res 1