/**
* @file TM7707_ATY.h
*
* @param Project DEVICE_GENERAL_ATY_LIB
*
* @author ATY
*
* @copyright
* - Copyright 2017 - 2023 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
*
* https://mengze.top/MZ-ATY_VCJS
* - CC 4.0 BY-NC-SA -
*
* https://creativecommons.org/licenses/by-nc-sa/4.0/
* - Your use will be deemed to have accepted the terms of this statement.
*
* @brief Familiar functions of TM7707 for all embedded device
*
* @version
* - 1_01_220601 > ATY
* -# Preliminary version, first Release
* - Undone
********************************************************************************
*/
#ifndef __TM7707_ATY_H
#define __TM7707_ATY_H
#include "INCLUDE_ATY.h"
/******************************* For user *************************************/
#include "bt.h"
#include "gpio.h"
#include "flash.h"
#define TM7707_MCLKIN_PORT GpioPortB
#define TM7707_MCLKIN_PIN GpioPin14 // TIM3_CH1B / TIM0_CHA
#define TM7707_SCLK_PORT GpioPortB
#define TM7707_SCLK_PIN GpioPin15
#define TM7707_CS_PORT GpioPortB
#define TM7707_CS_PIN GpioPin13
#define TM7707_RESET_PORT GpioPortB
#define TM7707_RESET_PIN GpioPin12
#define TM7707_DIN_PORT GpioPortA
#define TM7707_DIN_PIN GpioPin10
#define TM7707_DOUT_PORT GpioPortA
#define TM7707_DOUT_PIN GpioPin9
#define TM7707_DRDY_PORT GpioPortA
#define TM7707_DRDY_PIN GpioPin8
#define TM7707_REFEN_PORT GpioPortA
#define TM7707_REFEN_PIN GpioPin11
#define TM7707_DATA_GROUP_SIZE 12
void TM7707_GPIO_Init(void);
#define TIM_COUNT_WHOLE 0x0001 // 1MHz // 0x07d0 - 2000 1KHz
#define TIM_COUNT_HALF 0x0000
void TM7707_MCLK_Init(void);
/******************************************************************************/
#define TM7707_REFEN_SET_H GPIO_SET_H(TM7707_REFEN_PORT, TM7707_REFEN_PIN)
#define TM7707_REFEN_SET_L GPIO_SET_L(TM7707_REFEN_PORT, TM7707_REFEN_PIN)
#define TM7707_RESET_SET_H GPIO_SET_H(TM7707_RESET_PORT, TM7707_RESET_PIN)
#define TM7707_RESET_SET_L GPIO_SET_L(TM7707_RESET_PORT, TM7707_RESET_PIN)
#define TM7707_CS_SET_H GPIO_SET_H(TM7707_CS_PORT, TM7707_CS_PIN)
#define TM7707_CS_SET_L GPIO_SET_L(TM7707_CS_PORT, TM7707_CS_PIN)
#define TM7707_SCLK_SET_H GPIO_SET_H(TM7707_SCLK_PORT, TM7707_SCLK_PIN)
#define TM7707_SCLK_SET_L GPIO_SET_L(TM7707_SCLK_PORT, TM7707_SCLK_PIN)
#define TM7707_DIN_SET_H GPIO_SET_H(TM7707_DIN_PORT, TM7707_DIN_PIN)
#define TM7707_DIN_SET_L GPIO_SET_L(TM7707_DIN_PORT, TM7707_DIN_PIN)
#define TM7707_DRDY_GET_H GPIO_GET_H(TM7707_DRDY_PORT, TM7707_DRDY_PIN)
#define TM7707_DRDY_GET_L GPIO_GET_L(TM7707_DRDY_PORT, TM7707_DRDY_PIN)
#define TM7707_DOUT_GET_H GPIO_GET_H(TM7707_DOUT_PORT, TM7707_DOUT_PIN)
#define TM7707_DOUT_GET_L GPIO_GET_L(TM7707_DOUT_PORT, TM7707_DOUT_PIN)
// Communication register
#define TM7707_REG_COMM (0<<4) // Communication register
#define TM7707_REG_SETUP (1<<4) // Setting register
#define TM7707_REG_FILTER_H (2<<4) // Filter(high) register
#define TM7707_REG_DATA (3<<4) // Data register
#define TM7707_REG_TEST (4<<4) // Test register
#define TM7707_REG_FILTER_L (5<<4) // Filter(low) register
#define TM7707_REG_ZERO_CAL (6<<4) // Zero cal register
#define TM7707_REG_FULL_CAL (7<<4) // Full cal register
#define TM7707_WRITE (0<<3) // Write in next
#define TM7707_READ (1<<3) // Read in next
#define TM7707_STBY_MODE (1<<2) // Stanby mode, Cannot write to device register.
#define TM7707_WORK_MODE (0<<2) // Work mode, Cannot write to device register.
#define TM7707_CH_1 (0<<0) // AIN1+ AIN1-
#define TM7707_CH_2 (1<<0) // AIN2+ AIN2-
#define TM7707_CH_3 (2<<0) // AIN1- AIN1-
#define TM7707_CH_4 (3<<0) // AIN1- AIN2-
// Setting register (Reset 0x01)
#define TM7707_MD_NORMAL (0<<6) // Normal
#define TM7707_MD_CAL_SELF (1<<6) // Self calibrate
#define TM7707_MD_CAL_ZERO (2<<6) // Calibrate Zero
#define TM7707_MD_CAL_FULL (3<<6) // Calibrate Full
#define TM7707_GAIN_1 (0<<3) // Gain x1
#define TM7707_GAIN_2 (1<<3) // Gain x2
#define TM7707_GAIN_4 (2<<3) // Gain x4
#define TM7707_GAIN_8 (3<<3) // Gain x8
#define TM7707_GAIN_16 (4<<3) // Gain x16
#define TM7707_GAIN_32 (5<<3) // Gain x32
#define TM7707_GAIN_64 (6<<3) // Gain x64
#define TM7707_GAIN_128 (7<<3) // Gain x128
#define TM7707_BIPOLAR (0<<2) // Bipolar input
#define TM7707_UNIPOLAR (1<<2) // Unipolar input
#define TM7707_BUF_NO (0<<1) // Non-buffered input
#define TM7707_BUF_EN (1<<1) // Buffered input
#define TM7707_FSYNC_0 (0<<0) // Filter no sync
#define TM7707_FSYNC_1 (1<<0) // Filter sync
// Filter high register (Reset 0x05)
#define TM7707_BST_0 (0<<5) // Front current low
#define TM7707_BST_1 (1<<5) // Front current high
#define TM7707_CLKDIS_OUT0 (0<<4) // Clock out disable (safe power)
#define TM7707_CLKDIS_OUT1 (1<<4) // Clock out enable
// Filter low register (Reset 0x00)
// Data register
// Test register (Reset 0x00)
// Zero cal register (Reset 0x1F4000)
// Full cal register register (Reset 0x5761AB)
// Legacy info regester
#define TM7707_CLK_2_4576M (0<<2) // 2.4576M
#define TM7707_CLK_1M (1<<2) // 1M
#define TM7707_CLK_4_9152M (2<<2) // 4.9152M
#define TM7707_CLK_2M (3<<2) // 2M
#define TM7707_ZERO_0 0x0000
#define TM7707_ZERO_1 0x0080
#define TM7707_FS_50HZ 0x0000
#define TM7707_FS_60HZ 0x0100
#define TM7707_FS_250HZ 0x0200
#define TM7707_FS_500HZ 0x0400
#define TM7707_FS_DEFAULT 0x0F00
void TM7707_WriteByte(uint8_t data_t);
uint8_t TM7707_ReadByte(void);
uint32_t TM7707_ReadData(void);
float TM7707_VoltageCalc(float refVoltage, uint32_t dataIn);
void TM7707_Init(void);
void TM7707_Test(void);
#endif /* __TM7707_ATY_H */
/******************************** End Of File *********************************/