/*!
    \file    main.c
    \brief   led spark with systick, USART print and key example
    
    \version 2017-12-26, V1.0.0, firmware for GD32E10x
*/

/*
    Copyright (c) 2017, GigaDevice Semiconductor Inc.

    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 copyright holder 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 COPYRIGHT HOLDER 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.
*/

#include "s600.h"
#include "main.h"
#include "s600_can.h"
#include "s600_iap.h"

#ifdef CONFIG_BOARD_PS100
#include "bl_common.h"
#include "bl_drv_usart.h"
#include "bl_drv_usart_2.h"

DELAY_COMMON send_delay;
uint32_t g_event = NO_EVENT;

static uint8_t app_rs485_buf[TX_BUFFER_SIZE] = { 0x17, 0x31, 0x42, 0x10, 0x8B, 0xBE, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

__STATIC_INLINE void Send_Sub_BMS_CMD_Delay(void)
{
	if (send_delay.set) {
		++send_delay.count;

		if(send_delay.count >= 4) {
			send_delay.count = 0;
			g_event |= SUB_BMS_2_SEND_CMD_EVENT;
		} else if(send_delay.count == 2) {
			g_event |= SUB_BMS_1_SEND_CMD_EVENT;
		}
	}
}

__STATIC_INLINE void BMS_Com_Initial(void)
{
	Usart1_Initial();
	Usart2_Initial();

	send_delay.set = 1;
	send_delay.count = 4;

	rcu_periph_clock_enable(RCU_GPIOA);
	gpio_init(GPIOA,GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_12);
	gpio_bit_reset(GPIOA, GPIO_PIN_12);

	rcu_periph_clock_enable(RCU_GPIOA);
	gpio_init(GPIOA,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_7);

	gpio_bit_reset(GPIOA, GPIO_PIN_7);
}

__STATIC_INLINE int8_t Send_Sub_BMS_CMD_1(void)
{
	return Send_Data_RS485(app_rs485_buf, app_rs485_buf[0]);
}

__STATIC_INLINE int8_t Send_Sub_BMS_CMD_2(void)
{
	return Send_Data_2_RS485(app_rs485_buf, app_rs485_buf[0]);
}
#endif

__STATIC_INLINE void systick_config(void)
{
	/* setup systick timer for 10Hz interrupts */
	SysTick_Config(SystemCoreClock / 10);
	/* configure the systick handler priority */
	NVIC_SetPriority(SysTick_IRQn, 0x00U);
}

int main(void)
{
	u8 command[] = { 0xF0, CONFIG_CAN_ID, 0x00 };

	s600_iap_boot(true);

	systick_config();

#ifdef CONFIG_BOARD_PS100
	BMS_Com_Initial();
#endif

	s600_can_device_config();

	s600_can_send_command(S600_CAN_ALL_ID, command, sizeof(command));

	while (1) {
		s600_can_poll();

		if (s600_iap_100ms > 600) {
			s600_iap_boot(false);
			s600_iap_100ms = 0;
			s600_can_send_command(S600_CAN_ALL_ID, command, sizeof(command));
		}

#ifdef CONFIG_BOARD_PS100
		if (g_event & SUB_BMS_1_SEND_CMD_EVENT) {
			Send_Sub_BMS_CMD_1();
			g_event &= ~SUB_BMS_1_SEND_CMD_EVENT;
		}

		if (g_event & SUB_BMS_2_SEND_CMD_EVENT) {
			Send_Sub_BMS_CMD_2();
			g_event &= ~SUB_BMS_2_SEND_CMD_EVENT;
		}
#endif
	}
}

void SysTick_Handler(void)
{
	s600_iap_100ms++;

#ifdef CONFIG_BOARD_PS100
	Send_Sub_BMS_CMD_Delay();
#endif
}
