generation.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 update_three_signal_values(tweaked_params_s * pParams, wb_mailbox_button_regs_t * pButtons)
  45. {
  46. // TODO: I assume here that value of 1 means one button press - how to interpret long presses?
  47. // Optimization - we only call delta - increment and decrement
  48. int32_t delta = pButtons->regs.inc_f1 - pButtons->regs.dec_f1;
  49. if (delta > 0)
  50. inc_f1(pParams, delta);
  51. else if (delta < 0)
  52. dec_f1(pParams, -delta);
  53. // TODO: add handling of the rest
  54. //delta = pButtons->regs.inc_d1 - pButtons->regs.dec_d1;
  55. }
  56. // TODO: all of these have to be updated by a value read from the register (how to calibrate it?)
  57. void inc_f1(tweaked_params_s * pParams, uint32_t val) {
  58. int32_t f1 = pParams->f1;
  59. int32_t _period1 = 0;
  60. while (val) {
  61. f1 += 5;
  62. if (f1 > MAX_FREQ1)
  63. return;
  64. _period1 = DIV_ROUND(BASE_FREQ, f1);
  65. uint32_t pulses_time = pParams->_period3*(pParams->N3+2)*N_PULSE_TRAINS;
  66. if (pulses_time > _period1)
  67. {
  68. // undo the last step
  69. f1 -= 5;
  70. _period1 = DIV_ROUND(BASE_FREQ, f1);
  71. break;
  72. }
  73. --val;
  74. }
  75. int32_t _duty1 = _period1*pParams->D1/4000;
  76. int32_t _duty2 = _period1*pParams->D2/4000;
  77. int32_t _phase2 = _period1*pParams->Ph2/4000;
  78. pParams->_duty1 = _duty1;
  79. pParams->_duty2 = _duty2;
  80. pParams->_phase2 = _phase2;
  81. pParams->_period1 = _period1;
  82. pParams->f1 = f1;
  83. }
  84. void dec_f1(tweaked_params_s * pParams, uint32_t val) {
  85. int32_t f1 = pParams->f1;
  86. int32_t _period1 = 0;
  87. while (val) {
  88. f1 -= 5;
  89. if (f1 < MIN_FREQ1)
  90. {
  91. f1 += 5;
  92. _period1 = DIV_ROUND(BASE_FREQ, f1);
  93. break;
  94. }
  95. _period1 = DIV_ROUND(BASE_FREQ, f1);
  96. --val;
  97. }
  98. int32_t _duty1 = _period1*pParams->D1/4000;
  99. int32_t _duty2 = _period1*pParams->D2/4000;
  100. int32_t _phase2 = _period1*pParams->Ph2/4000;
  101. pParams->_duty1 = _duty1;
  102. pParams->_duty2 = _duty2;
  103. pParams->_phase2 = _phase2;
  104. pParams->_period1 = _period1;
  105. pParams->f1 = f1;
  106. }
  107. void inc_D1(tweaked_params_s * pParams) {
  108. int32_t D1 = pParams->D1;
  109. D1++;
  110. if(D1>MAX_D1D2)
  111. return;
  112. int32_t _duty1 = pParams->_period1*D1/4000;
  113. if(_duty1+pParams->_phase2 >= pParams->_period1/2)
  114. return;
  115. pParams->D1 = D1;
  116. pParams->_duty1 = _duty1;
  117. }
  118. void dec_D1(tweaked_params_s * pParams) {
  119. int32_t D1 = pParams->D1;
  120. D1--;
  121. if(D1<MIN_D1D2)
  122. return;
  123. int32_t _duty1 = pParams->_period1*D1/4000;
  124. pParams->D1 = D1;
  125. pParams->_duty1 = _duty1;
  126. }
  127. void inc_D2(tweaked_params_s * pParams) {
  128. int32_t D2 = pParams->D2;
  129. D2++;
  130. if(D2>MAX_D1D2)
  131. return;
  132. int32_t _duty2 = pParams->_period1*D2/4000;
  133. pParams->D2 = D2;
  134. pParams->_duty2 = _duty2;
  135. }
  136. void dec_D2(tweaked_params_s * pParams) {
  137. int32_t D2 = pParams->D2;
  138. D2--;
  139. if(D2<MIN_D1D2)
  140. return;
  141. int32_t _duty2 = pParams->_period1*D2/4000;
  142. pParams->D2 = D2;
  143. pParams->_duty2 = _duty2;
  144. }
  145. void inc_Ph2(tweaked_params_s * pParams) {
  146. int32_t Ph2 = pParams->Ph2;
  147. Ph2++;
  148. int32_t _phase2 = Ph2*pParams->_period1/4000;
  149. if(pParams->_duty1+_phase2 >= pParams->_period1/2)
  150. return;
  151. pParams->Ph2 = Ph2;
  152. pParams->_phase2 = _phase2;
  153. }
  154. void dec_Ph2(tweaked_params_s * pParams) {
  155. int32_t Ph2 = pParams->Ph2;
  156. Ph2--;
  157. if(Ph2<0)
  158. return;//limit value
  159. int32_t _phase2 = Ph2*pParams->_period1/4000;
  160. pParams->Ph2 = Ph2;
  161. pParams->_phase2 = _phase2;
  162. }
  163. void inc_Ph3(tweaked_params_s * pParams) {
  164. int32_t Ph3 = pParams->Ph3;
  165. Ph3++;
  166. if(Ph3>MAX_PH3)
  167. return;//limit value
  168. pParams->Ph3 = Ph3;
  169. }
  170. void dec_Ph3(tweaked_params_s * pParams) {
  171. int32_t Ph3 = pParams->Ph3;
  172. Ph3--;
  173. if(Ph3<0)
  174. return;//limit value
  175. pParams->Ph3 = Ph3;
  176. }
  177. void inc_f3(tweaked_params_s * pParams) {
  178. int32_t f3 = pParams->f3;
  179. f3 += 10000;
  180. if(f3>MAX_FREQ3)
  181. return;
  182. int32_t _period3 = DIV_ROUND(BASE_FREQ, f3);
  183. pParams->f3 = f3;
  184. pParams->_period3 = _period3;
  185. }
  186. void dec_f3(tweaked_params_s * pParams) {
  187. int32_t f3 = pParams->f3;
  188. f3 -= 10000;
  189. if(f3<MIN_FREQ3)
  190. return;
  191. int32_t _period3 = DIV_ROUND(BASE_FREQ, f3);
  192. uint32_t pulses_time = _period3*(pParams->N3+2)*N_PULSE_TRAINS;
  193. if(pulses_time>pParams->_period1)
  194. return;
  195. pParams->f3 = f3;
  196. pParams->_period3 = _period3;
  197. }
  198. void inc_D3(tweaked_params_s * pParams) {
  199. int32_t D3 = pParams->D3;
  200. D3 += 5;
  201. if(D3>1000)
  202. return;
  203. pParams->D3 = D3;
  204. }
  205. void dec_D3(tweaked_params_s * pParams) {
  206. int32_t D3 = pParams->D3;
  207. D3 -= 5;
  208. if(D3<0)
  209. return;
  210. pParams->D3 = D3;
  211. }
  212. void inc_N3(tweaked_params_s * pParams) {
  213. int32_t N3 = pParams->N3;
  214. N3++;
  215. if(N3>254)
  216. return;
  217. uint32_t pulses_time = pParams->_period3*(N3+2)*N_PULSE_TRAINS;
  218. if(pulses_time>pParams->_period1)
  219. return;
  220. pParams->N3 = N3;
  221. }
  222. void dec_N3(tweaked_params_s * pParams) {
  223. int32_t N3 = pParams->N3;
  224. N3--;
  225. if(N3<1)
  226. return;
  227. pParams->N3 = N3;
  228. }
  229. void ena_force_toggle(tweaked_params_s * pParams) {
  230. enable_force_t ena = pParams->ena;
  231. ena = ena==ENABLE_FORCE_DISABLE?ENABLE_FORCE_ENABLE:ENABLE_FORCE_DISABLE;
  232. pParams->ena = ena;
  233. }