2015-02-06 14:35:48 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-04-18 14:29:28 +01:00
|
|
|
#include <stdio.h>
|
2015-02-06 14:35:48 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-02-21 18:58:43 +00:00
|
|
|
#include "py/mpconfig.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include MICROPY_HAL_H
|
2015-02-21 18:58:43 +00:00
|
|
|
#include "py/obj.h"
|
2015-06-09 09:46:50 +01:00
|
|
|
#include "py/runtime.h"
|
2015-02-25 21:38:33 +00:00
|
|
|
#include "hw_ints.h"
|
|
|
|
#include "hw_types.h"
|
|
|
|
#include "hw_gpio.h"
|
|
|
|
#include "hw_memmap.h"
|
|
|
|
#include "hw_gprcm.h"
|
|
|
|
#include "hw_common_reg.h"
|
|
|
|
#include "pin.h"
|
|
|
|
#include "gpio.h"
|
2015-03-25 13:14:25 +00:00
|
|
|
#ifndef BOOTLOADER
|
|
|
|
#include "pybpin.h"
|
|
|
|
#include "pins.h"
|
|
|
|
#endif
|
2015-02-25 21:38:33 +00:00
|
|
|
#include "rom.h"
|
|
|
|
#include "rom_map.h"
|
|
|
|
#include "prcm.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
#include "pybuart.h"
|
|
|
|
#include "utils.h"
|
2015-02-26 14:03:52 +00:00
|
|
|
#include "mperror.h"
|
2015-02-06 14:35:48 +00:00
|
|
|
|
|
|
|
|
2015-02-26 14:03:52 +00:00
|
|
|
/******************************************************************************
|
|
|
|
DEFINE CONSTANTS
|
|
|
|
******************************************************************************/
|
2015-03-04 12:52:39 +00:00
|
|
|
#define MPERROR_TOOGLE_MS (40)
|
|
|
|
#define MPERROR_SIGNAL_ERROR_MS (1000)
|
2015-02-26 14:03:52 +00:00
|
|
|
#define MPERROR_HEARTBEAT_ON_MS (80)
|
2015-03-12 20:15:57 +00:00
|
|
|
#define MPERROR_HEARTBEAT_OFF_MS (4920)
|
2015-02-25 21:38:33 +00:00
|
|
|
|
2015-02-26 14:03:52 +00:00
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE DATA
|
|
|
|
******************************************************************************/
|
2015-06-09 16:39:48 +01:00
|
|
|
#ifndef BOOTLOADER
|
2015-06-09 09:46:50 +01:00
|
|
|
STATIC const mp_obj_base_t pyb_heartbeat_obj = {&pyb_heartbeat_type};
|
2015-06-09 16:39:48 +01:00
|
|
|
#endif
|
2015-06-09 09:46:50 +01:00
|
|
|
|
2015-03-12 20:15:57 +00:00
|
|
|
struct mperror_heart_beat {
|
|
|
|
uint32_t off_time;
|
|
|
|
uint32_t on_time;
|
|
|
|
bool beating;
|
|
|
|
bool enabled;
|
2015-05-24 14:06:50 +01:00
|
|
|
bool do_disable;
|
|
|
|
} mperror_heart_beat = {.off_time = 0, .on_time = 0, .beating = false, .enabled = false, .do_disable = false};
|
2015-02-25 21:38:33 +00:00
|
|
|
|
2015-02-26 14:03:52 +00:00
|
|
|
/******************************************************************************
|
|
|
|
DEFINE PUBLIC FUNCTIONS
|
|
|
|
******************************************************************************/
|
2015-02-25 21:38:33 +00:00
|
|
|
void mperror_init0 (void) {
|
|
|
|
#ifdef BOOTLOADER
|
2015-03-25 13:14:25 +00:00
|
|
|
// enable the system led and the safe boot pin peripheral clocks
|
|
|
|
MAP_PRCMPeripheralClkEnable(MICROPY_SYS_LED_PRCM, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
2015-02-25 21:38:33 +00:00
|
|
|
MAP_PRCMPeripheralClkEnable(MICROPY_SAFE_BOOT_PRCM, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
2015-03-25 13:14:25 +00:00
|
|
|
// configure the safe boot pin
|
2015-02-25 21:38:33 +00:00
|
|
|
MAP_PinTypeGPIO(MICROPY_SAFE_BOOT_PIN_NUM, PIN_MODE_0, false);
|
|
|
|
MAP_PinConfigSet(MICROPY_SAFE_BOOT_PIN_NUM, PIN_STRENGTH_4MA, PIN_TYPE_STD_PD);
|
|
|
|
MAP_GPIODirModeSet(MICROPY_SAFE_BOOT_PORT, MICROPY_SAFE_BOOT_PORT_PIN, GPIO_DIR_MODE_IN);
|
2015-03-25 13:14:25 +00:00
|
|
|
// configure the bld
|
|
|
|
MAP_PinTypeGPIO(MICROPY_SYS_LED_PIN_NUM, PIN_MODE_0, false);
|
|
|
|
MAP_PinConfigSet(MICROPY_SYS_LED_PIN_NUM, PIN_STRENGTH_6MA, PIN_TYPE_STD);
|
|
|
|
MAP_GPIODirModeSet(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, GPIO_DIR_MODE_OUT);
|
|
|
|
#else
|
|
|
|
// configure the system led
|
|
|
|
pin_config ((pin_obj_t *)&MICROPY_SYS_LED_GPIO, PIN_MODE_0, GPIO_DIR_MODE_OUT, PIN_TYPE_STD, PIN_STRENGTH_6MA);
|
2015-02-25 21:38:33 +00:00
|
|
|
#endif
|
2015-06-06 17:42:51 +01:00
|
|
|
mperror_heart_beat.enabled = true;
|
2015-03-25 13:14:25 +00:00
|
|
|
mperror_heartbeat_switch_off();
|
2015-02-25 21:38:33 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 08:59:47 +00:00
|
|
|
void mperror_bootloader_check_reset_cause (void) {
|
2015-02-26 14:29:47 +00:00
|
|
|
// if we are recovering from a WDT reset, trigger
|
|
|
|
// a hibernate cycle for a clean boot
|
|
|
|
if (MAP_PRCMSysResetCauseGet() == PRCM_WDT_RESET) {
|
|
|
|
HWREG(0x400F70B8) = 1;
|
|
|
|
UtilsDelay(800000/5);
|
|
|
|
HWREG(0x400F70B0) = 1;
|
|
|
|
UtilsDelay(800000/5);
|
|
|
|
|
|
|
|
HWREG(0x4402E16C) |= 0x2;
|
|
|
|
UtilsDelay(800);
|
|
|
|
HWREG(0x4402F024) &= 0xF7FFFFFF;
|
|
|
|
|
2015-03-14 08:59:47 +00:00
|
|
|
// since the reset cause will be changed, we must store the right reason
|
2015-03-25 13:14:25 +00:00
|
|
|
// so that the application knows it when booting for the next time
|
2015-03-14 08:59:47 +00:00
|
|
|
PRCMSignalWDTReset();
|
|
|
|
|
2015-02-26 14:29:47 +00:00
|
|
|
MAP_PRCMHibernateWakeupSourceEnable(PRCM_HIB_SLOW_CLK_CTR);
|
|
|
|
// set the sleep interval to 10ms
|
|
|
|
MAP_PRCMHibernateIntervalSet(330);
|
|
|
|
MAP_PRCMHibernateEnter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 21:38:33 +00:00
|
|
|
void mperror_deinit_sfe_pin (void) {
|
|
|
|
// disable the pull-down
|
|
|
|
MAP_PinConfigSet(MICROPY_SAFE_BOOT_PIN_NUM, PIN_STRENGTH_4MA, PIN_TYPE_STD);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mperror_signal_error (void) {
|
|
|
|
uint32_t count = 0;
|
2015-03-04 12:52:39 +00:00
|
|
|
while ((MPERROR_TOOGLE_MS * count++) < MPERROR_SIGNAL_ERROR_MS) {
|
2015-02-25 21:38:33 +00:00
|
|
|
// toogle the led
|
|
|
|
MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, ~MAP_GPIOPinRead(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN));
|
|
|
|
UtilsDelay(UTILS_DELAY_US_TO_COUNT(MPERROR_TOOGLE_MS * 1000));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 13:14:25 +00:00
|
|
|
void mperror_heartbeat_switch_off (void) {
|
2015-06-06 17:42:51 +01:00
|
|
|
if (mperror_heart_beat.enabled) {
|
|
|
|
mperror_heart_beat.on_time = 0;
|
|
|
|
mperror_heart_beat.off_time = 0;
|
|
|
|
MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, 0);
|
|
|
|
}
|
2015-02-26 14:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mperror_heartbeat_signal (void) {
|
2015-05-24 14:06:50 +01:00
|
|
|
if (mperror_heart_beat.do_disable) {
|
|
|
|
mperror_heart_beat.do_disable = false;
|
|
|
|
mperror_heartbeat_switch_off();
|
2015-06-06 17:42:51 +01:00
|
|
|
mperror_heart_beat.enabled = false;
|
2015-05-24 14:06:50 +01:00
|
|
|
}
|
|
|
|
else if (mperror_heart_beat.enabled) {
|
2015-03-12 20:15:57 +00:00
|
|
|
if (!mperror_heart_beat.beating) {
|
|
|
|
if ((mperror_heart_beat.on_time = HAL_GetTick()) - mperror_heart_beat.off_time > MPERROR_HEARTBEAT_OFF_MS) {
|
2015-02-26 14:03:52 +00:00
|
|
|
MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, MICROPY_SYS_LED_PORT_PIN);
|
2015-03-12 20:15:57 +00:00
|
|
|
mperror_heart_beat.beating = true;
|
2015-02-26 14:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-03-12 20:15:57 +00:00
|
|
|
if ((mperror_heart_beat.off_time = HAL_GetTick()) - mperror_heart_beat.on_time > MPERROR_HEARTBEAT_ON_MS) {
|
2015-03-25 13:14:25 +00:00
|
|
|
MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, 0);
|
2015-03-12 20:15:57 +00:00
|
|
|
mperror_heart_beat.beating = false;
|
2015-02-26 14:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 14:35:48 +00:00
|
|
|
void NORETURN __fatal_error(const char *msg) {
|
2015-02-25 21:38:33 +00:00
|
|
|
#ifdef DEBUG
|
2015-02-06 14:35:48 +00:00
|
|
|
if (msg != NULL) {
|
|
|
|
// wait for 20ms
|
|
|
|
UtilsDelay(UTILS_DELAY_US_TO_COUNT(20000));
|
2015-02-13 15:04:53 +00:00
|
|
|
mp_hal_stdout_tx_str("\r\nFATAL ERROR:");
|
|
|
|
mp_hal_stdout_tx_str(msg);
|
|
|
|
mp_hal_stdout_tx_str("\r\n");
|
2015-02-06 14:35:48 +00:00
|
|
|
}
|
2015-02-25 21:38:33 +00:00
|
|
|
#endif
|
|
|
|
// signal the crash with the system led
|
|
|
|
MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, MICROPY_SYS_LED_PORT_PIN);
|
2015-02-06 14:35:48 +00:00
|
|
|
for ( ;; ) {__WFI();}
|
|
|
|
}
|
|
|
|
|
|
|
|
void __assert_func(const char *file, int line, const char *func, const char *expr) {
|
|
|
|
(void) func;
|
|
|
|
printf("Assertion failed: %s, file %s, line %d\n", expr, file, line);
|
|
|
|
__fatal_error(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nlr_jump_fail(void *val) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
char msg[64];
|
|
|
|
snprintf(msg, sizeof(msg), "uncaught exception %p\n", val);
|
|
|
|
__fatal_error(msg);
|
|
|
|
#else
|
|
|
|
__fatal_error(NULL);
|
|
|
|
#endif
|
|
|
|
}
|
2015-02-26 14:29:47 +00:00
|
|
|
|
|
|
|
#ifndef BOOTLOADER
|
|
|
|
/******************************************************************************/
|
|
|
|
// Micro Python bindings
|
|
|
|
|
2015-06-09 09:46:50 +01:00
|
|
|
/// \classmethod \constructor()
|
|
|
|
///
|
|
|
|
/// Return the heart beat object
|
|
|
|
STATIC mp_obj_t pyb_heartbeat_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
|
|
|
// check arguments
|
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 0, false);
|
|
|
|
|
|
|
|
// return constant object
|
|
|
|
return (mp_obj_t)&pyb_heartbeat_obj;
|
|
|
|
}
|
|
|
|
|
2015-02-26 14:29:47 +00:00
|
|
|
/// \function enable()
|
|
|
|
/// Enables the heartbeat signal
|
|
|
|
STATIC mp_obj_t pyb_enable_heartbeat(mp_obj_t self) {
|
2015-06-06 17:42:51 +01:00
|
|
|
mperror_heart_beat.enabled = true;
|
2015-02-26 14:29:47 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_enable_heartbeat_obj, pyb_enable_heartbeat);
|
|
|
|
|
|
|
|
/// \function disable()
|
|
|
|
/// Disables the heartbeat signal
|
|
|
|
STATIC mp_obj_t pyb_disable_heartbeat(mp_obj_t self) {
|
2015-06-06 17:42:51 +01:00
|
|
|
mperror_heart_beat.do_disable = true;
|
2015-02-26 14:29:47 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_disable_heartbeat_obj, pyb_disable_heartbeat);
|
|
|
|
|
|
|
|
STATIC const mp_map_elem_t pyb_heartbeat_locals_dict_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_enable), (mp_obj_t)&pyb_enable_heartbeat_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_disable), (mp_obj_t)&pyb_disable_heartbeat_obj },
|
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(pyb_heartbeat_locals_dict, pyb_heartbeat_locals_dict_table);
|
|
|
|
|
2015-06-09 09:46:50 +01:00
|
|
|
const mp_obj_type_t pyb_heartbeat_type = {
|
2015-02-26 14:29:47 +00:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_HeartBeat,
|
2015-06-09 09:46:50 +01:00
|
|
|
.make_new = pyb_heartbeat_make_new,
|
2015-02-26 14:29:47 +00:00
|
|
|
.locals_dict = (mp_obj_t)&pyb_heartbeat_locals_dict,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|