cancel
Showing results for 
Search instead for 
Did you mean: 

USART bytes not received after a long transmission

shahmonil93
Associate II
Posted on August 12, 2014 at 16:00

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?


#include ''stm32f0xx.h''

#include ''string.h''

char
value, lati_value[10], lati_dir, longi_value[10], longi_dir, 
time
[6],

speed[10], date[6];

int
i = 0;

void
periphinit() {

RCC->AHBENR |= (1 << 17);

RCC->AHBENR |= (1 << 18);

RCC->AHBENR |= (1 << 19);

RCC->APB2ENR |= (1 << 14);

RCC->APB1ENR |= (1 << 17) | (1 << 18) | (1 << 19);

GPIOA->MODER = 0xA5285555;

GPIOB->MODER = 0x55555555;

GPIOC->MODER = 0x55A55A55;

GPIOA->AFR[1] = 0x11000110;

GPIOC->AFR[1] = 0x00000000;

GPIOC->AFR[0] = 0x00110000;

GPIOC->PUPDR = 0x55555555;

GPIOC->OSPEEDR = 0xFFFFFFFF;

GPIOC->OTYPER = 0x0000;


USART1->BRR = 0x0341;

USART1->CR1 |= (1 << 0);

USART1->CR1 |= (1 << 2);

USART1->CR1 |= (1 << 3);


USART2->BRR = 0x0341;

USART2->CR1 |= (1 << 0);

USART2->CR1 |= (1 << 2);

USART2->CR1 |= (1 << 3);


USART4->BRR = 0x0341;

USART4->CR1 |= (1 << 0);

USART4->CR1 |= (1 << 2);

USART4->CR1 |= (1 << 3);


USART3->BRR = 0x0341;

USART3->CR1 |= (1 << 0);

USART3->CR1 |= (1 << 2);

USART3->CR1 |= (1 << 3);

}


char
usartgpsrec() {

while
((USART1->ISR & USART_ISR_RXNE) == RESET)

;

return
USART1->RDR;

}

void
usartlcdtrans(
char
uldata) {

while
((USART1->ISR & USART_ISR_TXE) == RESET)

;

USART1->TDR = uldata;

}

char
usartgsmrec() {


char
tremp = 
'\0'
;

if
((USART4->ISR & USART_ISR_RXNE) != RESET)

tremp = USART4->RDR;

if
(USART4->ISR & USART_ISR_ORE != RESET) {

(
void
) USART4->RDR;

}

return
tremp;

}

void
usartgsmtrans(
char
udata) {

while
((USART4->ISR & USART_ISR_TXE) == RESET)

;

USART4->TDR = udata;


}


void
usartgsmtrans_string(
char
s[200]) {

int
p;

for
(p = 0; s[p] != 
'\0'
; p++) {

usartgsmtrans(s[p]);


}

}


void
usartlcd_string(
char
s1[200]) {

int
p;

for
(p = 0; s1[p] != 
'\0'
; p++) {

usartlcdtrans(s1[p]);

}

}



bool
GSM_checkOK(uint8_t a, uint8_t b) {

uint8_t k = 0;

uint8_t j = 0;

do
{

char
x = usartgsmrec();

if
(x > 0x20 && x < 0x7F)

usartlcdtrans(x);

if
(x == 
'O'
| x == 
'K'
) {

k = k + 1;

} 
else
if
(x == 
'E'
| x == 
'R'
) {

j = j + 1;

}

} 
while
(k < a & j < b);

if
(k == a) {

return
true
;

} 
else
if
(j == b) {

return
false
;

} 
else
{

return
false
;

}

}

bool
GSM_checkP() {

int
num = 0;

do
{

char
x = usartgsmrec();

usartlcdtrans(x);

if
(x == 
'>'
) {

num = 1;

} 
else
if
(x == 
'E'
| x == 
'R'
) {

num = 2;

}

} 
while
(num == 0);

if
(num == 1) {

return
true
;

} 
else
if
(num == 2) {

return
false
;

} 
else
{

return
false
;

}

}



void
GSM_init() {

do
{

usartgsmtrans_string(
''AT\r''
);

} 
while
(!GSM_checkOK(2,2));

usartlcd_string(
''AT OK''
);

do
{

usartgsmtrans_string(
''AT+SAPBR=3,1,\''Contype\'',\''GPRS\''\r''
);

} 
while
(!GSM_checkOK(2, 2));

usartlcd_string(
''contype set''
);

do
{

usartgsmtrans_string(
''AT+SAPBR=3,1,\''APN\'',\''uninor\''\r''
);

} 
while
(!GSM_checkOK(2, 2));

usartlcd_string(
''apn set''
);

do
{

usartgsmtrans_string(
''at+sapbr=1,1\r''
);

} 
while
(!GSM_checkOK(2, 2));

do
{

usartgsmtrans_string(
''at+sapbr=2,1\r''
);

} 
while
(!GSM_checkOK(2, 2));

usartlcd_string(
''GPRS Initialized!''
);

}


int
main() {

periphinit();

do
{

usartgsmtrans(
'A'
);

usartgsmtrans(
'T'
);

usartgsmtrans(
'\r'
);

} 
while
(!GSM_checkOK(2, 2));



GSM_init();



}

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.
}

#discovery #usart #uart #stm32 #!complicated
2 REPLIES 2
Trev
Associate II
Posted on August 12, 2014 at 16:26

You are using | in your if staetements when you should be using ||

shahmonil93
Associate II
Posted on August 13, 2014 at 06:37

Thank you for your suggestion Trev!

I changed the 'if' condition. But, it still doesn't work. Something seems to be wrong in the receive or transmit part. Can someone help me with that?

Thanks in advance!