/**********************************************/
/* Example Program For ET-BASE Z51F6412 V1.0  */
/* MCU      : Zilog MCS51 / Z51F6412          */
/*          : Run Internal RC (8.00 MHz)      */
/* Compiler : Keil C51 (V9.50a)               */
/* Editor   : uVision V4.53.0.6               */
/* Write By : Eakachai Makarn(ETT CO.,LTD.)   */
/* Function : Demo UART0,UART1 Interrupt Mode */
/* Update   : 18 August 2012                  */
/**********************************************/
// ET-BASE Z51F6412 Hardware Statdard I/O
// SW1    = P8[0] GPIO Input
// LED    = P8[1] GPIO Output
// BUZZER = P5[0] Buzzer Drive
// UART0  = P0[2]:TX0,P0[3]:RX0
// UART1  = P3[2]:TX1,P3[3]:RX1
//
#include <z51f6412.h>																							// Z8051 : Z51F6412
	
/******************************************/
/* ET-BASE Z51F6412 Hardware System Clock */
/******************************************/
#define FREQ_32KHZ    			(32768ul)															// 32.768 KHz
#define FREQ_125KHZ					(125000ul)														// 125 KHz
#define FREQ_1MHZ						(1000000ul)														// 1.00 MHZ
#define FREQ_2MHZ						(2000000ul)														// 2.00 MHz
#define FREQ_4MHZ						(4000000ul)   												// 4.00 MHz
#define FREQ_8MHZ						(8000000ul)   												// 8.00 MHz
#define FREQ_10MHZ					(10000000ul)   												// 10.00 MHz
#define FREQ_12MHZ					(12000000ul)  												// 12.00 MHz
#define FREQ_16MHZ					(16000000ul)  												// 16.00 MHz
#define FREQ_PLL	    			(14745600ul)  												// 14.7456MHz
#define MY_FREQ							FREQ_8MHZ

/*******************************************/
/* ET-BASE Z51F6412 Hardware UART Baudrate */
/*******************************************/
#define BAUD_1200     			(1200ul)															// 1200
#define BAUD_2400     			(2400ul)															// 2400
#define BAUD_4800     			(4800ul)															// 4800
#define BAUD_9600     			(9600ul)															// 9600
#define BAUD_19200    			(19200ul)															// 19200
#define BAUD_38400    			(38400ul)															// 38400
#define BAUD_57600    			(57600ul)															// 57600
#define BAUD_115200   			(115200ul)														// 115200
#define MY_BAUD							BAUD_9600					
	
/************************************/
/* ET-BASE Z51F6412 Hardware SW Pin */
/************************************/
#define SW_PIN							(1 << 0)															// P8[0] = SW
#define SW_PORT_DIR					P8IO																	// Port Direction
#define SW_PORT_DATA				P8																		// Port Data
#define SW_PORT_PULLUP			P8PU																	// Port Pull-Up
#define SW_PORT_DEBOUNCE		P8DB																	// Port Debounce
#define SW_PORT_INIT() 			SW_PORT_DIR      	&= ~(SW_PIN)				// SW = Input 
#define SW_LOGIC_INIT()     SW_PORT_DATA     	|=  (SW_PIN)				// SW Defualt Logic = "1"
#define SW_PULLUP_INIT()		SW_PORT_PULLUP   	|=  (SW_PIN)				// Enable Pull-Up SW Pin
#define SW_DEBOUNCE_INIT()  SW_PORT_DEBOUNCE 	|=  (SW_PIN)				// Enable Debounce SW Pin

/*************************************/
/* ET-BASE Z51F6412 Hardware LED Pin */
/*************************************/
#define LED_PIN							(1 << 1)															// P8[1] = LED
#define LED_PORT_DIR				P8IO
#define LED_PORT_DATA 			P8
#define LED_PORT_INIT()    	LED_PORT_DIR 			|=  (LED_PIN)				// LED = Output
#define LED_OFF()						LED_PORT_DATA			&= ~(LED_PIN) 			// LED Pin = 0
#define LED_ON()  					LED_PORT_DATA			|=  (LED_PIN) 			// LED Pin = 1
#define LED_TOGGLE()  			LED_PORT_DATA			^=  (LED_PIN) 			// LED Pin = Toggle

/*************************************/
/* ET-BASE Z51F6412 Hardware SPI Pin */
/*************************************/
#define SPI0_CS_PIN	  			(1 << 4)															// P3[4] = CS0#
#define SPI0_PORT_DIR				P3IO
#define SPI0_PORT_DATA			P3
#define SPI0_CS_INIT()  		SPI0_PORT_DIR 		|=  (SPI0_CS_PIN)
#define SPI0_CS_LOW()				SPI0_PORT_DATA  	&= ~(SPI0_CS_PIN)
#define SPI0_CS_HIGH()  		SPI0_PORT_DATA  	|=  (SPI0_CS_PIN)

