5803 / Mbed 2 deprecated Code4

Dependencies:   mbed

Monitor.cpp

Committer:
gabinader
Date:
2018-02-28
Revision:
0:58bc93cc5d0f

File content as of revision 0:58bc93cc5d0f:

/**----------------------------------------------------------------------------
             \file Monitor.cpp
--                                                                           --
--              ECEN 5003 Mastering Embedded System Architecture             --
--                  Project 1 Module 3                                       --
--                Microcontroller Firmware                                   --
--                      Monitor.cpp                                            --
--                                                                           --
-------------------------------------------------------------------------------
--
--  Designed for:  University of Colorado at Boulder
--               
--                
--  Designed by:  Tim Scherr
--  Revised by:  Glenn Feinberg and George Abi-Nader
-- 
-- Version: 2.0
-- Date of current revision:  2016-09-29   
-- Target Microcontroller: Freescale MKL25ZVMT4 
-- Tools used:  ARM mbed compiler
--              ARM mbed SDK
--              Freescale FRDM-KL25Z Freedom Board
--               
-- 
   Functional Description: See below 
--
--      Copyright (c) 2015 Tim Scherr All rights reserved.
--
*/              

#include <stdio.h>
#include "shared.h"

//Const String Array to Debug Registers
const char* regNames[] = {"R0: ", "R1: ", "R2: ", "R3: ", "R4: ", "R5: ", "R6: ", "R7: ", "R8: ", "R9: ", "R10: ", "R11: ", "R12: ", "R13: ", "R14: ", "PC: "};  


/*******************************************************************************
* Set Display Mode Function
* Function determines the correct display mode.  The 3 display modes operate as 
*   follows:
*
*  NORMAL MODE       Outputs only mode and state information changes   
*                     and calculated outputs
*
*  QUIET MODE        No Outputs
*
*  DEBUG MODE        Outputs mode and state information, error counts,
*                    register displays, sensor states, and calculated output
*
*
* There is deliberate delay in switching between modes to allow the RS-232 cable 
* to be plugged into the header without causing problems. 
*******************************************************************************/

void set_display_mode(void)   
{
  UART_direct_msg_put("\r\nSelect Mode");
  UART_direct_msg_put("\r\n Hit NOR - Normal");
  UART_direct_msg_put("\r\n Hit QUI - Quiet");
  UART_direct_msg_put("\r\n Hit DEB - Debug" );
	UART_direct_msg_put("\r\n Hit RES - Reset" );
  UART_direct_msg_put("\r\n Hit V - Version#\r\n");
  UART_direct_msg_put("\r\nSelect:  ");  
}


//*****************************************************************************/
/// \fn void chk_UART_msg(void) 
///
//*****************************************************************************/
void chk_UART_msg(void)    
{
   UCHAR j;
   while( UART_input() )      // becomes true only when a byte has been received
   {                                    // skip if no characters pending
      j = UART_get();                 // get next character

      if( j == '\r' )          // on a enter (return) key press
      {                // complete message (all messages end in carriage return)
         UART_msg_put("->");
         UART_msg_process();
      }
      else 
      {
         if ((j != 0x02) )         // if not ^B
         {                             // if not command, then   
            UART_put(j);              // echo the character   
         }
         else
         {
           ;
         }
         if( j == '\b' ) 
         {                             // backspace editor
            if( msg_buf_idx != 0) 
            {                       // if not 1st character then destructive 
               UART_msg_put(" \b");// backspace
               msg_buf_idx--;
            }
         }
         else if( msg_buf_idx >= MSG_BUF_SIZE )  
         {                                // check message length too large
            UART_msg_put("\r\nToo Long!");
            msg_buf_idx = 0;
         }
         else if ((display_mode == QUIET) && (msg_buf[0] != 0x02) && 
                  (msg_buf[0] != 'D') && (msg_buf[0] != 'N') && 
                  (msg_buf[0] != 'V') && (msg_buf[0] != 'R') &&
                  (msg_buf_idx != 0))
         {                          // if first character is bad in Quiet mode
            msg_buf_idx = 0;        // then start over
         }
         else {                        // not complete message, store character
 
            msg_buf[msg_buf_idx] = j;
            msg_buf_idx++;
            if (msg_buf_idx > 2)
            {
               UART_msg_process();
            }
         }
      }
   }
}

