cancel
Showing results for 
Search instead for 
Did you mean: 

STM32C011 unbale to configure nRST pin as GPIO

pratik0199
Associate II

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?

1 ACCEPTED SOLUTION

Accepted Solutions
pratik0199
Associate II

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
        }
  }

View solution in original post

4 REPLIES 4
ONadr.1
Senior III

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  */
pratik0199
Associate II

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

 

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)

 

 

 

pratik0199
Associate II

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
        }
  }