cc3200: Add callback support to the UART for RX interrupts.

This commit is contained in:
Daniel Campora 2015-03-26 10:25:28 +01:00
parent e909e38871
commit 2d717ad97a
13 changed files with 143 additions and 110 deletions

View File

@ -36,6 +36,7 @@
#define MICROPY_STDIO_UART PYB_UART_0 #define MICROPY_STDIO_UART PYB_UART_0
#define MICROPY_STDIO_UART_BAUD 115200 #define MICROPY_STDIO_UART_BAUD 115200
#define MICROPY_STDIO_UART_RX_BUF_SIZE 128
#define MICROPY_SYS_LED_PRCM PRCM_GPIOA1 #define MICROPY_SYS_LED_PRCM PRCM_GPIOA1
#define MICROPY_SAFE_BOOT_PRCM PRCM_GPIOA2 #define MICROPY_SAFE_BOOT_PRCM PRCM_GPIOA2

View File

@ -36,6 +36,7 @@
#define MICROPY_STDIO_UART PYB_UART_0 #define MICROPY_STDIO_UART PYB_UART_0
#define MICROPY_STDIO_UART_BAUD 115200 #define MICROPY_STDIO_UART_BAUD 115200
#define MICROPY_STDIO_UART_RX_BUF_SIZE 128
#define MICROPY_SYS_LED_PRCM PRCM_GPIOA3 #define MICROPY_SYS_LED_PRCM PRCM_GPIOA3
#define MICROPY_SAFE_BOOT_PRCM PRCM_GPIOA3 #define MICROPY_SAFE_BOOT_PRCM PRCM_GPIOA3

View File

@ -36,6 +36,7 @@
#define MICROPY_STDIO_UART PYB_UART_0 #define MICROPY_STDIO_UART PYB_UART_0
#define MICROPY_STDIO_UART_BAUD 115200 #define MICROPY_STDIO_UART_BAUD 115200
#define MICROPY_STDIO_UART_RX_BUF_SIZE 128
#define MICROPY_SYS_LED_PRCM PRCM_GPIOA3 #define MICROPY_SYS_LED_PRCM PRCM_GPIOA3
#define MICROPY_SAFE_BOOT_PRCM PRCM_GPIOA3 #define MICROPY_SAFE_BOOT_PRCM PRCM_GPIOA3

View File

