##############################################################################
#                                                                            #
# IAR ARM ANSI C/C++ Compiler V4.11A/W32               20/Jul/2004  18:58:47 #
# Copyright 1999-2004 IAR Systems. All rights reserved.                      #
#                                                                            #
#    Cpu mode        =  arm                                                  #
#    Endian          =  little                                               #
#    Stack alignment =  4                                                    #
#    Source file     =  C:\medusa\uart0.c                                    #
#    Command line    =  -I "C:\Program Files\IAR Systems\Embedded Workbench  #
#                       4.0\ARM\INC\" -lc C:\medusa\Debug\List\ -la          #
#                       C:\medusa\Debug\List\ -o C:\medusa\Debug\Obj\ -s2    #
#                       --no_cse --no_unroll --no_inline --no_code_motion    #
#                       --no_tbaa --no_clustering --no_scheduling --debug    #
#                       --cpu_mode arm --endian little --cpu ARM7TDMI-S      #
#                       --stack_align 4 -e --fpu None                        #
#                       "-D_DLIB_CONFIG_FILE=C:\Program Files\IAR            #
#                       Systems\Embedded Workbench                           #
#                       4.0\ARM\LIB\dl4tpannl8n.h" C:\medusa\uart0.c         #
#    List file       =  C:\medusa\Debug\List\uart0.lst                       #
#    Object file     =  C:\medusa\Debug\Obj\uart0.r79                        #
#                                                                            #
#                                                                            #
##############################################################################

