Skip to content

Commit 9e44383

Browse files
committed
cc3200: Add power management framework. Add mpcallback class.
Supports suspend and hibernate modes. Waking is possible throug GPIO and WLAN. The mpcallback class is generic and can be reused by other classes.
1 parent 73aee8d commit 9e44383

34 files changed

Lines changed: 1279 additions & 1168 deletions

cc3200/FreeRTOS/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
#define configUSE_TICK_HOOK 1
8484
#define configCPU_CLOCK_HZ ( ( unsigned long ) 80000000 )
8585
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
86-
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 64 )
86+
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 72 )
8787
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 16384 ) )
8888
#define configMAX_TASK_NAME_LEN ( 8 )
8989
#define configUSE_TRACE_FACILITY 0

cc3200/application.mk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ APP_MISC_SRC_C = $(addprefix misc/,\
7676
FreeRTOSHooks.c \
7777
pin_named_pins.c \
7878
help.c \
79+
mpcallback.c \
7980
mperror.c \
8081
mpexception.c \
8182
pin_defs_cc3200.c \
@@ -89,8 +90,8 @@ APP_MODS_SRC_C = $(addprefix mods/,\
8990
modutime.c \
9091
modwlan.c \
9192
pybadc.c \
92-
pybi2c.c \
9393
pybpin.c \
94+
pybi2c.c \
9495
pybrtc.c \
9596
pybsd.c \
9697
pybsleep.c \
@@ -128,6 +129,7 @@ APP_UTIL_SRC_C = $(addprefix util/,\
128129

129130
APP_UTIL_SRC_S = $(addprefix util/,\
130131
gchelper.s \
132+
sleeprestore.s \
131133
)
132134

133135
APP_MAIN_SRC_C = \

cc3200/boards/LAUNCHXL/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@
4646
#define MICROPY_SYS_LED_PORT_PIN GPIO_PIN_1
4747
#define MICROPY_SAFE_BOOT_PORT_PIN GPIO_PIN_6
4848

49+
#define MICROPY_PORT_SFLASH_BLOCK_COUNT 32
50+

cc3200/boards/cc3200_prefix.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
{ \
4444
{ &pin_type }, \
4545
.name = MP_QSTR_ ## p_pin_name, \
46-
.callback = NULL, \
46+
.callback = mp_const_none, \
4747
.port = PORT_A ## p_port, \
4848
.type = PIN_TYPE_STD, \
4949
.bit = (p_bit), \

cc3200/boards/make-pins.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ def set_is_board_pin(self):
4040
self.board_pin = True
4141

4242
def print(self):
43-
print('const pin_obj_t pin_{:6s} = PIN({:6s}, {:1d}, {:3d}, {:2d});'.format(
43+
print('pin_obj_t pin_{:6s} = PIN({:6s}, {:1d}, {:3d}, {:2d});'.format(
4444
self.name, self.name, self.port, self.gpio_bit, self.pin_num))
4545

4646
def print_header(self, hdr_file):
47-
hdr_file.write('extern const pin_obj_t pin_{:s};\n'.
48-
format(self.name))
47+
hdr_file.write('extern pin_obj_t pin_{:s};\n'.format(self.name))
4948

5049

5150
class Pins(object):

cc3200/bootmgr/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@
5757
//*****************************************************************************
5858
// Local Constants
5959
//*****************************************************************************
60-
#define SL_STOP_TIMEOUT 250
60+
#define SL_STOP_TIMEOUT 35
6161
#define BOOTMGR_HASH_ALGO SHAMD5_ALGO_MD5
6262
#define BOOTMGR_HASH_SIZE 32
6363
#define BOOTMGR_BUFF_SIZE 512
6464

6565
#define BOOTMGR_WAIT_SAFE_MODE_MS 1600
6666
#define BOOTMGR_WAIT_SAFE_MODE_TOOGLE_MS 200
6767

68-
#define BOOTMGR_SAFE_MODE_ENTER_MS 700
69-
#define BOOTMGR_SAFE_MODE_ENTER_TOOGLE_MS 70
68+
#define BOOTMGR_SAFE_MODE_ENTER_MS 800
69+
#define BOOTMGR_SAFE_MODE_ENTER_TOOGLE_MS 80
7070

7171
//*****************************************************************************
7272
// Exported functions declarations
@@ -307,7 +307,7 @@ int main (void) {
307307
bootmgr_board_init();
308308

309309
// start simplelink since we need it to access the sflash
310-
sl_Start(NULL, NULL, NULL);
310+
sl_Start(0, 0, 0);
311311

312312
// if a boot info file is found, load it, else, create a new one with the default boot info
313313
if (!sl_FsOpen((unsigned char *)IMG_BOOT_INFO, FS_MODE_OPEN_READ, NULL, &fhandle)) {

cc3200/fatfs/src/drivers/sflash_diskio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define SFLASH_DISKIO_H_
33

44
#define SFLASH_BLOCK_SIZE 2048
5-
#define SFLASH_BLOCK_COUNT 32 // makes for 64KB of space
5+
#define SFLASH_BLOCK_COUNT MICROPY_PORT_SFLASH_BLOCK_COUNT
66
#define SFLASH_SECTOR_SIZE 512
77
#define SFLASH_SECTOR_COUNT ((SFLASH_BLOCK_SIZE * SFLASH_BLOCK_COUNT) / SFLASH_SECTOR_SIZE)
88
#define SFLASH_SECTORS_PER_BLOCK (SFLASH_BLOCK_SIZE / SFLASH_SECTOR_SIZE)

cc3200/hal/startup_gcc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void ResetISR(void);
6565
#ifdef DEBUG
6666
static void NmiSR(void) __attribute__( ( naked ) );
6767
static void FaultISR( void ) __attribute__( ( naked ) );
68-
void HardFault_HandlerC(unsigned long *hardfault_args);
68+
void HardFault_HandlerC(uint32_t *pulFaultStackAddress);
6969
static void BusFaultHandler(void) __attribute__( ( naked ) );
7070
#endif
7171
static void IntDefaultHandler(void) __attribute__( ( naked ) );

cc3200/misc/mpcallback.c

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015 Daniel Campora
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/mpconfig.h"
28+
#include MICROPY_HAL_H
29+
#include "py/obj.h"
30+
#include "py/runtime.h"
31+
#include "py/gc.h"
32+
#include "inc/hw_types.h"
33+
#include "interrupt.h"
34+
#include "pybsleep.h"
35+
#include "mpcallback.h"
36+
#include "mpexception.h"
37+
#include "mperror.h"
38+
39+
40+
/******************************************************************************
41+
DECLARE PRIVATE FUNCTIONS
42+
******************************************************************************/
43+
STATIC mpcallback_obj_t *mpcallback_find (mp_obj_t parent);
44+
45+
/******************************************************************************
46+
DEFINE PUBLIC DATA
47+
******************************************************************************/
48+
const mp_arg_t mpcallback_init_args[] = {
49+
{ MP_QSTR_intmode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
50+
{ MP_QSTR_handler, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
51+
{ MP_QSTR_priority, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
52+
{ MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
53+
{ MP_QSTR_wake, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PYB_PWR_MODE_ACTIVE_IDLE } },
54+
};
55+
56+
/******************************************************************************
57+
DEFINE PUBLIC FUNCTIONS
58+
******************************************************************************/
59+
void mpcallback_init0 (void) {
60+
// initialize the callback objects list
61+
mp_obj_list_init(&MP_STATE_PORT(mpcallback_obj_list), 0);
62+
}
63+
64+
mp_obj_t mpcallback_new (mp_obj_t parent, mp_obj_t handler, const mp_cb_methods_t *methods) {
65+
mpcallback_obj_t *self = m_new_obj(mpcallback_obj_t);
66+
self->base.type = &pyb_callback_type;
67+
self->handler = handler;
68+
self->parent = parent;
69+
self->methods = (mp_cb_methods_t *)methods;
70+
// remove any old callback if present
71+
mpcallback_remove(self->parent);
72+
mp_obj_list_append(&MP_STATE_PORT(mpcallback_obj_list), self);
73+
return self;
74+
}
75+
76+
void mpcallback_remove (const mp_obj_t parent) {
77+
mpcallback_obj_t *callback_obj;
78+
if ((callback_obj = mpcallback_find(parent))) {
79+
mp_obj_list_remove(&MP_STATE_PORT(mpcallback_obj_list), callback_obj);
80+
}
81+
}
82+
83+
uint mpcallback_translate_priority (uint priority) {
84+
if (priority < 1 || priority > 7) {
85+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
86+
}
87+
88+
switch (priority) {
89+
case 1:
90+
return INT_PRIORITY_LVL_7;
91+
case 2:
92+
return INT_PRIORITY_LVL_6;
93+
case 3:
94+
return INT_PRIORITY_LVL_5;
95+
case 4:
96+
return INT_PRIORITY_LVL_4;
97+
case 5:
98+
return INT_PRIORITY_LVL_3;
99+
case 6:
100+
return INT_PRIORITY_LVL_2;
101+
case 7:
102+
return INT_PRIORITY_LVL_1;
103+
default:
104+
return INT_PRIORITY_LVL_7;
105+
}
106+
}
107+
108+
void mpcallback_handler (mp_obj_t self_in) {
109+
mpcallback_obj_t *self = self_in;
110+
if (self->handler != mp_const_none) {
111+
// disable interrupts to avoid nesting
112+
uint primsk = disable_irq();
113+
// when executing code within a handler we must lock the GC to prevent
114+
// any memory allocations. We must also catch any exceptions.
115+
gc_lock();
116+
nlr_buf_t nlr;
117+
if (nlr_push(&nlr) == 0) {
118+
mp_call_function_1(self->handler, self->parent);
119+
nlr_pop();
120+
}
121+
else {
122+
// uncaught exception; disable the callback so that it doesn't run again
123+
self->methods->disable (self->parent);
124+
self->handler = mp_const_none;
125+
// printing an exception here will cause a stack overflow that will end up in
126+
// a hard fault, so is better to signal the uncaught (probably non-recoverable)
127+
// exception by blinking the system led instead.
128+
mperror_signal_error();
129+
}
130+
gc_unlock();
131+
enable_irq(primsk);
132+
}
133+
}
134+
135+
/******************************************************************************
136+
DEFINE PRIVATE FUNCTIONS
137+
******************************************************************************/
138+
STATIC mpcallback_obj_t *mpcallback_find (mp_obj_t parent) {
139+
for (mp_uint_t i = 0; i < MP_STATE_PORT(mpcallback_obj_list).len; i++) {
140+
// search for the object and then remove it
141+
mpcallback_obj_t *callback_obj = ((mpcallback_obj_t *)(MP_STATE_PORT(mpcallback_obj_list).items[i]));
142+
if (callback_obj->parent == parent) {
143+
return callback_obj;
144+
}
145+
}
146+
return NULL;
147+
}
148+
149+
/******************************************************************************/
150+
// Micro Python bindings
151+
152+
/// \method init()
153+
/// Initializes the interrupt callback. With no parameters passed, everything will default
154+
/// to the values assigned to mpcallback_init_args[].
155+
STATIC mp_obj_t callback_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
156+
mpcallback_obj_t *self = pos_args[0];
157+
// this is a bit of a hack, but it let us reuse the callback_create method from our parent
158+
((mp_obj_t *)pos_args)[0] = self->parent;
159+
self->methods->init (n_args, pos_args, kw_args);
160+
return mp_const_none;
161+
}
162+
MP_DEFINE_CONST_FUN_OBJ_KW(callback_init_obj, 1, callback_init);
163+
164+
/// \method enable()
165+
/// Enables the interrupt callback
166+
STATIC mp_obj_t callback_enable (mp_obj_t self_in) {
167+
mpcallback_obj_t *self = self_in;
168+
self->methods->enable(self->parent);
169+
return mp_const_none;
170+
}
171+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(callback_enable_obj, callback_enable);
172+
173+
/// \method disable()
174+
/// Disables the interrupt callback
175+
STATIC mp_obj_t callback_disable (mp_obj_t self_in) {
176+
mpcallback_obj_t *self = self_in;
177+
self->methods->disable(self->parent);
178+
return mp_const_none;
179+
}
180+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(callback_disable_obj, callback_disable);
181+
182+
/// \method \call()
183+
/// Triggers the interrupt callback
184+
STATIC mp_obj_t callback_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
185+
mp_arg_check_num(n_args, n_kw, 0, 0, false);
186+
mpcallback_handler (self_in);
187+
return mp_const_none;
188+
}
189+
190+
STATIC const mp_map_elem_t callback_locals_dict_table[] = {
191+
// instance methods
192+
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&callback_init_obj },
193+
{ MP_OBJ_NEW_QSTR(MP_QSTR_enable), (mp_obj_t)&callback_enable_obj },
194+
{ MP_OBJ_NEW_QSTR(MP_QSTR_disable), (mp_obj_t)&callback_disable_obj },
195+
};
196+
197+
STATIC MP_DEFINE_CONST_DICT(callback_locals_dict, callback_locals_dict_table);
198+
199+
const mp_obj_type_t pyb_callback_type = {
200+
{ &mp_type_type },
201+
.name = MP_QSTR_callback,
202+
.call = callback_call,
203+
.locals_dict = (mp_obj_t)&callback_locals_dict,
204+
};
205+

cc3200/misc/mpcallback.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015 Daniel Campora
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MPCALLBACK_H_
28+
#define MPCALLBACK_H_
29+
30+
/******************************************************************************
31+
DEFINE CONSTANTS
32+
******************************************************************************/
33+
#define mpcallback_INIT_NUM_ARGS 5
34+
35+
/******************************************************************************
36+
DEFINE TYPES
37+
******************************************************************************/
38+
typedef void (*mp_cb_method_t) (mp_obj_t self);
39+
typedef mp_obj_t (*mp_cb_init_t) (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
40+
41+
typedef struct {
42+
mp_cb_init_t init;
43+
mp_cb_method_t enable;
44+
mp_cb_method_t disable;
45+
} mp_cb_methods_t;
46+
47+
typedef struct {
48+
mp_obj_base_t base;
49+
mp_obj_t parent;
50+
mp_obj_t handler;
51+
mp_cb_methods_t *methods;
52+
} mpcallback_obj_t;
53+
54+
/******************************************************************************
55+
DECLARE EXPORTED DATA
56+
******************************************************************************/
57+
extern const mp_arg_t mpcallback_init_args[];
58+
extern const mp_obj_type_t pyb_callback_type;
59+
60+
/******************************************************************************
61+
DECLARE PUBLIC FUNCTIONS
62+
******************************************************************************/
63+
void mpcallback_init0 (void);
64+
mp_obj_t mpcallback_new (mp_obj_t parent, mp_obj_t handler, const mp_cb_methods_t *methods);
65+
void mpcallback_remove (const mp_obj_t parent);
66+
void mpcallback_handler (mp_obj_t self_in);
67+
uint mpcallback_translate_priority (uint priority);
68+
mp_obj_t mpcallback_new (mp_obj_t parent, mp_obj_t handler, const mp_cb_methods_t *methods);
69+
70+
#endif /* MPCALLBACK_H_ */

0 commit comments

Comments
 (0)