home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / SIMTEL / HITECH-C / MOTOROLA.EXE / FEAT68K.C < prev    next >
C/C++ Source or Header  |  1993-04-21  |  4KB  |  143 lines

  1. #include    <intrpt.h>
  2.  
  3. /*
  4.  *    This source file demonstrates some of the special features
  5.  *    of the HI-TECH 68000 C Cross Compiler.  This program should be
  6.  *    compiled with 68000 code generation selected.
  7.  *
  8.  *    To generate the best possible code for this program, select
  9.  *    "68000 Code" from the options menu and "Full Optimization" from
  10.  *    the optimization menu.
  11.  *
  12.  *    HI-TECH C has a number of features designed to make programming
  13.  *    embedded processors simpler and more efficient, these include:
  14.  *
  15.  *    interrupt functions: interrupt handlers may be written entirely
  16.  *                 in C (no messy assembler code required).
  17.  *
  18.  *    absolute variables:  variables of any type (even complex
  19.  *                 structures and arrays) can be placed at
  20.  *                 any absolute address in memory - great
  21.  *                 for handling memory mapped devices.
  22.  *
  23.  *    in-line assembler:   assembler code can be place in line.  This
  24.  *                 is useful for accessing special instructions.
  25.  */
  26.  
  27. #ifndef    m68k
  28. #error    This code should be compiled with 68000 code generation selected.
  29. #endif
  30.  
  31. volatile unsigned char    PIA_STATUS @ 0xC0000000;    /* mem mapped PIA */
  32. volatile unsigned char    PIA_BUFFER @ 0xC0000001;    /* at C0000000 */
  33.  
  34. unsigned char        rxbuf[256];        /* 256 byte circular buffer */
  35. near unsigned char    ri_ptr, ro_ptr;        /* head, tail pointers */
  36.  
  37. volatile struct {
  38.     unsigned char    baud;        /* baud rate register */
  39.     unsigned char    sccr1;        /* control reg 1 */
  40.     unsigned char    sccr2;        /* control reg 2 */
  41.     unsigned char    scsr;        /* status register */
  42.     unsigned char    scdr;        /* data register */
  43. } SCC @ 0xC0000100;
  44.  
  45. /*
  46.  *    rx_intr handles receive interrupts from the on board serial port
  47.  *
  48.  *    Note the efficiency of the code generated for this function.  With
  49.  *    HI-TECH C there is no need to revert to assembly language coding
  50.  *    for interrupt handlers.
  51.  */
  52.  
  53. interrupt void
  54. rx_intr(void)
  55. {
  56.     rxbuf[ri_ptr++] = SCC.scdr;
  57.     if (ro_ptr == ri_ptr)        /* buffer overflow ? */
  58.         --ri_ptr;
  59. }
  60.  
  61. /*
  62.  *    serial_char_avail: returns 1 if a char is available, 0 otherwise
  63.  */
  64.  
  65. unsigned char
  66. serial_char_avail(void)
  67. {
  68.     return (ri_ptr != ro_ptr);
  69. }
  70.  
  71. /*
  72.  *    serial_get_char: returns one char from the tail of the circular
  73.  *                  buffer.  Assumes that a char is available.
  74.  */
  75.  
  76. unsigned char
  77. serial_get_char(void)
  78. {
  79.     unsigned char    ch;
  80.  
  81.     di();
  82.     ch = rxbuf[ro_ptr++];
  83.     ei();
  84.     return ch;
  85. }
  86.  
  87. /*
  88.  *    Transmit a character via the memory mapped PIA
  89.  *    bit 0 of STATUS register indicates it is ready to accept data
  90.  */
  91.  
  92. void
  93. pia_send(unsigned char ch)
  94. {
  95.     while (!(PIA_STATUS & 1))
  96.         continue;
  97.     PIA_BUFFER = ch;
  98. }
  99.  
  100. /*
  101.  *    Initialize the serial port and PIA 
  102.  *
  103.  *    Notes on code generated:
  104.  *
  105.  *        1.    Note the quality of the I/O port manipulation code
  106.  *            generated by the compiler for the serial port
  107.  *            and PIA initialization sequence below.
  108.  *
  109.  *        2.    The serial port initialization code below is "real"
  110.  *            and was taken from the source code for the 68HC11
  111.  *            "Lucifer" monitor.
  112.  */
  113.  
  114. #define    BAUD_VAL    0x30
  115.  
  116. void
  117. device_inits(void)
  118. {
  119.     unsigned char    i;
  120.  
  121.     di();
  122.     /* setup interrupt vector here */
  123.     SCC.baud = BAUD_VAL;
  124.     SCC.sccr1 = 0;
  125.     SCC.sccr2 = 0x0C;        /* rx and tx enable */
  126.     i = SCC.scsr;            /* read status register */
  127.     i = SCC.scdr;            /* read data register */
  128.     PIA_STATUS = 0x80;        /* reset PIA */
  129.     PIA_STATUS = 0x40;        /* mode 2: bidirectional, ints off */
  130.     ri_ptr = ro_ptr = 0;
  131.     ei();
  132. }
  133.  
  134. main()
  135. {
  136.     device_inits();
  137.     for (;;) {
  138.         if (serial_char_avail())
  139.             pia_send(serial_get_char());
  140.         /* other processing */
  141.     }
  142. }
  143.