Skip to content

Commit

Permalink
ports/psoc6: Changes to adapt extmod changes.
Browse files Browse the repository at this point in the history
Signed-off-by: enriquezgarc <[email protected]>
  • Loading branch information
jaenrig-ifx authored and actions-user committed Jan 9, 2024
1 parent 5ad0475 commit f9e65b6
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 94 deletions.
1 change: 0 additions & 1 deletion ports/psoc6/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ DRIVERS_SRC_C += $(addprefix drivers/,\
MOD_SRC_C += $(addprefix modules/,\
gc/modgc.c \
\
machine/modmachine.c \
machine/machine_i2c.c \
machine/machine_pin_phy.c \
machine/machine_pin.c \
Expand Down
3 changes: 1 addition & 2 deletions ports/psoc6/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "shared/runtime/pyexec.h"
#include "extmod/modnetwork.h"

#include "modules/machine/modmachine.h"
#include "modules/machine/machine_pin_phy.h"

#if MICROPY_PY_NETWORK
Expand All @@ -52,8 +53,6 @@ __attribute__((section(".bss"))) static char gc_heap[MICROPY_GC_HEAP_SIZE];
extern void rtc_init(void);
extern void time_init(void);
extern void os_init(void);
extern void machine_init(void);
extern void machine_deinit(void);
extern void network_init(void);
extern void network_deinit(void);
extern void mod_pin_deinit(void);
Expand Down
137 changes: 47 additions & 90 deletions ports/psoc6/modules/machine/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "py/runtime.h"
#include "py/mphal.h"
#include "shared/runtime/pyexec.h"
#include "extmod/modmachine.h"

// MTB includes
#include "cybsp.h"
Expand Down Expand Up @@ -186,52 +185,44 @@ STATIC mp_obj_t machine_info(size_t n_args, const mp_obj_t *args) {
mp_printf(&mp_plat_print, "\n");
return mp_const_none;
}
// STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_info_obj, machine_info);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_info_obj, 0, 1, machine_info);


// machine.freq()
STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) {
if (n_args == 0) {
mp_printf(&mp_plat_print, "System core freq (CM4): %d Hz\n", system_get_cpu_freq());
return mp_const_none;
} else {
mp_raise_NotImplementedError(MP_ERROR_TEXT("Not implemented!!!\n"));
return mp_const_none;
}
STATIC mp_obj_t mp_machine_get_freq(void) {
mp_printf(&mp_plat_print, "System core freq (CM4): %d Hz\n", system_get_cpu_freq());
return mp_const_none;
}

STATIC void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) {
mp_raise_NotImplementedError(MP_ERROR_TEXT("Not implemented!!!\n"));
}

STATIC void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
mp_raise_NotImplementedError(MP_ERROR_TEXT("Not implemented!!!\n"));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq);

NORETURN STATIC void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) {
mp_raise_NotImplementedError(MP_ERROR_TEXT("Not implemented!!!\n"));
}

// machine.unique_id()
STATIC mp_obj_t machine_unique_id(void) {
STATIC mp_obj_t mp_machine_unique_id(void) {
uint64_t id = system_get_unique_id();
byte *id_addr = (byte *)&id;
mplogger_print("ID_formatted:%02x%02x%02x%02x:%02x%02x%02x%02x\n", id_addr[0], id_addr[1], id_addr[2], id_addr[3], id_addr[4], id_addr[5], id_addr[6], id_addr[7]);
mplogger_print("RAW_ID_data:");
return mp_obj_new_bytes(id_addr, 8);
}
MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id);


// machine.reset()
STATIC mp_obj_t machine_reset(void) {
NORETURN STATIC void mp_machine_reset(void) {
system_reset();
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset);

// machine.soft_reset()
STATIC mp_obj_t machine_soft_reset(void) {
pyexec_system_exit = PYEXEC_FORCED_EXIT;
mp_raise_type(&mp_type_SystemExit);
while (true) {
}
;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset);

// machine.reset_cause()
// Note: accurate returns only after one forced hard reset post hex upload.
// Possible reason is that the board does not get (soft) reset after firmware upload
STATIC mp_obj_t machine_reset_cause(void) {
STATIC mp_int_t mp_machine_reset_cause(void) {
qstr mp_reset_qstr = MP_QSTR_None;
uint8_t reset_cause_const = -1;
uint32_t reset_cause = system_reset_cause();
Expand All @@ -254,9 +245,8 @@ STATIC mp_obj_t machine_reset_cause(void) {
}

mplogger_print("Reset cause: %q; ", mp_reset_qstr);
return MP_OBJ_NEW_SMALL_INT(reset_cause_const);
return reset_cause_const;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause);

// machine.disable_irq()
STATIC mp_obj_t machine_disable_irq(void) {
Expand All @@ -283,66 +273,33 @@ MP_DEFINE_CONST_FUN_OBJ_1(machine_enable_irq_obj, machine_enable_irq);
// This executies a wfi machine instruction which reduces power consumption
// of the MCU until an interrupt occurs, at which point execution continues.
// see: https://www.infineon.com/dgdl/Infineon-AN219528_PSoC_6_MCU_low-power_modes_and_power_reduction_techniques-ApplicationNotes-v06_00-EN.pdf?fileId=8ac78c8c7cdc391c017d0d31efdc659f pg.7
STATIC mp_obj_t machine_idle(void) {
STATIC void mp_machine_idle(void) {
__WFI(); // standard ARM instruction
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(machine_idle_obj, machine_idle);


STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
// instance functions
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_machine) },
{ MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&machine_info_obj) },
{ MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) },
{ MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) },
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) },
{ MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&machine_soft_reset_obj) },
{ MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) },

{ MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
{ MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },

{ MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) },

// { MP_ROM_QSTR(MP_QSTR_bootloader), MP_ROM_PTR(&machine_bootloader_obj) }, // Note: not implemented

// class constants
{ MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(MACHINE_PWRON_RESET) },
{ MP_ROM_QSTR(MP_QSTR_HARD_RESET), MP_ROM_INT(MACHINE_HARD_RESET) },
{ MP_ROM_QSTR(MP_QSTR_WDT_RESET), MP_ROM_INT(MACHINE_WDT_RESET) },
{ MP_ROM_QSTR(MP_QSTR_DEEPSLEEP_RESET), MP_ROM_INT(MACHINE_DEEPSLEEP_RESET) },
{ MP_ROM_QSTR(MP_QSTR_SOFT_RESET), MP_ROM_INT(MACHINE_SOFT_RESET) },

// TODO: dynamic memory allocation functions/objects. Not yet implemented
// { MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) },
// { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) },
// { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) },

// Modules
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
{ MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) },
{ MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) },
{ MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) },
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) },
{ MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) },
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
{ MP_ROM_QSTR(MP_QSTR_ADCBlock), MP_ROM_PTR(&machine_adcblock_type) },
{ MP_ROM_QSTR(MP_QSTR_I2S), MP_ROM_PTR(&machine_i2s_type) },
};

STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);


const mp_obj_module_t mp_module_machine = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&machine_module_globals,
};

// Register machine module in MPY
MP_REGISTER_MODULE(MP_QSTR_machine, mp_module_machine);

#define MICROPY_PY_MACHINE_EXTRA_GLOBALS \
{ MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&machine_info_obj) }, \
{ MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, \
\
/* class constants */ \
{ MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(MACHINE_PWRON_RESET) }, \
{ MP_ROM_QSTR(MP_QSTR_HARD_RESET), MP_ROM_INT(MACHINE_HARD_RESET) }, \
{ MP_ROM_QSTR(MP_QSTR_WDT_RESET), MP_ROM_INT(MACHINE_WDT_RESET) }, \
{ MP_ROM_QSTR(MP_QSTR_DEEPSLEEP_RESET), MP_ROM_INT(MACHINE_DEEPSLEEP_RESET) }, \
{ MP_ROM_QSTR(MP_QSTR_SOFT_RESET), MP_ROM_INT(MACHINE_SOFT_RESET) }, \
\
/* Modules */ \
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, \
{ MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, \
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, \
{ MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, \
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) }, \
{ MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, \
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) }, \
{ MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, \
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, \
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, \
{ MP_ROM_QSTR(MP_QSTR_ADCBlock), MP_ROM_PTR(&machine_adcblock_type) }, \
{ MP_ROM_QSTR(MP_QSTR_I2S), MP_ROM_PTR(&machine_i2s_type) }, \

#endif // MICROPY_PY_MACHINE
1 change: 1 addition & 0 deletions ports/psoc6/modules/psoc6/psoc6_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "py/runtime.h"
#include "extmod/vfs.h"
#include "modpsoc6.h"
#include "mphalport.h"


// MTB includes
Expand Down
1 change: 1 addition & 0 deletions ports/psoc6/modules/psoc6/psoc6_qspi_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "cybsp.h"
#include "cy_serial_flash_qspi.h"
#include "cycfg_qspi_memslot.h"
#include "mphalport.h"

#define FLASH_BASE (0x00) // absolute address of xip/qspi flash. Does not depend on ld file values since xip isn't used
#define FLASH_SIZE (0x4000000) // qspi flash is 512 Mb / 64 MB in size
Expand Down
4 changes: 4 additions & 0 deletions ports/psoc6/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@
#define MICROPY_PY_TIME_INCLUDEFILE "ports/psoc6/modules/time/modtime.c"

#define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_MACHINE_INCLUDEFILE "ports/psoc6/modules/machine/modmachine.c"
#define MICROPY_PY_MACHINE_BARE_METAL_FUNCS (1)
#define MICROPY_PY_MACHINE_BOOTLOADER (0)
#define MICROPY_PY_MACHINE_DISABLE_IRQ_ENABLE_IRQ (0)
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
#define MICROPY_PY_MACHINE_PWM (1)
#define MICROPY_PY_MACHINE_PWM_INCLUDEFILE "ports/psoc6/modules/machine/machine_pwm.c"
Expand Down
3 changes: 2 additions & 1 deletion ports/psoc6/mphalport.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

// port-specific includes


#define MICROPY_BEGIN_ATOMIC_SECTION() (0)
#define MICROPY_END_ATOMIC_SECTION(state) {(void)state;}

#define MP_HAL_PIN_FMT "%u"
#define mp_hal_pin_obj_t uint
Expand Down

0 comments on commit f9e65b6

Please sign in to comment.