AVR GCC Error initializer element is not constant - gcc

I am using Atmel Studio 6.2, the chip xmega64D3, and I have a problem with intitializing some fields of a structure:
typedef struct
{
uint32_t val;
uint32_t inv;
uint32_t xor;
}SPROTU32;
typedef struct
{
SPROTU32 SerialNr;
SPROTU32 SwVersion;
SPROTU32 HwVersion;
SPROTU32 ProdCode;
SPROTU32 ProdDate;
SPROTU32 PartNum;
}BasicData;
#define sw_high 02
#define sw_low 12
#define sw_ver ((sw_high << 16) + sw_low)
#define hw_high 01
#define hw_low 06
#define hw_ver ((hw_high << 16) + hw_low)
#define DEFAULT_SERIAL 0
#define DEFAULT_SW_VERSION sw_ver
#define DEFAULT_HW_VERSION hw_ver
#define DEFAULT_PROD_CODE 0
#define DEFAULT_PROD_DATE 0
#define DEFAULT_PART_NUM 05545410
EEMEM BasicData eeBasicData =
{
{DEFAULT_SERIAL,~DEFAULT_SERIAL,DEFAULT_SERIAL^0x55555555},
{DEFAULT_SW_VERSION,~DEFAULT_SW_VERSION,DEFAULT_SW_VERSION^0x55555555},
{DEFAULT_HW_VERSION,~DEFAULT_HW_VERSION,DEFAULT_HW_VERSION^0x55555555},
{DEFAULT_PROD_CODE,~DEFAULT_PROD_CODE,DEFAULT_PROD_CODE^0x55555555},
{DEFAULT_PROD_DATE,~DEFAULT_PROD_DATE,DEFAULT_PROD_DATE^0x55555555},
{DEFAULT_PART_NUM,~DEFAULT_PART_NUM,DEFAULT_PART_NUM^0x55555555}
};
Compile output image
The compiler says always initilizer is not constant. So, how can I do to initialize my structure with what I want?

try to use multiply operation instead << operation
#define _VERSION(h,l) ((h * 65536) | l)
#define DEFAULT_SW_VERSION _VERSION(sw_high, sw_low)

Related

C Macro is not expanded within Macro

I am developing a application which interact with hardware with ioctls. I wrote some lower level api for performing device operations. I wrote some macros as wrappers. as following.
WRITE_REGISTER(99, REGISTER_ADDRESS( 10, SCKT0_REG) );
with gcc -E,
I noticed inner macro is not getting expanded. Could you please help me to resolve the issue?
Compiling source code snippet is given bellow.
#include <stdio.h>
#include <string.h>
#define MAX_REG_NAME_LENGTH 32
#define MODULE_SEQUENCER 20
#define SCKT0_REG 11
struct register_struct
{
unsigned int reg_addr;
unsigned int reg_value;
char reg_name [MAX_REG_NAME_LENGTH];
char module_name[MAX_REG_NAME_LENGTH] ;
} register_st;
#define GET_REGISTER_ADDRESS(module_index, register_offset) \
(module_index * (1 << BITS_PER_MODULE) + register_offset) \
#define REGISTER_ADDRESS_(module_index, register_offset) \
{ \
memset(&register_st, 0, sizeof(struct register_struct)); \
register_st.reg_addr = GET_REGISTER_ADDRESS(module_index, register_offset); \
memcpy(register_st.reg_name, STR(register_offset), sizeof(STR(register_offset))); \
memcpy(register_st.module_name, STR(MODULE_SEQUENCER), sizeof(STR(MODULE_SEQUENCER))); \
} \
#define REGISTER_ADDRESS(x, y) REGISTER_ADDRESS_(x, y)
#define WRITE_REGISTER(register_value, register_offset) \
({ \
register_st.reg_value = register_value; \
}) \
int main()
{
unsigned int uiValue = 0;
WRITE_REGISTER(99, REGISTER_ADDRESS( 10, SCKT0_REG) );
return 0;
}

how does subtracting works in macro definition

why does C = 2 shouldn't it be 0 A+1 = 1 and 1-B =0 how does it work
#include <iostream>
#include <string>
using namespace std;
#define A 0
#define B A+1
#define C 1-B
int main()
{
cout << C ;
}
Thanks to Lakshay Garg i understand what he means it just replaces the Macro with whatever i define so in the case of "#define C 1-B" the B will be replaced with A+1 which is 0+1
So in in my cout C= 1-0+1 Which is 2 again thanks for the help

Masks (macros of bits)

So I'm about to finish my course in university in C programming.
I want to get better at bit operations (such as creating masks) so I'll go to it:
#define BIT_I_SET(TYPE,I) ((TYPE)(1) << (I))
#define SET_BIT(NUM,I,TYPE) \
NUM |= BIT_I_SET(I,TYPE)
I am still at the early stages and learning the syntax at the moment, I have no idea why the compiler says there's error:
Severity Code Description Project File Line Suppression State
Error (active) E0109 expression preceding parentheses of apparent call must have (pointer-to-) function type Project14
full program (yeah it's for synatx only):
#include <stdio.h>
#include <stdlib.h>
#define SHIFT(I,TYPE) ((TYPE)(1) << (I))
#define NEGATIVE(TYPE) (~(TYPE)(0))
#define BIT_I_SET(TYPE,I) ((TYPE)(1) << (I))
#define BIT_I_CLEAR(I,TYPE) (~((TYPE)(1)<< (I)))
#define MSB_SET(TYPE) ((TYPE)(1) << (sizeof(TYPE)*8-1)
#define SET_BIT(NUM,I,TYPE) \
NUM |= BIT_I_SET(I,TYPE)
void main()
{
unsigned char i, j;
int shift = 3;
i = 0;
j = 0;
SET_BIT(j, 2, unsigned char);
printf("%d\n",sizeof(j));
printf("%d",i);
system("pause>null");
}
change
NUM |= BIT_I_SET(I,TYPE)
to
NUM |= BIT_I_SET(TYPE, I)
You can run just the preprocessor stage of your compiler, which expand the macros
using the command:
gcc -E file.c

usb_libusb10.h(72): error C2146: syntax error : missing ';' before identifier 'cb'

I am getting the following error even though it seems there are no syntax errors.
usb_libusb10.h(72): error C2146: syntax error : missing ';' before identifier 'cb'
The line where the error is generating is given below-
fnusb_iso_cb cb;
I also tried to highlighted this line by using "**" in the code.
Following is the code I am using.
#pragma once
#include <Windows.h>
#include <afxwin.h>
#include "libfreenect.h"
#include <libusb.h>
// There are a few rules: PKTS_PER_XFER * NUM_XFERS <= 1000, PKTS_PER_XFER % 8 == 0.
#if defined(__APPLE__)
#define DEPTH_PKTBUF 2048
#define VIDEO_PKTBUF 2048
#define PKTS_PER_XFER 128
#define NUM_XFERS 4
#else
#define DEPTH_PKTBUF 1920
#define VIDEO_PKTBUF 1920
#if defined(_WIN32)
#define PKTS_PER_XFER 32
#define NUM_XFERS 8
#else
#define PKTS_PER_XFER 16
#define NUM_XFERS 16
#endif
#endif
typedef struct {
libusb_context *ctx;
int should_free_ctx;
} fnusb_ctx;
typedef struct {
freenect_device *parent; //so we can go up from the libusb userdata
libusb_device_handle *dev;
int device_dead; // set to 1 when the underlying libusb_device_handle vanishes (ie, Kinect was unplugged)
int VID;
int PID;
} fnusb_dev;
typedef struct {
fnusb_dev *parent; //so we can go up from the libusb userdata
struct libusb_transfer **xfers;
uint8_t *buffer;
**fnusb_iso_cb cb;**
int num_xfers;
int pkts;
int len;
int dead;
int dead_xfers;
} fnusb_isoc_stream;
int fnusb_num_devices(fnusb_ctx *ctx);
int fnusb_list_device_attributes(fnusb_ctx *ctx, struct freenect_device_attributes** attribute_list);
int fnusb_init(fnusb_ctx *ctx, freenect_usb_context *usb_ctx);
int fnusb_shutdown(fnusb_ctx *ctx);
int fnusb_process_events(fnusb_ctx *ctx);
int fnusb_process_events_timeout(fnusb_ctx *ctx, struct timeval* timeout);
int fnusb_open_subdevices(freenect_device *dev, int index);
int fnusb_close_subdevices(freenect_device *dev);
int fnusb_start_iso(fnusb_dev *dev, fnusb_isoc_stream *strm, fnusb_iso_cb cb, unsigned char endpoint, int xfers, int pkts, int len);
int fnusb_stop_iso(fnusb_dev *dev, fnusb_isoc_stream *strm);
int fnusb_get_max_iso_packet_size(fnusb_dev *dev, unsigned char endpoint, int default_size);
int fnusb_control(fnusb_dev *dev, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint8_t *data, uint16_t wLength);
int fnusb_bulk(fnusb_dev *dev, uint8_t endpoint, uint8_t *data, int len, int *transferred);
int fnusb_num_interfaces(fnusb_dev *dev);

Is there a memset-like function which can set integer value in visual studio?

1, It is a pity that memset(void* dst, int value, size_t size) fools a lot of people when they first use this function! 2nd parameter "int value" should be "uchar value" to describe the real operation inside.
Don't misunderstand me, I am asking a memset-like function!
2, I know there are some c++ candy function like std::fill_n(my_array, array_length, constant_value);
even a pure c function in OS X: memset_pattern4(grid, &pattern, sizeof grid);
mentioned in a perfect thread Why is memset() incorrectly initializing int?.
So, is there a similar c function in runtime library of visual studio like memset_pattern4()?
3, for somebody asked why i wouldn't use a for-loop to set integer by integer. here is my answer: memset turns to a better performance when setting big trunk(10K?) of memory at least in x86.
http://www.gamedev.net/topic/472631-performance-of-memset/page-2 gives more discussion, although without a conclusion(I doubt there will be).
4, said function can be used to simplify counting sort by avoiding useless Fibonacci accumulation.
Original:
for (int i = 0; i < SRC_ARRY_SIZE; i++)
counter_arry[src_arry[i]]++;
for (int i = SRC_LOW_BOUND; i < SRC_HI_BOUND; i++)//forward fabnacci??
counter_arry[i+1] += counter_arry[i];
for (int i = 0; i < SRC_ARRY_SIZE; i++)
{
value = src_arry[i];
map = --counter_arry[value];//then counter down!
temp[map] = value;
}
Expected:
for (int i = 0; i < SRC_ARRY_SIZE; i++)
counter_arry[src_arry[i]]++;
for (int i = SRC_LOW_BOUND; i < SRC_HI_BOUND+1; i++)//forward fabnacci??
{
memset_4(cur_ptr,i, counter_arry[i]);
cur_ptr += counter_arry[i];
}
Thanks for your kindly review and reply!
Here's an implementation of memset_pattern4() that you might find useful. It's nothing like Darwin's SSE assembly language version, except that it has the same interface.
#include <string.h>
#include <stdint.h>
/*
A portable implementation of the Darwin memset_patternX() family of functions:
These are analogous to memset(), except that they fill memory with a replicated
pattern either 4, 8, or 16 bytes long. b points to a buffer of size len bytes
which is to be filled. The second parameter points to the pattern. If the
buffer length is not an even multiple of the pattern length, the last instance
of the pattern will be truncated. Neither the buffer nor the pattern pointer
need be aligned.
*/
/*
alignment utility macros stolen from Linux
see https://lkml.org/lkml/2006/11/25/2 for a discussion of why typeof() is used
*/
#if !_MSC_VER
#define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
#define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
#define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
#define __ALIGN_MASK(x, mask) __ALIGN_KERNEL_MASK((x), (mask))
#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((uintptr_t)(p), (a)))
#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
#define IS_PTR_ALIGNED(p, a) (IS_ALIGNED((uintptr_t)(p), (a)))
#else
/* MS friendly versions */
/* taken from the DDK's fltKernel.h header */
#define IS_ALIGNED(_pointer, _alignment) \
((((uintptr_t) (_pointer)) & ((_alignment) - 1)) == 0)
#define ROUND_TO_SIZE(_length, _alignment) \
((((uintptr_t)(_length)) + ((_alignment)-1)) & ~(uintptr_t) ((_alignment) - 1))
#define __ALIGN_KERNEL(x, a) ROUND_TO_SIZE( (x), (a))
#define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
#define PTR_ALIGN(p, a) ALIGN((p), (a))
#define IS_PTR_ALIGNED(p, a) (IS_ALIGNED((uintptr_t)(p), (a)))
#endif
void nx_memset_pattern4(void *b, const void *pattern4, size_t len)
{
enum { pattern_len = 4 };
unsigned char* dst = (unsigned char*) b;
unsigned const char* src = (unsigned const char*) pattern4;
if (IS_PTR_ALIGNED( dst, pattern_len) && IS_PTR_ALIGNED( src, pattern_len)) {
/* handle aligned moves */
uint32_t val = *((uint32_t*)src);
uint32_t* word_dst = (uint32_t*) dst;
size_t word_count = len / pattern_len;
dst += word_count * pattern_len;
len -= word_count * pattern_len;
for (; word_count != 0; --word_count) {
*word_dst++ = val;
}
}
else {
while (pattern_len <= len) {
memcpy(dst, src, pattern_len);
dst += pattern_len;
len -= pattern_len;
}
}
memcpy( dst, src, len);
}

Resources