Świetnie, że przerwania działają! Teraz przejdźmy do odtwarzania plików .wav z karty SD. To wymaga kilku modyfikacji w kodzie, głównie związanych z:
- Inicjalizacją karty SD i systemu plików
- Znajdowaniem pierwszego pliku .wav
- Strumieniowym odczytywaniem danych z pliku (zamiast z tablicy w pamięci)
- Zarządzaniem buforami do płynnego odtwarzania
Oto jak można to zaimplementować:
Krok 1: Dodanie nagłówków i zmiennych globalnych
#include "mcu_regs.h"
#include "type.h"
#include "uart.h"
#include "stdio.h"
#include "timer32.h"
#include "gpio.h"
// Dodaj nagłówki dla obsługi karty SD i systemu plików
#include "ff.h" // FatFs - system plików FAT
#include "diskio.h" // Interfejs dysku dla FatFs
// Jeśli używasz innych bibliotek SD, dostosuj nagłówki
// Zmienne globalne do zarządzania odtwarzaniem
volatile uint32_t g_audio_idx = 0;
volatile uint8_t g_is_playing = 0;
volatile uint32_t g_pwm_cycles_count = 0;
const uint32_t PWM_CYCLES_PER_SAMPLE = 32;
// Nowe zmienne dla obsługi plików SD
FATFS g_fatfs; // Obiekt systemu plików
FIL g_audio_file; // Obiekt pliku audio
FRESULT g_sd_result; // Wynik operacji SD
// Bufory do strumieniowego odczytywania
#define AUDIO_BUFFER_SIZE 512 // Rozmiar bufora (dostosuj do potrzeb)
uint8_t g_audio_buffer[AUDIO_BUFFER_SIZE];
volatile uint32_t g_buffer_read_pos = 0; // Pozycja odczytu w buforze
volatile uint32_t g_buffer_data_size = 0; // Ilość ważnych danych w buforze
volatile uint8_t g_buffer_needs_refill = 1; // Flaga: bufor wymaga uzupełnienia
// Informacje o pliku WAV
uint32_t g_sample_rate = 0;
uint32_t g_audio_data_size = 0;
uint32_t g_samples_played = 0;
Krok 2: Funkcje pomocnicze do obsługi karty SD
// Funkcja inicjalizacji karty SD
uint8_t init_sd_card(void) {
UARTSendString((uint8_t*)"Initializing SD card...\r\n");
// Montowanie systemu plików
g_sd_result = f_mount(&g_fatfs, "", 1); // "" oznacza domyślny dysk
if (g_sd_result != FR_OK) {
UARTSendString((uint8_t*)"SD card mount failed\r\n");
return 0;
}
UARTSendString((uint8_t*)"SD card initialized successfully\r\n");
return 1;
}
// Funkcja znajdowania pierwszego pliku .wav
uint8_t find_first_wav_file(char* filename, uint32_t max_len) {
DIR dir;
FILINFO fno;
UARTSendString((uint8_t*)"Searching for .wav files...\r\n");
// Otwórz katalog główny
g_sd_result = f_opendir(&dir, "/");
if (g_sd_result != FR_OK) {
UARTSendString((uint8_t*)"Failed to open root directory\r\n");
return 0;
}
// Przeszukaj pliki
while (1) {
g_sd_result = f_readdir(&dir, &fno);
if (g_sd_result != FR_OK || fno.fname[0] == 0) {
break; // Błąd lub koniec katalogu
}
// Sprawdź rozszerzenie pliku
char* ext = strrchr(fno.fname, '.');
if (ext && (strcasecmp(ext, ".wav") == 0 || strcasecmp(ext, ".WAV") == 0)) {
// Znaleziono plik .wav
if (strlen(fno.fname) < max_len) {
strcpy(filename, fno.fname);
f_closedir(&dir);
UARTSendString((uint8_t*)"Found WAV file: ");
UARTSendString((uint8_t*)filename);
UARTSendString((uint8_t*)"\r\n");
return 1;
}
}
}
f_closedir(&dir);
UARTSendString((uint8_t*)"No .wav files found\r\n");
return 0;
}
// Funkcja parsowania nagłówka WAV z pliku
uint8_t parse_wav_header(void) {
uint8_t header[44]; // Standardowy nagłówek WAV ma 44 bajty
UINT bytes_read;
// Odczytaj nagłówek
g_sd_result = f_read(&g_audio_file, header, 44, &bytes_read);
if (g_sd_result != FR_OK || bytes_read != 44) {
UARTSendString((uint8_t*)"Failed to read WAV header\r\n");
return 0;
}
// Sprawdź RIFF
if (header[0] != 'R' || header[1] != 'I' ||
header[2] != 'F' || header[3] != 'F') {
UARTSendString((uint8_t*)"Invalid RIFF header\r\n");
return 0;
}
// Sprawdź WAVE
if (header[8] != 'W' || header[9] != 'A' ||
header[10] != 'V' || header[11] != 'E') {
UARTSendString((uint8_t*)"Invalid WAVE header\r\n");
return 0;
}
// Sprawdź fmt
if (header[12] != 'f' || header[13] != 'm' ||
header[14] != 't' || header[15] != ' ') {
UARTSendString((uint8_t*)"Invalid fmt header\r\n");
return 0;
}
// Odczytaj częstotliwość próbkowania (bajty 24-27)
g_sample_rate = header[24] | (header[25] << 8) |
(header[26] << 16) | (header[27] << 24);
if (g_sample_rate != 8000) {
UARTSendString((uint8_t*)"Only 8kHz sample rate supported\r\n");
return 0;
}
// Sprawdź data chunk
if (header[36] != 'd' || header[37] != 'a' ||
header[38] != 't' || header[39] != 'a') {
UARTSendString((uint8_t*)"Invalid data header\r\n");
return 0;
}
// Odczytaj rozmiar danych audio (bajty 40-43)
g_audio_data_size = header[40] | (header[41] << 8) |
(header[42] << 16) | (header[43] << 24);
UARTSendString((uint8_t*)"WAV header parsed successfully\r\n");
return 1;
}
// Funkcja uzupełniania bufora danymi z pliku
uint8_t refill_audio_buffer(void) {
UINT bytes_read;
if (g_samples_played >= g_audio_data_size) {
// Koniec pliku
return 0;
}
// Oblicz ile bajtów można jeszcze odczytać
uint32_t bytes_to_read = AUDIO_BUFFER_SIZE;
if ((g_samples_played + bytes_to_read) > g_audio_data_size) {
bytes_to_read = g_audio_data_size - g_samples_played;
}
g_sd_result = f_read(&g_audio_file, g_audio_buffer, bytes_to_read, &bytes_read);
if (g_sd_result != FR_OK) {
UARTSendString((uint8_t*)"Failed to read audio data\r\n");
return 0;
}
g_buffer_data_size = bytes_read;
g_buffer_read_pos = 0;
g_buffer_needs_refill = 0;
return 1;
}
Krok 3: Zmodyfikowana procedura obsługi przerwania
void TIMER_32_1_IRQHandler(void) {
if (LPC_TMR32B1->IR & (1 << 3)) {
LPC_TMR32B1->IR = (1 << 3);
if (g_is_playing) {
g_pwm_cycles_count++;
if (g_pwm_cycles_count >= PWM_CYCLES_PER_SAMPLE) {
g_pwm_cycles_count = 0;
// Sprawdź, czy bufor wymaga uzupełnienia
if (g_buffer_needs_refill) {
// Sygnalizuj głównej pętli, że bufor wymaga uzupełnienia
// W ISR nie wykonujemy operacji na karcie SD (zbyt wolne)
g_is_playing = 0; // Tymczasowo zatrzymaj odtwarzanie
return;
}
// Sprawdź, czy są dostępne dane w buforze
if (g_buffer_read_pos < g_buffer_data_size) {
LPC_TMR32B1->MR1 = g_audio_buffer[g_buffer_read_pos] + 30;
g_buffer_read_pos++;
g_samples_played++;
// Sprawdź, czy bufor jest prawie pusty
if (g_buffer_read_pos >= g_buffer_data_size) {
if (g_samples_played < g_audio_data_size) {
g_buffer_needs_refill = 1;
} else {
// Koniec pliku
g_is_playing = 0;
LPC_TMR32B1->MR1 = 127; // Wartość neutralna
}
}
} else {
// Brak danych - zatrzymaj odtwarzanie
g_is_playing = 0;
LPC_TMR32B1->MR1 = 127;
}
}
}
}
}
Krok 4: Zmodyfikowana funkcja main()
int main(void) {
char wav_filename[64];
GPIOInit();
UARTInit(115200);
UARTSendString((uint8_t*)"Audio Player - SD Card WAV\r\n");
// Inicjalizacja GPIO i timera (bez zmian)
GPIOSetDir(PORT1, 9, 1);
GPIOSetDir(PORT1, 10, 1);
GPIOSetDir(PORT3, 0, 1);
GPIOSetDir(PORT3, 1, 1);
GPIOSetDir(PORT3, 2, 1);
GPIOSetDir(PORT1, 2, 1);
LPC_IOCON->JTAG_nTRST_PIO1_2 = (LPC_IOCON->JTAG_nTRST_PIO1_2 & ~0x7) | 0x3;
GPIOSetValue(PORT3, 0, 0);
GPIOSetValue(PORT3, 1, 0);
GPIOSetValue(PORT3, 2, 0);
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<10);
// Konfiguracja timera (bez zmian)
LPC_TMR32B1->TCR = 2;
LPC_TMR32B1->TCR = 0;
LPC_TMR32B1->PR = 0;
LPC_TMR32B1->MCR = (1<<10)|(1<<9);
LPC_TMR32B1->MR3 = 286;
LPC_TMR32B1->MR1 = 127;
LPC_TMR32B1->PWMC = (1<<1);
LPC_TMR32B1->IR = 0xf;
NVIC_EnableIRQ(TIMER_32_1_IRQn);
// Inicjalizacja karty SD
if (!init_sd_card()) {
UARTSendString((uint8_t*)"SD card initialization failed\r\n");
return -1;
}
LPC_TMR32B1->TCR = 1; // Uruchom timer
while(1) {
// Znajdź pierwszy plik .wav
if (!find_first_wav_file(wav_filename, sizeof(wav_filename))) {
UARTSendString((uint8_t*)"No WAV files found. Waiting...\r\n");
for(volatile uint32_t i = 0; i < 0x500000; i++) __NOP();
continue;
}
// Otwórz plik
g_sd_result = f_open(&g_audio_file, wav_filename, FA_READ);
if (g_sd_result != FR_OK) {
UARTSendString((uint8_t*)"Failed to open WAV file\r\n");
for(volatile uint32_t i = 0; i < 0x500000; i++) __NOP();
continue;
}
// Parsuj nagłówek WAV
if (!parse_wav_header()) {
f_close(&g_audio_file);
UARTSendString((uint8_t*)"Invalid WAV file format\r\n");
for(volatile uint32_t i = 0; i < 0x500000; i++) __NOP();
continue;
}
// Przygotuj do odtwarzania
g_samples_played = 0;
g_buffer_read_pos = 0;
g_buffer_data_size = 0;
g_buffer_needs_refill = 1;
g_pwm_cycles_count = 0;
UARTSendString((uint8_t*)"Starting playback...\r\n");
// Wczytaj pierwszy bufor
if (!refill_audio_buffer()) {
f_close(&g_audio_file);
UARTSendString((uint8_t*)"Failed to read initial audio data\r\n");
continue;
}
g_is_playing = 1;
// Pętla odtwarzania
while (g_samples_played < g_audio