Skip to content

Commit 619dda1

Browse files
authored
Merge pull request #17 from mutablelogic/dev
Added more picofuse code
2 parents 2752594 + 094294d commit 619dda1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+3839
-532
lines changed

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ include(CTest)
4040
add_subdirectory(tests/fuse)
4141

4242
# Examples
43-
add_subdirectory(examples/fuse)
4443
if(TARGET_OS STREQUAL "pico")
45-
add_subdirectory(examples/picofuse)
44+
add_subdirectory(examples)
4645
endif()

examples/CMakeLists.txt

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
##############################################################################
2+
3+
set(NAME "blink")
4+
5+
add_executable(${NAME}
6+
common/main.c
7+
blink/run.c
8+
)
9+
target_include_directories(${NAME} PRIVATE
10+
${CMAKE_CURRENT_LIST_DIR}/../include
11+
)
12+
target_link_libraries(${NAME} picofuse)
13+
14+
# enable usb output, disable uart output
15+
pico_enable_stdio_usb(${NAME} 1)
16+
pico_enable_stdio_uart(${NAME} 0)
17+
18+
# create map/bin/hex/uf2 file etc.
19+
pico_add_extra_outputs(${NAME})
20+
21+
##############################################################################
22+
23+
set(NAME "bme280_example")
24+
25+
add_executable(${NAME}
26+
common/main.c
27+
bme280/run.c
28+
)
29+
target_include_directories(${NAME} PRIVATE
30+
${CMAKE_CURRENT_LIST_DIR}/../include
31+
)
32+
target_link_libraries(${NAME}
33+
picofuse
34+
bme280
35+
)
36+
37+
# enable usb output, disable uart output
38+
pico_enable_stdio_usb(${NAME} 1)
39+
pico_enable_stdio_uart(${NAME} 0)
40+
41+
# create map/bin/hex/uf2 file etc.
42+
pico_add_extra_outputs(${NAME})
43+
44+
##############################################################################
45+
46+
set(NAME "fade")
47+
48+
add_executable(${NAME}
49+
common/main.c
50+
fade/run.c
51+
)
52+
target_include_directories(${NAME} PRIVATE
53+
${CMAKE_CURRENT_LIST_DIR}/../include
54+
)
55+
target_link_libraries(${NAME}
56+
picofuse
57+
)
58+
59+
# enable usb output, disable uart output
60+
pico_enable_stdio_usb(${NAME} 1)
61+
pico_enable_stdio_uart(${NAME} 0)
62+
63+
# create map/bin/hex/uf2 file etc.
64+
pico_add_extra_outputs(${NAME})
65+
66+
##############################################################################
67+
68+
set(NAME "gpio_in")
69+
70+
add_executable(${NAME}
71+
common/main.c
72+
gpio_in/run.c
73+
)
74+
target_include_directories(${NAME} PRIVATE
75+
${CMAKE_CURRENT_LIST_DIR}/../include
76+
)
77+
target_link_libraries(${NAME} picofuse)
78+
79+
# enable usb output, disable uart output
80+
pico_enable_stdio_usb(${NAME} 1)
81+
pico_enable_stdio_uart(${NAME} 0)
82+
83+
# create map/bin/hex/uf2 file etc.
84+
pico_add_extra_outputs(${NAME})
85+
86+
##############################################################################
87+
88+
set(NAME "inky")
89+
90+
add_executable(${NAME}
91+
common/main.c
92+
inky/run.c
93+
)
94+
target_include_directories(${NAME} PRIVATE
95+
${CMAKE_CURRENT_LIST_DIR}/../include
96+
)
97+
target_link_libraries(${NAME}
98+
picofuse
99+
uc8151
100+
)
101+
102+
# enable usb output, disable uart output
103+
pico_enable_stdio_usb(${NAME} 1)
104+
pico_enable_stdio_uart(${NAME} 0)
105+
106+
# create map/bin/hex/uf2 file etc.
107+
pico_add_extra_outputs(${NAME})

examples/blink/run.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <picofuse/picofuse.h>
2+
3+
#define LED_DELAY_MS 250
4+
5+
/* @brief LED Callback
6+
*/
7+
void led_callback(fuse_t *self, fuse_event_t *evt, void *user_data)
8+
{
9+
assert(self);
10+
assert(evt);
11+
12+
// LED is the user_data field
13+
fuse_led_t *led = (fuse_led_t *)user_data;
14+
15+
// Blink the LED
16+
fuse_led_set(led, !fuse_led_get(led));
17+
}
18+
19+
int run(fuse_t *self)
20+
{
21+
// LED output
22+
fuse_led_t *led = (fuse_led_t *)fuse_retain(self, fuse_new_led(self));
23+
assert(led);
24+
25+
// Register timer callback on core 0
26+
assert(fuse_register_callback(self, FUSE_EVENT_TIMER, 0, led_callback));
27+
28+
// Schedule timer to run every second
29+
assert(fuse_timer_schedule(self, LED_DELAY_MS, true, led));
30+
31+
return 0;
32+
}

