/*********************************************************************************************************************
* RT1064DVL6A Opensourec Library RT1064DVL6A Դ⣩һڹٷ SDK ӿڵĵԴ
* Copyright (c) 2022 SEEKFREE ɿƼ
* 
* ļ RT1064DVL6A Դһ
* 
* RT1064DVL6A Դ 
* Ըᷢ GPLGNU General Public License GNUͨù֤
*  GPL ĵ3棨 GPL3.0ѡģκκİ汾·/޸
* 
* Դķϣܷãδκεı֤
* ûԻʺض;ı֤
* ϸμ GPL
* 
* ӦյԴͬʱյһ GPL ĸ
* ûУ<https://www.gnu.org/licenses/>
* 
* ע
* Դʹ GPL3.0 Դ֤Э Ϊİ汾
* Ӣİ libraries/doc ļµ GPL3_permission_statement.txt ļ
* ֤ libraries ļ ļµ LICENSE ļ
* ӭλʹò ޸ʱ뱣ɿƼİȨ
* 
* ļ          zf_device_bluetooth_ch9141
* ˾          ɶɿƼ޹˾
* 汾Ϣ          鿴 libraries/doc ļ version ļ 汾˵
*           IAR 8.32.4 or MDK 5.33
* ƽ̨          RT1064DVL6A
*           https://seekfree.taobao.com/
* 
* ޸ļ¼
*                                                                    ע
* 2024-06-25        W            first version
********************************************************************************************************************/
/*********************************************************************************************************************
* ߶壺
*                   ------------------------------------
*                   ģܽ            Ƭܽ
*                   RX                  鿴 zf_device_wireless_ble6a20.h  BLE6A20_RX_PIN  궨
*                   TX                  鿴 zf_device_wireless_ble6a20.h  BLE6A20_TX_PIN  궨
*                   RTS                 鿴 zf_device_wireless_ble6a20.h  BLE6A20_RTS_PIN 궨
*                   VCC                 3.3VԴ
*                   GND                 Դ
*                   
*                   ------------------------------------
*********************************************************************************************************************/

#include "zf_device_ble6a20.h"
#include "zf_common_clock.h"
#include "zf_common_debug.h"
#include "zf_common_fifo.h"
#include "zf_driver_delay.h"
#include "zf_driver_gpio.h"
#include "zf_driver_uart.h"
#include "zf_device_type.h"

static  fifo_struct                                     ble6a20_fifo;
static  uint8                                           ble6a20_buffer[BLE6A20_BUFFER_SIZE];

static          uint8                                   ble6a20_data          = 0;
#if (1 == BLE6A20_AUTO_BAUD_RATE)
static volatile ble6a20_auto_baudrate_state_enum  ble6a20_auto_baud_flag     = BLE6A20_AUTO_BAUD_RATE_INIT;
static volatile uint8                                   ble6a20_auto_baud_data[3]  = {0x00, 0x01, 0x03};
#endif

//-------------------------------------------------------------------------------------------------------------------
//      תģ 
// ˵     data            8bit 
// ز     uint32          ʣ෢ͳ 0- 1-δ
// ʹʾ     ble6a20_send_byte(data);
// עϢ     
//-------------------------------------------------------------------------------------------------------------------
uint32 ble6a20_send_byte (const uint8 data)
{
    uint16 time_count = BLE6A20_TIMEOUT_COUNT;
    while(time_count)
    {
        if(!gpio_get_level(BLE6A20_RTS_PIN))
        {
            uart_write_byte(BLE6A20_INDEX, data);                         // 
            break;
        }
        time_count --;
        system_delay_ms(1);
    }
    return (0 == time_count);
}

