pimoroni-pico/examples/camera_pack/camera_pack_sdcard_test.cpp

117 lines
2.3 KiB
C++
Raw Normal View History

2023-02-27 10:28:56 +00:00
#include <cstdio>
#include <math.h>
#include <stdio.h>
#include <stdio.h>
#include <string>
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "drivers/fatfs/ff.h"
FATFS fs;
FRESULT fr;
2023-02-27 17:34:34 +00:00
char data[] = "ouch this is going to hurt";
2023-02-27 10:28:56 +00:00
2023-02-27 17:34:34 +00:00
void print_error(FRESULT error){
switch (error)
{
case FR_DISK_ERR:
printf("FR_DISK_ERROR\n");
break;
case FR_NOT_READY:
printf("FR_NOT_READY\n");
break;
case FR_EXIST:
printf("FR_EXIST\n");
break;
case FR_NOT_ENABLED:
printf("FR_NOT_ENABLED\n");
break;
default:
printf("unknown error %d\n", error);
break;
}
}
2023-02-27 10:28:56 +00:00
int main() {
stdio_init_all();
sleep_ms(2000);
gpio_init(17);
gpio_set_function(17, GPIO_FUNC_SIO);
gpio_init(SDCARD_PIN_SPI0_CS);
gpio_pull_up(SDCARD_PIN_SPI0_CS);
gpio_set_function(SDCARD_PIN_SPI0_CS, GPIO_FUNC_SIO);
gpio_set_dir(17 , true);
gpio_put(17, 1);
int count =0 ;
2023-02-27 17:34:34 +00:00
const void* buff = &data[0];
2023-02-27 10:28:56 +00:00
while (1)
{
/* code */
printf("mounting sd card.. \n");
printf(" cs pin %d ", SDCARD_PIN_SPI0_CS);
printf(" count %d \n", count);
count ++;
fr = f_mount(&fs, "", 1);
if (fr != FR_OK) {
printf("Failed to mount SD card, error: %d\n", fr);
}
else
{
printf("done!\n");
}
printf("Listing sd card contents..\n");
FILINFO file;
auto dir = new DIR();
f_opendir(dir, "/");
while(f_readdir(dir, &file) == FR_OK && file.fname[0]) {
printf("- %s %lld\n", file.fname, file.fsize);
}
2023-02-27 17:34:34 +00:00
f_closedir(dir);
2023-02-27 10:28:56 +00:00
const TCHAR* filename = "test1.txt";
FIL *fil = new FIL;
2023-02-27 17:34:34 +00:00
uint count = 0;
2023-02-27 10:28:56 +00:00
uint bytes_written;
2023-02-27 17:34:34 +00:00
while (1){
fr = f_open(fil, filename, FA_OPEN_APPEND | FA_WRITE);
if ( fr == FR_OK) {
//fr = f_lseek(fil, f_size(fil));
f_printf(fil, "hello world %d \n", count);
f_write(fil, buff, 12, &bytes_written);
printf("I have written %d\n", bytes_written);
2023-02-27 10:28:56 +00:00
2023-02-27 17:34:34 +00:00
fr = f_close(fil);
if (fr != FR_OK){
print_error(fr);
}
printf("linewritten\n");
2023-02-27 10:28:56 +00:00
}
else {
printf("no write \n");
2023-02-27 17:34:34 +00:00
print_error(fr);
}
sleep_ms(2000);
count++;
2023-02-27 10:28:56 +00:00
}
printf("done!\n");
sleep_ms(2000);
}
return 0;
}