usci.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #define TXIFG 0x02
  16. #define RXIFG 0x01
  17. #include "usci.h"
  18. /*
  19. int master;
  20. FILE *slave;
  21. int sp;
  22. char c;
  23. void *thrd (void *ctxt)
  24. {
  25. Usci *usci = (Usci *)ctxt;
  26. char buf[64] = {0};
  27. while (true) {
  28. usleep(333);
  29. if ( read(sp, buf, 1) > 0 ) {
  30. while (*usci->IFG2 & RXIFG);
  31. if (*buf == '\n') {
  32. *buf = '\r';
  33. }
  34. if (*buf == '\\') {
  35. // Ah, escape sequence, what will I parse it as?
  36. read(sp, buf, 1);
  37. if (*buf == 'h') {
  38. read(sp, buf, 2);
  39. buf[2] = 0;
  40. *usci->UCA0RXBUF = (uint8_t) strtoul(buf, NULL, 16);
  41. }
  42. }
  43. else {
  44. *usci->UCA0RXBUF = *(uint8_t *) buf;
  45. }
  46. *usci->IFG2 |= RXIFG;
  47. }
  48. }
  49. return NULL;
  50. }
  51. void open_pty (Emulator *emu)
  52. {
  53. Cpu *cpu = emu->cpu;
  54. char slavename[64], buf[64];
  55. struct termios termios_p;
  56. master = posix_openpt(O_RDWR);
  57. grantpt(master);
  58. unlockpt(master);
  59. ptsname_r(master, slavename, sizeof slavename);
  60. snprintf(buf, sizeof buf, "-S%s/%d", strrchr(slavename,'/')+1, master);
  61. // Child (pty)
  62. if( !fork() ) {
  63. char * const args[] = {
  64. "xterm", buf,
  65. NULL
  66. };
  67. setpgid(0, 0);
  68. execvp(args[0], args);
  69. exit(1);
  70. }
  71. // Parent
  72. sp = open(slavename, O_RDWR, O_NONBLOCK);
  73. read(sp, buf, 100);
  74. tcgetattr(sp, &termios_p);
  75. termios_p.c_lflag |= ECHO;
  76. tcsetattr(sp, 0, &termios_p);
  77. pthread_t t;
  78. if( pthread_create(&t, NULL, thrd, (void *)cpu->usci ) ) {
  79. fprintf(stderr, "Error creating thread\n");
  80. }
  81. }
  82. */
  83. void handle_usci (Emulator *emu)
  84. {
  85. Cpu *cpu = emu->cpu;
  86. Debugger *deb = emu->debugger;
  87. Usci *usci = cpu->usci;
  88. Port_1 *p1 = cpu->p1;
  89. static bool uart_active = false;
  90. // Handle sending from TX pin (P1.2)
  91. if (p1->SEL_2 && p1->SEL2_2) {
  92. if (uart_active == false) {
  93. puts("UART TX pin activated on P1.2");
  94. uart_active = true;
  95. }
  96. // UCAxTXIFG
  97. if (*usci->IFG2 & TXIFG) {
  98. uint8_t c = *usci->UCA0TXBUF;
  99. unsigned char str[2];
  100. str[0] = c;
  101. str[1] = 0;
  102. *usci->IFG2 &= ~TXIFG;
  103. if (c & 0xFF) {
  104. if (deb->web_interface) {
  105. print_serial(emu, (char*)&str[0]);
  106. //write(sp, usci->UCA0TXBUF, 1);
  107. }
  108. else if (deb->console_interface) {
  109. //write(sp, usci->UCA0TXBUF, 1);
  110. }
  111. *usci->UCA0TXBUF = 0;
  112. }
  113. //*usci->IFG2 &= TXIFG;
  114. *usci->IFG2 |= TXIFG;
  115. }
  116. }
  117. return;
  118. }
  119. void setup_usci (Emulator *emu)
  120. {
  121. Cpu *cpu = emu->cpu;
  122. Usci *usci = cpu->usci;
  123. static const uint16_t UCA0CTL0_VLOC = 0x60; // Control Register 0
  124. static const uint16_t UCA0CTL1_VLOC = 0x61; // Control Register 1
  125. static const uint16_t UCA0BR0_VLOC = 0x62; // Baud Rate ctl Register 0
  126. static const uint16_t UCA0BR1_VLOC = 0x63; // Baud Rate ctl Register 1
  127. static const uint16_t UCA0MCTL_VLOC = 0x64; // Modulation ctl Register
  128. static const uint16_t UCA0STAT_VLOC = 0x65; // Status Register
  129. static const uint16_t UCA0RXBUF_VLOC = 0x66; // RECV buffer register
  130. static const uint16_t UCA0TXBUF_VLOC = 0x67; // Transmit buffer register
  131. static const uint16_t UCA0ABCTL_VLOC = 0x5D; // Auto-Baud control register
  132. static const uint16_t UCA0IRTCTL_VLOC = 0x5E; // IrDA transmit control reg
  133. static const uint16_t UCA0IRRCTL_VLOC = 0x5F; // IrDA Receive control reg
  134. static const uint16_t IFG2_VLOC = 0x03; // Interrupt flag register 2
  135. // Set initial values
  136. *(usci->UCA0CTL0 = (uint8_t *) get_addr_ptr(UCA0CTL0_VLOC)) = 0;
  137. *(usci->UCA0CTL1 = (uint8_t *) get_addr_ptr(UCA0CTL1_VLOC)) = 0x01;
  138. *(usci->UCA0BR0 = (uint8_t *) get_addr_ptr(UCA0BR0_VLOC)) = 0;
  139. *(usci->UCA0BR1 = (uint8_t *) get_addr_ptr(UCA0BR1_VLOC)) = 0;
  140. *(usci->UCA0MCTL = (uint8_t *) get_addr_ptr(UCA0MCTL_VLOC)) = 0;
  141. *(usci->UCA0STAT = (uint8_t *) get_addr_ptr(UCA0STAT_VLOC)) = 0;
  142. *(usci->UCA0RXBUF = (uint8_t *) get_addr_ptr(UCA0RXBUF_VLOC)) = 0;
  143. *(usci->UCA0TXBUF = (uint8_t *) get_addr_ptr(UCA0TXBUF_VLOC)) = 0;
  144. *(usci->UCA0ABCTL = (uint8_t *) get_addr_ptr(UCA0ABCTL_VLOC)) = 0;
  145. *(usci->UCA0IRTCTL = (uint8_t *) get_addr_ptr(UCA0IRTCTL_VLOC)) = 0;
  146. *(usci->UCA0IRRCTL = (uint8_t *) get_addr_ptr(UCA0IRRCTL_VLOC)) = 0;
  147. usci->IFG2 = (uint8_t *) get_addr_ptr(IFG2_VLOC);
  148. *usci->IFG2 |= TXIFG;
  149. *usci->IFG2 &= ~RXIFG;
  150. }