/******************************************************************************
 *
 * 2017 (c) Lattice Semiconductor Corporation
 *
 * This program is free software; you can redistribute it and/or
 * modify it only under the terms of version 2 of the GNU General Public License
 * as published by the Free Software Foundation, and not any later version.
 *
 * This program is distributed "AS IS" WITHOUT ANY WARRANTY of any kind,
 * whether express or implied, including without limitation the implied warranty
 * of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE or NON-INFRINGEMENT.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * You may contact Lattice Semiconductor Corporation at
 * 111 SW 5th Ave, Suite 700, Portland, OR 97204.
 *
 *****************************************************************************/

#include "sii_datatypes.h"
#include "sii_platform_api.h"

static uint32_t (*block_write)(UINT32 dev_id, UINT16 addr,
		const UINT8 *p_data, UINT16 size) = NULL;
static uint32_t (*block_read)(UINT32 dev_id, UINT16 addr,
		UINT8 *p_data, UINT16 size) = NULL;
static void (*reset_set)(bool_t b_on) = NULL;
static uint32_t (*time_msec_query)(void) = NULL;
static void (*log_print)(const char *message) = NULL;
static void (*sleep_msec)(uint32_t millisec) = NULL;

void sii_platform_register_api(
		uint32_t (*in_block_write)(UINT32 dev_id, UINT16 addr, const UINT8 *p_data, UINT16 size),
		uint32_t (*in_block_read)(UINT32 dev_id, UINT16 addr, UINT8 *p_data, UINT16 size),
		void (*in_reset_set)(bool_t b_on),
		uint32_t (*in_time_msec_query)(void),
		void (*in_log_print)(const char *message),
		void (*in_sleep_msec)(uint32_t millisec)) {
	block_write = in_block_write;
	block_read = in_block_read;
	reset_set = in_reset_set;
	time_msec_query = in_time_msec_query;
	log_print = in_log_print;
	sleep_msec = in_sleep_msec;
}

uint32_t sii_platform_host_block_write(uint32_t dev_id, uint16_t addr,
		const uint8_t *p_data, uint16_t size) {
	if (NULL == block_write)
		return 1;
	return block_write(dev_id, addr, p_data, size);
}

uint32_t sii_platform_host_block_read(uint32_t dev_id, uint16_t addr,
		uint8_t *p_data, uint16_t size) {
	if (NULL == block_read)
		return 1;
	return block_read(dev_id, addr, p_data, size);
}

void sii_platform_device_reset_set(uint32_t dev_id, bool_t b_on) {
	if (NULL == reset_set)
		return;
	reset_set(b_on);
}

uint32_t sii_platform_time_msec_query(void) {
	if (NULL == time_msec_query)
		return 1;
	return time_msec_query();
}

void sii_platform_log_print(const char *message) {
	if (NULL == log_print)
		return;
	log_print(message);
}

/* Delay Function */
void sii_platform_sleep_msec(uint32_t millisec) {
	if (NULL == sleep_msec)
		return;
	sleep_msec(millisec);
}
