From: Adam Shamblin Date: Tue, 31 May 2022 22:22:43 +0000 (-0600) Subject: Further work with timers and interrupts. X-Git-Url: https://git.vexinglabs.com/?a=commitdiff_plain;h=ba0a9db8e861965b242feec668885dd7894991f6;p=forthdeck.git Further work with timers and interrupts. --- diff --git a/gpio.fs b/gpio.fs index dbf58b4..65ab7b8 100644 --- a/gpio.fs +++ b/gpio.fs @@ -69,7 +69,8 @@ SIO_BASE $02c + constant GPIO_OE_XOR output? IF ." High" ELSE ." Low" THEN cr LOOP ; -\ TODO I haven't read much about reading inputs yet, this will require fleshing out. +\ TODO I haven't read much about reading inputs yet; +\ this will require fleshing out. : input@ ( n -- ? ) 1 swap lshift GPIO_IN @ ; : inputs. ( -- ) diff --git a/timer.fs b/timer.fs index f738c95..7355d39 100644 --- a/timer.fs +++ b/timer.fs @@ -1,16 +1,39 @@ \ Words for dealing with the system timer peripheral +$e0000000 constant PPB_BASE +PPB_BASE $e100 + constant NVIC_ISER \ Interrupt set-enable register +PPB_BASE $e180 + constant NVIC_ICER \ Interrupt clear-enable register + +: interrupt-enable ( n -- ) 1 swap lshift NVIC_ISER @ or NVIC_ISER ! ; +: interrupt-disable ( n -- ) 1 swap lshift NVIC_ICER @ and NVIC_ICER ! ; + +\ Check if an interrupt is enabled. +\ example: 12 interrupt-enabled? +: interrupt-enabled? ( n -- flag ) 1 swap lshift NVIC_ISER @ and 0 > ; + +\ Display a list of interrupts and their enabled status. +: interrupts-enabled. ( -- ) + cr 32 0 DO I dup ." IRQ-" u. + interrupt-enabled? IF ." 1" ELSE ." 0" THEN + cr LOOP ; + $40054000 constant TIMER_BASE -TIMER_BASE $00 + constant TIMEHW +TIMER_BASE $00 + constant TIMEHW \ High/Low Write TIMER_BASE $04 + constant TIMELW -TIMER_BASE $08 + constant TIMEHR +TIMER_BASE $08 + constant TIMEHR \ High/Low Read TIMER_BASE $0c + constant TIMELR +TIMER_BASE $10 + constant ALARM0 +TIMER_BASE $14 + constant ALARM1 +TIMER_BASE $18 + constant ALARM2 +TIMER_BASE $1c + constant ALARM3 +TIMER_BASE $24 + constant TIMERAWH \ Timer, raw read, no side effects (latch) +TIMER_BASE $28 + constant TIMERAWL -TIMER_BASE $38 + constant TIMER_INTE +TIMER_BASE $38 + constant TIMER_INTE \ Interrupt enable \ Display the current system timer. "counter print" -: counter. ( -- ) TIMELR @ TIMEHR @ swap D. ; +: counter. ( -- ) TIMELR @ TIMEHR @ swap UD. ; \ Enable or disable a specific alarm. \ example: 2 alarm-enable @@ -20,3 +43,9 @@ TIMER_BASE $38 + constant TIMER_INTE \ Check if an alarm is enabled. \ example: 2 alarm-enabled? : alarm-enabled? ( n -- flag ) 1 swap lshift TIMER_INTE @ and 0 > ; + +: ms ( n -- ) + 0 dup alarm-enable interrupt-enable + 1000 * \ convert milliseconds to microseconds + TIMERAWL + \ add raw time, lower 32 bits + ALARM0 ! \ write time to ALARM0