AnsweredAssumed Answered

USART2 problem

Question asked by bn.yogesh on May 2, 2013
Latest reply on Sep 27, 2013 by Clive One
code is here ....
/*
question :
Simple Embedded Shell
 */
/**********************************************************************************************************
                        YOGESH BN
                        09424
                        ME SSA
                        ELECTRICAL

**********************************************************************************************************/


//USART PROGRAM

//    STM32L1xxx DRIVER HEADER FILES
#include "stm32l1xx.h"
#include "stm32l1xx_rcc.h"
#include "stm32l1xx_pwr.h"
#include "stm32l1xx_usart.h"
#include "stm32l1xx_gpio.h"

//    DISCOVERY BOARD DRIVER HEADER FILES
#include "discover_board.h"
#include "discover_functions.h"

//#include "command.h"

#define USART_Send(a) USART_SendData(USART1, a);\
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == 0)

void initcommands(void);
void command_help(void);
void command_ledon(void);
void command_ledoff(void);
void command_rl(void);
void command_rw(void);
void command_rb(void);
void command_wl(void);
void command_ww(void);
void command_wb(void);



unsigned int* addr_ptr;//Pointer to address received from command

static volatile uint32_t TimingDelay;
uint8_t state_machine;
uint16_t Int_CurrentSTBY;


char received_data_buffer[100];
uint32_t data_count;
uint32_t received_data;
char checkname[10], data[11], addr[11];
uint32_t name_len,data_len,addr_len;
unsigned int no_of_spaces;
unsigned int match=0;
unsigned int parse_index;
unsigned int compare_index;
unsigned int validity_index;
unsigned int check_data; // variable which store decimal value of data entered
unsigned int check_addr; // variable which store decimal value of address entered
unsigned int* addr_ptr;//Pointer to address received from command

static char message[]={"******A simple shell program****** \n"};
static char message1[]={"Enter the command : \n"};
static char function_name_list[9][7]={"help\n" , "ledon\n" , "ledoff\n", "w.l\n","w.w\n","w.b\n", "r.l\n","r.w\n","r.b\n"};
static char function_helptext_list[9][150]={"help   : lists different commands available and their syntax \n",
                                            "ledon  : to turn on  the led \n",
                                            "ledoff :to turn off the led \n",
                                            "w.l    : to write thirty two bit data as per the address  \n",
                                            "w.w    : write sixteen    bit data as per the address  \n",
                                            "w.b    : write eight      bit data as per the address  \n",
                                            "r.l    : Reads len number of thirty two bit word starting from the address addr and print them on the terminal in hexadecimal format \n",
                                            "r.w    : Reads len number of sixteen    bit word starting from the address addr and print them on the terminal in hexadecimal format \n",
                                            "r.b    : Reads len number of eight      bit word starting from the address addr and print them on the terminal in hexadecimal format \n"};

struct command
{
  char *name;
  char *helptext;
  void (*function)();
}commands[9];

/**********************************************************************************************************/
int main()
 {
   Init();        //INITIALIZES SYSTEM
   initcommands();
   while(1)
     {

      receive();
      parse_index = parse_data(); // returns 1 if error
      compare_index = compare_name(checkname);
      validity_index=check_validity(); //returns command index if valid else return 10
      if(validity_index<9)
         execute_command(validity_index);
      else
        send_data("ERROR!!! SORRY :( \n");
 //     while(1);
     } // end of while
      return 0;
 } // end of main()
/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/
//Convert to Hex character
char hex_ascii(char value)
{
    switch(value)
    {
        case 10:
            {
                return 'A';
                break;
            }
        case 11:
        {
            return 'B';
            break;
        }

        case 12:
        {
            return 'C';
            break;
        }

        case 13:
        {
            return 'D';
            break;
        }

        case 14:
        {
            return 'E';
            break;
        }

        case 15:
        {
            return 'F';
            break;
        }

        default:
            {
                return (value + 48);
                break;
            }

    }
}

/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/

void command_wl()
{
    send_data("writing 32 bit data \n");
    send_hex(check_data);
    send_data("to address \n");
    send_hex(check_addr);
    addr_ptr  = (unsigned int*)check_addr;//Point to the address
    *addr_ptr = check_data;//Write 32 bit data
}
/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/

