Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.68 KB | None | 0 0
  1. #include "stm32f4xx.h"
  2. #include "stm32f4_discovery.h"
  3. #include "delay.h"
  4. #include <stdio.h>
  5. #include "fpu.h"
  6. #include "spi_sd.h"
  7. #include "ff.h"
  8. #include <string.h>
  9. #include <stdint.h>
  10.  
  11. FATFS fatfs;
  12. FIL fileWAVE;
  13. FRESULT SD_fres;
  14. UINT sav_bytes = 0;;
  15.  
  16. //WAVE
  17. uint32_t z = 0;
  18. int FileCounter = 0; //Counter of files
  19. //uint32_t lengths = 8; //Length of the file in seconds
  20. void MakeFile();
  21. extern uint8_t rawAudio[123200];
  22.  
  23. uint32_t empty = 0; //Set empty integer for Sizes
  24. uint32_t ForDat = 16; //Set length of format data
  25. uint16_t Type = 1; //Set it as PCM file
  26. uint16_t Channels = 1; //1 for Mono, 2 for Stereo
  27. uint32_t SampleRate = 16000; //Number of Samples per sec
  28. uint16_t BitsPerSample = 8;
  29. uint32_t ByteRate = 16000; //SampleRate * Channels * BitsPerSample / 8; //Average rate of bytes
  30. uint16_t BlockAllign = 1;//Channels * BitsPerSample / 8; //Bytes per sample including all channels
  31. uint32_t FileSize = 0;//SampleRate * lengths + 44 - 8;
  32. uint32_t DataSize =0; //SampleRate * lengths;
  33.  
  34.  
  35.  
  36.  
  37. //adc
  38. uint16_t ADC_Result =0;
  39. void initADC();
  40.  
  41. void initRGB();
  42. void RGB_Red();
  43. void RGB_Green();
  44. void RGB_Blue();
  45. //Holders
  46. void initTimH();
  47. int c=0 ;
  48.  
  49. //----------------------
  50. void EXTI0_IRQHandler(void)
  51. {
  52. if(EXTI_GetITStatus(EXTI_Line0) != RESET)
  53. {
  54. EXTI_ClearITPendingBit(EXTI_Line0);
  55. TIM_Cmd(TIM3, ENABLE);
  56.  
  57. }
  58. }
  59.  
  60. void TIM3_IRQHandler(void)
  61. {
  62. if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  63. {
  64. if(TIM_GetFlagStatus(TIM3, TIM_FLAG_Update)) {
  65. if(!GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_0)){
  66.  
  67. c++; //////////////////////<---------co sie robi po wcisniecu przycisku
  68. TIM_Cmd(TIM3, DISABLE);
  69. TIM_SetCounter(TIM3, 0);
  70. }
  71. TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  72.  
  73. }}}
  74.  
  75.  
  76.  
  77.  
  78. void TIM4_IRQHandler(void)
  79. {
  80. if(TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
  81. {
  82. if(TIM_GetFlagStatus(TIM4, TIM_FLAG_Update)) {
  83. if(c==0){
  84. GPIO_ResetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  85. RGB_Blue();
  86. }
  87. if(c==1){
  88. //Set filename
  89. FileCounter++;
  90. char fileName[11];
  91. sprintf(fileName, "File%d.wav", FileCounter);
  92.  
  93.  
  94. //init constants:
  95. char * RIFF = ("RIFF");
  96. char * WAVE = ("WAVE");
  97. char * fmt = ("fmt ");
  98. char * data = ("data");
  99.  
  100. z = 0;
  101. SD_fres = f_open( &fileWAVE, fileName, FA_OPEN_ALWAYS | FA_WRITE );
  102.  
  103. //HEADER
  104. SD_fres = f_write(&fileWAVE, ( const void * ) RIFF,4, &sav_bytes);
  105. SD_fres = f_write(&fileWAVE, &empty,4, &sav_bytes);
  106. SD_fres = f_write(&fileWAVE, ( const void * ) WAVE,4, &sav_bytes);
  107. SD_fres = f_write(&fileWAVE, ( const void * ) fmt, 4, &sav_bytes);
  108. SD_fres = f_write(&fileWAVE, &ForDat,4, &sav_bytes);
  109. SD_fres = f_write(&fileWAVE, &Type,2, &sav_bytes);
  110. SD_fres = f_write(&fileWAVE, &Channels,2, &sav_bytes);
  111. SD_fres = f_write(&fileWAVE, &SampleRate,4, &sav_bytes);
  112. SD_fres = f_write(&fileWAVE, &ByteRate,4, &sav_bytes);
  113. SD_fres = f_write(&fileWAVE, &BlockAllign,2, &sav_bytes);
  114. SD_fres = f_write(&fileWAVE, &BitsPerSample,2, &sav_bytes);
  115. SD_fres = f_write(&fileWAVE, ( const void * ) data,4, &sav_bytes);
  116. SD_fres = f_write(&fileWAVE, &empty,4, &sav_bytes);
  117.  
  118. z += 44;
  119. //uint32_t NumOfSampleSets = SampleRate * 10; //Get the number of Sample sets
  120. /*uint32_t d = 0;
  121.  
  122. int x=50000;
  123. while (x >= 1)
  124. {
  125. if(d >= sizeof(rawAudio)/sizeof(rawAudio[0])) {d = 0; printf("%d", (int)d);}
  126. uint8_t sample = rawAudio[d];
  127. d++; z += 1;
  128. SD_fres = f_write(&fileWAVE, &sample,1, &sav_bytes);
  129.  
  130.  
  131. }
  132. */
  133.  
  134. GPIO_SetBits(GPIOD,GPIO_Pin_12);
  135. c++;
  136. }
  137. if(c==2){
  138. RGB_Red();
  139. GPIO_SetBits(GPIOD,GPIO_Pin_13);
  140. ADC_SoftwareStartConv(ADC1);
  141. while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  142. ADC_Result = ADC_GetConversionValue(ADC1);
  143. uint8_t sample =ADC_Result ;
  144. z += 1;
  145. SD_fres = f_write(&fileWAVE, &sample,1, &sav_bytes);
  146.  
  147.  
  148. }
  149. if(c==3){
  150.  
  151. FileSize = z -8;
  152. DataSize = z -44;
  153.  
  154. f_lseek(&fileWAVE, 4);
  155. SD_fres = f_write(&fileWAVE, &FileSize,4, &sav_bytes);
  156. f_lseek(&fileWAVE, 44);
  157. SD_fres = f_write(&fileWAVE, &DataSize,4, &sav_bytes);
  158. GPIO_SetBits(GPIOD, GPIO_Pin_14);
  159. GPIO_SetBits(GPIOD, GPIO_Pin_15);
  160. RGB_Green();
  161. //fclose(fs)
  162. SD_fres = f_close( &fileWAVE );
  163.  
  164.  
  165. GPIO_SetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);
  166.  
  167. }
  168. if(c==4){
  169. GPIO_SetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  170. c=0;
  171. }
  172.  
  173.  
  174.  
  175.  
  176. TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
  177.  
  178. }}}
  179.  
  180.  
  181. int main(void)
  182. {
  183. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  184.  
  185.  
  186. /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
  187. GPIO_InitTypeDef GPIO_InitStructure;
  188. GPIO_InitStructure.GPIO_Pin =
  189. GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
  190. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  191. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  192. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  193. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  194. GPIO_Init(GPIOD, &GPIO_InitStructure);
  195.  
  196.  
  197. SystemInit();
  198. initADC();
  199. initRGB();
  200. initTimH();
  201. fpu_enable();
  202. delay_init( 168 );
  203. SPI_SD_Init();
  204. SD_fres = f_mount( 0, &fatfs );
  205.  
  206.  
  207. GPIO_SetBits(GPIOD,GPIO_Pin_15);
  208.  
  209. for(;;);
  210. }
  211.  
  212.  
  213.  
  214.  
  215.  
  216. void MakeFile()
  217. {
  218. //Set filename
  219. FileCounter++;
  220. char fileName[11];
  221. sprintf(fileName, "File%d.wav", FileCounter);
  222.  
  223. uint32_t empty = 0; //Set empty integer for Sizes
  224. uint32_t ForDat = 16; //Set length of format data
  225. uint16_t Type = 1; //Set it as PCM file
  226. uint16_t Channels = 1; //1 for Mono, 2 for Stereo
  227. uint32_t SampleRate = 16000; //Number of Samples per sec
  228. uint16_t BitsPerSample = 8;
  229. uint32_t ByteRate = SampleRate * Channels * BitsPerSample / 8; //Average rate of bytes
  230. uint16_t BlockAllign = Channels * BitsPerSample / 8; //Bytes per sample including all channels
  231. uint32_t FileSize = 0;
  232. uint32_t DataSize = 0;
  233.  
  234.  
  235.  
  236.  
  237.  
  238. //init constants:
  239. char * RIFF = ("RIFF");
  240. char * WAVE = ("WAVE");
  241. char * fmt = ("fmt ");
  242. char * data = ("data");
  243.  
  244.  
  245.  
  246.  
  247. uint32_t z = 0;
  248.  
  249. SD_fres = f_open( &fileWAVE, fileName, FA_OPEN_ALWAYS | FA_WRITE );
  250. if (1==1)
  251. {
  252.  
  253. //HEADER
  254. SD_fres = f_write(&fileWAVE, ( const void * ) RIFF,4, &sav_bytes);
  255. SD_fres = f_write(&fileWAVE, &empty,4, &sav_bytes);
  256. SD_fres = f_write(&fileWAVE, ( const void * ) WAVE,4, &sav_bytes);
  257. SD_fres = f_write(&fileWAVE, ( const void * ) fmt, 4, &sav_bytes);
  258. SD_fres = f_write(&fileWAVE, &ForDat,4, &sav_bytes);
  259. SD_fres = f_write(&fileWAVE, &Type,2, &sav_bytes);
  260. SD_fres = f_write(&fileWAVE, &Channels,2, &sav_bytes);
  261. SD_fres = f_write(&fileWAVE, &SampleRate,4, &sav_bytes);
  262. SD_fres = f_write(&fileWAVE, &ByteRate,4, &sav_bytes);
  263. SD_fres = f_write(&fileWAVE, &BlockAllign,2, &sav_bytes);
  264. SD_fres = f_write(&fileWAVE, &BitsPerSample,2, &sav_bytes);
  265. SD_fres = f_write(&fileWAVE, ( const void * ) data,4, &sav_bytes);
  266. SD_fres = f_write(&fileWAVE, &empty,4, &sav_bytes);
  267. }
  268. z += 44;
  269.  
  270. //--------------------------------------------------------------------
  271. uint32_t NumOfSampleSets = SampleRate * 10; //Get the number of Sample sets
  272.  
  273. //uint16_t emptysample = 100;
  274. uint32_t d = 0;
  275. while (c >= 1)
  276. {
  277. if(d >= sizeof(rawAudio)/sizeof(rawAudio[0])) {d = 0; printf("%d", (int)d);}
  278. uint8_t sample = rawAudio[d];
  279. d++; z += 1;
  280. SD_fres = f_write(&fileWAVE, &sample,1, &sav_bytes);
  281.  
  282. }
  283.  
  284. //-------------------------------------
  285. FileSize = z -8;
  286. DataSize = z -44;
  287.  
  288. f_lseek(&fileWAVE, 4);
  289. SD_fres = f_write(&fileWAVE, &FileSize,4, &sav_bytes);
  290. f_lseek(&fileWAVE, 44);
  291. SD_fres = f_write(&fileWAVE, &DataSize,4, &sav_bytes);
  292. GPIO_SetBits(GPIOD, GPIO_Pin_14);
  293. GPIO_SetBits(GPIOD, GPIO_Pin_15);
  294.  
  295. //fclose(fs)
  296. SD_fres = f_close( &fileWAVE );
  297.  
  298. }
  299.  
  300. void initRGB(){
  301.  
  302. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  303. GPIO_InitTypeDef GPIO_InitStructure;
  304. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7 | GPIO_Pin_8;
  305. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  306. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  307. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  308. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  309. GPIO_Init(GPIOB, &GPIO_InitStructure);
  310.  
  311. }
  312. void RGB_Red()
  313. {
  314. GPIO_ResetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_5);
  315. GPIO_SetBits(GPIOB, GPIO_Pin_7);
  316. }
  317.  
  318. void RGB_Green()
  319. {
  320. GPIO_ResetBits(GPIOB, GPIO_Pin_7 | GPIO_Pin_5);
  321. GPIO_SetBits(GPIOB, GPIO_Pin_8);
  322. }
  323.  
  324. void RGB_Blue()
  325. {
  326. GPIO_ResetBits(GPIOB, GPIO_Pin_7 | GPIO_Pin_8);
  327. GPIO_SetBits(GPIOB, GPIO_Pin_5);
  328. }
  329.  
  330. void initTimH(){
  331. NVIC_InitTypeDef NVIC_InitStructure;
  332.  
  333. NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
  334.  
  335. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  336.  
  337. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  338.  
  339. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  340.  
  341. NVIC_Init(&NVIC_InitStructure);
  342.  
  343. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  344. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  345.  
  346.  
  347.  
  348.  
  349. EXTI_InitTypeDef EXTI_InitStructure;
  350.  
  351. EXTI_InitStructure.EXTI_Line = GPIO_Pin_0;
  352.  
  353. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  354.  
  355. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  356.  
  357. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  358.  
  359. EXTI_Init(&EXTI_InitStructure);
  360.  
  361. SYSCFG_EXTILineConfig(GPIOA, EXTI_PinSource0);
  362.  
  363.  
  364. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //////////Timer usuwaacy drgania stykow
  365. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  366. TIM_TimeBaseStructure.TIM_Period = 10-1;
  367. TIM_TimeBaseStructure.TIM_Prescaler = (100-1);;
  368. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  369. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  370. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  371.  
  372. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  373. NVIC_InitTypeDef NVIC_InitStructure1;
  374. // numer przerwania
  375. NVIC_InitStructure1.NVIC_IRQChannel = TIM3_IRQn;
  376. // priorytet główny
  377. NVIC_InitStructure1.NVIC_IRQChannelPreemptionPriority =0;
  378. // subpriorytet
  379. NVIC_InitStructure1.NVIC_IRQChannelSubPriority = 0;
  380. // uruchom dany kanał
  381. NVIC_InitStructure1.NVIC_IRQChannelCmd = ENABLE;
  382. // zapisz wypełnioną strukturę do rejestrów
  383. NVIC_Init(&NVIC_InitStructure1);
  384.  
  385. TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  386.  
  387. TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  388. TIM_Cmd(TIM3, DISABLE);
  389.  
  390.  
  391.  
  392.  
  393. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //////////timer 16000hz
  394. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure4;
  395. TIM_TimeBaseStructure4.TIM_Period = (10-1);
  396. TIM_TimeBaseStructure4.TIM_Prescaler = (525-1);
  397. TIM_TimeBaseStructure4.TIM_ClockDivision = TIM_CKD_DIV1;
  398. TIM_TimeBaseStructure4.TIM_CounterMode = TIM_CounterMode_Up;
  399. TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure4);
  400.  
  401. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  402. NVIC_InitTypeDef NVIC_InitStructure4;
  403. // numer przerwania
  404. NVIC_InitStructure4.NVIC_IRQChannel = TIM4_IRQn;
  405. // priorytet główny
  406. NVIC_InitStructure4.NVIC_IRQChannelPreemptionPriority =10;
  407. // subpriorytet
  408. NVIC_InitStructure4.NVIC_IRQChannelSubPriority =10;
  409. // uruchom dany kanał
  410. NVIC_InitStructure4.NVIC_IRQChannelCmd = ENABLE;
  411. // zapisz wypełnioną strukturę do rejestrów
  412. NVIC_Init(&NVIC_InitStructure4);
  413.  
  414. TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
  415.  
  416. TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
  417. TIM_Cmd(TIM4, ENABLE);
  418. }
  419.  
  420. void initADC(){
  421. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE); // zegar dla portu GPIO z którego wykorzystany zostanie pin jako wejście ADC (PA1)
  422. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // zegar dla modułu ADC1
  423.  
  424. GPIO_InitTypeDef GPIO_InitStructure;
  425. //inicjalizacja wejścia ADC
  426. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  427. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  428. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  429. GPIO_Init(GPIOA, &GPIO_InitStructure);
  430.  
  431. ADC_CommonInitTypeDef ADC_CommonInitStructure;
  432. // niezależny tryb pracy przetworników
  433. ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  434. // zegar główny podzielony przez 2
  435. ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  436. // opcja istotna tylko dla trybu multi ADC
  437. ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  438. // czas przerwy pomiędzy kolejnymi konwersjami
  439. ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  440. ADC_CommonInit(&ADC_CommonInitStructure);
  441.  
  442. ADC_InitTypeDef ADC_InitStructure;
  443. //ustawienie rozdzielczości przetwornika na maksymalną (12 bitów)
  444. ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b;
  445. //wyłączenie trybu skanowania (odczytywać będziemy jedno wejście ADC
  446. //w trybie skanowania automatycznie wykonywana jest konwersja na wielu //wejściach/kanałach)
  447. ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  448. //włączenie ciągłego trybu pracy
  449. ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  450. //wyłączenie zewnętrznego wyzwalania
  451. //konwersja może być wyzwalana timerem, stanem wejścia itd. (szczegóły w //dokumentacji)
  452. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  453.  
  454. ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  455. //wartość binarna wyniku będzie podawana z wyrównaniem do prawej
  456. //funkcja do odczytu stanu przetwornika ADC zwraca wartość 16-bitową
  457. //dla przykładu, wartość 0xFF wyrównana w prawo to 0x00FF, w lewo 0x0FF0
  458. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  459. //liczba konwersji równa 1, bo 1 kanał
  460. ADC_InitStructure.ADC_NbrOfConversion = 1;
  461. // zapisz wypełnioną strukturę do rejestrów przetwornika numer 1
  462. ADC_Init(ADC1, &ADC_InitStructure);
  463.  
  464. ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_84Cycles);
  465.  
  466. ADC_Cmd(ADC1, ENABLE);
  467.  
  468. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement