generation.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. #include "generation.h"
  2. /* Default values of client's registers
  3. * f1: 16kHz
  4. * D1: 49.2%
  5. * Ph2: 0.0
  6. * D2: 49.2%
  7. * Ph3: 0.0
  8. * f3: 1.15MHz
  9. * N3: 10 impules
  10. * D3: 4%
  11. */
  12. void init_params(tweaked_params_s * pParams) {
  13. pParams->f1 = 16000;
  14. pParams->D1 = 0.492 * FXP_SCALING;//49.2 %
  15. pParams->D2 = 0.492 * FXP_SCALING;//49.2 %
  16. pParams->Ph2 = 0.00 * FXP_SCALING;
  17. pParams->Ph3 = 0.00 * FXP_SCALING;
  18. pParams->f3 = 1150000;
  19. pParams->D3 = 40;//4%
  20. pParams->N3 = 10;
  21. pParams->ena = ENABLE_FORCE_NONE;
  22. pParams->_period1 = DIV_ROUND(BASE_FREQ, pParams->f1);
  23. pParams->_duty1 = pParams->_period1 * pParams->D1 / FXP_SCALING;
  24. pParams->_duty2 = pParams->_period1 * pParams->D2 / FXP_SCALING;
  25. pParams->_phase2 = pParams->Ph2 * pParams->_period1 / FXP_SCALING;
  26. pParams->_period3 = DIV_ROUND(BASE_FREQ, pParams->f3);
  27. }
  28. uint32_t validate_generation_values(tweaked_params_s * pParams)
  29. {
  30. if (pParams->f1 < MIN_FREQ1 || pParams->f1 > MAX_FREQ1)
  31. return -1;
  32. if (pParams->D1 < MIN_D1D2 || pParams->D1 > MAX_D1D2)
  33. return -1;
  34. // TODO: missing ph2 restrictions?
  35. if (pParams->D2 < MIN_D1D2 || pParams->D2 > MAX_D1D2)
  36. return -1;
  37. if (pParams->Ph3 > MAX_PH3)
  38. return -1;
  39. if (pParams->f3 < MIN_FREQ3 || pParams->f3 > MAX_FREQ3)
  40. return -1;
  41. // TODO: missing restrictions for f3 and D3
  42. return 0;
  43. }
  44. void obtain_button_deltas(volatile wb_mailbox_button_regs_t * pCurrentButtons, button_regs_t * pOldButtons, button_deltas_t * pDeltas)
  45. {
  46. // differences will fit in the int32_t and then in int16_t
  47. int32_t inc_delta, dec_delta;
  48. int32_t inc_reg, dec_reg;
  49. #define GET_DELTA(REG_I, REG_D, RESULT) {\
  50. /* get reg */\
  51. inc_reg = pCurrentButtons->regs.REG_I;\
  52. dec_reg = pCurrentButtons->regs.REG_D;\
  53. \
  54. /* calc new press */\
  55. inc_delta = inc_reg - pOldButtons->REG_I;\
  56. dec_delta = dec_reg - pOldButtons->REG_D;\
  57. \
  58. /* save reg */\
  59. pOldButtons->REG_I = inc_reg;\
  60. pOldButtons->REG_D = dec_reg;\
  61. \
  62. RESULT = inc_delta - dec_delta;\
  63. }
  64. GET_DELTA(inc_f1 , dec_f1 , pDeltas->dt_f1 );
  65. GET_DELTA(inc_d1 , dec_d1 , pDeltas->dt_d1 );
  66. GET_DELTA(inc_d2 , dec_d2 , pDeltas->dt_d2 );
  67. GET_DELTA(inc_ph2, dec_ph2, pDeltas->dt_ph2);
  68. GET_DELTA(inc_ph3, dec_ph3, pDeltas->dt_ph3);
  69. GET_DELTA(inc_f3 , dec_f3 , pDeltas->dt_f3 );
  70. GET_DELTA(inc_d3 , dec_d3 , pDeltas->dt_d3 );
  71. GET_DELTA(inc_n3 , dec_n3 , pDeltas->dt_n3 );
  72. }
  73. void update_three_signal_values(tweaked_params_s * pParams, button_deltas_t * pDeltas)
  74. {
  75. // TODO: I assume here that value of 1 means one button press - how to interpret long presses?
  76. // Optimization - we only call delta - increment and decrement
  77. if (pDeltas->dt_f1 > 0)
  78. inc_f1(pParams, pDeltas->dt_f1);
  79. else if (pDeltas->dt_f1 < 0)
  80. dec_f1(pParams, pDeltas->dt_f1);
  81. if (pDeltas->dt_d1 > 0)
  82. inc_D1(pParams, pDeltas->dt_d1);
  83. else if (pDeltas->dt_d1 < 0)
  84. dec_D1(pParams, pDeltas->dt_d1);
  85. if (pDeltas->dt_d2 > 0)
  86. inc_D2(pParams, pDeltas->dt_d2);
  87. else if (pDeltas->dt_d2 < 0)
  88. dec_D2(pParams, pDeltas->dt_d2);
  89. if (pDeltas->dt_ph2 > 0)
  90. inc_Ph2(pParams, pDeltas->dt_ph2);
  91. else if (pDeltas->dt_ph2 < 0)
  92. dec_Ph2(pParams, pDeltas->dt_ph2);
  93. if (pDeltas->dt_ph3 > 0)
  94. inc_Ph3(pParams, pDeltas->dt_ph3);
  95. else if (pDeltas->dt_ph3 < 0)
  96. dec_Ph3(pParams, pDeltas->dt_ph3);
  97. if (pDeltas->dt_f3 > 0)
  98. inc_f3(pParams, pDeltas->dt_f3);
  99. else if (pDeltas->dt_f3 < 0)
  100. dec_f3(pParams, pDeltas->dt_f3);
  101. if (pDeltas->dt_d3 > 0)
  102. inc_D3(pParams, pDeltas->dt_d3);
  103. else if (pDeltas->dt_d3 < 0)
  104. dec_D3(pParams, pDeltas->dt_d3);
  105. if (pDeltas->dt_n3 > 0)
  106. inc_N3(pParams, pDeltas->dt_n3);
  107. else if (pDeltas->dt_n3 < 0)
  108. dec_N3(pParams, pDeltas->dt_n3);
  109. }
  110. void inc_f1(tweaked_params_s * pParams, int16_t val) {
  111. int32_t f1 = pParams->f1;
  112. int32_t _period1 = 0;
  113. while (val) {
  114. f1 += 5;
  115. if (f1 > MAX_FREQ1)
  116. return;
  117. _period1 = DIV_ROUND(BASE_FREQ, f1);
  118. uint32_t pulses_time = pParams->_period3*(pParams->N3+2)*N_PULSE_TRAINS;
  119. if (pulses_time > _period1)
  120. {
  121. // undo the last step
  122. f1 -= 5;
  123. _period1 = DIV_ROUND(BASE_FREQ, f1);
  124. break;
  125. }
  126. --val;
  127. }
  128. int32_t _duty1 = _period1*pParams->D1/FXP_SCALING;
  129. int32_t _duty2 = _period1*pParams->D2/FXP_SCALING;
  130. int32_t _phase2 = _period1*pParams->Ph2/FXP_SCALING;
  131. pParams->_duty1 = _duty1;
  132. pParams->_duty2 = _duty2;
  133. pParams->_phase2 = _phase2;
  134. pParams->_period1 = _period1;
  135. pParams->f1 = f1;
  136. }
  137. void dec_f1(tweaked_params_s * pParams, int16_t val) {
  138. int32_t f1 = pParams->f1;
  139. int32_t _period1 = 0;
  140. while (val) {
  141. f1 -= 5;
  142. if (f1 < MIN_FREQ1)
  143. {
  144. f1 += 5;
  145. _period1 = DIV_ROUND(BASE_FREQ, f1);
  146. break;
  147. }
  148. _period1 = DIV_ROUND(BASE_FREQ, f1);
  149. --val;
  150. }
  151. int32_t _duty1 = _period1*pParams->D1/FXP_SCALING;
  152. int32_t _duty2 = _period1*pParams->D2/FXP_SCALING;
  153. int32_t _phase2 = _period1*pParams->Ph2/FXP_SCALING;
  154. pParams->_duty1 = _duty1;
  155. pParams->_duty2 = _duty2;
  156. pParams->_phase2 = _phase2;
  157. pParams->_period1 = _period1;
  158. pParams->f1 = f1;
  159. }
  160. void inc_D1(tweaked_params_s * pParams, int16_t val) {
  161. int32_t D1 = pParams->D1;
  162. int32_t _duty1 = pParams->_duty1;
  163. while (val) {
  164. D1++;
  165. if (D1 > MAX_D1D2)
  166. return;
  167. _duty1 = pParams->_period1*D1/FXP_SCALING;
  168. if (_duty1 + pParams->_phase2 >= pParams->_period1/2)
  169. {
  170. // undo the last step
  171. D1--;
  172. _duty1 = pParams->_period1*D1/FXP_SCALING;
  173. break;
  174. }
  175. --val;
  176. }
  177. pParams->D1 = D1;
  178. pParams->_duty1 = _duty1;
  179. }
  180. void dec_D1(tweaked_params_s * pParams, int16_t val) {
  181. int32_t D1 = pParams->D1;
  182. int32_t _duty1 = pParams->_duty1;
  183. while (val) {
  184. D1--;
  185. if (D1 < MIN_D1D2)
  186. return;
  187. _duty1 = pParams->_period1*D1/FXP_SCALING;
  188. --val;
  189. }
  190. pParams->D1 = D1;
  191. pParams->_duty1 = _duty1;
  192. }
  193. void inc_D2(tweaked_params_s * pParams, int16_t val) {
  194. int32_t D2 = pParams->D2;
  195. int32_t _duty2 = pParams->_duty2;
  196. while (val) {
  197. D2++;
  198. if (D2 > MAX_D1D2)
  199. return;
  200. _duty2 = pParams->_period1*D2/FXP_SCALING;
  201. --val;
  202. }
  203. pParams->D2 = D2;
  204. pParams->_duty2 = _duty2;
  205. }
  206. void dec_D2(tweaked_params_s * pParams, int16_t val) {
  207. int32_t D2 = pParams->D2;
  208. int32_t _duty2 = pParams->_duty2;
  209. while (val) {
  210. D2--;
  211. if (D2 < MIN_D1D2)
  212. return;
  213. _duty2 = pParams->_period1*D2/FXP_SCALING;
  214. --val;
  215. }
  216. pParams->D2 = D2;
  217. pParams->_duty2 = _duty2;
  218. }
  219. void inc_Ph2(tweaked_params_s * pParams, int16_t val) {
  220. int32_t Ph2 = pParams->Ph2;
  221. int32_t _phase2 = pParams->_phase2;
  222. while (val) {
  223. Ph2++;
  224. _phase2 = Ph2*pParams->_period1/FXP_SCALING;
  225. if (pParams->_duty1 + _phase2 >= pParams->_period1/2)
  226. {
  227. // undo the last step
  228. Ph2--;
  229. _phase2 = Ph2*pParams->_period1/FXP_SCALING;
  230. break;
  231. }
  232. --val;
  233. }
  234. pParams->Ph2 = Ph2;
  235. pParams->_phase2 = _phase2;
  236. }
  237. void dec_Ph2(tweaked_params_s * pParams, int16_t val) {
  238. int32_t Ph2 = pParams->Ph2;
  239. int32_t _phase2 = pParams->_phase2;
  240. while (val) {
  241. Ph2--;
  242. if (Ph2 < 0)
  243. return;
  244. _phase2 = Ph2*pParams->_period1/FXP_SCALING;
  245. --val;
  246. }
  247. pParams->Ph2 = Ph2;
  248. pParams->_phase2 = _phase2;
  249. }
  250. void inc_Ph3(tweaked_params_s * pParams, int16_t val) {
  251. int32_t Ph3 = pParams->Ph3;
  252. while (val) {
  253. Ph3++;
  254. if (Ph3 > MAX_PH3)
  255. return;
  256. --val;
  257. }
  258. pParams->Ph3 = Ph3;
  259. }
  260. void dec_Ph3(tweaked_params_s * pParams, int16_t val) {
  261. int32_t Ph3 = pParams->Ph3;
  262. while (val) {
  263. Ph3--;
  264. if (Ph3 < 0)
  265. return;
  266. --val;
  267. }
  268. pParams->Ph3 = Ph3;
  269. }
  270. void inc_f3(tweaked_params_s * pParams, int16_t val) {
  271. int32_t f3 = pParams->f3;
  272. int32_t _period3 = 0;
  273. while (val) {
  274. f3 += 10000;
  275. if (f3 > MAX_FREQ3)
  276. return;
  277. _period3 = DIV_ROUND(BASE_FREQ, f3);
  278. --val;
  279. }
  280. pParams->f3 = f3;
  281. pParams->_period3 = _period3;
  282. }
  283. void dec_f3(tweaked_params_s * pParams, int16_t val) {
  284. int32_t f3 = pParams->f3;
  285. int32_t _period3 = 0;
  286. while (val) {
  287. f3 -= 10000;
  288. if (f3 < MIN_FREQ3)
  289. return;
  290. _period3 = DIV_ROUND(BASE_FREQ, f3);
  291. uint32_t pulses_time = _period3*(pParams->N3+2)*N_PULSE_TRAINS;
  292. if(pulses_time > pParams->_period1)
  293. return;
  294. --val;
  295. }
  296. pParams->f3 = f3;
  297. pParams->_period3 = _period3;
  298. }
  299. void inc_D3(tweaked_params_s * pParams, int16_t val) {
  300. int32_t D3 = pParams->D3;
  301. while (val) {
  302. D3 += 5;
  303. if (D3 > 1000)
  304. return;
  305. --val;
  306. }
  307. pParams->D3 = D3;
  308. }
  309. void dec_D3(tweaked_params_s * pParams, int16_t val) {
  310. int32_t D3 = pParams->D3;
  311. while (val) {
  312. D3 -= 5;
  313. if (D3 < 0)
  314. return;
  315. --val;
  316. }
  317. pParams->D3 = D3;
  318. }
  319. void inc_N3(tweaked_params_s * pParams, int16_t val) {
  320. int32_t N3 = pParams->N3;
  321. while (val) {
  322. N3++;
  323. if (N3 > 254)
  324. return;
  325. uint32_t pulses_time = pParams->_period3*(N3+2)*N_PULSE_TRAINS;
  326. if(pulses_time > pParams->_period1)
  327. return;
  328. --val;
  329. }
  330. pParams->N3 = N3;
  331. }
  332. void dec_N3(tweaked_params_s * pParams, int16_t val) {
  333. int32_t N3 = pParams->N3;
  334. while (val) {
  335. N3--;
  336. if (N3 < 1)
  337. return;
  338. --val;
  339. }
  340. pParams->N3 = N3;
  341. }
  342. void ena_force_toggle(tweaked_params_s * pParams) {
  343. enable_force_t ena = pParams->ena;
  344. ena = (ena == ENABLE_FORCE_DISABLE) ? ENABLE_FORCE_ENABLE : ENABLE_FORCE_DISABLE;
  345. pParams->ena = ena;
  346. }