Skip to content

Commit 60829a1

Browse files
authored
Make examples friendlier for non GCC compilers and fixed slave_mem_i2c.c to compile on boards without I2C pins (raspberrypi#336)
1 parent 9d3fea1 commit 60829a1

File tree

39 files changed

+89
-86
lines changed

39 files changed

+89
-86
lines changed

CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ add_subdirectory(hello_world)
2828
add_compile_options(-Wall
2929
-Wno-format # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
3030
-Wno-unused-function # we have some for the docs that aren't called
31-
-Wno-maybe-uninitialized
3231
)
32+
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
33+
add_compile_options(-Wno-maybe-uninitialized)
34+
endif()
3335

3436
# Hardware-specific examples in subdirectories:
3537
add_subdirectory(adc)

adc/adc_console/adc_console.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int main(void) {
7272
printf("%03x\n", sample_buf[i]);
7373
break;
7474
}
75-
case 'w':
75+
case 'w': {
7676
printf("\nPress any key to stop wiggling\n");
7777
int i = 1;
7878
gpio_set_dir_all_bits(-1);
@@ -85,6 +85,7 @@ int main(void) {
8585
gpio_set_dir_all_bits(0);
8686
printf("Wiggling halted.\n");
8787
break;
88+
}
8889
case '\n':
8990
case '\r':
9091
break;

adc/microphone_adc/microphone_adc.c

-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,4 @@ int main() {
4444
printf("%.2f\n", adc_raw * ADC_CONVERT);
4545
sleep_ms(10);
4646
}
47-
48-
return 0;
4947
}

adc/onboard_temperature/onboard_temperature.c

-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,4 @@ int main() {
5858
#endif
5959
sleep_ms(990);
6060
}
61-
62-
return 0;
6361
}

flash/cache_perfctr/flash_cache_perfctr.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ int main() {
7979
printf("Hit rate so far: %.1f%%\n", hit * 100.f / access);
8080

8181
printf("Calculate 25th fibonacci number: %d\n", recursive_fibonacci(25));
82-
printf("New hit rate after printf and fibonacci: %.1f%%\n", xip_ctrl_hw->ctr_hit * 100.f / xip_ctrl_hw->ctr_acc);
82+
uint32_t ctr_hit = xip_ctrl_hw->ctr_hit;
83+
uint32_t ctr_acc = xip_ctrl_hw->ctr_acc;
84+
printf("New hit rate after printf and fibonacci: %.1f%%\n", ctr_hit * 100.f / ctr_acc);
8385

8486
check_hit_miss_invalidate();
8587

flash/nuke/nuke.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//
1818
// To the CMakeLists.txt app for this file. Just to be sure, we can check the
1919
// define:
20-
#if !PICO_NO_FLASH
20+
#if !PICO_NO_FLASH && !PICO_COPY_TO_RAM
2121
#error "This example must be built to run from SRAM!"
2222
#endif
2323

gpio/hello_7segment/hello_7segment.c

-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,5 @@ int main() {
8989
sleep_ms(250);
9090
gpio_clr_mask(mask);
9191
}
92-
93-
return 0;
9492
}
9593
/// \end::hello_gpio[]

gpio/hello_gpio_irq/hello_gpio_irq.c

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ int main() {
2727

2828
// Wait forever
2929
while (1);
30-
31-
return 0;
3230
}
3331

3432

hello_world/serial/hello_serial.c

-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ int main() {
1313
printf("Hello, world!\n");
1414
sleep_ms(1000);
1515
}
16-
return 0;
1716
}

hello_world/usb/hello_usb.c

-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ int main() {
1313
printf("Hello, world!\n");
1414
sleep_ms(1000);
1515
}
16-
return 0;
1716
}

i2c/bmp280_i2c/bmp280_i2c.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ int main() {
214214
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
215215
#warning i2c / bmp280_i2c example requires a board with I2C pins
216216
puts("Default I2C pins were not defined");
217+
return 0;
217218
#else
218219
// useful information for picotool
219220
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() {
248249
// poll every 500ms
249250
sleep_ms(500);
250251
}
251-
252252
#endif
253-
return 0;
254253
}

i2c/ht16k33_i2c/ht16k33_i2c.c

-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,5 @@ int main() {
224224
ht16k33_set_blink(0);
225225

226226
goto again;
227-
228-
return 0;
229227
#endif
230228
}

i2c/lcd_1602_i2c/lcd_1602_i2c.c

-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,5 @@ int main() {
163163
lcd_clear();
164164
}
165165
}
166-
167-
return 0;
168166
#endif
169167
}

i2c/lis3dh_i2c/lis3dh_i2c.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ int main() {
122122
sleep_ms(500);
123123

124124
// Clear terminal
125-
printf("\e[1;1H\e[2J");
125+
printf("\033[1;1H\033[2J");
126126
}
127127
#endif
128-
return 0;
129128
}

i2c/mma8451_i2c/mma8451_i2c.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ int main() {
121121
sleep_ms(500);
122122

123123
// Clear terminal
124-
printf("\e[1;1H\e[2J");
124+
printf("\033[1;1H\033[2J");
125125
}
126126

