diff options
-rw-r--r-- | stmhal/diskio.c | 25 | ||||
-rw-r--r-- | stmhal/modpyb.c | 11 | ||||
-rw-r--r-- | stmhal/pin.c | 61 | ||||
-rw-r--r-- | stmhal/qstrdefsport.h | 8 | ||||
-rw-r--r-- | stmhal/rtc.c | 115 | ||||
-rw-r--r-- | stmhal/rtc.h | 6 |
6 files changed, 160 insertions, 66 deletions
diff --git a/stmhal/diskio.c b/stmhal/diskio.c index 4ac3a35aa2..69a6432418 100644 --- a/stmhal/diskio.c +++ b/stmhal/diskio.c @@ -9,11 +9,19 @@ #include <stdint.h> #include <stdio.h> -#include "ff.h" /* FatFs lower layer API */ -#include "diskio.h" /* FatFs lower layer API */ + +#include "stm32f4xx_hal.h" + #include "misc.h" +#include "mpconfig.h" +#include "qstr.h" +#include "obj.h" +#include "systick.h" +#include "rtc.h" #include "storage.h" #include "sdcard.h" +#include "ff.h" /* FatFs lower layer API */ +#include "diskio.h" /* FatFs lower layer API */ const PARTITION VolToPart[] = { {0, 1}, // Logical drive 0 ==> Physical drive 0, 1st partition @@ -190,12 +198,9 @@ DWORD get_fattime ( void ) { - // TODO replace with call to RTC - int year = 2013; - int month = 10; - int day = 12; - int hour = 21; - int minute = 42; - int second = 13; - return ((year - 1980) << 25) | ((month) << 21) | ((day) << 16) | ((hour) << 11) | ((minute) << 5) | (second / 2); + RTC_TimeTypeDef time; + RTC_DateTypeDef date; + HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN); + HAL_RTC_GetDate(&RTCHandle, &date, FORMAT_BIN); + return ((2000 + date.Year - 1980) << 25) | ((date.Month) << 21) | ((date.Date) << 16) | ((time.Hours) << 11) | ((time.Minutes) << 5) | (time.Seconds / 2); } diff --git a/stmhal/modpyb.c b/stmhal/modpyb.c index ccf9fa53c6..56a7233cc2 100644 --- a/stmhal/modpyb.c +++ b/stmhal/modpyb.c @@ -30,6 +30,13 @@ #include "modpyb.h" #include "ff.h" +STATIC mp_obj_t pyb_unique_id(void) { + // get unique id; 96 bits + byte *id = (byte*)0x1fff7a10; + return mp_obj_new_bytes(id, 12); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_unique_id_obj, pyb_unique_id); + // get lots of info about the board STATIC mp_obj_t pyb_info(uint n_args, const mp_obj_t *args) { // get and print unique id; 96 bits @@ -233,6 +240,7 @@ MP_DECLARE_CONST_FUN_OBJ(pyb_usb_mode_obj); // defined in main.c STATIC const mp_map_elem_t pyb_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_pyb) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_unique_id), (mp_obj_t)&pyb_unique_id_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_info_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_gc), (mp_obj_t)&pyb_gc_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_repl_info), (mp_obj_t)&pyb_set_repl_info_obj }, @@ -260,8 +268,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = { #endif #if MICROPY_HW_ENABLE_RTC - { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&pyb_rtc_read_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_rtc_info), (mp_obj_t)&pyb_rtc_info_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_RTC), (mp_obj_t)&pyb_rtc_type }, #endif { MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pin_type }, diff --git a/stmhal/pin.c b/stmhal/pin.c index ce66a7ad67..3230c8f47b 100644 --- a/stmhal/pin.c +++ b/stmhal/pin.c @@ -157,6 +157,8 @@ STATIC void pin_print(void (*print)(void *env, const char *fmt, ...), void *env, print(env, "<Pin %s>", self->name); } +STATIC mp_obj_t pin_obj_init(uint n_args, mp_obj_t *args); + // Pin constructor STATIC mp_obj_t pin_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) { mp_check_nargs(n_args, 1, 3, n_kw, false); @@ -166,30 +168,11 @@ STATIC mp_obj_t pin_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const mp_ if (n_args >= 2) { // pin mode given, so configure this GPIO - - // get io mode - uint mode = mp_obj_get_int(args[1]); - if (!IS_GPIO_MODE(mode)) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid pin mode: %d", mode)); - } - - // get pull mode - uint pull = GPIO_NOPULL; - if (n_args >= 3) { - pull = mp_obj_get_int(args[2]); - if (!IS_GPIO_PULL(pull)) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid pin pull: %d", pull)); - } + mp_obj_t args2[3] = {(mp_obj_t)pin, args2[1], MP_OBJ_NULL}; + if (n_args == 3) { + args2[2] = args[2]; } - - // configure the GPIO as requested - GPIO_InitTypeDef GPIO_InitStructure; - GPIO_InitStructure.Pin = pin->pin_mask; - GPIO_InitStructure.Mode = mode; - GPIO_InitStructure.Pull = pull; - GPIO_InitStructure.Speed = GPIO_SPEED_FAST; - GPIO_InitStructure.Alternate = 0; - HAL_GPIO_Init(pin->gpio, &GPIO_InitStructure); + pin_obj_init(n_args, args2); } return (mp_obj_t)pin; @@ -228,6 +211,37 @@ STATIC mp_obj_t pin_debug(uint n_args, mp_obj_t *args) { STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_debug_fun_obj, 1, 2, pin_debug); STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(pin_debug_obj, (mp_obj_t)&pin_debug_fun_obj); +STATIC mp_obj_t pin_obj_init(uint n_args, mp_obj_t *args) { + pin_obj_t *self = args[0]; + + // get io mode + uint mode = mp_obj_get_int(args[1]); + if (!IS_GPIO_MODE(mode)) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid pin mode: %d", mode)); + } + + // get pull mode + uint pull = GPIO_NOPULL; + if (n_args >= 3) { + pull = mp_obj_get_int(args[2]); + if (!IS_GPIO_PULL(pull)) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid pin pull: %d", pull)); + } + } + + // configure the GPIO as requested + GPIO_InitTypeDef GPIO_InitStructure; + GPIO_InitStructure.Pin = self->pin_mask; + GPIO_InitStructure.Mode = mode; + GPIO_InitStructure.Pull = pull; + GPIO_InitStructure.Speed = GPIO_SPEED_FAST; + GPIO_InitStructure.Alternate = 0; + HAL_GPIO_Init(self->gpio, &GPIO_InitStructure); + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_init_obj, 2, 3, pin_obj_init); + STATIC mp_obj_t pin_value(uint n_args, mp_obj_t *args) { pin_obj_t *self = args[0]; if (n_args == 1) { @@ -279,6 +293,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_pin_obj, pin_pin); STATIC const mp_map_elem_t pin_locals_dict_table[] = { // instance methods + { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pin_init_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&pin_value_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_low), (mp_obj_t)&pin_low_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_high), (mp_obj_t)&pin_high_obj }, diff --git a/stmhal/qstrdefsport.h b/stmhal/qstrdefsport.h index d502dae493..7b50a7f9fb 100644 --- a/stmhal/qstrdefsport.h +++ b/stmhal/qstrdefsport.h @@ -2,6 +2,7 @@ Q(help) Q(pyb) +Q(unique_id) Q(info) Q(sd_test) Q(present) @@ -38,13 +39,18 @@ Q(FileIO) // Entries for sys.path Q(0:/) Q(0:/lib) -Q(rtc_info) Q(millis) +// for RTC class +Q(RTC) +Q(info) +Q(datetime) + // for Pin class Q(Pin) Q(PinAF) Q(PinNamed) +Q(init) Q(value) Q(low) Q(high) diff --git a/stmhal/rtc.c b/stmhal/rtc.c index d1ed69a5ab..9a0ebfac4d 100644 --- a/stmhal/rtc.c +++ b/stmhal/rtc.c @@ -1,16 +1,16 @@ #include <stdio.h> #include "stm32f4xx_hal.h" -#include "stm32f4xx_hal_rtc.h" #include "misc.h" #include "mpconfig.h" #include "qstr.h" #include "obj.h" -#include "systick.h" +#include "runtime.h" +//#include "systick.h" #include "rtc.h" -static RTC_HandleTypeDef RtcHandle; +RTC_HandleTypeDef RTCHandle; static machine_uint_t rtc_info; #define RTC_INFO_USE_EXISTING (0) @@ -125,7 +125,7 @@ static void RTC_CalendarConfig(void); void rtc_init(void) { /*##-1- Configure the RTC peripheral #######################################*/ - RtcHandle.Instance = RTC; + RTCHandle.Instance = RTC; /* Configure RTC prescaler and RTC data registers */ /* RTC configured as follow: @@ -135,16 +135,16 @@ void rtc_init(void) { - OutPut = Output Disable - OutPutPolarity = High Polarity - OutPutType = Open Drain */ - RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; - RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; - RtcHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; - RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; - RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; - RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; + RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24; + RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; + RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; + RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE; + RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; + RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; machine_uint_t tick = HAL_GetTick(); - if(HAL_RTC_Init(&RtcHandle) != HAL_OK) + if(HAL_RTC_Init(&RTCHandle) != HAL_OK) { /* Initialization Error */ //Error_Handler(); @@ -156,7 +156,7 @@ void rtc_init(void) { /*##-2- Check if Data stored in BackUp register0: No Need to reconfigure RTC#*/ /* Read the Back Up Register 0 Data */ - if(HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR0) != 0x32F2) + if(HAL_RTCEx_BKUPRead(&RTCHandle, RTC_BKP_DR0) != 0x32F2) { /* Configure RTC Calendar */ RTC_CalendarConfig(); @@ -193,7 +193,7 @@ static void RTC_CalendarConfig(void) sdatestructure.Date = 0x18; sdatestructure.WeekDay = RTC_WEEKDAY_TUESDAY; - if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure,FORMAT_BCD) != HAL_OK) + if(HAL_RTC_SetDate(&RTCHandle,&sdatestructure,FORMAT_BCD) != HAL_OK) { /* Initialization Error */ //Error_Handler(); @@ -209,7 +209,7 @@ static void RTC_CalendarConfig(void) stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ; stimestructure.StoreOperation = RTC_STOREOPERATION_RESET; - if(HAL_RTC_SetTime(&RtcHandle,&stimestructure,FORMAT_BCD) != HAL_OK) + if(HAL_RTC_SetTime(&RTCHandle,&stimestructure,FORMAT_BCD) != HAL_OK) { /* Initialization Error */ //Error_Handler(); @@ -217,25 +217,86 @@ static void RTC_CalendarConfig(void) } /*##-3- Writes a data in a RTC Backup data Register0 #######################*/ - HAL_RTCEx_BKUPWrite(&RtcHandle,RTC_BKP_DR0,0x32F2); + HAL_RTCEx_BKUPWrite(&RTCHandle,RTC_BKP_DR0,0x32F2); } /******************************************************************************/ // Micro Python bindings -mp_obj_t pyb_rtc_info(void) { - return mp_obj_new_int(rtc_info); -} +typedef struct _pyb_rtc_obj_t { + mp_obj_base_t base; +} pyb_rtc_obj_t; -MP_DEFINE_CONST_FUN_OBJ_0(pyb_rtc_info_obj, pyb_rtc_info); +STATIC const pyb_rtc_obj_t pyb_rtc_obj = {{&pyb_rtc_type}}; -mp_obj_t pyb_rtc_read(void) { - RTC_TimeTypeDef time; - RTC_DateTypeDef date; - HAL_RTC_GetTime(&RtcHandle, &time, FORMAT_BIN); - HAL_RTC_GetDate(&RtcHandle, &date, FORMAT_BIN); - printf("%02d-%02d-%04d %02d:%02d:%02d\n",date.Date, date.Month, 2000 + date.Year, time.Hours, time.Minutes, time.Seconds); - return mp_const_none; +STATIC mp_obj_t pyb_rtc_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { + // check arguments + mp_check_nargs(n_args, 0, 0, n_kw, false); + + // return constant object + return (mp_obj_t)&pyb_rtc_obj; } -MP_DEFINE_CONST_FUN_OBJ_0(pyb_rtc_read_obj, pyb_rtc_read); +mp_obj_t pyb_rtc_info(mp_obj_t self_in) { + return mp_obj_new_int(rtc_info); +} +MP_DEFINE_CONST_FUN_OBJ_1(pyb_rtc_info_obj, pyb_rtc_info); + +mp_obj_t pyb_rtc_datetime(uint n_args, const mp_obj_t *args) { + if (n_args == 1) { + // get date and time + // note: need to call get time then get date to correctly access the registers + RTC_DateTypeDef date; + RTC_TimeTypeDef time; + HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN); + HAL_RTC_GetDate(&RTCHandle, &date, FORMAT_BIN); + mp_obj_t tuple[8] = { + mp_obj_new_int(2000 + date.Year), + mp_obj_new_int(date.Month), + mp_obj_new_int(date.Date), + mp_obj_new_int(date.WeekDay), + mp_obj_new_int(time.Hours), + mp_obj_new_int(time.Minutes), + mp_obj_new_int(time.Seconds), + mp_obj_new_int(time.SubSeconds), + }; + return mp_obj_new_tuple(8, tuple); + } else { + // set date and time + mp_obj_t *items; + mp_obj_get_array_fixed_n(args[1], 8, &items); + + RTC_DateTypeDef date; + date.Year = mp_obj_get_int(items[0]) - 2000; + date.Month = mp_obj_get_int(items[1]); + date.Date = mp_obj_get_int(items[2]); + date.WeekDay = mp_obj_get_int(items[3]); + HAL_RTC_SetDate(&RTCHandle, &date, FORMAT_BCD); + + RTC_TimeTypeDef time; + time.Hours = mp_obj_get_int(items[4]); + time.Minutes = mp_obj_get_int(items[5]); + time.Seconds = mp_obj_get_int(items[6]); + time.SubSeconds = mp_obj_get_int(items[7]); + time.TimeFormat = RTC_HOURFORMAT12_AM; + time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; + time.StoreOperation = RTC_STOREOPERATION_SET; + HAL_RTC_SetTime(&RTCHandle, &time, FORMAT_BCD); + + return mp_const_none; + } +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_datetime_obj, 1, 2, pyb_rtc_datetime); + +STATIC const mp_map_elem_t pyb_rtc_locals_dict_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_rtc_info_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_datetime), (mp_obj_t)&pyb_rtc_datetime_obj }, +}; +STATIC MP_DEFINE_CONST_DICT(pyb_rtc_locals_dict, pyb_rtc_locals_dict_table); + +const mp_obj_type_t pyb_rtc_type = { + { &mp_type_type }, + .name = MP_QSTR_RTC, + .make_new = pyb_rtc_make_new, + .locals_dict = (mp_obj_t)&pyb_rtc_locals_dict, +}; diff --git a/stmhal/rtc.h b/stmhal/rtc.h index fee4da8882..22d8af97cb 100644 --- a/stmhal/rtc.h +++ b/stmhal/rtc.h @@ -1,4 +1,4 @@ -void rtc_init(void); +extern RTC_HandleTypeDef RTCHandle; +extern const mp_obj_type_t pyb_rtc_type; -MP_DECLARE_CONST_FUN_OBJ(pyb_rtc_info_obj); -MP_DECLARE_CONST_FUN_OBJ(pyb_rtc_read_obj); +void rtc_init(void); |