void command_ww()
{
    send_data("writing 16 bit data \n");
    send_hex((0x0000FFFF & check_data));
    send_data("to address \n");
    send_hex(check_addr);
    addr_ptr  = (unsigned int*)check_addr;//Point to the address
    *addr_ptr = (((*addr_ptr) & 0xFFFF0000) | (0x0000FFFF & check_data));//Write 16 bit data
}

/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/
//Command functions: w.b
void command_wb()
{
    send_data("writing 8 bit data \n");
    send_hex((0x000000FF & check_data));
    send_data("to address \n");
    send_hex(check_addr);
    addr_ptr  = (unsigned int*)check_addr;//Point to the address
    *addr_ptr = (((*addr_ptr) & 0xFFFFFF00) | (0x000000FF & check_data));//Write 08 bit data
}

/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/
void command_rl()
{
    unsigned int i, temp;

    addr_ptr = (unsigned int*)check_addr;//Point to the address

    for(i = 0; i < check_data; i++)
    {
        //send_data("inside for \n");
        if(i % 4 == 0)
        {
//            send_data("The address is : \n");
            send_hex((unsigned int)addr_ptr);
        }

        temp     = *addr_ptr;//Read data
        send_data(" Data is : \n");
        send_hex(temp);//Send data
        addr_ptr =  addr_ptr + 1;//Point to next location

        //USART_Send_Dec(temp);
//        send_data("outside if  \n");
    }

}
/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/

//Command functions: r.w
void command_rw()
{
    unsigned int i, temp, temp1;

    addr_ptr = (unsigned int*)check_addr;//Point to the address

    for(i = 0; i < check_data; i++)
     {
        if(i % 8 == 0)
        {
            send_data("The address is : \n");
            send_hex((unsigned int)addr_ptr);
        }

        temp     = *addr_ptr;//Read data

        temp1 = 0x0000FFFF & temp;//Lower 16 bits
        temp  = (0xFFFF0000 & temp) >> 16;//Upper 16 bits
        addr_ptr =  addr_ptr + 1;//Point to next location

        //USART_Send_Dec(temp);
        send_data(" Data is : \n");
        send_hex(temp1);//Send data
//        USART_Send(' ');

        if((i + 1) <= check_data)
        {
            send_data("Next data is : \n");
            send_hex(temp);//Send data
            i++;
        }
    }
}

//Command functions: r.b
void command_rb()
{
    unsigned int i,j, temp[4];

    addr_ptr = (unsigned int*)check_addr;//Point to the address

    for(i = 0; i < check_data; i++)
    {
        if(i % 16 == 0)
        {
            send_data("The address is : \n");
            send_hex((unsigned int)addr_ptr);
        }

        temp[3]  = *addr_ptr;//Read data
        addr_ptr =  addr_ptr + 1;//Point to next location

        temp[0] =  0x000000FF & temp[3];//Lower 8 bits
        temp[1] = (0x0000FF00 & temp[3]) >> 8;//Next 8 bits
        temp[2] = (0x00FF0000 & temp[3]) >> 16;//Next 8 bits
        temp[3] = (0xFF000000 & temp[3]) >> 24;//Upper 8 bits

        send_data("Data is : \n");
        send_hex(temp[0]);//Send data

        for(j=1; ((i + 1) < check_data) && (j < 4); j++, i++)
        {
            send_data("Next data : \n");
            send_hex(temp[j]);//Send data
            //USART_Send(' ');
        }
    }
}



void execute_command(unsigned int val)
 {
    commands[val].function();
 }
/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/
int compare_name(char string[])
 {
//    send_data("inside compare name \n");
    unsigned int i,j=0;
    for(i=0;i<9;i++)
      for(j=0;string[j]!='\n';j++)
      {
//        send_data(commands[i].name);
        if(commands[i].name[j]==string[j])  // if characters not equal or entered commandlength > commandlength
            {
//                USART_SendData(USART3,string[j]);
//                while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == 0);//WAIT UNTILCHARACTER IS SEND
//                USART_SendData(USART3,j+48);
//                   while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == 0);//WAIT UNTILCHARACTER IS SEND

              if(commands[i].name[j+1]=='\n' && string[j+1]=='\n')
               {
                  send_data("correct match \n");
                  send_data(commands[i].name);
                  match=1;
                  return i;
               }
            }

        else if(commands[i].name[j]!=string[j])
          {
//            send_data("no match this time \n");
            break;
          }
      } //end of for loop
  }
