Dear everyone,
I want to add new struct type called I2C_TypeDef to the CMSDK_CM3.h I followed the format, added right below the other CMSDK_TypeDef, but strangely the Keil IDE cannot recognize my new type. Here is what I added
/*------------------- I2C ----------------------------------------------*/ /** @addtogroup I2C @{ */ typedef struct { __IO uint32_t PRER; // <h> I2C Prescaler Register (R/W) </h> __IO uint32_t CTR; // <h> I2C Control Register (R/W) </h> __O uint32_t TXR; // <h> I2C Transmit Register (W) </h> __I uint32_t RXR; // <h> I2C Receive Register (R) </h> union { __O uint32_t CR; // <h> I2C Command Register (W) </h> __I uint32_t SR; // <h> I2C Status Register (R) </h> }; } I2C_TypeDef; /*@}*/ /* end of group I2C */
it is compiled but whenever I try to use the I2C_TypeDef in main, for example, I2C_TypeDef mySensor;
It doesn't compile with an 'error: unknown type name 'I2C_TypeDef' I am sure that I have included the CMSDK_CM3.h since the other CMSDK_xxx_typedef work just fine. Also, when I declare the struct in the same file as the main. It recognizes the struct and now compiles. Please, anybody know what is the problem? I am using Keil MDK5 by the way. Thank you for any help.
The warning appears before the other error warning : #1215-D: #warning directive: "My I2C TypeDef Included" on every files that includes the CMSDK_CM3.h. This means that it has been included correctly, right? And in the CMSDK_CM3.h there is already piece of guard like
#ifndef CMSDK_CM3_H #define CMSDK_CM3_H // all the rest of the library #endif
Isn't that should be enough to protect from recursive includes?
Also, naming the union doesn't help
It seems like the CMSDK_CM3.h not accepting any modification even though I can edit and save it normally. I've found out that not only struct, but simple #define addition also doesn't work.
So, I tried to add all the I2C-related structs and defines in separate header file "i2c.h". The error changed to "invalid redeclaration of type name I2C_TypeDef" If I comment out the structs and defines in the CMSDK_CM3.h, it compiles and works with one error left
error: #3092: anonymous unions are only supported in --gnu mode, or when enabled with #pragma_anon_unions
I hope this information helps
An error which suggests you name the union, or use the pragma..
Given the downstream code probably assumes it is anonymous, so use the pragma
Thankyou, Westonsupermare Pier. Using the pragma actually solved the problem. But, I still cannot add the struct in CMSDK_CM3.h. Do you know what may be the problem? Isn't it just strange if I must create separate header files everytime? I know it is neat and probably the CMSDK is designed to do that?
In case anybody find the same trouble. I tried to clean my project folder by deleting everything except the .uvporjx, sources, and header files. Without any setting are changed, I opened the .uvprojx, rebuild the project, and success. I still don't know the issue but that's it.