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 * \brief Atmel SAM3X-EK testcase
35 * \author Stefano Fedrigo <aleph@develer.com>
40 #include "hw/hw_led.h"
41 #include "hw/hw_lcd.h"
42 #include "hw/hw_adc.h"
43 #include "hw/hw_sdram.h"
46 #include "cfg/cfg_i2s.h"
48 #include <cfg/debug.h>
51 #include <cpu/byteorder.h>
57 #include <drv/timer.h>
58 #include <drv/lcd_hx8347.h>
60 #include <drv/wm8731.h>
61 #include <drv/dmac_sam3.h>
68 #include <icons/logo.h>
71 #include <io/kfile_block.h>
73 #include <kern/signal.h>
74 #include <kern/proc.h>
81 #include <struct/list.h>
90 #define KEY_MASK (K_LEFT | K_RIGHT)
93 * Codec has 7-bit address, the eighth is the R/W bit, so we
94 * write the codec address with one bit shifted left
96 #define CODEC_ADDR 0x36
99 static PROC_DEFINE_STACK(play_proc_stack, 1024);
100 static PROC_DEFINE_STACK(rec_proc_stack, 1024);
101 MsgPort proc_play_inPort;
102 MsgPort proc_rec_inPort;
104 #define MAX_ITEM_NODES 30
105 #define MAX_ITEMS_ROW 15
106 #define NEXT_ITEM_COL 10
108 typedef struct FileItemNode
114 typedef struct AudioMsg
120 FileItemNode item_nodes[MAX_ITEM_NODES];
124 // SD fat filesystem context
132 static Wm8731 wm8731_ctx;
134 static uint8_t raster[RAST_SIZE(LCD_WIDTH, LCD_HEIGHT)];
135 static Bitmap lcd_bitmap;
136 extern Font font_gohu;
137 static int lcd_brightness = LCD_BACKLIGHT_MAX;
138 static uint16_t headphone_volume = 90;
139 static size_t played_size = 0;
140 static bool is_playing = false;
141 static bool is_recording = false;
142 static int recorderd_file_idx;
144 static void codec_play(struct I2s *i2s, void *_buf, size_t len)
146 played_size += kfile_read(&play_file.fd, _buf, len);
147 if (played_size >= play_file.fat_file.fsize - sizeof(WavHdr))
149 kprintf("stop %d\n", played_size);
156 static void codec_rec(struct I2s *i2s, void *_buf, size_t len)
159 kfile_write(&rec_file.fd, _buf, len);
162 static void NORETURN play_proc(void)
166 event_wait(&proc_play_inPort.event);
168 play = (AudioMsg *)msg_get(&proc_play_inPort);
169 if (play && SD_CARD_PRESENT())
173 bool sd_ok = sd_init(&sd, NULL, 0);
178 kprintf("Mount FAT filesystem.\n");
180 result = f_mount(0, &fs);
183 kprintf("Mounting FAT volumes error[%d]\n", result);
189 result = fatfile_open(&play_file, play->file_name, FA_OPEN_EXISTING | FA_READ);
192 kprintf("Open file: %s size %ld\n", play->file_name, play_file.fat_file.fsize);
194 kfile_read(&play_file.fd, &wav, sizeof(WavHdr));
195 if (wav_checkHdr(&wav, 1, CONFIG_CHANNEL_NUM, CONFIG_SAMPLE_FREQ, CONFIG_WORD_BIT_SIZE) != -1)
197 kputs("Wav file play..\n");
199 wm8731_setVolume(&wm8731_ctx, WM8731_HEADPHONE, headphone_volume);
201 i2s_dmaStartTxStreaming(&i2s, tmp, sizeof(tmp), sizeof(tmp) / 4, codec_play);
203 wm8731_setVolume(&wm8731_ctx, WM8731_HEADPHONE, 0);
206 // Flush data and close the files.
207 kfile_flush(&play_file.fd);
208 kfile_close(&play_file.fd);
212 kprintf("Unable to open file: '%s' error[%d]\n", play->file_name, result);
222 static void NORETURN rec_proc(void)
226 event_wait(&proc_rec_inPort.event);
228 rec = (AudioMsg *)msg_get(&proc_rec_inPort);
229 if (rec && SD_CARD_PRESENT())
233 bool sd_ok = sd_init(&sd, NULL, 0);
238 kprintf("Mount FAT filesystem.\n");
240 result = f_mount(0, &fs);
243 kprintf("Mounting FAT volumes error[%d]\n", result);
249 result = fatfile_open(&rec_file, rec->file_name, FA_CREATE_ALWAYS | FA_WRITE);
252 kprintf("Open file: %s size %ld\n", rec->file_name, rec_file.fat_file.fsize);
254 wav_writeHdr(&wav, 1, CONFIG_CHANNEL_NUM, CONFIG_SAMPLE_FREQ, CONFIG_WORD_BIT_SIZE);
255 kfile_write(&rec_file.fd, &wav, sizeof(WavHdr));
256 kputs("Rec Wav file..\n");
258 i2s_dmaStartRxStreaming(&i2s, tmp, sizeof(tmp), sizeof(tmp) / 4, codec_rec);
260 // Flush data and close the files.
261 kfile_flush(&rec_file.fd);
262 kfile_close(&rec_file.fd);
266 kprintf("Unable to open file: '%s' error[%d]\n", rec->file_name, result);
276 INLINE void start_play(char *file_name)
279 memcpy(play_msg.file_name, file_name, sizeof(play_msg.file_name));
280 msg_put(&proc_play_inPort, &play_msg.msg);
283 INLINE void stop_play(void)
291 INLINE void start_rec(char *file_name)
294 memcpy(rec_msg.file_name, file_name, sizeof(rec_msg.file_name));
295 msg_put(&proc_rec_inPort, &rec_msg.msg);
298 INLINE void stop_rec(void)
301 is_recording = false;
304 INLINE FileItemNode *select_item(Bitmap *bm, List *file_list, int select_idx)
307 FileItemNode *select_node;
312 select_node = (FileItemNode *)LIST_HEAD(file_list);
313 FOREACH_NODE(item, file_list)
315 if (row > MAX_ITEMS_ROW)
321 text_style(bm, 0, STYLEF_MASK);
322 if (select_idx <= MAX_ITEMS_ROW)
324 if (row == select_idx && col == 0)
326 text_style(bm, STYLEF_INVERT, STYLEF_INVERT);
332 if (row == (select_idx - MAX_ITEMS_ROW) && col == NEXT_ITEM_COL)
334 text_style(bm, STYLEF_INVERT, STYLEF_INVERT);
339 text_xprintf(bm, row, col, TEXT_NORMAL, "%s", item->file_name);
343 lcd_hx8347_blitBitmap(bm);
348 static void play_menu(Bitmap *bm)
351 LIST_INIT(&file_list);
352 int file_list_size = 0;
355 memcpy (&item_nodes[0].file_name, "<- Return..", sizeof(item_nodes[0].file_name));
356 ADDTAIL(&file_list, &item_nodes[0].n);
358 if (SD_CARD_PRESENT())
362 bool sd_ok = sd_init(&sd, NULL, 0);
367 kprintf("Mount FAT filesystem.\n");
368 result = f_mount(0, &fs);
371 kprintf("Mounting FAT volumes error[%d]\n", result);
381 /* Open the directory */
382 result = f_opendir(&dir, "/");
385 // First element is reserved for "return" label
386 for (int i = 1;; i++)
388 /* Read a directory item */
389 result = f_readdir(&dir, &fno);
390 if (result != FR_OK || fno.fname[0] == 0)
391 break; /* Break on error or end of dir */
392 if (fno.fname[0] == '.')
393 continue; /* Ignore dot entry */
394 if (fno.fattrib & AM_DIR)
398 if (i < MAX_ITEM_NODES)
400 memcpy (&item_nodes[i].file_name, fno.fname, sizeof(item_nodes[i].file_name));
401 ADDTAIL(&file_list, &item_nodes[i].n);
403 kprintf("%s\n", item_nodes[i].file_name);
407 kputs("No enought space to store items in list\n");
420 kputs("No card insert..\n");
421 text_xprintf(bm, 5, 0, TEXT_CENTER | TEXT_FILL, "%s", "No card insert..");
425 FileItemNode *selected_node = NULL;
426 select_item(bm, &file_list, idx);
429 keymask_t key = kbd_peek();
433 if (idx > file_list_size)
436 selected_node = select_item(bm, &file_list, idx);
444 start_play(selected_node->file_name);
454 static void rec_menu(Bitmap *bm)
457 text_style(bm, STYLEF_BOLD | STYLEF_UNDERLINE, STYLEF_BOLD | STYLEF_UNDERLINE);
458 text_xprintf(bm, 0, 0, TEXT_CENTER | TEXT_FILL, "Microphone recorder.");
459 text_style(bm, 0, STYLEF_MASK);
460 text_xprintf(bm, 2, 0, TEXT_NORMAL, "Press RIGHT button to start recording");
461 text_xprintf(bm, 3, 0, TEXT_NORMAL, "and to stop it re-press RIGHT button.");
462 lcd_hx8347_blitBitmap(bm);
467 keymask_t key = kbd_peek();
475 memset(file_name, 0, sizeof(file_name));
476 sprintf(file_name, "REC%d.WAV", recorderd_file_idx);
477 kprintf("rec %s\n", file_name);
481 start_rec(file_name);
482 text_xprintf(bm, 5, 0, TEXT_CENTER | TEXT_FILL, "Start recording on file: %s", file_name);
483 lcd_hx8347_blitBitmap(bm);
484 start = timer_clock();
489 recorderd_file_idx++;
490 text_xprintf(bm, 5, 0, TEXT_CENTER | TEXT_FILL, "Stop recording: %s", file_name);
491 mtime_t elaps = ticks_to_ms(timer_clock() - start);
492 text_xprintf(bm, 6, 0, TEXT_CENTER | TEXT_FILL, "Recorded: %ld.%ldsec", elaps / 1000, elaps % 1000);
493 lcd_hx8347_blitBitmap(bm);
506 static void setBrightness(Bitmap *bm)
511 text_xprintf(bm, 1, 0, TEXT_FILL | TEXT_CENTER, "Brightness: %d", lcd_brightness);
512 text_xprintf(bm, 3, 0, TEXT_FILL | TEXT_CENTER, "RIGHT key: change");
513 text_xprintf(bm, 4, 0, TEXT_FILL | TEXT_CENTER, "LEFT key: back ");
514 lcd_hx8347_blitBitmap(bm);
516 keymask_t mask = kbd_get();
520 else if (mask & K_RIGHT)
522 if (++lcd_brightness > LCD_BACKLIGHT_MAX)
524 lcd_setBacklight(lcd_brightness);
529 static void setVolume(Bitmap *bm)
532 text_style(bm, STYLEF_BOLD | STYLEF_UNDERLINE, STYLEF_BOLD | STYLEF_UNDERLINE);
533 text_xprintf(bm, 0, 0, TEXT_CENTER, "Headphone Volume");
534 text_style(bm, 0, STYLEF_MASK);
535 text_xprintf(bm, 2, 0, TEXT_NORMAL, "Turn VR1 potentiometer to adjust it.");
539 headphone_volume = ADC_RANGECONV(adc_read(1), 0, 100);
540 text_xprintf(bm, 5, 0, TEXT_FILL | TEXT_CENTER, "Volume %d%%", headphone_volume);
541 lcd_hx8347_blitBitmap(bm);
544 if (kbd_peek() & KEY_MASK)
549 static void NORETURN soft_reset(Bitmap * bm)
556 text_xprintf(bm, 2, 0, TEXT_FILL | TEXT_CENTER, "%d", i);
557 lcd_hx8347_blitBitmap(bm);
560 text_xprintf(bm, 2, 0, TEXT_FILL | TEXT_CENTER, "REBOOT");
561 lcd_hx8347_blitBitmap(bm);
564 /* Perform a software reset request */
565 HWREG(NVIC_APINT) = NVIC_APINT_VECTKEY | NVIC_APINT_SYSRESETREQ;
570 static struct MenuItem main_items[] =
572 { (const_iptr_t)"Play SD file", 0, (MenuHook)play_menu, (iptr_t)&lcd_bitmap },
573 { (const_iptr_t)"Record file on SD", 0, (MenuHook)rec_menu, (iptr_t)&lcd_bitmap },
574 { (const_iptr_t)"Set brightness", 0, (MenuHook)setBrightness, (iptr_t)&lcd_bitmap },
575 { (const_iptr_t)"Set volume", 0, (MenuHook)setVolume, (iptr_t)&lcd_bitmap },
576 { (const_iptr_t)"Reboot", 0, (MenuHook)soft_reset, (iptr_t)&lcd_bitmap },
577 { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
579 static struct Menu main_menu = { main_items, "BeRTOS", MF_STICKY | MF_SAVESEL, &lcd_bitmap, 0, lcd_hx8347_blitBitmap };
594 kprintf("sam3x %s: %d times\n", VERS_HOST, VERS_BUILD);
597 i2c_init(&i2c, I2C_BITBANG0, CONFIG_I2C_FREQ);
598 i2s_init(&i2s, SSC0);
600 wm8731_init(&wm8731_ctx, &i2c, CODEC_ADDR);
601 wm8731_setVolume(&wm8731_ctx, WM8731_HEADPHONE, 0);
603 kprintf("CPU Frequecy:%ld\n", CPU_FREQ);
606 lcd_setBacklight(lcd_brightness);
608 gfx_bitmapInit(&lcd_bitmap, raster, LCD_WIDTH, LCD_HEIGHT);
609 gfx_setFont(&lcd_bitmap, &font_luBS14);
610 lcd_hx8347_blitBitmap(&lcd_bitmap);
614 proc_new(play_proc, NULL, sizeof(play_proc_stack), play_proc_stack);
615 proc_new(rec_proc, NULL, sizeof(rec_proc_stack), rec_proc_stack);
616 msg_initPort(&proc_play_inPort, event_createGeneric());
617 msg_initPort(&proc_rec_inPort, event_createGeneric());
619 lcd_hx8347_blitBitmap24(10, 52, BMP_LOGO_WIDTH, BMP_LOGO_HEIGHT, bmp_logo);
625 menu_handle(&main_menu);