#define SPI1_CS_PIN	  			(1 << 4)															// P4[4] = CS1#
#define SPI1_PORT_DIR				P4IO
#define SPI1_PORT_DATA			P4
#define SPI1_CS_INIT()  		SPI1_PORT_DIR 		|=  (SPI1_CS_PIN)
#define SPI1_CS_LOW()				SPI1_PORT_DATA  	&= ~(SPI1_CS_PIN)
#define SPI1_CS_HIGH()  		SPI1_PORT_DATA  	|=  (SPI1_CS_PIN)			

/**********************************
 * Start of UART FIFO Buffer Area *
 **********************************/
// FIFO USART Buffer
// Ring buffers for transfering data to and from the USART ISR:
//  - (Save pointer == Read pointer) is defined as an empty FIFO
//  - (Save pointer == Read pointer - 1), accounting for wraparound,
//    is defined as a completely full FIFO.  As a result, the max data 
//    in a FIFO is the buffer size - 1.
static volatile char UART_RX0_FIFO[32];														// 31 Byte Max. Receive
static volatile char UART_RX1_FIFO[32];														// 31 Byte Max. Receive
static volatile char *RX0SavePtr;
static volatile char *RX0ReadPtr;
static volatile char *RX1SavePtr;
static volatile char *RX1ReadPtr;

/***********************
 * Function Prototypes *
 ***********************/
void init_uart0(unsigned long freq, unsigned long baud);
void init_uart1(unsigned long freq, unsigned long baud);
void my_putchar0(char ch);
void my_putchar1(char ch);
void print_uart0(char *uart_buf);
void print_uart1(char *uart_buf);
char getchar0_fifo(void);
char getchar1_fifo(void);

/********************
 * Function: main() *
 ********************/
void main(void)
{	  
	char ch;
	
	/* Config System Clock = Internal RC 8.00 MHz */
	//PLLCR = 0,0,0,00,00,0
	// 0xxxxxxx : PLL Output Status
	// x0xxxxxx : PLL Output Bypass
	// xx0xxxxx : Power PLL = Default
	// xxx00xxx : FBDiv = Default
	// xxxxx00x : PLL M = Default
	// xxxxxxx0 : PLL Disable
	PLLCR = 0x00;																										// Disable PLL
	
	// SCCR = 0,01,0,0,1,00
	// 0xxxxxxx : Stop Mode = Mode 2
	// x01xxxxx : INTRC Clock Divide = INTRC(16MHz)/2 = 8MHz
	// xxx0xxxx : Clock Change By Hardware
	// xxxx0xxx : RC Oscillator Enable
	// xxxxx1xx : XTAL Oscillator Disable
	// xxxxxx00 : System Clock Source = INTRC(16MHz)
	SCCR = 0x24;  																									// INT-RC 8MHz	
	/* Now System Clock = 8.00MHz */
  
	// Initial UART0,UART1
	init_uart0(MY_FREQ, MY_BAUD);
  init_uart1(MY_FREQ, MY_BAUD);
	RX0SavePtr = &UART_RX0_FIFO[0];																	// UART0 Interrupt Buffer Service
  RX0ReadPtr = &UART_RX0_FIFO[0];
  RX1SavePtr = &UART_RX1_FIFO[0];																	// UART1 Interrupt Buffer Service
  RX1ReadPtr = &UART_RX1_FIFO[0];
	EA = 1;																													// Enable Glocbal Interrupt
	
	print_uart0("\n\rET-BASE Z51F6412 V1.0 Run Internal RC at 8MHZ");
	print_uart0("\n\rDemo UART0 RX Interrupt,TX Polling");
	print_uart0("\n\rUART0>");
	
	print_uart1("\n\rET-BASE Z51F6412 V1.0 Run Internal RC at 8MHZ");
	print_uart1("\n\rDemo UART1 RX Interrupt,TX Polling");
	print_uart1("\n\rUART1>");
  
	//Main Loop
	while(1)
	{
		// Verify uart0 receive
		if(RX0ReadPtr != RX0SavePtr)																	// If FIFO Buffer Data Receive
    {	
			ch = getchar0_fifo();																				// get char from fofo uart0
		  my_putchar0(ch);																						// echo char to uart0
			
		  if(ch==0x0D)																								// if Enter print prompt
		  {
			  print_uart0("\r\nUART0>"); 
      }
	  }	
		
		// Verify uart1 receive
    if(RX1ReadPtr != RX1SavePtr)																	// If FIFO Buffer Data Receive
    {	
			ch = getchar1_fifo();																				// get char from fofo uart1
		  my_putchar1(ch);																						// echo char to uart1
			
		  if(ch==0x0D)																								// if Enter print prompt
		  {			  
			  print_uart1("\r\nUART1>"); 
      }
	  }				
	} 
}

