#define	UART_SPI		0x80
#define UART_RS232		0x00

/**
 * Define that sets the baudrate to 2400 Bit/s
 *
 * @see           #Comm_init(UINT8 bps)
 * @since         1.0
 */
#define BPS_2400       (0x01)

/**
 * Define that sets the baudrate to 4800 Bit/s
 *
 * @see           #Comm_init(UINT8 bps)
 * @since         1.0
 */
#define BPS_4800       (0x02)

/**
 * Define that sets the baudrate to 960000 Bit/s
 *
 * @see           #Comm_init(UINT8 bps)
 * @since         1.0
 */
#define BPS_9600       (0x03)

/**
 * Define that sets the baudrate to 14400 Bit/s
 *
 * @see           #Comm_init(UINT8 bps)
 * @since         1.0
 */
#define BPS_14400      (0x04)

/**
 * Define that sets the baudrate to 19200 Bit/s
 *
 * @see           #Comm_init(UINT8 bps)
 * @since         1.0
 */
#define BPS_19200      (0x05)

/**
 * Define that sets the baudrate to 38400 Bit/s
 *
 * @see           #Comm_init(UINT8 bps)
 * @since         1.0
 */
#define BPS_38400      (0x06)

/**
 * Define that sets the baudrate to 57600 Bit/s
 *
 * @see           #Comm_init(UINT8 bps)
 * @since         1.0
 */
#define BPS_57600      (0x07)

/**
 * Define that sets the baudrate to 1115200 Bit/s
 *
 * @see           #Comm_init(UINT8 bps)
 * @since         1.0
 */
#define BPS_115200     (0x08)

void _Comm_setup() {
	int i;

	Comm_logLevel = Configuration.boot_logLevel;
	Comm_clear();
	// init vars
	serial_pos = 0;
	for(i=0; i < SERIAL_BUFSIZE; i++) {
		serial_buf1[i] = 0x00;
		serial_buf2[i] = 0x00;
	}
	serial_line = 0;
	buf = serial_buf1;
	// Standard is to not use RTS/CTS by setting rts_bit and cts_bit to zero.
	rts_port = &P1IN;
	cts_port = &P1OUT;
	rts_bit = 0;
	cts_bit = 0;
	P3SEL |= 0xC0; // No select.
	
	// first time setup
	Comm_init( UART_RS232 | BPS_115200 );
	
	//
	// PIN configuration for SD Card
	//
	P5SEL |=  0x0E;  // 00 00 11 10  -> Dout, Din, Clk = peripheral
	P5SEL &= ~0x01;  // 00 00 00 01  -> Cs = I/O
	P5OUT |=  0x01;  // 00 00 00 01  -> Cs = High
	P5DIR |=  0x0D;  // 00 00 11 01  -> Dout, Clk, Cs = output
	P5DIR &= ~0x02;  // 00 00 00 10  -> Din = Input
	P2SEL &= ~0x40;  // 11 00 00 00  -> protect, detect = I/O
	P2DIR &= ~0x40;  // 11 00 00 00  -> protect, detect = input		
}	

bool Comm_init(UINT8 setting) {
	static UINT8 bps;
	
	/// SPI
	if( setting & UART_SPI ) {
		IE2 &= ~URXIE1;							// disable IRQ
		UCTL1 = CHAR | SYNC | MM | SWRST;		// 8-bit SPI Master **SWRST**
		UTCTL1 = CKPH | SSEL1 | SSEL0 | STC;	// SMCLK, 3-pin mode, clock idle low, data valid on rising edge, UCLK delayed
		UBR01 = 0x02;							// 0x02: UCLK/2 (4 MHz), works also with 3 and 4
		UBR11 = 0x00;							
		UMCTL1 = 0x00;							// no modulation
		ME2 |= USPIE1;							// Enable USART1 SPI mode
		UCTL1 &= ~SWRST;						// clear SWRST
		while (!(IFG2 & UTXIFG1));				// USART1 TX buffer ready (empty)?
	} else {
		if( setting & 0x0F ) != 0
			bps = setting & 0x0F;
		
		//////////////////////////////////////// RS232
		UCTL1  = SWRST;
		UCTL1 |= CHAR; // 8-bit character
		switch(bps) {
		case BPS_2400:
	      UTCTL1 = SSEL1;
	      UBR01 = 0x55;
	      UBR11 = 0x07;
	      UMCTL1 = 0x09;
	      break;
	     // RS232 with 2400 bps @ 32768 Hz ACLK
		case BPS_4800:
		  U0TCTL = SSEL1;
		  UBR01 = 0xAA;
		  UBR11 = 0x03;
		  UMCTL1 = 0x6D;
		  break; // 4800
		case BPS_9600:
		  UTCTL1 = SSEL1;
		  UBR01 = 0xD5;
		  UBR11 = 0x01;
		  UMCTL1 = 0x52;
		  break; // uart1 4505600Hz 14399bps
		case BPS_14400:
		  UTCTL1 = SSEL1;
		  UBR01 = 0x38;
		  UBR11 = 0x01;
		  UMCTL1 = 0xEF;
		  break; // uart1 4505600Hz 14399bps
		case BPS_19200:
		  UTCTL1 = SSEL1;
		  UBR01 = 0xEA;
		  UBR11 = 0x00;
		  UMCTL1 = 0x5B;
		  break; // uart1 4505600Hz 19197bps - RS232 with 19200 bps @  Mhz
		case BPS_38400:
		  UTCTL1 = SSEL1;
		  UBR01 = 0x75;
		  UBR11 = 0x00;
		  UMCTL1 = 0x52;
		  break; // uart1 4505600Hz 38378bps - RS232 with 38400 bps @  Mhz
		case BPS_57600:
		  UTCTL1 = SSEL1;
		  UBR01 = 0x4E;
		  UBR11 = 0x00;
		  UMCTL1 = 0x44;
		  break; // uart1 4505600Hz 57616bps - RS232 with 57600 bps @  Mhz
		case BPS_115200:
		  UTCTL1 = SSEL1;
		  UBR01 = 0x27;
		  UBR11 = 0x00;
		  UMCTL1 = 0x10;
		  break; // uart1 4505600Hz 115232bps - RS232 with 115200 bps @  Mhz
	  }
		ME2 |= UTXE1 + URXE1; // Enable USART1 TXD/RXD, disabling does not yield any powersavings
		UCTL1  &= ~SWRST;     // Last thing is reset
		SET_ISR(UART1RX_VECTOR, serial_rec);
		IE2 |= URXIE1;        // Enable USART1 RX interrupt
	}
	return True;
}
