2019-08-06 08:40 AM
I'm trying to send message to sim900A via the serial port. The AT commands work but the GSM replies with some unwanted text (Previously sent text ) then finally OK. The GSM is supposed to reply what I typed with some prompts and finally OK. The picture below clearly tells what's happening. Is there any way to delete the data on the GSM buffer (If there is any) before sending a new one?
CODE:
#include "stm32f10x.h" // Device header
void clockConfig(void);
void UART1Init(void);
void USART2Init(void);
void GSMSend(void);
uint8_t recv[64];
uint64_t pos;
int main(void)
{
clockConfig();
UART1Init();
USART2Init();
while(1)
{
if (USART1->SR & USART_SR_RXNE)
{
uint8_t c;
do {
while((USART1->SR & USART_SR_RXNE) == 0);
c = USART1->DR;
if ( c == '*' )
recv[pos++] = 0x1A; // CTRL + Z
else
recv[pos++] = c;
// Echo back what you typed and save it in a buffer for later use
while((USART1->SR & USART_SR_TXE) == 0);
USART1->DR = c;
if ( c=='\r' )
{
recv[pos++] = '\n';
while(!(USART1->SR & USART_SR_TXE));
USART1->DR = '\n';
// Reset position for the next time
pos = 0;
}
} while(c != '\r');
// Check is message has been completely stored in the recv buffer
if ( c == '\r' ) GSMSend();
}
}
}
void GSMSend(void)
{
uint16_t buf = 0;
while(recv[buf] != '\0')
{
while(!(USART2->SR & USART_SR_TXE));
USART2->DR = recv[buf];
buf++;
}
}
void USART2_IRQHandler(void)
{
if ( USART2->SR & USART_SR_RXNE )
{
//uint8_t c = USART2->DR;
while(!(USART1->SR & USART_SR_TXE));
USART1->DR = USART2->DR;
}
}
void USART2Init(void)
{
// USART2_Tx: PA2
// USART2_Rx: PA3
// Enable clock access for port A
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
// Configure PA2 as AF push pull
GPIOA->CRL |= GPIO_CRL_CNF2_1 | GPIO_CRL_MODE2;
GPIOA->CRL &= ~GPIO_CRL_CNF2_0;
// Configure PA3 as floating input
// Enable clock for USART2
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
// 36MHz, 9600bps
USART2->BRR = 0xEA6;
// Enable Tx and Rx
USART2->CR1 |= USART_CR1_TE | USART_CR1_RE;
// Enable USART2_Rx interrupt
USART2->CR1 |= USART_CR1_RXNEIE;
NVIC_EnableIRQ(USART2_IRQn);
// Enable USART2
USART2->CR1 |= USART_CR1_UE;
}
void UART1Init(void)
{
// UART1_Tx: PA9
// UART1_Rx: PA10
// Enable clock access for port A
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
// COnfigure PA9 as AF push pull
GPIOA->CRH |= GPIO_CRH_CNF9_1 | GPIO_CRH_MODE9;
GPIOA->CRH &= ~GPIO_CRH_CNF9_0;
// COnfigure PA10 as floating input ( Reset state)
// Enable clock access for USART1
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
// 72MHZ, 9600bps
USART1->BRR = 0x1D4C;
// Enable transmitter and receiver
USART1->CR1 |= USART_CR1_TE | USART_CR1_RE;
// Enable USART1
USART1->CR1 |= USART_CR1_UE;
}
void clockConfig(void)
{
// HSE ON
RCC->CR |= 0x1<<16;
// wait unit HSE is ready
while(!(RCC->CR & (0x1<<17))){}
// PLL OFF
RCC->CR &= ~(0x1<<24);
//HSE clock not divided
RCC->CFGR &= ~(0x1<<17);
// PLL SRC = HSE
RCC->CFGR |= 0x1<<16;
// PLL MUL, x9
RCC->CFGR &= ~(0xF<<18);
RCC->CFGR |= 0x7<<18;
// PLL ON
RCC->CR |= 0x1<<24;
// wait until PLL is ready
while(!(RCC->CR & (0x1 << 25))){}
// choose PLL as Sys clock
RCC->CFGR &= ~(0x3);
RCC->CFGR |= 0x2;
// AHB prescaler x1
RCC->CFGR &= ~(0xF<<4);
// APB1 (36MHz max ) prescaler /2
RCC->CFGR &= ~(0x7<<8);
RCC->CFGR |= (0x4<<8);
// APB2(72MHz) prescaler x1
RCC->CFGR &= ~(0x7<<11);
}