hardware serial for GSM

Post here first, or if you can't find a relevant section!
Post Reply
geologic
Posts: 10
Joined: Thu Dec 15, 2022 10:12 am
Answers: 1

hardware serial for GSM

Post by geologic »

Hi All

I've made a custom board with STM32L051 IC and i'm using Arduino_Core_STM32. I created a variant based on L051C(6-8)(T-U) example and changed Serial to UART1 (PA9,PA10 pins).
My variant_mydevice.h looks like:

Code: Select all

// UART Definitions
#ifndef SERIAL_UART_INSTANCE
  #define SERIAL_UART_INSTANCE  0	
#endif

// Default pin used for generic 'Serial' instance
// Mandatory for Firmata
#ifndef PIN_SERIAL_RX
  #define PIN_SERIAL_RX         PA10	
#endif
#ifndef PIN_SERIAL_TX
  #define PIN_SERIAL_TX         PA9 	
#endif
I did not modify PeripheralPins.c:

Code: Select all

//*** UART ***

#ifdef HAL_UART_MODULE_ENABLED
WEAK const PinMap PinMap_UART_TX[] = {
  {PA_2,  USART2,  STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF4_USART2)},
  {PA_9,  USART1,  STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF4_USART1)},
  {PA_14, USART2,  STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF4_USART2)},
  {PB_6,  USART1,  STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_USART1)},
  {PB_10, LPUART1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF4_LPUART1)},
  {NC,    NP,      0}
};
#endif

#ifdef HAL_UART_MODULE_ENABLED
WEAK const PinMap PinMap_UART_RX[] = {
  {PA_3,  USART2,  STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF4_USART2)},
  {PA_10, USART1,  STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF4_USART1)},
  {PA_15, USART2,  STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF4_USART2)},
  {PB_7,  USART1,  STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_USART1)},
  {PB_11, LPUART1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF4_LPUART1)},
  {NC,    NP,      0}
};
#endif
I have a Serial to USB adapter on pins PA9, PA10 and Serial.println() is working just fine on serial monitor.
I have a GSM modem attached to USART2 (PA3, PA2 pins) and i'm trying to send AT commands with code/serial monitor, but it does not work.

Here is my code:

Code: Select all


HardwareSerial Serial2(PA2, PA3);        // (RX,TX)   
            
void setup() {

  Serial.begin(9600);
  Serial.println("### AT TEST ###");
  delay(500);
  Serial.flush();  

  Serial2.begin(9600);
  delay(5000);
  Serial2.println("AT"); 			//Once the handshake test is successful, it will back to OK
  updateSerial();

}

void loop()
{
  updateSerial();
}

void updateSerial()
{
  delay(500);
  while (Serial.available()) {
    Serial2.write(Serial.read());
  }
  while(Serial2.available()) {
    Serial.write(Serial2.read());
  }
}
When running, i see at serial monitor "AT### AT TEST ###" instead of the expected "OK"
If i type any AT command, serial monitor always shows the same.

I already tried swapping HardwareSerial Serial2(PA3, PA2), but that shows a blank serial monitor.

If i use softwareSerial, replacing Serial2 by mySerial

Code: Select all

#include <SoftwareSerial.h>
SoftwareSerial mySerial(PA2, PA3);        // (RX,TX) 


it works as expected, i see AT response "OK" and if i enter AT commands i see the response at the serial monitor.

Can you please help?
Thanks
by geologic » Mon Nov 13, 2023 10:36 pm
After your confirmation that HardwareSerial Serial2(PA3, PA2); was the right syntax, i narrowed the problem to a simple swap TX and RX pins on the GSM side.

Thanks fpiSTM for your help, you point me into the right direction.
Go to full post
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: hardware serial for GSM

Post by fpiSTM »

First

Code: Select all

HardwareSerial Serial2(PA3, PA2);        
Is the correct syntax.
geologic
Posts: 10
Joined: Thu Dec 15, 2022 10:12 am
Answers: 1

Re: hardware serial for GSM

Post by geologic »

After your confirmation that HardwareSerial Serial2(PA3, PA2); was the right syntax, i narrowed the problem to a simple swap TX and RX pins on the GSM side.

Thanks fpiSTM for your help, you point me into the right direction.
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: hardware serial for GSM

Post by fpiSTM »

Welcome
aryastark
Posts: 1
Joined: Wed Feb 28, 2024 2:03 am
Location: https://buckshotroulette.com

Re: hardware serial for GSM

Post by aryastark »

You haven't modified PeripheralPins.c, but its default configuration might interfere with your USART2 setup. I recommend manually defining pin mappings for USART2 in `variant_mydevice.h`.

Code: Select all

#define PA2_ALT_FUNC  STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_USART2)
#define PA3_ALT_FUNC  STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_USART2)
Post Reply

Return to “General discussion”