How to change Serial in STM32F103C8T6?

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
mehungry
Posts: 2
Joined: Sun Jan 13, 2013 2:59 pm

How to change Serial in STM32F103C8T6?

Post by mehungry »

Hello!

I have STM32F103C8T6 (http://www.kamami.pl/dl/avt1609.pdf) and as programmer I use STM32VLDISCOVERY (STM32F100RB).
I have a problem with USART2 because there Rx seems to be broken it doesn't react on anything. So I wanted to use "demo" for STM32F103 and change USART to 1 or 3 but I have a problem with both. I have tested PA9(USART1_TX) and PB10 (USART3_TX) and there is nothing in there.

Can you tell me what do I have to change except this:

Code: Select all

/*
 * SERIAL driver system settings.
 */
#define STM32_SERIAL_USE_USART1             TRUE
#define STM32_SERIAL_USE_USART2             TRUE
#define STM32_SERIAL_USE_USART3             TRUE
#define STM32_SERIAL_USE_UART4              FALSE
#define STM32_SERIAL_USE_UART5              FALSE
#define STM32_SERIAL_USE_USART6             FALSE
#define STM32_SERIAL_USART1_PRIORITY        12
#define STM32_SERIAL_USART2_PRIORITY        12
#define STM32_SERIAL_USART3_PRIORITY        12
#define STM32_SERIAL_UART4_PRIORITY         12
#define STM32_SERIAL_UART5_PRIORITY         12
#define STM32_SERIAL_USART6_PRIORITY        12


main.c:

Code: Select all

#include "ch.h"
#include "hal.h"

int main(void) {
 
  halInit();
  chSysInit();

   sdStart(&SD1, NULL);
   sdStart(&SD2, NULL);
   sdStart(&SD3, NULL);
   sdWrite(&SD1,"ChibiOS/RT Wita na pokladzie!\n\r",32);
   sdWrite(&SD2,"ChibiOS/RT Wita na pokladzie!\n\r",32);
   sdWrite(&SD3,"ChibiOS/RT Wita na pokladzie!\n\r",32);

  while (TRUE) {
      sdWrite(&SD1,"SD1\n\r",5);
      chThdSleepMilliseconds(50);
      sdWrite(&SD2,"SD2\n\r",5);
      chThdSleepMilliseconds(50);
      sdWrite(&SD3,"SD3\n\r",5);

      chThdSleepMilliseconds(500);
  }
}


What can be wrong?
Why only USART2 works?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: How to change Serial in STM32F103C8T6?

Post by Giovanni »

Hi,

You also have to program the I/O pins to be USART instead of GPIO, this is done by changing the default settings in board.h or changing the mode at runtime using palSetPadMode().

On the F103, if I remember well, RX pins must be "input", TX pins must be "alternate". Newer STM32 device are different, both RX and TX must be "alternate(n)".

Giovanni
mehungry
Posts: 2
Joined: Sun Jan 13, 2013 2:59 pm

Re: How to change Serial in STM32F103C8T6?

Post by mehungry »

Thanks very much. Now it works great on USART 3. It seams USART2 realy have some problem with RX.

changes in board.h:

Code: Select all

#define VAL_GPIOBCRH            0xB4B84B88

Now it works fine for USART3:
- RX at PB11
- TX at PB10
I have change this:

Code: Select all

PB15 PB14 PB13 PB12 PB11 PB10  PB9  PB8
1011 0100 1011 1000 1000 1000 1000 1000

into this:

Code: Select all

PB15 PB14 PB13 PB12 PB11 PB10  PB9  PB8
1011 0100 1011 1000 0100 1011 1000 1000

i mean:

Code: Select all

/*
 * Port B setup.
 * Everything input with pull-up except:
 * PB13 - Alternate output  (MMC SPI2 SCK).
 * PB14 - Normal input      (MMC SPI2 MISO).
 * PB15 - Alternate output  (MMC SPI2 MOSI).
 */
#define VAL_GPIOBCRL            0x88888888      /*  PB7...PB0 */
#define VAL_GPIOBCRH            0xB4B84B88 //0xB4B88888      /* PB15...PB8 */
#define VAL_GPIOBODR            0xFFFFFFFF


How to use palSetPadMode?
I don't know where to find constans for Port B. When I write:

Code: Select all

palSetPadMode(GPIOB, /*PB10*/0xA, 0x3);
   palSetPadMode(GPIOB, /*PB11*/0xB, 0x0);

it doesn't work. I think I still don't understand this. I know there should be some constant somewhere but I have no idea where to look for it.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: How to change Serial in STM32F103C8T6?

Post by Giovanni »

Hi,

Look at the documentation of the PAL driver in the STM32 HAL manual, there are predefined constants there. Note that the constants are different between the STM32F1 and newer devices.

For the F1:

Code: Select all

/**
 * @brief   After reset state.
 * @details The state itself is not specified and is architecture dependent,
 *          it is guaranteed to be equal to the after-reset state. It is
 *          usually an input state.
 */
#define PAL_MODE_RESET                  0

/**
 * @brief   Safe state for <b>unconnected</b> pads.
 * @details The state itself is not specified and is architecture dependent,
 *          it may be mapped on @p PAL_MODE_INPUT_PULLUP,
 *          @p PAL_MODE_INPUT_PULLDOWN or @p PAL_MODE_OUTPUT_PUSHPULL for
 *          example.
 */
#define PAL_MODE_UNCONNECTED            1

/**
 * @brief   Regular input high-Z pad.
 */
#define PAL_MODE_INPUT                  2

/**
 * @brief   Input pad with weak pull up resistor.
 */
#define PAL_MODE_INPUT_PULLUP           3

/**
 * @brief   Input pad with weak pull down resistor.
 */
#define PAL_MODE_INPUT_PULLDOWN         4

/**
 * @brief   Analog input mode.
 */
#define PAL_MODE_INPUT_ANALOG           5

/**
 * @brief   Push-pull output pad.
 */
#define PAL_MODE_OUTPUT_PUSHPULL        6

/**
 * @brief   Open-drain output pad.
 */
#define PAL_MODE_OUTPUT_OPENDRAIN       7

/**
 * @brief   STM32 specific alternate push-pull output mode.
 */
#define PAL_MODE_STM32_ALTERNATE_PUSHPULL   16

/**
 * @brief   STM32 specific alternate open-drain output mode.
 */
#define PAL_MODE_STM32_ALTERNATE_OPENDRAIN  17


Giovanni
Post Reply