Make examples friendlier for non GCC compilers and fixed slave_mem_i2c.c to compile on boards without I2C pins (#336)

This commit is contained in:
Graham Sanderson 2023-03-10 15:41:40 -06:00 committed by GitHub
parent 9d3fea1419
commit 60829a134a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
39 changed files with 89 additions and 86 deletions

View File

@ -28,8 +28,10 @@ add_subdirectory(hello_world)
add_compile_options(-Wall
-Wno-format # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
-Wno-unused-function # we have some for the docs that aren't called
-Wno-maybe-uninitialized
)
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wno-maybe-uninitialized)
endif()
# Hardware-specific examples in subdirectories:
add_subdirectory(adc)

View File

@ -72,7 +72,7 @@ int main(void) {
printf("%03x\n", sample_buf[i]);
break;
}
case 'w':
case 'w': {
printf("\nPress any key to stop wiggling\n");
int i = 1;
gpio_set_dir_all_bits(-1);
@ -85,6 +85,7 @@ int main(void) {
gpio_set_dir_all_bits(0);
printf("Wiggling halted.\n");
break;
}
case '\n':
case '\r':
break;

View File

@ -44,6 +44,4 @@ int main() {
printf("%.2f\n", adc_raw * ADC_CONVERT);
sleep_ms(10);
}
return 0;
}

View File

@ -58,6 +58,4 @@ int main() {
#endif
sleep_ms(990);
}
return 0;
}

View File

@ -79,7 +79,9 @@ int main() {
printf("Hit rate so far: %.1f%%\n", hit * 100.f / access);
printf("Calculate 25th fibonacci number: %d\n", recursive_fibonacci(25));
printf("New hit rate after printf and fibonacci: %.1f%%\n", xip_ctrl_hw->ctr_hit * 100.f / xip_ctrl_hw->ctr_acc);
uint32_t ctr_hit = xip_ctrl_hw->ctr_hit;
uint32_t ctr_acc = xip_ctrl_hw->ctr_acc;
printf("New hit rate after printf and fibonacci: %.1f%%\n", ctr_hit * 100.f / ctr_acc);
check_hit_miss_invalidate();

View File

@ -17,7 +17,7 @@
//
// To the CMakeLists.txt app for this file. Just to be sure, we can check the
// define:
#if !PICO_NO_FLASH
#if !PICO_NO_FLASH && !PICO_COPY_TO_RAM
#error "This example must be built to run from SRAM!"
#endif

View File

@ -89,7 +89,5 @@ int main() {
sleep_ms(250);
gpio_clr_mask(mask);
}
return 0;
}
/// \end::hello_gpio[]

View File

@ -27,8 +27,6 @@ int main() {
// Wait forever
while (1);
return 0;
}

View File

@ -13,5 +13,4 @@ int main() {
printf("Hello, world!\n");
sleep_ms(1000);
}
return 0;
}

View File

@ -13,5 +13,4 @@ int main() {
printf("Hello, world!\n");
sleep_ms(1000);
}
return 0;
}

View File

@ -214,6 +214,7 @@ int main() {
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
#warning i2c / bmp280_i2c example requires a board with I2C pins
puts("Default I2C pins were not defined");
return 0;
#else
// useful information for picotool
bi_decl(bi_2pins_with_func(PICO_DEFAULT_I2C_SDA_PIN, PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C));
@ -248,7 +249,5 @@ int main() {
// poll every 500ms
sleep_ms(500);
}
#endif
return 0;
}

View File

@ -224,7 +224,5 @@ again:
ht16k33_set_blink(0);
goto again;
return 0;
#endif
}

View File

@ -163,7 +163,5 @@ int main() {
lcd_clear();
}
}
return 0;
#endif
}

View File

@ -122,8 +122,7 @@ int main() {
sleep_ms(500);
// Clear terminal
printf("\e[1;1H\e[2J");
printf("\033[1;1H\033[2J");
}
#endif
return 0;
}

View File

@ -121,7 +121,7 @@ int main() {
sleep_ms(500);
// Clear terminal
printf("\e[1;1H\e[2J");
printf("\033[1;1H\033[2J");
}
#endif

