You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.7 KiB

  1. #ifdef USE_SERIAL
  2. #ifdef SERIAL_USE_MULTI_TRANSACTION
  3. /* --- USE flexible API (using multi-type transaction function) --- */
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <stddef.h>
  7. #include <split_scomm.h>
  8. #include "serial.h"
  9. #ifdef CONSOLE_ENABLE
  10. #include "debug.h"
  11. #endif
  12. uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
  13. uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
  14. uint8_t volatile status_com = 0;
  15. uint8_t volatile status1 = 0;
  16. uint8_t slave_buffer_change_count = 0;
  17. uint8_t s_change_old = 0xff;
  18. uint8_t s_change_new = 0xff;
  19. SSTD_t transactions[] = {
  20. #define GET_SLAVE_STATUS 0
  21. /* master buffer not changed, only recive slave_buffer_change_count */
  22. { (uint8_t *)&status_com,
  23. 0, NULL,
  24. sizeof(slave_buffer_change_count), &slave_buffer_change_count,
  25. },
  26. #define PUT_MASTER_GET_SLAVE_STATUS 1
  27. /* master buffer changed need send, and recive slave_buffer_change_count */
  28. { (uint8_t *)&status_com,
  29. sizeof(serial_master_buffer), (uint8_t *)serial_master_buffer,
  30. sizeof(slave_buffer_change_count), &slave_buffer_change_count,
  31. },
  32. #define GET_SLAVE_BUFFER 2
  33. /* recive serial_slave_buffer */
  34. { (uint8_t *)&status1,
  35. 0, NULL,
  36. sizeof(serial_slave_buffer), (uint8_t *)serial_slave_buffer
  37. }
  38. };
  39. void serial_master_init(void)
  40. {
  41. soft_serial_initiator_init(transactions, TID_LIMIT(transactions));
  42. }
  43. void serial_slave_init(void)
  44. {
  45. soft_serial_target_init(transactions, TID_LIMIT(transactions));
  46. }
  47. // 0 => no error
  48. // 1 => slave did not respond
  49. // 2 => checksum error
  50. int serial_update_buffers(int master_update)
  51. {
  52. int status, smatstatus;
  53. static int need_retry = 0;
  54. if( s_change_old != s_change_new ) {
  55. smatstatus = soft_serial_transaction(GET_SLAVE_BUFFER);
  56. if( smatstatus == TRANSACTION_END ) {
  57. s_change_old = s_change_new;
  58. #ifdef CONSOLE_ENABLE
  59. if (debug_matrix) {
  60. uprintf("slave matrix = %b %b %b %b\n",
  61. serial_slave_buffer[0], serial_slave_buffer[1],
  62. serial_slave_buffer[2], serial_slave_buffer[3]);
  63. }
  64. #endif
  65. }
  66. } else {
  67. // serial_slave_buffer dosen't change
  68. smatstatus = TRANSACTION_END; // dummy status
  69. }
  70. if( !master_update && !need_retry) {
  71. status = soft_serial_transaction(GET_SLAVE_STATUS);
  72. } else {
  73. status = soft_serial_transaction(PUT_MASTER_GET_SLAVE_STATUS);
  74. }
  75. if( status == TRANSACTION_END ) {
  76. s_change_new = slave_buffer_change_count;
  77. need_retry = 0;
  78. } else {
  79. need_retry = 1;
  80. }
  81. return smatstatus;
  82. }
  83. #endif // SERIAL_USE_MULTI_TRANSACTION
  84. #endif /* USE_SERIAL */