define pointer to array from struct in c - arm

i have tried to access array of registers in stm32f103 MCU by struct method like this:
typedef struct
{
volatile u32 MNVIC_IABR[3];
}MNVIC_IABR_T;
#define MNVIC_IABR ((volatile MNVIC_IABR_T*) 0xE000E300)
but it gives me error :
In file included from ../src/MNVIC_program.c:8:0:
../include/MNVIC_PRIVATE.h:58:21: error: expected identifier before '(' token
#define MNVIC_IABR ((volatile MNVIC_IABR_T*) 0xE000E300)
^
i have discovered the problem , it was name of pointer which is the same name of register in last line, so i make change and it is working now :
#define MNVIC_IABR_STRUCT ((volatile MNVIC_IABR_T*) 0xE000E300)

Related

Is there a way to define macro for pins in AVR gcc so i can access those as variables?

I'm trying to write definitions for AVR C code so that i can access pins by simmple macro like
STATUS_LED_OUT =1;
in GENET_HW_DEF.h file, included to main C file. You can reproduce this bug by including this file into any C project.
I'm using avr studio 6.2 and 7 - both give the same result. I cannot compile getting werid macro unfold message like below. (CPU ATMega1284p)
D:\_SVN\Compass\AVR\Compass_IO_proto\Compass IO_proto\GENET_HW_DEF.h(19,49): error: expected ')' before '&' token
#define REGISTER_BIT(rg,bt) ((volatile _io_reg*)&rg)->bit##bt
^
D:\_SVN\Compass\AVR\Compass_IO_proto\Compass IO_proto\GENET_HW_DEF.h(42,25): info: in expansion of macro 'REGISTER_BIT'
#define STATUS_LED_OUT REGISTER_BIT(PORTB,7)
^
D:\_SVN\Compass\AVR\Compass_IO_proto\Compass IO_proto\GENET_HW_DEF.h(46,2): info: in expansion of macro 'STATUS_LED_OUT'
STATUS_LED_OUT=1;
^
Interesting enough, copied to fresh project with just only one or two declarations compiles fine, until one makes any changes in the declarations - like adding another macro reference. Then it becomes stuck again.
Also - if i comment all macro usages like
STATUS_LED_DIR=1;
STATUS_LED_OUT=1;
then I'm able to compile project and then after uncommenting usage lines it still compiles fine.. untill clean is executed. I'm probably messing with macro quirks but I have no idea where.
typedef struct
{
unsigned int bit0:1;
unsigned int bit1:1;
unsigned int bit2:1;
unsigned int bit3:1;
unsigned int bit4:1;
unsigned int bit5:1;
unsigned int bit6:1;
unsigned int bit7:1;
} _io_reg;
#define REGISTER_BIT(rg,bt) ((volatile _io_reg*)&rg)->bit##bt
#ifndef GENET_HW_DEF
#define GENET_HW_DEF
// define functionalities and flags - comment/uncomment apropriate lines
#define GENET_USART_0 256
#define GENET_USART_1 256
#define F_CPU 20000000UL
#define STATUS_LED 7
#define ERROR_LED 4
#define ADC_GLOBAL_ENABLE A
#define ADC_CHANNEL_0 0
#define ADC_CHANNEL_1 4
#define ADC_CHANNEL_2 2
#define ADC_CHANNEL_3 3
#define ADC_CHANNEL_4 1
#define ADC_CHANNEL_5 5
#define ADC_CHANNEL_6 6
#define ADC_CHANNEL_7 7
// actual definitions and initialization
#ifdef STATUS_LED
#define STATUS_LED_OUT REGISTER_BIT(PORTB,STATUS_LED)
#define STATUS_LED_DIR REGISTER_BIT(DDRB,STATUS_LED)
#define STATUS_LED_PIN REGISTER_BIT(PINB,STATUS_LED)
STATUS_LED_DIR=1;
STATUS_LED_OUT=1;
#endif
#ifdef ERROR_LED
#define ERROR_LED_OUT REGISTER_BIT(PORTC,ERROR_LED)
#define ERROR_LED_DIR REGISTER_BIT(DDRC,ERROR_LED)
ERROR_LED_DIR=1;
ERROR_LED_OUT=1;
#endif
#ifdef GENET_USART_0
#define USART0_ENABLED
#define UART_RX0_BUFFER_SIZE GENET_USART_0
#define UART_TX0_BUFFER_SIZE GENET_USART_0
#endif
#ifdef GENET_USART_1
#define USART1_ENABLED
#define UART_RX1_BUFFER_SIZE GENET_USART_1
#define UART_TX1_BUFFER_SIZE GENET_USART_1
#endif
#endif
I reproduced your problem.
You cannot call STATUS_LED_DIR=1; outside code execution flow. This must be inside a function (for example main()).
Now you will end with other compilation errors but this was the main mistake.
Second correction, you need 2 level for concatenation
#define CONCAT(bt) bit##bt
#define REGISTER_BIT(rg,bt) ((volatile _io_reg*)&rg)->CONCAT(bt)

undeclared here (not in a function) in C

