How can I make a uint96_t ?? (XMEGA 128A3U)

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

For a data logger project, I'm using a 12-bit 8-channel ADC.
Now, it just sooo happens that (including some other stuff that needs to be collected) I can fit 16 data collections of 32 bytes into a single 512-byte page for storing in the data logger's EPROM. But, only if I can somehow compress the 8*12 bit channels into 12 bytes. That'd be elegant, and give me 4 months storage on the 4MB chip I'm using.

I found a way by googling how to reduce a uint128_t, using a union - but GCC aint got those either! (Plus, I suspect it was c++, which I'm clueless about - it's in the code section attached)

typedef union { // you can use union or struct here
    __uint128_t i : 96;
} __attribute__((packed)) uint96_t;

uint96_t var;  // new uint96_t variable
var.i = 123;   // set the value to 123 (for example)
// This worked for me with gcc. If you test the size of uint96_t with sizeof you should get 12 bytes (12 * 8 = 96).

I  'grepped' through the include files to see if I could even find out how uint64_t was 'made' but came up dry.

Guess I could 'live' with shifting the first 4 channels into a uint64_t, and write the lower 6 bytes, then do the same with the next 4 channels...but if it looks like a warthog, it's a warthog. Which it would.

 

Ideas welcome, folks! Even a clue as to where I should be looking. Source of GCC? Dunno where even that is, and it'd probably take a fortnight to find, and figure it out.

 

Thanks

-Andy

 

This topic has a solution.

Last Edited: Tue. Apr 26, 2016 - 09:34 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can't just invent data types like this in 'C' and have them work with the standard operators like '='.

 

You would have to write your own function to do the assignment - but that's really just a matter of copying bytes into the right places and right order ... 

 

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...
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bitfields.

 

#include <stdio.h>

// Define a type consisting of 8 12-bit unsigned values
typedef struct {
    unsigned int c0 : 12;
    unsigned int c1 : 12;
    unsigned int c2 : 12;
    unsigned int c3 : 12;
    unsigned int c4 : 12;
    unsigned int c5 : 12;
    unsigned int c6 : 12;
    unsigned int c7 : 12;
} foo_t;

// Declare a group of 8 samples
foo_t eight_samples;

The struct eight_samples will be 12 bytes long.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

Thanks, I'll give that a shot!
Never come across in AVR C the use of a colon like this...Time to study... UPDATE - FOUND at http://www.tutorialspoint.com/cp...

unsigned int c0 : 12;

 

 

Thanks, joeymorin. Exactly what I'm looking for !

 

ASIDE: Did find GCC-avr release 4 has uint128_t, so I'm also searching for it. But us mere mortals using Studio 6.2* are stuck with version /3.4.1061...(AVR-32's got v4, but AVR-8 has v3.

If the GODS who could, in their Infinite Bearedness give us mere muggles uint128_t, then, if I am a disciple long enough I may Receive Great Wisdom and learn how to do it...

Thanks

-Andy

* stuck with 6,2 becuse - even with Atmel's best efforts, I can't get Studio 7 to simulate. Bah!

 

 

Last Edited: Tue. Apr 26, 2016 - 09:19 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

andycrofts wrote:
Never come across in AVR C the use of a colon like this

Bitfields are (and, I think, always have been) a standard part of the 'C' programming language - not specific to AVR or GCC.

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:

Bitfields are (and, I think, always have been) a standard part of the 'C' programming language - not specific to AVR or GCC.

Yes and indeed you can find it even in the "Get Started" books.

 

I've read about 4-5 books about C and have seen "BitFields" in all of them.

"One's value is inherent; money is not inherent"

 

Chuck, you are in my heart!

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

No current release of AVR GCC includes support for __int128 or __uint128.  The widest types are int16_t int64_t and uint16_t uint64_t.

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Tue. Apr 26, 2016 - 11:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah, must've missed bitfields in my reading somehow - or never connected them with this.

 The widest types are int16_t and uint16_t.

No, Atmel Studio's version of gcc  permits uint32_t and uint64_t, which is what I was hoping for as a progression. They're in stdint-gcc, which is why this bit of crap compiles...
 

#include <avr/io.h>
uint64_t mylonglong = 4095;
uint64_t xx;
int main(void)
{
int i;

for (i=0; i < 4; i++){

	mylonglong |= (mylonglong << 12);
}

 

 

Last Edited: Tue. Apr 26, 2016 - 01:02 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wow that was a serious pre-coffee typo! ;-) Meant to say int64_t and uint64_t. I'll fix my post later when I'm not on my mobile...

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

Pobody's Nerfect! wink

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

andycrofts wrote:

Pobody's Nerfect! wink

Cool Finnishlaugh

"One's value is inherent; money is not inherent"

 

Chuck, you are in my heart!

Last Edited: Tue. Apr 26, 2016 - 02:05 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

andycrofts wrote:
Pobody's Nerfect! wink 

 

https://www.youtube.com/watch?v=...

 

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: 1

Ta Muchly!

OK, (well - off - topic) but I'll share this:
 

Attachment(s): 

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

Ahhhh! The handle stops them from falling in and makes it easy to retrieve.

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

Late to this party but:

typedef union { // you can use union or struct here
    __uint128_t i : 96;
} __attribute__((packed)) uint96_t;

As soon as I read that comment alarm bells sounded. I wonder why the author would every choose "union" here? Did she have the first idea what she was doing? Sounds a bit like "suck it and see" programming.