| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- /**
- * @file TM7707_ATY.c
- *
- * @param Project DEVICE_GENERAL_ATY_LIB
- *
- * @author ATY
- *
- * @copyright
- * - Copyright 2017 - 2026 MZ-ATY
- * - This code follows:
- * - MZ-ATY Various Contents Joint Statement -
- * <a href="https://mengze.top/MZ-ATY_VCJS">
- * https://mengze.top/MZ-ATY_VCJS</a>
- * - CC 4.0 BY-NC-SA -
- * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
- * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
- * - Your use will be deemed to have accepted the terms of this statement.
- *
- * @brief functions of TM7707 for C platform
- *
- * @version
- * - 1_01_220601 > ATY
- * -# Preliminary version, first Release
- * - todo
- ********************************************************************************
- */
- #ifndef __TM7707_ATY_C
- #define __TM7707_ATY_C
- #include "TM7707_ATY.h"
- /******************************* For user *************************************/
- void TM7707_GPIO_Init(void)
- {
- stc_gpio_config_t pstcGpioCfg;
- Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
- pstcGpioCfg.enDir = GpioDirOut;
- pstcGpioCfg.enDrv = GpioDrvH;
- pstcGpioCfg.enPuPd = GpioPu;
- pstcGpioCfg.enOD = GpioOdDisable;
- pstcGpioCfg.enCtrlMode = GpioAHB;
- Gpio_Init(TM7707_SCLK_PORT, TM7707_SCLK_PIN, &pstcGpioCfg);
- Gpio_Init(TM7707_CS_PORT, TM7707_CS_PIN, &pstcGpioCfg);
- Gpio_Init(TM7707_RESET_PORT, TM7707_RESET_PIN, &pstcGpioCfg);
- Gpio_Init(TM7707_DIN_PORT, TM7707_DIN_PIN, &pstcGpioCfg);
- Gpio_Init(TM7707_REFEN_PORT, TM7707_REFEN_PIN, &pstcGpioCfg);
- pstcGpioCfg.enDir = GpioDirIn;
- Gpio_Init(TM7707_DOUT_PORT, TM7707_DOUT_PIN, &pstcGpioCfg);
- Gpio_Init(TM7707_DRDY_PORT, TM7707_DRDY_PIN, &pstcGpioCfg);
- }
- void TM7707_MCLK_Init(void)
- {
- // uint32_t pclFrc = Sysctrl_GetPClkFreq();
- uint16_t u16ArrValue;
- uint16_t u16CntValue;
- uint16_t u16CompareAValue;
- uint8_t u8ValidPeriod;
- stc_sysctrl_clk_config_t stcCfg;
- stc_gpio_config_t stcTIM0Port;
- stc_bt_mode23_config_t stcBtBaseCfg;
- stc_bt_m23_compare_config_t stcBtPortCmpCfg;
- DDL_ZERO_STRUCT(stcCfg);
- DDL_ZERO_STRUCT(stcTIM0Port);
- DDL_ZERO_STRUCT(stcBtBaseCfg);
- DDL_ZERO_STRUCT(stcBtPortCmpCfg);
- ///< 开启FLASH外设时钟
- Sysctrl_SetPeripheralGate(SysctrlPeripheralFlash, TRUE);
- ///<========================== 时钟初始化配置 ===================================
- ///< 因要使用的时钟源HCLK小于24M:此处设置FLASH 读等待周期为0 cycle(默认值也为0 cycle)
- Flash_WaitCycle(FlashWaitCycle0);
- ///< 选择内部RCH作为HCLK时钟源;
- stcCfg.enClkSrc = SysctrlClkRCH;
- ///< HCLK SYSCLK/1
- stcCfg.enHClkDiv = SysctrlHclkDiv1;
- ///< PCLK 为HCLK/1
- stcCfg.enPClkDiv = SysctrlPclkDiv1;
- ///< 时钟初始化前,优先设置要使用的时钟源:此处设置RCH为4MHz
- Sysctrl_SetRCHTrim(SysctrlRchFreq4MHz);
- ///< 系统时钟初始化
- Sysctrl_ClkInit(&stcCfg);
- Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE); //GPIO 外设时钟使能
- Sysctrl_SetPeripheralGate(SysctrlPeripheralBTim, TRUE); //Base Timer外设时钟使能
- stcTIM0Port.enDir = GpioDirOut;
- Gpio_Init(TM7707_MCLKIN_PORT, TM7707_MCLKIN_PIN, &stcTIM0Port);
- Gpio_SetAfMode(TM7707_MCLKIN_PORT, TM7707_MCLKIN_PIN, GpioAf4); //PA00设置为TIM0_CHA
- stcBtBaseCfg.enWorkMode = BtWorkMode3; //锯齿波模式
- stcBtBaseCfg.enCT = BtTimer; //定时器功能,计数时钟为内部PCLK
- stcBtBaseCfg.enPRS = BtPCLKDiv1; //PCLK
- stcBtBaseCfg.enCntDir = BtCntUp; //向上计数,在三角波模式时只读
- stcBtBaseCfg.enPWMTypeSel = BtIndependentPWM; //独立输出PWM
- stcBtBaseCfg.enPWM2sSel = BtSinglePointCmp; //单点比较功能
- stcBtBaseCfg.bOneShot = FALSE; //循环计数
- stcBtBaseCfg.bURSSel = FALSE; //上下溢更新
- // stcBtBaseCfg.pfnTim0Cb = Tim0Int; //中断函数入口
- Bt_Mode23_Init(TIM0, &stcBtBaseCfg); //TIM0 的模式2功能初始化
- u16ArrValue = TIM_COUNT_WHOLE;
- Bt_M23_ARRSet(TIM0, u16ArrValue, TRUE); //设置重载值,并使能缓存
- u16CompareAValue = TIM_COUNT_HALF;
- Bt_M23_CCR_Set(TIM0, BtCCR0A, u16CompareAValue); //设置比较值A
- stcBtPortCmpCfg.enCH0ACmpCtrl = BtCMPInverse; //OCREFA输出控制OCMA:PWM模式2
- stcBtPortCmpCfg.enCH0APolarity = BtPortPositive; //正常输出
- stcBtPortCmpCfg.bCh0ACmpBufEn = TRUE; //A通道缓存控制
- stcBtPortCmpCfg.enCh0ACmpIntSel = BtCmpIntNone; //A通道比较控制:无
- Bt_M23_PortOutput_Config(TIM0, &stcBtPortCmpCfg); //比较输出端口配置
- u8ValidPeriod = 0; //事件更新周期设置,0表示锯齿波每个周期更新一次,每+1代表延迟1个周期
- Bt_M23_SetValidPeriod(TIM0, u8ValidPeriod); //间隔周期设置
- u16CntValue = 0;
- Bt_M23_Cnt16Set(TIM0, u16CntValue); //设置计数初值
- // Bt_ClearAllIntFlag(TIM0); //清中断标志
- // EnableNvic(TIM0_IRQn, IrqLevel0, TRUE); //TIM0中断使能
- // Bt_Mode23_EnableIrq(TIM0, BtUevIrq); //使能TIM0 UEV更新中断
- Bt_M23_EnPWM_Output(TIM0, TRUE, FALSE); //TIM0 端口输出使能
- Bt_M23_Run(TIM0); //TIM0 运行
- }
- /******************************************************************************/
- /**
- * @brief Write one byte to TM7707
- * @param data_t data to write
- */
- void TM7707_WriteByte(uint8_t data_t)
- {
- // set CS low to selet chip enable
- TM7707_CS_SET_L;
- for(uint8_t i = 0; i < 8; i++)
- {
- // set SCLK low to start write
- TM7707_SCLK_SET_L;
- DelayUs(10);
- // write high bit first
- if(data_t & 0x80)
- TM7707_DIN_SET_H;
- else
- TM7707_DIN_SET_L;
- DelayUs(10);
- // set SCLK high, write one byte over
- TM7707_SCLK_SET_H;
- DelayUs(10);
- data_t <<= 1;
- }
- // set CS high after write one byte
- TM7707_CS_SET_H;
- DelayUs(10);
- }
- /**
- * @brief Read one byte from TM7707
- * @return on byte data of read
- */
- uint8_t TM7707_ReadByte(void)
- {
- uint8_t temp_uint8;
- TM7707_CS_SET_L;
- for(uint8_t i = 0; i < 8; i++)
- {
- TM7707_SCLK_SET_L;
- DelayUs(10);
- temp_uint8 = (temp_uint8 << 1) | TM7707_DOUT_GET_H; // | 0 = no change
- TM7707_SCLK_SET_H;
- DelayUs(10);
- }
- TM7707_CS_SET_H;
- DelayUs(10);
- return temp_uint8;
- }
- /**
- * @brief Read data from TM7707 Data Register once
- * @return 24 bit data of read
- */
- uint32_t TM7707_ReadData(void)
- {
- uint32_t temp_uint32 = 0;
- // ready to read while DRDY pin cahnge to low
- while(TM7707_DRDY_GET_H);
- // set next operation is read data regsiter, selet TM7707_CH_1
- TM7707_WriteByte(TM7707_REG_COMM | TM7707_READ | TM7707_REG_DATA | TM7707_CH_1);
- // read 8 bit every time, 24 bit total
- for(uint8_t i = 0; i < 3; i++)
- {
- temp_uint32 |= (TM7707_ReadByte() << ((2 - i) * 8));
- }
- return temp_uint32;
- }
- /**
- * @brief Calculate voltage from 24 bit data
- * @param refVoltage reference voltage at TM7707 REF_IN+ pin
- * @param dataIn 24 bit data from: uint32_t TM7707_ReadData(void)
- * @return voltage data
- */
- float TM7707_VoltageCalc(float refVoltage, uint32_t dataIn)
- {
- return refVoltage * dataIn / 16777216;
- }
- /**
- * @brief Init TM7707
- */
- void TM7707_Init(void)
- {
- TM7707_GPIO_Init();
- // generate >=1MHz frequence for TM7707 main clock
- TM7707_MCLK_Init();
- #ifdef __DEBUG_TM7707_ATY
- printf("\r\nTM7707 GPIO & MCLK init done!\r\n");
- #endif /* __DEBUG_TM7707_ATY */
- TM7707_REFEN_SET_H;
- TM7707_SCLK_SET_H;
- TM7707_CS_SET_H;
- TM7707_RESET_SET_L;
- DelayMs(10);
- TM7707_RESET_SET_H;
- DelayMs(10);
- // TM7707_DRDY_H;
- #ifdef __DEBUG_TM7707_ATY
- printf("\r\nTM7707 Resetting & Initializing...\r\n");
- #endif /* __DEBUG_TM7707_ATY */
- TM7707_DIN_SET_H;
- for(uint8_t i = 0; i < 40; i++)
- {
- TM7707_SCLK_SET_L;
- DelayUs(10);
- TM7707_SCLK_SET_H;
- DelayUs(10);
- }
- #ifdef __DEBUG_TM7707_ATY
- printf("\r\nTM7707 reset done!\r\n");
- #endif /* __DEBUG_TM7707_ATY */
- // set next operation is write filter low regsiter, selet TM7707_CH_1
- TM7707_WriteByte(TM7707_REG_COMM | TM7707_WRITE | TM7707_REG_FILTER_L);
- TM7707_WriteByte(0x00);
- DelayMs(10);
- // set next operation is write filter high regsiter, selet TM7707_CH_1
- TM7707_WriteByte(TM7707_REG_COMM | TM7707_WRITE | TM7707_REG_FILTER_H);
- TM7707_WriteByte(0x0C);
- DelayMs(10);
- // set next operation is write setting regsiter, selet TM7707_CH_1
- TM7707_WriteByte(TM7707_REG_COMM | TM7707_WRITE | TM7707_REG_SETUP | TM7707_CH_1);
- // calibration self, polarity / magnification and buffer shall also be set during calibration
- TM7707_WriteByte(TM7707_GAIN_64 | TM7707_UNIPOLAR | TM7707_MD_CAL_SELF | TM7707_BUF_EN);
- while(TM7707_DRDY_GET_H);
- DelayMs(10);
- // TM7707_WriteByte(TM7707_REG_COMM | TM7707_WRITE | TM7707_REG_SETUP | TM7707_CH_2);
- // TM7707_WriteByte(TM7707_GAIN_1 | TM7707_UNIPOLAR | TM7707_MD_CAL_SELF | TM7707_BUF_EN);
- // TM7707_WriteByte(TM7707_GAIN_1 | TM7707_BIPOLAR | TM7707_MD_CAL_SELF | TM7707_BUF_NO);
- // while(TM7707_DRDY_GET_H);
- // DelayMs(10);
- // set next operation is write setting regsiter, selet TM7707_CH_1
- TM7707_WriteByte(TM7707_REG_COMM | TM7707_WRITE | TM7707_REG_SETUP | TM7707_CH_1);
- // set magnification 1, unipolar input, enable input buffer
- // TM7707_BIPOLAR read acture voltage in AIN1+?, TM7707_UNIPOLAR read (AIN1+) - (AIN1-)
- TM7707_WriteByte(TM7707_GAIN_64 | TM7707_UNIPOLAR | TM7707_BUF_EN);
- DelayMs(10);
- }
- /**
- * @brief TM7707 test
- */
- void TM7707_Test(void)
- {
- uint32_t tm7707Data24[TM7707_DATA_GROUP_SIZE];
- uint32_t tm7707Data24Temp;
- for(uint8_t i = 0; i < TM7707_DATA_GROUP_SIZE; i++)
- {
- printf("\r\n%02d: ", i);
- tm7707Data24[i] = TM7707_ReadData();
- printf("CH1: %06x / %d \r\n",
- tm7707Data24[i], tm7707Data24[i]);
- }
- ALGO_AverageInDelExtremum(uint32_t, 1, tm7707Data24, tm7707Data24Temp);
- printf("\r\nCH1 Averange Data: %d\r\n", tm7707Data24Temp);
- printf("\r\nCH1 Averange Voltage: %1.6f V\r\n",
- TM7707_VoltageCalc(1.2093, tm7707Data24Temp));
- printf("\r\n");
- }
- // todo: not tested
- #endif /* __TM7707_ATY_C */
- /******************************** End Of File *********************************/
|