AnsweredAssumed Answered

USART and wifi

Question asked by serao.pietro on Jan 30, 2013
Latest reply on Mar 5, 2013 by Clive One
good morning, i need to develop an application that sends and receives some srtings via usart, because i need to communicate with a wifi device (i'm using a stm32f0discovery board).
As my first step i want to recognize the mac address, well, the protocol that allow to obtain the mac is the seguent:

cotrex sends "$$$"
wifi returns "CMD"      (now we are in command mode)

cotex sends "get mac<cr>"
wifi returns "Mac Addr=00:06:66:72:55:3f
<2.32>"

cortex sends "exit<cr>"
wifi sends "EXIT"

At the moment i have build this code but i have some problem



/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx.h"
#include "stm32f0_discovery.h"
#include "stdio.h"
 #include <string.h>
#include <main.h>
/** @addtogroup STM32F10x_StdPeriph_Examples
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define TxBufferSize   (countof(TxBuffer))
/* Private macro -------------------------------------------------------------*/
#define countof(a)   (sizeof(a) / sizeof(*(a)))
/* Private function prototypes -----------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
 NVIC_InitTypeDef NVIC_InitStructure;
__IO uint8_t TxCounter = 0;  
short RxChar=0;
char RxBuffer[];
char TxBuffer[];
USART_InitTypeDef USART_InitStructure;
extern uint8_t NbrOfDataToTransfer;
extern __IO uint8_t TxCount; 
extern __IO uint16_t RxCount; 
 short lettura=0;
/* Private function prototypes -----------------------------------------------*/
 /* Private functions ---------------------------------------------------------*/
void USART_Configuration(void);
//void USART2_Configuration(void);


/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
 
USART_Configuration();
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  
printf("SETUP WIFI\n");


// enter command mode
 strcpy(TxBuffer,"$$$");

 while((TxCounter <(strlen(TxBuffer))))
 {     
    /* Send one byte from USART1 */
    USART_SendData(USART1, TxBuffer[TxCounter++]); 
    /* Loop until USART1 DR register is empty */ 
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
   {
 }
 }
 while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {}   

  if(TxCounter==(strlen(TxBuffer)))
    { 
     TxCounter=0; 
    
      }
for (int i=0; i<(strlen(TxBuffer));i++)
  {
    TxBuffer[i]=0;
  }


  // sending get mac
 strcpy(TxBuffer,"get mac");
 TxBuffer[strlen(TxBuffer)-1]=13;
 
 while((TxCounter <(strlen(TxBuffer))))
 {     
    /* Send one byte from USART1 */
    USART_SendData(USART1, TxBuffer[TxCounter++]); 
    /* Loop until USART1 DR register is empty */ 
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
   {
 }
  
 }
 while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {} 
 if(TxCounter==(strlen(TxBuffer)))
    { 
     TxCounter=0; 
    
      }


for (int i=0; i<(strlen(TxBuffer));i++)
  {
    TxBuffer[i]=0;
  }
 


// exit
 strcpy(TxBuffer,"exit");
 TxBuffer[strlen(TxBuffer)]=13;
 
 while((TxCounter <(strlen(TxBuffer))-1))
 {     
    /* Send one byte from USART1*/
    USART_SendData(USART1, TxBuffer[TxCounter++]); 
    /* Loop until USARTy DR register is empty */ 
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
   {
 }
   
 }
  if(TxCounter==(strlen(TxBuffer)))
    { 
     TxCounter=0; 
    
      }


for (int i=0; i<(strlen(TxBuffer));i++)
  {
    TxBuffer[i]=0;
  }

}




/**
  * @brief Configura l'USART 
  * @param  None
  * @retval None
  */
 void USART_Configuration(void)

  /* Abilita clock GPIOA e DMA  */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);
  
  /* Abilita clock USART1 APB  */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  
  /* configurazione pin USART1   **************************************************/
   
  /* Connette i pin PA9 e PA10 alla periferica USART */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);    
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1); 
  
  /* Configura i pin come AF pushpull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_2;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure); 


   
  USART_DeInit(USART1);
  USART_InitStructure.USART_BaudRate = 9600;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(USART1, &USART_InitStructure);
 
 
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;                // we want to configure the USART2 interrupts
        NVIC_InitStructure.NVIC_IRQChannelPriority = 0;// this sets the priority group of the USART2 interrupts
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                  // the USART2 interrupts are globally enabled
        NVIC_Init(&NVIC_InitStructure);          
  
  USART_Cmd(USART1, ENABLE); 
}






#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)

  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */


  /* Infinite loop */
  while (1)
  {
  }
}


#endif
**************************************************************************************************************************************************
END OF MAIN CODE


in this case i want only send the input commands, if i activate a breakpoint on the instruction " strcpy(TxBuffer,"get mac");" the sending phase works.

and if i activate the reception inetrrupt routine nothing happens.

this is the reception routine: 

void USART1_IRQHandler(void) 



 if((USART_GetITStatus(USART1, USART_IT_RXNE) != RESET))
  {
    /* Read one byte from the receive data register */
  RxBuffer[RxCount++] = (USART_ReceiveData(USART1) );
       
    if((RxBuffer[RxCount-1]=='>'))
  
    {
    
      USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); 
      for(int i=RxCount; i<strlen(RxBuffer);i++){
      RxBuffer[i]=0;
      }
     printf("%s \n", RxBuffer);
    
     RxCount = 0;
  
 
    }
  
  }
}

Thanks.

Outcomes