Skip to content

Commit ebb1e15

Browse files
committed
tmp
1 parent 225b496 commit ebb1e15

File tree

7 files changed

+76
-19
lines changed

7 files changed

+76
-19
lines changed

ports/esp32/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ if(MICROPY_BOARD_VARIANT)
2525
endif()
2626
endif()
2727

28+
# set(MICROPY_MPY_TOOL_FLAGS -mlongint_impl longlong)
29+
2830
# Define the output sdkconfig so it goes in the build directory.
2931
set(SDKCONFIG ${CMAKE_BINARY_DIR}/sdkconfig)
3032

ports/esp32/lockfiles/dependencies.lock.esp32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dependencies:
2525
idf:
2626
source:
2727
type: idf
28-
version: 5.5.0
28+
version: 5.4.2
2929
direct_dependencies:
3030
- espressif/lan867x
3131
- espressif/mdns

ports/esp32/machine_can.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,26 @@
5959

6060
//static twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
6161

62+
static esp32_can_obj_t *esp32_can_objs[SOC_TWAI_CONTROLLER_NUM] = {};
63+
6264
// INTERNAL Deinitialize can
6365
void can_deinit(esp32_can_obj_t *self) {
64-
check_esp_err(twai_stop_v2(self->handle));
65-
check_esp_err(twai_driver_uninstall_v2(self->handle));
66-
if (self->irq_handler != NULL) {
66+
if (self->handle) {
67+
check_esp_err(twai_stop_v2(self->handle));
68+
check_esp_err(twai_driver_uninstall_v2(self->handle));
69+
self->handle = NULL;
70+
}
71+
if (self->irq_handler) {
6772
vTaskDelete(self->irq_handler);
73+
self->irq_handler = NULL;
74+
}
75+
}
76+
77+
// This called from Ctrl-D soft reboot
78+
void machine_can_deinit_all() {
79+
for (int i = 0; i < SOC_TWAI_CONTROLLER_NUM; i++) {
80+
can_deinit(esp32_can_objs[i]);
6881
}
69-
self->irq_handler = NULL;
70-
self->handle = NULL;
7182
}
7283

7384
static void esp32_can_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
@@ -201,8 +212,6 @@ static mp_obj_t esp32_can_init_helper(esp32_can_obj_t *self, size_t n_args, cons
201212
{ MP_QSTR_rx_queue, MP_ARG_INT, {.u_int = 5} },
202213
};
203214

204-
printf("esp32_can_init_helper() \n");
205-
206215
// parse args
207216
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
208217
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -325,7 +334,6 @@ static mp_obj_t esp32_can_init_helper(esp32_can_obj_t *self, size_t n_args, cons
325334
if (xTaskCreatePinnedToCore(esp32_can_irq_task, "can_irq_task", CAN_TASK_STACK_SIZE, self, CAN_TASK_PRIORITY, (TaskHandle_t *)&self->irq_handler, MP_TASK_COREID) != pdPASS) {
326335
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("failed to create can irq task handler"));
327336
}
328-
printf("2: esp32_can_init_helper() \n");
329337
return mp_const_none;
330338
}
331339

@@ -344,8 +352,9 @@ static mp_obj_t esp32_can_make_new(const mp_obj_type_t *type, size_t n_args, siz
344352
// mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("out of CAN controllers:%d"), SOC_TWAI_CONTROLLER_NUM);
345353
}
346354

347-
// esp32_can_obj_t *self = &esp32_can_obj;
348355
esp32_can_obj_t *self = mp_obj_malloc(esp32_can_obj_t, &machine_can_type);
356+
esp32_can_objs[can_idx] = self;
357+
//esp32_can_obj_t *self = &esp32_can_objs[can_idx];
349358

350359
self->g_config = (twai_general_config_t)TWAI_GENERAL_CONFIG_DEFAULT_V2(can_idx, GPIO_NUM_22, GPIO_NUM_23, TWAI_MODE_NORMAL);
351360
self->f_config = (twai_filter_config_t)TWAI_FILTER_CONFIG_ACCEPT_ALL();
@@ -502,7 +511,7 @@ static mp_obj_t esp32_can_send(size_t n_args, const mp_obj_t *pos_args, mp_map_t
502511
}
503512

