//******************************************************************************
// Demo - MSP430F169, UART0, Send "Hello" with Iridium Modem
//
// Description - This code will configure a MSP430F169 to send a message with an
// Iridium modem (Model 9601-D from Nal Research) using UART0.  First P1.6/TA1
// (pin 18) will be configured for input to check if the modem has availability
// to the Iridium network and P3.4/UTXD0 and P3.5/URXD0 (pins 32 and 33) are
// configured for UART use.  UART0 is then enabled with an 8-bit character
// length and 9600 baud from the ACLK (external 32.768kHz crystal required).
// The USART state machine is then initialized with the RX interrupt enabled
// and the GIE bit is set.  First the program will send the AT command to allow
// data to be taken in by the modem.  Once is this complete a message letting
// the user know the modem is ready for data will print.  Next the program will
// send the modem the message "hello" along with the 2-byte checksum required.
// A message is then printed letting the user know the data has been accepted by
// the modem.  Now the program will check for network availability.  If the
// network is not visible by the modem a message will be printed out letting the
// user know.  It will continue this until the network is available.  Then the
// AT command to send the data will be sent to the modem. A message letting the
// user know the message is being sent will be printed.  If the data is sent
// a message will let the user know and the program will terminate.  If the
// data is not sent, a message will let the user know and repeat until the data
// is sent successfully.  If the program is terminated before it is complete the
// modem should be reset in order to avoid errors.  The modem should also be
// reset before the program is run.
//
// Note:  The modem must first be configured for 9600 baud and the echo of
// characters sent to the modem must be turned off.
//
// Stefan Gadomski
// UC Santa Cruz
// June 4, 2007
// Senior Design 123B
// Built with IAR Embedded Workbench Version: 3.42A
//******************************************************************************

#include  <msp430x16x.h>
#include  <stdio.h>

char string2[] = {"AT+SBDWB=5\r"};          // Command to transfer a 5 byte message to 9601-D buffer
char string3[] = {"AT+SBDI\r"};            // Command to perform SBD session            

unsigned char modem[20];
unsigned char network = 0x00;
unsigned int flag = 0;
unsigned int wait;
unsigned char i;
unsigned char j;

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog
  P1SEL |= 0x40;                            // Set P1.6/TA1 for I/O
  P1DIR &= 0x40;                            // Set P1.6/TA1 to input direction
  P3SEL = 0x30;                             // P3.4,5 = USART0 TXD/RXD
  ME1 |= UTXE0 + URXE0;                     // Enabled USART0 TXD/RXD
  UCTL0 |= CHAR;                            // 8-bit character, SWRST=1
  UTCTL0 |= SSEL0;                          // UCLK = ACLK
  UBR00 = 0x03;                             // 9600 from 32.768 kHz
  UBR10 = 0x00;                             //
  UMCTL0 = 0x4A;                            // Modulation for 9600
  UCTL0 &= ~SWRST;                          // Initialize USART state machine
  IE1 |= URXIE0;                            // Enable USART0 RX interrupt
  IFG1 &= ~UTXIFG0;                         // Clear inital flag on POR
  _BIS_SR( GIE );                           // Enable interrupts
 
  while ( flag == 0)
  {
    i = 0;
    j = 0;
    while (i < sizeof string2-1)            // Send command to tranfer data to modem buffer
    {
      while (!(U0TCTL & TXEPT));            // USART0 TX buffer ready?
      TXBUF0 = string2[i++];
    }
    
    for( wait = 0; wait < 5000; wait++);   // Wait for interrupt to receive all data

    printf ("Modem is ready to accept message\n");
      
    i = 0;
    j = 0;
 
    while (!(U0TCTL & TXEPT));
    TXBUF0 = 0x68;                        // h
    while (!(U0TCTL & TXEPT));
    TXBUF0 = 0x65;                        // e        
    while (!(U0TCTL & TXEPT));
    TXBUF0 = 0x6C;                        // l
    while (!(U0TCTL & TXEPT));
    TXBUF0 = 0x6C;                        // l
    while (!(U0TCTL & TXEPT));
    TXBUF0 = 0x6F;                        // o
    while (!(U0TCTL & TXEPT));
    TXBUF0 = 0x02;                        // Checksum high order byte
    while (!(U0TCTL & TXEPT));
    TXBUF0 = 0x14;                        // Checksum low order byte
  
    for( wait = 0; wait < 5000; wait++);  // Wait for interrupt to receive all data
    
    printf ("Message accepted\n");
  
    i = 0;
    j = 0;
  
    while( network == 0x00)
    {
      network = P1IN;                     // Check for network availability
      if( network == 0x00)
      {
      printf("No network available\n");
      }
    }
    printf("Network available\n");
    while (i < sizeof string3-1)
    {
      while (!(U0TCTL & TXEPT));                // USART0 TX buffer ready?
      TXBUF0 = string3[i++];
    }
  
    printf("Sending Message...\n");
  
    while ( j < 8);                        // Wait for interrupt to receive 8 byes of data
    for( wait = 0; wait < 5000; wait++);   // Wait for interrupt to receive rest of data
    
    if( modem[9] == 0x31)                  // If modem[9] = 1 message was sent
    {
      flag = 1;
      printf ("Message sent!\n");          // If modem[9] = 2 message was not sent
    }
    if(modem[9] == 0x32)
    {
      printf ("Message was not sent, trying again...\n");
    }
  }
}

// UART0 RX ISR
#pragma vector=UART0RX_VECTOR
__interrupt void usart0_rx (void)
{
  modem[j] = RXBUF0;
  j++;
}
