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
76 // SD fat filesystem context
83 //static Wm8731 wm8731_ctx;
85 static void init(void)
89 kprintf("sam3x %s: %d times\n", VERS_HOST, VERS_BUILD);
95 i2c_init(&i2c, I2C_BITBANG0, CONFIG_I2C_FREQ);
99 // wm8731_init(&wm8731_ctx, &i2c, CODEC_ADDR);
100 // wm8731_setVolume(&wm8731_ctx, WM8731_HEADPHONE, (uint8_t)ADC_RANGECONV(adc_read(1), 0, 100));
103 typedef struct WavHdr
109 char subchunk1_id[4];
110 uint32_t subchunk1_size;
111 uint16_t audio_format;
112 uint16_t num_channels;
113 uint32_t sample_rate;
115 uint16_t block_align;
116 uint16_t bits_per_sample;
118 uint8_t subchunk2[8];
122 //#define FILE_NAME "input0.wav"
123 #define FILE_NAME "sample.wav"
124 //#define FILE_NAME "testaa"
125 #define ACQ_FILE_NAME "acq.wav"
130 static void codec(struct I2s *i2s, void *_buf, size_t len)
132 count += kfile_read(&log_file.fd, _buf, len);
133 if (count >= log_file.fat_file.fsize - sizeof(WavHdr))
135 kprintf("stop %d\n", count);
142 static int wav_check(KFile *fd)
147 if (kfile_read(fd, &header, sizeof(header)) != sizeof(header))
149 kputs("Error reading wave file header\n");
153 if (strncmp(header.chunk_id, "RIFF", 4))
155 kputs("RIFF tag not found\n");
159 if (strncmp(header.format, "WAVE", 4))
161 kputs("WAVE tag not found\n");
165 if (le16_to_cpu(header.audio_format) != 1)
167 kprintf("Audio format not valid, found [%d]\n", le16_to_cpu(header.audio_format));
171 if (le16_to_cpu(header.num_channels) != 2)
173 kprintf("Channels number not valid, found [%d]\n", le16_to_cpu(header.num_channels));
178 if (le32_to_cpu(header.sample_rate) != 48000)
180 kprintf("Sample rate not valid, found [%ld]\n", le32_to_cpu(header.sample_rate));
184 if (le16_to_cpu(header.bits_per_sample) != 16)
186 kprintf("Bits per sample not valid, found [%d]\n", le16_to_cpu(header.bits_per_sample));
195 #include <drv/eeprom.h>
201 eeprom_init(&eep, &i2c, EEPROM_24XX512, 0x51, false);
206 if (SD_CARD_PRESENT())
209 memset(uno, 0xcc, eep.blk.blk_size);
210 // kblock_write(&eep.blk, 0, uno, 0, eep.blk.blk_size);
212 memset(tre, 0xaa, eep.blk.blk_size);
213 kblock_read(&eep.blk, 0, uno, 0, eep.blk.blk_size);
215 kprintf("blk[%d]\n", eep.blk.blk_size);
216 if (!memcmp(uno, tre, eep.blk.blk_size))
219 for (size_t i = 0; i < eep.blk.blk_size; i++)
220 kprintf("%x ", tre[i]);
225 for (size_t i = 0; i < eep.blk.blk_size; i++)
226 kprintf("%x ", tre[i]);
229 uint16_t vol = ADC_RANGECONV(adc_read(1), 0, 100);
233 wm8731_setVolume(&wm8731_ctx, WM8731_HEADPHONE, (uint8_t)vol);
238 bool sd_ok = sd_init(&sd, NULL, 0);
241 memset(due, 0xbb, sizeof(due));
244 kprintf("Mount FAT filesystem.\n");
245 result = f_mount(0, &fs);
248 kprintf("Mounting FAT volumes error[%d]\n", result);
254 result = fatfile_open(&log_file, FILE_NAME, FA_OPEN_EXISTING | FA_READ);
257 kprintf("Opened log file '%s' size %ld\n", FILE_NAME, log_file.fat_file.fsize);
258 if (wav_check(&log_file.fd) >= 0)
260 //int len = kfile_read(&log_file.fd, due, 1024);
261 //i2s_dmaTxBuffer(&i2s, due, len);
264 kprintf("len %d\n", len);
265 for (size_t i=0; i < sizeof(due); i++)
266 kprintf("%x ", due[i]);
270 //count = log_file.fat_file.fsize - sizeof(WavHdr);
271 //count = log_file.fat_file.fsize;
274 int len = kfile_read(&log_file.fd, due, sizeof(due));
275 i2s_dmaTxBuffer(&i2s, due, len);
277 //for (int i = 0; i < len / 2; i++)
278 //i2s_write(&i2s, (due[i]));
282 i2s_dmaStartTxStreaming(&i2s, due, sizeof(due), 1024, codec);
285 // Flush data and close the files.
286 kfile_flush(&log_file.fd);
287 kfile_close(&log_file.fd);
291 kprintf("Unable to open file: '%s' error[%d]\n", FILE_NAME, result);
294 //Unmount always to prevent accidental sd remove.
305 kputs("No card insert..\n");