C:\medusa\uart0.c
      1          #include "archinc.h"
      2          #include "pll.h"
      3          #include "eventq.h"
      4          #include "cli.h"
      5          #include "clichar.h"
      6          #include "uartregs.h"
      7          #include "uart0.h"
      8          
      9          #include <stdio.h>
     10          #include <limits.h>
     11          
     12          #ifdef CONFIG_INCLUDE_LPC210X_UART0
     13          
     14          static char rxQueue[CONFIG_UART0_RX_QUEUE_SIZE];
     15          static int rxHead = 0;
     16          static int rxTail = 0;
     17          static int rxLen = 0;
     18          static int maxRxLen = 0;
     19          static int rxErrors = 0;
     20          static int rxCharsDropped = 0;
     21          
     22          static char txQueue[CONFIG_UART0_TX_QUEUE_SIZE];
     23          static int txHead = 0;
     24          static int txTail = 0;
     25          static int txLen = 0;
     26          static int maxTxLen = 0;
     27          static int txErrors = 0;
     28          static int txCharsDropped = 0;
     29          
     30          static int maxIsrPassCount = 0;
     31          
     32          static boolean txIntEnable = false;
     33          
     34          /* ISR and deferred handler declaration */
     35          DECLARE_ISR(uart0Isr);
     36          
     37          /* baudrate list */
     38          static unsigned int validBaudrates[] = { 300, 1200, 2400, 9600, 19200, 38400, 57600, 115200 };
     39          
     40          static int setBaudrate(unsigned int baudrate)
     41          {
     42            int i;
     43            unsigned int divisor;
     44            int retVal = -1;
     45          
     46            for (i=0; i<SIZEOF_ARRAY(validBaudrates); i++) {
                               ^
Warning[Pe191]: type qualifier is meaningless on cast type
     47              if (baudrate == validBaudrates[i]) {
     48                retVal = 0;
     49                break;
     50                }
     51              }
     52          
     53            if (retVal == -1)
     54              return -1;
     55          
     56            /* calculate peripheral clock divisor using 16x oversampling */
     57          
     58            divisor = (pllGetPeriphFreq() / (16 * baudrate));
     59          
     60            U0LCR |= LCR_DLAB_ENABLE; /* Enable DLAB */
     61            U0DLL = (unsigned char)(divisor & 0xFF);
     62            U0DLM = (unsigned char)((divisor >> 8) & 0xFF);
     63            U0LCR &= ~(LCR_DLAB_ENABLE); /* Disable DLAB */
     64          
     65            return retVal;
     66          }
     67          
     68          
     69          static int uart0StatHandler(int argc, char *argv[], int flags)
     70          {
     71            int bufLen;
     72            char * pOutBuf;
     73            unsigned char putData;
                                 ^
Warning[Pe177]: variable "putData" was declared but never referenced

  static int rxErrors = 0;
             ^
"C:\medusa\uart0.c",19  Warning[Pe177]: variable "rxErrors" was declared but
          never referenced

  static int txErrors = 0;
             ^
"C:\medusa\uart0.c",27  Warning[Pe177]: variable "txErrors" was declared but
          never referenced
     74          
     75            pOutBuf = clicharGetOutputBuffer(&bufLen);
     76            sprintf(pOutBuf, "%s: maxTx[%d] maxRx[%d] maxIsrPass[%d]\r\n",argv[0], maxTxLen, maxRxLen, maxIsrPassCount);
     77            clicharSendOutputBuffer(pOutBuf, bufLen);
     78            sprintf(pOutBuf, "%s: txDrop[%d] rxDrop[%d] \r\n",argv[0], txCharsDropped, rxCharsDropped);
     79            clicharSendOutputBuffer(pOutBuf, bufLen);
     80          
     81            return 0;
     82          }
     83          
     84          int uart0Init(unsigned int baudrate) 
     85          {
     86            int i;
     87            int retVal = -1;
     88          
     89            // disable interrupts at the peripheral
     90            U0IER = 0;
     91          
     92            // initialize rx queue
     93            for(i=0; i<CONFIG_UART0_RX_QUEUE_SIZE; i++)
     94              rxQueue[i] = 0x00;
     95          
     96            // initialize tx queue
     97            for(i=0; i<CONFIG_UART0_TX_QUEUE_SIZE; i++)
     98              txQueue[i] = 0x00;
     99          
    100            // setup the baudrate
    101            //if (setBaudrate(baudrate) != 0)
    102            //  return retVal;
    103          
    104            setBaudrate(baudrate);
    105          
    106            // Setup FIFO: FIFO's automatically cleared :-) by setting LSB
    107            // U0FCR = (FIFO_ENABLE | FIFO_RX_TRG_N_8);
    108            U0FCR = FIFO_ENABLE; // 1 byte only for best response
    109          
    110            // setup data format: 8 data bits, 1 stop bit, no parity */
    111            U0LCR = LCR_WORDLEN_8BITS; 
    112          
    113            /* enable receiver only, only enable tx when we're ready to send */
    114          
    115            // setup interrupts at the VIC
    116            if (armvicIntConnect(LPC210X_IRQVEC_UART0, uart0Isr) == 0) {
    117              // enable interrupts at the VIC
    118              armvicIntEnable(LPC210X_IRQVEC_UART0);
    119              U0IER = IER_RX_DATA;
    120              retVal = 0;
    121              }
    122          
    123            cliRegisterCommand("u0stat", uart0StatHandler, "u0stat - display uart status info");
    124          
    125            return retVal;
    126          }
    127          
    128          int uart0Puts(char * putStr)
    129          {
    130            int retVal = 0;
    131          
    132            if (putStr == NULL)
    133              return -1;
    134          
    135            while ((*putStr != '\0') && (retVal != -1))
    136              retVal = uart0TxOne(*putStr++);
    137          
    138            return retVal;
    139          }
    140          
    141          int uart0TxOne(char txCh)
    142          {
    143            int retVal = -1;
    144          
    145            if (txIntEnable == false) {
    146              if (txLen < CONFIG_UART0_TX_QUEUE_SIZE) {
    147                txQueue[txHead] = txCh;
    148                circInc(txHead, 0, (CONFIG_UART0_TX_QUEUE_SIZE-1));
    149                txLen++;
    150                maxTxLen = ((txLen > maxTxLen) ? txLen : maxTxLen);
    151                retVal = 0;
    152                }
    153              else {
    154                return -1;
    155                }
    156          
    157              /*
    158               * send the first char in the queue right away and enable tx irq 
    159               * for subsequent chars. If there are no more chars to be sent, 
    160               * the tx ISR will turn off the transmitter available interrupt, 
    161               * otherwise the tx queue will be drained by the ISR.
    162               *
    163               */
    164          
    165              while (((U0LSR) & (LSR_TX_EMPTY | LSR_THR_EMPTY)) == 0)
    166                ;  // wait for THR to become empty
    167          
    168              U0THR = (lpc210x_reg)txQueue[txTail];
    169              circInc(txTail, 0, (CONFIG_UART0_TX_QUEUE_SIZE-1));
    170              txLen--;
    171              txIntEnable = true;
    172              U0IER = (IER_RX_DATA | IER_THRE);
    173              }
    174            else if (txLen < CONFIG_UART0_TX_QUEUE_SIZE) {
    175              IRQ_DISABLE();
    176              txQueue[txHead] = txCh;
    177              circInc(txHead, 0, (CONFIG_UART0_TX_QUEUE_SIZE-1));
    178              txLen++;
    179              IRQ_ENABLE();
    180              maxTxLen = ((txLen > maxTxLen) ? txLen : maxTxLen);
    181              retVal = 0;
    182              }
    183          
    184            return retVal;
    185          }
    186          
    187          int uart0RxOne(char *pRxCh)
    188          {
    189            if ((rxLen == 0) || (pRxCh == NULL))
    190              return -1;
    191          
    192            // update maxRxLen
    193            maxRxLen = ((rxLen > maxRxLen) ? rxLen : maxRxLen);
    194          
    195            IRQ_DISABLE();
    196            *pRxCh = rxQueue[rxTail];
    197            circInc(rxTail, 0, (CONFIG_UART0_RX_QUEUE_SIZE-1));
    198            rxLen--;
    199            IRQ_ENABLE();
    200          
    201            return 0;
    202          }
    203          
    204          static char dummy;
                             ^
