Skip to content

Commit

Permalink
Make examples friendlier for non GCC compilers and fixed slave_mem_i2…
Browse files Browse the repository at this point in the history
…c.c to compile on boards without I2C pins (#336)
  • Loading branch information
kilograham authored Mar 10, 2023
1 parent 9d3fea1 commit 60829a1
Show file tree
Hide file tree
Showing 39 changed files with 89 additions and 86 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion adc/adc_console/adc_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -85,6 +85,7 @@ int main(void) {
gpio_set_dir_all_bits(0);
printf("Wiggling halted.\n");
break;
}
case '\n':
case '\r':
break;
Expand Down
2 changes: 0 additions & 2 deletions adc/microphone_adc/microphone_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,4 @@ int main() {
printf("%.2f\n", adc_raw * ADC_CONVERT);
sleep_ms(10);
}

return 0;
}
2 changes: 0 additions & 2 deletions adc/onboard_temperature/onboard_temperature.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,4 @@ int main() {
#endif
sleep_ms(990);
}

return 0;
}
4 changes: 3 additions & 1 deletion flash/cache_perfctr/flash_cache_perfctr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion flash/nuke/nuke.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions gpio/hello_7segment/hello_7segment.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,5 @@ int main() {
sleep_ms(250);
gpio_clr_mask(mask);
}

return 0;
}
/// \end::hello_gpio[]
2 changes: 0 additions & 2 deletions gpio/hello_gpio_irq/hello_gpio_irq.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ int main() {

// Wait forever
while (1);

return 0;
}


Expand Down
1 change: 0 additions & 1 deletion hello_world/serial/hello_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ int main() {
printf("Hello, world!\n");
sleep_ms(1000);
}
return 0;
}
1 change: 0 additions & 1 deletion hello_world/usb/hello_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ int main() {
printf("Hello, world!\n");
sleep_ms(1000);
}
return 0;
}
3 changes: 1 addition & 2 deletions i2c/bmp280_i2c/bmp280_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -248,7 +249,5 @@ int main() {
// poll every 500ms
sleep_ms(500);
}

#endif
return 0;
}
2 changes: 0 additions & 2 deletions i2c/ht16k33_i2c/ht16k33_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,5 @@ int main() {
ht16k33_set_blink(0);

goto again;

return 0;
#endif
}
2 changes: 0 additions & 2 deletions i2c/lcd_1602_i2c/lcd_1602_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,5 @@ int main() {
lcd_clear();
}
}

return 0;
#endif
}
3 changes: 1 addition & 2 deletions i2c/lis3dh_i2c/lis3dh_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion i2c/mma8451_i2c/mma8451_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ int main() {
sleep_ms(500);

// Clear terminal
printf("\e[1;1H\e[2J");
printf("\033[1;1H\033[2J");
}

#endif
Expand Down
9 changes: 6 additions & 3 deletions i2c/mpl3115a2_i2c/mpl3115a2_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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");

Expand Down Expand Up @@ -202,5 +206,4 @@ int main() {
};

#endif
return 0;
}
3 changes: 1 addition & 2 deletions i2c/mpu6050_i2c/mpu6050_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -111,7 +112,5 @@ int main() {

sleep_ms(100);
}

#endif
return 0;
}
25 changes: 12 additions & 13 deletions i2c/pa1010d_i2c/pa1010d_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

const int addr = 0x10;
const int max_read = 250;
#define MAX_READ 250

#ifdef i2c_default

Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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";
Expand All @@ -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");
Expand All @@ -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;
}
3 changes: 1 addition & 2 deletions i2c/pcf8523_i2c/pcf8523_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

8 changes: 8 additions & 0 deletions i2c/slave_mem_i2c/slave_mem_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
15 changes: 10 additions & 5 deletions interp/hello_interp/hello_interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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:");
Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 4 additions & 5 deletions multicore/multicore_runner_queue/multicore_runner_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

typedef struct
{
void *func;
int32_t (*func)(int32_t);
int32_t data;
} queue_entry_t;

Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 60829a1

Please sign in to comment.