/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/

int check_validity( void )
 {
//    send_data("INSIDE CHECK_VALIDITY\n");
    if(match==0)
      {
          send_data("no match \n");
          return 10;
      }
    else
      {
        if(compare_index<=2)// for help, ledon, ledoff
         {
            if(no_of_spaces==0 && data_len==0&&addr_len==0)
                 return compare_index;
            else
                {
                 send_data("Recheck the command entered!!  Extra arguments or space entered \n");
                 return 10;
                }
         }
        else // for remaining commands with arguments
         {
            if(data_len==0 || addr_len==0)
             {
              send_data("Very few arguments(enter address and data)");
              return 10;
             }
//           send_data("checking address... \n");
             check_addr=stringtodec(addr,addr_len-1);
//           send_data("checking data... \n");
              check_data=stringtodec(data,(data_len-1));

              if((((check_addr >=0x20000000 ) && (check_addr <=( 0x20000000 + 0x3FFF) )) || ((check_addr >= 0x40000000) && (check_addr <= 0x400263FF))) &&(check_addr!=0))
                  return compare_index;
              else
                  send_data("Invalid address!! specified memory not accessible!!\n");

         }//end of else of if(compare_index<=2)// for help, ledon, ledoff
      } //end of if match==1

 }
/************************************************************************************************************************************
 * function name : int stringtodec(char)
 * Description   : checks if entered char array is hex or deci and converts into deci
 ************************************************************************************************************************************/

int stringtodec(char array[],unsigned int len)
 {
    int i,num=0,temp=1;
    int hex_match=0; //dec_match=0;
   //int num;
//    send_data("array is : \n");
//    send_data(array);

    if(array[0]=='0' && (array[1]=='x' || array[1]=='X')) //hex
     {
//        send_data("hex !!\n");
        for(i=len;i>=2;i--)
          {
          if((array[i]>='0'&& array[i]<='9') || ( array[i]>='a'&& array[i]<='f') || ( array[i]>='A'&& array[i]<='F') )
             {
            if(array[i]>='0'&& array[i]<='9')
                array[i]=(array[i]-48);

            else if(array[i]>='A'&& array[i]<='F')
               array[i]=(array[i]-55);

            else if(array[i]>='a'&& array[i]<='f')
                array[i]=(array[i]-87);

            num = num + (((unsigned int)array[i]) * temp);
            temp*=16;
//            send_dec(num);
            hex_match=1;
           }
          else
              {
               hex_match=0;
               send_data("Recheck values entered!! value not hex\n");
               return 0;
              }
         }

//          send_dec(num);
          return num;

     }//end of if(data[0]=='0' && (data[1]=='x'||data[1]=='X')) //hex
    else// if decimal
     {
//       num=0;temp=1;
//       send_data("dec !!\n");
         for(i=len;i>=0;i--)
        {
           if(array[i]>='0'&& array[i]<='9')
          {
            num+=(array[i]-48)*temp;
            temp*=10;
//            USART_SendData(USART3,array[i]);        // SEND LINE FEED
//            while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == 0);//WAIT UNTIL EACH CHARACTER IS SEND

//            USART_SendData(USART3,10);        // SEND LINE FEED
//            while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == 0);//WAIT UNTIL EACH CHARACTER IS SEND
//            USART_SendData(USART3,13);        // SEND CARRIAGE RETURN
//            while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == 0);//WAIT UNTIL EACH CHARACTER IS SEND
//            send_dec(num);
         }
        else
         if(hex_match==0)
          {
             send_data("error!! neither deci nor hex\n");
             return 0;
          }
        }
      }
//    send_dec(num);
    return num;
 }

/************************************************************************************************************************************
 * function name : parse_data()
 * Description   : parses received data into name address and data
 ************************************************************************************************************************************/
