aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMitch Bradley <wmb@firmworks.com>2017-09-11 08:41:14 -1000
committerGitHub <noreply@github.com>2017-09-11 08:41:14 -1000
commit5a93c306da5815b8135521e1325903ef5a1113d1 (patch)
tree598aa4bd4a53084b5eac3112addb332a303c7349
parent6f8f55cbe7ff91b480fecd45a0d146c8a2652612 (diff)
parentacbe7ebe4ca4eb475607ce096adfc16d4686dc6e (diff)
downloadcforth-5a93c306da5815b8135521e1325903ef5a1113d1.tar.gz
Merge pull request #29 from ivand58/bit-band
Bit band calculation for cortex M3/4
-rw-r--r--build/arm-stm32f103/Makefile1
-rw-r--r--src/app/arm-stm32f103/app.fth2
-rw-r--r--src/app/arm-stm32f103/example.fth58
-rw-r--r--src/cpu/arm/cortex-m3/bitband.fth21
4 files changed, 81 insertions, 1 deletions
diff --git a/build/arm-stm32f103/Makefile b/build/arm-stm32f103/Makefile
index f6b026b..3cce6cd 100644
--- a/build/arm-stm32f103/Makefile
+++ b/build/arm-stm32f103/Makefile
@@ -22,6 +22,7 @@ include $(TOPDIR)/src/app/$(MYNAME)/targets.mk
ifeq ("$(wildcard $(STMLIB))","")
$(info You need the STM Standard Peripheral Library)
$(info Get it from http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32-standard-peripheral-libraries/stsw-stm32054.html)
+ $(info Get it from https://my.st.com/content/ccc/resource/technical/software/firmware/48/ab/e5/17/0d/79/43/74/stsw-stm32054.zip/files/stsw-stm32054.zip/_jcr_content/translations/en.stsw-stm32054.zip)
$(info Unpack it into the parent directory of the cforth/ tree)
$(error )
endif
diff --git a/src/app/arm-stm32f103/app.fth b/src/app/arm-stm32f103/app.fth
index 0052090..1dbf783 100644
--- a/src/app/arm-stm32f103/app.fth
+++ b/src/app/arm-stm32f103/app.fth
@@ -1,5 +1,5 @@
\ Load file for application-specific Forth extensions
-
+fl ../../cpu/arm/cortex-m3/bitband.fth
fl ../../lib/misc.fth
fl ../../lib/dl.fth
$3000 $5000 npatch load-base
diff --git a/src/app/arm-stm32f103/example.fth b/src/app/arm-stm32f103/example.fth
new file mode 100644
index 0000000..46a9dc7
--- /dev/null
+++ b/src/app/arm-stm32f103/example.fth
@@ -0,0 +1,58 @@
+\ Example event loop for periodic ADC sampling
+
+\ Exponential smoothing filter
+#10000 value filter-denom
+ #9000 value filter-alpha
+0 value smoothed-adc
+: filter ( new-value -- filtered value )
+ filter-denom filter-alpha - * ( new-value'*denom )
+ smoothed-adc filter-alpha * + ( smoothed-value*denom )
+ filter-denom / ( smoothed-value' ) \ Rounded division
+ dup to smoothed-adc ( filtered-value )
+;
+
+#300 value adc-ms \ The sampling period in milliseconds
+
+0 value next-adc-time
+
+: set-next-adc-time ( -- ) get-msecs adc-ms + to next-adc-time ;
+: adc-time? ( -- flag ) get-msecs next-adc-time - 0>= ;
+: check-adc ( -- )
+ adc-time? if
+ set-next-adc-time
+ adc@ dup ." Current: " .d ( val )
+ ." filtered " filter .d cr ( )
+ then
+;
+
+#800 value led-ms \ The sampling period in milliseconds
+
+0 value next-led-time
+0 value led-state
+
+: set-next-led-time ( -- ) get-msecs led-ms + to next-led-time ;
+: led-time? ( -- flag ) get-msecs next-led-time - 0>= ;
+
+: check-led ( -- )
+ led-time? if
+ set-next-led-time
+ led-state led-gpio gpio-pin!
+ led-state 0= to led-state
+ then
+;
+
+: test ( -- )
+ init-led
+ set-next-led-time
+
+ 0 init-adc
+ set-next-adc-time \ Set up for first periodic sample
+ adc@ drop \ Discard the first sample which is often suspect
+ adc@ to smoothed-adc \ Prime the smoothed value with the current value
+
+ begin
+ check-led \ Blink
+ check-adc \ Handle the ADC every so often
+ wfi \ The processor idles here until the next timer tick
+ key? until \ Exit when a key is pressed
+;
diff --git a/src/cpu/arm/cortex-m3/bitband.fth b/src/cpu/arm/cortex-m3/bitband.fth
new file mode 100644
index 0000000..2966409
--- /dev/null
+++ b/src/cpu/arm/cortex-m3/bitband.fth
@@ -0,0 +1,21 @@
+\ Bit-banding maps a complete word of memory onto a single bit in the bit-band
+\ region. For example, writing to one of the alias words sets or clears the
+\ corresponding bit in the bit-band region. This enables every individual bit in
+\ the bit-banding region to be directly accessible from a word-aligned address
+\ using a single LDR instruction. It also enables individual bits to be toggled
+\ without performing a read-modify-write sequence of instructions.
+
+\ Usage:
+\ variable x
+\ 0 x !
+\ x 0 BITBAND 1 swap c! x @ .
+
+: bitband ( bit adr -- aliasadr )
+ dup 5 lshift ( bit adr byte-offset ) \ Spread low part of address; high bits will be shifted out
+ swap $f000.0000 and or ( bit adr' ) \ Keep high nibble of address
+ $0200.0000 or ( bit adr' ) \ Offset to bitband alias space
+ swap la+ ( aliasadr ) \ Merge bit number as 32-bit word offset
+;
+
+." Test for SFR " 5 $4001140C BITBAND $42228194 = if ." Passed" else ." Failed" then CR
+." Test for RAM " #24 $200FFFFC BITBAND $23ffffe0 = if ." Passed" else ." Failed" then CR