cancel
Showing results for 
Search instead for 
Did you mean: 

STM32C031 About starting I2C and starting USART1 interrupt

shank
Associate III

Hello
I am creating a program using STM32C031.

Before entering while(1) when powering up, I would like to use HAL_I2C to retrieve EEPROM data from the IC, and then start the USART1 and USART2 receive interrupts and the SPI1 transmit interrupt.
However, in my current program, after reading data with I2C, I intend to enable the USART1 receive interrupt, but it is not enabled, and USART1 reception does not start. By the way, the USART2 and SPI1 interrupts start normally.
Furthermore, if I switch the order of the I2C read function (rfid_check()) and the USART1 interrupt enable command(huart1.Instance->CR1 |= 0x20;), the USART1 interrupt will also work normally. However, in this design, I do not want the USART interrupt to work while I2C is running. If you know a solution, I would appreciate it if you could let me know.

 

void rfid_check(){
  unsigned char x;
  
    for(x=0;x<8;x++){
      rfid_buff[x] = *((uint8_t*)0x08006810+x*0x00000004);
    }
    HAL_Delay(10);
    
    while(icdata_flg==0){
      HAL_GPIO_WritePin(GPIOA, ANNTENA_SW_Pin,GPIO_PIN_SET);
      HAL_Delay(10);
      tdata[0]=0x01;
      HAL_I2C_Master_Transmit(&hi2c1,0xaa,tdata,1,100);
      HAL_Delay(10);
      HAL_I2C_Master_Receive(&hi2c1,0xab,rdata,16,100);
      HAL_Delay(10);
      HAL_GPIO_WritePin(GPIOA, ANNTENA_SW_Pin,GPIO_PIN_RESET);
      
      if((rdata[0]!=0)&&(rdata[1]!=0)){ 
        icdata_flg = 1;
      }
      
      zero_cnt++;
      if((icdata_flg!=1)&&(zero_cnt>10)){
        rdata[0] = rfid_buff[0];
        rdata[1] = rfid_buff[1];
        rdata[2] = rfid_buff[2];
        rdata[3] = rfid_buff[3];
        icdata_flg = 1;
        zero_flg = 1;
      }
    }
    rec_addset = *((uint8_t*)0x08006830);
}


int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

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

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */
  Data_Init();

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  __HAL_RCC_SYSCFG_CLK_ENABLE();
  
  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SPI1_Init();
  MX_USART1_UART_Init();
  MX_I2C1_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  *(int*)0xE000ED08 = 0x08002800;

  rfid_chack();
  huart1.Instance->CR1 |= 0x20;
  huart2.Instance->CR1 |= 0x20;
  hspi1.Instance->CR1 |= 0x40;
  
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  
  /* USER CODE END 3 */
}

 

1 REPLY 1
shank
Associate III

I wrote that it would work properly if I swapped the I2C function and the USAR1 interrupt enable instructions, but it did not work properly. In this case, USART1 was not receiving properly either.
I have confirmed that it works properly if I put the I2C function inside a while(1) loop. I don't really understand how the I2C operation and the USART1 operation affect each other.