int parse_data(void)
 {
//    send_data("inside parse data \n");
    unsigned int i=0;
    name_len=data_len=addr_len=0;
    no_of_spaces=0;
   while(received_data_buffer[i]!='\n')
    {
//       send_data("inside while data[i]!= 10 \n");
       if(received_data_buffer[i]==' ')
        {
           no_of_spaces++;
//           send_data("space \n");
           i++;
           continue;
        }

       if(no_of_spaces==0) //no space yet
        {
           if(name_len==8)
               return 1;
           checkname[i]=received_data_buffer[i];
           i++;
           name_len+=1;
//           send_data(" no_of_words= 0 \n");
//           send_dec(name_len);
//           send_dec(i);
           checkname[name_len]='\n';
//           send_data(checkname);

        }// end of if()
       else if(no_of_spaces==1)
         {
           if(addr_len==11)
               return 1;
           addr[i-name_len-1]=received_data_buffer[i];
           i++;
           addr_len+=1;
//           send_data(" no_of_words= 1 \n");
//           send_dec(addr_len);
//           send_dec(i);
           addr[addr_len]='\n';
//           send_data(addr);

//           send_data(no_of_words+0x30);
         }
       else if(no_of_spaces==2)
            {
           if(data_len==11)
            return 1;
          data[i-addr_len-name_len-2]=received_data_buffer[i];
          i++;
             data_len+=1;
 //            send_data(" no_of_words= 2 \n");
 //            send_dec(data_len);
 //          send_dec(i);
             data[data_len]='\n';
//             send_data(data);

       //           send_data(no_of_words+0x30);
        }

 //    send_data(no_of_words);
    }//while(received_data!='\n')

    if(no_of_spaces==0)
     {
        name_len-=1;
        checkname[name_len]='\n';
     }
    else if(no_of_spaces==1)
     {
        addr_len-=1;
        addr[addr_len]='\n';
     }


    else if(no_of_spaces==2)
     {
           data_len-=1;
        data[data_len]='\n';
     }

    else  // if no of words >3
 //       send_data("no of spaces >2\n");
      return 1;

 return 0;
 }//end of parse_data()
/******************************************************************************************************************************/

/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/

void send_dec(unsigned int num)
 {
    USART_SendData(USART2,10);        // SEND LINE FEED
    while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == 0);//WAIT UNTIL EACH CHARACTER IS SEND
    USART_SendData(USART2,13);        // SEND CARRIAGE RETURN
    while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == 0);//WAIT UNTIL EACH CHARACTER IS SEND

    unsigned int temp=1,num1=num;
    while(num>=10)
      {
        temp*=10;
        num/=10;
      }
    while(temp>0)
      {
        USART_SendData(USART2,(num1/temp)+48);
        while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == 0);//WAIT UNTILCHARACTER IS SEND
        num1%=temp;
        temp/=10;
      }
 }

/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/

void send_hex(unsigned int num)
 {
    unsigned int temp=1,num1=num;
    while(num>=16)
      {
        temp*=16;
        num/=16;
      }
    while(temp>0)
      {
        USART_SendData(USART2,hex_ascii(num1/temp));
        while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == 0);//WAIT UNTILCHARACTER IS SEND
        num1%=temp;
        temp/=16;
      }
 }

/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/

void initcommands(void)
 {
    data_count = 0;
    name_len=data_len=0;

    int i;
      for(i=0;i<10;i++)
      {
          commands[i].name=function_name_list[i];
          commands[i].helptext=function_helptext_list[i];
      }//end of for

      commands[0].function = &command_help;
      commands[1].function = &command_ledon;
      commands[2].function = &command_ledoff;
      commands[3].function = &command_wl;
      commands[4].function = &command_ww;
      commands[5].function = &command_wb;
      commands[6].function = &command_rl;
      commands[7].function = &command_rw;
      commands[8].function = &command_rb;
 }

/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/

void command_help(void)
   {
    int i=0;
    for(;i<10;i++)
        send_data(commands[i].helptext);
   }

/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/

void command_ledon(void)
 {
    GPIO_SetBits(LD_GPIO_PORT,LD_GREEN_GPIO_PIN);    // TURN GREEN LED ON WHEN FIRST DATA RECEIVED
    GPIO_SetBits(LD_GPIO_PORT,LD_BLUE_GPIO_PIN);    // TURN GREEN LED ON WHEN FIRST DATA RECEIVED
 }

/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/

void command_ledoff(void)
 {
    GPIO_ResetBits(LD_GPIO_PORT,LD_GREEN_GPIO_PIN);    // TURN GREEN LED ON WHEN FIRST DATA RECEIVED
    GPIO_ResetBits(LD_GPIO_PORT,LD_BLUE_GPIO_PIN);    // TURN GREEN LED ON WHEN FIRST DATA RECEIVED
 }