im new with c language, but i try to unerstand this quark hashing algorithm that was written in c language, and i found an error while im compiling the source code, from what i understand the WIDTH it already declared, but why is it still error ?
this is the source code
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
/* uncomment to printf execution traces */
// #define DEBUG
#if defined(UQUARK)
#define CAPACITY 16
#define RATE 1
#define WIDTH 17
#elif defined(DQUARK)
#define CAPACITY 20
#define RATE 2
#define WIDTH 22
#endif
#define DIGEST WIDTH
typedef uint64_t u64;
typedef uint32_t u32;
typedef uint8_t u8;
typedef struct {
int pos; /* number of bytes read into x from current block */
// u32 x[ WIDTH*8 ]; /* one bit stored in each word */
u32 x[ WIDTH*8 ]; /* one bit stored in each word */
} hashState;
#if defined(UQUARK)
/* 17 bytes */
u8 iv[] = {0xd8,0xda,0xca,0x44,0x41,0x4a,0x09,0x97,
0x19,0xc8,0x0a,0xa3,0xaf,0x06,0x56,0x44,0xdb};
and its showing this error
quark.c:36:10: error : 'WIDTH' undeclared here (not in a function)
u32 x[WIDTH*8];
I guess for some reason neither UQUARK nor DQUARK are defined.
Add this:
#if defined(UQUARK) && defined(DQUARK)
#error both UQUARK and DQUARK are defined
#endif
#if !defined(UQUARK) && !defined(dQUARK)
#error Neither UQUARK nor DQUARK are defined
#endif
just before following line:
#if defined(UQUARK)
Then the compilation will abort if either both UQUARK and DQUARK are defined (which probably makes no sense) or if neither UQUARK nor DQUARK are defined (which probably happens in your case).
Now the question is: who defines UQUARK and/or DQUARK? Only you can tell.

Setting up MCU headers

I've been having some trouble setting up my libraries.
I'm looking forward to get a macro like PINSEL(P,N) with P as the number of the GPIO port and also N as the number of the pin in the port.
#ifndef uint32_t
typedef unsigned int uint32_t;
#endif
#ifndef __RW
#define __RW volatile
#endif
#ifndef __R
#define __R volatile const
#endif
typedef union{
struct {
uint32_t PINSELP4:2;
} PINSELP4[16];
struct{
__RW uint32_t PINSEL9_;
};
struct{
__RW uint32_t RES1_:24;
__RW uint32_t PINSELP4_28_:2;
__RW uint32_t PINSELP4_29_:2;
__RW uint32_t RES2_:4;
};
}PINSELP4_t;
#define PINSELP4_ ((PINSELP4_t *)(0x4002C024))
#define PINSELP4(N) PINSELP4_->PINSELP4_[N]->PINSELP4
#define PINSEL9 PINSELP4_->PINSEL9_
#define PINSELP4_28 PINSELP4_->PINSELP4_28_
#define PINSELP4_29 PINSELP4_->PINSELP4_29_
The example above shows the shorter struct of the 5 I've defined.
so then I've defined this two macros
#define PINSEL__(N)[] {PINSELP0(N),PINSELP1(N),PINSELP2(N),PINSELP3(N),PINSELP4(N)}
#define PINSEL(P,N) PINSEL__(N)[P]
I'm trying to make an "array of macros" for this to work. I'ts the only way I've figured it out. It still doesn't work. When I call the macro, I get
../src/../Includes/PINSEL_LPC1769.h:246:20: error: expected expression before '[' token
#define PINSEL__(N)[]={PINSELP0(N),PINSELP1(N),PINSELP2(N),PINSELP3(N),PINSELP4(N)}
^
as error.
I'm trying to make a really complete down layer so I can get a much clearer app layer. So if there's another way of doing this, I'll be listening.
Thank you all!

Why does the variable in header file give an error?

My Header file test.h contains two variables that are deined as:
#ifndef TEST_H_
#define TEST_H_
#define APPS 6;
#define NODES 1;
#endif;
I use it another header file called test2.h as follows:
#include"test.h"
typedef struct {
uint8 State[APPS];
} AppState;
But I get an error in the test.h file saying
expected ']' before ';' token
Since there are no brackets I have no idea why I get this strange error. Could somebody please point out my mistake.Thank you
Do not define constants with ; at the end:
#define APPS 6
#define NODES 1
Otherwise this line:
uint8 State[APPS];
Become this:
uint8 State[6;];
With is obviously not compilying. You may think about #define as a "search and replace".

Invalid Declaration Syntax in a struct

I'm working in C and getting the following error:
*** ERROR C074 IN LINE 78 OF ..\..\..\libraries\mchpstack_base\arp.c : Invalid declaration syntax
The relevant portion of code is:
typedef struct _ARP_PACKET
{
WORD HardwareType;
WORD Protocol;
BYTE MACAddrLen;
BYTE ProtocolLen;
WORD Operation;
MAC_ADDR2 SenderMACAddr;
IP_ADDR SenderIPAddr;
MAC_ADDR2 TargetMACAddr; // This is line 78
IP_ADDR TargetIPAddr;
} ARP_PACKET;
Now MAC_ADDR2 is define elsewhere in a header file which I am sure is included in arp.c:
typedef struct _MAC_ADDR2
{
BYTE v[6];
} MAC_ADDR2;
I'm new to C; but read over guides on creating structs, the typedef usage, and the namespace issue within C. I'm quite stuck. What is that error telling me?

Resources