504513
check_esp_err(twai_get_status_info_v2(self->handle, &self->status));
505-
debug_printf("state:%d", self->status.state);
514+
//debug_printf("state:%d", self->status.state);
506515
if (self->status.state == TWAI_STATE_RUNNING) {
507516
uint32_t timeout_ms = args[ARG_timeout].u_int;
508517

ports/esp32/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ void mp_task(void *pvParameter) {
205205
mp_hal_stdout_tx_str("MPY: soft reboot\r\n");
206206

207207
// deinitialise peripherals
208+
machine_can_deinit_all();
208209
machine_pwm_deinit_all();
209210
// TODO: machine_rmt_deinit_all();
210211
machine_pins_deinit();

ports/esp32/modmachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ void machine_init(void);
2121
void machine_deinit(void);
2222
void machine_pins_init(void);
2323
void machine_pins_deinit(void);
24+
void machine_can_deinit_all();
2425
void machine_pwm_deinit_all(void);
2526
// TODO: void machine_rmt_deinit_all(void);
2627
void machine_timer_deinit_all(void);

ports/esp32/mpconfigport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
#define MICROPY_ENABLE_GC (1)
6262
#define MICROPY_STACK_CHECK_MARGIN (1024)
6363
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
64-
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
64+
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) // (MICROPY_LONGINT_IMPL_LONGLONG) //
6565
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL +1) // Debugging Note: Increase the error reporting level to view
6666
// __FUNCTION__, __LINE__, __FILE__ in check_esp_err() exceptions
6767
#define MICROPY_WARNINGS (1)

py/mpz.c

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131

3232
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
3333

34+
#include "py/objint.h"
35+
#include "py/runtime.h"
36+
#define debug_printf(...) // mp_printf(&mp_plat_print, __VA_ARGS__); mp_printf(&mp_plat_print, " | %d at %s\n", __LINE__, __FILE__);
37+
3438
#define DIG_SIZE (MPZ_DIG_SIZE)
3539
#define DIG_MASK ((MPZ_LONG_1 << DIG_SIZE) - 1)
3640
#define DIG_MSB (MPZ_LONG_1 << (DIG_SIZE - 1))
@@ -851,19 +855,58 @@ size_t mpz_set_from_str(mpz_t *z, const char *str, size_t len, bool neg, unsigne
851855
}
852856

853857
void mpz_set_from_bytes(mpz_t *z, bool big_endian, bool is_signed, size_t len, const byte *buf) {
854-
int delta = 1;
855-
if (big_endian) {
856-
buf += len - 1;
857-
delta = -1;
858+
debug_printf("MPZ");
859+
debug_printf("big_endian:%d, is_signed:%d, len:%d", big_endian, is_signed, len)
860+
debug_printf("buf %X %X %X %X %X %X", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5])
861+
union {
862+
long long value;
863+
byte buf[sizeof(long long)];
864+
} result = {0};
865+
if (len > sizeof(result)) {
866+
mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("big-int overflow"));
858867
}
859-
868+
if (big_endian) {
869+
debug_printf("big_endian");
870+
reverce_memcpy(&result, buf, len);
871+
} else { // little-endian
872+
debug_printf("little-endian");
873+
memcpy(&result, buf, len);
874+
}
875+
876+
if ((is_signed) && (sizeof(result) > len) && (result.buf[len - 1] & 0x80)) {
877+
debug_printf("DDD");
878+
// Sign propagation in little-endian
879+
// x = 2
880+
// x.to_bytes(1, 'little', True) -> b'\x02'
881+
// x.to_bytes(4, 'little', True) -> b'\x02\x00\x00\x00'
882+
// x = -2
883+
// x.to_bytes(1, 'little', True) -> b'\xFE'
884+
// x.to_bytes(4, 'little', True) -> b'\xFE\xFF\xFF\xFF'
885+
memset(result.buf + len, 0xFF, sizeof(result) - len);
886+
}
887+
debug_printf("val: %ld, 0x%X", result.value, result.value);
888+
mpz_set_from_ll(z, result.value, is_signed);
889+
return;
890+
/*
891+
debug_printf("MPZ");
892+
debug_printf("big_endian:%d, is_signed:%d, len:%d", big_endian, is_signed, len)
893+
debug_printf("buf %X %X %X %X %X %X %X %X", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[len - 1] & 0x80, buf[0] & 0x80)
860894
mpz_need_dig(z, (len * 8 + DIG_SIZE - 1) / DIG_SIZE);
861895
862896
mpz_dig_t d = 0;
863897
int num_bits = 0;
864898
z->neg = 0;
865-
if ((is_signed) && (buf[len - 1] & 0x80)) {
866-
z->neg = 1;
899+
if (is_signed) {
900+
debug_printf("is_signed");
901+
if (((!big_endian) && (buf[len - 1] & 0x80)) || ((big_endian) && (buf[0] & 0x80))) {
902+
z->neg = 1;
903+
debug_printf("NEG");
904+
}
905+
}
906+
int delta = 1;
907+
if (big_endian) {
908+
buf += len - 1;
909+
delta = -1;
867910
}
868911
z->len = 0;
869912
while (len) {
@@ -891,6 +934,7 @@ void mpz_set_from_bytes(mpz_t *z, bool big_endian, bool is_signed, size_t len, c
891934
z->dig[z->len++] = d & DIG_MASK;
892935
}
893936
z->len = mpn_remove_trailing_zeros(z->dig, z->dig + z->len);
937+
*/
894938
}
895939

896940
#if 0

0 commit comments

Comments
 (0)