/*Includes ----------------------------------------------*/
#include "tool.h"
#include "bsp_time.h"
#include "nrf_delay.h"
#include "bll_imu.h"
#include "hal_qma.h"
#include "exception.h"
#include "system.h"

static uint8_t QmaSetFlag =0;//Qma配置成功标志位

/*API --------------------------------------------------------------*/

static void hal_qma_Porcess(void)
{
	static uint8_t state=0;
	switch(state){
		case 0:
			    if(0 == QmaSetFlag){
						 drv_qma_power_off();
						 state =1;
					}
		      break;
	  case 1:
			    if(0 == drv_qma_power_on()){
					  state =2;
					}
					else{
						Except_TxError(EXCEPT_QMA7981,"QMA7981 set fail");
						DEBUG_LOG("qma7981 config FAIL !\r\n");
						state =1;
					}
			    break;
	  case 2:
			    if(0 == drv_qma_set_acc_odr(QMA_ACC_ODR_12HZ5)){
						QmaSetFlag =1;
						DEBUG_LOG("qma7981 config success !\r\n");
						Except_TxError(EXCEPT_QMA7981,"QMA7981 set ok");
					}else{
						DEBUG_LOG("qma7981 config FAIL !\r\n");
						Except_TxError(EXCEPT_QMA7981,"QMA7981 set fail");
					}
					state =0;
			    break;
		default:state =0;break;
	}
}

uint8_t hal_qma_get_state(void){
	 return QmaSetFlag;
}

int	hal_qma_Init(void)
{
	int ret = -1;
	
	ret = drv_qma_Init();
	
	nrf_delay_ms(20);
	
	ret += drv_qma_set_acc_odr(QMA_ACC_ODR_12HZ5);

	if(ret < 0)ret = drv_qma_set_acc_odr(QMA_ACC_ODR_12HZ5);//再配置一次

	Process_Start(1000,"qma_err",hal_qma_Porcess);
	if(ret == 0){
		QmaSetFlag =1;
		DEBUG_LOG("qma7981 config success !\r\n");
	}
	else{
		QmaSetFlag = 0;
		DEBUG_LOG("qma7981 config error:%d !\r\n",ret);
	}

	ret = ret < 0 ? -1 : 0;
	
	return ret;
}








