#if #endif preprocessing Directive, PortAudio - c

I understand the basics to the #if #endif preprocessor directives in C, in that depending which expression evaluates to be true, the proceeding code within the #if will be compiled, however I'm currently learning portaudio(I'm making a VOIP app for school) and I'm looking over some of their examples and I've become confused over a small section
/* Select sample format. */
#if 1
#define PA_SAMPLE_TYPE paFloat32
typedef float SAMPLE;
#define SAMPLE_SILENCE (0.0f)
#define PRINTF_S_FORMAT "%.8f"
#elif 1
#define PA_SAMPLE_TYPE paInt16
typedef short SAMPLE;
#define SAMPLE_SILENCE (0)
#define PRINTF_S_FORMAT "%d"
#elif 0
#define PA_SAMPLE_TYPE paInt8
typedef char SAMPLE;
#define SAMPLE_SILENCE (0)
#define PRINTF_S_FORMAT "%d"
#else
#define PA_SAMPLE_TYPE paUInt8
typedef unsigned char SAMPLE;
#define SAMPLE_SILENCE (128)
#define PRINTF_S_FORMAT "%d"
#endif
the first question that comes to mind is
#if 1
#define PA_SAMPLE_TYPE paFloat32
typedef float SAMPLE;
#define SAMPLE_SILENCE (0.0f)
#define PRINTF_S_FORMAT "%.8f"
#elif 1
#define PA_SAMPLE_TYPE paInt16
typedef short SAMPLE;
#define SAMPLE_SILENCE (0)
#define PRINTF_S_FORMAT "%d"
wont the #elif 1 always be skipped because if somehow #if 1 (#if true) evaluates to false, wont #elif 1 also evaluate to false?
question 2
isn't 1 evaluate to true and 0 to false? so wont #elif 0 always evaluate to false? i.e it doesn't really matter?
question 3
I'm going to be sending these samples over a socket, is skipping this preprocessor directive and just working with the code
#define PA_SAMPLE_TYPE paInt8
typedef char SAMPLE;
#define SAMPLE_SILENCE (0)
#define PRINTF_S_FORMAT "%d"
or
#define PA_SAMPLE_TYPE paUInt8
typedef unsigned char SAMPLE;
#define SAMPLE_SILENCE (128)
#define PRINTF_S_FORMAT "%d"
#endif
Would that be beter so as my SAMPLE_TYPE/SAMPLE can be treated as an array of characters/ Unsigned Characters (not having to convert floats to chars and then back again) for writing/reading from the socket?

What you need to understand is between a sequence of #if / #elif / #else, only one condition will be selected:
#if is selected here:
#if 1
// only this one will be selected
#elif 1
#else
#endif
#elif is selected here:
#if 0
#elif 1
// only this one will be selected
#else
#endif
#else is selected here:
#if 0
#elif 0
#else
// only this one will be selected
#endif

Related

How to make types configurable with preprocessor directives in C?

As of now, I have something like the following code in the header file of my library (names changed for clarity):
#ifndef CFG_RESULT_TYPE
#define CFG_RESULT_TYPE double
#endif
#if CFG_RESULT_TYPE == float
#define result_parser strtof
#elif CFG_RESULT_TYPE == double
#define result_parser strtod
#elif CFG_RESULT_TYPE == long double
#define result_parser strtold
#else
#error "Invalid result type"
#endif
typedef RESULT_TYPE result_t
I thought this would work, and it seems to for float and double (then again, maybe it's actually undefined behavior), but not for long double. What is the correct way to implement this?
I should also add that _Generic is out of the question; I am stuck on the C99 standard for this project.
#define DOUBLE 1
#define FLOAT 2
#define LONG_DOUBLE 3
#ifndef CFG_RESULT_TYPE
#define CFG_RESULT_TYPE DOUBLE
#endif
#if CFG_RESULT_TYPE == FLOAT
#define result_parser strtof
#define RESULT_TYPE float
#elif CFG_RESULT_TYPE == DOUBLE
#define result_parser strtod
#define RESULT_TYPE double
#elif CFG_RESULT_TYPE == LONG_DOUBLE
#define result_parser strtold
#define RESULT_TYPE long double
#else
#error "Invalid result type"
#endif
#ifdef RESULT_TYPE
typedef RESULT_TYPE result_t;
#endif

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)

Problem with If-Condition for Preprocessor

I write a bootloader for an AVR XMega Microcontroller and the bootloader got configured by a configuration file:
Config_Bootloader.h
#ifndef CONFIG_BOOTLOADER_H_
#define CONFIG_BOOTLOADER_H_
#include <avr/io.h>
#define BOOTLOADER_INTERFACE &USARTE0
#define BOOTLOADER_BAUD 115200
#define BOOTLOADER_TX 3
#endif /* CONFIG_BOOTLOADER_H_ */
This configuration file is should be preprocessed by another include file to get some register values etc.
Bootloader_Preprocessing.h
#ifndef BOOTLOADER_PREPROCESSING_H_
#define BOOTLOADER_PREPROCESSING_H_
#include <avr/io.h>
#ifdef USARTE0
#if(BOOTLOADER_INTERFACE == &USARTE0)
#define BOOTLOADER_PORT &PORTE
#else
#error "Invalid bootloader interface!"
#endif
#endif
#if(BOOTLOADER_BAUD == 9600)
#define BOOTLOADER_BRREG_VALUE 12
#define BOOTLOADER_SCALE_VALUE 0
#elif(BOOTLOADER_BAUD == 19200)
#define BOOTLOADER_BRREG_VALUE 11
#define BOOTLOADER_SCALE_VALUE -1
#elif(BOOTLOADER_BAUD == 38400)
#define BOOTLOADER_BRREG_VALUE 9
#define BOOTLOADER_SCALE_VALUE -2
#elif(BOOTLOADER_BAUD == 57600)
#define BOOTLOADER_BRREG_VALUE 75
#define BOOTLOADER_SCALE_VALUE -6
#elif(BOOTLOADER_BAUD == 115200)
#define BOOTLOADER_BRREG_VALUE 11
#define BOOTLOADER_SCALE_VALUE -7
#else
#error "Invalid baud rate for bootloader!"
#endif
#endif /* BOOTLOADER_PREPROCESSING_H_ */
I include both files into my Bootloader.h
#ifndef BOOTLOADER_H_
#define BOOTLOADER_H_
#include "Config_Bootloader.h"
#include "Bootloader_Preprocessing.h"
#endif /* BOOTLOADER_H_ */
And I get this errors and warnings:
> #define BOOTLOADER_INTERFACE &USARTE0
operator '&' has no left operand
> #if(BOOTLOADER_INTERFACE == &USARTE0)
in expansion of macro 'BOOTLOADER_INTERFACE'
#error "Invalid bootloader interface!"
So why does the compare of the address doesn´t work?
There is no such thing as an "address" in the preprocessor, therefore they cannot be compared in an #if preprocessor instruction.
See the GCC docs for #IF for details on what it can and can't do. Consult the documentation for the preprocessor you are using, additional/different restrictions may apply (you tagged this as AVR).
It seems that your preprocessor concluded that the operator & has to be the bitwise operator &, which is a binary operator and therefore requires a left operand.
Okay, I have a solution after struggeling with the C preprocessor.
I define the symbol BOOTLOADER_INTERFACE=E,0 in my config and process the input:
#define CATENATE(Prefix, Name) Prefix##Name
#define FIRST_ARG(A, B) A
#define SECOND_ARG(A, B) B
#define MAKE_USART_NAME(Uart) CATENATE(USART, Uart)
#define MAKE_PORT_NAME(Port) CATENATE(PORT, Port)
#define USART_NAME(Name) MAKE_USART_NAME(CATENATE(Name))
#define PORT_NAME(Name) MAKE_PORT_NAME(FIRST_ARG(Name))
The result is the address of the PORT- and USART-Structure, depending on the given USART interface.

How are the GNU C preprocessor predefined macros used?

