st25r3916_aat.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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(
  67. const struct st25r3916AatTuneParams* tuningParams,
  68. struct st25r3916AatTuneResult* tuningStatus);
  69. static int32_t aatGreedyDescent(
  70. uint32_t* f_min,
  71. const struct st25r3916AatTuneParams* tuningParams,
  72. struct st25r3916AatTuneResult* tuningStatus,
  73. int32_t previousDir);
  74. static int32_t aatSteepestDescent(
  75. uint32_t* f_min,
  76. const struct st25r3916AatTuneParams* tuningParams,
  77. struct st25r3916AatTuneResult* tuningStatus,
  78. int32_t previousDir,
  79. int32_t previousDir2);
  80. static ReturnCode aatMeasure(
  81. uint8_t serCap,
  82. uint8_t parCap,
  83. uint8_t* amplitude,
  84. uint8_t* phase,
  85. uint16_t* measureCnt);
  86. static uint32_t
  87. aatCalcF(const struct st25r3916AatTuneParams* tuningParams, uint8_t amplitude, uint8_t phase);
  88. static ReturnCode aatStepDacVals(
  89. const struct st25r3916AatTuneParams* tuningParams,
  90. uint8_t* a,
  91. uint8_t* b,
  92. int32_t dir);
  93. /*******************************************************************************/
  94. ReturnCode st25r3916AatTune(
  95. const struct st25r3916AatTuneParams* tuningParams,
  96. struct st25r3916AatTuneResult* tuningStatus) {
  97. ReturnCode err;
  98. const struct st25r3916AatTuneParams* tp = tuningParams;
  99. struct st25r3916AatTuneResult* ts = tuningStatus;
  100. struct st25r3916AatTuneParams defaultTuningParams = {
  101. .aat_a_min = 0,
  102. .aat_a_max = 255,
  103. .aat_a_start = 127,
  104. .aat_a_stepWidth = 32,
  105. .aat_b_min = 0,
  106. .aat_b_max = 255,
  107. .aat_b_start = 127,
  108. .aat_b_stepWidth = 32,
  109. .phaTarget = 128,
  110. .phaWeight = 2,
  111. .ampTarget = 196,
  112. .ampWeight = 1,
  113. .doDynamicSteps = true,
  114. .measureLimit = 50,
  115. };
  116. struct st25r3916AatTuneResult defaultTuneResult;
  117. if((NULL != tp) && ((tp->aat_a_min > tp->aat_a_max) || (tp->aat_a_start < tp->aat_a_min) ||
  118. (tp->aat_a_start > tp->aat_a_max) || (tp->aat_b_min > tp->aat_b_max) ||
  119. (tp->aat_b_start < tp->aat_b_min) || (tp->aat_b_start > tp->aat_b_max))) {
  120. return ERR_PARAM;
  121. }
  122. if(NULL == tp) { /* Start from current caps with default params */
  123. st25r3916ReadRegister(ST25R3916_REG_ANT_TUNE_A, &defaultTuningParams.aat_a_start);
  124. st25r3916ReadRegister(ST25R3916_REG_ANT_TUNE_B, &defaultTuningParams.aat_b_start);
  125. tp = &defaultTuningParams;
  126. }
  127. if(NULL == ts) {
  128. ts = &defaultTuneResult;
  129. }
  130. ts->measureCnt = 0; /* Clear current measure count */
  131. err = aatHillClimb(tp, ts);
  132. return err;
  133. }
  134. /*******************************************************************************/
  135. static ReturnCode aatHillClimb(
  136. const struct st25r3916AatTuneParams* tuningParams,
  137. struct st25r3916AatTuneResult* tuningStatus) {
  138. ReturnCode err = ERR_NONE;
  139. uint32_t f_min;
  140. int32_t direction, gdirection;
  141. uint8_t amp, phs;
  142. struct st25r3916AatTuneParams tp = *tuningParams; // local copy to obey const
  143. tuningStatus->aat_a = tuningParams->aat_a_start;
  144. tuningStatus->aat_b = tuningParams->aat_b_start;
  145. /* Get a proper start value */
  146. aatMeasure(tuningStatus->aat_a, tuningStatus->aat_b, &amp, &phs, &tuningStatus->measureCnt);
  147. f_min = aatCalcF(&tp, amp, phs);
  148. direction = 0;
  149. st25r3916AatLog("%d %d: %d***\n", tuningStatus->aat_a, tuningStatus->aat_b, f_min);
  150. do {
  151. direction =
  152. 0; /* Initially and after reducing step sizes we don't have a previous direction */
  153. do {
  154. /* With the greedy step below always executed aftwards the -direction does never need to be investigated */
  155. direction = aatSteepestDescent(&f_min, &tp, tuningStatus, direction, -direction);
  156. if(tuningStatus->measureCnt > tp.measureLimit) {
  157. err = ERR_OVERRUN;
  158. break;
  159. }
  160. do {
  161. gdirection = aatGreedyDescent(&f_min, &tp, tuningStatus, direction);
  162. if(tuningStatus->measureCnt > tp.measureLimit) {
  163. err = ERR_OVERRUN;
  164. break;
  165. }
  166. } while(0 != gdirection);
  167. } while(0 != direction);
  168. tp.aat_a_stepWidth /= 2U; /* Reduce step sizes */
  169. tp.aat_b_stepWidth /= 2U;
  170. } while(tp.doDynamicSteps && ((tp.aat_a_stepWidth > 0U) || (tp.aat_b_stepWidth > 0U)));
  171. return err;
  172. }
  173. /*******************************************************************************/
  174. static int32_t aatSteepestDescent(
  175. uint32_t* f_min,
  176. const struct st25r3916AatTuneParams* tuningParams,
  177. struct st25r3916AatTuneResult* tuningStatus,
  178. int32_t previousDir,
  179. int32_t previousDir2) {
  180. int32_t i;
  181. uint8_t amp, phs;
  182. uint32_t f;
  183. int32_t bestdir =
  184. 0; /* Negative direction: decrease, Positive: increase. (-)1: aat_a, (-)2: aat_b */
  185. for(i = -2; i <= 2; i++) {
  186. uint8_t a = tuningStatus->aat_a, b = tuningStatus->aat_b;
  187. if((0 == i) || (i == -previousDir) ||
  188. (i == -previousDir2)) { /* Skip no direction and avoid going backwards */
  189. continue;
  190. }
  191. if(0U != aatStepDacVals(
  192. tuningParams,
  193. &a,
  194. &b,
  195. i)) { /* If stepping did not change the value, omit this direction */
  196. continue;
  197. }
  198. aatMeasure(a, b, &amp, &phs, &tuningStatus->measureCnt);
  199. f = aatCalcF(tuningParams, amp, phs);
  200. st25r3916AatLog("%d : %d %d: %d", i, a, b, f);
  201. if(f < *f_min) { /* Value is better than all previous ones */
  202. st25r3916AatLog("*");
  203. *f_min = f;
  204. bestdir = i;
  205. }
  206. st25r3916AatLog("\n");
  207. }
  208. if(0 != bestdir) { /* Walk into the best direction */
  209. aatStepDacVals(tuningParams, &tuningStatus->aat_a, &tuningStatus->aat_b, bestdir);
  210. }
  211. return bestdir;
  212. }
  213. /*******************************************************************************/
  214. static int32_t aatGreedyDescent(
  215. uint32_t* f_min,
  216. const struct st25r3916AatTuneParams* tuningParams,
  217. struct st25r3916AatTuneResult* tuningStatus,
  218. int32_t previousDir) {
  219. uint8_t amp, phs;
  220. uint32_t f;
  221. uint8_t a = tuningStatus->aat_a, b = tuningStatus->aat_b;
  222. if(0U != aatStepDacVals(
  223. tuningParams,
  224. &a,
  225. &b,
  226. previousDir)) { /* If stepping did not change the value, omit this direction */
  227. return 0;
  228. }
  229. aatMeasure(a, b, &amp, &phs, &tuningStatus->measureCnt);
  230. f = aatCalcF(tuningParams, amp, phs);
  231. st25r3916AatLog("g : %d %d: %d", a, b, f);
  232. if(f < *f_min) { /* Value is better than previous one */
  233. st25r3916AatLog("*\n");
  234. tuningStatus->aat_a = a;
  235. tuningStatus->aat_b = b;
  236. *f_min = f;
  237. return previousDir;
  238. }
  239. st25r3916AatLog("\n");
  240. return 0;
  241. }
  242. /*******************************************************************************/
  243. static uint32_t
  244. aatCalcF(const struct st25r3916AatTuneParams* tuningParams, uint8_t amplitude, uint8_t phase) {
  245. /* f(amp, pha) = (ampWeight * |amp - ampTarget|) + (phaWeight * |pha - phaTarget|) */
  246. uint8_t ampTarget = tuningParams->ampTarget;
  247. uint8_t phaTarget = tuningParams->phaTarget;
  248. uint32_t ampWeight = tuningParams->ampWeight;
  249. uint32_t phaWeight = tuningParams->phaWeight;
  250. /* Temp variables to avoid MISRA R10.8 (cast on composite expression) */
  251. uint8_t ad = ((amplitude > ampTarget) ? (amplitude - ampTarget) : (ampTarget - amplitude));
  252. uint8_t pd = ((phase > phaTarget) ? (phase - phaTarget) : (phaTarget - phase));
  253. uint32_t ampDelta = (uint32_t)ad;
  254. uint32_t phaDelta = (uint32_t)pd;
  255. return ((ampWeight * ampDelta) + (phaWeight * phaDelta));
  256. }
  257. /*******************************************************************************/
  258. static ReturnCode aatStepDacVals(
  259. const struct st25r3916AatTuneParams* tuningParams,
  260. uint8_t* a,
  261. uint8_t* b,
  262. int32_t dir) {
  263. int16_t aat_a = (int16_t)*a, aat_b = (int16_t)*b;
  264. switch(abs(dir)) { /* Advance by steps size in requested direction */
  265. case 1:
  266. aat_a = (dir < 0) ? (aat_a - (int16_t)tuningParams->aat_a_stepWidth) :
  267. (aat_a + (int16_t)tuningParams->aat_a_stepWidth);
  268. if(aat_a < (int16_t)tuningParams->aat_a_min) {
  269. aat_a = (int16_t)tuningParams->aat_a_min;
  270. }
  271. if(aat_a > (int16_t)tuningParams->aat_a_max) {
  272. aat_a = (int16_t)tuningParams->aat_a_max;
  273. }
  274. if((int16_t)*a == aat_a) {
  275. return ERR_PARAM;
  276. }
  277. break;
  278. case 2:
  279. aat_b = (dir < 0) ? (aat_b - (int16_t)tuningParams->aat_b_stepWidth) :
  280. (aat_b + (int16_t)tuningParams->aat_b_stepWidth);
  281. if(aat_b < (int16_t)tuningParams->aat_b_min) {
  282. aat_b = (int16_t)tuningParams->aat_b_min;
  283. }
  284. if(aat_b > (int16_t)tuningParams->aat_b_max) {
  285. aat_b = (int16_t)tuningParams->aat_b_max;
  286. }
  287. if((int16_t)*b == aat_b) {
  288. return ERR_PARAM;
  289. }
  290. break;
  291. default:
  292. return ERR_REQUEST;
  293. }
  294. /* We only get here if actual values have changed. In all other cases an error is returned */
  295. *a = (uint8_t)aat_a;
  296. *b = (uint8_t)aat_b;
  297. return ERR_NONE;
  298. }
  299. /*******************************************************************************/
  300. static ReturnCode aatMeasure(
  301. uint8_t serCap,
  302. uint8_t parCap,
  303. uint8_t* amplitude,
  304. uint8_t* phase,
  305. uint16_t* measureCnt) {
  306. ReturnCode err;
  307. *amplitude = 0;
  308. *phase = 0;
  309. st25r3916WriteRegister(ST25R3916_REG_ANT_TUNE_A, serCap);
  310. st25r3916WriteRegister(ST25R3916_REG_ANT_TUNE_B, parCap);
  311. /* Wait till caps have settled.. */
  312. platformDelay(ST25R3916_AAT_CAP_DELAY_MAX);
  313. /* Get amplitude and phase .. */
  314. err = rfalChipMeasureAmplitude(amplitude);
  315. if(ERR_NONE == err) {
  316. err = rfalChipMeasurePhase(phase);
  317. }
  318. if(measureCnt != NULL) {
  319. (*measureCnt)++;
  320. }
  321. return err;
  322. }