be_chti/projet_keil/Src/principal.c
2020-05-29 16:17:49 +02:00

139 lines
3.6 KiB
C

#include "gassp72.h"
#include "stdlib.h"
// **********************
// * GESTION DU SON *
// **********************
typedef struct {
int position; // index courant dans le tableau d'echantillons
int taille; // nombre d'echantillons de l'enregistrement
short int *son; // adresse de base du tableau d'echantillons en ROM
int resolution; // pleine echelle du modulateur PWM
int Tech_en_Tck; // periode d'ech. audio en periodes d'horloge CPU
} type_etat;
// Variables définies en asm
extern short Son;
extern int LongueurSon;
extern int PeriodeSonMicroSec;
type_etat etat;
int Periode_PWM_en_Tck = 1638;
int Periode_ech_en_Tck = 6552;
void timer_callback(void);
// ************************
// * GESTION DU SCORE *
// ************************
// Constantes
#define NB_JOUEURS 6
#define TYPE_SIGNAL 0x33
const int SYSTICK_PER = 5*72000;
const int N = 64;
const int M2TIR = 985661;
// Fe = 320 kHz donc pasFreq = 5 kHz
// F1 = 85 kHz -> k = 17
// F2 = 90 kHz -> k = 18
// F3 = 95 kHz -> k = 19
// F4 = 100 kHz -> k = 20
// F5 = 115 kHz -> k = 23
// F6 = 120 kHz -> k = 24
const int kFreq[NB_JOUEURS] = {17, 18, 19, 20, 23, 24};
int dft(unsigned short *sig, int k);
unsigned short *bufferDMA;
int counters[NB_JOUEURS];
int debug_result[NB_JOUEURS];
int scores[NB_JOUEURS];
int time = 0;
void incrementScore(int playerID) {
// incrémentation du score
++scores[playerID];
// reset de la position pour jouer le son depuis le début
etat.position = 0;
}
void callbackTimer(void) {
// Démarrage DMA pour 64 points
Start_DMA1(64);
Wait_On_End_Of_DMA1();
Stop_DMA1;
for (int i = 0; i < NB_JOUEURS; ++i) {
int k = kFreq[i];
debug_result[i] = dft(bufferDMA, k);
if (debug_result[i] > M2TIR) {
++counters[i];
if (counters[i] == 3) {
incrementScore(i);
}
} else {
counters[i] = 0;
}
}
time += 5;
}
int main(void) {
bufferDMA = malloc(N * sizeof(short));
for (int i = 0; i < NB_JOUEURS; ++i) {
counters[i] = 0;
scores[i] = 0;
}
// activation de la PLL qui multiplie la fréquence du quartz par 9
CLOCK_Configure();
// PA2 (ADC voie 2) = entrée analog
GPIO_Configure(GPIOA, 2, INPUT, ANALOG);
// GESTION DU SON
// config port PB0 pour être utilisé par TIM3-CH3
GPIO_Configure(GPIOB, 0, OUTPUT, ALT_PPULL);
// config TIM3-CH3 en mode PWM
etat.resolution = PWM_Init_ff(TIM3, 3, Periode_PWM_en_Tck);
etat.taille = LongueurSon;
etat.son = &Son;
etat.position = LongueurSon;
// initialisation du timer 4
// Periode_en_Tck doit fournir la durée entre interruptions,
// exprimée en périodes Tck de l'horloge principale du STM32 (72 MHz)
Timer_1234_Init_ff(TIM4, Periode_ech_en_Tck);
// enregistrement de la fonction de traitement de l'interruption timer
// ici le 2 est la priorité, timer_callback est l'adresse de cette fonction, a créér en asm,
// cette fonction doit être conforme à l'AAPCS
Active_IT_Debordement_Timer(TIM4, 2, timer_callback);
// lancement du timer pour jouer le son
Run_Timer(TIM4);
// activation ADC, sampling time 1us
Init_TimingADC_ActiveADC_ff(ADC1, TYPE_SIGNAL);
Single_Channel_ADC(ADC1, 2);
// Déclenchement ADC par timer2, periode (72MHz/320kHz)ticks
Init_Conversion_On_Trig_Timer_ff(ADC1, TIM2_CC2, 225);
// Config DMA pour utilisation du buffer dma_buf (a créér)
Init_ADC1_DMA1(0, bufferDMA);
// Config Timer, période exprimée en périodes horloge CPU (72 MHz)
Systick_Period_ff(SYSTICK_PER);
// enregistrement de la fonction de traitement de l'interruption timer
// ici le 3 est la priorité, callbackTimer est l'adresse de cette fonction, a créér en C
Systick_Prio_IT(3, callbackTimer);
SysTick_On;
SysTick_Enable_IT;
while(1) {}
}