//*****************************************************************************/
///  \fn void UART_msg_process(void) 
///UART Input Message Processing
//*****************************************************************************/
void UART_msg_process(void)
{
   UCHAR chr,err=0;
//   unsigned char  data;


   if( (chr = msg_buf[0]) <= 0x60 ) 
   {      // Upper Case
      switch( chr ) 
      {
         case 'D':
            if((msg_buf[1] == 'E') && (msg_buf[2] == 'B') && (msg_buf_idx == 3)) 
            {
               display_mode = DEBUG;
               UART_msg_put("\r\nMode=DEBUG\n");
               display_timer = 0;
            }
            else
               err = 1;
            break;

         case 'N':
            if((msg_buf[1] == 'O') && (msg_buf[2] == 'R') && (msg_buf_idx == 3)) 
            {
               display_mode = NORMAL;
               UART_msg_put("\r\nMode=NORMAL\n");
               //display_timer = 0;
            }
            else
               err = 1;
            break;

         case 'Q':
            if((msg_buf[1] == 'U') && (msg_buf[2] == 'I') && (msg_buf_idx == 3)) 
            {
               display_mode = QUIET;
               UART_msg_put("\r\nMode=QUIET\n");
               display_timer = 0;
            }
            else
               err = 1;
            break;

         case 'R':
            if((msg_buf[1] == 'E') && (msg_buf[2] == 'S') && (msg_buf_idx == 3)) 
            {
               //RESdisplay_mode = RESET;
               UART_msg_put("\r\nMode=RESETTING BOARD!\n");                   
   
							 UART_direct_msg_put("\r\nSystem Reset\r\nCode ver. ");
							 UART_direct_msg_put( CODE_VERSION );
							 UART_direct_msg_put("\r\n");
							 UART_direct_msg_put( COPYRIGHT );
							 UART_direct_msg_put("\r\n");
							
							 //Set All Values back to 0
               				display_timer = 0;
							 serial_flag = 0;
							
							 display_mode = QUIET;					
							
							 resetTimers(); 

							 set_display_mode();            
							
							err = 0;
            }
            else
               err = 1;
            break;

         case 'V':
            display_mode = VERSION;
            UART_msg_put("\r\n");
            UART_msg_put( CODE_VERSION ); 
            UART_msg_put("\r\nSelect  ");
            display_timer = 0;
            break;
                
         default:
            err = 1;
      }
   }

   else 
   {                                 // Lower Case
      switch( chr ) 
      {
        default:
         err = 1;
      }
   }

   if( err == 1 )
   {
      UART_msg_put("\n\rError!");
   }   
   else if( err == 2 )
   {
      UART_msg_put("\n\rNot in DEBUG Mode!");
   }   
   else
   {
    msg_buf_idx = 0;          // put index to start of buffer for next message
      ;
   }
   msg_buf_idx = 0;          // put index to start of buffer for next message


}


