Subversion Repositories pentevo

Rev

Rev 100 | Rev 295 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3.  
  4. #include "mytypes.h"
  5. #include "rs232.h"
  6. #include "pins.h"
  7.  
  8. //if want Log than comment next string
  9. #undef LOGENABLE
  10. #define LOGENABLE
  11.  
  12. #define FOSC 11059200// Clock Speed
  13. #define BAUD115200 115200
  14. #define UBRR115200 (((FOSC/16)/BAUD115200)-1)
  15.  
  16. //Registers for 16550 emulation:
  17.  
  18. //Divisor Latch LSB
  19. static UBYTE rs232_DLL;
  20. //Divisor Latch MSB
  21. static UBYTE rs232_DLM;
  22. //Interrupt Enable
  23. static UBYTE rs232_IER;
  24. //Interrupt Identification
  25. static UBYTE rs232_IIR;
  26. //FIFO Control
  27. static UBYTE rs232_FCR;
  28. //Line Control
  29. static UBYTE rs232_LCR;
  30. //Modem Control
  31. static UBYTE rs232_MCR;
  32. //Line Status
  33. static UBYTE rs232_LSR;
  34. //Modem Status
  35. static UBYTE rs232_MSR;
  36. //Scratch Pad
  37. static UBYTE rs232_SCR;
  38. //Fifo In
  39. static UBYTE rs232_FI[16];
  40. static UBYTE rs232_FI_start;
  41. static UBYTE rs232_FI_end;
  42. //Fifo Out
  43. static UBYTE rs232_FO[16];
  44. static UBYTE rs232_FO_start;
  45. static UBYTE rs232_FO_end;
  46.  
  47. void rs232_init(void)
  48. {
  49.         // Set baud rate
  50.         UBRR1H = (UBYTE)(UBRR115200>>8);
  51.         UBRR1L = (UBYTE)UBRR115200;
  52.         // Clear reg
  53.         UCSR1A = 0;
  54.         // Enable receiver and transmitter
  55.         UCSR1B = (1<<RXEN)|(1<<TXEN);
  56.         // Set frame format: 8data, 1stop bit
  57.         UCSR1C = (1<<USBS)|(1<<UCSZ0)|(1<<UCSZ1);
  58.         // Set TXD pin
  59.         //RS232TXD_DDR |= (1<<RS232TXD);
  60.  
  61.         //Set default values:
  62.         rs232_IER = 0;
  63.         rs232_FCR = 0;
  64.         rs232_IIR = 0x01;
  65.         rs232_LCR = 0;
  66.         rs232_MCR = 0;
  67.         rs232_LSR = 0x60;
  68.         rs232_MSR = 0;
  69.         rs232_SCR = 0xFF;
  70.         rs232_FI_start = rs232_FI_end = 0;
  71.         rs232_FO_start = rs232_FO_end = 0;
  72. }
  73.  
  74. void rs232_transmit( UBYTE data )
  75. {
  76.         // Wait for empty transmit buffer
  77.         while ( !( UCSR1A & (1<<UDRE)) );
  78.         // Put data into buffer, sends the data
  79.         UDR1 = data;
  80. }
  81.  
  82. #ifdef LOGENABLE
  83. void to_log(char* ptr)
  84. {
  85.         while( (*ptr)!=0 )
  86.         {
  87.                 rs232_transmit(*ptr);
  88.                 ptr++;
  89.         }
  90. }
  91. #endif
  92.  
  93.  
  94. //after DLL or DLM changing
  95. void rs232_set_baud(void)
  96. {
  97.         if ( rs232_DLM | rs232_DLL )
  98.         {
  99.                 ULONG i = BAUD115200/ ((((UWORD)rs232_DLM)<<8) + rs232_DLL);
  100.                 UWORD rate = ((FOSC/16)/i)-1;
  101.                 // Set baud rate
  102.                 UBRR1H = (UBYTE)(rate>>8);
  103.                 UBRR1L = (UBYTE)rate;
  104.         }
  105. }
  106.  
  107. void rs232_zx_write(UBYTE index, UBYTE data)
  108. {
  109. #ifdef LOGENABLE
  110.         char log_write[] = "A..D..W\r\n";
  111.         log_write[1] = ((index >> 4) <= 9 )?'0'+(index >> 4):'A'+((index >> 4)-10);
  112.         log_write[2] = ((index & 0x0F) <= 9 )?'0'+(index & 0x0F):'A'+(index & 0x0F)-10;
  113.         log_write[4] = ((data >> 4) <= 9 )?'0'+(data >> 4):'A'+((data >> 4)-10);
  114.         log_write[5] = ((data & 0x0F) <= 9 )?'0'+(data & 0x0F):'A'+((data & 0x0F)-10);
  115.         to_log(log_write);
  116. #endif
  117.         switch( index )
  118.         {
  119.         case 0:
  120.                 if ( rs232_LCR & 0x80 )
  121.                 {
  122.                         rs232_DLL = data;
  123.                 }
  124.                 else
  125.                 {
  126.                         //place byte to fifo out
  127.                         //if ( rs232_FO_end )
  128.                         {
  129.                                 rs232_FO[rs232_FO_end] = data;
  130.                                 rs232_FO_end = (rs232_FO_end + 1) & 0x0F;
  131.                         }
  132.                 }
  133.                 break;
  134.  
  135.         case 1:
  136.                 if ( rs232_LCR & 0x80 )
  137.                 {
  138.                         //write to DLM
  139.                         rs232_DLM = data;
  140.                 }
  141.                 else
  142.                 {
  143.                         //bit 7-4 not used and set to '0'
  144.                         rs232_IER = data & 0x0F;
  145.                 }
  146.                 break;
  147.  
  148.         case 2:
  149.                 rs232_FCR = data;
  150.                 break;
  151.  
  152.         case 3:
  153.                 rs232_LCR = data;
  154.                 break;
  155.  
  156.         case 4:
  157.                 //bit 7-5 not used and set to '0'
  158.                 rs232_MCR = data & 0x1F;
  159.                 break;
  160.  
  161.         case 5:
  162.                 rs232_LSR = data;
  163.                 break;
  164.  
  165.         case 6:
  166.                 rs232_MSR = data;
  167.                 break;
  168.  
  169.         case 7:
  170.                 rs232_SCR = data;
  171.                 break;
  172.         }
  173. }
  174.  
  175. UBYTE rs232_zx_read(UBYTE index)
  176. {
  177.         UBYTE data = 0;
  178.         switch( index )
  179.         {
  180.         case 0:
  181.                 if ( rs232_LCR & 0x80 )
  182.                 {
  183.                         data = rs232_DLL;
  184.                 }
  185.                 else
  186.                 {
  187.                         //get byte from fifo in
  188.                         if ( rs232_FI_start != rs232_FI_end )
  189.                         {
  190.                                 data = rs232_FI[rs232_FI_start];
  191.                                 rs232_FI_start = ( rs232_FI_start + 1 ) & 0x0F;
  192.                         }
  193.                 }
  194.                 break;
  195.  
  196.         case 1:
  197.                 if ( rs232_LCR & 0x80 )
  198.                 {
  199.                         data = rs232_DLM;
  200.                 }
  201.                 else
  202.                 {
  203.                         data = rs232_IIR;
  204.                 }
  205.                 break;
  206.  
  207.         case 2:
  208.                 data = rs232_FCR;
  209.                 break;
  210.  
  211.         case 3:
  212.                 data = rs232_LCR;
  213.                 break;
  214.  
  215.         case 4:
  216.                 data = rs232_MCR;
  217.                 break;
  218.  
  219.         case 5:
  220.                 data = rs232_LSR;
  221.                 break;
  222.  
  223.         case 6:
  224.                 data = rs232_MSR;
  225.                 break;
  226.  
  227.         case 7:
  228.                 data = rs232_SCR;
  229.                 break;
  230.         }
  231. #ifdef LOGENABLE
  232.         static UBYTE last = 0;
  233.         if ( (last!=6) || (last!=index) )
  234.         {
  235.                 char log_read[] = "A..D..R\r\n";
  236.                 log_read[1] = ((index >> 4) <= 9 )?'0'+(index >> 4):'A'+((index >> 4)-10);
  237.                 log_read[2] = ((index & 0x0F) <= 9 )?'0'+(index & 0x0F):'A'+(index & 0x0F)-10;
  238.                 log_read[4] = ((data >> 4) <= 9 )?'0'+(data >> 4):'A'+((data >> 4)-10);
  239.                 log_read[5] = ((data & 0x0F) <= 9 )?'0'+(data & 0x0F):'A'+((data & 0x0F)-10);
  240.                 to_log(log_read);
  241.         }
  242.         last = index;
  243. #endif
  244.  
  245.         return data;
  246. }
  247.