Skip to content

Commit

Permalink
Sdcard Support for App and Firmware Updates #752 (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
josancamon19 authored Sep 11, 2024
2 parents 15c1e89 + f4b72fe commit c64fd18
Show file tree
Hide file tree
Showing 17 changed files with 707 additions and 150 deletions.
20 changes: 20 additions & 0 deletions Friend/firmware/firmware_v1.0/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,23 @@ To enable USB serial debugging:
- **Transport**: Manages Bluetooth connectivity and audio streaming.
- **Storage**: Handles SD card operations and audio file management.
- **LED Control**: Provides visual feedback about device status.

## On the Storage Reads
The storage will automatically activate whenever there is no bluetooth connection to the app. Whenever you turn on the device, a new file is created which
will begin filling with opus encoded data. Whenever you connect to the app, the contents of the storage will begin streaming to the app. When it is finished, it will try to delete the file on the device.

The format of each packet is similar to the mic packets: that is, there is a 3 byte header denoting the order of packet arrival. The fourth bit is the number of bytes contained in the opus packet. The next bytes are the opus bytes themselves. The rest can be ignored.
Each packet (for now) is 83 bytes each for ease of transmission.

You don't need the app to test! Simply insert your device id in the file get_audio_file.py, and then run the file. If you want to decode the current file, then run decode_audio.py afterwards. There are some numbers that get sent by the device as a format
of acknowledgement. All of them are one byte each, so check for these status codes by checking the message lengths. Here are the important ones:

0 - This means the command was successfully parsed by the device. This means the start of transmission of audio data or deletion of a file.

100 - This number means the end of the audio transmission. You can know that the transmission ended with this code.

1,2,3,4,5 - These usually denote some error bits. They also mean that the device rejects the command and no transmission/deletion happens as a result.

Messages to the device take the form [a,b] or [a,b,c,d,e,f], where a denotes (0 for read) and (1 for delete), while b denotes the file number (1 is the first file, 2 is the second, etc. There is no notion of a 0th file). The optional [c,d,e,f] bytes denote the offset in uint format
in case you want to read from a file at some offset.

4 changes: 1 addition & 3 deletions Friend/firmware/firmware_v1.0/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,12 @@ int main(void)
set_led_green(false);

err = mount_sd_card();
printk("result of mount:%d\n",err);
LOG_INF("result of mount:%d",err);

k_msleep(500);
storage_init();




