Advertisement
uaa

estimate clocks modified SoftwareSerial.cpp calc code

uaa
Jul 24th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. static uint16_t subtract_cap(uint16_t num, uint16_t sub) {
  5. if (num > sub)
  6. return num - sub;
  7. else
  8. return 1;
  9. }
  10.  
  11. void calc(int f_cpu, int speed, uint16_t *txd, uint16_t *rxd_cent, uint16_t *rxd_intra, uint16_t *rxd_stop)
  12. {
  13. #define BASE 4
  14.  
  15. uint16_t bit_delay = (f_cpu / speed) / BASE;
  16.  
  17. *txd = subtract_cap(bit_delay, 14 / BASE);
  18. *rxd_cent = subtract_cap(bit_delay / 2, (4 + 4 + 44 + 14 - 14) / BASE);
  19. *rxd_intra = subtract_cap(bit_delay, 14 / BASE);
  20. *rxd_stop = subtract_cap(bit_delay * 3 / 4, (28 + 6) / BASE);
  21.  
  22. /* convert to delay times -> real clocks */
  23. *txd *= BASE;
  24. *rxd_cent *= BASE;
  25. *rxd_intra *= BASE;
  26. *rxd_stop *= BASE;
  27. }
  28.  
  29. /* original calculation code */
  30. void calc2(int f_cpu, int speed, uint16_t *txd, uint16_t *rxd_cent, uint16_t *rxd_intra, uint16_t *rxd_stop)
  31. {
  32. uint16_t bit_delay = (f_cpu / speed) / 4;
  33.  
  34. *txd = subtract_cap(bit_delay, 15 / 4);
  35. *rxd_cent = subtract_cap(bit_delay / 2, (4 + 4 + 75 + 17 - 23) / 4);
  36. *rxd_intra = subtract_cap(bit_delay, 23 / 4);
  37. *rxd_stop = subtract_cap(bit_delay * 3 / 4, (37 + 11) / 4);
  38.  
  39. /* convert to delay times -> real clocks */
  40. *txd *= 4;
  41. *rxd_cent *= 4;
  42. *rxd_intra *= 4;
  43. *rxd_stop *= 4;
  44. }
  45.  
  46. int main(int argc, char *argv[])
  47. {
  48. #define F_CPU 16000000
  49.  
  50. int speed_table[] = {
  51. 300, 600, 1200, 2400, 4800, 9600, 19200,
  52. 38400, 57600, 115200, 230400,
  53. };
  54. int i;
  55. uint16_t _rx_delay_centering, _rx_delay_centering2;
  56. uint16_t _rx_delay_intrabit, _rx_delay_intrabit2;
  57. uint16_t _rx_delay_stopbit, _rx_delay_stopbit2;
  58. uint16_t _tx_delay, _tx_delay2;
  59.  
  60. for (i = 0; i < sizeof(speed_table) / sizeof(int); i++) {
  61. calc(F_CPU, speed_table[i], &_tx_delay, &_rx_delay_centering,
  62. &_rx_delay_intrabit, &_rx_delay_stopbit);
  63. calc2(F_CPU, speed_table[i], &_tx_delay2, &_rx_delay_centering2,
  64. &_rx_delay_intrabit2, &_rx_delay_stopbit2);
  65.  
  66.  
  67. printf("%8d %6d %5d(%d) %5d(%d) %5d(%d) %5d(%d)\n",
  68. F_CPU, speed_table[i],
  69. _tx_delay, _tx_delay - _tx_delay2,
  70. _rx_delay_centering, _rx_delay_centering - _rx_delay_centering2,
  71. _rx_delay_intrabit, _rx_delay_intrabit - _rx_delay_intrabit2,
  72. _rx_delay_stopbit, _rx_delay_stopbit - _rx_delay_stopbit2);
  73. }
  74.  
  75. return 0;
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement