//----------------------------------------------------
/**
	\file		ctk/hw/Accel_MMA8453Q.h
	\ingroup	hardware
	\ingroup	all

	\brief		knihovna pro ovladani Digitalniho
				Akcelerometru,
				urceno k vlozeni do hwboard
				<<< ALL >>>

	\author		ConTeK s.r.o., Jakub Kastner
	\version	01.01
	\date		28.01.2013
*/
//----------------------------------------------------

#ifndef	_ACCEL_MMA8453Q_H_
#define _ACCEL_MMA8453Q_H_

#include <twim.h>
#include <arm7tdmi/inc/ctktypes.h>
#include <stmachine.h>


//----------------------------------------------------
/**
	\name 	defineice pro akcelerometr
*/
//----------------------------------------------------

#define ACCEL_SLAVE  0x1C	// adresa na TWI [6:0]

#define ACCEL_MAX_VALUE 0x3FF	// maximalni hodnota pro 10bit cislo



//----------------------------------------------------
//!	stavy automatu ovladace akcelerometru MMA8453Q
typedef enum _ACCEL_STATE{
	ACCEL_ST_INIT = STM_FIRST_USER_STATE, //
	ACCEL_ST_READ_XYZ,
	ACCEL_ST_SET_REG,
	ACCEL_ST_READ_REG,
	ACCEL_ST_NEW_DATA,
	ACCEL_ST_DO_NOTHING,
} ACCEL_STATE;

//----------------------------------------------------
//!	adresy registru akcelerometru MMA8453Q
typedef enum _ACCEL_REG{
	STATUS				=	0x00,	// R
	OUT_X_MSB			=	0x01,	// R
	OUT_X_LSB			=	0x02,	// R
	OUT_Y_MSB			=	0x03,	// R
	OUT_Y_LSB			=	0x04,	// R
	OUT_Z_MSB			=	0x05,	// R
	OUT_Z_LSB			=	0x06,	// R

	SYSMODE				=	0x0B,	// R
	INT_SOURCE			= 	0x0C,	// R
	WHO_AM_I			=	0x0D,	// R
	XYZ_DATA_CFG		=	0x0E,	// R/W
	HP_FILTER_CUTOFF	=	0x0F,	// R/W
	PL_STATUS			=	0x10,	// R
	PL_CFG				=	0x11,	// R/W
	PL_COUNT			=	0x12,	// R
	PL_BF_ZCOMP			=	0x13,	// R
	P_L_THS_REG			=	0x14,	// R/W
	FF_MT_CFG			=	0x15,	// R/W
	FF_MT_SCR			=	0x16,	// R
	FF_MT_THS			=	0x17,	// R/W
	FF_MT_COUNT			= 	0x18,	// R/W

	TRANSIENT_CFG		=	0x1D,	// R/W
	TRANSIENT_SRC		=	0x1E,	// R
	TRANSIENT_THS		=	0x1F,	// R/W
	TRNASIENT_COUNT		=	0x20,	// R/W
	PULSE_CFG			=	0x21,	// R/W
	PULSE_SRC			=	0x22,	// R
	PULSE_THSX			=	0x23,	// R/W
	PULSE_THSY			=	0x24,	// R/W
	PULSE_THSZ			=	0x25,	// R/W
	PULSE_TMLT			=	0x26,	// R/W
	PULSE_LTCY			=	0x27,	// R/W
	PULSE_WIND			=	0x28,	// R/W
	ASLP_COUNT			=	0x29,	// R/W
	CTRL_REG1			=	0x2A,	// R/W
	CTRL_REG2			=	0x2B,	// R/W
	CTRL_REG3			=	0x2C,	// R/W
	CTRL_REG4			=	0x2D,	// R/W
	CTRL_REG5			=	0x2E,	// R/W
	OFF_X				=	0x2F,	// R/W
	OFF_Y				=	0x30,	// R/W
	OFF_Z				=	0x31	// R/W
} ACCEL_REG;


//----------------------------------------------------
//!	Kontext automatu ovladace Digitalniho akcelerometru

typedef struct _ACCELEROMETER_CTX
{
	STMACHINE	Stm;

	TWIDESC		* Twi;
	bool_t 		TwiReady;

	uint8_t 		tmp[6];
	int16_t			XYZ[3];
	float_t			r,a;

	ACCEL_REG		ptrReg;
	uint8_t			data;
} ACCELEROMETER_CTX;


//----------------------------------------------------
/**
	\brief	Funkce pro inicializaci Akcelerometru

	\param	_ctx	kontext ovladace akceletometru
			_twi

	\return	TRUE	inicializace probehla v poradku
			FALSE	chyba
*/
//----------------------------------------------------

bool_t AccelInitialize( ACCELEROMETER_CTX * const _ctx, TWIDESC * const _twiCtx );


//----------------------------------------------------
/**
	\brief	zapsani dat od registru akcelerometru MMA8453Q

	\param	_ctx	kontext ovladace akceletometru
			_prtReg	Adresa registu
			_data	Data, kter8 se maji zapsat;
*/
//----------------------------------------------------

void AccelReadReg(ACCELEROMETER_CTX * const _ctx, ACCEL_REG const _prtReg, uint8_t const _data);


//----------------------------------------------------
/**
	\brief	tick knihovnou akcelerometru MMA8453Q

	\param	_ctx	kontext ovladace akceletometru
*/
//----------------------------------------------------

void AccelTick(ACCELEROMETER_CTX * const _ctx);


//----------------------------------------------------
/**
	\brief	Funkce pro odpojeni akcelerometru

	\param	_ctx	kontext ovladace akceletometru
*/
//----------------------------------------------------

void AccelFinalize(  ACCELEROMETER_CTX * const _ctx );

#endif //_ACCEL_MMA8453Q_H_


