Skip to main content
Graduate
February 23, 2024
Solved

STM32C011 unbale to configure nRST pin as GPIO

  • February 23, 2024
  • 5 replies
  • 4605 views

Hi everyone,

Recently started working on STM32C011J4M3 micro. I'm trying to set PF2-NRST pin as a GPIO. but when I configure it from cube MX as GPIO PIN always reamins in High state that is a 3.28v and i'm unable to toggle the pin or perform any other operation.

 

int main(void)

{

/* USER CODE BEGIN 1 */

// SET_BIT(FLASH->OPTR,FLASH_OPTR_NRST_MODE_1);

WRITE_REG(FLASH->OPTR,0x2UL << 027U); // Even tried stting nrst mode2.

/* USER CODE END 1 */

 

/* MCU Configuration--------------------------------------------------------*/

 

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */

HAL_Init();

 

/* USER CODE BEGIN Init */

 

/* USER CODE END Init */

 

/* Configure the system clock */

SystemClock_Config();

 

/* USER CODE BEGIN SysInit */

 

/* USER CODE END SysInit */

 

/* Initialize all configured peripherals */

MX_GPIO_Init();

/* USER CODE BEGIN 2 */

 

/* USER CODE END 2 */

 

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

 

HAL_GPIO_WritePin(GPIOF, GPIO_PIN_2, GPIO_PIN_RESET);

HAL_Delay(3000);

HAL_GPIO_WritePin(GPIOF, GPIO_PIN_2, GPIO_PIN_SET);

/* USER CODE END WHILE */

 

/* USER CODE BEGIN 3 */

}

/* USER CODE END 3 */

}

 

can anyone help me with this,

