duskos

dusk os fork
git clone git://git.alexwennerberg.com/duskos
Log | Files | Refs | README | LICENSE

commit 71ecf02055d5a1f562b05237bc41b39ccf9c82ad
parent c3575490c46b60f1d657e4de3d9080d6c757ee38
Author: Virgil Dupras <hsoft@hardcoded.net>
Date:   Wed,  7 Jun 2023 11:21:36 -0400

rpi: rewrite uartinit in forth

Diffstat:
Mfs/xcomp/arm/rpi/kernel.fs | 52++++------------------------------------------------
Mfs/xcomp/rpiboot.fs | 39+++++++++++++++++++++++++++++++++++++--
2 files changed, 41 insertions(+), 50 deletions(-)

diff --git a/fs/xcomp/arm/rpi/kernel.fs b/fs/xcomp/arm/rpi/kernel.fs @@ -4,17 +4,9 @@ \ For RPI model 1 $20000000 const MMIO_BASE $200000 const GPIO_BASE -$94 const GPPUD -$98 const GPPUDCLK0 $1000 const UART0_BASE $00 const UART0_DR $18 const UART0_FR -$24 const UART0_IBRD -$28 const UART0_FBRD -$2c const UART0_LCRH -$30 const UART0_CR -$38 const UART0_IMSC -$44 const UART0_ICR \ Macros : xnip, add) rPSP rdn) CELLSZ i) ,) ; @@ -28,10 +20,6 @@ $44 const UART0_ICR : wcall, xwordlbl execute, ; : wjmp, xwordlbl abs>rel b) ,) ; \ only for leaf words! -: delay, ( ncycles -- ) - mov) r0 rd) swap ( ncycles ) i) ,) - pc sub) r0 rdn) 1 i) f) ,) ( pc ) abs>rel b) ne) ,) ; - : pc>reg, ( pc r -- ) dip pc -^ 8 + | ( off r ) mov) over rd) rPC rm) ,) @@ -145,6 +133,8 @@ xcode parse ( str -- n? f ) L4 abs>rel b) ,) xcode emit ( c -- ) \ Preserves r0, r1, r2 + r7 MMIO_BASE GPIO_BASE movi2, + add) r7 rdn) UART0_BASE i) ,) pc ldr) r3 rd) r7 rn) UART0_FR +i) ,) tst) r3 rn) $20 i) ,) @@ -154,6 +144,8 @@ xcode emit ( c -- ) \ Preserves r0, r1, r2 exit, xcode key ( -- c ) + r7 MMIO_BASE GPIO_BASE movi2, + add) r7 rdn) UART0_BASE i) ,) pc ldr) r3 rd) r7 rn) UART0_FR +i) ,) tst) r3 rn) $10 i) ,) @@ -835,42 +827,6 @@ xcode runword ( str -- ) pushret, L1 execute, popret, L2 abs>rel b) ,) -xcode uartinit - r6 MMIO_BASE GPIO_BASE movi2, \ r6 = GPIO_BASE - add) r7 rd) r6 rn) UART0_BASE i) ,) \ r7 = UART0_BASE - \ Disable UART0 - mov) r1 rd) 0 i) ,) str) r1 rd) r7 rn) UART0_CR +i) ,) - \ Disable pull up/down for all GPIO pins & delay for 150 cycles. - ( r1=0 ) str) r1 rd) r6 rn) GPPUD +i) ,) - 150 delay, - \ Disable pull up/down for pin 14,15 & delay for 150 cycles. - mov) r1 rd) $c000 i) ,) str) r1 rd) r6 rn) GPPUDCLK0 +i) ,) - 150 delay, - \ Write 0 to GPPUD and GPPUDCLK0 to make it take effect. - mov) r1 rd) 0 i) ,) - str) r1 rd) r6 rn) GPPUD +i) ,) - str) r1 rd) r6 rn) GPPUDCLK0 +i) ,) - \ Clear pending interrupts. - mov) r1 rd) $700 i) ,) add) r1 rd) r1 rn) $ff i) ,) - str) r1 rd) r7 rn) UART0_ICR +i) ,) - \ Set integer & fractional part of baud rate. - \ UART_CLOCK on rpi1 is 48Mhz - \ Divider = UART_CLOCK/(16 * Baud) - \ Fraction part register = 64th of the unit - \ Baud = 115200. - \ Divider = 48000000 / (16 * 115200) = 26.042 - mov) r1 rd) 26 i) ,) str) r1 rd) r7 rn) UART0_IBRD +i) ,) - mov) r1 rd) 3 i) ,) str) r1 rd) r7 rn) UART0_FBRD +i) ,) - \ Enable FIFO & 8 bit data transmission (1 stop bit, no parity). - mov) r1 rd) $70 i) ,) str) r1 rd) r7 rn) UART0_LCRH +i) ,) - \ Mask all interrupts. - mov) r1 rd) $700 i) ,) add) r1 rd) r1 rn) $f2 i) ,) - str) r1 rd) r7 rn) UART0_IMSC +i) ,) - \ Enable UART0, receive & transfer part of UART. - mov) r1 rd) $300 i) ,) add) r1 rd) r1 rn) $01 i) ,) - str) r1 rd) r7 rn) UART0_CR +i) ,) - exit, - pc to L1 ( halfbyte -- ) cmp) rTOP rn) 10 i) ,) add) lo) rTOP rdn) '0' i) ,) diff --git a/fs/xcomp/rpiboot.fs b/fs/xcomp/rpiboot.fs @@ -90,7 +90,6 @@ code ?dup W=0>Z, 0 Z) branchC, dup, then exit, Z) _ = NZ) _ <> >) _ < <) _ > >=) _ <= <=) _ >= : \ begin in< $20 < until ; immediate -uartinit \ see the (wnf) if comment is mishandled \ hello, this is a comment! : exit popret, exit, ; immediate : ( begin @@ -138,4 +137,40 @@ code (cell) pushret, r> popret, exit, : create code pushret, compile (cell) ; : const code litn exit, ; 4 const CELLSZ -prompt interactive! + +\ For RPI model 1 +$20000000 const MMIO_BASE +MMIO_BASE $200000 + const GPIO_BASE +GPIO_BASE $94 + const GPPUD +GPIO_BASE $98 + const GPPUDCLK0 +GPIO_BASE $1000 + const UART0_BASE +UART0_BASE const UART0_DR +UART0_BASE $18 + const UART0_FR +UART0_BASE $24 + const UART0_IBRD +UART0_BASE $28 + const UART0_FBRD +UART0_BASE $2c + const UART0_LCRH +UART0_BASE $30 + const UART0_CR +UART0_BASE $38 + const UART0_IMSC +UART0_BASE $44 + const UART0_ICR + +: delay ( n -- ) begin 1- ?dup not until ; +: uartinit + 0 UART0_CR ! \ Disable UART0 + 0 GPPUD ! 100 delay \ Disable pull up/down for all GPIO pins + $c000 GPPUDCLK0 ! 100 delay \ Disable pull up/down for pins 14,15 + \ Write 0 to GPPUD and GPPUDCLK0 to make it take effect. + 0 GPPUD ! 0 GPPUDCLK0 ! + $7ff UART0_ICR ! \ Clear pending interrupts. + \ Set integer & fractional part of baud rate. + \ UART_CLOCK on rpi1 is 48Mhz + \ Divider = UART_CLOCK/(16 * Baud) + \ Fraction part register = 64th of the unit + \ Baud = 115200. + \ Divider = 48000000 / (16 * 115200) = 26.042 + 26 UART0_IBRD ! + 3 UART0_FBRD ! + $70 UART0_LCRH ! \ Enable FIFO & 8N1 + $7f2 UART0_IMSC ! \ Mask all interrupts + $301 UART0_CR ! ; \ Enable UART0, receive & transfer part of UART. + +uartinit prompt interactive!