-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyxmpcallbacks.cpp
125 lines (116 loc) · 3.29 KB
/
myxmpcallbacks.cpp
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
117
118
119
120
121
122
123
124
125
#if defined(__IMXRT1062__)
#include <SdFat.h>
// Do my_xmp_read in two steps:
// first read from SD card to DMAMEM (malloc) then copy from DMAMEM to PSRAM
// This slows down the reads but seems to improve the reliability of SD card reads
// Maybe the SD card and PSRAM are interfering with each other because they're so close
// This is no longer necessary with the SdFat drive strength fix.
#define TWO_STEP_XMP_FILE_READ 0
size_t my_xmp_read(void *user_data, void *buf, size_t size, size_t num){
/*
Serial.print(__FUNCTION__);
Serial.print(" ");
Serial.print((uint32_t) buf, HEX);
Serial.print(" ");
Serial.print(size);
Serial.print(" ");
Serial.println(num);
*/
#if TWO_STEP_XMP_FILE_READ
// All of libxmp's memory allocations are in the PSRAM,
// so there is plenty of malloc memory available
const int XMP_READ_BUF_SIZE = 128*1024;
char *xmp_read_buf = (char*) malloc(XMP_READ_BUF_SIZE);
if(!xmp_read_buf){
return 0;
}
size_t total_bytes_to_read = size * num;
size_t total_bytes_read = 0;
char *charBuf = (char*)buf;
FsFile *file = (FsFile *)user_data;
while(total_bytes_read < total_bytes_to_read){
size_t bytes_to_read = min(total_bytes_to_read - total_bytes_read, sizeof(xmp_read_buf));
size_t bytes_read = file->read(xmp_read_buf, bytes_to_read);
if(bytes_read != bytes_to_read){
Serial.print("my_xmp_read: tried to read ");
Serial.print(bytes_to_read);
Serial.print(" bytes but got ");
Serial.print(bytes_read);
Serial.print(" bytes. read a total of ");
Serial.print(total_bytes_read);
Serial.print("/");
Serial.print(total_bytes_to_read);
Serial.println("bytes");
break;
}else{
memcpy(charBuf, xmp_read_buf, bytes_read);
arm_dcache_flush_delete(charBuf, bytes_read);
charBuf += bytes_read;
total_bytes_read += bytes_read;
}
}
free(xmp_read_buf);
#else
FsFile *file = (FsFile *)user_data;
int total_bytes_read = file->read(buf, size * num);
if(total_bytes_read != size * num){
Serial.print("my_xmp_read: tried to read ");
Serial.print(size * num);
Serial.print(" bytes but got ");
Serial.print(total_bytes_read);
Serial.println(" bytes. ");
}
#endif
/*
Serial.print("return ");
Serial.println(bytes_read / size);
*/
return total_bytes_read / size;
}
int my_xmp_seek(void *user_data, long offset, int whence){
/*
Serial.print(__FUNCTION__);
Serial.print(" ");
Serial.print(offset);
Serial.print(" ");
Serial.println(whence);
*/
FsFile *file = (FsFile *)user_data;
bool success;
if(whence == SEEK_SET){
success = file->seek(offset);
}else if(whence == SEEK_CUR){
success = file->seekCur(offset);
}else if(whence == SEEK_END){
success = file->seekEnd(offset);
}else{
/*
Serial.print("unsupported whence for seeking: ");
Serial.print(whence);
*/
return -1;
}
return success ? 0 : -1;
}
long my_xmp_tell(void *user_data){
/*
Serial.println(__FUNCTION__);
*/
FsFile *file = (FsFile *)user_data;
return file->curPosition();
}
int my_xmp_eof(void *user_data){
/*
Serial.println(__FUNCTION__);
*/
FsFile *file = (FsFile *)user_data;
return !file->available();
}
long my_xmp_size(void *user_data){
/*
Serial.print(__FUNCTION__);
*/
FsFile *file = (FsFile *)user_data;
return file->fileSize();
}
#endif