aboutsummaryrefslogtreecommitdiff
path: root/logger.c
blob: a574ea0e86c9bcb9ecce4850bd56adf8f873cd86 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "logger.h"

void putch(unsigned char byteTx) {
	uart_putch(byteTx);
}

void init(void)
{
	OSCCON = 0xf0;

	ANSELA = 0x00;
	ANSELB = 0x00;
	ANSELC = 0x00;

	LATA = 0x00;
	TRISA = 0x00;

	LATCbits.LATC6 = 1;
	TRISCbits.TRISC6 = 0;
	TRISCbits.TRISC7 = 1;

	uart_init();
	RC6PPS = 0x24;
	RC7PPS = 0x25;

	dac_init();
}

void put_rc (const char * s, FRESULT rc) {
	const char *p;
	FRESULT i;
	static const char str[] = "OK\0DISK_ERR\0NOT_READY\0NO_FILE\0NOT_OPENED\0NOT_ENABLED\0NO_FILE_SYSTEM\0";

	for (p = str, i = 0; i != rc && *p; i++) while(*p++);
	printf("%s:\t%u\tFR_%s\r\n", s, rc, p);
}

static char buff[256];
static FATFS fatfs;
static DIR dir;
static FIL file;
static FILINFO fno;

void main(void)
{
	uint8_t fcount;

	uint16_t k;
	init();
	for (k = 0; k < 1000; k++);

	printf("\r\nStarting up...\r\n");
	
	DSTATUS stat = disk_initialize(0);
	printf("Initialised:\t%d\r\n", stat);

	put_rc("Mounted", f_mount(&fatfs, "", 0));

	put_rc("Opened dir", f_opendir(&dir, "/"));

	fcount = 0;
	for (;;) {
		uint8_t res = f_readdir(&dir, &fno);
		if (!fno.fname[0])
			break;
		if (fno.fattrib & AM_DIR)
			printf("d\t%s\r\n", fno.fname);
		else
			printf("%7lu\t%s\r\n", fno.fsize, fno.fname);
		fcount++;
	}
	printf("%u item(s)\r\n", fcount);

	printf("\r\nOpening audio.wav...\r\n");

	put_rc("Opened", f_open(&file, "audio.wav", FA_READ));
	printf("Reading %u sectors...\r\n", fno.fsize);

	FSIZE_t bytecount = fno.fsize;
	while (bytecount) {
		uint16_t cnt;
		if (bytecount >= sizeof buff)
			cnt = sizeof buff;
		else
			cnt = bytecount;
		bytecount -= cnt;

		uint8_t res = f_read(&file, buff, cnt, &k);
		if (res != FR_OK) {
			put_rc("Failed to read", res);
			break;
		}
		if (k != cnt) {
			printf("Bytecounts differ\r\n");
			break;
		}
		putch('.');

		for (k = 0; k < cnt; k++) {
			dac_set(buff[k]);
			delay_10us();
		}
	}
	printf("\r\nAll done.\r\n");

	k = 0;
	while (1) {
		//dac_set(0xff & (k++));
		if (k & 0x0100)
			k = 0;
	}

	while (1) {
		LATAbits.LATA1 = ~LATAbits.LATA1;
	}
}