View File

@ -148,12 +148,15 @@ void mpl3115a2_convert_fifo_batch(uint8_t start, volatile uint8_t buf[], struct
// 3 altitude registers: MSB (8 bits), CSB (8 bits) and LSB (4 bits, starting from MSB)
// first two are integer bits (2's complement) and LSB is fractional bits -> makes 20 bit signed integer
int32_t h = (int32_t) ((uint32_t) buf[start] << 24 | buf[start + 1] << 16 | buf[start + 2] << 8);
int32_t h = (int32_t) buf[start] << 24;
h |= (int32_t) buf[start + 1] << 16;
h |= (int32_t) buf[start + 2] << 8;
data->altitude = ((float)h) / 65536.f;
// 2 temperature registers: MSB (8 bits) and LSB (4 bits, starting from MSB)
// first 8 are integer bits with sign and LSB is fractional bits -> 12 bit signed integer
int16_t t = (int16_t) (((uint16_t) buf[start + 3]) << 8 | buf[start + 4]);
int16_t t = (int16_t) buf[start + 3] << 8;
t |= (int16_t) buf[start + 4];
data->temperature = ((float)t) / 256.f;
}
@ -162,6 +165,7 @@ int main() {
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
#warning i2c / mpl3115a2_i2c example requires a board with I2C pins
puts("Default I2C pins were not defined");
return 0;
#else
printf("Hello, MPL3115A2. Waiting for something to interrupt me!...\n");
@ -202,5 +206,4 @@ int main() {
};
#endif
return 0;
}

View File

@ -82,6 +82,7 @@ int main() {
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
#warning i2c/mpu6050_i2c example requires a board with I2C pins
puts("Default I2C pins were not defined");
return 0;
#else
printf("Hello, MPU6050! Reading raw data from registers...\n");
@ -111,7 +112,5 @@ int main() {
sleep_ms(100);
}
#endif
return 0;
}

View File

@ -24,7 +24,7 @@
*/
const int addr = 0x10;
const int max_read = 250;
#define MAX_READ 250
#ifdef i2c_default
@ -44,24 +44,24 @@ void pa1010d_parse_string(char output[], char protocol[]) {
int p = com_index - output;
// Splits components of output sentence into array
int no_of_fields = 14;
int max_len = 15;
#define NO_OF_FIELDS 14
#define MAX_LEN 15
int n = 0;
int m = 0;
char gps_data[no_of_fields][max_len];
char gps_data[NO_OF_FIELDS][MAX_LEN];
memset(gps_data, 0, sizeof(gps_data));
bool complete = false;
while (output[p] != '$' && n < max_len && complete == false) {
while (output[p] != '$' && n < MAX_LEN && complete == false) {
if (output[p] == ',' || output[p] == '*') {
n += 1;
m = 0;
} else {
gps_data[n][m] = output[p];
// Checks if sentence is complete
if (m < no_of_fields) {
if (m < NO_OF_FIELDS) {
m++;
} else {
complete = true;
@ -92,15 +92,15 @@ void pa1010d_parse_string(char output[], char protocol[]) {
}
void pa1010d_read_raw(char numcommand[]) {
uint8_t buffer[max_read];
uint8_t buffer[MAX_READ];
int i = 0;
bool complete = false;
i2c_read_blocking(i2c_default, addr, buffer, max_read, false);
i2c_read_blocking(i2c_default, addr, buffer, MAX_READ, false);
// Convert bytes to characters
while (i < max_read && complete == false) {
while (i < MAX_READ && complete == false) {
numcommand[i] = buffer[i];
// Stop converting at end of message
if (buffer[i] == 10 && buffer[i + 1] == 10) {
@ -119,7 +119,7 @@ int main() {
puts("Default I2C pins were not defined");
#else
char numcommand[max_read];
char numcommand[MAX_READ];
// Decide which protocols you would like to retrieve data from
char init_command[] = "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n";
@ -140,7 +140,7 @@ int main() {
while (1) {
// Clear array
memset(numcommand, 0, max_read);
memset(numcommand, 0, MAX_READ);
// Read and re-format
pa1010d_read_raw(numcommand);
pa1010d_parse_string(numcommand, "GNRMC");
@ -149,8 +149,7 @@ int main() {
sleep_ms(1000);
// Clear terminal
printf("\e[1;1H\e[2J");
printf("\033[1;1H\033[2J");
}
#endif
return 0;
}

View File

@ -156,9 +156,8 @@ int main() {
sleep_ms(500);
// Clear terminal
printf("\e[1;1H\e[2J");
printf("\033[1;1H\033[2J");
}
#endif
return 0;
}

View File

@ -14,6 +14,7 @@
static const uint I2C_SLAVE_ADDRESS = 0x17;
static const uint I2C_BAUDRATE = 100000; // 100 kHz
#ifdef i2c_default
// For this example, we run both the master and slave from the same board.
// You'll need to wire pin GP4 to GP6 (SDA), and pin GP5 to GP7 (SCL).
static const uint I2C_SLAVE_SDA_PIN = PICO_DEFAULT_I2C_SDA_PIN; // 4
@ -124,11 +125,18 @@ static void run_master() {
sleep_ms(2000);
}
}
#endif
int main() {
stdio_init_all();
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
#warning i2c / slave_mem_i2c example requires a board with I2C pins
puts("Default I2C pins were not defined");
return 0;
#else
puts("\nI2C slave example");
setup_slave();
run_master();
#endif
}

View File

@ -58,8 +58,11 @@ void cross_lanes() {
interp0->base[0] = 1;
interp0->base[1] = 0;
puts("Lane result crossover:");
for (int i = 0; i < 10; ++i)
printf("PEEK0, POP1: %d, %d\n", interp0->peek[0], interp0->pop[1]);
for (int i = 0; i < 10; ++i) {
uint32_t peek0 = interp0->peek[0];
uint32_t pop1 = interp0->pop[1];
printf("PEEK0, POP1: %d, %d\n", peek0, pop1);
}
}
void simple_blend1() {
@ -107,7 +110,7 @@ void simple_blend2() {
interp_config_set_blend(&cfg, true);
interp_set_config(interp0, 0, &cfg);
interp0->base[0] = -1000;
interp0->base[0] = (uint32_t) -1000;
interp0->base[1] = 1000;
puts("signed:");
@ -178,8 +181,10 @@ void linear_interpolation() {
int16_t *sample_pair = (int16_t *) interp0->peek[2];
interp0->base[0] = sample_pair[0];
interp0->base[1] = sample_pair[1];
printf("%d\t(%d%% between %d and %d)\n", (int) interp0->peek[1],
100 * (interp0->add_raw[1] & 0xff) / 0xff,
uint32_t peek1 = interp0->peek[1];
uint32_t add_raw1 = interp0->add_raw[1];
printf("%d\t(%d%% between %d and %d)\n", (int) peek1,
100 * (add_raw1 & 0xff) / 0xff,
sample_pair[0], sample_pair[1]);
interp0->add_raw[0] = step;
}

View File

@ -13,7 +13,7 @@
typedef struct
{
void *func;
int32_t (*func)(int32_t);
int32_t data;
} queue_entry_t;
@ -31,8 +31,7 @@ void core1_entry() {
queue_remove_blocking(&call_queue, &entry);
int32_t (*func)() = (int32_t(*)())(entry.func);
int32_t result = (*func)(entry.data);
int32_t result = entry.func(entry.data);
queue_add_blocking(&results_queue, &result);
}
@ -78,7 +77,7 @@ int main() {
multicore_launch_core1(core1_entry);
queue_entry_t entry = {&factorial, TEST_NUM};
queue_entry_t entry = {factorial, TEST_NUM};
queue_add_blocking(&call_queue, &entry);
// We could now do a load of stuff on core 0 and get our result later
@ -88,7 +87,7 @@ int main() {
printf("Factorial %d is %d\n", TEST_NUM, res);
// Now try a different function
entry.func = &fibonacci;
entry.func = fibonacci;
queue_add_blocking(&call_queue, &entry);
queue_remove_blocking(&results_queue, &res);

View File

@ -20,6 +20,10 @@
// Global brightness value 0->31
#define BRIGHTNESS 16
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
void put_start_frame(PIO pio, uint sm) {
pio_sm_put_blocking(pio, sm, 0u);
}
@ -50,7 +54,7 @@ int main() {
apa102_mini_program_init(pio, sm, offset, SERIAL_FREQ, PIN_CLK, PIN_DIN);
for (int i = 0; i < TABLE_SIZE; ++i)
wave_table[i] = powf(sinf(i * M_PI / TABLE_SIZE), 5.f) * 255;
wave_table[i] = (uint8_t) (powf(sinf(i * M_PI / TABLE_SIZE), 5.f) * 255);
uint t = 0;
while (true) {

View File

@ -110,8 +110,8 @@ static inline void i2c_program_init(PIO pio, uint sm, uint offset, uint pin_sda,
// Clear IRQ flag before starting, and make sure flag doesn't actually
// assert a system-level interrupt (we're using it as a status flag)
pio_set_irq0_source_enabled(pio, pis_interrupt0 + sm, false);
pio_set_irq1_source_enabled(pio, pis_interrupt0 + sm, false);
pio_set_irq0_source_enabled(pio, (enum pio_interrupt_source) ((uint) pis_interrupt0 + sm), false);
pio_set_irq1_source_enabled(pio, (enum pio_interrupt_source) ((uint) pis_interrupt0 + sm), false);
pio_interrupt_clear(pio, sm);
// Configure and start SM

View File

@ -33,10 +33,14 @@ static inline void pio_i2c_put16(PIO pio, uint sm, uint16_t data) {
while (pio_sm_is_tx_fifo_full(pio, sm))
;
// some versions of GCC dislike this
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#endif
*(io_rw_16 *)&pio->txf[sm] = data;
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
}
@ -48,10 +52,14 @@ void pio_i2c_put_or_err(PIO pio, uint sm, uint16_t data) {
if (pio_i2c_check_error(pio, sm))
return;
// some versions of GCC dislike this
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#endif
*(io_rw_16 *)&pio->txf[sm] = data;
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
}
uint8_t pio_i2c_get(PIO pio, uint sm) {

View File

@ -30,6 +30,10 @@
#define SERIAL_CLK_DIV 1.f
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// Format: cmd length (including cmd byte), post delay in units of 5 ms, then cmd payload
// Note the delays have been shortened a little
static const uint8_t st7789_init_seq[] = {
@ -130,8 +134,8 @@ int main() {
if (theta > theta_max)
theta -= theta_max;
int32_t rotate[4] = {
cosf(theta) * (1 << UNIT_LSB), -sinf(theta) * (1 << UNIT_LSB),
sinf(theta) * (1 << UNIT_LSB), cosf(theta) * (1 << UNIT_LSB)
(int32_t) (cosf(theta) * (1 << UNIT_LSB)), (int32_t) (-sinf(theta) * (1 << UNIT_LSB)),
(int32_t) (sinf(theta) * (1 << UNIT_LSB)), (int32_t) (cosf(theta) * (1 << UNIT_LSB))
};
interp0->base[0] = rotate[0];
interp0->base[1] = rotate[2];

View File

@ -83,9 +83,9 @@ void pattern_solid(uint len, uint t) {
}
}
int level = 8;
void pattern_fade(uint len, uint t) {
const int level = 8;
uint shift = 4;
uint max = 16; // let's not draw too much current!

View File

@ -64,7 +64,7 @@ int main() {
// values should be very close!
for (int i = 0; i < count_of(test_duty_cycles); ++i) {
float output_duty_cycle = test_duty_cycles[i];
pwm_set_gpio_level(OUTPUT_PIN, output_duty_cycle * (count_top + 1));
pwm_set_gpio_level(OUTPUT_PIN, (uint16_t) (output_duty_cycle * (count_top + 1)));
float measured_duty_cycle = measure_duty_cycle(MEASURE_PIN);
printf("Output duty cycle = %.1f%%, measured input duty cycle = %.1f%%\n",
output_duty_cycle * 100.f, measured_duty_cycle * 100.f);

View File

@ -43,7 +43,5 @@ int main() {
printf("\r%s ", datetime_str);
sleep_ms(100);
}
return 0;
}
/// \end::hello_rtc_main[]

View File

@ -56,6 +56,4 @@ int main() {
// Alarm will keep firing forever
while(1);
return 0;
}

View File

@ -235,7 +235,5 @@ int main() {
sleep_ms(1000);
}
return 0;
#endif
}

View File

@ -135,7 +135,5 @@ int main() {
bright++;
}
return 0;
#endif
}

View File

@ -142,7 +142,5 @@ int main() {
j = 0;
}
}
return 0;
#endif
}

View File

@ -150,6 +150,4 @@ int main() {
sleep_ms(100);
}
return 0;
}

View File

@ -37,8 +37,10 @@ int main() {
// on transfer size and address LSBs
printf("\nReading back 1 byte at a time\n");
// Little-endian!
printf("Should be ef be ad de: %02x %02x %02x %02x\n",
scratch8[0], scratch8[1], scratch8[2], scratch8[3]);
printf("Should be ef be ad de: %02x ", scratch8[0]);
printf("%02x ", scratch8[1]);
printf("%02x ", scratch8[2]);
printf("%02x\n", scratch8[3]);
// The Cortex-M0+ and the RP2040 DMA replicate byte writes across the bus,
// and IO registers will sample the entire write bus always.

View File

@ -151,7 +151,7 @@ int main() {
#if LCD_IS_RGB
uint8_t i = 0; // it's ok if this overflows and wraps, we're using sin
const float frequency = 0.1f;
float red, green, blue;
uint8_t red, green, blue;
#endif
while (1) {
@ -163,9 +163,9 @@ int main() {
if (c < 128) uart_putc_raw(UART_ID, c); // skip extra non-ASCII chars
#if LCD_IS_RGB
// change the display color on keypress, rainbow style!
red = sin(frequency * i + 0) * 127 + 128;
green = sin(frequency * i + 2) * 127 + 128;
blue = sin(frequency * i + 4) * 127 + 128;
red = (uint8_t)(sin(frequency * i + 0) * 127 + 128);
green = (uint8_t)(sin(frequency * i + 2) * 127 + 128);
blue = (uint8_t)(sin(frequency * i + 4) * 127 + 128);
lcd_set_backlight_color(red, green, blue);
i++;
#endif

View File

@ -9,8 +9,8 @@ else ()
message("Skipping TinyUSB host examples as TinyUSB is unavailable")
endif ()
if (TARGET tinyusb_pico_pio_usb)
if (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 11.3)
message("Skipping TinyUSB dual examples, as TinyUSB hw/mcu/raspberry_pi/Pico-PIO-USB does not currently compile on GCC 11.3 or greater")
if ((NOT CMAKE_C_COMPILER_ID STREQUAL "GNU") OR CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 11.3)
message("Skipping TinyUSB dual examples, as TinyUSB hw/mcu/raspberry_pi/Pico-PIO-USB does not currently compile on non GCC or GCC 11.3 or greater")
else()
add_subdirectory(dual)
endif()

View File

@ -65,8 +65,6 @@ int main(void)
hid_task();
}
return 0;
}
//--------------------------------------------------------------------+

View File

@ -26,8 +26,8 @@
// Device descriptors
#include "dev_lowlevel.h"
#define usb_hw_set hw_set_alias(usb_hw)
#define usb_hw_clear hw_clear_alias(usb_hw)
#define usb_hw_set ((usb_hw_t *)hw_set_alias_untyped(usb_hw))
#define usb_hw_clear ((usb_hw_t *)hw_clear_alias_untyped(usb_hw))
// Function prototypes for our device specific endpoint handlers defined
// later on
@ -575,6 +575,4 @@ int main(void) {
while (1) {
tight_loop_contents();
}
return 0;
}