TM7707_ATY.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. * @file TM7707_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of TM7707 for all embedded device
  20. *
  21. * @version
  22. * - 1_01_220601 > ATY
  23. * -# Preliminary version, first Release
  24. * - Undone
  25. ********************************************************************************
  26. */
  27. #ifndef __TM7707_ATY_C
  28. #define __TM7707_ATY_C
  29. #include "TM7707_ATY.h"
  30. /******************************* For user *************************************/
  31. void TM7707_GPIO_Init(void)
  32. {
  33. stc_gpio_config_t pstcGpioCfg;
  34. Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
  35. pstcGpioCfg.enDir = GpioDirOut;
  36. pstcGpioCfg.enDrv = GpioDrvH;
  37. pstcGpioCfg.enPuPd = GpioPu;
  38. pstcGpioCfg.enOD = GpioOdDisable;
  39. pstcGpioCfg.enCtrlMode = GpioAHB;
  40. Gpio_Init(TM7707_SCLK_PORT, TM7707_SCLK_PIN, &pstcGpioCfg);
  41. Gpio_Init(TM7707_CS_PORT, TM7707_CS_PIN, &pstcGpioCfg);
  42. Gpio_Init(TM7707_RESET_PORT, TM7707_RESET_PIN, &pstcGpioCfg);
  43. Gpio_Init(TM7707_DIN_PORT, TM7707_DIN_PIN, &pstcGpioCfg);
  44. Gpio_Init(TM7707_REFEN_PORT, TM7707_REFEN_PIN, &pstcGpioCfg);
  45. pstcGpioCfg.enDir = GpioDirIn;
  46. Gpio_Init(TM7707_DOUT_PORT, TM7707_DOUT_PIN, &pstcGpioCfg);
  47. Gpio_Init(TM7707_DRDY_PORT, TM7707_DRDY_PIN, &pstcGpioCfg);
  48. }
  49. void TM7707_MCLK_Init(void)
  50. {
  51. // uint32_t pclFrc = Sysctrl_GetPClkFreq();
  52. uint16_t u16ArrValue;
  53. uint16_t u16CntValue;
  54. uint16_t u16CompareAValue;
  55. uint8_t u8ValidPeriod;
  56. stc_sysctrl_clk_config_t stcCfg;
  57. stc_gpio_config_t stcTIM0Port;
  58. stc_bt_mode23_config_t stcBtBaseCfg;
  59. stc_bt_m23_compare_config_t stcBtPortCmpCfg;
  60. DDL_ZERO_STRUCT(stcCfg);
  61. DDL_ZERO_STRUCT(stcTIM0Port);
  62. DDL_ZERO_STRUCT(stcBtBaseCfg);
  63. DDL_ZERO_STRUCT(stcBtPortCmpCfg);
  64. ///< 开启FLASH外设时钟
  65. Sysctrl_SetPeripheralGate(SysctrlPeripheralFlash, TRUE);
  66. ///<========================== 时钟初始化配置 ===================================
  67. ///< 因要使用的时钟源HCLK小于24M:此处设置FLASH 读等待周期为0 cycle(默认值也为0 cycle)
  68. Flash_WaitCycle(FlashWaitCycle0);
  69. ///< 选择内部RCH作为HCLK时钟源;
  70. stcCfg.enClkSrc = SysctrlClkRCH;
  71. ///< HCLK SYSCLK/1
  72. stcCfg.enHClkDiv = SysctrlHclkDiv1;
  73. ///< PCLK 为HCLK/1
  74. stcCfg.enPClkDiv = SysctrlPclkDiv1;
  75. ///< 时钟初始化前,优先设置要使用的时钟源:此处设置RCH为4MHz
  76. Sysctrl_SetRCHTrim(SysctrlRchFreq4MHz);
  77. ///< 系统时钟初始化
  78. Sysctrl_ClkInit(&stcCfg);
  79. Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE); //GPIO 外设时钟使能
  80. Sysctrl_SetPeripheralGate(SysctrlPeripheralBTim, TRUE); //Base Timer外设时钟使能
  81. stcTIM0Port.enDir = GpioDirOut;
  82. Gpio_Init(TM7707_MCLKIN_PORT, TM7707_MCLKIN_PIN, &stcTIM0Port);
  83. Gpio_SetAfMode(TM7707_MCLKIN_PORT, TM7707_MCLKIN_PIN, GpioAf4); //PA00设置为TIM0_CHA
  84. stcBtBaseCfg.enWorkMode = BtWorkMode3; //锯齿波模式
  85. stcBtBaseCfg.enCT = BtTimer; //定时器功能,计数时钟为内部PCLK
  86. stcBtBaseCfg.enPRS = BtPCLKDiv1; //PCLK
  87. stcBtBaseCfg.enCntDir = BtCntUp; //向上计数,在三角波模式时只读
  88. stcBtBaseCfg.enPWMTypeSel = BtIndependentPWM; //独立输出PWM
  89. stcBtBaseCfg.enPWM2sSel = BtSinglePointCmp; //单点比较功能
  90. stcBtBaseCfg.bOneShot = FALSE; //循环计数
  91. stcBtBaseCfg.bURSSel = FALSE; //上下溢更新
  92. // stcBtBaseCfg.pfnTim0Cb = Tim0Int; //中断函数入口
  93. Bt_Mode23_Init(TIM0, &stcBtBaseCfg); //TIM0 的模式2功能初始化
  94. u16ArrValue = TIM_COUNT_WHOLE;
  95. Bt_M23_ARRSet(TIM0, u16ArrValue, TRUE); //设置重载值,并使能缓存
  96. u16CompareAValue = TIM_COUNT_HALF;
  97. Bt_M23_CCR_Set(TIM0, BtCCR0A, u16CompareAValue); //设置比较值A
  98. stcBtPortCmpCfg.enCH0ACmpCtrl = BtCMPInverse; //OCREFA输出控制OCMA:PWM模式2
  99. stcBtPortCmpCfg.enCH0APolarity = BtPortPositive; //正常输出
  100. stcBtPortCmpCfg.bCh0ACmpBufEn = TRUE; //A通道缓存控制
  101. stcBtPortCmpCfg.enCh0ACmpIntSel = BtCmpIntNone; //A通道比较控制:无
  102. Bt_M23_PortOutput_Config(TIM0, &stcBtPortCmpCfg); //比较输出端口配置
  103. u8ValidPeriod = 0; //事件更新周期设置,0表示锯齿波每个周期更新一次,每+1代表延迟1个周期
  104. Bt_M23_SetValidPeriod(TIM0, u8ValidPeriod); //间隔周期设置
  105. u16CntValue = 0;
  106. Bt_M23_Cnt16Set(TIM0, u16CntValue); //设置计数初值
  107. // Bt_ClearAllIntFlag(TIM0); //清中断标志
  108. // EnableNvic(TIM0_IRQn, IrqLevel0, TRUE); //TIM0中断使能
  109. // Bt_Mode23_EnableIrq(TIM0, BtUevIrq); //使能TIM0 UEV更新中断
  110. Bt_M23_EnPWM_Output(TIM0, TRUE, FALSE); //TIM0 端口输出使能
  111. Bt_M23_Run(TIM0); //TIM0 运行
  112. }
  113. /******************************************************************************/
  114. /**
  115. * @brief Write one byte to TM7707
  116. * @param data_t data to write
  117. */
  118. void TM7707_WriteByte(uint8_t data_t)
  119. {
  120. // set CS low to selet chip enable
  121. TM7707_CS_SET_L;
  122. for(uint8_t i = 0; i < 8; i++)
  123. {
  124. // set SCLK low to start write
  125. TM7707_SCLK_SET_L;
  126. DelayUs(10);
  127. // write high bit first
  128. if(data_t & 0x80)
  129. TM7707_DIN_SET_H;
  130. else
  131. TM7707_DIN_SET_L;
  132. DelayUs(10);
  133. // set SCLK high, write one byte over
  134. TM7707_SCLK_SET_H;
  135. DelayUs(10);
  136. data_t <<= 1;
  137. }
  138. // set CS high after write one byte
  139. TM7707_CS_SET_H;
  140. DelayUs(10);
  141. }
  142. /**
  143. * @brief Read one byte from TM7707
  144. * @return on byte data of read
  145. */
  146. uint8_t TM7707_ReadByte(void)
  147. {
  148. uint8_t temp_uint8;
  149. TM7707_CS_SET_L;
  150. for(uint8_t i = 0; i < 8; i++)
  151. {
  152. TM7707_SCLK_SET_L;
  153. DelayUs(10);
  154. temp_uint8 = (temp_uint8 << 1) | TM7707_DOUT_GET_H; // | 0 = no change
  155. TM7707_SCLK_SET_H;
  156. DelayUs(10);
  157. }
  158. TM7707_CS_SET_H;
  159. DelayUs(10);
  160. return temp_uint8;
  161. }
  162. /**
  163. * @brief Read data from TM7707 Data Register once
  164. * @return 24 bit data of read
  165. */
  166. uint32_t TM7707_ReadData(void)
  167. {
  168. uint32_t temp_uint32 = 0;
  169. // ready to read while DRDY pin cahnge to low
  170. while(TM7707_DRDY_GET_H);
  171. // set next operation is read data regsiter, selet TM7707_CH_1
  172. TM7707_WriteByte(TM7707_REG_COMM | TM7707_READ | TM7707_REG_DATA | TM7707_CH_1);
  173. // read 8 bit every time, 24 bit total
  174. for(uint8_t i = 0; i < 3; i++)
  175. {
  176. temp_uint32 |= (TM7707_ReadByte() << ((2 - i) * 8));
  177. }
  178. return temp_uint32;
  179. }
  180. /**
  181. * @brief Calculate voltage from 24 bit data
  182. * @param refVoltage reference voltage at TM7707 REF_IN+ pin
  183. * @param dataIn 24 bit data from: uint32_t TM7707_ReadData(void)
  184. * @return voltage data
  185. */
  186. float TM7707_VoltageCalc(float refVoltage, uint32_t dataIn)
  187. {
  188. return refVoltage * dataIn / 16777216;
  189. }
  190. /**
  191. * @brief Init TM7707
  192. */
  193. void TM7707_Init(void)
  194. {
  195. TM7707_GPIO_Init();
  196. // generate >=1MHz frequence for TM7707 main clock
  197. TM7707_MCLK_Init();
  198. #ifdef __DEBUG_TM7707_ATY
  199. printf("\r\nTM7707 GPIO & MCLK init done!\r\n");
  200. #endif /* __DEBUG_TM7707_ATY */
  201. TM7707_REFEN_SET_H;
  202. TM7707_SCLK_SET_H;
  203. TM7707_CS_SET_H;
  204. TM7707_RESET_SET_L;
  205. DelayMs(10);
  206. TM7707_RESET_SET_H;
  207. DelayMs(10);
  208. // TM7707_DRDY_H;
  209. #ifdef __DEBUG_TM7707_ATY
  210. printf("\r\nTM7707 Resetting & Initializing...\r\n");
  211. #endif /* __DEBUG_TM7707_ATY */
  212. TM7707_DIN_SET_H;
  213. for(uint8_t i = 0; i < 40; i++)
  214. {
  215. TM7707_SCLK_SET_L;
  216. DelayUs(10);
  217. TM7707_SCLK_SET_H;
  218. DelayUs(10);
  219. }
  220. #ifdef __DEBUG_TM7707_ATY
  221. printf("\r\nTM7707 reset done!\r\n");
  222. #endif /* __DEBUG_TM7707_ATY */
  223. // set next operation is write filter low regsiter, selet TM7707_CH_1
  224. TM7707_WriteByte(TM7707_REG_COMM | TM7707_WRITE | TM7707_REG_FILTER_L);
  225. TM7707_WriteByte(0x00);
  226. DelayMs(10);
  227. // set next operation is write filter high regsiter, selet TM7707_CH_1
  228. TM7707_WriteByte(TM7707_REG_COMM | TM7707_WRITE | TM7707_REG_FILTER_H);
  229. TM7707_WriteByte(0x0C);
  230. DelayMs(10);
  231. // set next operation is write setting regsiter, selet TM7707_CH_1
  232. TM7707_WriteByte(TM7707_REG_COMM | TM7707_WRITE | TM7707_REG_SETUP | TM7707_CH_1);
  233. // calibration self, polarity / magnification and buffer shall also be set during calibration
  234. TM7707_WriteByte(TM7707_GAIN_64 | TM7707_UNIPOLAR | TM7707_MD_CAL_SELF | TM7707_BUF_EN);
  235. while(TM7707_DRDY_GET_H);
  236. DelayMs(10);
  237. // TM7707_WriteByte(TM7707_REG_COMM | TM7707_WRITE | TM7707_REG_SETUP | TM7707_CH_2);
  238. // TM7707_WriteByte(TM7707_GAIN_1 | TM7707_UNIPOLAR | TM7707_MD_CAL_SELF | TM7707_BUF_EN);
  239. // TM7707_WriteByte(TM7707_GAIN_1 | TM7707_BIPOLAR | TM7707_MD_CAL_SELF | TM7707_BUF_NO);
  240. // while(TM7707_DRDY_GET_H);
  241. // DelayMs(10);
  242. // set next operation is write setting regsiter, selet TM7707_CH_1
  243. TM7707_WriteByte(TM7707_REG_COMM | TM7707_WRITE | TM7707_REG_SETUP | TM7707_CH_1);
  244. // set magnification 1, unipolar input, enable input buffer
  245. // TM7707_BIPOLAR read acture voltage in AIN1+?, TM7707_UNIPOLAR read (AIN1+) - (AIN1-)
  246. TM7707_WriteByte(TM7707_GAIN_64 | TM7707_UNIPOLAR | TM7707_BUF_EN);
  247. DelayMs(10);
  248. }
  249. /**
  250. * @brief TM7707 test
  251. */
  252. void TM7707_Test(void)
  253. {
  254. uint32_t tm7707Data24[TM7707_DATA_GROUP_SIZE];
  255. uint32_t tm7707Data24Temp;
  256. for(uint8_t i = 0; i < TM7707_DATA_GROUP_SIZE; i++)
  257. {
  258. printf("\r\n%02d: ", i);
  259. tm7707Data24[i] = TM7707_ReadData();
  260. printf("CH1: %06x / %u \r\n",
  261. tm7707Data24[i], tm7707Data24[i]);
  262. }
  263. ALGO_AverageInDelExtremum(uint32_t, 1, tm7707Data24, tm7707Data24Temp);
  264. printf("\r\nCH1 Averange Data: %d\r\n", tm7707Data24Temp);
  265. printf("\r\nCH1 Averange Voltage: %1.6f V\r\n",
  266. TM7707_VoltageCalc(1.2093, tm7707Data24Temp));
  267. printf("\r\n");
  268. }
  269. // todo: not tested
  270. #endif /* __TM7707_ATY_C */
  271. /******************************** End Of File *********************************/