//-------------------------------------------------------------------------------------------------------------------
//      תģ ݿ
// ˵     *buff           ͻ
// ˵     len             ݳ
// ز     uint32          ʣ෢ͳ
// ʹʾ     ble6a20_send_buffer(buff, 64);
// עϢ     
//-------------------------------------------------------------------------------------------------------------------
uint32 ble6a20_send_buffer (const uint8 *buff, uint32 len)
{
    zf_assert(NULL != buff);
    uint16 time_count = 0;
    while(0 != len)
    {
        if(!gpio_get_level(BLE6A20_RTS_PIN))                              // RTSΪ͵ƽ 
        {
            if(30 <= len)                                                       // ݷ 30byte ÿ
            {
                uart_write_buffer(BLE6A20_INDEX, buff, 30);               // 
                buff += 30;                                                     // ַƫ
                len -= 30;                                                      // 
                time_count = 0;
            }
            else                                                                //  30byte һԷ
            {
                uart_write_buffer(BLE6A20_INDEX, buff, len);              // 
                len = 0;
                break;
            }
        }
        else                                                                    // RTSΪߵƽ ģæ
        {
            if(BLE6A20_TIMEOUT_COUNT <= (++ time_count))                  // ȴʱ
            {
                break;                                                          // ˳
            }
            system_delay_ms(1);
        }
    }
    return len;
}

//-------------------------------------------------------------------------------------------------------------------
//      תģ ַ
// ˵     *str            Ҫ͵ַַ
// ز     uint32          ʣ෢ͳ
// ʹʾ     ble6a20_send_string("Believe in yourself.");
// עϢ     
//-------------------------------------------------------------------------------------------------------------------
uint32 ble6a20_send_string (const char *str)
{
    zf_assert(NULL != str);
    uint16 time_count = 0;
    uint32 len = strlen(str);
    while(0 != len)
    {
        if(!gpio_get_level(BLE6A20_RTS_PIN))                              // RTSΪ͵ƽ 
        {
            if(128 <= len)                                                       // ݷ 128byte ÿ
            {
                uart_write_buffer(BLE6A20_INDEX, (const uint8 *)str, 128); // 
                str += 128;                                                      // ַƫ
                len -= 128;                                                      // 
                time_count = 0;
            }
            else                                                                //  128byte һԷ
            {
                uart_write_buffer(BLE6A20_INDEX, (const uint8 *)str, len);// 
                len = 0;
                break;
            }
        }
        else                                                                    // RTSΪߵƽ ģæ
        {
            if(BLE6A20_TIMEOUT_COUNT <= (++ time_count))                  // ȴʱ
            {
                break;                                                          // ˳
            }
            system_delay_ms(1);
        }
    }
    return len;
}

//-------------------------------------------------------------------------------------------------------------------
//      תģ ͷͼλ鿴ͼ
// ˵     *image_addr     Ҫ͵ͼַ
// ˵     image_size      ͼĴС
// ز     void
// ʹʾ     ble6a20_send_image(&mt9v03x_image[0][0], MT9V03X_IMAGE_SIZE);
// עϢ     
//-------------------------------------------------------------------------------------------------------------------
void ble6a20_send_image (const uint8 *image_addr, uint32 image_size)
{
    zf_assert(NULL != image_addr);
    extern uint8 camera_send_image_frame_header[4];
    ble6a20_send_buffer(camera_send_image_frame_header, 4);
    ble6a20_send_buffer((uint8 *)image_addr, image_size);
}

//-------------------------------------------------------------------------------------------------------------------
//      תģ ȡ
// ˵     *buff           ջ
// ˵     len             ȡݳ
// ز     uint32          ʵʶȡݳ
// ʹʾ     ble6a20_read_buffer(buff, 32);
// עϢ     
//-------------------------------------------------------------------------------------------------------------------
uint32 ble6a20_read_buffer (uint8 *buff, uint32 len)
{
    zf_assert(NULL != buff);
    uint32 data_len = len;
    fifo_read_buffer(&ble6a20_fifo, buff, &data_len, FIFO_READ_AND_CLEAN);
    return data_len;
}

//-------------------------------------------------------------------------------------------------------------------
//      תģ жϻص
// ˵     void
// ز     void
// ʹʾ     ble6a20_callback();
// עϢ     ú ISR ļ жϳ򱻵
//              ɴжϷ wireless_module_uart_handler() 
//               wireless_module_uart_handler() ñ
//-------------------------------------------------------------------------------------------------------------------
void ble6a20_callback (void)
{
    if(uart_query_byte(BLE6A20_INDEX, &ble6a20_data))
	{
		fifo_write_buffer(&ble6a20_fifo, &ble6a20_data, 1);
#if BLE6A20_AUTO_BAUD_RATE                                                // Զ
		if(BLE6A20_AUTO_BAUD_RATE_START == ble6a20_auto_baud_flag && 3 == fifo_used(&ble6a20_fifo))
		{
			uint32 ble6a20_auto_baud_count = 3;
			ble6a20_auto_baud_flag = BLE6A20_AUTO_BAUD_RATE_GET_ACK;
			fifo_read_buffer(&ble6a20_fifo, (uint8 *)ble6a20_auto_baud_data, (uint32 *)&ble6a20_auto_baud_count, FIFO_READ_AND_CLEAN);
		}
#endif
	}

}