set_led_blue(true);
set_codec_callback(codec_handler);
err = codec_start();
Expand Down
90 changes: 46 additions & 44 deletions Friend/firmware/firmware_v1.0/src/sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int move_read_pointer(uint8_t num) {
struct fs_dirent entry;
int res = fs_stat(&read_buffer,&entry);
if (res) {
printk("invalid file\n");
LOG_ERR("invalid file\n");

return -1;
}
Expand All @@ -56,7 +56,7 @@ int move_write_pointer(uint8_t num) {
struct fs_dirent entry;
int res = fs_stat(&write_buffer,&entry);
if (res) {
printk("invalid file\n");
LOG_ERR("invalid file\n");

return -1;
}
Expand All @@ -73,7 +73,7 @@ int mount_sd_card(void)
uint32_t block_size;
static const char *disk_pdrv = "SD";
int err = disk_access_init(disk_pdrv);
printk("disk_access_init: %d\n", err);
LOG_INF("disk_access_init: %d\n", err);
if (err) {
k_msleep(2000);
err = disk_access_init(disk_pdrv);
Expand All @@ -85,52 +85,52 @@ int mount_sd_card(void)
int res = fs_mount(&mount_point);

if (res == FR_OK) {
printk("SD card mounted successfully\n");
LOG_INF("SD card mounted successfully");
} else {
printk("f_mount failed: %d\n", res);
LOG_ERR("f_mount failed: %d", res);
return -1;
}
res = fs_mkdir("/SD:/audio");
if (res == FR_OK) {
printk("audio directory created successfully\n");
LOG_INF("audio directory created successfully");
}
else if (res == FR_EXIST) {
printk("audio directory already exists\n");
LOG_INF("audio directory already exists");
}
else {
printk("audio directory creation failed: %d\n", res);
LOG_INF("audio directory creation failed: %d", res);
}

struct fs_dir_t zdp;
fs_dir_t_init(&zdp);
err = fs_opendir(&zdp,"/SD:/audio");
if (err) {
printk("error while opening directory \n",err);
LOG_ERR("error while opening directory ",err);
return -1;
}
printk("result of opendir: %d\n",err);
LOG_INF("result of opendir: %d",err);

struct fs_dirent entry_;

file_count =get_next_item(&zdp, &entry_);
if (file_count < 0) {
printk(" error getting file count\n");
LOG_ERR(" error getting file count");
return -1;
}

fs_closedir(&zdp);
printk("current num files: %d\n",file_count);
LOG_INF("current num files: %d",file_count);
file_count++;
printk("new num files: %d\n",file_count);
LOG_INF("new num files: %d",file_count);
initialize_audio_file(file_count);
err = move_write_pointer(file_count);
if (err) {
printk("erro while moving the write pointer\n");
LOG_ERR("erro while moving the write pointer");
return -1;
}
move_read_pointer(file_count);
if (err) {
printk("error while moving the reader pointer\n");
LOG_ERR("error while moving the reader pointer\n");
return -1;
}

Expand All @@ -139,10 +139,10 @@ int mount_sd_card(void)
res = fs_stat(info_path,&entry);
if (res) {
res = create_file("info.txt");
printk("result of info.txt creation: %d\n ",res);
LOG_INF("result of info.txt creation: %d ",res);

}
printk("result of check: %d\n",res);
LOG_INF("result of check: %d",res);


return 0;
Expand All @@ -160,7 +160,7 @@ int create_file(const char *file_path){

if (ret)
{
printk("File creation failed %d\n", ret);
LOG_ERR("File creation failed %d", ret);
return -2;
}
fs_close(&data_filp);
Expand Down Expand Up @@ -191,31 +191,11 @@ int read_audio_data(uint8_t *buf, int amount,int offset) {
int write_to_file(uint8_t *data,uint32_t length)
{

int ret = 0;
struct fs_file_t data_loc;
fs_file_t_init(&data_loc);
uint8_t *temp_ptr = data;
ret = fs_open(&data_loc, write_buffer , FS_O_WRITE | FS_O_APPEND);
if(ret)
{
printk("Error opening file\n");

return -1;
}
ret = fs_write(&data_loc, temp_ptr, length);
// // printk("length is %d\n", length);
// printk("write data: ");
// for (int i = 0; i < length; i++) {
// printk("%d ",temp_ptr[i]);
// }
// printk("\n");

if(ret < 0)
{
printk("er %d\n",ret);

return -1;
}
fs_open(&data_loc, write_buffer , FS_O_WRITE | FS_O_APPEND);
fs_write(&data_loc, temp_ptr, length);
fs_close(&data_loc);

return 0;
Expand Down Expand Up @@ -284,17 +264,39 @@ int get_next_item(struct fs_dir_t *zdp, struct fs_dirent *entry) {
}
int count = 0;
file_num_array[count] = entry->size;
printk("file numarray %d %d \n",count,file_num_array[count]);
printk("file name is %s \n", entry->name);
LOG_INF("file numarray %d %d ",count,file_num_array[count]);
LOG_INF("file name is %s ", entry->name);
count++;
while (zdp->mp->fs->readdir(zdp, entry) == 0 ) {
if (entry->name[0] == 0 ) {
break;
}
file_num_array[count] = entry->size;
printk("file numarray %d %d \n",count,file_num_array[count]);
printk("file name is %s \n", entry->name);
LOG_INF("file numarray %d %d ",count,file_num_array[count]);
LOG_INF("file name is %s ", entry->name);
count++;
}
return count;
}
//we should clear instead of delete since we lose fifo structure
int clear_audio_file(uint8_t num) {

char *ptr = generate_new_audio_header(num);
snprintf(current_full_path, sizeof(current_full_path), "%s%s", disk_mount_pt, ptr);
k_free(ptr);
int res = fs_unlink(current_full_path);
if (res) {
LOG_ERR("error deleting file");
return -1;
}
char *ptr2 = generate_new_audio_header(num);
k_msleep(10);
res = create_file(ptr2);
k_free(ptr2);
if (res) {
LOG_ERR("error creating file");
return -1;
}

return 0;
}
2 changes: 1 addition & 1 deletion Friend/firmware/firmware_v1.0/src/sdcard.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ uint32_t get_file_size(uint8_t num);

int move_read_pointer(uint8_t num);
int move_write_pointer(uint8_t num);

int clear_audio_file(uint8_t num);
Loading

0 comments on commit c64fd18

Please sign in to comment.