#include "mw_bluetooth.h"

/* user external inc. */

#include "mw_soft_timer.h" 

#include "stm32f10x.h"

#include "public_diy.h"


#define BLUETOOTH_USART_PERIPH      (USART1)
// Initial the entity of bluetooth buf.
mw_bluetooth_t bt_drv_buf[bluetooth_num];

/*************************************************************************************
 * @brief    Get the bluetooth object
 * @param[in/out] e_val
 * @return mw_bluetooth_t
 * 
 * @warning  
 * @note     
  *************************************************************************************/
mw_bluetooth_t mw_get_bluetooth_drv(bluetooth_type_enum e_val)
{
    if(e_val >= bluetooth_num)
    {
        while(1);
    }
    return bt_drv_buf[e_val];
}
/*************************************************************************************
 * @brief    Bluetooth_usart init function
 * @return int8_t      【返回值注释】
 * 
 * @warning  【不可重入,阻塞等警告】
 * @note     【重大修改】
  *************************************************************************************/
static int8_t bluetooth_system_usart_init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//TX
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//RX
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(BLUETOOTH_USART_PERIPH, &USART_InitStructure);
	
	USART_ITConfig(BLUETOOTH_USART_PERIPH, USART_IT_RXNE, ENABLE);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(BLUETOOTH_USART_PERIPH, ENABLE);

    return RET_OK;
}

static int8_t bluetooth_system_usart_deinit(void)
{
    USART_DeInit(BLUETOOTH_USART_PERIPH);
    return RET_OK;
}

static int8_t bluetooth_system_usart_send_one_byte(uint8_t uc_data)
{
    USART_SendData(BLUETOOTH_USART_PERIPH, uc_data);
    return RET_OK;
}

static uint8_t bluetooth_system_usart_receive_one_byte(void)
{
    return (uint8_t)(USART_ReceiveData(BLUETOOTH_USART_PERIPH));
}

static void bluetooth_init(void)
{
	;
}
static void bluetooth_deinit(void)
{
	;
}
static void bluetooth_send_bytes(uint8_t * tx_buf, uint16_t len)
{
	;
}
static void bluetooth_recv_deal(void)
{
	;
}
/*************************************************************************************
 * @brief    bluetooth driver init.
 * 
 * @warning  
 * @note     
 *************************************************************************************/
void mw_bluetooth_drv_init(void)
{
/*  =============== hc06 init ======================== */
    /* launcg bluetooth driver type */
    bt_drv_buf[hc06].bt_drv = hc06;
    /* launch timer api - 32bit systick */ 
    bt_drv_buf[hc06].bt_timer.pf_get_systick_ms = get_systick_ms;
    /* launch the api of USART. */
    bt_drv_buf[hc06].bt_usart.pf_init = bluetooth_system_usart_init;
    bt_drv_buf[hc06].bt_usart.pf_deinit = bluetooth_system_usart_deinit;
    bt_drv_buf[hc06].bt_usart.pf_recv_one_byte = bluetooth_system_usart_receive_one_byte;
    bt_drv_buf[hc06].bt_usart.pf_trans_one_byte = bluetooth_system_usart_send_one_byte;
	bt_drv_buf[hc06].bt_usart.pf_recv_one_byte = bluetooth_system_usart_receive_one_byte;
	/* launch the api of inner bluetooth. */
	bt_drv_buf[hc06].pf_init = bluetooth_init;
	bt_drv_buf[hc06].pf_deinit = bluetooth_deinit;
	bt_drv_buf[hc06].pf_send_bytes = bluetooth_send_bytes;
	bt_drv_buf[hc06].pf_recv_deal = bluetooth_recv_deal;
	

}


