Do Not Understand This Programming Style!

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Greetings -

 

I was just reviewing Microchip's twi library originally written, I think (but possibly not), for Arduino. twi.h contains several struct definitions like this:

 

/* Slave Transaction result enumeration */
typedef enum TWIS_RESULT_enum {
	TWIS_RESULT_UNKNOWN            = (0x00<<0),
	TWIS_RESULT_OK                 = (0x01<<0),
	TWIS_RESULT_BUFFER_OVERFLOW    = (0x02<<0),
	TWIS_RESULT_TRANSMIT_COLLISION = (0x03<<0),
	TWIS_RESULT_BUS_ERROR          = (0x04<<0),
	TWIS_RESULT_FAIL               = (0x05<<0),
	TWIS_RESULT_ABORTED            = (0x06<<0),
} TWIS_RESULT_t;

 What is the purpose of (0x03<<0) and (0x04<<0)? Are these not identical to (0x03), (0x04), etc? Why add the nil-shift operations? 

 

Thanks

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am gonna guess....

 

Since the note at teh top mentions 'enumeration', I am guessing that what you have there is another way of converting 'magic numbers' to human readable results.

 

For example, if after a TWI transaction the status register has the value of 0x03 in it, then you could write:

 

if(STATUS == TWIS_RESULT_TRANSMIT_CONDITION)
{
    //DO THIS!!
}

So, it looks to me to be a rather overly exotic version of a simple ENUM structure.

 

Just a guess....I am probably wrong

 

Right Side Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why add the nil-shift operations? 

Probably just blindly following some template (maybe auto-generated).  These almost certainly are bit positions (rather than bit masks, since they have more then one bit set(ex: 6))  

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd agree - it looks like someone has aimed for consistency in the source code.

 

I'm less consistent:

 

// source registers - bits 0-3
#define S_B 	0
#define S_C 	1
#define S_D 	2
<...>

// destination registers - bits 4-7
#define D_B 	(0 << 4)
#define D_C 	(1 << 4)
#define D_D 	(2 << 4)
<...>

// address source - bits 8-9
#define	HL		(0 << 8)
#define PC		(1 << 8)
<...>

// arithmetic operations - bits 10-13
#define ADDOP	(0 << 10)
#define ADCOP	(1 << 10)
<...>

though I think in context it's clear enough: using the (x << 0) makes it explicit and perhaps offers an option for later automation if the context changes.

 

Neil

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I was wondering if it was to befuddle humans while satisfying the cravings of our machine-masters! I guess that we are being assimilated!

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your example in #1 shows a trivial enum.    It does not need any initialisers because enums default to 0, 1, 2, 3, ...

However,  look at this one

/* Initial Delay Selection select */
typedef enum ADC_INITDLY_enum
{
    ADC_INITDLY_DLY0_gc = (0x00<<5),  /* Delay 0 CLK_ADC cycles */
    ADC_INITDLY_DLY16_gc = (0x01<<5),  /* Delay 16 CLK_ADC cycles */
    ADC_INITDLY_DLY32_gc = (0x02<<5),  /* Delay 32 CLK_ADC cycles */
    ADC_INITDLY_DLY64_gc = (0x03<<5),  /* Delay 64 CLK_ADC cycles */
    ADC_INITDLY_DLY128_gc = (0x04<<5),  /* Delay 128 CLK_ADC cycles */
    ADC_INITDLY_DLY256_gc = (0x05<<5),  /* Delay 256 CLK_ADC cycles */
} ADC_INITDLY_t;

 

It is initialising to 0, 0x20, 0x40, 0x60, 0x80, 0xA0

 

The Compiler is much more strict about enum values that simple integers.    So if you tried (0x06<<5) it would be an illegal enum.

 

Yes,  I agree that the traditional Mega header files look simpler.   You just need to know that this particular bitfield starts at 5 without knowing how to spell the fancy macro.

 

VassistX will help you with the spelling.   And reading the the resultant code is much more informative.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
Are these not identical to (0x03), (0x04), etc?

Yes, they are.

 

ka7ehk wrote:
Why add the nil-shift operations? 

as the others, I would guess that it's just "for consistency"

 

It may well be auto-generated.

 

So  a bit like messages which says "there were 1 error(s)":  they could have put in the effort to make a special case for "there was 1 error" - but they just haven't bothered.

(in fact, VAX-VMS had "lexical functions" to handle exactly this)

 

At least it will all be done at compile-time - so there is no actual overhead introduced.

 

EDIT

 

Seems that Cliff was writing essentially the same thing at the same time.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Nov 24, 2021 - 09:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I would suggest this code was written by a machine. Presumably the ADTFs are fed in and the .h's pop out? I guess the person who wrote the Python (I'm guessing!) did not consider the case for shift_value==0 and realise the auto-generator did not need to generate "<<shift_value" in that case?

 

OTOH it seems pretty harmless.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
You just need to know that this particular bitfield starts at 5

but isn't there a symbolic definition for that - rather than using Magic Number ... ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
david.prentice wrote:
You just need to know that this particular bitfield starts at 5

but isn't there a symbolic definition for that - rather than using Magic Number ... ?

Maybe one is not supposed to need to know that the bitfield starts at 5.

Perhaps one just needs to know that it does not overlap with some other bitfields.

In any case, all uses of the magic number, 5, are within eyeball distance from each other.

Moderation in all things. -- ancient proverb