cancel
Showing results for 
Search instead for 
Did you mean: 

TIMER

DYann.1
Senior

Hello,

I found an example on the internet of a project but I don't understand the usefulness of using a Timer 7. Could you tell me why ? Thanks in advance.

 

__IO uint32_t usTick;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  /* Prevent unused argument(s) compilation warning */
  if(htim==&htim7){
	usTick++;	
	}
  /* NOTE : This function Should not be modified, when the callback is needed,
            the __HAL_TIM_PeriodElapsedCallback could be implemented in the user file
   */
}

void delay_us(__IO uint32_t Delay){
uint32_t t=usTick;
uint32_t wait = Delay;
while( usTick - t < wait);

}
int main(void)
{

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

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

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM7_Init();
	/*if (HAL_TIM_Base_Start(&htim7) != HAL_OK) {
		      _Error_Handler(__FILE__, __LINE__);
    }*/
	if (HAL_TIM_Base_Start_IT(&htim7) != HAL_OK) {
		      _Error_Handler(__FILE__, __LINE__);
    }

  MX_I2C1_Init();
	  MX_I2S2_Init();

	HAL_Delay(100);

	audio.init();


	bool ptt=HAL_GPIO_ReadPin(GPIOE,PTT_SWITCH);
	bool pttstate=ptt;
	uint16_t index = 0;
	uint16_t buf = 0;
	audio.set_volume(0);
  /* Infinite loop */
  	while (1)//	read power switch
	{
		ptt=HAL_GPIO_ReadPin(GPIO_PINS,PTT_SWITCH);
		if(ptt!=pttstate){
			if(ptt==0){

				audio.start_receive();
				for(buf=0;buf<32;buf++){
					for(index=0;index < AUDIO_BUF_SIZE;index++){
						HAL_I2SEx_TransmitReceive(&hi2s2, &i2s_zero,&i2s_buf[buf][index], 1,0xFF);
					}
				}

				audio.start_play();
				for(buf=0;buf<32;buf++){
					for(index=0;index < AUDIO_BUF_SIZE;index++){
						HAL_I2SEx_TransmitReceive(&hi2s2, &i2s_buf[buf][index],&i2s_zero, 1,0xFF);
					}
				}
			}
			
			pttstate=ptt;
		}
  	}

}

 

 

40 REPLIES 40
DYann.1
Senior

It's probably to do a clocking in µs ?

Yes, exactly this.

DYann.1
Senior

Yes but only use in the HAL_TIM_PeriodElapsedCallback and nothing else (the usTick) and apparently the timer works by itself and we don't use it manually to recover the data. I don't see the use of the Timer yet the author says : 'This function Should not be modified, when the callback is needed, the __HAL_TIM_PeriodElapsedCallback could be implemented in the user file'

But there is no callback so why the Timer is necessary ? Thank you for your helps.

TDK
Guru

If the random code you found needs to interrupt at 1 MHz, I would say maybe find a different code to go from.

If you feel a post has answered your question, please click "Accept as Solution".
Pavel A.
Evangelist II

>This function Should not be modified

This looks like just copy/paste from some place in the template code; the HAL_TIM_PeriodElapsedCallback is defined there with "weak" attribute.  In your code it is exactly the "implementation in user file". This function *is* the callback from timer interrupt handler.

/* Example why bad comments are worse than no comments */

 

In this case, I don't need to implement a Timer I guess since there is no callback.

DYann.1
Senior

Hi, 

I tried to adapt the code to my microprocessor but I don't know which one to choose? In your opinion ?

DYann1_0-1698682177704.png

After me the SAI (equivalent to I2S) work with 3 possible modes: IRQ, DMA and IT. Probably, I'll start with IRQ to make it as simple as possible.

I do this but I get a warning. Do you know how to call the SAI function to retrieve the values ?

DYann1_1-1698683324614.png

Pavel A.
Evangelist II

Source of the ST library is available. Please read the documentation in the library files (.h and c) , it is helpful. The compiler warnings are very helpful - please read them and try to understand the reason.

 

DYann.1
Senior

Hi,

Just to need the type and now It's correct.

  uint8_t txData[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
	 start_receive ();
	 for(buf=0;buf<32;buf++){
		 for(index=0;index < AUDIO_BUF_SIZE;index++){
			HAL_SAI_Transmit(&hsai_BlockB1,txData,1, 0xFF);
		 }
	 }

But in the example found on the internet the person uses the TransmitRrecieve command. 

HAL_I2SEx_TransmitReceive(&hi2s2, &i2s_zero,&i2s_buf[buf][index], 1,0xFF);

Where to find in the documentation to find the right command to retrieve the values ?