I am trying to figure out how the integer type definitions from various header files are related to each other.
I just installed the CygWin. I opened the Cygwin\x86\usr\include\machine\_default_types.h. In it I noticed below code snippets:
#ifdef __INT8_TYPE__
typedef __INT8_TYPE__ __int8_t;
According to here, the __INT8_TYPE__ is one of the predefined pre-processor macros. And:
You should not use these macros directly; instead, include the
appropriate headers and use the typedefs. Some of these macros may not
be defined on particular systems if GCC does not provide a stdint.h
header on those systems.
So it seems the __INT8_TYPE__ should be defined somewhere else. But I searched the whole CygWin installation, there's no definition for it. All I found are some conditional statements just like above one.
Since I shouldn't use it directly. And no other files are defining it. How could this macro ever come into action? Or did I misunderstand something?
This is a predefined macro. To see them you can:
gcc -dM -E - < /dev/null
e.g.:
$ gcc -dM -E - < /dev/null | grep __INT
#define __INTMAX_C(c) c ## L
#define __INT8_C(c) c
#define __INT64_C(c) c ## L
#define __INT32_MAX__ 0x7fffffff
#define __INT_FAST32_MAX__ 0x7fffffffffffffffL
#define __INT_FAST16_TYPE__ long int
#define __INT_LEAST32_MAX__ 0x7fffffff
#define __INT_FAST64_TYPE__ long int
#define __INT32_C(c) c
#define __INT_FAST32_TYPE__ long int
#define __INT16_MAX__ 0x7fff
#define __INT8_TYPE__ signed char
#define __INT_LEAST16_TYPE__ short int
#define __INT_FAST16_MAX__ 0x7fffffffffffffffL
#define __INT_LEAST16_MAX__ 0x7fff
#define __INT64_MAX__ 0x7fffffffffffffffL
#define __INT_LEAST64_TYPE__ long int
#define __INT16_TYPE__ short int
#define __INT_LEAST8_TYPE__ signed char
#define __INT_FAST8_MAX__ 0x7f
#define __INTPTR_MAX__ 0x7fffffffffffffffL
#define __INTPTR_TYPE__ long int
#define __INT_FAST64_MAX__ 0x7fffffffffffffffL
#define __INT_MAX__ 0x7fffffff
#define __INT64_TYPE__ long int
#define __INT_LEAST64_MAX__ 0x7fffffffffffffffL
#define __INT_LEAST8_MAX__ 0x7f
#define __INT_LEAST32_TYPE__ int
#define __INT_FAST8_TYPE__ signed char
#define __INTMAX_MAX__ 0x7fffffffffffffffL
#define __INT8_MAX__ 0x7f
#define __INT32_TYPE__ int
#define __INTMAX_TYPE__ long int
#define __INT16_C(c) c

C Preprocessor generate macros by concatenation and stringification [duplicate]

