STM32F103CBT6 configuration help

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

Moderator: RoccoMarco

Post Reply
joewa
Posts: 22
Joined: Tue May 13, 2014 10:40 pm

STM32F103CBT6 configuration help

Post by joewa »

Hi all!

I am new to Chibios and need some support getting started to configure my custom board.

Here is what I did so far but now I am a little lost...

I'd like to make the LED blink and communicate via USART.

I configured in mcuconf.h:

Code: Select all

#define STM32_NO_INIT                       FALSE
#define STM32_HSI_ENABLED                   TRUE
#define STM32_LSI_ENABLED                   FALSE
#define STM32_HSE_ENABLED                   FALSE
#define STM32_LSE_ENABLED                   FALSE
#define STM32_SW                            STM32_SW_PLL
#define STM32_PLLSRC                        STM32_PLLSRC_HSI //STM32_PLLSRC_HSE
#define STM32_PLLXTPRE                      STM32_PLLXTPRE_DIV1
#define STM32_PLLMUL_VALUE                  9
#define STM32_HPRE                          STM32_HPRE_DIV1
#define STM32_PPRE1                         STM32_PPRE1_DIV2
#define STM32_PPRE2                         STM32_PPRE2_DIV2
#define STM32_ADCPRE                        STM32_ADCPRE_DIV4
#define STM32_USB_CLOCK_REQUIRED            TRUE
#define STM32_USBPRE                        STM32_USBPRE_DIV1P5
#define STM32_MCOSEL                        STM32_MCOSEL_NOCLOCK
#define STM32_RTCSEL                        STM32_RTCSEL_NOCLOCK //STM32_RTCSEL_LSI //STM32_RTCSEL_HSEDIV
#define STM32_PVD_ENABLE                    FALSE
#define STM32_PLS                           STM32_PLS_LEV0
....
#define STM32_UART_USE_USART1               TRUE
...


and in halconf.h

Code: Select all

#define HAL_USE_PAL                 TRUE


The LEDs are at PB4 & 5. So I configured in my board.h ...

Code: Select all

#if !defined(STM32_LSECLK)
#define STM32_LSECLK                0
#endif
#define STM32_LSEDRV                (3 << 3)
#if !defined(STM32_HSECLK)
#define STM32_HSECLK                0
#endif
#define STM32_HSE_BYPASS
#define STM32F10X_MD
....
//#define VAL_GPIOBCRL            0x4B668888      /*  PB7...PB0 */
//#define VAL_GPIOBCRH            0xAAA88888      /* PB15...PB8 */
#define VAL_GPIOBCRL            0x4B338888      /*  PB7...PB0 */
#define VAL_GPIOBCRH            0xAAA88888      /* PB15...PB8 */
#define VAL_GPIOBODR            0xFFFFFFFF


Now I tried to blink the LEDs

Code: Select all

    palTogglePad(GPIOB, 4);
    palTogglePad(GPIOB, 5);

Which makes the LED at PB5 blink but NOT the LED at PB4!

What may be wrong there?

To verify that the board is ok, this code (using libopencm3) makes both LEDs blink as they should:

Code: Select all

/*
 * This file is part of the libopencm3 project.
 */

#include <libopencm3/stm32/f1/rcc.h>
#include <libopencm3/stm32/f1/gpio.h>

static void clock_setup(void)
{
   /* Set STM32 to 64 MHz. */
   rcc_clock_setup_in_hsi_out_64mhz();

   /* Enable alternate function peripheral clock. */
   rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);

   /* Enable GPIOB clock. */
   rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
}

static void gpio_setup(void)
{

   /* Configure PB4 as GPIO. */
   AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST;
   
   /* Set GPIO4 and 5 (in GPIO port B) to 'output push-pull'. */
   gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
            GPIO_CNF_OUTPUT_PUSHPULL, GPIO4 | GPIO5);
   
}

int main(void)
{
   int i;

   clock_setup();
   gpio_setup();

   /* Blink the LEDs on the board. */
   while (1) {
      gpio_toggle(GPIOB, GPIO4);   /* LED on/off */
      for (i = 0; i < 8000000; i++)   /* Wait a bit. */
         __asm__("nop");
      gpio_toggle(GPIOB, GPIO5);   /* LED on/off */
      for (i = 0; i < 8000000; i++)   /* Wait a bit. */
         __asm__("nop");
   }

   return 0;
}