Warning[Pe177]: variable "dummy" was declared but never referenced
    205          static lpc210x_reg iirReg;
    206          static lpc210x_reg lsrReg;
    207          
    208          static int isrPassCount;
    209          
    210          ISR_FUNCTION(uart0Isr)
    211          {
    212            ISR_ENTER()
    213          
    214            isrPassCount = 0;
    215          
    216            while ((((iirReg = (U0IIR)) & IIR_NO_IRQ) == 0) && (isrPassCount < 128)) {
    217              lsrReg = U0LSR;
    218              switch (iirReg & IIR_MASK) {
    219                case IIR_THRE:
    220                  /* transmitter available */
    221                  if (txLen > 0) {
    222                    if (lsrReg & (LSR_THR_EMPTY | LSR_TX_EMPTY)) {
    223                      U0THR = (lpc210x_reg)txQueue[txTail];
    224                      circInc(txTail, 0, (CONFIG_UART0_TX_QUEUE_SIZE-1));
    225                      txLen--;
    226                      }
    227                    }
    228                  else {
    229                    // we've transmitted all the characters, so turn the IRQ off
    230                    U0IER = IER_RX_DATA;
    231                    txIntEnable = false;
    232                    }
    233                  break;
    234                case IIR_RX_DATA:
    235                case IIR_CH_TMOUT:
    236                  /* received data available */
    237                  if (lsrReg & LSR_RX_DATA) {
    238                    if (rxLen < CONFIG_UART0_RX_QUEUE_SIZE) {
    239                      rxQueue[rxHead] = U0RBR;
    240                      circInc(rxHead, 0, (CONFIG_UART0_RX_QUEUE_SIZE-1));
    241                      rxLen++;
    242                      }
    243                    else {
    244                      rxCharsDropped++;
    245                      }
    246                    eventqPut(CONSOLE_RXCHAR);
    247                    }
    248                  break;
    249                case IIR_RX_LSR:
    250                  break;
    251                default:
    252                  break;
    253                  }
    254                isrPassCount++;
    255              }
    256          
    257            maxIsrPassCount = ((isrPassCount > maxIsrPassCount) ? isrPassCount : maxIsrPassCount);
    258            
    259            VICIrqDone;
    260            ISR_LEAVE()
    261          }
    262          
    263          #ifdef CONFIG_TOOLCHAIN_IAR
    264          void __write(int ch)
    265          #endif
    266          #ifdef CONFIG_TOOLCHAIN_ROWLEY
    267          void __putchar(int ch)
    268          #endif
    269          {
    270            if (ch == '\n')
    271              uart0TxOne('\r');
    272            uart0TxOne(ch);
    273          }
    274          
    275          
    276          #endif /* CONFIG_INCLUDE_LPC210X_UART0 */
    277          

   Maximum stack usage in bytes:

     Function         CSTACK CSTACK IRQSTACK
     --------         ------ ------ --------
     __write              8      0        0
     setBaudrate         20      0        0
     uart0Init           16      0        0
     uart0Isr             0     20        0
     uart0Puts           12      0        0
     uart0RxOne           4      0        0
     uart0StatHandler    36      0        0
     uart0TxOne           4      0        0


   Segment part sizes:

     Function/Label                 Bytes
     --------------                 -----
     rxQueue                          64
     rxHead                            4
     rxTail                            4
     rxLen                             4
     maxRxLen                          4
     rxCharsDropped                    4
     txQueue                         256
     txHead                            4
     txTail                            4
     txLen                             4
     maxTxLen                          4
     txCharsDropped                    4
     maxIsrPassCount                   4
     txIntEnable                       4
     validBaudrates                   32
     setBaudrate                     216
     uart0StatHandler                168
     uart0Init                       236
     uart0Puts                        80
     uart0TxOne                      584
     uart0RxOne                      220
     iirReg                            4
     lsrReg                            4
     isrPassCount                      4
     uart0Isr                        644
     __write                          40
     ??DataTable37                     4
     ??DataTable44                     4
     ??DataTable48                     4
     ??DataTable51                     4
     ??DataTable56                     4
     ??DataTable58                     4
     ??DataTable59                     4
     ??DataTable61                     4
     ??DataTable63                     4
     ??DataTable65                     4
     ??DataTable69                     4
     ?<Initializer for validBaudrates>
                                      32
     ?<Constant "%s: maxTx[%d] maxRx[%...">
                                      44
     ?<Constant "%s: txDrop[%d] rxDrop...">
                                      32
     ?<Constant "u0stat">              8
     ?<Constant "u0stat - display uart...">
                                      36
      Others                         156

 
 2 364 bytes in segment CODE
   120 bytes in segment DATA_C
    32 bytes in segment DATA_I
    32 bytes in segment DATA_ID
   380 bytes in segment DATA_Z
    24 bytes in segment INITTAB
 
 2 232 bytes of CODE  memory (+ 156 bytes shared)
   152 bytes of CONST memory
   412 bytes of DATA  memory

Errors: none
Warnings: 5
