cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F030C8T6 with M35 Modem Usart2 Receiving Problem

raj11
Associate II
Posted on November 14, 2016 at 12:25

Hi,

I have connected STM32f030c8t6 with M35 modem using Usart 2 , Stn32f0 transmits the Command Successfully, but it can't retrive the reply given by the modem. This my code. I have tested this code with shorting RX and TX of Stm32f0 , by transmitting a string and receiving it successfully. I didn't get it where i am going wrong in this code.

#include <
stdint.h
>
#include <
stdio.h
>
#include <
stm32f0xx_gpio.h
>
#include <
stm32f0xx_rcc.h
>
#include <
stm32f0xx_usart.h
>
#include <
semihosting.h
>
#include <
stm32f0xx_misc.h
>
#include <
string.h
>
#define MAX_BUFFER_SIZE 256
typedef struct Buffer_st
{
uint8_t size;
char data[MAX_BUFFER_SIZE];
}Buffer_st;
Buffer_st receivedDataUART2;
char temp_buff1[300]={'\0'};
volatile uint8_t OKGot = 0,GGAGot=0;
short flag=0,irq_bit=0;
void UARTSend(const unsigned char *pucBuffer, unsigned long ulCount);
void UART_Send1(const unsigned char pucBuffer, unsigned long ulCount);
void resetReceivedDataBufferUART2(void);
void USART2_IRQHandler(void)
{
uint8_t j=0;
// if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
receivedDataUART2.data[receivedDataUART2.size++] = (char)USART_ReceiveData(USART2);
if(receivedDataUART2.size == MAX_BUFFER_SIZE)
{ resetReceivedDataBufferUART2();
receivedDataUART2.size=0;
}
}
printf(''U2: %s \n'',receivedDataUART2.data);
}
void resetReceivedDataBufferUART2(void)
{
receivedDataUART2.size = 0;
memset(receivedDataUART2.data, MAX_BUFFER_SIZE, receivedDataUART2.size);
}
int main (void)
{
const unsigned char init[]=''AT\r\n'';
const unsigned char init8[]=''ATD*111#\r\n'';
const unsigned char init9[]=''ATE1\r\n'';
int64_t i=0;
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef N;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
//Configure USART2 pins: Rx and Tx ----------------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
USART_DeInit(USART2);
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(USART2, &USART_InitStructure);
USART_OverrunDetectionConfig(USART2, USART_OVRDetection_Disable);
USART_Cmd(USART2,ENABLE);
// INterrupt for USART2
N.NVIC_IRQChannel = USART2_IRQn;
N.NVIC_IRQChannelCmd = ENABLE;
N.NVIC_IRQChannelPriority = 0;
NVIC_Init(&N);
NVIC_EnableIRQ(USART2_IRQn);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
resetReceivedDataBufferUART2();
// power of the modem init
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; // Green LED
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1;
GPIO_Init(GPIOB, & GPIO_InitStructure);
GPIO_WriteBit(GPIOB, GPIO_Pin_14,Bit_SET);
for (i=0; i<2500000;i++);
GPIO_WriteBit(GPIOB, GPIO_Pin_14, Bit_RESET);
for (i=0; i<3000000;i++);
resetReceivedDataBufferUART2();
for(i=0;i<(SystemCoreClock/1920);i++);
UARTSend(init,(sizeof(init)-1)); // AT command
for(i=0;i<(SystemCoreClock/1920);i++);
UARTSend(init9,(sizeof(init9)-1)); // ATE1 command
for(i=0;i<(SystemCoreClock/1920);i++);
UARTSend(init8,(sizeof(init8)-1)); // ATD*111# command
while(1)
{
}
}
void UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{
while(ulCount--)
{
USART_SendData(USART2,(uint16_t) *pucBuffer++);// Last Version USART_SendData(USART1,(uint16_t) *pucBuffer++);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
{
}
}
}
void UART_Send1(const unsigned char pucBuffer, unsigned long ulCount)
{
while(ulCount--)
{
USART_SendData(USART2,(uint16_t) pucBuffer);// Last Version USART_SendData(USART1,(uint16_t) *pucBuffer++);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
{
}
}
}

#m35-modem #uart #stm32f030
2 REPLIES 2
Posted on November 14, 2016 at 17:42

Can we please keep this topic/theme in a single thread, you've got 3 or 4 going now. This is a final warning.

Delay loops are a bad plan, the variable needs to be volatile so the compiler won't optimize it away, but it is NOT how the modem works. It sends you a response, you should wait for it, and process it.

Don't use printf() in an interrupt. Ensure that if you are using strings, that they have proper NUL terminations.

Consider using a ring/fifo buffer in the RXNE IRQHandler, and then review the content of the buffer in the main function, instead of the empty wait loops you have now.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
raj11
Associate II
Posted on November 14, 2016 at 17:48

Sorry for the inconvenience Mr.Clive1.......