127127
#endif

i2c/mpl3115a2_i2c/mpl3115a2_i2c.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,15 @@ void mpl3115a2_convert_fifo_batch(uint8_t start, volatile uint8_t buf[], struct
148148

149149
// 3 altitude registers: MSB (8 bits), CSB (8 bits) and LSB (4 bits, starting from MSB)
150150
// first two are integer bits (2's complement) and LSB is fractional bits -> makes 20 bit signed integer
151-
int32_t h = (int32_t) ((uint32_t) buf[start] << 24 | buf[start + 1] << 16 | buf[start + 2] << 8);
151+
int32_t h = (int32_t) buf[start] << 24;
152+
h |= (int32_t) buf[start + 1] << 16;
153+
h |= (int32_t) buf[start + 2] << 8;
152154
data->altitude = ((float)h) / 65536.f;
153155

154156
// 2 temperature registers: MSB (8 bits) and LSB (4 bits, starting from MSB)
155157
// first 8 are integer bits with sign and LSB is fractional bits -> 12 bit signed integer
156-
int16_t t = (int16_t) (((uint16_t) buf[start + 3]) << 8 | buf[start + 4]);
158+
int16_t t = (int16_t) buf[start + 3] << 8;
159+
t |= (int16_t) buf[start + 4];
157160
data->temperature = ((float)t) / 256.f;
158161
}
159162

@@ -162,6 +165,7 @@ int main() {
162165
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
163166
#warning i2c / mpl3115a2_i2c example requires a board with I2C pins
164167
puts("Default I2C pins were not defined");
168+
return 0;
165169
#else
166170
printf("Hello, MPL3115A2. Waiting for something to interrupt me!...\n");
167171

@@ -202,5 +206,4 @@ int main() {
202206
};
203207

204208
#endif
205-
return 0;
206209
}

i2c/mpu6050_i2c/mpu6050_i2c.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ int main() {
8282
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
8383
#warning i2c/mpu6050_i2c example requires a board with I2C pins
8484
puts("Default I2C pins were not defined");
85+
return 0;
8586
#else
8687
printf("Hello, MPU6050! Reading raw data from registers...\n");
8788

@@ -111,7 +112,5 @@ int main() {
111112

112113
sleep_ms(100);
113114
}
114-
115115
#endif
116-
return 0;
117116
}

i2c/pa1010d_i2c/pa1010d_i2c.c

+12-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525

2626
const int addr = 0x10;
27-
const int max_read = 250;
27+
#define MAX_READ 250
2828

2929
#ifdef i2c_default
3030

