Skip to main content
DYann.1
Senior II
October 30, 2023
Question

TIMER

  • October 30, 2023
  • 40 replies
  • 9791 views

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

}

 

 

This topic has been closed for replies.

40 replies

DYann.1
DYann.1Author
Senior II
October 30, 2023

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

Pavel A.
October 30, 2023

Yes, exactly this.

DYann.1
DYann.1Author
Senior II
October 30, 2023

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
October 30, 2023

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.
October 30, 2023

>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 */

 

DYann.1
DYann.1Author
Senior II
October 30, 2023

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

DYann.1
DYann.1Author
Senior II
October 30, 2023

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.
October 30, 2023

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
DYann.1Author
Senior II
October 31, 2023

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 ?

LCE
Principal II
October 31, 2023

"someone something on the internet" + HAL stuff...

Good example for: read the ref manual and learn what you need to know, together with checking the HAL functions to understand what's going on.

There so much great stuff out there, but also lots of... not so good stuff. You can't just copy and paste things and expect them to work, especially on another processor or if you only see part of the application.

DYann.1
DYann.1Author
Senior II
October 31, 2023

Hi,

When I have this configuration with the SAI + DMA, I have no communication problem with I2C to configure the CODEC.

DYann1_0-1698759525211.png

But I don't know where the DMA stores the values ? How to recover these values ? Do you have any information or documentation so I can better understand ?

Thank you

LCE
Principal II
October 31, 2023

Oh my... you need some basic tutorials!

CubeMX does just the basic setup for the peripherals.

You need to start the DMA transfers, by doing that you assign some buffers, and that's where you'll find the data.

Look for some simple example code for your STM32, go through that step by step and try to understand that.

DYann.1
DYann.1Author
Senior II
October 31, 2023

Of cours I'm a beginner... So you have a problem with theses people ? I just ask how DMA works or the documentation.

After your explanation, I can find an example of code with my evaluation card and thank you

DYann1_0-1698769291858.png

But it's even better if I can find the principle of how DMA works, I've seen so many tutorials on youtube and code examples but nothing matches what I'm doing exactly.