Skip to content

MCUs: update deprecated use of term_from_int32#2241

Open
UncleGrumpy wants to merge 7 commits intoatomvm:release-0.7from
UncleGrumpy:update_adc_deprecations
Open

MCUs: update deprecated use of term_from_int32#2241
UncleGrumpy wants to merge 7 commits intoatomvm:release-0.7from
UncleGrumpy:update_adc_deprecations

Conversation

@UncleGrumpy
Copy link
Copy Markdown
Collaborator

@UncleGrumpy UncleGrumpy commented Mar 28, 2026

Update all MCU platforms useage of term_from_int32 to safer variants of term_from_int*

See also: #1897

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

@UncleGrumpy UncleGrumpy force-pushed the update_adc_deprecations branch 3 times, most recently from 90bf284 to 0bd3c10 Compare March 28, 2026 17:31
@UncleGrumpy UncleGrumpy changed the title ESP32 adc_driver.c: update deprecated APIs ESP32: update deprecated use of term_from_int32 Mar 28, 2026
@UncleGrumpy UncleGrumpy requested a review from pguyot March 28, 2026 17:35
Copy link
Copy Markdown
Collaborator Author

@UncleGrumpy UncleGrumpy Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All values encoded in adc_driver.c with term_from_int28 are 17 bits or less.

@UncleGrumpy UncleGrumpy removed the request for review from pguyot March 28, 2026 17:39
@UncleGrumpy UncleGrumpy force-pushed the update_adc_deprecations branch from 0bd3c10 to 793c199 Compare March 28, 2026 18:28
@UncleGrumpy UncleGrumpy requested a review from pguyot March 28, 2026 18:29
err_t delete_res = netconn_delete(socket_data->conn);

socket_data->conn = NULL;
struct ESP32PlatformData *platform = ctx->global->platform_data;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we, as a bonus, handle the warning here?

Copy link
Copy Markdown
Collaborator Author

@UncleGrumpy UncleGrumpy Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there may be a bug around there:

    struct ESP32PlatformData *platform = ctx->global->platform_data;
    synclist_remove(&platform->sockets, &socket_data->sockets_head);

It appears that struct ESP32PlatformData *platform is intended to be used. The same pattern emits a warning at lines 675-676. Is this warning just a false positive?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If SMP is disabled, platform is unused.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand now, thanks for the clarification.

term_set_map_assoc(ret, 1, globalcontext_make_atom(glb, features_atom), get_features(ctx, info.features));
term_set_map_assoc(ret, 2, globalcontext_make_atom(glb, model_atom), get_model(ctx, info.model));
term_set_map_assoc(ret, 3, globalcontext_make_atom(glb, revision_atom), term_from_int32(info.revision));
term_set_map_assoc(ret, 3, globalcontext_make_atom(glb, revision_atom), term_from_int11(info.revision));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add something like:

 _Static_assert(SOC_CPU_CORES_NUM <= 7, "core count may exceed term_from_int4 range");
 _Static_assert(CONFIG_ESP_REV_MAX_FULL <= 1023, "chip revision may not fit in term_from_int11"); 

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea, it will likely be many years (if ever) before these need to be updated, and this could end up slipping past unnoticed.

@UncleGrumpy UncleGrumpy force-pushed the update_adc_deprecations branch from 793c199 to 4bb7414 Compare March 29, 2026 15:49
@petermm
Copy link
Copy Markdown
Contributor

petermm commented Mar 30, 2026

Know this extends the scope - but the last remaining is below:

Remaining term_from_int32 call-sites

1. STM32 — gpio_driver.c

File: src/platforms/stm32/src/lib/gpio_driver.c:675

term_put_tuple_element(gpio_tuple, 1, term_from_int32((int32_t) gpio_pin));

Context: Converts an STM32 GPIO pin number to a term inside the interrupt callback.

Recommended replacement: term_from_int11
STM32 GPIO pin numbers are 0–15 per bank, well within 11 bits.
Matches the ESP32 gpio_driver.c fix already applied in this PR.

term_put_tuple_element(gpio_tuple, 1, term_from_int11((int16_t) gpio_pin));

2. RP2 — networkdriver.c (IP address octets)

File: src/platforms/rp2/src/lib/networkdriver.c:131-134

terms[0] = term_from_int32((addr >> 24) & 0xFF);
terms[1] = term_from_int32((addr >> 16) & 0xFF);
terms[2] = term_from_int32((addr >> 8) & 0xFF);
terms[3] = term_from_int32(addr & 0xFF);

Context: Builds a {A, B, C, D} IP address tuple. Each octet is 0–255.

Recommended replacement: term_from_int11
Each octet is an unsigned byte (0–255), fits in 11 bits. Alternatively
term_from_int works since these are small constants.

terms[0] = term_from_int11((addr >> 24) & 0xFF);
terms[1] = term_from_int11((addr >> 16) & 0xFF);
terms[2] = term_from_int11((addr >> 8) & 0xFF);
terms[3] = term_from_int11(addr & 0xFF);

3. RP2 — networkdriver.c (RSSI)

File: src/platforms/rp2/src/lib/networkdriver.c:718

term rssi = term_from_int32(sta_rssi);

Context: WiFi signal strength (RSSI). Typically −90 to 0 dBm, always a small signed integer.

Recommended replacement: term_from_int11
RSSI values are well within ±1024.

term rssi = term_from_int11((int16_t) sta_rssi);

@UncleGrumpy UncleGrumpy force-pushed the update_adc_deprecations branch 2 times, most recently from 064164e to 2dcde17 Compare March 30, 2026 17:31
@UncleGrumpy UncleGrumpy changed the title ESP32: update deprecated use of term_from_int32 MCUs: update deprecated use of term_from_int32 Mar 30, 2026
Update the use of deprecated `term_from_int32` to `term_from_int11` for
encoding gpio pin values. The values will fit in an int4, but the type
is uint16_t, so `term_to_int11` is used for correctness.

Signed-off-by: Winford <winford@object.stream>
Change all uses of the `term_from_int32` to `term_from_int11`, which is
of adequate size to contain all of the encoded terms.

Signed-off-by: Winford <winford@object.stream>
Replace use of `term_from_int32` other safe term_from_int* variants.

Signed-off-by: Winford <winford@object.stream>
Updates use of deprecated `term_from_int32` to `term_from_int` when
encoding the `send_timeout_ms` value.

Signed-off-by: Winford <winford@object.stream>
Update the use of `term_from_int32` to encode `port` (`u16_t`) values
to the new `term_from_int28`.

Signed-off-by: Winford <winford@object.stream>
Update the use of deprecated `term_from_int32` to `term_from_int11` for
encoding gpio pin values. The 11-bit value is more than enough to allow
for the number of pins available on any ESP32 for many years.

Signed-off-by: Winford <winford@object.stream>
Update deprecated `term_from_int32` to use `term_from_int28`.

See also: atomvm#1897

Signed-off-by: Winford <winford@object.stream>
@UncleGrumpy UncleGrumpy force-pushed the update_adc_deprecations branch from 2dcde17 to 9d2a026 Compare March 30, 2026 17:40
@UncleGrumpy
Copy link
Copy Markdown
Collaborator Author

This should take care of all of the remaining uses of term_from_int32.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants