Skip to content

Commit

Permalink
Merge pull request #17 from mutablelogic/dev
Browse files Browse the repository at this point in the history
Added more picofuse code
  • Loading branch information
djthorpe authored Oct 21, 2024
2 parents 2752594 + 094294d commit 619dda1
Show file tree
Hide file tree
Showing 76 changed files with 3,839 additions and 532 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ include(CTest)
add_subdirectory(tests/fuse)

# Examples
add_subdirectory(examples/fuse)
if(TARGET_OS STREQUAL "pico")
add_subdirectory(examples/picofuse)
add_subdirectory(examples)
endif()
107 changes: 107 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
##############################################################################

set(NAME "blink")

add_executable(${NAME}
common/main.c
blink/run.c
)
target_include_directories(${NAME} PRIVATE
${CMAKE_CURRENT_LIST_DIR}/../include
)
target_link_libraries(${NAME} picofuse)

# enable usb output, disable uart output
pico_enable_stdio_usb(${NAME} 1)
pico_enable_stdio_uart(${NAME} 0)

# create map/bin/hex/uf2 file etc.
pico_add_extra_outputs(${NAME})

##############################################################################

set(NAME "bme280_example")

add_executable(${NAME}
common/main.c
bme280/run.c
)
target_include_directories(${NAME} PRIVATE
${CMAKE_CURRENT_LIST_DIR}/../include
)
target_link_libraries(${NAME}
picofuse
bme280
)

# enable usb output, disable uart output
pico_enable_stdio_usb(${NAME} 1)
pico_enable_stdio_uart(${NAME} 0)

# create map/bin/hex/uf2 file etc.
pico_add_extra_outputs(${NAME})

##############################################################################

set(NAME "fade")

add_executable(${NAME}
common/main.c
fade/run.c
)
target_include_directories(${NAME} PRIVATE
${CMAKE_CURRENT_LIST_DIR}/../include
)
target_link_libraries(${NAME}
picofuse
)

# enable usb output, disable uart output
pico_enable_stdio_usb(${NAME} 1)
pico_enable_stdio_uart(${NAME} 0)

# create map/bin/hex/uf2 file etc.
pico_add_extra_outputs(${NAME})

##############################################################################

set(NAME "gpio_in")

add_executable(${NAME}
common/main.c
gpio_in/run.c
)
target_include_directories(${NAME} PRIVATE
${CMAKE_CURRENT_LIST_DIR}/../include
)
target_link_libraries(${NAME} picofuse)

# enable usb output, disable uart output
pico_enable_stdio_usb(${NAME} 1)
pico_enable_stdio_uart(${NAME} 0)

# create map/bin/hex/uf2 file etc.
pico_add_extra_outputs(${NAME})

##############################################################################

set(NAME "inky")

add_executable(${NAME}
common/main.c
inky/run.c
)
target_include_directories(${NAME} PRIVATE
${CMAKE_CURRENT_LIST_DIR}/../include
)
target_link_libraries(${NAME}
picofuse
uc8151
)

# enable usb output, disable uart output
pico_enable_stdio_usb(${NAME} 1)
pico_enable_stdio_uart(${NAME} 0)

# create map/bin/hex/uf2 file etc.
pico_add_extra_outputs(${NAME})
32 changes: 32 additions & 0 deletions examples/blink/run.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <picofuse/picofuse.h>

#define LED_DELAY_MS 250

/* @brief LED Callback
*/
void led_callback(fuse_t *self, fuse_event_t *evt, void *user_data)
{
assert(self);
assert(evt);

// LED is the user_data field
fuse_led_t *led = (fuse_led_t *)user_data;

// Blink the LED
fuse_led_set(led, !fuse_led_get(led));
}

int run(fuse_t *self)
{
// LED output
fuse_led_t *led = (fuse_led_t *)fuse_retain(self, fuse_new_led(self));
assert(led);

// Register timer callback on core 0
assert(fuse_register_callback(self, FUSE_EVENT_TIMER, 0, led_callback));

// Schedule timer to run every second
assert(fuse_timer_schedule(self, LED_DELAY_MS, true, led));

return 0;
}
80 changes: 80 additions & 0 deletions examples/bme280/run.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <picofuse/picofuse.h>

#define GPIO_SPI_SCK 2 // SCK = GPIO2, Physical pin 4
#define GPIO_SPI_TX 3 // TX = GPIO3, Physical pin 5
#define GPIO_SPI_RX 4 // RX = GPIO4, Physical pin 6
#define GPIO_SPI_CS 5 // CS = GPIO5, Physical pin 7

