-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bit band calculation for cortex M3/4 #29
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bitband.fth is specific to a particular processor family so it should not be in the generic src/cforth/lib/directory. A better place would be src/cpu/arm/cortex-m3
src/lib/bitband.fth
Outdated
+ + | ||
BITBAND.OFFSET + \ add the bit_word_offset - the position of the target bit in the bit-band memory region. | ||
; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
: 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
;
The above is smaller, faster and more direct. There is no value in declaring the masks as constants because they are fixed for this architecture. The value of having symbolic names is nullified by the confusion caused by the similarity of the names that differ only in a trailing '_', which has no mnemonic value.
But, all that said, if one is to use the bitbanding feature, why not just define the bit object address within the bitband region directly, avoiding the address,bit# representation entirely?
$4222.0234 constant mybit
1 mybit l!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixing the formatting:
: 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
;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about the following usecase:
VARIABLE x
\ ...
1 x BITBAND
1 SWAP C!
In this case it is difficult to type the address of the bit that corresponds to the address of the variable.
For interest, my Teensy 3.x GPIO file at https://github.com/MitchBradley/cforth/blob/master/src/platform/arm-teensy3/gpio.fth is a bit band implementation but without mentioning bit band. 😁 |
The benefit of Bit-banding is that a write to a word in the alias region performs a write to the corresponding bit in the Bit-band region. Also, reading a word in the alias region will return the value of the corresponding bit in the Bit-band region. These operations take a single machine instruction thus eliminate race conditions. This is especially useful for interacting with peripheral registers where it is often necessary to set and clear individual bits.