2015-02-06 14:35:48 +00:00
|
|
|
/*
|
2017-06-30 07:22:17 +00:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2015-02-06 14:35:48 +00:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
* Copyright (c) 2015 Daniel Campora
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2020-12-23 12:41:56 +00:00
|
|
|
#include <stdio.h>
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2015-03-04 12:52:39 +00:00
|
|
|
#include "py/runtime.h"
|
2015-10-30 23:03:58 +00:00
|
|
|
#include "py/mphal.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include "inc/hw_types.h"
|
|
|
|
#include "inc/hw_gpio.h"
|
|
|
|
#include "inc/hw_ints.h"
|
|
|
|
#include "inc/hw_memmap.h"
|
|
|
|
#include "inc/hw_uart.h"
|
2015-05-26 14:03:46 +00:00
|
|
|
#include "rom_map.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include "prcm.h"
|
|
|
|
#include "pybuart.h"
|
2015-02-09 18:38:02 +00:00
|
|
|
#include "pybpin.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include "pybrtc.h"
|
|
|
|
#include "simplelink.h"
|
2015-05-23 18:52:42 +00:00
|
|
|
#include "modnetwork.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include "modwlan.h"
|
2015-03-04 12:52:39 +00:00
|
|
|
#include "moduos.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include "FreeRTOS.h"
|
|
|
|
#include "portable.h"
|
|
|
|
#include "task.h"
|
|
|
|
#include "random.h"
|
2015-02-19 20:03:21 +00:00
|
|
|
#include "pybadc.h"
|
2015-02-10 12:11:27 +00:00
|
|
|
#include "pybi2c.h"
|
2015-02-21 13:10:33 +00:00
|
|
|
#include "pybsd.h"
|
2015-02-25 10:08:51 +00:00
|
|
|
#include "pybwdt.h"
|
2015-03-04 12:52:39 +00:00
|
|
|
#include "pybsleep.h"
|
2015-03-19 14:17:39 +00:00
|
|
|
#include "pybspi.h"
|
2015-05-09 15:46:16 +00:00
|
|
|
#include "pybtimer.h"
|
2015-02-10 12:11:27 +00:00
|
|
|
#include "utils.h"
|
2015-02-22 16:31:26 +00:00
|
|
|
#include "gccollect.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
extern OsiTaskHandle mpTaskHandle;
|
|
|
|
extern OsiTaskHandle svTaskHandle;
|
2015-02-22 16:31:26 +00:00
|
|
|
extern OsiTaskHandle xSimpleLinkSpawnTaskHndl;
|
2015-02-06 14:35:48 +00:00
|
|
|
#endif
|
|
|
|
|
2015-03-04 12:52:39 +00:00
|
|
|
|
2015-09-27 11:45:48 +00:00
|
|
|
/// \module machine - functions related to the SoC
|
2015-02-06 14:35:48 +00:00
|
|
|
///
|
|
|
|
|
2020-06-18 02:19:04 +00:00
|
|
|
MP_DECLARE_CONST_FUN_OBJ_0(machine_disable_irq_obj);
|
|
|
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_enable_irq_obj);
|
|
|
|
|
2015-09-27 11:45:48 +00:00
|
|
|
/******************************************************************************/
|
2017-06-30 07:22:17 +00:00
|
|
|
// MicroPython bindings;
|
2015-09-27 11:45:48 +00:00
|
|
|
|
|
|
|
STATIC mp_obj_t machine_reset(void) {
|
2015-03-18 20:49:48 +00:00
|
|
|
// disable wlan
|
|
|
|
wlan_stop(SL_STOP_TIMEOUT_LONG);
|
2015-05-26 14:03:46 +00:00
|
|
|
// reset the cpu and it's peripherals
|
|
|
|
MAP_PRCMMCUReset(true);
|
2015-02-06 14:35:48 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2015-09-27 11:45:48 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset);
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2015-09-27 11:45:48 +00:00
|
|
|
STATIC mp_obj_t machine_info(uint n_args, const mp_obj_t *args) {
|
2015-02-06 14:35:48 +00:00
|
|
|
// FreeRTOS info
|
|
|
|
{
|
|
|
|
printf("---------------------------------------------\n");
|
|
|
|
printf("FreeRTOS\n");
|
|
|
|
printf("---------------------------------------------\n");
|
|
|
|
printf("Total heap: %u\n", configTOTAL_HEAP_SIZE);
|
|
|
|
printf("Free heap: %u\n", xPortGetFreeHeapSize());
|
|
|
|
printf("MpTask min free stack: %u\n", (unsigned int)uxTaskGetStackHighWaterMark((TaskHandle_t)mpTaskHandle));
|
|
|
|
printf("ServersTask min free stack: %u\n", (unsigned int)uxTaskGetStackHighWaterMark((TaskHandle_t)svTaskHandle));
|
|
|
|
printf("SlTask min free stack: %u\n", (unsigned int)uxTaskGetStackHighWaterMark(xSimpleLinkSpawnTaskHndl));
|
|
|
|
printf("IdleTask min free stack: %u\n", (unsigned int)uxTaskGetStackHighWaterMark(xTaskGetIdleTaskHandle()));
|
|
|
|
|
|
|
|
uint32_t *pstack = (uint32_t *)&_stack;
|
|
|
|
while (*pstack == 0x55555555) {
|
|
|
|
pstack++;
|
|
|
|
}
|
|
|
|
printf("MAIN min free stack: %u\n", pstack - ((uint32_t *)&_stack));
|
|
|
|
printf("---------------------------------------------\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2015-09-27 11:45:48 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_info_obj, 0, 1, machine_info);
|
2015-02-06 14:35:48 +00:00
|
|
|
#endif
|
|
|
|
|
2015-09-27 11:45:48 +00:00
|
|
|
STATIC mp_obj_t machine_freq(void) {
|
2017-04-05 08:58:17 +00:00
|
|
|
return mp_obj_new_int(HAL_FCPU_HZ);
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
2015-09-27 11:45:48 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq);
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2015-09-27 11:45:48 +00:00
|
|
|
STATIC mp_obj_t machine_unique_id(void) {
|
2015-05-29 08:22:20 +00:00
|
|
|
uint8_t mac[SL_BSSID_LENGTH];
|
|
|
|
wlan_get_mac (mac);
|
|
|
|
return mp_obj_new_bytes(mac, SL_BSSID_LENGTH);
|
|
|
|
}
|
2015-09-27 11:45:48 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id);
|
|
|
|
|
2015-09-27 15:04:49 +00:00
|
|
|
STATIC mp_obj_t machine_main(mp_obj_t main) {
|
2019-01-30 11:05:48 +00:00
|
|
|
if (mp_obj_is_str(main)) {
|
2015-09-27 15:04:49 +00:00
|
|
|
MP_STATE_PORT(machine_config_main) = main;
|
|
|
|
} else {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("invalid argument(s) value"));
|
2015-09-27 15:04:49 +00:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(machine_main_obj, machine_main);
|
|
|
|
|
2015-09-27 11:45:48 +00:00
|
|
|
STATIC mp_obj_t machine_idle(void) {
|
|
|
|
__WFI();
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_idle_obj, machine_idle);
|
|
|
|
|
2019-01-30 03:15:08 +00:00
|
|
|
STATIC mp_obj_t machine_lightsleep(void) {
|
2015-09-27 11:45:48 +00:00
|
|
|
pyb_sleep_sleep();
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2019-01-30 03:15:08 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_lightsleep_obj, machine_lightsleep);
|
2015-09-27 11:45:48 +00:00
|
|
|
|
|
|
|
STATIC mp_obj_t machine_deepsleep (void) {
|
|
|
|
pyb_sleep_deepsleep();
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_deepsleep_obj, machine_deepsleep);
|
|
|
|
|
|
|
|
STATIC mp_obj_t machine_reset_cause (void) {
|
|
|
|
return mp_obj_new_int(pyb_sleep_get_reset_cause());
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause);
|
2015-05-29 08:22:20 +00:00
|
|
|
|
2015-09-27 11:45:48 +00:00
|
|
|
STATIC mp_obj_t machine_wake_reason (void) {
|
|
|
|
return mp_obj_new_int(pyb_sleep_get_wake_reason());
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_wake_reason_obj, machine_wake_reason);
|
|
|
|
|
2017-08-21 11:34:23 +00:00
|
|
|
STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) },
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2017-08-21 11:34:23 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) },
|
2015-02-06 14:35:48 +00:00
|
|
|
#ifdef DEBUG
|
2017-08-21 11:34:23 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&machine_info_obj) },
|
2015-02-06 14:35:48 +00:00
|
|
|
#endif
|
2017-08-21 11:34:23 +00:00
|
|
|
{ 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_main), MP_ROM_PTR(&machine_main_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_rng), MP_ROM_PTR(&machine_rng_get_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) },
|
2019-01-30 03:15:08 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_lightsleep_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_lightsleep), MP_ROM_PTR(&machine_lightsleep_obj) },
|
2017-08-21 11:34:23 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_wake_reason), MP_ROM_PTR(&machine_wake_reason_obj) },
|
|
|
|
|
2020-06-18 02:19:04 +00:00
|
|
|
{ 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) },
|
2017-08-21 11:34:23 +00:00
|
|
|
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pin_type) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&pyb_i2c_type) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_spi_type) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&pyb_timer_type) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&pyb_wdt_type) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_SD), MP_ROM_PTR(&pyb_sd_type) },
|
2015-09-27 11:45:48 +00:00
|
|
|
|
|
|
|
// class constants
|
2017-08-21 11:34:23 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_IDLE), MP_ROM_INT(PYB_PWR_MODE_ACTIVE) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_SLEEP), MP_ROM_INT(PYB_PWR_MODE_LPDS) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(PYB_PWR_MODE_HIBERNATE) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_POWER_ON), MP_ROM_INT(PYB_SLP_PWRON_RESET) }, // legacy constant
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(PYB_SLP_PWRON_RESET) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_HARD_RESET), MP_ROM_INT(PYB_SLP_HARD_RESET) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_WDT_RESET), MP_ROM_INT(PYB_SLP_WDT_RESET) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_DEEPSLEEP_RESET), MP_ROM_INT(PYB_SLP_HIB_RESET) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_SOFT_RESET), MP_ROM_INT(PYB_SLP_SOFT_RESET) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_WLAN_WAKE), MP_ROM_INT(PYB_SLP_WAKED_BY_WLAN) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PIN_WAKE), MP_ROM_INT(PYB_SLP_WAKED_BY_GPIO) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_RTC_WAKE), MP_ROM_INT(PYB_SLP_WAKED_BY_RTC) },
|
2015-02-06 14:35:48 +00:00
|
|
|
};
|
|
|
|
|
2015-09-27 11:45:48 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);
|
2015-02-06 14:35:48 +00:00
|
|
|
|
2022-02-02 05:04:00 +00:00
|
|
|
const mp_obj_module_t mp_module_machine = {
|
2015-02-06 14:35:48 +00:00
|
|
|
.base = { &mp_type_module },
|
2015-09-27 11:45:48 +00:00
|
|
|
.globals = (mp_obj_dict_t*)&machine_module_globals,
|
2015-02-06 14:35:48 +00:00
|
|
|
};
|
2022-04-20 06:06:22 +00:00
|
|
|
|
2022-05-31 12:56:11 +00:00
|
|
|
MP_REGISTER_MODULE(MP_QSTR_umachine, mp_module_machine);
|
2022-07-01 19:28:58 +00:00
|
|
|
MP_REGISTER_ROOT_POINTER(mp_obj_t machine_config_main);
|