blob: 57dc327ce6ad1a421451893cc56359af78667415 [file] [log] [blame]
/*
* Copyright (c) 2015, Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL CORPORATION OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __QM_COMMON_H__
#define __QM_COMMON_H__
#if (UNIT_TEST)
#define __volatile__(x)
#define __asm__
#endif /* UNIT_TEST */
#include <stdint.h>
#include <stdbool.h>
#include "qm_rc.h"
#define QM_R volatile const
#define QM_W volatile
#define QM_RW volatile
#ifndef NULL
#define NULL ((void *)0)
#endif
#define REG_VAL(addr) (*((volatile uint32_t *)addr))
/**
* In our reference implementation, by default DEBUG enables QM_PUTS and
* QM_ASSERT but not QM_PRINTF.
* User can modify this block to customise the default DEBUG configuration.
*/
#if (DEBUG)
#ifndef ASSERT_ENABLE
#define ASSERT_ENABLE (1)
#endif
#ifndef PUTS_ENABLE
#define PUTS_ENABLE (1)
#endif
#endif /* DEBUG */
/**
* Decide how to handle user input validation. Possible options upon discovering
* an invalid input parameter are:
* - Return an error code.
* - Assert.
* - Assert and save the error into the General Purpose Sticky Register 0.
* - Do nothing, only recommended when code size is a major concern.
*/
#if (DEBUG)
#ifndef QM_CHECK_RETURN_ERROR
#define QM_CHECK_RETURN_ERROR (1)
#endif
#ifndef QM_CHECK_ASSERT
#define QM_CHECK_ASSERT (0)
#endif
#ifndef QM_CHECK_ASSERT_SAVE_ERROR
#define QM_CHECK_ASSERT_SAVE_ERROR (0)
#endif
#endif /* DEBUG */
/*
* Setup UART for stdout/stderr.
*/
void stdout_uart_setup(uint32_t baud_divisors);
/*
* Default UART baudrate divisors at the sysclk speed set by Boot ROM
*/
#define BOOTROM_UART_115200 (QM_UART_CFG_BAUD_DL_PACK(0, 17, 6))
/**
* Selectively enable printf/puts/assert.
* User can modify the defines to divert the calls to custom implementations.
*/
#if (PRINTF_ENABLE || PUTS_ENABLE || ASSERT_ENABLE)
#include <stdio.h>
#endif /* PRINTF_ENABLE || PUTS_ENABLE || ASSERT_ENABLE */
#if (PRINTF_ENABLE)
#define QM_PRINTF(...) printf(__VA_ARGS__)
#else
#define QM_PRINTF(...)
#endif /* PRINTF_ENABLE */
#if (PUTS_ENABLE)
#define QM_PUTS(x) puts(x)
#else
#define QM_PUTS(x)
#endif /* PUTS_ENABLE */
#if (ASSERT_ENABLE)
#include <assert.h>
#define QM_ASSERT(x) assert(x)
#else
#define QM_ASSERT(x)
#endif /* ASSERT_ENABLE */
/**
* Select which UART to use for stdout/err (default: UART0)
*/
#if (STDOUT_UART_0 && STDOUT_UART_1)
#error "STDOUT_UART_0 and STDOUT_UART_1 are mutually exclusive"
#elif(!(STDOUT_UART_0 || STDOUT_UART_1))
#define STDOUT_UART_0 (1)
#endif
#if (STDOUT_UART_0)
#define STDOUT_UART (QM_UART_0)
#elif(STDOUT_UART_1)
#define STDOUT_UART (QM_UART_1)
#endif
/**
* Select assert action (default: put the IA core into HLT state)
*/
#if (ASSERT_ACTION_HALT && ASSERT_ACTION_RESET)
#error "ASSERT_ACTION_HALT and ASSERT_ACTION_RESET are mutually exclusive"
#elif(!(ASSERT_ACTION_HALT || ASSERT_ACTION_RESET))
#define ASSERT_ACTION_HALT (1)
#endif
/**
* Select check action
*/
#if (1 < QM_CHECK_RETURN_ERROR + QM_CHECK_ASSERT + QM_CHECK_ASSERT_SAVE_ERROR)
#error "Only a single check action may be performed"
#endif
#if (QM_CHECK_RETURN_ERROR)
#define QM_CHECK(cond, error) \
do { \
if (!(cond)) { \
return (error); \
} \
} while (0)
#elif(QM_CHECK_ASSERT)
#define QM_CHECK(cond, error) QM_ASSERT(cond)
#elif(QM_CHECK_ASSERT_SAVE_ERROR)
#define SAVE_LAST_ERROR(x) \
do { \
QM_SCSS_GP->gps[0] = x; \
} while (0)
#define QM_CHECK(cond, error) \
do { \
SAVE_LAST_ERROR(error); \
QM_ASSERT(cond); \
} while (0)
#else
/* Do nothing with checks */
#define QM_CHECK(cond, error)
#endif
/* Bitwise operation helpers */
#ifndef BIT
#define BIT(x) (1U << x)
#endif
/* Set all bits */
#ifndef SET_ALL_BITS
#define SET_ALL_BITS (-1)
#endif
/**
* Helper to convert a macro parameter into its literal text.
*/
#define QM_STRINGIFY(s) #s
/**
* Convert the source version numbers into an ASCII string.
*/
#define QM_VER_STRINGIFY(major, minor, patch) \
QM_STRINGIFY(major) "." QM_STRINGIFY(minor) "." QM_STRINGIFY(patch)
#endif /* __QM_COMMON_H__ */