4

I’m trying to port an application written for a micro to Visual Studio so I can more easily run the debugger and scan through the code, rather than debug on the chip which is a bit of a pain when you want to learn how the application was written. I’m not at all a programming guru, and I keep getting this error, listed below, which is related to a structure:

error C2059: syntax error : '.'

The code is listed below, can anyone point me in the right direction.

typedef struct usart_reg_map 
{
    volatile uint32 SR;
    volatile uint32 DR;
    volatile uint32 BRR;
    volatile uint32 CR1;
    volatile uint32 CR2;
    volatile uint32 CR3;
    volatile uint32 GTPR;
}usart_reg_map;

#define USART1_BASE ((struct usart_reg_map*))

typedef struct usart_dev 
{
    usart_reg_map *regs;
}usart_dev;

struct usart_dev usart1 =
{
    .regs = USART1_BASE, 
};

usart_dev *USART1 = &usart1;

3 Answers3

3

There's two issues to consider:

  • Most likely you are compiling the code as C++. But C++ is not C and vice versa - they are not compatible languages. You need to enforce C compilation, which is typically done by naming your file .c instead of .cpp.

  • Even in C mode, Visual Studio has horrible support for standard C. The . initialization syntax is known as designated initializers and was introduced in the language year 1999. Microsoft has however insisted not to upgrade their C compiler, until some half-hearted attempt a few years ago. They may or may not support designated initializers, I don't know.

I would strongly recommend any beginner programmer to use a standard-compilant compiler instead. You can tell VS to use the gcc compiler instead of the default crap one, or you can download another IDE such as Codeblocks, which comes with gcc/mingw pre-installed per default.

Lundin
  • 460
2

In all cases it is a lot of work, but it is possible:

  1. Find out which classes, structures, types and functions you need from the micro libaries.
  2. Create files for them, similar to the micros libraries, preferably about the same file structure, but in a different location. These will only run in the PC version.
  3. Create a new project for the non micro environment, where the files in step 2 are used instead of 1. This can be done by changing the include path probably.

In step 2, you can make even some intelligence in the functions if you want to test that. Also you do not need to change any file that runs on the micro that you created yourself, because only the micro libraries have a PC counterpart.

About C99, make sure that you don't use C99 specific features in the generic code (your microcontroller code), and if you need it, make that code in separate functions which you could either port to VS, or maybe they are not needed as they are part of the microcontroller libraries or facade functions.

Michel Keijzers
  • 467
  • 2
  • 9
  • 23
1

Your specific problem is because, as you have discovered, the designated initializer .regs = USART1_BASE does not work in Visual Studio.

I usually use GCC and a makefile when testing my microcontroller projects on windows. This seems to work a lot better when trying to use features from a specific standard like C99.

As others have pointed out, you will likely have to do a lot of refactoring to make your entire project actually run on Windows. I usually break my project up into simple modules that I can compile and test on Windows with GCC, and then combine those well tested modules into a larger app that I can run and test on the target device.

cholz
  • 11