Skip to main content
rasimoguzturk
Associate III
October 4, 2012
Question

stm32f100c8 and ublox leon g100 USART communication

  • October 4, 2012
  • 15 replies
  • 2547 views
Posted on October 04, 2012 at 13:20

hi all,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++;

}

/*----------------------------------------------*/

/*--------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------*/

    This topic has been closed for replies.

    15 replies

    frankmeyer9
    Associate III
    October 4, 2012
    Posted on October 04, 2012 at 14:08

    I guess this is the common HSE_VALUE problem.

    Open your stm32f10x.h header file, and search for the definition of this value (HSE_VALUE), which defines the frequency of the ext. quartz oscillator.

    It will probably be 25.000.000, which is not correct for the stm32vl_discovery - it has a 8MHz quartz (8.000.000).

    Change it either directly, or (better), add the proper definitions to your build options.

    frankmeyer9
    Associate III
    October 4, 2012
    Posted on October 04, 2012 at 14:08

    double post, forum software crashed yet again ...

    Andrew Neil
    Super User
    October 4, 2012
    Posted on October 04, 2012 at 14:37

    ''

    It sends me back same string. i send 'AT'''

    That's called Echo - you control it with the ATE command.

    Before writing code on a microcontroller, you should explore these things ''manually'' with a terminal program...

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    rasimoguzturk
    Associate III
    October 4, 2012
    Posted on October 04, 2012 at 14:53

    @ fm -> my external board has 16000000 clock i have already changed HSE value it did not work:(..

    @neil when i send ''ATE'' command it sends me back only ''AT'' as a response.my focus is first send AT and recieve OK as response
    Tesla DeLorean
    Guru
    October 4, 2012
    Posted on October 04, 2012 at 17:10

    unsigned char command[]=''AT\0\r\n'';

    The NUL here is problematic.
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    rasimoguzturk
    Associate III
    October 4, 2012
    Posted on October 04, 2012 at 19:09

    i tried both scenario,''AT\r\r'',''AT'',''AT\0''etc..also i tried ''ATE0''..when i send ''ATE0'' it sends me back only ''AT'',but for other any strings it returns same string..ex:

    send(''AT\r\n'') 

    response:''AT''

    send(''ATE0\r\n'')

    response: ''AT''

    send(''helloworld\r\n'')

    response: helloworld..:(

    Tesla DeLorean
    Guru
    October 4, 2012
    Posted on October 04, 2012 at 19:26

    Sounds a lot like you've got it wired up wrong. Either that, or how you're making the determination about the data being received.

    The code doesn't look dysfunctional, suggest you work on your debugging/troubleshooting skills.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    rasimoguzturk
    Associate III
    October 4, 2012
    Posted on October 04, 2012 at 19:45

    i think so..i cannot test the module whether working properly or not and i spent too much time with the code searching any missed thing.

    Andrew Neil
    Super User
    October 4, 2012
    Posted on October 05, 2012 at 00:03

    ''i tried both scenario,''AT\r\r'',''AT'',''AT\0''etc..''

     

    But, again, you need to ensure that you can do this manually from a terminal

    before

      you start messing with microcontroller code!
    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    rasimoguzturk
    Associate III
    October 5, 2012
    Posted on October 05, 2012 at 13:24

    i think you mean that..not to send command in the code,send it from terminal.here is my code but the result is the same:(..

    /*----------------------------------------------*/

    /*----------------------------------------------*/

    #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 USART3_IRQHandler(void);

    void USART1_IRQHandler(void);

    void ResetModule(void);

    /*-----------------------------------------------*/

    /*-------------global variables------------------*/

    unsigned char r;

    unsigned char c;

    /*----------------------------------------------*/

    /*----------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_2MHz;

    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); //usart3 receive interrupt

    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);// usart1 receive interrupt

    }

    /*----------------------------------------------*/

    /*----------NVIC configurations-----------------*/

    void NVIC_Configuration(void){

    NVIC_InitTypeDef NVIC_InitStructure;

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //usart3 priority=0

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //usart1 priority=1

    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();

    NVIC_Configuration();

    ResetModule();

    USART_Configuration();

      

    while(1){

    }

    }

    //usart1 receive character from terminal

    void USART1_IRQHandler(void){

    while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

    c=USART_ReceiveData(USART1);

    //send character to usart3 TX

    while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);

    USART_SendData(USART3, c);

    }

    /*----------------------------------------------*/

    /*----------Receive character from USART3-------*/

    void USART3_IRQHandler(void){ 

    r=USART_ReceiveData(USART3); //receive character from usart3 RX

     

    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //send to usart1 for display in terminal

    USART_SendData(USART1, r);

    }

    /*----------------------------------------------*/

    /*-----------------------------------------------------*/

    /*-------------------------------------------------------------*/

    /*------------------------------------------------------------------------*/

    /*--------------------------------------------------------------------------------------------*/