/*****************************************************************************
 * file    Inc/recording_wave.h 
 * author  YuLiang
 * version 1.0.0
 * date    03-Aug-2022
 * brief   This file provides all the headers of recording wave function.
 ******************************************************************************
 * Attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2022 LandPower</center></h2>
 *
 * 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 WTOE 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.
 *
 ******************************************************************************/

#ifndef __RECORDING_WAVE_H__
#define __RECORDING_WAVE_H__

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os.h"

/* Define --------------------------------------------------------------------*/
#define WAVE_WRITE (0x80)
#define WAVE_READ (0x00)

#define WAVE_ALL_DATA_LEN 8000000
#define WAVE_PKT_SUM 8000
#define WAVE_DATA_LEN 512

#define WAVE_RT_END 0x01                // 采样完成.
#define WAVE_RT_E_ADC 0x02              // 采样 ADC   错误.
#define WAVE_RT_E_TO 0x03               // 采样超时  错误.
#define WAVE_RT_E_V 0x04                // 电压错误.
#define WAVE_RT_E_ERR 0x05              // 采样超时  错误.

/* Exported types ------------------------------------------------------------*/
/* 高频录波状态. */
typedef enum
{
    WAVE_STATE_IDLE = 0,
    WAVE_STATE_START,
    WAVE_STATE_WAIT,
    WAVE_STATE_READ,
    WAVE_STATE_STOP,
    WAVE_STATE_ADC_ERROR,
    WAVE_STATE_TIMEOUT,
    WAVE_STATE_COMPLETION = 255
} WAVE_STATE_E;

/* 高频录波状态机. */
typedef struct
{
    uint8_t state;
    uint32_t timeout;
} wave_machine_t;

/* 高频录波全局结构体. */
typedef struct
{
    osThreadId_t handle;                    // ADC 任务句柄.
    SPI_HandleTypeDef *spi;                 // ADC 使用的 SPI 句柄.
    TIM_HandleTypeDef *tim;                 // ADC 使用的 TIMER 句柄.
    wave_machine_t state;                   // 状态机.
    uint32_t DAU_version;                   // DAU FPGA 软件版本号.
    uint32_t buf_wr[132];
    uint32_t buf_rd[132];
    uint32_t time_restart;                  // 录波失败后, 下次录波间隔.
    uint8_t is_9231;                        // 线程运行结果.
    uint8_t rt;                             // 线程运行结果.
} wave_ctrl_t;

/* Exported macro ------------------------------------------------------------*/
/* Extern global variables ---------------------------------------------------*/
extern wave_ctrl_t wave_ctrl;

/* Extern functions ----------------------------------------------------------*/
extern void wave_init_os(void);
extern void wave_show(void);
#endif
/******************* (C) COPYRIGHT LandPower ***** END OF FILE ****************/
