Life.augmented
Skip to main content
Go Search
  

STM32

Modify settings and columns
STM32 - ARM Cortex-M 32-Bit MCU
Share Discussion
  
View: 
Post
Started: 5/3/2012 3:14 AM
marwen.azouzi
  Posts : 10
stm32VL UART problem
Hi
i have to communicate with modem using uart via AT command
my pb is when i send the first command and receive the reponse i can't send the next AT using uart
i can post my code if any one can help me 

Tags: adc stm32 discovery , uart
Posted: 5/3/2012 3:28 AM
marwen.azouzi
  Posts : 10


Posted: 5/3/2012 4:21 AM
clive1
  Posts : 8788
#include "stm32f10x.h"
#include "STM32vldiscovery.h"
 
#include "stm32f10x_gpio.h"
 
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
   
 
void SendString(char *s)
{
  while(*s)
  {
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
 
    USART_SendData(USART1, *s++);
  }
}
 
int main(void)
{
  USART_InitTypeDef USART_InitStructure;
 
  /* System Clocks Configuration */
  RCC_Configuration();
        
  /* NVIC configuration */
  //NVIC_Configuration();
 
  /* Configure the GPIO ports */
  GPIO_Configuration();
   
/* USARTx configuration ------------------------------------------------------*/
  /* USARTx configured as follow:
        - BaudRate = 9600 baud 
        - Word Length = 8 Bits
        - Two Stop Bit
        - Odd parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  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);
 
  /* You might want some delays between these,
     I'm just illustrating how to transmit to the USART
     with a subroutine */
 
  SendString("AT?\n");
 
  SendString("ATB\n");
 
  while(1)
  {
  }
}
 
 
void RCC_Configuration(void)
{
  /* Enable GPIO clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  /* Enable USART2 Clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
 
  /* Setup the microcontroller system. Initialize the Embedded Flash Interface, 
     initialize the PLL and update the SystemFrequency variable. */
}
 
 
 
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
 
 
  /* Configure USART1 Rx as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA,&GPIO_InitStructure);
   
  /* Configure USART1 Tx as alternate function push-pull   */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA,&GPIO_InitStructure);
}
 
#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


Posted: 5/4/2012 2:37 AM
marwen.azouzi
  Posts : 10
thx using fct to send is an easy way 
in your opinion  for the reception I had to use  interrupt or just polling mode 
i must compare the Inbuffer to send other AT command
Posted: 5/4/2012 4:24 PM
clive1
  Posts : 8788
in your opinion  for the reception I had to use  interrupt or just polling mode 
i must compare the Inbuffer to send other AT command.

There are a number of ways of doing it, I personally use interrupts, and FIFO (ring) buffers on both the receive and transmit side.
Posted: 5/4/2012 6:22 PM
marwen.azouzi
  Posts : 10
One little thing in case of using interrupt after receiving data from modem i must compare the reponse with the true one
so i have to clear Rxbuffer after each reception ? 
Posted: 5/4/2012 6:58 PM
clive1
  Posts : 8788
One little thing in case of using interrupt after receiving data from modem i must compare the reponse with the true one so i have to clear Rxbuffer after each reception ?

In a robust solution, you'll need to parse the responses, different modems respond differently, and you might get unexpected responses. Typically you'd be looking for "OK", "ERROR", "NO CARRIER", etc.
Posted: 5/9/2012 12:57 AM
marwen.azouzi
  Posts : 10
Hi again
I use for the comparaison the function "buffercmp" which compaire 2 buffer as result "FAILED" or "PASSED" i want a way which after the comparaison to clear the Rxbuffer  
Posted: 5/9/2012 2:31 AM
clive1
  Posts : 8788
I use for the comparaison the function "buffercmp" which compaire 2 buffer as result "FAILED" or "PASSED" i want a way which after the comparaison to clear the Rxbuffer

While I think this is a rather sloppy approach, what you're suggesting would look like this :

// Receive enough to evaluate...
 
if (Buffercmp(RxBuffer, "ERROR", RxCounter) == PASSED)
  puts("Modem returned ERROR!");
 
RxCounter = 0; // Clear RxBuffer to beginning
 
Posted: 5/9/2012 4:18 AM
marwen.azouzi
  Posts : 10
which the best way so in your opinion
i have multiple reception for each reception i must compare and send the suitable command
Posted: 10/4/2012 1:51 PM
nazuhin
  Posts : 26
hi,i am recently working on a project that sending AT command to gsm module from stm32f100c8  via usart and receiving response with interrupt but i am very confused about usart interfaces.i cant receive response from gsm module.i am using stm32vl discovery and ublox leon g100 gsm module usart1 and usart3 and terminal.It sends me back same string.i send "AT" and i want to get "OK" response from module.please help where am i wrong?here is my code :

/*----------------------------------------------*/
/*----------------------------------------------*/
#include "stm32f10x.h"
#include "stdio.h"
#include "string.h"
/*----------------------------------------------*/
/*---------function prototypes------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
void ResetModule(void);
void USART_SendCommand(unsigned char *s);
/*------------------------------------------------*/
/*-------------global variables-------------------*/
unsigned char command[]="AT\0\r\n";
char response[50];
int res_index=0;
unsigned char r;

/*----------------------------------------------*/
/*----------RCC configurations------------------*/
void RCC_Configuration(void){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
}
/*----------------------------------------------*/
/*----------GPIO configurations------------------*/
void GPIO_Configuration(void){
GPIO_InitTypeDef GPIO_InitStructure;

/* Configuring USART1_Tx as 'alternate function push-pull' */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configuring USART1_Rx as 'input floating' */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configuring USART3_Tx as 'alternate function push-pull' */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);

/* Configuring USART3_Rx as 'input floating' */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);

/* reset the module*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure );
}
/*----------------------------------------------*/
/*----------USART configurations----------------*/
void USART_Configuration(void){
USART_InitTypeDef USART_InitStructure;

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_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;


USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);

USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);

USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

}
/*----------------------------------------------*/
/*----------NVIC configurations-----------------*/
void NVIC_Configuration(void){
NVIC_InitTypeDef NVIC_InitStructure;

NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*----------------------------------------------*/
/*----------RESET module-----------*/
void ResetModule(void){

long int n;
//reset 
GPIO_SetBits(GPIOB,GPIO_Pin_13);
for(n = 0;n < 10000000;n++); 
GPIO_ResetBits(GPIOB,GPIO_Pin_13);

for(n = 0;n < 30000000;n++);

//power
GPIO_SetBits(GPIOB,GPIO_Pin_12);
for(n = 0;n < 10000000;n++);
GPIO_ResetBits(GPIOB,GPIO_Pin_12);
}
/*----------------------------------------------*/
/*----------Main function-----------------------*/
int main(){

RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
NVIC_Configuration();
ResetModule();
USART_SendCommand(command);

while(1){
}
}
/*----------------------------------------------*/
/*----------Send command------------------------*/
void USART_SendCommand(unsigned char *s)
{
while(*s)
{

//while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
//USART_SendData(USART1, *s);

while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
USART_SendData(USART3, *s++);


}
}
/*----------------------------------------------*/
/*----------Receive character from USART3-------*/
void USART3_IRQHandler(void){ 

r=USART_ReceiveData(USART3);
response[res_index]=r;
res_index++;

}
/*----------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*
Posted: 10/4/2012 3:57 PM
clive1
  Posts : 8788
The Slash Zero '\0' seems out of place, why would you put a NUL there?

The modem will often echo, turning it off requires ATE0

Browse Private Forums

NOTE: these forums are exclusive for users of this Extranet!)