//-------------------------------------------------------------------------------------------------------------------
//      תģ ʼ
// ˵     void
// ز     void
// ʹʾ     ble6a20_init();
// עϢ     
//-------------------------------------------------------------------------------------------------------------------
uint8 ble6a20_init (void)
{
    uint8 return_state = 0;

    // ȴģʼ
    system_delay_ms(50);

    set_wireless_type(BLE6A20, ble6a20_callback);

    fifo_init(&ble6a20_fifo, FIFO_DATA_8BIT, ble6a20_buffer, BLE6A20_BUFFER_SIZE);
    gpio_init(BLE6A20_RTS_PIN, GPI, GPIO_HIGH, GPI_PULL_UP);
#if(0 == BLE6A20_AUTO_BAUD_RATE)                                          // رԶ
    // ʹõĲΪ115200 ΪתģĬϲ ģ鲢޸ĴڵĲ
    uart_init (BLE6A20_INDEX, BLE6A20_BUAD_RATE, BLE6A20_RX_PIN, BLE6A20_TX_PIN);   // ʼ
    uart_rx_interrupt(BLE6A20_INDEX, 1);
#elif(1 == BLE6A20_AUTO_BAUD_RATE)                                        // Զ
    uint8 rts_init_status = 0;

    ble6a20_auto_baud_flag = BLE6A20_AUTO_BAUD_RATE_INIT;
    ble6a20_auto_baud_data[0] = 0;
    ble6a20_auto_baud_data[1] = 1;
    ble6a20_auto_baud_data[2] = 3;

    rts_init_status = gpio_get_level(BLE6A20_RTS_PIN);
    gpio_init(BLE6A20_RTS_PIN, GPO, rts_init_status, GPO_PUSH_PULL);      // ʼ

    uart_init (BLE6A20_INDEX, BLE6A20_BUAD_RATE, BLE6A20_RX_PIN, BLE6A20_TX_PIN);   // ʼ
    uart_rx_interrupt(BLE6A20_INDEX, 1);

    system_delay_ms(5);                                                         // ģϵ֮Ҫʱȴ
    gpio_set_level(BLE6A20_RTS_PIN, !rts_init_status);                    // RTSߣԶģʽ
    system_delay_ms(100);                                                       // RTS֮ʱ20ms
    gpio_toggle_level(BLE6A20_RTS_PIN);                                   // RTSȡ

    do
    {
        ble6a20_auto_baud_flag = BLE6A20_AUTO_BAUD_RATE_START;
        uart_write_byte(BLE6A20_INDEX, ble6a20_auto_baud_data[0]);       // ض ģԶжϲ
        uart_write_byte(BLE6A20_INDEX, ble6a20_auto_baud_data[1]);       // ض ģԶжϲ
        uart_write_byte(BLE6A20_INDEX, ble6a20_auto_baud_data[2]);       // ض ģԶжϲ
        system_delay_ms(20);

        if(BLE6A20_AUTO_BAUD_RATE_GET_ACK != ble6a20_auto_baud_flag)     // ԶǷ
        {
            return_state = 1;                                                   // 뵽 ˵Զʧ
            break;
        }

        if( 0xa5 != ble6a20_auto_baud_data[0] &&                               // ԶǷȷ
            0xff != ble6a20_auto_baud_data[1] &&                               // ԶǷȷ
            0xff != ble6a20_auto_baud_data[2] )                                // ԶǷȷ
        {
            return_state = 1;                                                   // 뵽 ˵Զʧ
            break;
        }
        ble6a20_auto_baud_flag = BLE6A20_AUTO_BAUD_RATE_SUCCESS;

        gpio_init(BLE6A20_RTS_PIN, GPI, 0, GPI_PULL_UP);                        // ʼ
        system_delay_ms(10);                                                    // ʱȴ ģ׼
    }while(0);
#endif
    return return_state;
}
