fixup examples based on default pin #define-s and also support them being undefined

This commit is contained in:
graham sanderson 2021-02-28 09:00:36 -06:00 committed by Graham Sanderson
parent 9c7e31b8e7
commit 82b6dc0576
10 changed files with 101 additions and 43 deletions

View File

@ -52,6 +52,9 @@ void dma_handler() {
}
int main() {
#ifndef PICO_DEFAULT_LED_PIN
#warning dma/channel_irq example requires a board with a regular LED
#else
// Set up a PIO state machine to serialise our bits
uint offset = pio_add_program(pio0, &pio_serialiser_program);
pio_serialiser_program_init(pio0, 0, offset, PICO_DEFAULT_LED_PIN, PIO_SERIAL_CLKDIV);
@ -87,5 +90,5 @@ int main() {
// time to sit and think about its early retirement -- maybe open a bakery?
while (true)
tight_loop_contents();
#endif
}

View File

@ -47,6 +47,9 @@ const struct {uint32_t len; const char *data;} control_blocks[] = {
};
int main() {
#ifndef uart_default
#warning dma/control_blocks example requires a UART
#else
stdio_init_all();
puts("DMA control block example:");
@ -81,7 +84,7 @@ int main() {
c = dma_channel_get_default_config(data_chan);
channel_config_set_transfer_data_size(&c, DMA_SIZE_8);
channel_config_set_dreq(&c, DREQ_UART0_TX + 2 * PICO_DEFAULT_UART);
channel_config_set_dreq(&c, DREQ_UART0_TX + 2 * uart_get_index(uart_default));
// Trigger ctrl_chan when data_chan completes
channel_config_set_chain_to(&c, ctrl_chan);
// Raise the IRQ flag when 0 is written to a trigger register (end of chain):
@ -90,7 +93,7 @@ int main() {
dma_channel_configure(
data_chan,
&c,
&(PICO_DEFAULT_UART ? uart1_hw : uart0_hw)->dr,
&uart_get_hw(uart_default)->dr,
NULL, // Initial read address and transfer count are unimportant;
0, // the control channel will reprogram them each time.
false // Don't start yet.
@ -108,4 +111,5 @@ int main() {
dma_hw->ints0 = 1u << data_chan;
puts("DMA finished.");
#endif
}

View File

@ -26,12 +26,20 @@
#include "pico/bootrom.h"
int main() {
flash_range_erase(0, PICO_FLASH_SIZE_BYTES);
uint flash_size_bytes;
#ifndef PICO_FLASH_SIZE_BYTES
#warning PICO_FLASH_SIZE_BYTES not set, assuming 16M
flash_size_bytes = 16 * 1024 * 1024;
#else
flash_size_bytes = PICO_FLASH_SIZE_BYTES;
#endif
flash_range_erase(0, flash_size_bytes);
// Leave an eyecatcher pattern in the first page of flash so picotool can
// more easily check the size:
static const uint8_t eyecatcher[FLASH_PAGE_SIZE] = "NUKE";
flash_range_program(0, eyecatcher, FLASH_PAGE_SIZE);
#ifdef PICO_DEFAULT_LED_PIN
// Flash LED for success
gpio_init(PICO_DEFAULT_LED_PIN);
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
@ -41,6 +49,7 @@ int main() {
gpio_put(PICO_DEFAULT_LED_PIN, 0);
sleep_ms(100);
}
#endif
// Pop back up as an MSD drive
reset_usb_boot(0, 0);

View File

@ -9,7 +9,10 @@
#include "pico/stdlib.h"
#include "hardware/gpio.h"
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
#ifdef PICO_DEFAULT_LED_PIN
#define LED_PIN PICO_DEFAULT_LED_PIN
#endif
const uint DHT_PIN = 15;
const uint MAX_TIMINGS = 85;
@ -22,9 +25,11 @@ void read_from_dht(dht_reading *result);
int main() {
stdio_init_all();
gpio_init(LED_PIN);
gpio_init(DHT_PIN);
#ifdef LED_PIN
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
#endif
while (1) {
dht_reading reading;
read_from_dht(&reading);
@ -46,7 +51,9 @@ void read_from_dht(dht_reading *result) {
sleep_ms(20);
gpio_set_dir(DHT_PIN, GPIO_IN);
#ifdef LED_PIN
gpio_put(LED_PIN, 1);
#endif
for (uint i = 0; i < MAX_TIMINGS; i++) {
uint count = 0;
while (gpio_get(DHT_PIN) == last) {
@ -63,7 +70,9 @@ void read_from_dht(dht_reading *result) {
j++;
}
}
#ifdef LED_PIN
gpio_put(LED_PIN, 0);
#endif
if ((j >= 40) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) {
result->humidity = (float) ((data[0] << 8) + data[1]) / 10;

View File

@ -22,6 +22,7 @@
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/i2c.h"
// I2C reserves some addresses for special purposes. We exclude these from the scan.
@ -33,13 +34,18 @@ bool reserved_addr(uint8_t addr) {
int main() {
// Enable UART so we can print status output
stdio_init_all();
// This example will use I2C0 on GPIO4 (SDA) and GPIO5 (SCL)
i2c_init(i2c0, 100 * 1000);
gpio_set_function(4, GPIO_FUNC_I2C);
gpio_set_function(5, GPIO_FUNC_I2C);
gpio_pull_up(4);
gpio_pull_up(5);
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
#warning i2c/bus_scane example requires a board with I2C pins
puts("Default I2C pins were not defined");
#else
// This example will use I2C0 on the default SDA and SCL pins (4, 5 on a Pico)
i2c_init(i2c_default, 100 * 1000);
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
// Make the I2C pins available to picotool
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));
printf("\nI2C Bus Scan\n");
printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
@ -67,4 +73,5 @@ int main() {
}
printf("Done.\n");
return 0;
#endif
}

View File

@ -58,7 +58,6 @@ const int LCD_BACKLIGHT = 0x08;
const int LCD_ENABLE_BIT = 0x04;
#define I2C_PORT i2c0
// By default these LCD display drivers are on bus address 0x27
static int addr = 0x27;
@ -71,7 +70,9 @@ static int addr = 0x27;
/* Quick helper function for single byte transfers */
void i2c_write_byte(uint8_t val) {
i2c_write_blocking(I2C_PORT, addr, &val, 1, false);
#ifdef i2c_default
i2c_write_blocking(i2c_default, addr, &val, 1, false);
#endif
}
void lcd_toggle_enable(uint8_t val) {
@ -129,14 +130,17 @@ void lcd_init() {
}
int main() {
// This example will use I2C0 on GPIO4 (SDA) and GPIO5 (SCL)
i2c_init(I2C_PORT, 100 * 1000);
gpio_set_function(4, GPIO_FUNC_I2C);
gpio_set_function(5, GPIO_FUNC_I2C);
gpio_pull_up(4);
gpio_pull_up(5);
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
#warning i2c/bus_scane example requires a board with I2C pins
#else
// This example will use I2C0 on the default SDA and SCL pins (4, 5 on a Pico)
i2c_init(i2c_default, 100 * 1000);
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
// Make the I2C pins available to picotool
bi_decl( bi_2pins_with_func(4, 5, GPIO_FUNC_I2C));
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));
lcd_init();
@ -161,4 +165,5 @@ int main() {
}
return 0;
#endif
}

View File

@ -7,6 +7,7 @@
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/i2c.h"
/* Example code to talk to a MPU6050 MEMS accelerometer and gyroscope
@ -23,8 +24,8 @@
Connections on Raspberry Pi Pico board, other boards may vary.
GPIO 4 (pin 6)-> SDA on MPU6050 board
GPIO 5 (pin 7)-> SCL on MPU6050 board
GPIO PICO_DEFAULT_I2C_SDA_PIN (On Pico this is 4 (pin 6)) -> SDA on MPU6050 board
GPIO PICO_DEFAULT_I2C_SCK_PIN (On Pico this is 5 (pin 7)) -> SCL on MPU6050 board
3.3v (pin 36) -> VCC on MPU6050 board
GND (pin 38) -> GND on MPU6050 board
*/
@ -32,13 +33,12 @@
// By default these devices are on bus address 0x68
static int addr = 0x68;
#define I2C_PORT i2c0
#ifdef i2c_default
static void mpu6050_reset() {
// Two byte reset. First byte register, second byte data
// There are a load more options to set up the device in different ways that could be added here
uint8_t buf[] = {0x6B, 0x00};
i2c_write_blocking(I2C_PORT, addr, buf, 2, false);
i2c_write_blocking(i2c_default, addr, buf, 2, false);
}
static void mpu6050_read_raw(int16_t accel[3], int16_t gyro[3], int16_t *temp) {
@ -50,8 +50,8 @@ static void mpu6050_read_raw(int16_t accel[3], int16_t gyro[3], int16_t *temp) {
// Start reading acceleration registers from register 0x3B for 6 bytes
uint8_t val = 0x3B;
i2c_write_blocking(I2C_PORT, addr, &val, 1, true); // true to keep master control of bus
i2c_read_blocking(I2C_PORT, addr, buffer, 6, false);
i2c_write_blocking(i2c_default, addr, &val, 1, true); // true to keep master control of bus
i2c_read_blocking(i2c_default, addr, buffer, 6, false);
for (int i = 0; i < 3; i++) {
accel[i] = (buffer[i * 2] << 8 | buffer[(i * 2) + 1]);
@ -60,8 +60,8 @@ static void mpu6050_read_raw(int16_t accel[3], int16_t gyro[3], int16_t *temp) {
// Now gyro data from reg 0x43 for 6 bytes
// The register is auto incrementing on each read
val = 0x43;
i2c_write_blocking(I2C_PORT, addr, &val, 1, true);
i2c_read_blocking(I2C_PORT, addr, buffer, 6, false); // False - finished with bus
i2c_write_blocking(i2c_default, addr, &val, 1, true);
i2c_read_blocking(i2c_default, addr, buffer, 6, false); // False - finished with bus
for (int i = 0; i < 3; i++) {
gyro[i] = (buffer[i * 2] << 8 | buffer[(i * 2) + 1]);;
@ -70,23 +70,29 @@ static void mpu6050_read_raw(int16_t accel[3], int16_t gyro[3], int16_t *temp) {
// Now temperature from reg 0x41 for 2 bytes
// The register is auto incrementing on each read
val = 0x41;
i2c_write_blocking(I2C_PORT, addr, &val, 1, true);
i2c_read_blocking(I2C_PORT, addr, buffer, 2, false); // False - finished with bus
i2c_write_blocking(i2c_default, addr, &val, 1, true);
i2c_read_blocking(i2c_default, addr, buffer, 2, false); // False - finished with bus
*temp = buffer[0] << 8 | buffer[1];
}
#endif
int main() {
stdio_init_all();
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
#warning i2c/bus_scane example requires a board with I2C pins
puts("Default I2C pins were not defined');
#else
printf("Hello, MPU6050! Reading raw data from registers...\n");
// This example will use I2C0 on GPIO4 (SDA) and GPIO5 (SCL) running at 400kHz.
i2c_init(I2C_PORT, 400 * 1000);
gpio_set_function(4, GPIO_FUNC_I2C);
gpio_set_function(5, GPIO_FUNC_I2C);
gpio_pull_up(4);
gpio_pull_up(5);
// This example will use I2C0 on the default SDA and SCL pins (4, 5 on a Pico)
i2c_init(i2c_default, 400 * 1000);
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
// Make the I2C pins available to picotool
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));
mpu6050_reset();
@ -106,5 +112,6 @@ int main() {
sleep_ms(100);
}
#endif
return 0;
}

View File

@ -11,7 +11,7 @@
#include "hardware/structs/ioqspi.h"
#include "hardware/structs/sio.h"
// This example blinks the Picoboard LED when the BOOTSEL button is pressed.
// This example blinks the Pico LED when the BOOTSEL button is pressed.
//
// Picoboard has a button attached to the flash CS pin, which the bootrom
// checks, and jumps straight to the USB bootcode if the button is pressed
@ -53,10 +53,14 @@ bool __no_inline_not_in_flash_func(get_bootsel_button)() {
}
int main() {
#ifndef PICO_DEFAULT_LED_PIN
#warning picobooard/button example requires a board with a regular LED
#else
gpio_init(PICO_DEFAULT_LED_PIN);
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
while (true) {
gpio_put(PICO_DEFAULT_LED_PIN, get_bootsel_button());
gpio_put(PICO_DEFAULT_LED_PIN, get_bootsel_button() ^ PICO_DEFAULT_LED_PIN_INVERTED);
sleep_ms(10);
}
#endif
}

View File

@ -10,6 +10,9 @@
#include "hello.pio.h"
int main() {
#ifndef PICO_DEFAULT_LED_PIN
#warning pio/hello_pio example requires a board with a regular LED
#else
// Choose which PIO instance to use (there are two instances)
PIO pio = pio0;
@ -35,4 +38,5 @@ int main() {
pio_sm_put_blocking(pio, sm, 0);
sleep_ms(500);
}
#endif
}

View File

@ -13,6 +13,7 @@
#include "hardware/irq.h"
#include "hardware/pwm.h"
#ifdef PICO_DEFAULT_LED_PIN
void on_pwm_wrap() {
static int fade = 0;
static bool going_up = true;
@ -36,8 +37,12 @@ void on_pwm_wrap() {
// Note this range matches with the wrap value
pwm_set_gpio_level(PICO_DEFAULT_LED_PIN, fade * fade);
}
#endif
int main() {
#ifndef PICO_DEFAULT_LED_PIN
#warning pwm/led_fade example requires a board with a regular LED
#else
// Tell the LED pin that the PWM is in charge of its value.
gpio_set_function(PICO_DEFAULT_LED_PIN, GPIO_FUNC_PWM);
// Figure out which slice we just connected to the LED pin
@ -62,4 +67,5 @@ int main() {
// can twiddle our thumbs
while (1)
tight_loop_contents();
#endif
}