st25r3916_aat.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /******************************************************************************
  2. * \attention
  3. *
  4. * <h2><center>&copy; COPYRIGHT 2020 STMicroelectronics</center></h2>
  5. *
  6. * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
  7. * You may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * www.st.com/myliberty
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
  15. * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. ******************************************************************************/
  21. /*
  22. * PROJECT: ST25R3916 firmware
  23. * Revision:
  24. * LANGUAGE: ISO C99
  25. */
  26. /*! \file st25r3916_aat.c
  27. *
  28. * \author
  29. *
  30. * \brief ST25R3916 Antenna Tuning
  31. *
  32. * The antenna tuning algorithm tries to find the optimal settings for
  33. * the AAT_A and AAT_B registers, which are connected to variable capacitors
  34. * to tune the antenna matching.
  35. *
  36. */
  37. /*
  38. ******************************************************************************
  39. * INCLUDES
  40. ******************************************************************************
  41. */
  42. #include "st25r3916_aat.h"
  43. #include "utils.h"
  44. #include "st_errno.h"
  45. #include "st25r3916.h"
  46. #include "st25r3916_com.h"
  47. #include "platform.h"
  48. #include "rfal_chip.h"
  49. /*
  50. ******************************************************************************
  51. * GLOBAL DEFINES
  52. ******************************************************************************
  53. */
  54. #define ST25R3916_AAT_CAP_DELAY_MAX 10 /*!< Max Variable Capacitor settle delay */
  55. /*
  56. ******************************************************************************
  57. * GLOBAL MACROS
  58. ******************************************************************************
  59. */
  60. #define st25r3916AatLog(...) /* platformLog(__VA_ARGS__) */ /*!< Logging macro */
  61. /*
  62. ******************************************************************************
  63. * LOCAL FUNCTION PROTOTYPES
  64. ******************************************************************************
  65. */
  66. static ReturnCode aatHillClimb(const struct st25r3916AatTuneParams *tuningParams, struct st25r3916AatTuneResult *tuningStatus);
  67. static int32_t aatGreedyDescent(uint32_t *f_min, const struct st25r3916AatTuneParams *tuningParams, struct st25r3916AatTuneResult *tuningStatus, int32_t previousDir);
  68. static int32_t aatSteepestDescent(uint32_t *f_min, const struct st25r3916AatTuneParams *tuningParams, struct st25r3916AatTuneResult *tuningStatus, int32_t previousDir, int32_t previousDir2);
  69. static ReturnCode aatMeasure(uint8_t serCap, uint8_t parCap, uint8_t *amplitude, uint8_t *phase, uint16_t *measureCnt);
  70. static uint32_t aatCalcF(const struct st25r3916AatTuneParams *tuningParams, uint8_t amplitude, uint8_t phase);
  71. static ReturnCode aatStepDacVals(const struct st25r3916AatTuneParams *tuningParams,uint8_t *a, uint8_t *b, int32_t dir);
  72. /*******************************************************************************/
  73. ReturnCode st25r3916AatTune(const struct st25r3916AatTuneParams *tuningParams, struct st25r3916AatTuneResult *tuningStatus)
  74. {
  75. ReturnCode err;
  76. const struct st25r3916AatTuneParams *tp = tuningParams;
  77. struct st25r3916AatTuneResult *ts = tuningStatus;
  78. struct st25r3916AatTuneParams defaultTuningParams =
  79. {
  80. .aat_a_min=0,
  81. .aat_a_max=255,
  82. .aat_a_start=127,
  83. .aat_a_stepWidth=32,
  84. .aat_b_min=0,
  85. .aat_b_max=255,
  86. .aat_b_start=127,
  87. .aat_b_stepWidth=32,
  88. .phaTarget=128,
  89. .phaWeight=2,
  90. .ampTarget=196,
  91. .ampWeight=1,
  92. .doDynamicSteps=true,
  93. .measureLimit=50,
  94. };
  95. struct st25r3916AatTuneResult defaultTuneResult;
  96. if ((NULL != tp) && (
  97. (tp->aat_a_min > tp->aat_a_max )
  98. || (tp->aat_a_start < tp->aat_a_min )
  99. || (tp->aat_a_start > tp->aat_a_max )
  100. || (tp->aat_b_min > tp->aat_b_max )
  101. || (tp->aat_b_start < tp->aat_b_min )
  102. || (tp->aat_b_start > tp->aat_b_max )
  103. ))
  104. {
  105. return ERR_PARAM;
  106. }
  107. if (NULL == tp)
  108. { /* Start from current caps with default params */
  109. st25r3916ReadRegister(ST25R3916_REG_ANT_TUNE_A, &defaultTuningParams.aat_a_start);
  110. st25r3916ReadRegister(ST25R3916_REG_ANT_TUNE_B, &defaultTuningParams.aat_b_start);
  111. tp = &defaultTuningParams;
  112. }
  113. if (NULL == ts){ts = &defaultTuneResult;}
  114. ts->measureCnt = 0; /* Clear current measure count */
  115. err = aatHillClimb(tp, ts);
  116. return err;
  117. }
  118. /*******************************************************************************/
  119. static ReturnCode aatHillClimb(const struct st25r3916AatTuneParams *tuningParams, struct st25r3916AatTuneResult *tuningStatus)
  120. {
  121. ReturnCode err = ERR_NONE;
  122. uint32_t f_min;
  123. int32_t direction, gdirection;
  124. uint8_t amp,phs;
  125. struct st25r3916AatTuneParams tp = *tuningParams; // local copy to obey const
  126. tuningStatus->aat_a = tuningParams->aat_a_start;
  127. tuningStatus->aat_b = tuningParams->aat_b_start;
  128. /* Get a proper start value */
  129. aatMeasure(tuningStatus->aat_a,tuningStatus->aat_b,&amp,&phs,&tuningStatus->measureCnt);
  130. f_min = aatCalcF(&tp, amp, phs);
  131. direction = 0;
  132. st25r3916AatLog("%d %d: %d***\n",tuningStatus->aat_a,tuningStatus->aat_b,f_min);
  133. do {
  134. direction = 0; /* Initially and after reducing step sizes we don't have a previous direction */
  135. do {
  136. /* With the greedy step below always executed aftwards the -direction does never need to be investigated */
  137. direction = aatSteepestDescent(&f_min, &tp, tuningStatus, direction, -direction);
  138. if (tuningStatus->measureCnt > tp.measureLimit)
  139. {
  140. err = ERR_OVERRUN;
  141. break;
  142. }
  143. do
  144. {
  145. gdirection = aatGreedyDescent(&f_min, &tp, tuningStatus, direction);
  146. if (tuningStatus->measureCnt > tp.measureLimit) {
  147. err = ERR_OVERRUN;
  148. break;
  149. }
  150. } while (0 != gdirection);
  151. } while (0 != direction);
  152. tp.aat_a_stepWidth /= 2U; /* Reduce step sizes */
  153. tp.aat_b_stepWidth /= 2U;
  154. } while (tp.doDynamicSteps && ((tp.aat_a_stepWidth>0U) || (tp.aat_b_stepWidth>0U)));
  155. return err;
  156. }
  157. /*******************************************************************************/
  158. static int32_t aatSteepestDescent(uint32_t *f_min, const struct st25r3916AatTuneParams *tuningParams, struct st25r3916AatTuneResult *tuningStatus, int32_t previousDir, int32_t previousDir2)
  159. {
  160. int32_t i;
  161. uint8_t amp,phs;
  162. uint32_t f;
  163. int32_t bestdir = 0; /* Negative direction: decrease, Positive: increase. (-)1: aat_a, (-)2: aat_b */
  164. for (i = -2; i <= 2; i++)
  165. {
  166. uint8_t a = tuningStatus->aat_a , b = tuningStatus->aat_b;
  167. if ((0==i) || (i==-previousDir) || (i==-previousDir2))
  168. { /* Skip no direction and avoid going backwards */
  169. continue;
  170. }
  171. if (0U!=aatStepDacVals(tuningParams, &a, &b, i))
  172. { /* If stepping did not change the value, omit this direction */
  173. continue;
  174. }
  175. aatMeasure(a,b,&amp,&phs,&tuningStatus->measureCnt);
  176. f = aatCalcF(tuningParams, amp, phs);
  177. st25r3916AatLog("%d : %d %d: %d",i,a, b, f);
  178. if (f < *f_min)
  179. { /* Value is better than all previous ones */
  180. st25r3916AatLog("*");
  181. *f_min = f;
  182. bestdir = i;
  183. }
  184. st25r3916AatLog("\n");
  185. }
  186. if (0!=bestdir)
  187. { /* Walk into the best direction */
  188. aatStepDacVals(tuningParams, &tuningStatus->aat_a, &tuningStatus->aat_b, bestdir);
  189. }
  190. return bestdir;
  191. }
  192. /*******************************************************************************/
  193. static int32_t aatGreedyDescent(uint32_t *f_min, const struct st25r3916AatTuneParams *tuningParams, struct st25r3916AatTuneResult *tuningStatus, int32_t previousDir)
  194. {
  195. uint8_t amp,phs;
  196. uint32_t f;
  197. uint8_t a = tuningStatus->aat_a , b = tuningStatus->aat_b;
  198. if (0U != aatStepDacVals(tuningParams, &a, &b, previousDir))
  199. { /* If stepping did not change the value, omit this direction */
  200. return 0;
  201. }
  202. aatMeasure(a,b,&amp,&phs,&tuningStatus->measureCnt);
  203. f = aatCalcF(tuningParams, amp, phs);
  204. st25r3916AatLog("g : %d %d: %d",a, b, f);
  205. if (f < *f_min)
  206. { /* Value is better than previous one */
  207. st25r3916AatLog("*\n");
  208. tuningStatus->aat_a = a;
  209. tuningStatus->aat_b = b;
  210. *f_min = f;
  211. return previousDir;
  212. }
  213. st25r3916AatLog("\n");
  214. return 0;
  215. }
  216. /*******************************************************************************/
  217. static uint32_t aatCalcF(const struct st25r3916AatTuneParams *tuningParams, uint8_t amplitude, uint8_t phase)
  218. {
  219. /* f(amp, pha) = (ampWeight * |amp - ampTarget|) + (phaWeight * |pha - phaTarget|) */
  220. uint8_t ampTarget = tuningParams->ampTarget;
  221. uint8_t phaTarget = tuningParams->phaTarget;
  222. uint32_t ampWeight = tuningParams->ampWeight;
  223. uint32_t phaWeight = tuningParams->phaWeight;
  224. /* Temp variables to avoid MISRA R10.8 (cast on composite expression) */
  225. uint8_t ad = ((amplitude > ampTarget) ? (amplitude - ampTarget) : (ampTarget - amplitude));
  226. uint8_t pd = ((phase > phaTarget) ? (phase - phaTarget) : (phaTarget - phase));
  227. uint32_t ampDelta = (uint32_t)ad;
  228. uint32_t phaDelta = (uint32_t)pd;
  229. return ((ampWeight * ampDelta) + (phaWeight * phaDelta));
  230. }
  231. /*******************************************************************************/
  232. static ReturnCode aatStepDacVals(const struct st25r3916AatTuneParams *tuningParams,uint8_t *a, uint8_t *b, int32_t dir)
  233. {
  234. int16_t aat_a = (int16_t)*a, aat_b = (int16_t)*b;
  235. switch (abs(dir))
  236. { /* Advance by steps size in requested direction */
  237. case 1:
  238. aat_a = (dir<0)?(aat_a - (int16_t)tuningParams->aat_a_stepWidth):(aat_a + (int16_t)tuningParams->aat_a_stepWidth);
  239. if(aat_a < (int16_t)tuningParams->aat_a_min){ aat_a = (int16_t)tuningParams->aat_a_min; }
  240. if(aat_a > (int16_t)tuningParams->aat_a_max){ aat_a = (int16_t)tuningParams->aat_a_max; }
  241. if ((int16_t)*a == aat_a) {return ERR_PARAM;}
  242. break;
  243. case 2:
  244. aat_b = (dir<0)?(aat_b - (int16_t)tuningParams->aat_b_stepWidth):(aat_b + (int16_t)tuningParams->aat_b_stepWidth);
  245. if(aat_b < (int16_t)tuningParams->aat_b_min){ aat_b = (int16_t)tuningParams->aat_b_min; }
  246. if(aat_b > (int16_t)tuningParams->aat_b_max){ aat_b = (int16_t)tuningParams->aat_b_max; }
  247. if ((int16_t)*b == aat_b) {return ERR_PARAM;}
  248. break;
  249. default:
  250. return ERR_REQUEST;
  251. }
  252. /* We only get here if actual values have changed. In all other cases an error is returned */
  253. *a = (uint8_t)aat_a;
  254. *b = (uint8_t)aat_b;
  255. return ERR_NONE;
  256. }
  257. /*******************************************************************************/
  258. static ReturnCode aatMeasure(uint8_t serCap, uint8_t parCap, uint8_t *amplitude, uint8_t *phase, uint16_t *measureCnt)
  259. {
  260. ReturnCode err;
  261. *amplitude = 0;
  262. *phase = 0;
  263. st25r3916WriteRegister(ST25R3916_REG_ANT_TUNE_A, serCap);
  264. st25r3916WriteRegister(ST25R3916_REG_ANT_TUNE_B, parCap);
  265. /* Wait till caps have settled.. */
  266. platformDelay( ST25R3916_AAT_CAP_DELAY_MAX );
  267. /* Get amplitude and phase .. */
  268. err = rfalChipMeasureAmplitude(amplitude);
  269. if (ERR_NONE == err)
  270. {
  271. err = rfalChipMeasurePhase(phase);
  272. }
  273. if( measureCnt != NULL )
  274. {
  275. (*measureCnt)++;
  276. }
  277. return err;
  278. }