//*****************************************************************************
///   \fn   is_hex
/// Function takes 
///  @param a single ASCII character and returns 
///  @return 1 if hex digit, 0 otherwise.
///    
//*****************************************************************************
UCHAR is_hex(UCHAR c)
{
   if( (((c |= 0x20) >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f'))  )
      return 1;
   return 0;
}

 /**
 * @brief value of R0
 *
 * Gets the value of general purpose register R0
 *
 * @param[] 
 *  Void, takes no inputs  
 *
 * @return
 *  The return value is an integer: the content of R0.
 *   
 */
__asm int reg0(void)
	{
		BX	lr              ; return control to C	
	}

 /**
 * @brief value of general purpose register
 *
 * returns value of any general purpose register except R0
 *
 * @param[x] 
 *  integer the number of the register to be inspected. 
 *  Value range between 1 and 15
 *
 * @return
 *  The return value is an integer: the content of register R[x].
 *  For values outside the range 1-15, x is returned 
 */
__asm int reg(int x)
	{
		CMP r0 , #1		; is it asking for r1
		BNE two				; if not try r2
		MOV r0, r1		; otherwise return what is in r1
		BX lr
two
		CMP r0 , #2		
		BNE three			
		MOV r0, r2		
		BX lr
three
		CMP r0 , #3		
		BNE four			
		MOV r0, r3		
		BX lr
four
		CMP r0 , #4		
		BNE five			
		MOV r0, r4		
		BX lr	
five
		CMP r0 , #5		
		BNE six			
		MOV r0, r5		
		BX lr
six
		CMP r0 , #6		
		BNE seven			
		MOV r0, r6		
		BX lr
seven
		CMP r0 , #7		
		BNE eight			
		MOV r0, r7		
		BX lr
eight	
		CMP r0 , #8		
		BNE nine			
		MOV r0, r9		
		BX lr
nine
		CMP r0 , #9		
		BNE ten			
		MOV r0, r9		
		BX lr
ten 
		CMP r0 , #10		
		BNE eleven			
		MOV r0, r10		
		BX lr
eleven
		CMP r0 , #11		
		BNE twelve			
		MOV r0, r11		
		BX lr
twelve
		CMP r0 , #12		
		BNE thirteen			
		MOV r0, r12		
		BX lr
thirteen
		CMP r0 , #13		
		BNE fourteen			
		MOV r0, r13		
		BX lr
fourteen
		CMP r0 , #14		
		BNE done 			
		MOV r0, r14	
done		
		BX lr

	}
	
	 /**
 * @brief contents of memory location
 *
 * Gets the contents of a memory location
 *
 * @param[x] 
 *  32 bit address of memory 
 *
 * @return
 *  The return value is a 32 bit integer: the content of the memory location.
 *   
 */
__asm int content(uint32_t x )
	{
		LDR r0,[r0];
		BX	lr              ; return control to C	
	}

/*******************************************************************************
*   \fn  DEBUG and DIAGNOSTIC Mode UART Operation
*******************************************************************************/
void monitor(void)
{
	char tmp[20];
/**********************************/
/*     Spew outputs               */
/**********************************/

   switch(display_mode)
   {
      case(QUIET):
         {
             UART_msg_put("\r\n ");
             display_flag = 0;
         }  
         break;
      case(RESET):
         {
             UART_msg_put("\r\n RESETTING!!!!");
             display_flag = 0;
         }  
         break;
      case(VERSION):
         {
             display_flag = 0;
         }  
         break;         
      case(NORMAL):
         {
          
            if (display_flag == 1)
            {
			  UART_direct_msg_put("\r\n------------+------------+---------+--------+-----------+"); 
              UART_direct_msg_put("\r\nNORMAL MODE 											   "); 
			  UART_direct_msg_put("\r\n Time: ");  
              itoa(systemTime, tmp);
              UART_direct_msg_put(tmp);
              UART_direct_msg_put("\r\n Frequency: "); 
              itoa(F, tmp);
              UART_direct_msg_put(tmp);
              UART_direct_msg_put("\r\n Temperature C: "); 
              itoa(Temp, tmp);
              UART_direct_msg_put(tmp);
          	  UART_direct_msg_put("\r\n Flow G/min: "); 
              itoa(flowG, tmp);
              UART_direct_msg_put(tmp);              
		  	  UART_direct_msg_put("\r\n------------+------------+---------+--------+-----------+"); 
			  display_flag = 0;
              
            }
         }  
         break;
      case(DEBUG):
         {
            if (display_flag == 1)
            {				 
				UART_direct_msg_put("\r\n-----------+------------+---------+--------+-----------+"); 			
                UART_direct_msg_put("\r\nDEBUG MODE +"); 
                UART_direct_msg_put("\r\n------------+------------+---------+--------+-----------+"); 
            /*    
                UART_direct_msg_put("\r\n Time: "); 
             	itoa(systemTime, tmp);
           	    UART_direct_msg_put(tmp);
           	    UART_direct_msg_put("\r\n Frequency: "); 
                itoa(F, tmp);
                UART_direct_msg_put(tmp);
              	UART_direct_msg_put("\r\n Temperature C: "); 
              	itoa(Temp, tmp);
              	UART_direct_msg_put(tmp);
          	  	UART_direct_msg_put("\r\n Flow G/min: "); 
              	itoa(flowG, tmp);
              	UART_direct_msg_put(tmp);              
		  	   	UART_direct_msg_put("\r\n------------+------------+---------+--------+-----------+"); 
                
              */  
				UART_direct_msg_put("\r\n-----------+------------+---------+--------+-----------+");
							
							//Debug R0-R15
							UART_direct_msg_put("\r\n R0 to R15: "); 
							UART_direct_msg_put("\r\n");
							
							volatile uint32_t x;
							int i = 0;
							x = reg0();
							
							UART_direct_msg_put(regNames[i]);
							UART_direct_hex_put(x >> 24);
							UART_direct_hex_put(x >> 16);
							UART_direct_hex_put(x >> 8);
							UART_direct_hex_put(x);
							
							for ( i = 1; i < 16; i++)
							{
								UART_direct_msg_put("\r\n");
							  UART_direct_msg_put(regNames[i]); 
								x = reg(i);
								UART_direct_hex_put(x >> 24);
								UART_direct_hex_put(x >> 16);
								UART_direct_hex_put(x >> 8);
								UART_direct_hex_put(x);
							}
								UART_direct_msg_put("\r\n");  
					/*		
							//  Create a command to read 16 words from the current stack 
               // and display it in reverse chronological order.
								UART_direct_msg_put("\r\n 16 words of the stack, most recent first: "); 
								UART_direct_msg_put("\r\n"); 
								volatile uint32_t SP; // variable to hold the stack pointer
							
								SP = reg(13);				// register 13 is the stack pointer
								for( i = 0; i < 16; i++)
								{
									UART_direct_msg_put("\r\n"); 
									x = content(SP+4*i);
									UART_direct_hex_put(x >> 24);
									UART_direct_hex_put(x >> 16);
									UART_direct_hex_put(x >> 8);
									UART_direct_hex_put(x);
								}
						*/
               // clear flag to ISR      
               display_flag = 0;
             }   
         }  
         break;

      default:
      {
         UART_msg_put("Mode Error");
      }  
   }
}

/**
 * @brief Reverse the order of characters 
 *
 * Reverses the order of characters of a string 
 *
 * @param[*str, len] 
 *  
 * char *str pointer to the string to be reversed
 * unint32_t len, legnth of the string to be reversed
 *
 * @return
 *  
 *  Void, reversed string replaced the input  
 */
void reverse(char *str, uint32_t len)
{
    uint32_t i=0, j=len-1, temp;
    while (i<j)
    {
        temp = str[i];
        str[i] = str[j];
        str[j] = temp;
        i++; j--;
    }
}

/**
 * @brief Integer to Ascii
 *
 * creates ascii equivalent of an integer 
 *
 * @param[n, char] 
 *  
 * int32_t n, the integer that requires ascii equivalent 
 * char s[], string to receive the ascii characters
 *
 * @return
 *  
 *  Void, characters put in the passed string  
 */
void itoa(int32_t n, char s[])
{
   uint32_t i = 0;
   int32_t sign;
   
   if ((sign = n) < 0)  /* record sign */
      n = -n;          /* make n positive */
   i = 0;
   do {       /* generate digits in reverse order */
      s[i++] = n % 10 + '0';   /* get next digit */
   } while ((n /= 10) > 0);     /* delete it */
   if (sign < 0)
      s[i++] = '-';
   s[i] = '\0';
   reverse(s, i);
}