do I need to add any push-up/pull down specifically to this pin to use as GPIO?

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

    To configure PF2 as a GPIO (input, output, AF, or analog I/O), set the NOT_RESET_INPUT_ONLY bit and clear the NOT_GPIO_MODE_ONLY bit of the FLASH option bytes. The new setting only takes effect upon the option byte loading (OBL) event following a reset. Until the reset release, PF2 keeps acting as reset I/O. When PF2 acts as a GPIO, reset can only be triggered from one of the device internal reset sources and the reset signal cannot be output.

    please see chapter 3.4.2, pages 57-58, from the STM32C0x1 advanced Arm<Sup>®</Sup>-based 32-bit MCUs - Reference manual

    please make sure it is not entering continuously in the portion of the code that triggers the option byte programming, as it can only withstand around 10K cycles and this can be achieved pretty fast if left unchecked:

    int main(void)
    {
     /* USER CODE BEGIN 1 */
     FLASH_OBProgramInitTypeDef OptionsBytesStruct;
     /* USER CODE END 1 */
    
     /* MCU Configuration--------------------------------------------------------*/
     /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
     HAL_Init();
     /* USER CODE BEGIN Init */
     /* Configure the system clock to 48 MHz */
     /* USER CODE END Init */
    
     /* Configure the system clock */
     SystemClock_Config();
     /* USER CODE BEGIN SysInit */
    
     /* USER CODE END SysInit */
    
     /* Initialize all configured peripherals */
     /* USER CODE BEGIN 2 */
     //Check to see if the option byte is already properly configured and only updates it if needed
     HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
     if((OptionsBytesStruct.USERConfig & OB_USER_NRST_MODE )!= OB_RESET_MODE_GPIO)
     {
     /* Unlock the Flash to enable the flash control register access *************/
     HAL_FLASH_Unlock();
     /* Unlock the Option Byte *************/
     HAL_FLASH_OB_Unlock();
     OptionsBytesStruct.OptionType = OPTIONBYTE_USER ; //Configure USER type
     OptionsBytesStruct.USERType = OB_USER_NRST_MODE;
     OptionsBytesStruct.USERConfig = OB_RESET_MODE_GPIO;//Set the NRST pin to behave only as GPIO
     HAL_FLASHEx_OBProgram(&OptionsBytesStruct);
     //Option byte update sequence
     SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
     while((FLASH->SR & FLASH_SR_BSY1) != 0)
     {
     NVIC_SystemReset();//MCU will reset to perform the option byte update
     }
     }
    

    5 replies

    Graduate
    February 23, 2024

    There was an error in the definition of ports in the HAL. I don't know if it's already fixed, I reported it here.

    Here 

    P.S. In your code is only one dellay.

    HAL_GPIO_WritePin(GPIOF, GPIO_PIN_2, GPIO_PIN_RESET);
    
    HAL_Delay(3000);
    
    HAL_GPIO_WritePin(GPIOF, GPIO_PIN_2, GPIO_PIN_SET);
    
    HAL_Delay(3000); /* new delay */
    Graduate
    February 23, 2024

    hi @ONadr.1  thanks for your response. I did check. issue is still not fixed according to your post. but this does not help me. those configurations are used for exti mode configuration and it should not be a issue for normal GPIO operation. still debigging further, will try with LL drivers

     

    Graduate
    February 23, 2024

    That problem is related to the NRST pin used as GPIO. The problem is that the base for port F, which is used as an output for the NRST pin, is incorrectly defined in the HAL code.

    See the last line of base addres definition.

     

     

    // original code 
    #define GPIO_GET_INDEX(__GPIOx__) (((__GPIOx__) == (GPIOA))? 0uL :\
     ((__GPIOx__) == (GPIOB))? 1uL :\
     ((__GPIOx__) == (GPIOC))? 2uL :\
     ((__GPIOx__) == (GPIOF))? 3uL : 4uL)
     
    //new code
    #define GPIO_GET_INDEX(__GPIOx__) (((__GPIOx__) == (GPIOA))? 0uL :\
     ((__GPIOx__) == (GPIOB))? 1uL :\
     ((__GPIOx__) == (GPIOC))? 2uL :\
     ((__GPIOx__) == (GPIOF))? 5uL : 6uL)

     

     

     

    pratik0199AuthorAnswer
    Graduate
    February 29, 2024

    To configure PF2 as a GPIO (input, output, AF, or analog I/O), set the NOT_RESET_INPUT_ONLY bit and clear the NOT_GPIO_MODE_ONLY bit of the FLASH option bytes. The new setting only takes effect upon the option byte loading (OBL) event following a reset. Until the reset release, PF2 keeps acting as reset I/O. When PF2 acts as a GPIO, reset can only be triggered from one of the device internal reset sources and the reset signal cannot be output.

    please see chapter 3.4.2, pages 57-58, from the STM32C0x1 advanced Arm<Sup>®</Sup>-based 32-bit MCUs - Reference manual

    please make sure it is not entering continuously in the portion of the code that triggers the option byte programming, as it can only withstand around 10K cycles and this can be achieved pretty fast if left unchecked:

    int main(void)
    {
     /* USER CODE BEGIN 1 */
     FLASH_OBProgramInitTypeDef OptionsBytesStruct;
     /* USER CODE END 1 */
    
     /* MCU Configuration--------------------------------------------------------*/
     /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
     HAL_Init();
     /* USER CODE BEGIN Init */
     /* Configure the system clock to 48 MHz */
     /* USER CODE END Init */
    
     /* Configure the system clock */
     SystemClock_Config();
     /* USER CODE BEGIN SysInit */
    
     /* USER CODE END SysInit */
    
     /* Initialize all configured peripherals */
     /* USER CODE BEGIN 2 */
     //Check to see if the option byte is already properly configured and only updates it if needed
     HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
     if((OptionsBytesStruct.USERConfig & OB_USER_NRST_MODE )!= OB_RESET_MODE_GPIO)
     {
     /* Unlock the Flash to enable the flash control register access *************/
     HAL_FLASH_Unlock();
     /* Unlock the Option Byte *************/
     HAL_FLASH_OB_Unlock();
     OptionsBytesStruct.OptionType = OPTIONBYTE_USER ; //Configure USER type
     OptionsBytesStruct.USERType = OB_USER_NRST_MODE;
     OptionsBytesStruct.USERConfig = OB_RESET_MODE_GPIO;//Set the NRST pin to behave only as GPIO
     HAL_FLASHEx_OBProgram(&OptionsBytesStruct);
     //Option byte update sequence
     SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
     while((FLASH->SR & FLASH_SR_BSY1) != 0)
     {
     NVIC_SystemReset();//MCU will reset to perform the option byte update
     }
     }
    
    Explorer II
    March 11, 2025

    There is a safer way to set the NRST pin as GPIO.

    1. Open STM32CubeProgrammer and connect to your target device.
    2. Look for the Option Bytes menu (third tab on the left).
    3. Unpack User Configuration menu.
    4. Find NRST_MODE and set it to 2 (GPIO: standard GPIO pad functionality, only internal reset possible).
    5. Click Apply and power cycle (or disconnect from target)

      AdamZ_0-1741693974976.png

    Graduate
    March 11, 2025

    @AdamZ  thanks for the respose.

    But this does not work for production. why to add additional step when you can handle this from code. you cannot configure each and every controller during production. plus this requires reliance on ST link debugger and does not work with Jlink. while most professionals used Jlink as standard programming tool.

    Visitor II
    April 16, 2024

    Hi everyone,

    Recently started working on STM32C011J4M3. I'm trying to set PF2-NRST pin as a GPIO and I want Pin 4  to multiplexed  to PA0 USART TX pin . At first i tried  multiplexed the Pin 4 to PA0  using the following API                        " LL_SYSCFG_ConfigPinMux(LL_PINMUX_SO8_PIN4_PA0);"  but this didn't work  and to mention that this API function worked  on Pin 1 and Pin 5 .  I came across this post  to  set PF2-NRST pin as a GPIO  and change the Pin 4 to PA0 thinking that  multiplex is only done with GPIO Ports . I tried above code to set the NOT_RESET_INPUT_ONLY bit and clear the NOT_GPIO_MODE_ONLY bit of the FLASH option bytes  this works in Registers but to my surprise  the Pin 4 still works as PF2-NRST pin  and not as GPIO .  Debugging works as always and when i remove the Pin4 connection  from debugger  I receive  console message as "Target is not responding, retrying..."  I conclude that this massage show that still the Pin 4 is set as  PF2-NRST pin even though  the NOT_GPIO_MODE_ONLY bit is cleared . I'm really confused plz  check the attachment and  help me out . 

     

    Graduate
    April 17, 2024

    you have to be very careful when working with this piece of code. it is mostly possible that you got locked out of your micro without setting the GPIO configuration properly.

     

    you have to run this code first thing while running main, and once you do OB write you have to reset controller. but before flashing this code to controller you have to make sure that you have configured this pin as A gpio port pA0 in gpio init function. as you loose your access to nrst and if your SWD and SWclck is also occupied you get locked out of the system. 

     

    as far as I can see you have not configured pin as PA0 gpio in output and now you are locked out of your system.

    Visitor II
    April 17, 2024

    Thanks for your response @pratik0199  I have Configured pin 4 as  pA0  USART TX  pin and this is done In USART Init function . Are you saying that I have initialize UART function to set Pin 4 to pA0 USART tx pin before calling OB write  function.

    Visitor II
    April 23, 2024

    Hello Guy,

                  the problem was with me i miss understood the SW1 pin of STM32C0116DK  as reset pin . The code provide by @pratik0199  works great . just config PF2 as GPIO pin   and using API "LL_SYSCFG_ConfigPinMux" is enough to multiples Pin4 (PF2) to PA0 .