Skip to content

Commit fa655ce

Browse files
author
Daniel Campora
committed
cc3200: Improve interrupt handling and fix bug in HAL_Delay().
1 parent 194c8c7 commit fa655ce

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

cc3200/hal/cc3200_hal.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "mpexception.h"
4646
#include "telnet.h"
4747
#include "pybuart.h"
48+
#include "utils.h"
4849

4950
#ifdef USE_FREERTOS
5051
#include "FreeRTOS.h"
@@ -107,16 +108,23 @@ uint32_t HAL_GetTick(void) {
107108
}
108109

109110
void HAL_Delay(uint32_t delay) {
110-
#ifdef USE_FREERTOS
111-
vTaskDelay (delay / portTICK_PERIOD_MS);
112-
#else
113-
uint32_t start = HAL_tickCount;
114-
// Wraparound of tick is taken care of by 2's complement arithmetic.
115-
while (HAL_tickCount - start < delay) {
116-
// Enter sleep mode, waiting for (at least) the SysTick interrupt.
117-
__WFI();
111+
// only if we are not within interrupt context
112+
if ((HAL_NVIC_INT_CTRL_REG & HAL_VECTACTIVE_MASK) == 0) {
113+
#ifdef USE_FREERTOS
114+
vTaskDelay (delay / portTICK_PERIOD_MS);
115+
#else
116+
uint32_t start = HAL_tickCount;
117+
// wraparound of tick is taken care of by 2's complement arithmetic.
118+
while (HAL_tickCount - start < delay) {
119+
// enter sleep mode, waiting for (at least) the SysTick interrupt.
120+
__WFI();
121+
}
122+
#endif
123+
} else {
124+
for (int ms = 1; ms <= delay; ms++) {
125+
UtilsDelay(UTILS_DELAY_US_TO_COUNT(ms * 1000));
126+
}
118127
}
119-
#endif
120128
}
121129

122130
void mp_hal_set_interrupt_char (int c) {

cc3200/misc/mpcallback.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "std.h"
28+
2729
#include "py/mpconfig.h"
2830
#include MICROPY_HAL_H
2931
#include "py/obj.h"
@@ -124,10 +126,8 @@ uint mpcallback_translate_priority (uint priority) {
124126
void mpcallback_handler (mp_obj_t self_in) {
125127
mpcallback_obj_t *self = self_in;
126128
if (self && self->handler != mp_const_none) {
127-
// disable interrupts to avoid nesting
128-
uint primsk = disable_irq();
129129
// when executing code within a handler we must lock the GC to prevent
130-
// any memory allocations. We must also catch any exceptions.
130+
// any memory allocations.
131131
gc_lock();
132132
nlr_buf_t nlr;
133133
if (nlr_push(&nlr) == 0) {
@@ -138,13 +138,13 @@ void mpcallback_handler (mp_obj_t self_in) {
138138
// uncaught exception; disable the callback so that it doesn't run again
139139
self->methods->disable (self->parent);
140140
self->handler = mp_const_none;
141-
// printing an exception here will cause a stack overflow that will end up in
142-
// a hard fault, so is better to signal the uncaught (probably non-recoverable)
143-
// exception by blinking the system led instead.
141+
// signal the error using the heart beat led and print an
142+
// exception message as well
144143
mperror_signal_error();
144+
printf("Uncaught exception in callback handler\n");
145+
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
145146
}
146147
gc_unlock();
147-
enable_irq(primsk);
148148
}
149149
}
150150

cc3200/mods/modutime.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
125125
/// \function sleep(seconds)
126126
/// Sleep for the given number of seconds.
127127
STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
128-
HAL_Delay(mp_obj_get_int(seconds_o) * 1000);
128+
int32_t sleep_s = mp_obj_get_int(seconds_o);
129+
if (sleep_s > 0) {
130+
HAL_Delay(sleep_s * 1000);
131+
}
129132
return mp_const_none;
130133
}
131134
MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);

cc3200/mods/pybuart.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@
8989
/******************************************************************************
9090
DEFINE CONSTANTS
9191
******************************************************************************/
92-
#define PYBUART_TX_WAIT_MS 1
93-
#define PYBUART_TX_MAX_TIMEOUT_MS 5
92+
#define PYBUART_TX_WAIT_US (50)
93+
#define PYBUART_TX_MAX_TIMEOUT_MS (5)
9494

9595
/******************************************************************************
9696
DECLARE PRIVATE FUNCTIONS
@@ -156,10 +156,10 @@ bool uart_tx_char(pyb_uart_obj_t *self, int c) {
156156
uint32_t timeout = 0;
157157

158158
while (!MAP_UARTCharPutNonBlocking(self->reg, c)) {
159-
if (timeout++ > (PYBUART_TX_MAX_TIMEOUT_MS / PYBUART_TX_WAIT_MS)) {
159+
if (timeout++ > ((PYBUART_TX_MAX_TIMEOUT_MS * 1000) / PYBUART_TX_WAIT_US)) {
160160
return false;
161161
}
162-
HAL_Delay (PYBUART_TX_WAIT_MS);
162+
UtilsDelay(UTILS_DELAY_US_TO_COUNT(PYBUART_TX_WAIT_US));
163163
}
164164
return true;
165165
}

0 commit comments

Comments
 (0)