Additionally the USART configuration seems not to work...

The full code is at:
https://github.com/joewa/bldc-strip

You can see the schematics at:
https://github.com/joewa/open-bldc-hardware/tree/master/strip/v0.2

Cheers,
Jörg
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F103CBT6 configuration help

Post by Giovanni »

Look at the code, in order to be able to use PB4 you have to remap the JTAG function allocated there, it is the line that modifies the AFIO.

Code: Select all

   /* Configure PB4 as GPIO. */
   AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_JNTRST;


You can copy that line in your chibios application and it should work.

Giovanni
joewa
Posts: 22
Joined: Tue May 13, 2014 10:40 pm

Re: STM32F103CBT6 configuration help

Post by joewa »

Hi Giovanni,

thanks for the quick reply!

There is a typo but nevertheless you put me on the right path :)

I added to my board.c

Code: Select all

void boardInit(void) {
   /* Configure PB4 as GPIO. */
   AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_NOJNTRST;
   /* Make USART1 working */
   AFIO->MAPR |= AFIO_MAPR_USART1_REMAP;
}

Tthe LED and USART1 are working now!

Can you please tell me where these things are documented?

Cheers,
Jörg
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F103CBT6 configuration help

Post by Giovanni »

Hi,

The STM32 Reference Manual :)

Giovanni
joewa
Posts: 22
Joined: Tue May 13, 2014 10:40 pm

Re: STM32F103CBT6 configuration help

Post by joewa »

Hi again :)
anyway I am still struggling to set up my board properly.
I need to setup PB13, PB14 and PB15 as GPIO.
Setting up PWM for PA8-10 is now working!
Here is what I did:

Code: Select all

   /* Configure PB4 as GPIO. */
   AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_NOJNTRST;
   /* Make USART1 working */
   AFIO->MAPR |= AFIO_MAPR_USART1_REMAP;
   /* Remap CH1N, CH2N, CH3N and BKIN to unused pins */
   AFIO->MAPR |= AFIO_MAPR_TIM1_REMAP_PARTIALREMAP;
   /* Remap USART3 to make PB13 & PB14 available for driver control as GPIO ??????? */
   AFIO->MAPR |= AFIO_MAPR_USART3_REMAP_FULLREMAP;

This is my config in board.h

Code: Select all

#define VAL_GPIOACRL            0x88000000      /*  PA7...PA0 */
#define VAL_GPIOACRH            0x88888AAA      /* PA15...PA8 */
#define VAL_GPIOAODR            0xFFFFFFFF
#define VAL_GPIOBCRL            0x4B778888      /*  PB7...PB0 */
#define VAL_GPIOBCRH            0xAAA88888      /* PB15...PB8 */
#define VAL_GPIOBODR            0xFFFFFFFF

Do you have an idea what is missing again?

Thanks!
Jörg
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32F103CBT6 configuration help

Post by Giovanni »

"A" means Alternate Push Pull not GPIO.

See the following table:

Code: Select all

/*
 * I/O ports initial setup, this configuration is established soon after reset
 * in the initialization code.
 *
 * The digits have the following meaning:
 *   0 - Analog input.
 *   1 - Push Pull output 10MHz.
 *   2 - Push Pull output 2MHz.
 *   3 - Push Pull output 50MHz.
 *   4 - Digital input.
 *   5 - Open Drain output 10MHz.
 *   6 - Open Drain output 2MHz.
 *   7 - Open Drain output 50MHz.
 *   8 - Digital input with PullUp or PullDown resistor depending on ODR.
 *   9 - Alternate Push Pull output 10MHz.
 *   A - Alternate Push Pull output 2MHz.
 *   B - Alternate Push Pull output 50MHz.
 *   C - Reserved.
 *   D - Alternate Open Drain output 10MHz.
 *   E - Alternate Open Drain output 2MHz.
 *   F - Alternate Open Drain output 50MHz.
 * Please refer to the STM32 Reference Manual for details.
 */


Giovanni
Post Reply