Skip to content
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

Merged
merged 9 commits into from
Sep 11, 2017
Merged

Bit band calculation for cortex M3/4 #29

merged 9 commits into from
Sep 11, 2017

Conversation

ivand58
Copy link
Contributor

@ivand58 ivand58 commented Sep 5, 2017

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.

Copy link
Owner

@MitchBradley MitchBradley left a 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

+ +
BITBAND.OFFSET + \ add the bit_word_offset - the position of the target bit in the bit-band memory region.
;

Copy link
Owner

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!

Copy link
Owner

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
;

Copy link
Contributor Author

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.

@quozl
Copy link
Collaborator

quozl commented Sep 6, 2017

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. 😁

@MitchBradley MitchBradley merged commit 5a93c30 into MitchBradley:master Sep 11, 2017
@ivand58 ivand58 deleted the bit-band branch September 12, 2017 06:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants