Skip to main content
ed_62
Associate II
August 20, 2015
Question

USART on STM32F4 using Arduino board

  • August 20, 2015
  • 2 replies
  • 1969 views
Posted on August 21, 2015 at 00:28

NOTE: edited by moderator on Oct 6th, 2016

Hello everyone,

I just received my stm32f4 discovery board yesterday and it works well :). I managed to run a LED blinking example. I now want to display some results in a serial communication. I am trying to interface an Arduino Uno with my stm board. I found an example on the internet :

[[this link/image has been flagged as malicious by our security scan software and has been deleted]]

but I am not able to make it works properly.

I put wires between TX of Arduino- PB7 and RX of Arduino - PB6. Here is my Arduino code :

#include <SoftwareSerial.h>
SoftwareSerial gprsSerial(0, 1);
void
setup() {
// initialize serial communication at 9600 bits per second:
gprsSerial.begin(9600);
Serial.begin(9600);
}
/* Loop() */
void
loop() { 
// Si des données sont disponible en lecture
if
(gprsSerial.available() > 0) {
// Lecture d'un octet depuis le port série
int

 c = gprsSerial.read();
Serial.println(c);}
}

When I click on the reset button of the stm board, I receive these 4 numbers on the Arduinoterminal : 255;28;0;252 I don't know how to deal with these numbers. It represents Byte but what is the relation ? Moreover, If I change the sentence to display.

USART_puts(
''b''
);

I still have 4 numbers : 255;224;0;252

Do you know what it means ? Normally, I should receive more numbers for the sentence than the char. Thank you very much for your help.
    This topic has been closed for replies.

    2 replies

    ed_62
    ed_62Author
    Associate II
    August 23, 2015
    Posted on August 23, 2015 at 20:54

    NOTE: edited by moderator on Oct 6th, 2016

    I have modified the way I dot it. I linked the pin Reset with the Ground of the Arduino board. So I directly receive the data from the STM32 without writing any code on the Arduino board. But it still doesn't work ... I use this terminal application :

    https://sites.google.com/site/terminalbpp/

    Here is my code :

    #include ''stm32f4xx.h''
    #include ''stm32f4xx_gpio.h''
    #include ''stm32f4xx_rcc.h''
    #include ''stm32f4xx_usart.h''
    #include ''misc.h''
    inline 
    void
    Delay(__IO uint32_t nCount);
    void
    UART_Initialize(
    void
    );
    void
    GPIOInitialize(
    void
    );
    void
    NVICInitialize(
    void
    );
    void
    USART_puts( volatile 
    char
    *s);
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    int
    main(
    void
    )
    {
    GPIOInitialize();
    UART_Initialize();
    NVICInitialize();
    //USART_puts(''B'');
    while
    (1) {
    /* Variable gérant le ''spinner'' */
    unsigned 
    char
    spin = 1;
    /* Boucle infini */
    for
    (;;) {
    /* Lecture de l'état du bouton, si appuyé on entre dans le if */
    if
    (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) {
    USART_SendData(USART1, 'A');
    /* ''Ecrit'' les états des led en fonction du spinner (4 bits) */
    GPIO_WriteBit(GPIOD, GPIO_Pin_12, (spin & 1) ? 1 : 0); 
    // LSB
    GPIO_WriteBit(GPIOD, GPIO_Pin_13, (spin & 2) ? 1 : 0); 
    //
    GPIO_WriteBit(GPIOD, GPIO_Pin_14, (spin & 4) ? 1 : 0); 
    //
    GPIO_WriteBit(GPIOD, GPIO_Pin_15, (spin & 8) ? 1 : 0); 
    // MSB
    /* Delai (~1sec en gros) */
    Delay(0x3FFFFF);
    /* Rotation du ''spinner'' d'un cran */
    spin = (spin << 1) & 0x0f;
    /* Si le ''spinner'' a dépassé la valeur max : retour au début */
    if
    (spin == 0)
    spin = 1;
    }
    }
    }
    }
    /* Fonction de delai (trés grossiére) */
    void
    Delay(__IO uint32_t nCount) {
    while
    (nCount--)
    ;
    }
    void
    UART_Initialize(
    void
    )
    {
    /* Enable peripheral clock for USART1 */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    /* USART1 configured as follow:
    * BaudRate 9600 baud
    * Word Length 8 Bits
    * 1 Stop Bit
    * No parity
    * Hardware flow control disabled
    * Receive and transmit enabled
    */
    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(USART1, &USART_InitStructure); 
    // USART configuration
    USART_Cmd(USART1, ENABLE); 
    // Enable USART
    }
    void
    GPIOInitialize(
    void
    )
    {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); 
    //Enable clock for GPIOB
    /* Structure permettant de stocker la configuration d'une ou plusieurs broches */
    GPIO_InitTypeDef GPIO_InitStructure;
    /* Mise en marche des horloge gérant les ports D (led) et A (bouton) */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); 
    // Led
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 
    // Button
    /* Configuration de LED3, LED4, LED5, LED6 en sortie de type ''pushpull'' (mode classique) */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; 
    // LED3, LED4, LED5, LED6
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; 
    // Broches en sortie
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
    // Mode PushPull
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
    // Fréquence de commutation maximum
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 
    // Pas de résistance de pull-up / pull-down
    GPIO_Init(GPIOD, &GPIO_InitStructure);
    /* USART1 Tx on PB6 | Rx on PB7 */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
    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(GPIOB, &GPIO_InitStructure);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
    //Connect PB6 to USART1_Tx
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);
    //Connect PB7 to USART1_Rx
    /* Configuration du bouton ''User'' en entrée de type ''pushpull'' (mode classique) */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; 
    // Bouton ''User''
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    // Broches en entrée
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
    // Mode PushPull
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
    // Fréquence de commutation maximum
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 
    // Pas de résistance de pull-up / pull-down
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    }
    void
    NVICInitialize(
    void
    )
    {
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
    }
    /*void
    USART_puts(volatile 
    char
    *s){
    while
    (*s){
    // wait until data register is empty
    while
    ( !(USART1->SR & 0x00000040) );
    USART_SendData(USART1, *s);
    *s++;
    }
    }*/
    #ifdef USE_FULL_ASSERT
    void
    assert_failed(uint8_t* file, uint32_t line)
    {
    while
    (1)
    {}
    }
    #endif

    I am receiving these characters on the terminal : <0>ü

    I tried to change the sending characters but I still receive these kinds of characters : à, û, ü, <0>... All the parameters seem to be corrects. Do you have an idea ofhow to solve this problem ?

    There is something which is surprising. IfI connect PB6 ( Tx of STM32) to Rx of Arduino board, I receive nothing on the terminal. i have to connect PB6 to TX of Arduino to make It works...

    Thank you in advance for your help.

    ________________

    Attachments :

    Terminal.png : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0bn&d=%2Fa%2F0X0000000bbA%2FncDuLsE36UDUPsZk6lD2uBMuXLROR2o2Wp5G9JX3paU&asPdf=false
    Tesla DeLorean
    Guru
    August 23, 2015
    Posted on August 23, 2015 at 22:27

    NOTE: edited by moderator on Oct 6th, 2016

    The serial outputs from the STM32 are CMOS levels, RS232 compatible ones.

    The STM32F4-DISCO uses an 8 MHz clock source, not a 25 MHz one, make sure the PLL settings are for the right clock source, and that the HSE_VALUE define matches the clock being used.

    Makes sure to check TXE before writing data to the port, and check RXNE before reading.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..