/* @brief Callback when there is a measurement from the BME280
*/
void bme280_callback(fuse_t *self, fuse_event_t *evt, void* user_data)
{
assert(self);
assert(evt);
assert(user_data);

// Print the measurement
fuse_bme280_measurement_t* meas = (fuse_bme280_measurement_t*)user_data;
fuse_printf(self, "Temp: %f\n",meas->temperature);
if (meas->pressure) {
fuse_printf(self, "Pressure: %f\n",meas->pressure);
}
if (meas->humidity) {
fuse_printf(self, "Humidity: %f\n",meas->humidity);
}
}

/* @brief Callback for printing out memory stats
*/
void stats_callback(fuse_t *self, fuse_event_t *evt, void* user_data)
{
assert(self);
assert(evt);

// user_data should be NULL
if (user_data) {
return;
}

// Print the stats
size_t cur, max;
fuse_memstats(self, &cur, &max);

// Print the stats
fuse_printf(self, "Memory: %u bytes/%u bytes\n", cur, max);
}

int run(fuse_t *self)
{
// Create an SPI interface
fuse_spi_data_t config = {
.cs = GPIO_SPI_CS,
.ck = GPIO_SPI_SCK,
.tx = GPIO_SPI_TX,
.rx = GPIO_SPI_RX,
.baudrate = 500 * 1000, // This example will use SPI0 at 0.5MHz
.xfr_delayms = 10,
};

// Create a new BME280
fuse_bme280_t* bme280 = (fuse_bme280_t* )fuse_retain(self, fuse_new_bme280(self, fuse_new_spi(self, config)));
assert(bme280);

// Print driver details
fuse_printf(self, "bme280 initialized: %v\n", bme280);

// Register a callback on core 0 for the measured data
assert(fuse_register_callback(self, FUSE_EVENT_BME280, 0, bme280_callback));

// Register a callback on core 0 for timer
assert(fuse_register_callback(self, FUSE_EVENT_TIMER, 0, stats_callback));

// Read measurements every 10 secs
fuse_bme280_read(self, bme280, 10 * 1000);

// Timer to output the memory stats every minute
fuse_timer_t* timer = fuse_timer_schedule(self, 27 * 1000, true, NULL);
assert(timer);

return 0;
}
File renamed without changes.
58 changes: 58 additions & 0 deletions examples/fade/run.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <picofuse/picofuse.h>
#include <pico/stdlib.h>

/* @brief Callback when there is a wrap-around on the PWM counter
*/
void pwm_callback(fuse_t *self, fuse_event_t *evt, void *user_data)
{
assert(self);
assert(evt);

static int fade = 0;
static bool going_up = true;

if (going_up)
{
++fade;
if (fade > 0xFF)
{
fade = 0xFF;
going_up = false;
}
}
else
{
--fade;
if (fade < 1)
{
fade = 1;
going_up = true;
}
}

fuse_pwm_set_duty_cycle_b(self, (fuse_pwm_t *)user_data, fade);
}

int run(fuse_t *self)
{
fuse_pwm_config_t pwm_config = {
.gpio_b = PICO_DEFAULT_LED_PIN,
.duty_cycle_b = 0xFF,
.freq = 512 * 1000,
.event = true,
};
fuse_watchdog_config_t watchdog_config = {};

// Watchdog
fuse_watchdog_t *watchdog = (fuse_watchdog_t *)fuse_retain(self, fuse_new_watchdog(self, watchdog_config));
assert(watchdog);
fuse_debugf(self, "WATCHDOG: %v\n", watchdog);

// PWM
fuse_pwm_t *pwm = (fuse_pwm_t *)fuse_retain(self, fuse_new_pwm(self, pwm_config));
assert(pwm);
fuse_register_callback(self, FUSE_EVENT_PWM, 0, pwm_callback);
fuse_debugf(self, "PWM: %v\n", pwm);

return 0;
}
22 changes: 0 additions & 22 deletions examples/fuse/CMakeLists.txt

This file was deleted.

3 changes: 0 additions & 3 deletions examples/picofuse/gpio_in/run.c → examples/gpio_in/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ void gpio_callback(fuse_t *self, fuse_event_t *evt, void *user_data)

int run(fuse_t *self)
{
// Initialize picofuse
picofuse_init(self);

// GPIO inputs
assert(fuse_retain(self, fuse_new_gpio(self, 23, FUSE_GPIO_IN)));
assert(fuse_retain(self, fuse_new_gpio(self, 12, FUSE_GPIO_PULLUP)));
Expand Down
File renamed without changes.
Loading

0 comments on commit 619dda1

Please sign in to comment.