/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/

void receive(void)
  {
    data_count=0; // MOVE POINTER TO THE BEGINNING OF BUFFER
    send_data(message);
    send_data(message1);
    received_data=' ';
    while(received_data != 13 && received_data != 10)    //CHECK FOR CARRIAGE RETURN OR LINEFEED
     {
        while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != SET);//WAIT TILL DATA IS RECEIVED
        GPIO_SetBits(LD_GPIO_PORT,LD_BLUE_GPIO_PIN); // TURN BLUE LED OFF WHEN FIRST DATA RECEIVED
        received_data = USART_ReceiveData(USART2);        //READ DATA FROM USART

        if(received_data == 8)            // IF BACKSPACE RECEIVED
             {
               if(data_count > 0)
                   data_count--;
              }//end of if(received_data == 8)

        else // IF ANYTHING OTHER THAN LINEFEED, CARRIAGE RETURN, AND BACKSPACE RECEIVED
           {
            received_data_buffer[data_count] = received_data;        //STORE RECEIVED DATA TO BUFFER
            if(data_count < 35)                 // CHECK IF NO OF RECEIVED DATA < BUFFER SIZE
               data_count++;
            else
               GPIO_SetBits(LD_GPIO_PORT,LD_BLUE_GPIO_PIN);    // TURN BLUE LED ON WHEN BUFFER FULL
           } // end of else

     }//end of while()

    received_data_buffer[data_count++] = '\n';
    send_data("you typed : \n");
    send_data(received_data_buffer);
//    send_data("a \n");        // SEND LINE FEED

    //while(1);
  } //end of function receive()
/*************************************************************************************************************/

/*****************************************************************************************************************************************
 *
 * ***************************************************************************************************************************************/
void send_data(char txdata[])
 {
    USART_SendData(USART2,10);        // SEND LINE FEED
    while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == 0);//WAIT UNTIL EACH CHARACTER IS SEND
    USART_SendData(USART2,13);        // SEND CARRIAGE RETURN
    while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == 0);//WAIT UNTIL EACH CHARACTER IS SEND

    int j=0;
    while(txdata[j]!='\n')
     {
       USART_SendData(USART2, txdata[j]);
       while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == 0);//WAIT UNTIL EACH CHARACTER IS SEND
       j++;
     }
    }//end of send_data()
/**********************************************************************************************************/

