4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
33 * \author Stefano Federico <aleph@develer.com>
35 * \brief Empty project.
37 * This is a minimalist project, it just initializes the hardware of the
38 * supported board and proposes an empty main.
41 #include "hw/hw_led.h"
43 #include "cfg/cfg_i2s.h"
45 #include <cfg/debug.h>
48 #include <cpu/byteorder.h>
51 #include <io/kfile_block.h>
56 #include <drv/eeprom.h>
57 #include <drv/timer.h>
58 #include <drv/lcd_hx8347.h>
60 #include <drv/wm8731.h>
61 #include <drv/dmac_sam3.h>
71 * Codec has 7-bit address, the eighth is the R/W bit, so we
72 * write the codec address with one bit shifted left
74 #define CODEC_ADDR 0x36
85 uint32_t subchunk1_size;
86 uint16_t audio_format;
87 uint16_t num_channels;
91 uint16_t bits_per_sample;
97 //#define FILE_NAME "input0.wav"
98 //#define FILE_NAME "due.wav"
99 //#define FILE_NAME "sample.wav"
100 //#define FILE_NAME "testaa"
101 #define FILE_NAME "outfile.wav"
102 #define ACQ_FILE_NAME "acq.wav"
106 // SD fat filesystem context
113 static Wm8731 wm8731_ctx;
115 static void init(void)
119 kprintf("sam3x %s: %d times\n", VERS_HOST, VERS_BUILD);
126 i2c_init(&i2c, I2C_BITBANG0, CONFIG_I2C_FREQ);
127 i2s_init(&i2s, SSC0);
129 wm8731_init(&wm8731_ctx, &i2c, CODEC_ADDR);
130 wm8731_setVolume(&wm8731_ctx, WM8731_HEADPHONE, 0);
135 static void codec(struct I2s *i2s, void *_buf, size_t len)
137 count += kfile_read(&log_file.fd, _buf, len);
138 if (count >= log_file.fat_file.fsize - sizeof(WavHdr))
140 kprintf("stop %d\n", count);
146 static int wav_check(KFile *fd)
151 if (kfile_read(fd, &header, sizeof(header)) != sizeof(header))
153 kputs("Error reading wave file header\n");
157 if (strncmp(header.chunk_id, "RIFF", 4))
159 kputs("RIFF tag not found\n");
163 if (strncmp(header.format, "WAVE", 4))
165 kputs("WAVE tag not found\n");
169 if (le16_to_cpu(header.audio_format) != 1)
171 kprintf("Audio format not valid, found [%d]\n", le16_to_cpu(header.audio_format));
175 if (le16_to_cpu(header.num_channels) != 2)
177 kprintf("Channels number not valid, found [%d]\n", le16_to_cpu(header.num_channels));
182 if (le32_to_cpu(header.sample_rate) != 44100)
184 kprintf("Sample rate not valid, found [%ld]\n", le32_to_cpu(header.sample_rate));
188 if (le16_to_cpu(header.bits_per_sample) != 16)
190 kprintf("Bits per sample not valid, found [%d]\n", le16_to_cpu(header.bits_per_sample));
205 if (SD_CARD_PRESENT())
209 bool sd_ok = sd_init(&sd, NULL, 0);
214 kprintf("Mount FAT filesystem.\n");
215 result = f_mount(0, &fs);
218 kprintf("Mounting FAT volumes error[%d]\n", result);
224 result = fatfile_open(&log_file, FILE_NAME, FA_OPEN_EXISTING | FA_READ);
227 kprintf("Opened log file '%s' size %ld\n", FILE_NAME, log_file.fat_file.fsize);
228 wm8731_setVolume(&wm8731_ctx, WM8731_HEADPHONE, adc_read(1));
229 if (wav_check(&log_file.fd) >= 0)
231 kputs("Wav file play..\n");
232 i2s_dmaStartTxStreaming(&i2s, tmp, sizeof(tmp), sizeof(tmp) / 4, codec);
235 wm8731_setVolume(&wm8731_ctx, WM8731_HEADPHONE, 0);
237 // Flush data and close the files.
238 kfile_flush(&log_file.fd);
239 kfile_close(&log_file.fd);
243 kprintf("Unable to open file: '%s' error[%d]\n", FILE_NAME, result);
246 //Unmount always to prevent accidental sd remove.
258 kputs("No card insert..\n");