@@ -44,24 +44,24 @@ void pa1010d_parse_string(char output[], char protocol[]) {
4444
int p = com_index - output;
4545

4646
// Splits components of output sentence into array
47-
int no_of_fields = 14;
48-
int max_len = 15;
47+
#define NO_OF_FIELDS 14
48+
#define MAX_LEN 15
4949

5050
int n = 0;
5151
int m = 0;
5252

53-
char gps_data[no_of_fields][max_len];
53+
char gps_data[NO_OF_FIELDS][MAX_LEN];
5454
memset(gps_data, 0, sizeof(gps_data));
5555

5656
bool complete = false;
57-
while (output[p] != '$' && n < max_len && complete == false) {
57+
while (output[p] != '$' && n < MAX_LEN && complete == false) {
5858
if (output[p] == ',' || output[p] == '*') {
5959
n += 1;
6060
m = 0;
6161
} else {
6262
gps_data[n][m] = output[p];
6363
// Checks if sentence is complete
64-
if (m < no_of_fields) {
64+
if (m < NO_OF_FIELDS) {
6565
m++;
6666
} else {
6767
complete = true;
@@ -92,15 +92,15 @@ void pa1010d_parse_string(char output[], char protocol[]) {
9292
}
9393

9494
void pa1010d_read_raw(char numcommand[]) {
95-
uint8_t buffer[max_read];
95+
uint8_t buffer[MAX_READ];
9696

9797
int i = 0;
9898
bool complete = false;
9999

100-
i2c_read_blocking(i2c_default, addr, buffer, max_read, false);
100+
i2c_read_blocking(i2c_default, addr, buffer, MAX_READ, false);
101101

102102
// Convert bytes to characters
103-
while (i < max_read && complete == false) {
103+
while (i < MAX_READ && complete == false) {
104104
numcommand[i] = buffer[i];
105105
// Stop converting at end of message
106106
if (buffer[i] == 10 && buffer[i + 1] == 10) {
@@ -119,7 +119,7 @@ int main() {
119119
puts("Default I2C pins were not defined");
120120
#else
121121

122-
char numcommand[max_read];
122+
char numcommand[MAX_READ];
123123

124124
// Decide which protocols you would like to retrieve data from
125125
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() {
140140

141141
while (1) {
142142
// Clear array
143-
memset(numcommand, 0, max_read);
143+
memset(numcommand, 0, MAX_READ);
144144
// Read and re-format
145145
pa1010d_read_raw(numcommand);
146146
pa1010d_parse_string(numcommand, "GNRMC");
@@ -149,8 +149,7 @@ int main() {
149149
sleep_ms(1000);
150150

151151
// Clear terminal
152-
printf("\e[1;1H\e[2J");
152+
printf("\033[1;1H\033[2J");
153153
}
154154
#endif
155-
return 0;
156155
}

i2c/pcf8523_i2c/pcf8523_i2c.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,8 @@ int main() {
156156
sleep_ms(500);
157157

158158
// Clear terminal
159-
printf("\e[1;1H\e[2J");
159+
printf("\033[1;1H\033[2J");
160160
}
161161
#endif
162-
return 0;
163162
}
164163

i2c/slave_mem_i2c/slave_mem_i2c.c

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
static const uint I2C_SLAVE_ADDRESS = 0x17;
1515
static const uint I2C_BAUDRATE = 100000; // 100 kHz
1616

17+
#ifdef i2c_default
1718
// For this example, we run both the master and slave from the same board.
1819
// You'll need to wire pin GP4 to GP6 (SDA), and pin GP5 to GP7 (SCL).
1920
static const uint I2C_SLAVE_SDA_PIN = PICO_DEFAULT_I2C_SDA_PIN; // 4
@@ -124,11 +125,18 @@ static void run_master() {
124125
sleep_ms(2000);
125126
}
126127
}
128+
#endif
127129

128130
int main() {
129131
stdio_init_all();
132+
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
133+
#warning i2c / slave_mem_i2c example requires a board with I2C pins
134+
puts("Default I2C pins were not defined");
135+
return 0;
136+
#else
130137
puts("\nI2C slave example");
131138

132139
setup_slave();
133140
run_master();
141+
#endif
134142
}

interp/hello_interp/hello_interp.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ void cross_lanes() {
5858
interp0->base[0] = 1;
5959
interp0->base[1] = 0;
6060
puts("Lane result crossover:");
61-
for (int i = 0; i < 10; ++i)
62-
printf("PEEK0, POP1: %d, %d\n", interp0->peek[0], interp0->pop[1]);
61+
for (int i = 0; i < 10; ++i) {
62+
uint32_t peek0 = interp0->peek[0];
63+
uint32_t pop1 = interp0->pop[1];
64+
printf("PEEK0, POP1: %d, %d\n", peek0, pop1);
65+
}
6366
}
6467

6568
void simple_blend1() {
@@ -107,7 +110,7 @@ void simple_blend2() {
107110
interp_config_set_blend(&cfg, true);
108111
interp_set_config(interp0, 0, &cfg);
109112

110-
interp0->base[0] = -1000;
113+
interp0->base[0] = (uint32_t) -1000;
111114
interp0->base[1] = 1000;
112115

113116
puts("signed:");
@@ -178,8 +181,10 @@ void linear_interpolation() {
178181
int16_t *sample_pair = (int16_t *) interp0->peek[2];
179182
interp0->base[0] = sample_pair[0];
180183
interp0->base[1] = sample_pair[1];
181-
printf("%d\t(%d%% between %d and %d)\n", (int) interp0->peek[1],
182-
100 * (interp0->add_raw[1] & 0xff) / 0xff,
184+
uint32_t peek1 = interp0->peek[1];
185+
uint32_t add_raw1 = interp0->add_raw[1];
186+
printf("%d\t(%d%% between %d and %d)\n", (int) peek1,
187+
100 * (add_raw1 & 0xff) / 0xff,
183188
sample_pair[0], sample_pair[1]);
184189
interp0->add_raw[0] = step;
185190
}

multicore/multicore_runner_queue/multicore_runner_queue.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
typedef struct
1515
{
16-
void *func;
16+
int32_t (*func)(int32_t);
1717
int32_t data;
1818
} queue_entry_t;
1919

@@ -31,8 +31,7 @@ void core1_entry() {
3131

3232
queue_remove_blocking(&call_queue, &entry);
3333

34-
int32_t (*func)() = (int32_t(*)())(entry.func);
35-
int32_t result = (*func)(entry.data);
34+
int32_t result = entry.func(entry.data);
3635

3736
queue_add_blocking(&results_queue, &result);
3837
}
@@ -78,7 +77,7 @@ int main() {
7877

7978
multicore_launch_core1(core1_entry);
8079

81-
queue_entry_t entry = {&factorial, TEST_NUM};
80+
queue_entry_t entry = {factorial, TEST_NUM};
8281
queue_add_blocking(&call_queue, &entry);
8382

8483
// We could now do a load of stuff on core 0 and get our result later
@@ -88,7 +87,7 @@ int main() {
8887
printf("Factorial %d is %d\n", TEST_NUM, res);
8988

9089
// Now try a different function
91-
entry.func = &fibonacci;
90+
entry.func = fibonacci;
9291
queue_add_blocking(&call_queue, &entry);
9392

9493
queue_remove_blocking(&results_queue, &res);

0 commit comments

Comments
 (0)