@ -45,7 +45,7 @@ const mp_arg_t mpcallback_init_args[] = {
{ MP_QSTR_handler, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, { MP_QSTR_handler, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_priority, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, { MP_QSTR_priority, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
{ MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_wake, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PYB_PWR_MODE_ACTIVE} }, { MP_QSTR_wakes, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PYB_PWR_MODE_ACTIVE} },
}; };
/****************************************************************************** /******************************************************************************

View File

@ -911,7 +911,7 @@ STATIC mp_obj_t wlan_scan(mp_obj_t self_in) {
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_scan_obj, wlan_scan); STATIC MP_DEFINE_CONST_FUN_OBJ_1(wlan_scan_obj, wlan_scan);
/// \method callback(handler, intmode, value, priority, pwrmode) /// \method callback(handler, pwrmode)
/// Creates a callback object associated with WLAN /// Creates a callback object associated with WLAN
/// min num of arguments is 1 (pwrmode) /// min num of arguments is 1 (pwrmode)
STATIC mp_obj_t wlan_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { STATIC mp_obj_t wlan_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
@ -921,7 +921,7 @@ STATIC mp_obj_t wlan_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_ma
wlan_obj_t *self = pos_args[0]; wlan_obj_t *self = pos_args[0];
mp_obj_t _callback = mpcallback_find(self); mp_obj_t _callback = mpcallback_find(self);
// check if any parameters were passed // check if any parameters were passed
if (kw_args->used > 0 || _callback == mp_const_none) { if (kw_args->used > 0 || !_callback) {
// check the power mode // check the power mode
if (args[4].u_int != PYB_PWR_MODE_LPDS) { if (args[4].u_int != PYB_PWR_MODE_LPDS) {
// throw an exception since WLAN only supports LPDS mode // throw an exception since WLAN only supports LPDS mode

View File

@ -129,13 +129,6 @@ STATIC void i2c_init (pyb_i2c_obj_t *self) {
MAP_I2CMasterInitExpClk(I2CA0_BASE, self->baudrate); MAP_I2CMasterInitExpClk(I2CA0_BASE, self->baudrate);
} }
STATIC void i2c_deinit(void) {
MAP_I2CMasterDisable(I2CA0_BASE);
MAP_PRCMPeripheralClkDisable(PRCM_I2CA0, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
// invalidate the baudrate
pyb_i2c_obj.baudrate = 0;
}
STATIC bool pyb_i2c_transaction(uint cmd) { STATIC bool pyb_i2c_transaction(uint cmd) {
// Convert the timeout to microseconds // Convert the timeout to microseconds
int32_t timeout = PYBI2C_TRANSC_TIMEOUT_MS * 1000; int32_t timeout = PYBI2C_TRANSC_TIMEOUT_MS * 1000;
@ -319,7 +312,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_i2c_init_obj, pyb_i2c_init);
/// \method deinit() /// \method deinit()
/// Turn off the I2C bus. /// Turn off the I2C bus.
STATIC mp_obj_t pyb_i2c_deinit(mp_obj_t self_in) { STATIC mp_obj_t pyb_i2c_deinit(mp_obj_t self_in) {
i2c_deinit(); // disable the peripheral
MAP_I2CMasterDisable(I2CA0_BASE);
MAP_PRCMPeripheralClkDisable(PRCM_I2CA0, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
// invalidate the baudrate
pyb_i2c_obj.baudrate = 0;
// unregister it with the sleep module // unregister it with the sleep module
pybsleep_remove ((const mp_obj_t)self_in); pybsleep_remove ((const mp_obj_t)self_in);
return mp_const_none; return mp_const_none;

View File

@ -617,7 +617,7 @@ STATIC mp_obj_t pin_af(mp_obj_t self_in) {
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_af_obj, pin_af); STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_af_obj, pin_af);
/// \method callback(method, intmode, value, priority, pwrmode) /// \method callback(method, intmode, priority, pwrmode)
/// Creates a callback object associated to a pin /// Creates a callback object associated to a pin
/// min num of arguments is 1 (intmode) /// min num of arguments is 1 (intmode)
STATIC mp_obj_t pin_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { STATIC mp_obj_t pin_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
@ -627,7 +627,7 @@ STATIC mp_obj_t pin_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map
pin_obj_t *self = pos_args[0]; pin_obj_t *self = pos_args[0];
// check if any parameters were passed // check if any parameters were passed
mp_obj_t _callback = mpcallback_find(self); mp_obj_t _callback = mpcallback_find(self);
if (kw_args->used > 0 || _callback == mp_const_none) { if (kw_args->used > 0 || !_callback) {
// convert the priority to the correct value // convert the priority to the correct value
uint priority = mpcallback_translate_priority (args[2].u_int); uint priority = mpcallback_translate_priority (args[2].u_int);
// verify the interrupt mode // verify the interrupt mode
@ -840,8 +840,6 @@ STATIC void EXTI_Handler(uint port) {
pin_obj_t *self = (pin_obj_t *)pin_find_pin_by_port_bit(&pin_cpu_pins_locals_dict, port, bit); pin_obj_t *self = (pin_obj_t *)pin_find_pin_by_port_bit(&pin_cpu_pins_locals_dict, port, bit);
mp_obj_t _callback = mpcallback_find(self); mp_obj_t _callback = mpcallback_find(self);
if (_callback) {
mpcallback_handler(_callback); mpcallback_handler(_callback);
} }
}

View File

@ -179,7 +179,7 @@ mp_obj_t pyb_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_datetime_obj, 1, 2, pyb_rtc_datetime); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_datetime_obj, 1, 2, pyb_rtc_datetime);
/// \method callback(handler, intmode, value, priority, pwrmode) /// \method callback(handler, value, pwrmode)
/// Creates a callback object associated with the real time clock /// Creates a callback object associated with the real time clock
/// min num of arguments is 1 (value). The value is the alarm time /// min num of arguments is 1 (value). The value is the alarm time
/// in the future, in msec /// in the future, in msec
@ -189,7 +189,7 @@ STATIC mp_obj_t pyb_rtc_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp
// check if any parameters were passed // check if any parameters were passed
mp_obj_t _callback = mpcallback_find((mp_obj_t)&pyb_rtc_obj); mp_obj_t _callback = mpcallback_find((mp_obj_t)&pyb_rtc_obj);
if (kw_args->used > 0 || _callback == mp_const_none) { if (kw_args->used > 0 || !_callback) {
uint32_t f_mseconds = args[3].u_int; uint32_t f_mseconds = args[3].u_int;
uint32_t seconds; uint32_t seconds;
uint16_t mseconds; uint16_t mseconds;
@ -201,6 +201,10 @@ STATIC mp_obj_t pyb_rtc_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp
seconds += f_mseconds / 1000; seconds += f_mseconds / 1000;
mseconds += f_mseconds - ((f_mseconds / 1000) * 1000); mseconds += f_mseconds - ((f_mseconds / 1000) * 1000);
// disable the interrupt before updating anything
// (the object is not relevant here, the function already knows it)
pyb_rtc_callback_disable(NULL);
// set the match value // set the match value
MAP_PRCMRTCMatchSet(seconds, mseconds); MAP_PRCMRTCMatchSet(seconds, mseconds);

View File

@ -110,13 +110,6 @@ STATIC void pybspi_init (const pyb_spi_obj_t *self) {
MAP_SPIEnable(GSPI_BASE); MAP_SPIEnable(GSPI_BASE);
} }
STATIC void pybspi_deinit(void) {
MAP_SPIDisable(GSPI_BASE);
MAP_PRCMPeripheralClkDisable(PRCM_GSPI, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
// invalidate the baudrate
pyb_spi_obj.baudrate = 0;
}
STATIC void pybspi_tx (pyb_spi_obj_t *self, const void *data) { STATIC void pybspi_tx (pyb_spi_obj_t *self, const void *data) {
uint32_t txdata = 0xFFFFFFFF; uint32_t txdata = 0xFFFFFFFF;
if (data) { if (data) {
@ -278,7 +271,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_init_obj, 1, pyb_spi_init);
/// \method deinit() /// \method deinit()
/// Turn off the spi bus. /// Turn off the spi bus.
STATIC mp_obj_t pyb_spi_deinit(mp_obj_t self_in) { STATIC mp_obj_t pyb_spi_deinit(mp_obj_t self_in) {
pybspi_deinit(); // disable the peripheral
MAP_SPIDisable(GSPI_BASE);
MAP_PRCMPeripheralClkDisable(PRCM_GSPI, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
// invalidate the baudrate
pyb_spi_obj.baudrate = 0;
// unregister it with the sleep module // unregister it with the sleep module
pybsleep_remove((const mp_obj_t)self_in); pybsleep_remove((const mp_obj_t)self_in);
return mp_const_none; return mp_const_none;

View File

@ -47,6 +47,7 @@
#include "pybuart.h" #include "pybuart.h"
#include "pybioctl.h" #include "pybioctl.h"
#include "pybsleep.h" #include "pybsleep.h"
#include "mpcallback.h"
#include "mpexception.h" #include "mpexception.h"
#include "py/mpstate.h" #include "py/mpstate.h"
#include "osi.h" #include "osi.h"
@ -98,6 +99,8 @@ STATIC bool uart_rx_wait (pyb_uart_obj_t *self, uint32_t timeout);
STATIC void UARTGenericIntHandler(uint32_t uart_id); STATIC void UARTGenericIntHandler(uint32_t uart_id);
STATIC void UART0IntHandler(void); STATIC void UART0IntHandler(void);
STATIC void UART1IntHandler(void); STATIC void UART1IntHandler(void);
STATIC void uart_callback_enable (mp_obj_t self_in);
STATIC void uart_callback_disable (mp_obj_t self_in);
/****************************************************************************** /******************************************************************************
DEFINE PRIVATE TYPES DEFINE PRIVATE TYPES
@ -115,13 +118,14 @@ struct _pyb_uart_obj_t {
uint16_t read_buf_len; // len in chars; buf can hold len-1 chars uint16_t read_buf_len; // len in chars; buf can hold len-1 chars
volatile uint16_t read_buf_head; // indexes first empty slot volatile uint16_t read_buf_head; // indexes first empty slot
uint16_t read_buf_tail; // indexes first full slot (not full if equals head) uint16_t read_buf_tail; // indexes first full slot (not full if equals head)
bool enabled; byte peripheral;
}; };
/****************************************************************************** /******************************************************************************
DECLARE PRIVATE DATA DECLARE PRIVATE DATA
******************************************************************************/ ******************************************************************************/
STATIC pyb_uart_obj_t pyb_uart_obj[PYB_NUM_UARTS]; STATIC pyb_uart_obj_t pyb_uart_obj[PYB_NUM_UARTS];
STATIC const mp_cb_methods_t uart_cb_methods;
/****************************************************************************** /******************************************************************************
DEFINE PUBLIC FUNCTIONS DEFINE PUBLIC FUNCTIONS
@ -175,41 +179,60 @@ void uart_tx_strn_cooked(pyb_uart_obj_t *self, const char *str, uint len) {
} }
} }
mp_obj_t uart_callback_new (pyb_uart_obj_t *self, mp_obj_t handler, uint rxbuffer_size, mp_int_t priority) {
// disable the uart interrupts before updating anything
uart_callback_disable (self);
if (self->uart_id == PYB_UART_0) {
MAP_IntPrioritySet(INT_UARTA0, priority);
MAP_UARTIntRegister(self->reg, UART0IntHandler);
}
else {
MAP_IntPrioritySet(INT_UARTA1, priority);
MAP_UARTIntRegister(self->reg, UART1IntHandler);
}
// check the rx buffer size
if (rxbuffer_size > 0) {
// allocate the read buffer
self->read_buf_len = rxbuffer_size;
self->read_buf = m_new(byte, rxbuffer_size);
}
// create the callback
mp_obj_t _callback = mpcallback_new ((mp_obj_t)self, handler, &uart_cb_methods);
// enable the interrupts now
uart_callback_enable (self);
return _callback;
}
/****************************************************************************** /******************************************************************************
DEFINE PRIVATE FUNCTIONS DEFINE PRIVATE FUNCTIONS
******************************************************************************/ ******************************************************************************/
// assumes init parameters have been set up correctly // assumes init parameters have been set up correctly
STATIC void uart_init (pyb_uart_obj_t *self) { STATIC void uart_init (pyb_uart_obj_t *self) {
uint uartPerh; if (self->uart_id == PYB_UART_0) {
switch (self->uart_id) {
case PYB_UART_0:
self->reg = UARTA0_BASE; self->reg = UARTA0_BASE;
uartPerh = PRCM_UARTA0; self->peripheral = PRCM_UARTA0;
MAP_UARTIntRegister(UARTA0_BASE, UART0IntHandler); }
MAP_IntPrioritySet(INT_UARTA0, INT_PRIORITY_LVL_3); else {
break;
case PYB_UART_1:
self->reg = UARTA1_BASE; self->reg = UARTA1_BASE;
uartPerh = PRCM_UARTA1; self->peripheral = PRCM_UARTA1;
MAP_UARTIntRegister(UARTA1_BASE, UART1IntHandler);
MAP_IntPrioritySet(INT_UARTA1, INT_PRIORITY_LVL_3);
break;
default:
return;
} }
// Enable the peripheral clock // Enable the peripheral clock
MAP_PRCMPeripheralClkEnable(uartPerh, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK); MAP_PRCMPeripheralClkEnable(self->peripheral, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
// Reset the uart // Reset the uart
MAP_PRCMPeripheralReset(uartPerh); MAP_PRCMPeripheralReset(self->peripheral);
// Initialize the UART // Initialize the UART
MAP_UARTConfigSetExpClk(self->reg, MAP_PRCMPeripheralClockGet(uartPerh), MAP_UARTConfigSetExpClk(self->reg, MAP_PRCMPeripheralClockGet(self->peripheral),
self->baudrate, self->config); self->baudrate, self->config);
// Enbale the FIFO // Enable the FIFO
MAP_UARTFIFOEnable(self->reg); MAP_UARTFIFOEnable(self->reg);
// Configure the FIFO interrupt levels // Configure the FIFO interrupt levels
@ -217,14 +240,6 @@ STATIC void uart_init (pyb_uart_obj_t *self) {
// Configure the flow control mode // Configure the flow control mode
UARTFlowControlSet(self->reg, self->flowcontrol); UARTFlowControlSet(self->reg, self->flowcontrol);
// Setup the RX interrupts
if (self->read_buf != NULL) {
MAP_UARTIntEnable(self->reg, UART_INT_RX | UART_INT_RT);
}
else {
MAP_UARTIntDisable(self->reg, UART_INT_RX | UART_INT_RT);
}
} }
// Waits at most timeout milliseconds for at least 1 char to become ready for // Waits at most timeout milliseconds for at least 1 char to become ready for
@ -256,8 +271,8 @@ STATIC void UARTGenericIntHandler(uint32_t uart_id) {
MAP_UARTIntClear(self->reg, UART_INT_RX | UART_INT_RT); MAP_UARTIntClear(self->reg, UART_INT_RX | UART_INT_RT);
while (UARTCharsAvail(self->reg)) { while (UARTCharsAvail(self->reg)) {
int data = MAP_UARTCharGetNonBlocking(self->reg); int data = MAP_UARTCharGetNonBlocking(self->reg);
if (MICROPY_STDIO_UART == self->uart_id && data == user_interrupt_char) { if (pyb_stdio_uart == self && data == user_interrupt_char) {
// raise exception when interrupts are finished // raise an exception when interrupts are finished
mpexception_keyboard_nlr_jump(); mpexception_keyboard_nlr_jump();
} }
else if (self->read_buf_len != 0) { else if (self->read_buf_len != 0) {
@ -269,6 +284,9 @@ STATIC void UARTGenericIntHandler(uint32_t uart_id) {
} }
} }
} }
// call the user defined handler
mp_obj_t _callback = mpcallback_find(self);
mpcallback_handler(_callback);
} }
} }
@ -280,14 +298,23 @@ STATIC void UART1IntHandler(void) {
UARTGenericIntHandler(1); UARTGenericIntHandler(1);
} }
STATIC void uart_callback_enable (mp_obj_t self_in) {
pyb_uart_obj_t *self = self_in;
MAP_UARTIntClear(self->reg, UART_INT_RX | UART_INT_RT);
MAP_UARTIntEnable(self->reg, UART_INT_RX | UART_INT_RT);
}
STATIC void uart_callback_disable (mp_obj_t self_in) {
pyb_uart_obj_t *self = self_in;
MAP_UARTIntDisable(self->reg, UART_INT_RX | UART_INT_RT);
}
/******************************************************************************/ /******************************************************************************/
/* Micro Python bindings */ /* Micro Python bindings */
STATIC void pyb_uart_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { STATIC void pyb_uart_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
pyb_uart_obj_t *self = self_in; pyb_uart_obj_t *self = self_in;
if (!self->enabled) { if (self->baudrate > 0) {
print(env, "<UART%u>", self->uart_id);
} else {
print(env, "<UART%u, baudrate=%u, bits=", self->uart_id, self->baudrate); print(env, "<UART%u, baudrate=%u, bits=", self->uart_id, self->baudrate);
switch (self->config & UART_CONFIG_WLEN_MASK) { switch (self->config & UART_CONFIG_WLEN_MASK) {
case UART_CONFIG_WLEN_5: case UART_CONFIG_WLEN_5:
@ -314,6 +341,9 @@ STATIC void pyb_uart_print(void (*print)(void *env, const char *fmt, ...), void
(self->config & UART_CONFIG_STOP_MASK) == UART_CONFIG_STOP_ONE ? 1 : 2, (self->config & UART_CONFIG_STOP_MASK) == UART_CONFIG_STOP_ONE ? 1 : 2,
self->timeout, self->timeout_char, self->read_buf_len); self->timeout, self->timeout_char, self->read_buf_len);
} }
else {
print(env, "<UART%u>", self->uart_id);
}
} }
/// \method init(baudrate, bits=8, parity=None, stop=1, *, timeout=1000, timeout_char=0, read_buf_len=128) /// \method init(baudrate, bits=8, parity=None, stop=1, *, timeout=1000, timeout_char=0, read_buf_len=128)
@ -326,9 +356,8 @@ STATIC void pyb_uart_print(void (*print)(void *env, const char *fmt, ...), void
/// - `stop` is the number of stop bits, 1 or 2. /// - `stop` is the number of stop bits, 1 or 2.
/// - `flowcontrol` is the flow control mode, `None`, `UART.FLOW_TX`, /// - `flowcontrol` is the flow control mode, `None`, `UART.FLOW_TX`,
/// `UART.FLOW_RX', 'UART.FLOW_TXRX`. /// `UART.FLOW_RX', 'UART.FLOW_TXRX`.
/// - `timeout` is the timeout in milliseconds to wait for the first character. /// - `timeout` is the timeout (in milliseconds) when waiting for the first character.
/// - `timeout_char` is the timeout in milliseconds to wait between characters. /// - `timeout_char` is the timeout (in milliseconds) between characters.
/// - `read_buf_len` is the character length of the read buffer (0 to disable).
STATIC const mp_arg_t pyb_uart_init_args[] = { STATIC const mp_arg_t pyb_uart_init_args[] = {
{ MP_QSTR_baudrate, MP_ARG_REQUIRED | MP_ARG_INT, }, { MP_QSTR_baudrate, MP_ARG_REQUIRED | MP_ARG_INT, },
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
@ -337,7 +366,6 @@ STATIC const mp_arg_t pyb_uart_init_args[] = {
{ MP_QSTR_flow, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_int = UART_FLOWCONTROL_NONE} }, { MP_QSTR_flow, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_int = UART_FLOWCONTROL_NONE} },
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} }, { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} },
{ MP_QSTR_timeout_char, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, { MP_QSTR_timeout_char, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
{ MP_QSTR_read_buf_len, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 128} },
}; };
STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
@ -349,21 +377,11 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
self->timeout = args[5].u_int; self->timeout = args[5].u_int;
self->timeout_char = args[6].u_int; self->timeout_char = args[6].u_int;
// setup the read buffer // no read buffer for the moment
m_del(byte, self->read_buf, self->read_buf_len);
self->read_buf_head = 0; self->read_buf_head = 0;
self->read_buf_tail = 0; self->read_buf_tail = 0;
if (args[7].u_int <= 0) {
// no read buffer
self->read_buf_len = 0; self->read_buf_len = 0;
self->read_buf = NULL; self->read_buf = NULL;
}
else {
// read buffer using interrupts
self->read_buf_len = args[7].u_int;
self->read_buf = m_new(byte, args[7].u_int);
}
// get the baudrate // get the baudrate
self->baudrate = args[0].u_int; self->baudrate = args[0].u_int;
@ -395,7 +413,6 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
} }
// Stop bits // Stop bits
self->config |= (args[3].u_int == 1 ? UART_CONFIG_STOP_ONE : UART_CONFIG_STOP_TWO); self->config |= (args[3].u_int == 1 ? UART_CONFIG_STOP_ONE : UART_CONFIG_STOP_TWO);
// Flow control // Flow control
self->flowcontrol = args[4].u_int; self->flowcontrol = args[4].u_int;
} }
@ -403,9 +420,9 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
self->config = UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE; self->config = UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE;
self->flowcontrol = UART_FLOWCONTROL_NONE; self->flowcontrol = UART_FLOWCONTROL_NONE;
} }
// initialize and enable the uart // initialize and enable the uart
uart_init (self); uart_init (self);
self->enabled = true;
// register it with the sleep module // register it with the sleep module
pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)uart_init); pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)uart_init);
@ -414,7 +431,7 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
/// \classmethod \constructor(bus, ...) /// \classmethod \constructor(bus, ...)
/// ///
/// Construct a UART object on the given bus id. `bus id` can be 0-1 /// Construct a UART object on the given bus id. `bus id` can be 0 or 1
/// With no additional parameters, the UART object is created but not /// With no additional parameters, the UART object is created but not
/// initialised (it has the settings from the last initialisation of /// initialised (it has the settings from the last initialisation of
/// the bus, if any). /// the bus, if any).
@ -441,9 +458,6 @@ STATIC mp_obj_t pyb_uart_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
self->base.type = &pyb_uart_type; self->base.type = &pyb_uart_type;
self->uart_id = uart_id; self->uart_id = uart_id;
if (n_args > 1 || n_kw > 0) { if (n_args > 1 || n_kw > 0) {
// invalidate the buffer and clear the enabled flag
self->read_buf = NULL;
self->enabled = false;
// start the peripheral // start the peripheral
mp_map_t kw_args; mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
@ -462,31 +476,14 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_init_obj, 1, pyb_uart_init);
/// Turn off the UART bus. /// Turn off the UART bus.
mp_obj_t pyb_uart_deinit(mp_obj_t self_in) { mp_obj_t pyb_uart_deinit(mp_obj_t self_in) {
pyb_uart_obj_t *self = self_in; pyb_uart_obj_t *self = self_in;
uint uartPerh;
switch (self->uart_id) {
case PYB_UART_0:
uartPerh = PRCM_UARTA0;
break;
case PYB_UART_1:
uartPerh = PRCM_UARTA1;
break;
default:
return mp_const_none;
}
// unregister it with the sleep module // unregister it with the sleep module
pybsleep_remove (self); pybsleep_remove (self);
self->enabled = false; // invalidate the baudrate
self->baudrate = 0;
MAP_UARTIntDisable(self->reg, UART_INT_RX | UART_INT_RT); MAP_UARTIntDisable(self->reg, UART_INT_RX | UART_INT_RT);
MAP_UARTIntClear(self->reg, UART_INT_RX | UART_INT_RT);
MAP_UARTIntUnregister(self->reg);
MAP_UARTDisable(self->reg); MAP_UARTDisable(self->reg);
MAP_PRCMPeripheralClkDisable(uartPerh, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK); MAP_PRCMPeripheralClkDisable(self->peripheral, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
return mp_const_none; return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_deinit_obj, pyb_uart_deinit); STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_deinit_obj, pyb_uart_deinit);
@ -503,6 +500,33 @@ STATIC mp_obj_t pyb_uart_any(mp_obj_t self_in) {
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_any_obj, pyb_uart_any); STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_any_obj, pyb_uart_any);
/// \method callback(handler, value, priority)
/// Creates a callback object associated with the uart
/// min num of arguments is 1 (value). The value is the size of the rx buffer
STATIC mp_obj_t pyb_uart_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
mp_arg_val_t args[mpcallback_INIT_NUM_ARGS];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, mpcallback_INIT_NUM_ARGS, mpcallback_init_args, args);
// check if any parameters were passed
pyb_uart_obj_t *self = pos_args[0];
mp_obj_t _callback = mpcallback_find((mp_obj_t)self);
if (kw_args->used > 0 || !_callback) {
// convert the priority to the correct value
uint priority = mpcallback_translate_priority (args[2].u_int);
// check the power mode
if (PYB_PWR_MODE_ACTIVE != args[3].u_int) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
}
// register a new callback
return uart_callback_new (self, args[1].u_obj, args[3].u_int, priority);
}
return _callback;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_callback_obj, 1, pyb_uart_callback);
/// \method writechar(char) /// \method writechar(char)
/// Write a single character on the bus. `char` is an integer to write. /// Write a single character on the bus. `char` is an integer to write.
/// Return value: `None`. /// Return value: `None`.
@ -540,6 +564,7 @@ STATIC const mp_map_elem_t pyb_uart_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_uart_init_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_uart_init_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&pyb_uart_deinit_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&pyb_uart_deinit_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_any), (mp_obj_t)&pyb_uart_any_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_any), (mp_obj_t)&pyb_uart_any_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&pyb_uart_callback_obj },
/// \method read([nbytes]) /// \method read([nbytes])
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj },
@ -628,6 +653,12 @@ STATIC const mp_stream_p_t uart_stream_p = {
.is_text = false, .is_text = false,
}; };
STATIC const mp_cb_methods_t uart_cb_methods = {
.init = pyb_uart_callback,
.enable = uart_callback_enable,
.disable = uart_callback_disable,
};
const mp_obj_type_t pyb_uart_type = { const mp_obj_type_t pyb_uart_type = {
{ &mp_type_type }, { &mp_type_type },
.name = MP_QSTR_UART, .name = MP_QSTR_UART,

View File

@ -44,5 +44,6 @@ int uart_rx_char(pyb_uart_obj_t *uart_obj);
bool uart_tx_char(pyb_uart_obj_t *self, int c); bool uart_tx_char(pyb_uart_obj_t *self, int c);
bool uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len); bool uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len);
void uart_tx_strn_cooked(pyb_uart_obj_t *uart_obj, const char *str, uint len); void uart_tx_strn_cooked(pyb_uart_obj_t *uart_obj, const char *str, uint len);
mp_obj_t uart_callback_new (pyb_uart_obj_t *self, mp_obj_t handler, uint rxbuffer_size, mp_int_t priority);
#endif // PYBUART_H_ #endif // PYBUART_H_

View File

@ -40,6 +40,7 @@
#include "rom_map.h" #include "rom_map.h"
#include "pin.h" #include "pin.h"
#include "prcm.h" #include "prcm.h"
#include "interrupt.h"
#include "pybuart.h" #include "pybuart.h"
#include "pybpin.h" #include "pybpin.h"
#include "pybrtc.h" #include "pybrtc.h"
@ -134,16 +135,18 @@ soft_reset:
// we are alive, so let the world know it // we are alive, so let the world know it
mperror_enable_heartbeat(); mperror_enable_heartbeat();
// configure stdio uart pins with the correct af // configure the stdio uart pins with the correct alternate functions
// param 3 ("mode") is DON'T CARE" for AFs others than GPIO // param 3 ("mode") is DON'T CARE" for AFs others than GPIO
pin_config ((pin_obj_t *)&pin_GPIO1, PIN_MODE_3, 0, PIN_TYPE_STD, PIN_STRENGTH_2MA); pin_config ((pin_obj_t *)&pin_GPIO1, PIN_MODE_3, 0, PIN_TYPE_STD, PIN_STRENGTH_2MA);
pin_config ((pin_obj_t *)&pin_GPIO2, PIN_MODE_3, 0, PIN_TYPE_STD, PIN_STRENGTH_2MA); pin_config ((pin_obj_t *)&pin_GPIO2, PIN_MODE_3, 0, PIN_TYPE_STD, PIN_STRENGTH_2MA);
// Instantiate the stdio uart // instantiate the stdio uart
mp_obj_t args[2] = { mp_obj_t args[2] = {
mp_obj_new_int(MICROPY_STDIO_UART), mp_obj_new_int(MICROPY_STDIO_UART),
mp_obj_new_int(MICROPY_STDIO_UART_BAUD), mp_obj_new_int(MICROPY_STDIO_UART_BAUD),
}; };
pyb_stdio_uart = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args); pyb_stdio_uart = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args);
// create a callback for the uart, in order to enable the rx interrupts
uart_callback_new (pyb_stdio_uart, mp_const_none, MICROPY_STDIO_UART_RX_BUF_SIZE, INT_PRIORITY_LVL_3);
pybsleep_reset_cause_t rstcause = pybsleep_get_reset_cause(); pybsleep_reset_cause_t rstcause = pybsleep_get_reset_cause();
if (rstcause < PYB_SLP_SOFT_RESET) { if (rstcause < PYB_SLP_SOFT_RESET) {

View File

@ -151,7 +151,6 @@ Q(disable)
// for RTC class // for RTC class
Q(RTC) Q(RTC)
Q(datetime) Q(datetime)
Q(callback)
// for time class // for time class
Q(utime) Q(utime)
@ -245,7 +244,7 @@ Q(handler)
Q(intmode) Q(intmode)
Q(value) Q(value)
Q(priority) Q(priority)
Q(wake) Q(wakes)
// for Sleep class // for Sleep class
Q(Sleep) Q(Sleep)