usci.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. MSP430 Emulator
  3. Copyright (C) 2020 Rudolf Geosits (rgeosits@live.esu.edu)
  4. "MSP430 Emulator" is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. "MSP430 Emulator" is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "usci.h"
  16. /*
  17. int master;
  18. FILE *slave;
  19. int sp;
  20. char c;
  21. void *thrd (void *ctxt)
  22. {
  23. Usci *usci = (Usci *)ctxt;
  24. char buf[64] = {0};
  25. while (true) {
  26. usleep(333);
  27. if ( read(sp, buf, 1) > 0 ) {
  28. while (*usci->IFG2 & RXIFG);
  29. if (*buf == '\n') {
  30. *buf = '\r';
  31. }
  32. if (*buf == '\\') {
  33. // Ah, escape sequence, what will I parse it as?
  34. read(sp, buf, 1);
  35. if (*buf == 'h') {
  36. read(sp, buf, 2);
  37. buf[2] = 0;
  38. *usci->UCA0RXBUF = (uint8_t) strtoul(buf, NULL, 16);
  39. }
  40. }
  41. else {
  42. *usci->UCA0RXBUF = *(uint8_t *) buf;
  43. }
  44. *usci->IFG2 |= RXIFG;
  45. }
  46. }
  47. return NULL;
  48. }
  49. void open_pty (Emulator *emu)
  50. {
  51. Cpu *cpu = emu->cpu;
  52. char slavename[64], buf[64];
  53. struct termios termios_p;
  54. master = posix_openpt(O_RDWR);
  55. grantpt(master);
  56. unlockpt(master);
  57. ptsname_r(master, slavename, sizeof slavename);
  58. snprintf(buf, sizeof buf, "-S%s/%d", strrchr(slavename,'/')+1, master);
  59. // Child (pty)
  60. if( !fork() ) {
  61. char * const args[] = {
  62. "xterm", buf,
  63. NULL
  64. };
  65. setpgid(0, 0);
  66. execvp(args[0], args);
  67. exit(1);
  68. }
  69. // Parent
  70. sp = open(slavename, O_RDWR, O_NONBLOCK);
  71. read(sp, buf, 100);
  72. tcgetattr(sp, &termios_p);
  73. termios_p.c_lflag |= ECHO;
  74. tcsetattr(sp, 0, &termios_p);
  75. pthread_t t;
  76. if( pthread_create(&t, NULL, thrd, (void *)cpu->usci ) ) {
  77. fprintf(stderr, "Error creating thread\n");
  78. }
  79. }
  80. */
  81. void handle_usci (Emulator *emu) {
  82. Cpu *cpu = emu->cpu;
  83. Debugger *deb = emu->debugger;
  84. Usci *usci = cpu->usci;
  85. Port_1 *p1 = cpu->p1;
  86. static bool uart_active = false;
  87. // Handle sending from TX pin (P1.2)
  88. if (p1->SEL_2 && p1->SEL2_2) {
  89. if (uart_active == false) {
  90. puts("UART TX pin activated on P1.2");
  91. uart_active = true;
  92. }
  93. // UCAxTXIFG
  94. if (*usci->IFG2 & TXIFG) {
  95. uint8_t c = *usci->UCA0TXBUF;
  96. unsigned char str[2];
  97. str[0] = c;
  98. str[1] = 0;
  99. *usci->IFG2 &= ~TXIFG;
  100. if (c & 0xFF) {
  101. if (deb->web_interface) {
  102. print_serial(emu, (char*)&str[0]);
  103. //write(sp, usci->UCA0TXBUF, 1);
  104. }
  105. //else if (deb->console_interface) {
  106. //write(sp, usci->UCA0TXBUF, 1);
  107. //}
  108. *usci->UCA0TXBUF = 0;
  109. }
  110. //*usci->IFG2 &= TXIFG;
  111. *usci->IFG2 |= TXIFG;
  112. }
  113. }
  114. return;
  115. }
  116. void setup_usci (Emulator *emu)
  117. {
  118. Cpu *cpu = emu->cpu;
  119. Usci *usci = cpu->usci;
  120. static const uint16_t UCA0CTL0_VLOC = 0x60; // Control Register 0
  121. static const uint16_t UCA0CTL1_VLOC = 0x61; // Control Register 1
  122. static const uint16_t UCA0BR0_VLOC = 0x62; // Baud Rate ctl Register 0
  123. static const uint16_t UCA0BR1_VLOC = 0x63; // Baud Rate ctl Register 1
  124. static const uint16_t UCA0MCTL_VLOC = 0x64; // Modulation ctl Register
  125. static const uint16_t UCA0STAT_VLOC = 0x65; // Status Register
  126. static const uint16_t UCA0RXBUF_VLOC = 0x66; // RECV buffer register
  127. static const uint16_t UCA0TXBUF_VLOC = 0x67; // Transmit buffer register
  128. static const uint16_t UCA0ABCTL_VLOC = 0x5D; // Auto-Baud control register
  129. static const uint16_t UCA0IRTCTL_VLOC = 0x5E; // IrDA transmit control reg
  130. static const uint16_t UCA0IRRCTL_VLOC = 0x5F; // IrDA Receive control reg
  131. static const uint16_t IFG2_VLOC = 0x03; // Interrupt flag register 2
  132. // Set initial values
  133. *(usci->UCA0CTL0 = (uint8_t *) get_addr_ptr(UCA0CTL0_VLOC)) = 0;
  134. *(usci->UCA0CTL1 = (uint8_t *) get_addr_ptr(UCA0CTL1_VLOC)) = 0x01;
  135. *(usci->UCA0BR0 = (uint8_t *) get_addr_ptr(UCA0BR0_VLOC)) = 0;
  136. *(usci->UCA0BR1 = (uint8_t *) get_addr_ptr(UCA0BR1_VLOC)) = 0;
  137. *(usci->UCA0MCTL = (uint8_t *) get_addr_ptr(UCA0MCTL_VLOC)) = 0;
  138. *(usci->UCA0STAT = (uint8_t *) get_addr_ptr(UCA0STAT_VLOC)) = 0;
  139. *(usci->UCA0RXBUF = (uint8_t *) get_addr_ptr(UCA0RXBUF_VLOC)) = 0;
  140. *(usci->UCA0TXBUF = (uint8_t *) get_addr_ptr(UCA0TXBUF_VLOC)) = 0;
  141. *(usci->UCA0ABCTL = (uint8_t *) get_addr_ptr(UCA0ABCTL_VLOC)) = 0;
  142. *(usci->UCA0IRTCTL = (uint8_t *) get_addr_ptr(UCA0IRTCTL_VLOC)) = 0;
  143. *(usci->UCA0IRRCTL = (uint8_t *) get_addr_ptr(UCA0IRRCTL_VLOC)) = 0;
  144. usci->IFG2 = (uint8_t *) get_addr_ptr(IFG2_VLOC);
  145. *usci->IFG2 |= TXIFG;
  146. *usci->IFG2 &= ~RXIFG;
  147. }