Skip to content

Commit

Permalink
Clean up printfs
Browse files Browse the repository at this point in the history
  • Loading branch information
carlk3 committed Feb 12, 2023
1 parent f7226b4 commit d84f5da
Show file tree
Hide file tree
Showing 34 changed files with 394 additions and 2,835 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
*.png
*.zip
*.xlsx
*.bak
**/.vscode
15 changes: 9 additions & 6 deletions examples/PlatformIO/data_logger/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ debug_tool = picoprobe
upload_protocol = picoprobe
monitor_port = COM4
monitor_speed = 115200
build_flags =
-D PICO_STDIO_USB
-D PICO_STDIO_UART
lib_deps =
; build_flags =
; -D USE_PRINTF
; -D USE_DBG_PRINTF

; lib_deps =
; https://github.com/carlk3/no-OS-FatFS-SD-SPI-RPi-Pico.git#sdio
carlk3/no-OS-FatFS-SD-SPI-RPi-Pico@^1.0.0
lib_ldf_mode = chain+
; carlk3/no-OS-FatFS-SD-SPI-RPi-Pico@^1.0.0
; Use local copy:
lib_deps = no-OS-FatFS-SD-SPI-RPi-Pico.dev_sdio
lib_extra_dirs = ../../../..
28 changes: 26 additions & 2 deletions examples/PlatformIO/data_logger/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/

/*
This example reads analog input A0 and logs the voltage
in a file on an SD card once per second.
*/

#include <assert.h>
#include <time.h>
#include "FatFsSd.h"
Expand All @@ -21,6 +26,7 @@ specific language governing permissions and limitations under the License.
#include "pico/stdlib.h"