This question already has answers here:
What are the applications of the ## preprocessor operator and gotchas to consider?
(13 answers)
Closed 7 years ago.
I have a set of target macros for which I want to generate aliases based on a choosing macro, like so:
Choosing macro:
#define I2C_MODULE 1
Alias macros (conceptual form):
#define I2C_MODULE_BASE I2C<Value of I2C_MODULE>_BASE
#define I2C_MODULE_NVIC INT_I2C<Value of I2C_MODULE>
Target macros (from an external file out of my control):
#define INT_I2C0 24
#define INT_I2C1 53
...
#define I2C0_BASE 0x40020000
#define I2C1_BASE 0x40021000
...
I wanted to have the preprocessor generate the alias macros I2C_MODULE_BASE and I2C_MODULE_NVIC based on the
choosing macro I2C_MODULE, but after much reading Q1, P1 and many other references I lost track of, I ended up hard-coding their values. Below I show my current working definitions, and then my last failed attempts at generating the macros:
What works:
#define I2C_MODULE 1
#define I2C_MODULE_BASE I2C1_BASE
#define I2C_MODULE_NVIC INT_I2C1
what did not work:
#define I2C_MODULE 1
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
/* Attempt 1 */
#define I2C_MODULE_BASE "I2C" STR(I2C_MODULE) "_BASE"
#define I2C_MODULE_NVIC "INT_I2C" STR(I2C_MODULE)
/* Attempt 2 */
#define _I2C_MODULE_BASE "I2C" STR(I2C_MODULE) "_BASE"
#define _I2C_MODULE_NVIC "INT_I2C" STR(I2C_MODULE)
#define I2C_MODULE_BASE _I2C_MODULE_BASE
#define I2C_MODULE_NVIC _I2C_MODULE_NVIC
EDIT: I expanded upon the accepted answer to get to where I wanted, as follows:
#define PASTE2(a, b) a ## b
#define PASTE3(a, b, c) a ## b ## c
#define _I2C_MODULE_BASE(x) PASTE3(I2C, x, _BASE)
#define _I2C_MODULE_NVIC(x) PASTE2(INT_I2C, x)
#define I2C_MODULE_BASE _I2C_MODULE_BASE(I2C_MODULE)
#define I2C_MODULE_NVIC _I2C_MODULE_NVIC(I2C_MODULE)
This seems to work:
#define I2C_MODULE 1
//Alias macros (conceptual form):
//#define I2C_MODULE_BASE I2C<Value of I2C_MODULE>_BASE
//#define I2C_MODULE_NVIC INT_I2C<Value of I2C_MODULE>
//Target macros (from an external file out of my control):
#define INT_I2C0 24
#define INT_I2C1 53
#define I2C0_BASE 0x40020000
#define I2C1_BASE 0x40021000
#define PASTE2(a, b) a ## b
#define PASTE3(a, b, c) a ## b ## c
#define I2C_MODULE_BASE(x) PASTE3(I2C, x, _BASE)
#define I2C_MODULE_NVIC(x) PASTE2(INT_I2C, x)
extern int i2c_module_base = I2C_MODULE_BASE(I2C_MODULE);
extern int i2c_module_nvic = I2C_MODULE_NVIC(I2C_MODULE);
extern int i2c_module_base_0 = I2C_MODULE_BASE(0);
extern int i2c_module_nvic_0 = I2C_MODULE_NVIC(0);
extern int i2c_module_base_1 = I2C_MODULE_BASE(1);
extern int i2c_module_nvic_1 = I2C_MODULE_NVIC(1);
Sample output (from cpp):
# 1 "xx.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "xx.c"
# 21 "xx.c"
extern int i2c_module_base = 0x40021000;
extern int i2c_module_nvic = 53;
extern int i2c_module_base_0 = 0x40020000;
extern int i2c_module_nvic_0 = 24;
extern int i2c_module_base_1 = 0x40021000;
extern int i2c_module_nvic_1 = 53;
It is closely based on my answer to C preprocessor and token concatenation.
There are undoubtedly other ways that the I2C_MODULE_BASE and I2C_MODULE_NVIC macros could be written, but the key points are:
Using the ## token pasting operator (not the # stringifying operator).
Using two levels of macro (for example, I2C_MODULE_BASE and PASTE3).
I suspect that you are writing a I2C driver which can generically handle multiple I2C hardware peripherals in the same micro-controller without rewriting all the same code multiple times.
In that case, what you are really looking for probably is something like this:
#define I2C1 ((volatile uint8_t*)0x12345678) // address of first hw register for I2C1
#define I2C2 ((volatile uint8_t*)0x55555555) // address of first hw register for I2C2
/* map all registers used for I2C, they will have same register layout for every
peripheral no matter which one: */
#define I2C_CONTROL(base) (*(base + 0))
#define I2C_DATA(base) (*(base + 1))
...
// create some dummy typedef to make your functions look nice:
typedef volatile uint8_t* I2C_t;
// define whatever functions you need in the driver:
void i2c_init (IC2_t bus);
void i2c_send (I2C_t bus, const uint8_t* data, size_t n);
...
// implement functions in a bus-independent way:
void i2c_init (IC2_t bus)
{
I2C_CONTROL(bus) = THIS | THAT; // setup registers
}
// caller code:
i2c_init(I2C1);
i2c_init(I2C2);
...
i2c_send(I2C1, "hello", 5);
i2c_send(I2C2, "world", 5);
Just use #if / #else / #endif
#if (I2C_MODULE == 0)
#define I2C_MODULE_BASE I2C0_BASE
#define I2C_MODULE_NVIC INT_I2C0
#elif (I2C_MODULE == 1)
#define I2C_MODULE_BASE I2C1_BASE
#define I2C_MODULE_NVIC INT_I2C1
#else
#error Unknown configuration
#endif

Resources