Вообще оно тут:
https://sourceforge.net/projects/bluetoothcar/Насчет 20 строчек я, конечно, загнул, там больше, но 80% - копипаста со стандартного примера.
За код не бейте, это мой опыт в сях после доооолгого перерыва.
Конкретно звук вот:
Код:
/* Includes ------------------------------------------------------------------*/
#include "stm32l1xx.h"
#include "sound.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define DAC_DHR8R1_Address 0x40007410
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure1;
DAC_InitTypeDef DAC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
//uint8_t Idx = 0;
/* Private function prototypes -----------------------------------------------*/
void DAC_PreConfig(void);
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program
* @param None
* @retval None
*/
void saySomething(uint8_t wave [],int loop)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32l1xx_md.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32l1xx.c file
*/
/* Preconfiguration before using DAC----------------------------------------*/
DAC_PreConfig();
/* TIM2 Configuration ------------------------------------------------------*/
/* TIM2 Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure1);
TIM_TimeBaseStructure1.TIM_Period = SystemCoreClock/11025;
TIM_TimeBaseStructure1.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure1.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure1.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure1);
/* TIM2 TRGO selection */
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
DAC_DeInit();
/* DAC channel1 and channel2 Configuration */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
/* DAC channel1 Configuration */
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
/* DMA1 channel2 configuration */
DMA_DeInit(DMA1_Channel2);
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R1_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)(&(wave[54]));
DMA_InitStructure.DMA_Mode = loop ? DMA_Mode_Circular : DMA_Mode_Normal;
DMA_InitStructure.DMA_BufferSize = *(uint32_t*)(&(wave[46]));
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
/* Enable DMA1 Channel2 */
DMA_Cmd(DMA1_Channel2, ENABLE);
/* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is
automatically connected to the DAC converter. */
DAC_Cmd(DAC_Channel_1, ENABLE);
/* Enable DMA for DAC Channel2 */
DAC_DMACmd(DAC_Channel_1, ENABLE);
}
/**
* @brief PrecConfiguration: configure PA4 and PA5 in analog,
* enable DAC clock, enable DMA1 clock
* @param None
* @retval None
*/
void DAC_PreConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* DMA1 clock enable (to be used with DAC) */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DAC Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
/* GPIOA clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* Configure PA.04 (DAC_OUT1), PA.05 (DAC_OUT2) as analog */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}