/* Infrastructure*/
#if USE_PRINTF
extern "C" int printf(const char *__restrict format, ...) {
char buf[256] = {0};
va_list xArgs;
Expand All @@ -32,8 +38,26 @@ extern "C" int printf(const char *__restrict format, ...) {
extern "C" int puts(const char *s) {
return Serial1.println(s);
}
#else
# define printf Serial1.printf
# define puts Serial1.println
#endif

/* ********************************************************************** */
/*
This example assumes the following wiring:
| GPIO | Pico Pin | microSD | Function |
| ---- | -------- | ------- | ----------- |
| 16 | 21 | DET | Card Detect |
| 17 | 22 | CLK | SDIO_CLK |
| 18 | 24 | CMD | SDIO_CMD |
| 19 | 25 | DAT0 | SDIO_D0 |
| 20 | 26 | DAT1 | SDIO_D1 |
| 21 | 27 | DAT2 | SDIO_D2 |
| 22 | 29 | DAT3 | SDIO_D3 |
*/

// Hardware Configuration of the SD Card "objects"
static sd_card_t sd_cards[] = { // One for each SD card
Expand All @@ -54,8 +78,8 @@ static sd_card_t sd_cards[] = { // One for each SD card
.sdio_if = {
.CMD_gpio = 18,
.D0_gpio = 19,
.SDIO_PIO = pio1,
.DMA_IRQ_num = DMA_IRQ_1
.SDIO_PIO = pio1, // Either pio0 or pio1
.DMA_IRQ_num = DMA_IRQ_1 // Either DMA_IRQ_0 or DMA_IRQ_1
},
.use_card_detect = true,
.card_detect_gpio = 16, // Card detect
Expand Down
5 changes: 5 additions & 0 deletions examples/PlatformIO/hw_debug/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
22 changes: 22 additions & 0 deletions examples/PlatformIO/hw_debug/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[env:pico]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = pico
framework = arduino
board_build.core = earlephilhower
debug_tool = picoprobe
upload_protocol = picoprobe
monitor_port = COM4
monitor_speed = 115200

build_flags =
-D USE_PRINTF
-D USE_DBG_PRINTF
-I ../../tests

; lib_deps = https://github.com/carlk3/no-OS-FatFS-SD-SPI-RPi-Pico.git#sdio
; Use local copy:
lib_deps = no-OS-FatFS-SD-SPI-RPi-Pico.dev_sdio
lib_extra_dirs = ../../../..

; evaluate C/C++ Preprocessor conditional syntax
; lib_ldf_mode = chain+
104 changes: 104 additions & 0 deletions examples/PlatformIO/hw_debug/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
Copyright 2023 Carl John Kugler III
Licensed under the Apache License, Version 2.0 (the License); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/

/*
This example runs a low level I/O test than can be helpful for debugging hardware.
It will destroy the format of the SD card!
*/
#include "FatFsSd.h"
#include "tests/tests.h"
//
#include "SerialUART.h"

/* Infrastructure*/
extern "C" int printf(const char *__restrict format, ...) {
char buf[256] = {0};
va_list xArgs;
va_start(xArgs, format);
vsnprintf(buf, sizeof buf, format, xArgs);
va_end(xArgs);
return Serial1.printf("%s", buf);
}
extern "C" int puts(const char *s) {
return Serial1.println(s);
}

/* ********************************************************************** */
/*
This example assumes the following wiring:
| GPIO | Pico Pin | microSD | Function |
| ---- | -------- | ------- | ----------- |
| 16 | 21 | DET | Card Detect |
| 17 | 22 | CLK | SDIO_CLK |
| 18 | 24 | CMD | SDIO_CMD |
| 19 | 25 | DAT0 | SDIO_D0 |
| 20 | 26 | DAT1 | SDIO_D1 |
| 21 | 27 | DAT2 | SDIO_D2 |
| 22 | 29 | DAT3 | SDIO_D3 |
*/
// Hardware Configuration of the SD Card "objects"
static sd_card_t sd_cards[] = { // One for each SD card
{
.pcName = "0:", // Name used to mount device
.type = SD_IF_SDIO,
/*
Pins CLK_gpio, D1_gpio, D2_gpio, and D3_gpio are at offsets from pin D0_gpio.
The offsets are determined by sd_driver\SDIO\rp2040_sdio.pio.
CLK_gpio = (D0_gpio + SDIO_CLK_PIN_D0_OFFSET) % 32;
As of this writing, SDIO_CLK_PIN_D0_OFFSET is 30,
which is -2 in mod32 arithmetic, so:
CLK_gpio = D0_gpio -2.
D1_gpio = D0_gpio + 1;
D2_gpio = D0_gpio + 2;
D3_gpio = D0_gpio + 3;
*/
.sdio_if = {
.CMD_gpio = 18,
.D0_gpio = 19,
.SDIO_PIO = pio1,
.DMA_IRQ_num = DMA_IRQ_1
},
.use_card_detect = true,
.card_detect_gpio = 16, // Card detect
.card_detected_true = 1 // What the GPIO read returns when a card is
// present.
}
};
/*
The following *get_num, *get_by_num functions are required by the library API.
They are how the library finds out about the configuration.
*/
extern "C" size_t sd_get_num() { return count_of(sd_cards); }
extern "C" sd_card_t *sd_get_by_num(size_t num) {
if (num <= sd_get_num()) {
return &sd_cards[num];
} else {
return NULL;
}
}
// These need to be defined for the API even if SPI is not used:
extern "C" size_t spi_get_num() { return 0; }
extern "C" spi_t *spi_get_by_num(size_t num) { return NULL; }

/* ********************************************************************** */

void setup() {
Serial1.begin(115200); // set up Serial library at 9600 bps
while (!Serial1)
; // Serial is via USB; wait for enumeration
lliot(0);
}
void loop() {}
13 changes: 6 additions & 7 deletions examples/PlatformIO/one_SDIO/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ upload_protocol = picoprobe
monitor_port = COM4
monitor_speed = 115200

build_flags =
-D PICO_STDIO_USB
-D PICO_STDIO_UART
; build_flags =

; lib_deps = no-OS-FatFS-SD-SPI-RPi-Pico.dev_sdio
; lib_extra_dirs = ../../../..
lib_deps = https://github.com/carlk3/no-OS-FatFS-SD-SPI-RPi-Pico.git#sdio
; lib_deps = https://github.com/carlk3/no-OS-FatFS-SD-SPI-RPi-Pico.git#sdio
; Use local copy:
lib_deps = no-OS-FatFS-SD-SPI-RPi-Pico.dev_sdio
lib_extra_dirs = ../../../..

; evaluate C/C++ Preprocessor conditional syntax
lib_ldf_mode = chain+
; lib_ldf_mode = chain+
50 changes: 30 additions & 20 deletions examples/PlatformIO/one_SDIO/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,26 @@ CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/

/* Write "Hello, world!\n" to SD Card */
#include "FatFsSd.h"
//
#include "SerialUART.h"

/* Infrastructure*/
extern "C" int printf(const char *__restrict format, ...) {
char buf[256] = {0};
va_list xArgs;
va_start(xArgs, format);
vsnprintf(buf, sizeof buf, format, xArgs);
va_end(xArgs);
return Serial1.printf("%s", buf);
}
extern "C" int puts(const char *s) {
return Serial1.println(s);
}

/* ********************************************************************** */
#define printf Serial1.printf
#define puts Serial1.println
/*
This example assumes the following wiring:
| GPIO | Pico Pin | microSD | Function |
| ---- | -------- | ------- | ----------- |
| 16 | 21 | DET | Card Detect |
| 17 | 22 | CLK | SDIO_CLK |
| 18 | 24 | CMD | SDIO_CMD |
| 19 | 25 | DAT0 | SDIO_D0 |
| 20 | 26 | DAT1 | SDIO_D1 |
| 21 | 27 | DAT2 | SDIO_D2 |
| 22 | 29 | DAT3 | SDIO_D3 |
*/
// Hardware Configuration of the SD Card "objects"
static sd_card_t sd_cards[] = { // One for each SD card
{
Expand Down Expand Up @@ -59,6 +60,10 @@ static sd_card_t sd_cards[] = { // One for each SD card
// present.
}
};
/*
The following *get_num, *get_by_num functions are required by the library API.
They are how the library finds out about the configuration.
*/
extern "C" size_t sd_get_num() { return count_of(sd_cards); }
extern "C" sd_card_t *sd_get_by_num(size_t num) {
if (num <= sd_get_num()) {
Expand All @@ -67,7 +72,7 @@ extern "C" sd_card_t *sd_get_by_num(size_t num) {
return NULL;
}
}
// These need to be defined for the API:
// These need to be defined for the API even if SPI is not used:
extern "C" size_t spi_get_num() { return 0; }
extern "C" spi_t *spi_get_by_num(size_t num) { return NULL; }

Expand All @@ -77,26 +82,31 @@ void setup() {
Serial1.begin(115200); // set up Serial library at 9600 bps
while (!Serial1)
; // Serial is via USB; wait for enumeration
time_init();

puts("Hello, world!");

// See FatFs - Generic FAT Filesystem Module, "Application Interface",
// http://elm-chan.org/fsw/ff/00index_e.html
sd_card_t *pSD = sd_get_by_num(0);
FRESULT fr = f_mount(&pSD->fatfs, pSD->pcName, 1);
if (FR_OK != fr) panic("f_mount error: %s (%d)\n", FRESULT_str(fr), fr);
if (FR_OK != fr) {
printf("f_mount error: %s (%d)\n", FRESULT_str(fr), fr);
for (;;) __BKPT(1);
}
FIL fil;
const char* const filename = "filename.txt";
fr = f_open(&fil, filename, FA_OPEN_APPEND | FA_WRITE);
if (FR_OK != fr && FR_EXIST != fr)
panic("f_open(%s) error: %s (%d)\n", filename, FRESULT_str(fr), fr);
if (FR_OK != fr && FR_EXIST != fr) {
printf("f_open(%s) error: %s (%d)\n", filename, FRESULT_str(fr), fr);
for (;;) __BKPT(2);
}
if (f_printf(&fil, "Hello, world!\n") < 0) {
printf("f_printf failed\n");
for (;;) __BKPT(3);
}
fr = f_close(&fil);
if (FR_OK != fr) {
printf("f_close error: %s (%d)\n", FRESULT_str(fr), fr);
for (;;) __BKPT(4);
}
f_unmount(pSD->pcName);

Expand Down
11 changes: 5 additions & 6 deletions examples/PlatformIO/one_SPI/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ monitor_port = COM4
monitor_speed = 115200

build_flags =
-D PICO_STDIO_USB
-D PICO_STDIO_UART

; lib_deps = no-OS-FatFS-SD-SPI-RPi-Pico.dev_sdio
; lib_extra_dirs = ../../../..
lib_deps = https://github.com/carlk3/no-OS-FatFS-SD-SPI-RPi-Pico.git#sdio
; lib_deps = https://github.com/carlk3/no-OS-FatFS-SD-SPI-RPi-Pico.git#sdio
; Use local copy:
lib_deps = no-OS-FatFS-SD-SPI-RPi-Pico.dev_sdio
lib_extra_dirs = ../../../..

; evaluate C/C++ Preprocessor conditional syntax
lib_ldf_mode = chain+
; lib_ldf_mode = chain+
Loading

0 comments on commit d84f5da

Please sign in to comment.