/*
 * Function    : init_uart0()
 * Description : initialize UART0 resources
 *             : baudrates, parirty, stop bit
 *             : Enable RX0 Interrupt
 */
void init_uart0(unsigned long freq, unsigned long baud)
{
	// UCTRL01 = 00,00,011,0
  // 00xxxxxx : UART Mode
  // xx00xxxx : No-Parity 
  // xxxx011x : 8 Bit Data
	// xxxxxxx0 : Don't Care in UART Mode
	UCTRL01 = 0x06;

	// UCTRL02 = 0,0,1,0,1,1,1,1
  // 0xxxxxxx : Disable Data Empty Interrupt
  // x0xxxxxx : Disable TX Complete Interrupt
	// xx1xxxxx : Enable RX Complete Interrupt
	// xxx0xxxx : Disable Wakeup Interrupt
	// xxxx1xxx : Enable TX UART
	// xxxxx1xx : Enable RX UART
	// xxxxxx1x : Enable UART Module Function
	// xxxxxxx1 : Enable Double Baudrate
	UCTRL02 = 0x2F;
	
	// UCTRL03 = 0,0,0,0,0,0,0,0
	// 0xxxxxxx : Don't Care in UART Mode
	// x0xxxxxx : Normal Mode
	// xx0xxxxx : Don't Care in UART Mode
	// xxx0xxxx : Don't Care in UARt Mode
	// xxxx0xxx : Reserve Bit
	// xxxxx0xx : 1 Stop Bit
	// xxxxxx0x : Don't Care TX8 in 8 Bit Mode
	// xxxxxxx0 : Don't Care RX8 in 8 Bit Mode	
	UCTRL03 = 0x00;
	
	// Config UBARDx = Double Speed Mode Baudrate 
	// baudrate = (Fx / (8 * (UBARDx + 1)))
	// UBAUDx   = (Fx / (baudrate * 8))-1
	// Example Fx:8MHz,Baudrate:9600
	// UBAUDx   = (8MHz/(9600*8))-1
	//          = (8MHz/76800)-1
	//          = 104.166-1
  //          = 103.166
  //          = 103 = 0x67  
	UBAUD0 = (freq  / ((unsigned long)baud * 8ul)) - 1ul;
	
	// xxxxxxx1 :	Enable UART0 RX Interrupt									
	IE1 |= 0x01;	
}

/*
 * Function    : init_uart1()
 * Description : initialize UART1 resources
 *             : baudrates, parirty, stop bit
 *             : Enable RX1 Interrupt
 */
void init_uart1(unsigned long freq, unsigned long baud)
{
	// UCTRL11 = 00,00,011,0
  // 00xxxxxx : UART Mode
  // xx00xxxx : No-Parity 
  // xxxx011x : 8 Bit Data
	// xxxxxxx0 : Don't Care in UART Mode
	UCTRL11 = 0x06;

	// UCTRL12 = 0,0,1,0,1,1,1,1
  // 0xxxxxxx : Disable Data Empty Interrupt
  // x0xxxxxx : Disable TX Complete Interrupt
	// xx1xxxxx : Enable RX Complete Interrupt
	// xxx0xxxx : Disable Wakeup Interrupt
	// xxxx1xxx : Enable TX UART
	// xxxxx1xx : Enable RX UART
	// xxxxxx1x : Enable UART Module Function
	// xxxxxxx1 : Enable Double Baudrate
	UCTRL12 = 0x2F;
	
	// UCTRL13 = 0,0,0,0,0,0,0,0
	// 0xxxxxxx : Don't Care in UART Mode
	// x0xxxxxx : Normal Mode
	// xx0xxxxx : Don't Care in UART Mode
	// xxx0xxxx : Don't Care in UARt Mode
	// xxxx0xxx : Reserve Bit
	// xxxxx0xx : 1 Stop Bit
	// xxxxxx0x : Don't Care TX8 in 8 Bit Mode
	// xxxxxxx0 : Don't Care RX8 in 8 Bit Mode	
	UCTRL13 = 0x00;
		
	// Config UBARDx = Double Speed Mode Baudrate 
	// baudrate = (Fx / (8 * (UBARDx + 1)))
	// UBAUDx   = (Fx / (baudrate * 8))-1
	// Example Fx:8MHz,Baudrate:9600
	// UBAUDx   = (8MHz/(9600*8))-1
	//          = (8MHz/76800)-1
	//          = 104.166-1
  //          = 103.166
  //          = 103 = 0x67  
	UBAUD1 = (freq  / ((unsigned long)baud * 8ul)) - 1ul;
	
	// xxx1xxxx :	Enable UART1 RX Interrupt									
	IE1 |= 0x10;	
}

/*
 * Function    : my_putchar0
 * Description : Output a character to uart0
 */
void my_putchar0(char ch)
{
  while(!(USTAT0 & 0x80));																				// Wait until Trammit Buffer is Empty
	UDATA0 = ch;
}

/*
 * Function    : my_putchar1
 * Description : Output a character to uart1
 */
void my_putchar1(char ch)
{
  while(!(USTAT1 & 0x80));																				// Wait until Trammit Buffer is Empty
	UDATA1 = ch;
}

/*
 * Function    : print_uart0
 * Description : print a string to uart0
 */
void print_uart0(char *uart_buf)
{
	while(*uart_buf != '\0')
	{	
	  my_putchar0(*uart_buf++);	
	}	
}

/*
 * Function    : print_uart1
 * Description : print a string to uart1
 */
void print_uart1(char *uart_buf)
{
	while(*uart_buf != '\0')
  {
	  my_putchar1(*uart_buf++);	
	}	
}

/*
 * Function			: getchar0_fifo()
 * Description  : read a character from uart0 fifo
 */
char getchar0_fifo(void)
{
	char rx0_chr;

  rx0_chr = *RX0ReadPtr++;														
  if(RX0ReadPtr >= &UART_RX0_FIFO[0] + sizeof(UART_RX0_FIFO)) 
	{	
    RX0ReadPtr = &UART_RX0_FIFO[0];
  }
	
  return (rx0_chr);
}

/*
 * Function			: getchar1_fifo()
 * Description  : read a character from uart1 fifo
 */
char getchar1_fifo(void)
{
	char rx1_chr;

  rx1_chr = *RX1ReadPtr++;														
  if(RX1ReadPtr >= &UART_RX1_FIFO[0] + sizeof(UART_RX1_FIFO)) 
	{	
    RX1ReadPtr = &UART_RX1_FIFO[0];
  }
	
  return (rx1_chr);
}

/*
 *  Function    : RX0_ISR()
 *  Description : UART0 Receive Interrupt.
 *  If Used Keil Compiler Set Vector = 6(Address Vector:0x0033)
 */
void RX0_ISR(void) interrupt 6 	using 1
{
	char ch;
	
	ch = UDATA0;																										// Get UART0 Data
	
	// Ring buffers for transfering data to and from the USART ISR:
  //  - (Save pointer == Read pointer) is defined as an empty FIFO
  //  - (Save pointer == Read pointer - 1), accounting for wraparound,
  //    is defined as a completely full FIFO.  As a result, the max data 
  //    in a FIFO is the buffer size - 1.
  // Copy the byte into the local FIFO, if it won't cause an overflow
  if(RX0SavePtr != RX0ReadPtr - 1)
	{
	  if((RX0SavePtr != &UART_RX0_FIFO[0] + sizeof(UART_RX0_FIFO)) || (RX0ReadPtr != &UART_RX0_FIFO[0]))
	  {
	    *RX0SavePtr++ = ch;
		  if(RX0SavePtr >= &UART_RX0_FIFO[0] + sizeof(UART_RX0_FIFO)) 
		  RX0SavePtr = &UART_RX0_FIFO[0];		  
	  }
	}
}

/*
 *  Function    : RX1_ISR()
 *  Description : UART1 Receive Interrupt.
 *  If Used Keil Compiler Set Vector = 10(Address Vector:0x0053)
 */
void RX1_ISR(void) interrupt 10 using 2
{
	char ch;
	
	ch = UDATA1;																										// Get UART1 Data
	
	// Ring buffers for transfering data to and from the USART ISR:
  //  - (Save pointer == Read pointer) is defined as an empty FIFO
  //  - (Save pointer == Read pointer - 1), accounting for wraparound,
  //    is defined as a completely full FIFO.  As a result, the max data 
  //    in a FIFO is the buffer size - 1.
  // Copy the byte into the local FIFO, if it won't cause an overflow
  if(RX1SavePtr != RX1ReadPtr - 1)
	{
	  if((RX1SavePtr != &UART_RX1_FIFO[0] + sizeof(UART_RX1_FIFO)) || (RX1ReadPtr != &UART_RX1_FIFO[0]))
	  {
	    *RX1SavePtr++ = ch;
		  if(RX1SavePtr >= &UART_RX1_FIFO[0] + sizeof(UART_RX1_FIFO)) 
		  RX1SavePtr = &UART_RX1_FIFO[0];		  
	  }
	}
}