void Init(void)
 {

   PWR_VoltageScalingConfig(PWR_VoltageScaling_Range2);    // CONFIGURE INTERNAL VOLTAGE REGULATOR to 1.5V
   while (PWR_GetFlagStatus(PWR_FLAG_VOS) != RESET);    // WAIT TILL THE VOLTAGE IS CORRECTLY REGULATED, IT MAY TAKE SOMETIME FOR STABILIZATION

   RCC_Configuration();    // CLOCK CONFIGURATION
   GPIO_InitTypeDef GPIO_InitStructure;// GPIO Init STRUCTURE DECLARATION

          // LD3 & LD4 ARE CONFIGURED IN OUTPUT MODE
   GPIO_InitStructure.GPIO_Pin   = LD_GREEN_GPIO_PIN | LD_BLUE_GPIO_PIN;
   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
   GPIO_Init(LD_GPIO_PORT, &GPIO_InitStructure);

   GPIO_ResetBits(LD_GPIO_PORT,LD_GREEN_GPIO_PIN);
   GPIO_ResetBits(LD_GPIO_PORT,LD_BLUE_GPIO_PIN);
   Delay_us(1000000);
    GPIO_SetBits(LD_GPIO_PORT,LD_GREEN_GPIO_PIN);    // TURN GREEN LED ON WHEN FIRST DATA RECEIVED
    GPIO_SetBits(LD_GPIO_PORT,LD_BLUE_GPIO_PIN);    // TURN GREEN LED ON WHEN FIRST DATA RECEIVED
       Delay_us(1000000);
    GPIO_ResetBits(LD_GPIO_PORT,LD_GREEN_GPIO_PIN);    // TURN GREEN LED ON WHEN FIRST DATA RECEIVED
    GPIO_ResetBits(LD_GPIO_PORT,LD_BLUE_GPIO_PIN);    // TURN GREEN LED ON WHEN FIRST DATA RECEIVED
       Delay_us(1000000);
        GPIO_SetBits(LD_GPIO_PORT,LD_GREEN_GPIO_PIN);    // TURN GREEN LED ON WHEN FIRST DATA RECEIVED
        GPIO_SetBits(LD_GPIO_PORT,LD_BLUE_GPIO_PIN);    // TURN GREEN LED ON WHEN FIRST DATA RECEIVED

      /*    // CONFIGURE PB11 AS USART3 RX
   GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_11;
   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
   GPIO_Init(GPIOB, &GPIO_InitStructure);
   GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);

         // CONFIGURE PB10 AS USART3 TX
   GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_10;
   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
   GPIO_Init(GPIOB, &GPIO_InitStructure);
   GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);

   USART_InitTypeDef  USART_InitStruct;        //USART Init STRUCTURE DECLARATION

 //Initialise structure
   USART_InitStruct.USART_BaudRate = 9600;
   USART_InitStruct.USART_WordLength = USART_WordLength_8b;
   USART_InitStruct.USART_StopBits = USART_StopBits_1;
   USART_InitStruct.USART_Parity = USART_Parity_No ;
   USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
   USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

   USART_Init(USART2, &USART_InitStruct);        //INITIALISATION USART3
   USART_Cmd(USART3, ENABLE);            //ENABLES USART3*/

   USART_InitTypeDef  USART_InitStruct;        //USART Init STRUCTURE DECLARATION

// BLINK AND GLOW BLUE LED TO SAY PROGRAM IS LOADED AND WORKING
   GPIO_SetBits(LD_GPIO_PORT,LD_BLUE_GPIO_PIN);
   Delay_us(1000000);
   GPIO_ResetBits(LD_GPIO_PORT,LD_BLUE_GPIO_PIN);
   Delay_us(1000000);
   GPIO_SetBits(LD_GPIO_PORT,LD_BLUE_GPIO_PIN);


   // CONFIGURE PA02 AS USART2 RX
      GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_3;
      GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
      GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
      GPIO_Init(GPIOA, &GPIO_InitStructure);
      GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_USART2);

            // CONFIGURE PA03 AS USART2 TX
      GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_2;
      GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
      GPIO_Init(GPIOA, &GPIO_InitStructure);
      GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART2);

    //  USART_InitTypeDef  USART_InitStruct;        //USART Init STRUCTURE DECLARATION

    //Initialise structure
      USART_InitStruct.USART_BaudRate = 9600;
      USART_InitStruct.USART_WordLength = USART_WordLength_8b;
      USART_InitStruct.USART_StopBits = USART_StopBits_1;
      USART_InitStruct.USART_Parity = USART_Parity_No ;
      USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
      USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

      USART_Init(USART2, &USART_InitStruct);        //INITIALISATION USART2
      USART_Cmd(USART2, ENABLE);            //ENABLES USART2


 }//end of Init()

/**********************************************************************************************************/


/**********************************************************************************************************/


void RCC_Configuration(void)
 {

  RCC_HCLKConfig(RCC_SYSCLK_Div1);    // AHB Clock (HCLK) at 2.097 MHz
  RCC_PCLK1Config(RCC_HCLK_Div1);        // Low speed APB1 clock (PCLK1) clock at 2.097 MHz

 // RCC_APBeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);                // ENABLE GPIO CLOCK
  //RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);    // ENABLE USART3 CLOCK
  //RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // ENABLE USART2 CLOCK
 // RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

 }// end of funciton RCC_Configuration()


/**********************************************************************************************************/
void Delay_us(uint32_t time_ns)
{
   while(time_ns!=0)
    time_ns--;
}

/**********************************************************************************************************/

/**
  * @brief  Decrements the TimingDelay variable.
  * @caller SysTick interrupt Handler
  * @param  None
  * @retval None
  */
void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
   TimingDelay--;

}

/**
  * @brief  Inserts a delay time.
  * @param  nTime: specifies the delay time length, in 10 ms.
  * @retval None
  */
void Delay(uint32_t nTime)
{
   TimingDelay = nTime;
   while(TimingDelay != 0);
}



#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{

(void)file;
(void)line;
while (1);

}

#endif

Attachments

Outcomes