diff options
author | Camil Staps | 2017-01-31 23:15:28 +0100 |
---|---|---|
committer | Camil Staps | 2017-01-31 23:15:28 +0100 |
commit | 631204a1feffa8cf3795060370b14dfb9f53f533 (patch) | |
tree | 001d32e0157127607f7c088881f098c2eca841a1 /logger.c |
Diffstat (limited to 'logger.c')
-rw-r--r-- | logger.c | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/logger.c b/logger.c new file mode 100644 index 0000000..a574ea0 --- /dev/null +++ b/logger.c @@ -0,0 +1,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; + } +} |