Advertisement
Guest User

Untitled

a guest
Apr 8th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.19 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22.  
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. #include <math.h>
  26. /* USER CODE END Includes */
  27.  
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */
  30.  
  31. /* USER CODE END PTD */
  32.  
  33. /* Private define ------------------------------------------------------------*/
  34. /* USER CODE BEGIN PD */
  35. /* USER CODE END PD */
  36.  
  37. /* Private macro -------------------------------------------------------------*/
  38. /* USER CODE BEGIN PM */
  39.  
  40. /* USER CODE END PM */
  41.  
  42. /* Private variables ---------------------------------------------------------*/
  43. I2C_HandleTypeDef hi2c3;
  44.  
  45. I2S_HandleTypeDef hi2s2;
  46.  
  47. /* USER CODE BEGIN PV */
  48.  
  49. /* USER CODE END PV */
  50.  
  51. /* Private function prototypes -----------------------------------------------*/
  52. void SystemClock_Config(void);
  53. static void MX_GPIO_Init(void);
  54. static void MX_I2C3_Init(void);
  55. static void MX_I2S2_Init(void);
  56. /* USER CODE BEGIN PFP */
  57.  
  58. /* USER CODE END PFP */
  59.  
  60. /* Private user code ---------------------------------------------------------*/
  61. /* USER CODE BEGIN 0 */
  62. enum CodecRegister {
  63.   CODEC_REG_LEFT_LINE_IN = 0x00,
  64.   CODEC_REG_RIGHT_LINE_IN = 0x01,
  65.   CODEC_REG_LEFT_HEADPHONES_OUT = 0x02,
  66.   CODEC_REG_RIGHT_HEADPHONES_OUT = 0x03,
  67.   CODEC_REG_ANALOGUE_ROUTING = 0x04,
  68.   CODEC_REG_DIGITAL_ROUTING = 0x05,
  69.   CODEC_REG_POWER_MANAGEMENT = 0x06,
  70.   CODEC_REG_DIGITAL_FORMAT = 0x07,
  71.   CODEC_REG_SAMPLE_RATE = 0x08,
  72.   CODEC_REG_ACTIVE = 0x09,
  73.   CODEC_REG_RESET = 0x0f,
  74. };
  75.  
  76. enum CodecSettings {
  77.   CODEC_INPUT_0_DB = 0x17,
  78.   CODEC_INPUT_UPDATE_BOTH = 0x40,
  79.   CODEC_HEADPHONES_MUTE = 0x00,
  80.   CODEC_MIC_BOOST = 0x1,
  81.   CODEC_MIC_MUTE = 0x2,
  82.   CODEC_ADC_MIC = 0x4,
  83.   CODEC_ADC_LINE = 0x0,
  84.   CODEC_OUTPUT_DAC_ENABLE = 0x10,
  85.   CODEC_OUTPUT_MONITOR = 0x20,
  86.   CODEC_DEEMPHASIS_NONE = 0x00,
  87.   CODEC_DEEMPHASIS_32K = 0x01,
  88.   CODEC_DEEMPHASIS_44K = 0x02,
  89.   CODEC_DEEMPHASIS_48K = 0x03,
  90.   CODEC_SOFT_MUTE = 0x01,
  91.   CODEC_ADC_HPF = 0x00,
  92.  
  93.   CODEC_POWER_DOWN_LINE_IN = 0x01,
  94.   CODEC_POWER_DOWN_MIC = 0x02,
  95.   CODEC_POWER_DOWN_ADC = 0x04,
  96.   CODEC_POWER_DOWN_DAC = 0x08,
  97.   CODEC_POWER_DOWN_LINE_OUT = 0x10,
  98.   CODEC_POWER_DOWN_OSCILLATOR = 0x20,
  99.   CODEC_POWER_DOWN_CLOCK_OUTPUT = 0x40,
  100.   CODEC_POWER_DOWN_EVERYTHING = 0x80,
  101.  
  102.   CODEC_PROTOCOL_MASK_MSB_FIRST = 0x00,
  103.   CODEC_PROTOCOL_MASK_LSB_FIRST = 0x01,
  104.   CODEC_PROTOCOL_MASK_PHILIPS = 0x02,
  105.   CODEC_PROTOCOL_MASK_DSP = 0x03,
  106.  
  107.   CODEC_FORMAT_MASK_16_BIT = 0x00 << 2,
  108.   CODEC_FORMAT_MASK_20_BIT = 0x01 << 2,
  109.   CODEC_FORMAT_MASK_24_BIT = 0x02 << 2,
  110.   CODEC_FORMAT_MASK_32_BIT = 0x03 << 2,
  111.  
  112.   CODEC_FORMAT_LR_SWAP = 0x20,
  113.   CODEC_FORMAT_MASTER = 0x40,
  114.   CODEC_FORMAT_SLAVE = 0x00,
  115.   CODEC_FORMAT_INVERT_CLOCK = 0x80,
  116.  
  117.   CODEC_RATE_48K_48K = 0x00 << 2,
  118.   CODEC_RATE_8K_8K = 0x03 << 2,
  119.   CODEC_RATE_96K_96K = 0x07 << 2,
  120.   CODEC_RATE_32K_32K = 0x06 << 2,
  121.   CODEC_RATE_44K_44K = 0x08 << 2,
  122. };
  123.  
  124.  
  125.  
  126. uint8_t init_codec() {
  127.     uint8_t status = 0;
  128.     uint8_t val = 0;
  129.     // reset codec
  130.     status += HAL_I2C_Mem_Write(&hi2c3, (0x1A << 1 ), CODEC_REG_RESET, 1, &val, 1, HAL_MAX_DELAY);
  131.  
  132.     // config headphone outputs
  133. //  val = CODEC_HEADPHONES_MUTE;
  134. //  status += HAL_I2C_Mem_Write(&hi2c3, (0x1A << 1 ), CODEC_REG_LEFT_HEADPHONES_OUT, 1, &val, 1, HAL_MAX_DELAY);
  135. //  status += HAL_I2C_Mem_Write(&hi2c3, (0x1A << 1 ), CODEC_REG_RIGHT_HEADPHONES_OUT, 1, &val, 1, HAL_MAX_DELAY);
  136.     // config analog routing
  137.     val = CODEC_MIC_MUTE | CODEC_ADC_LINE | CODEC_OUTPUT_DAC_ENABLE;
  138.     status += HAL_I2C_Mem_Write(&hi2c3, (0x1A << 1 ), CODEC_REG_ANALOGUE_ROUTING, 1, &val, 1, HAL_MAX_DELAY);
  139.     // Configure digital routing
  140.     val = CODEC_DEEMPHASIS_NONE;
  141.     status += HAL_I2C_Mem_Write(&hi2c3, (0x1A << 1 ), CODEC_REG_DIGITAL_ROUTING, 1, &val, 1, HAL_MAX_DELAY);
  142.     // config power management
  143.     val = CODEC_POWER_DOWN_MIC;
  144.     status += HAL_I2C_Mem_Write(&hi2c3, (0x1A << 1 ), CODEC_REG_POWER_MANAGEMENT, 1, &val, 1, HAL_MAX_DELAY);
  145.     // format byte
  146.     val = CODEC_PROTOCOL_MASK_PHILIPS | CODEC_FORMAT_MASK_16_BIT | CODEC_FORMAT_MASTER;
  147.     status += HAL_I2C_Mem_Write(&hi2c3, (0x1A << 1 ), CODEC_REG_DIGITAL_FORMAT, 1, &val, 1, HAL_MAX_DELAY);
  148.     val = CODEC_RATE_48K_48K;
  149.     status += HAL_I2C_Mem_Write(&hi2c3, (0x1A << 1 ), CODEC_REG_SAMPLE_RATE, 1, &val, 1, HAL_MAX_DELAY);
  150.     // For now codec is not active.
  151.     val = 0x01;
  152.     status += HAL_I2C_Mem_Write(&hi2c3, (0x1A << 1 ), CODEC_REG_ACTIVE, 1, &val, 1, HAL_MAX_DELAY);
  153.  
  154.     return status;
  155.  
  156. }
  157. static int16_t audio_data[2 * 1200];
  158. void play_tone() {
  159.     for (int i = 0; i < 1200; i++) {
  160.         int16_t value = (int16_t)(32000.0 * sin(2.0 * M_PI * i / 48.0));
  161.         audio_data[i * 2] = value;
  162.         audio_data[i * 2 + 1] = value;
  163.     }
  164. }
  165.  
  166. /* USER CODE END 0 */
  167.  
  168. /**
  169.   * @brief  The application entry point.
  170.   * @retval int
  171.   */
  172. int main(void)
  173. {
  174.   /* USER CODE BEGIN 1 */
  175.  
  176.   /* USER CODE END 1 */
  177.  
  178.   /* MCU Configuration--------------------------------------------------------*/
  179.  
  180.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  181.   HAL_Init();
  182.  
  183.   /* USER CODE BEGIN Init */
  184.  
  185.   /* USER CODE END Init */
  186.  
  187.   /* Configure the system clock */
  188.   SystemClock_Config();
  189.  
  190.   /* USER CODE BEGIN SysInit */
  191.  
  192.   /* USER CODE END SysInit */
  193.  
  194.   /* Initialize all configured peripherals */
  195.   MX_GPIO_Init();
  196.   MX_I2C3_Init();
  197.   MX_I2S2_Init();
  198.   /* USER CODE BEGIN 2 */
  199.   uint8_t status = init_codec();
  200.   play_tone();
  201.   /* USER CODE END 2 */
  202.  
  203.   /* Infinite loop */
  204.   /* USER CODE BEGIN WHILE */
  205.   while (1)
  206.   {
  207.       HAL_I2S_Transmit(&hi2s2, (uint16_t*)audio_data, 2 * 1200, HAL_MAX_DELAY);
  208.     /* USER CODE END WHILE */
  209.  
  210.     /* USER CODE BEGIN 3 */
  211.   }
  212.   /* USER CODE END 3 */
  213. }
  214.  
  215. /**
  216.   * @brief System Clock Configuration
  217.   * @retval None
  218.   */
  219. void SystemClock_Config(void)
  220. {
  221.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  222.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  223.   RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  224.  
  225.   /** Configure the main internal regulator output voltage
  226.   */
  227.   __HAL_RCC_PWR_CLK_ENABLE();
  228.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  229.   /** Initializes the RCC Oscillators according to the specified parameters
  230.   * in the RCC_OscInitTypeDef structure.
  231.   */
  232.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  233.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  234.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  235.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  236.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  237.   RCC_OscInitStruct.PLL.PLLM = 8;
  238.   RCC_OscInitStruct.PLL.PLLN = 168;
  239.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  240.   RCC_OscInitStruct.PLL.PLLQ = 4;
  241.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  242.   {
  243.     Error_Handler();
  244.   }
  245.   /** Initializes the CPU, AHB and APB buses clocks
  246.   */
  247.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  248.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  249.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  250.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  251.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  252.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  253.  
  254.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  255.   {
  256.     Error_Handler();
  257.   }
  258.   PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
  259.   PeriphClkInitStruct.PLLI2S.PLLI2SN = 192;
  260.   PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
  261.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  262.   {
  263.     Error_Handler();
  264.   }
  265. }
  266.  
  267. /**
  268.   * @brief I2C3 Initialization Function
  269.   * @param None
  270.   * @retval None
  271.   */
  272. static void MX_I2C3_Init(void)
  273. {
  274.  
  275.   /* USER CODE BEGIN I2C3_Init 0 */
  276.  
  277.   /* USER CODE END I2C3_Init 0 */
  278.  
  279.   /* USER CODE BEGIN I2C3_Init 1 */
  280.  
  281.   /* USER CODE END I2C3_Init 1 */
  282.   hi2c3.Instance = I2C3;
  283.   hi2c3.Init.ClockSpeed = 100000;
  284.   hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
  285.   hi2c3.Init.OwnAddress1 = 0;
  286.   hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  287.   hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  288.   hi2c3.Init.OwnAddress2 = 0;
  289.   hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  290.   hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  291.   if (HAL_I2C_Init(&hi2c3) != HAL_OK)
  292.   {
  293.     Error_Handler();
  294.   }
  295.   /* USER CODE BEGIN I2C3_Init 2 */
  296.  
  297.   /* USER CODE END I2C3_Init 2 */
  298.  
  299. }
  300.  
  301. /**
  302.   * @brief I2S2 Initialization Function
  303.   * @param None
  304.   * @retval None
  305.   */
  306. static void MX_I2S2_Init(void)
  307. {
  308.  
  309.   /* USER CODE BEGIN I2S2_Init 0 */
  310.  
  311.   /* USER CODE END I2S2_Init 0 */
  312.  
  313.   /* USER CODE BEGIN I2S2_Init 1 */
  314.  
  315.   /* USER CODE END I2S2_Init 1 */
  316.   hi2s2.Instance = SPI2;
  317.   hi2s2.Init.Mode = I2S_MODE_MASTER_TX;
  318.   hi2s2.Init.Standard = I2S_STANDARD_PHILIPS;
  319.   hi2s2.Init.DataFormat = I2S_DATAFORMAT_16B;
  320.   hi2s2.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
  321.   hi2s2.Init.AudioFreq = I2S_AUDIOFREQ_48K;
  322.   hi2s2.Init.CPOL = I2S_CPOL_LOW;
  323.   hi2s2.Init.ClockSource = I2S_CLOCK_PLL;
  324.   hi2s2.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_DISABLE;
  325.   if (HAL_I2S_Init(&hi2s2) != HAL_OK)
  326.   {
  327.     Error_Handler();
  328.   }
  329.   /* USER CODE BEGIN I2S2_Init 2 */
  330.  
  331.   /* USER CODE END I2S2_Init 2 */
  332.  
  333. }
  334.  
  335. /**
  336.   * @brief GPIO Initialization Function
  337.   * @param None
  338.   * @retval None
  339.   */
  340. static void MX_GPIO_Init(void)
  341. {
  342.  
  343.   /* GPIO Ports Clock Enable */
  344.   __HAL_RCC_GPIOH_CLK_ENABLE();
  345.   __HAL_RCC_GPIOB_CLK_ENABLE();
  346.   __HAL_RCC_GPIOC_CLK_ENABLE();
  347.   __HAL_RCC_GPIOA_CLK_ENABLE();
  348.  
  349. }
  350.  
  351. /* USER CODE BEGIN 4 */
  352.  
  353. /* USER CODE END 4 */
  354.  
  355. /**
  356.   * @brief  This function is executed in case of error occurrence.
  357.   * @retval None
  358.   */
  359. void Error_Handler(void)
  360. {
  361.   /* USER CODE BEGIN Error_Handler_Debug */
  362.   /* User can add his own implementation to report the HAL error return state */
  363.  
  364.   /* USER CODE END Error_Handler_Debug */
  365. }
  366.  
  367. #ifdef  USE_FULL_ASSERT
  368. /**
  369.   * @brief  Reports the name of the source file and the source line number
  370.   *         where the assert_param error has occurred.
  371.   * @param  file: pointer to the source file name
  372.   * @param  line: assert_param error line source number
  373.   * @retval None
  374.   */
  375. void assert_failed(uint8_t *file, uint32_t line)
  376. {
  377.   /* USER CODE BEGIN 6 */
  378.   /* User can add his own implementation to report the file name and line number,
  379.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  380.   /* USER CODE END 6 */
  381. }
  382. #endif /* USE_FULL_ASSERT */
  383.  
  384. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  385.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement