AnsweredAssumed Answered

USART bytes not received after a long transmission

Question asked by shah.monil on Aug 12, 2014
Latest reply on Aug 13, 2014 by shah.monil
Hey, I am trying to communicate with SIM900 GSM module using STM32F072RB chip on discovery board. I have written the code, which works fine for a few commands. I can transmit and receive small commands and I receive the response (OK / ERROR) too! But as soon as I transmit more than 2-3 commands or after transmitting longer commands, I stop receiving the response. I cant receive even a single byte after that. I am using USART polling method and I am even checking for the over run condition. What can be wrong in the code?
001.#include "stm32f0xx.h"
002.#include "string.h"
003.char value, lati_value[10], lati_dir, longi_value[10], longi_dir, time[6],
004.        speed[10], date[6];
005.int i = 0;
006.void periphinit() {
007.    RCC->AHBENR |= (1 << 17);
008.    RCC->AHBENR |= (1 << 18);
009.    RCC->AHBENR |= (1 << 19);
010.    RCC->APB2ENR |= (1 << 14);
011.    RCC->APB1ENR |= (1 << 17) | (1 << 18) | (1 << 19);
012.    GPIOA->MODER = 0xA5285555;
013.    GPIOB->MODER = 0x55555555;
014.    GPIOC->MODER = 0x55A55A55;
015.    GPIOA->AFR[1] = 0x11000110;
016.    GPIOC->AFR[1] = 0x00000000;
017.    GPIOC->AFR[0] = 0x00110000;
018.    GPIOC->PUPDR = 0x55555555;
019.    GPIOC->OSPEEDR = 0xFFFFFFFF;
020.    GPIOC->OTYPER = 0x0000;
021. 
022.    USART1->BRR = 0x0341;
023.    USART1->CR1 |= (1 << 0);
024.    USART1->CR1 |= (1 << 2);
025.    USART1->CR1 |= (1 << 3);
026. 
027.    USART2->BRR = 0x0341;
028.    USART2->CR1 |= (1 << 0);
029.    USART2->CR1 |= (1 << 2);
030.    USART2->CR1 |= (1 << 3);
031. 
032.    USART4->BRR = 0x0341;
033.    USART4->CR1 |= (1 << 0);
034.    USART4->CR1 |= (1 << 2);
035.    USART4->CR1 |= (1 << 3);
036. 
037.    USART3->BRR = 0x0341;
038.    USART3->CR1 |= (1 << 0);
039.    USART3->CR1 |= (1 << 2);
040.    USART3->CR1 |= (1 << 3);
041.}
042. 
043.char usartgpsrec() {
044.    while ((USART1->ISR & USART_ISR_RXNE) == RESET)
045.        ;
046.    return USART1->RDR;
047.}
048.void usartlcdtrans(char uldata) {
049.    while ((USART1->ISR & USART_ISR_TXE) == RESET)
050.        ;
051.    USART1->TDR = uldata;
052.}
053.char usartgsmrec() {
054. 
055.    char tremp = '\0';
056.    if ((USART4->ISR & USART_ISR_RXNE) != RESET)
057.        tremp = USART4->RDR;
058.    if (USART4->ISR & USART_ISR_ORE != RESET) {
059.        (void) USART4->RDR;
060.    }
061.    return tremp;
062.}
063.void usartgsmtrans(char udata) {
064.    while ((USART4->ISR & USART_ISR_TXE) == RESET)
065.        ;
066.    USART4->TDR = udata;
067. 
068.}
069. 
070.void usartgsmtrans_string(char s[200]) {
071.    int p;
072.    for (p = 0; s[p] != '\0'; p++) {
073.        usartgsmtrans(s[p]);
074. 
075.    }
076.}
077. 
078.void usartlcd_string(char s1[200]) {
079.    int p;
080.    for (p = 0; s1[p] != '\0'; p++) {
081.        usartlcdtrans(s1[p]);
082.    }
083.}
084. 
085. 
086.bool GSM_checkOK(uint8_t a, uint8_t b) {
087.    uint8_t k = 0;
088.    uint8_t j = 0;
089.    do {
090.        char x = usartgsmrec();
091.        if (x > 0x20 && x < 0x7F)
092.            usartlcdtrans(x);
093.        if (x == 'O' | x == 'K') {
094.            k = k + 1;
095.        } else if (x == 'E' | x == 'R') {
096.            j = j + 1;
097.        }
098.    } while (k < a & j < b);
099.    if (k == a) {
100.        return true;
101.    } else if (j == b) {
102.        return false;
103.    } else {
104.        return false;
105.    }
106.}
107.bool GSM_checkP() {
108.    int num = 0;
109.    do {
110.        char x = usartgsmrec();
111.        usartlcdtrans(x);
112.        if (x == '>') {
113.            num = 1;
114.        } else if (x == 'E' | x == 'R') {
115.            num = 2;
116.        }
117.    } while (num == 0);
118.    if (num == 1) {
119.        return true;
120.    } else if (num == 2) {
121.        return false;
122.    } else {
123.        return false;
124.    }
125.}
126. 
127. 
128.void GSM_init() {
129.    do {
130.        usartgsmtrans_string("AT\r");
131.    } while (!GSM_checkOK(2,2));
132.    usartlcd_string("AT OK");
133.    do {
134.        usartgsmtrans_string("AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r");
135.    } while (!GSM_checkOK(2, 2));
136.    usartlcd_string("contype set");
137.    do {
138.        usartgsmtrans_string("AT+SAPBR=3,1,\"APN\",\"uninor\"\r");
139.    } while (!GSM_checkOK(2, 2));
140.    usartlcd_string("apn set");
141.    do {
142.        usartgsmtrans_string("at+sapbr=1,1\r");
143.    } while (!GSM_checkOK(2, 2));
144.    do {
145.        usartgsmtrans_string("at+sapbr=2,1\r");
146.    } while (!GSM_checkOK(2, 2));
147.    usartlcd_string("GPRS Initialized!");
148.}
149. 
150.int main() {
151.    periphinit();
152.    do {
153.                    usartgsmtrans('A');
154.                    usartgsmtrans('T');
155.                    usartgsmtrans('\r');
156.                } while (!GSM_checkOK(2, 2));
157. 
158. 
159.    GSM_init();
160. 
161.     
162.}
I also tried debugging with this piece of code in the main(), to see how many times can I receive OK from the GSM module. And i found that, I could go on till 85 times without any error! I use a 16x2 LCD display, to view the response and debug the code, step by step. Its connected to another microcontroller, which just throws the received value on the LCD display.
1.for ( int f = 0;  f < 85;  f++) {
2.        do {
3.                usartgsmtrans('A');
4.                usartgsmtrans('T');
5.                usartgsmtrans('\r');
6.            } while (!GSM_checkOK(2, 2));
7.        usartlcdtrans(32+f);
8.    }

Outcomes