examples/bme280/run.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <picofuse/picofuse.h>
2+
3+
#define GPIO_SPI_SCK 2 // SCK = GPIO2, Physical pin 4
4+
#define GPIO_SPI_TX 3 // TX = GPIO3, Physical pin 5
5+
#define GPIO_SPI_RX 4 // RX = GPIO4, Physical pin 6
6+
#define GPIO_SPI_CS 5 // CS = GPIO5, Physical pin 7
7+
8+
/* @brief Callback when there is a measurement from the BME280
9+
*/
10+
void bme280_callback(fuse_t *self, fuse_event_t *evt, void* user_data)
11+
{
12+
assert(self);
13+
assert(evt);
14+
assert(user_data);
15+
16+
// Print the measurement
17+
fuse_bme280_measurement_t* meas = (fuse_bme280_measurement_t*)user_data;
18+
fuse_printf(self, "Temp: %f\n",meas->temperature);
19+
if (meas->pressure) {
20+
fuse_printf(self, "Pressure: %f\n",meas->pressure);
21+
}
22+
if (meas->humidity) {
23+
fuse_printf(self, "Humidity: %f\n",meas->humidity);
24+
}
25+
}
26+
27+
/* @brief Callback for printing out memory stats
28+
*/
29+
void stats_callback(fuse_t *self, fuse_event_t *evt, void* user_data)
30+
{
31+
assert(self);
32+
assert(evt);
33+
34+
// user_data should be NULL
35+
if (user_data) {
36+
return;
37+
}
38+
39+
// Print the stats
40+
size_t cur, max;
41+
fuse_memstats(self, &cur, &max);
42+
43+
// Print the stats
44+
fuse_printf(self, "Memory: %u bytes/%u bytes\n", cur, max);
45+
}
46+
47+
int run(fuse_t *self)
48+
{
49+
// Create an SPI interface
50+
fuse_spi_data_t config = {
51+
.cs = GPIO_SPI_CS,
52+
.ck = GPIO_SPI_SCK,
53+
.tx = GPIO_SPI_TX,
54+
.rx = GPIO_SPI_RX,
55+
.baudrate = 500 * 1000, // This example will use SPI0 at 0.5MHz
56+
.xfr_delayms = 10,
57+
};
58+
59+
// Create a new BME280
60+
fuse_bme280_t* bme280 = (fuse_bme280_t* )fuse_retain(self, fuse_new_bme280(self, fuse_new_spi(self, config)));
61+
assert(bme280);
62+
63+
// Print driver details
64+
fuse_printf(self, "bme280 initialized: %v\n", bme280);
65+
66+
// Register a callback on core 0 for the measured data
67+
assert(fuse_register_callback(self, FUSE_EVENT_BME280, 0, bme280_callback));
68+
69+
// Register a callback on core 0 for timer
70+
assert(fuse_register_callback(self, FUSE_EVENT_TIMER, 0, stats_callback));
71+
72+
// Read measurements every 10 secs
73+
fuse_bme280_read(self, bme280, 10 * 1000);
74+
75+
// Timer to output the memory stats every minute
76+
fuse_timer_t* timer = fuse_timer_schedule(self, 27 * 1000, true, NULL);
77+
assert(timer);
78+
79+
return 0;
80+
}
File renamed without changes.

examples/fade/run.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <picofuse/picofuse.h>
2+
#include <pico/stdlib.h>
3+
4+
/* @brief Callback when there is a wrap-around on the PWM counter
5+
*/
6+
void pwm_callback(fuse_t *self, fuse_event_t *evt, void *user_data)
7+
{
8+
assert(self);
9+
assert(evt);
10+
11+
static int fade = 0;
12+
static bool going_up = true;
13+
14+
if (going_up)
15+
{
16+
++fade;
17+
if (fade > 0xFF)
18+
{
19+
fade = 0xFF;
20+
going_up = false;
21+
}
22+
}
23+
else
24+
{
25+
--fade;
26+
if (fade < 1)
27+
{
28+
fade = 1;
29+
going_up = true;
30+
}
31+
}
32+
33+
fuse_pwm_set_duty_cycle_b(self, (fuse_pwm_t *)user_data, fade);
34+
}
35+
36+
int run(fuse_t *self)
37+
{
38+
fuse_pwm_config_t pwm_config = {
39+
.gpio_b = PICO_DEFAULT_LED_PIN,
40+
.duty_cycle_b = 0xFF,
41+
.freq = 512 * 1000,
42+
.event = true,
43+
};
44+
fuse_watchdog_config_t watchdog_config = {};
45+
46+
// Watchdog
47+
fuse_watchdog_t *watchdog = (fuse_watchdog_t *)fuse_retain(self, fuse_new_watchdog(self, watchdog_config));
48+
assert(watchdog);
49+
fuse_debugf(self, "WATCHDOG: %v\n", watchdog);
50+
51+
// PWM
52+
fuse_pwm_t *pwm = (fuse_pwm_t *)fuse_retain(self, fuse_new_pwm(self, pwm_config));
53+
assert(pwm);
54+
fuse_register_callback(self, FUSE_EVENT_PWM, 0, pwm_callback);
55+
fuse_debugf(self, "PWM: %v\n", pwm);
56+
57+
return 0;
58+
}

examples/fuse/CMakeLists.txt

Lines changed: 0 additions & 22 deletions
This file was deleted.

examples/picofuse/gpio_in/run.c renamed to examples/gpio_in/run.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ void gpio_callback(fuse_t *self, fuse_event_t *evt, void *user_data)
1414

1515
int run(fuse_t *self)
1616
{
17-
// Initialize picofuse
18-
picofuse_init(self);
19-
2017
// GPIO inputs
2118
assert(fuse_retain(self, fuse_new_gpio(self, 23, FUSE_GPIO_IN)));
2219
assert(fuse_retain(self, fuse_new_gpio(self, 12, FUSE_GPIO_PULLUP)));
File renamed without changes.

0 commit comments

Comments
 (0)