cancel
Showing results for 
Search instead for 
Did you mean: 

TIMER

DYann.1
Senior II

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
LCE
Principal

"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
Senior II

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

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.

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.

LCE
Principal

Of cours I'm a beginner... So you have a problem with theses people ?

Surely not, but most people here don't have the time to take you by the hand.
That's why I say, read the manual, check some tutorials, example code, etcpp...
It helps if there are at least some basics.
As AC/DC said, it's a long way to the top! ;)

The good thing: trial and error will (probably & hopefully) not kill your hardware.

Pavel A.
Evangelist III

@DYann.1 Here is the official ST tutorial on the DMA :   https://wiki.st.com/stm32mcu/wiki/Getting_started_with_DMA

And there are so many folks ready to patiently help beginners .... they just expect something in return :) Care and share goes both ways.

 

ST official training on STM32L5: https://www.st.com/content/st_com/en/support/learning/stm32-education/stm32-online-training/stm32l5-online-training.html

Enjoy!

Thank you, I'll look at these 2 links.

DYann.1
Senior II

Hi,

After reading the SAI and DMA documentation, I coded a piece of the program. I think I'm recovering data. Here is the code what do you think ? Thank you for your help.

 

int main(void)
{
  /* USER CODE BEGIN 1 */
	HAL_StatusTypeDef fresult;
	int32_t playbuf[4096*2] __attribute__ ((aligned (32)));
  /* 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();

/* Configure the peripherals common clocks */
  PeriphCommonClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_SAI1_Init();
  MX_I2C1_Init();
  /* USER CODE BEGIN 2 */
  HAL_SAI_RegisterCallback(&hsai_BlockB1, HAL_SAI_TX_HALFCOMPLETE_CB_ID, HAL_SAI_TxHalfCpltCallback);
  HAL_SAI_RegisterCallback(&hsai_BlockB1, HAL_SAI_TX_COMPLETE_CB_ID, HAL_SAI_TxCpltCallback);
  fresult= HAL_SAI_Init(&hsai_BlockB1);
  if (fresult != HAL_OK)
	{
  	return HAL_ERROR;
	}
  fresult = HAL_SAI_Transmit_DMA(&hsai_BlockB1, (uint8_t *)playbuf , (sizeof(playbuf))/4);
  HAL_Delay(100);
  SGTL5000_init();
  set_volume(0);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
	  start_receive();
	  HAL_Delay(100);
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

Here is what I see in the table :

DYann1_0-1698920072462.png

 

Pavel A.
Evangelist III

The last image "Expressions" is indeed confusing. The values in the rightmost column are not values but addresses of 100-item slices of array playbuf.  If you expand any slice it will show the actual array elements.

This is how the Eclipse debugger behaves ((

LCE
Principal

For simple testing without the Codec, you could directly connect SAI data output to data input, then compare transmit buffer ("playbuf")  with the receive buffer.

Start with a simple signal in playbuf, like playbuf[i] = i; i++;