Skip to main content
Rkolh.1
Associate
May 7, 2021
Solved

STM32F103C8 pins PD0 & PD1 not working as output help me to resolve this issue i am using stm32 cube mx ide

  • May 7, 2021
  • 13 replies
  • 4404 views

I configured all pins of stm32f103c8 as output. All pins are toggling and only PD0 and PD1 aren't toggling .. I am using internal crystal of stm32f103c8 then obviously the PD0 and PD1 pins are free for configure as output or input.

This topic has been closed for replies.
Best answer by TDK

PD0/PD1 are used by the external oscillator by default. To use them as GPIO pins, see "Using OSC_IN/OSC_OUT pins as GPIO ports PD0/PD1" in the reference manual.

13 replies

TDK
TDKBest answer
May 7, 2021

PD0/PD1 are used by the external oscillator by default. To use them as GPIO pins, see "Using OSC_IN/OSC_OUT pins as GPIO ports PD0/PD1" in the reference manual.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Rkolh.1
Rkolh.1Author
Associate
May 8, 2021

Thank you .give me direct solution for this​

Rkolh.1
Rkolh.1Author
Associate
May 8, 2021

Thank you . Then stm32cube Mx generates faulty code to make remap pins On PD0 and PD1 and configure as ​IO? Please give me direct solution for this because I am new on stm32

​

Rkolh.1
Rkolh.1Author
Associate
May 8, 2021

IN REFERENCE MANUAL

​

9.3.2 Using OSC_IN/OSC_OUT pins as GPIO ports PD0/PD1

The HSE oscillator pins OSC_IN/OSC_OUT can be used as general-purpose I/O PD0/PD1 

by programming the PD01_REMAP bit in the AF remap and debug I/O configuration register 

(AFIO_MAPR).

This remap is available only on 36-, 48- and 64-pin packages (PD0 and PD1 are available 

on 100-pin and 144-pin packages, no need for remapping).

Note: The external interrupt/event function is not remapped. PD0 and PD1 cannot be used for 

external interrupt/event generation on 36-, 48- and 64-pin packages.

waclawek.jan
Super User
May 8, 2021

OK so as you have the 48-pin package, you need to set AFIO_MAPR.PD01_REMAP to be able to use the oscillator pins as PD0/PD1. It appers that in Cube/HAL, you could use AFIO_REMAP_ENABLE(AFIO_MAPR_PD01_REMAP) . I don't use Cube.

Note, that to be able to do that, the AFIO clock in RCC has to be enabled first, by setting RCC_APB2ENR.AFIOEN.

JW

Rkolh.1
Rkolh.1Author
Associate
May 8, 2021

Thank you .. you tried your best to explain it to me. but my outcomes are zero.as your suggestion i found out

__HAL_AFIO_REMAP_PD01_ENABLE();

 __HAL_RCC_AFIO_CLK_ENABLE();

TDK
May 8, 2021
/**
 * @brief GPIO Initialization Function
 * @param None
 * @retval None
 */
static void MX_GPIO_Init(void)
{
 GPIO_InitTypeDef GPIO_InitStruct = {0};
 
 /* GPIO Ports Clock Enable */
 __HAL_RCC_GPIOD_CLK_ENABLE();
 
 /*Configure GPIO pin Output Level */
 HAL_GPIO_WritePin(GPIOD, GPIO_PIN_0|GPIO_PIN_1, GPIO_PIN_RESET);
 
 /*Configure GPIO pins : PD0 PD1 */
 GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
 
 /*Configure peripheral I/O remapping */
 __HAL_AFIO_REMAP_PD01_ENABLE();
 
}

> Then stm32cube Mx generates faulty code to make remap pins

Seems right to me. (Note that  __HAL_RCC_AFIO_CLK_ENABLE() is called within HAL_MspInit.)

"If you feel a post has answered your question, please click ""Accept as Solution""."
Rkolh.1
Rkolh.1Author
Associate
May 8, 2021

Thank you TDK but not getting Output. Please cheak code

/* MAIN CODE*/

 HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_0);

 HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_1);

   HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13);

   HAL_Delay(10);

/* GPIO INIT FUNCTION*/

static void MX_GPIO_Init(void)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 /* GPIO Ports Clock Enable */

 __HAL_RCC_GPIOC_CLK_ENABLE();

 __HAL_RCC_GPIOD_CLK_ENABLE();

 __HAL_RCC_GPIOA_CLK_ENABLE();

 /*Configure GPIO pin Output Level */

 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);

 /*Configure GPIO pin Output Level */

 HAL_GPIO_WritePin(GPIOD, GPIO_PIN_0|GPIO_PIN_1, GPIO_PIN_RESET);

 /*Configure GPIO pins : PC13 PC14 PC15 */

 GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;

 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

 /*Configure GPIO pins : PD0 PD1 */

 GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;

 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

 /*Configure peripheral I/O remapping */

 __HAL_AFIO_REMAP_PD01_ENABLE();

}

/* SYSTEM INT*/

HAL_StatusTypeDef HAL_Init(void)

{

 /* Configure Flash prefetch */

#if (PREFETCH_ENABLE != 0)

#if defined(STM32F101x6) || defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG) || \

  defined(STM32F102x6) || defined(STM32F102xB) || \

  defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || \

  defined(STM32F105xC) || defined(STM32F107xC)

 /* Prefetch buffer is not available on value line devices */

 __HAL_FLASH_PREFETCH_BUFFER_ENABLE();

#endif

#endif /* PREFETCH_ENABLE */

 /* Set Interrupt Group Priority */

 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);

 /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */

 HAL_InitTick(TICK_INT_PRIORITY);

 /* Init the low level hardware */

 HAL_MspInit();

 /* Return function status */

 return HAL_OK;

}

/* MSP INIT */

__weak void HAL_MspInit(void)

{

 /* NOTE : This function should not be modified, when the callback is needed,

      the HAL_MspInit could be implemented in the user file

  */

}

/*MY EDITED MSP INT*/

__weak void HAL_MspInit(void)

{

 /* NOTE : This function should not be modified, when the callback is needed,

      the HAL_MspInit could be implemented in the user file

  */

 __HAL_RCC_AFIO_CLK_ENABLE();

}

NOT GETTING OUTPUT:confounded_face:

TDK
May 8, 2021

Per the comments in the code, you shouldn't modify the HAL_MspInit function. It should be implemented in user code. CubeMX generates it for you within "stm32f1xx_hal_msp.c". Here's what it generated for me:

/**
 * Initializes the Global MSP.
 */
void HAL_MspInit(void)
{
 /* USER CODE BEGIN MspInit 0 */
 
 /* USER CODE END MspInit 0 */
 
 __HAL_RCC_AFIO_CLK_ENABLE();
 __HAL_RCC_PWR_CLK_ENABLE();
 
 /* System interrupt init*/
 
 /** DISABLE: JTAG-DP Disabled and SW-DP Disabled
 */
 __HAL_AFIO_REMAP_SWJ_DISABLE();
 
 /* USER CODE BEGIN MspInit 1 */
 
 /* USER CODE END MspInit 1 */
}

But that's probably in your code too if you've generated it from CubeMX. If it is, not sure what the issue is. I don't have an F103 board.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Rkolh.1
Rkolh.1Author
Associate
May 8, 2021

Thank you friend.. you can try stm32f103c6

Simulation in protues 8.11​