Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This project is archived. Its data is
read-only
.
Show more breadcrumbs
card10
micropython
Commits
372e7a4d
Commit
372e7a4d
authored
Dec 9, 2018
by
Tobias Badertscher
Committed by
Damien George
Dec 29, 2018
Browse files
Options
Downloads
Patches
Plain Diff
stm32: Implement UART.irq() method with initial support for RX idle IRQ.
parent
06236bf2
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
ports/stm32/Makefile
+1
-0
1 addition, 0 deletions
ports/stm32/Makefile
ports/stm32/machine_uart.c
+118
-1
118 additions, 1 deletion
ports/stm32/machine_uart.c
ports/stm32/uart.c
+24
-0
24 additions, 0 deletions
ports/stm32/uart.c
ports/stm32/uart.h
+5
-0
5 additions, 0 deletions
ports/stm32/uart.h
with
148 additions
and
1 deletion
ports/stm32/Makefile
+
1
−
0
View file @
372e7a4d
...
@@ -114,6 +114,7 @@ SRC_LIB = $(addprefix lib/,\
...
@@ -114,6 +114,7 @@ SRC_LIB = $(addprefix lib/,\
utils/pyexec.c
\
utils/pyexec.c
\
utils/interrupt_char.c
\
utils/interrupt_char.c
\
utils/sys_stdio_mphal.c
\
utils/sys_stdio_mphal.c
\
utils/mpirq.c
\
)
)
ifeq
($(MICROPY_FLOAT_IMPL),double)
ifeq
($(MICROPY_FLOAT_IMPL),double)
...
...
This diff is collapsed.
Click to expand it.
ports/stm32/machine_uart.c
+
118
−
1
View file @
372e7a4d
...
@@ -33,6 +33,7 @@
...
@@ -33,6 +33,7 @@
#include
"py/mperrno.h"
#include
"py/mperrno.h"
#include
"py/mphal.h"
#include
"py/mphal.h"
#include
"lib/utils/interrupt_char.h"
#include
"lib/utils/interrupt_char.h"
#include
"lib/utils/mpirq.h"
#include
"uart.h"
#include
"uart.h"
#include
"irq.h"
#include
"irq.h"
#include
"pendsv.h"
#include
"pendsv.h"
...
@@ -72,6 +73,77 @@
...
@@ -72,6 +73,77 @@
///
///
/// uart.any() # returns True if any characters waiting
/// uart.any() # returns True if any characters waiting
typedef
struct
_pyb_uart_irq_map_t
{
uint16_t
irq_en
;
uint16_t
flag
;
}
pyb_uart_irq_map_t
;
STATIC
const
pyb_uart_irq_map_t
mp_irq_map
[]
=
{
{
USART_CR1_IDLEIE
,
UART_FLAG_IDLE
},
// RX idle
{
USART_CR1_PEIE
,
UART_FLAG_PE
},
// parity error
{
USART_CR1_TXEIE
,
UART_FLAG_TXE
},
// TX register empty
{
USART_CR1_TCIE
,
UART_FLAG_TC
},
// TX complete
{
USART_CR1_RXNEIE
,
UART_FLAG_RXNE
},
// RX register not empty
#if 0
// For now only IRQs selected by CR1 are supported
#if defined(STM32F4)
{ USART_CR2_LBDIE, UART_FLAG_LBD}, // LIN break detection
#else
{ USART_CR2_LBDIE, UART_FLAG_LBDF}, // LIN break detection
#endif
{
USART_CR3_CTSIE
,
UART_FLAG_CTS
},
// CTS
#endif
};
// OR-ed IRQ flags which should not be touched by the user
STATIC
const
uint32_t
mp_irq_reserved
=
UART_FLAG_RXNE
;
// OR-ed IRQ flags which are allowed to be used by the user
STATIC
const
uint32_t
mp_irq_allowed
=
UART_FLAG_IDLE
;
STATIC
mp_obj_t
pyb_uart_irq
(
size_t
n_args
,
const
mp_obj_t
*
pos_args
,
mp_map_t
*
kw_args
);
STATIC
void
pyb_uart_irq_config
(
pyb_uart_obj_t
*
self
,
bool
enable
)
{
if
(
self
->
mp_irq_trigger
)
{
for
(
size_t
entry
=
0
;
entry
<
MP_ARRAY_SIZE
(
mp_irq_map
);
++
entry
)
{
if
(
mp_irq_map
[
entry
].
flag
&
mp_irq_reserved
)
{
continue
;
}
if
(
mp_irq_map
[
entry
].
flag
&
self
->
mp_irq_trigger
)
{
if
(
enable
)
{
self
->
uartx
->
CR1
|=
mp_irq_map
[
entry
].
irq_en
;
}
else
{
self
->
uartx
->
CR1
&=
~
mp_irq_map
[
entry
].
irq_en
;
}
}
}
}
}
STATIC
mp_uint_t
pyb_uart_irq_trigger
(
mp_obj_t
self_in
,
mp_uint_t
new_trigger
)
{
pyb_uart_obj_t
*
self
=
MP_OBJ_TO_PTR
(
self_in
);
pyb_uart_irq_config
(
self
,
false
);
self
->
mp_irq_trigger
=
new_trigger
;
pyb_uart_irq_config
(
self
,
true
);
return
0
;
}
STATIC
mp_uint_t
pyb_uart_irq_info
(
mp_obj_t
self_in
,
mp_uint_t
info_type
)
{
pyb_uart_obj_t
*
self
=
MP_OBJ_TO_PTR
(
self_in
);
if
(
info_type
==
MP_IRQ_INFO_FLAGS
)
{
return
self
->
mp_irq_flags
;
}
else
if
(
info_type
==
MP_IRQ_INFO_TRIGGERS
)
{
return
self
->
mp_irq_trigger
;
}
return
0
;
}
STATIC
const
mp_irq_methods_t
pyb_uart_irq_methods
=
{
.
init
=
pyb_uart_irq
,
.
trigger
=
pyb_uart_irq_trigger
,
.
info
=
pyb_uart_irq_info
,
};
STATIC
void
pyb_uart_print
(
const
mp_print_t
*
print
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
)
{
STATIC
void
pyb_uart_print
(
const
mp_print_t
*
print
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
)
{
pyb_uart_obj_t
*
self
=
MP_OBJ_TO_PTR
(
self_in
);
pyb_uart_obj_t
*
self
=
MP_OBJ_TO_PTR
(
self_in
);
if
(
!
self
->
is_enabled
)
{
if
(
!
self
->
is_enabled
)
{
...
@@ -123,9 +195,13 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k
...
@@ -123,9 +195,13 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k
mp_print_str
(
print
,
"CTS"
);
mp_print_str
(
print
,
"CTS"
);
}
}
}
}
mp_printf
(
print
,
", timeout=%u, timeout_char=%u, rxbuf=%u
)
"
,
mp_printf
(
print
,
", timeout=%u, timeout_char=%u, rxbuf=%u"
,
self
->
timeout
,
self
->
timeout_char
,
self
->
timeout
,
self
->
timeout_char
,
self
->
read_buf_len
==
0
?
0
:
self
->
read_buf_len
-
1
);
// -1 to adjust for usable length of buffer
self
->
read_buf_len
==
0
?
0
:
self
->
read_buf_len
-
1
);
// -1 to adjust for usable length of buffer
if
(
self
->
mp_irq_trigger
!=
0
)
{
mp_printf
(
print
,
"; irq=0x%x"
,
self
->
mp_irq_trigger
);
}
mp_print_str
(
print
,
")"
);
}
}
}
}
...
@@ -414,6 +490,43 @@ STATIC mp_obj_t pyb_uart_sendbreak(mp_obj_t self_in) {
...
@@ -414,6 +490,43 @@ STATIC mp_obj_t pyb_uart_sendbreak(mp_obj_t self_in) {
}
}
STATIC
MP_DEFINE_CONST_FUN_OBJ_1
(
pyb_uart_sendbreak_obj
,
pyb_uart_sendbreak
);
STATIC
MP_DEFINE_CONST_FUN_OBJ_1
(
pyb_uart_sendbreak_obj
,
pyb_uart_sendbreak
);
// irq(handler, trigger, hard)
STATIC
mp_obj_t
pyb_uart_irq
(
size_t
n_args
,
const
mp_obj_t
*
pos_args
,
mp_map_t
*
kw_args
)
{
mp_arg_val_t
args
[
MP_IRQ_ARG_INIT_NUM_ARGS
];
mp_arg_parse_all
(
n_args
-
1
,
pos_args
+
1
,
kw_args
,
MP_IRQ_ARG_INIT_NUM_ARGS
,
mp_irq_init_args
,
args
);
pyb_uart_obj_t
*
self
=
MP_OBJ_TO_PTR
(
pos_args
[
0
]);
if
(
self
->
mp_irq_obj
==
NULL
)
{
self
->
mp_irq_trigger
=
0
;
self
->
mp_irq_obj
=
mp_irq_new
(
&
pyb_uart_irq_methods
,
MP_OBJ_FROM_PTR
(
self
));
}
if
(
n_args
>
1
||
kw_args
->
used
!=
0
)
{
// Check the handler
mp_obj_t
handler
=
args
[
MP_IRQ_ARG_INIT_handler
].
u_obj
;
if
(
handler
!=
mp_const_none
&&
!
mp_obj_is_callable
(
handler
))
{
mp_raise_ValueError
(
"handler must be None or callable"
);
}
// Check the trigger
mp_uint_t
trigger
=
args
[
MP_IRQ_ARG_INIT_trigger
].
u_int
;
mp_uint_t
not_supported
=
trigger
&
~
mp_irq_allowed
;
if
(
trigger
!=
0
&&
not_supported
)
{
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_ValueError
,
"trigger 0x%08x unsupported"
,
not_supported
));
}
// Reconfigure user IRQs
pyb_uart_irq_config
(
self
,
false
);
self
->
mp_irq_obj
->
handler
=
handler
;
self
->
mp_irq_obj
->
ishard
=
args
[
MP_IRQ_ARG_INIT_hard
].
u_bool
;
self
->
mp_irq_trigger
=
trigger
;
pyb_uart_irq_config
(
self
,
true
);
}
return
MP_OBJ_FROM_PTR
(
self
->
mp_irq_obj
);
}
STATIC
MP_DEFINE_CONST_FUN_OBJ_KW
(
pyb_uart_irq_obj
,
1
,
pyb_uart_irq
);
STATIC
const
mp_rom_map_elem_t
pyb_uart_locals_dict_table
[]
=
{
STATIC
const
mp_rom_map_elem_t
pyb_uart_locals_dict_table
[]
=
{
// instance methods
// instance methods
...
@@ -429,6 +542,7 @@ STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = {
...
@@ -429,6 +542,7 @@ STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = {
{
MP_ROM_QSTR
(
MP_QSTR_readinto
),
MP_ROM_PTR
(
&
mp_stream_readinto_obj
)
},
{
MP_ROM_QSTR
(
MP_QSTR_readinto
),
MP_ROM_PTR
(
&
mp_stream_readinto_obj
)
},
/// \method write(buf)
/// \method write(buf)
{
MP_ROM_QSTR
(
MP_QSTR_write
),
MP_ROM_PTR
(
&
mp_stream_write_obj
)
},
{
MP_ROM_QSTR
(
MP_QSTR_write
),
MP_ROM_PTR
(
&
mp_stream_write_obj
)
},
{
MP_ROM_QSTR
(
MP_QSTR_irq
),
MP_ROM_PTR
(
&
pyb_uart_irq_obj
)
},
{
MP_ROM_QSTR
(
MP_QSTR_writechar
),
MP_ROM_PTR
(
&
pyb_uart_writechar_obj
)
},
{
MP_ROM_QSTR
(
MP_QSTR_writechar
),
MP_ROM_PTR
(
&
pyb_uart_writechar_obj
)
},
{
MP_ROM_QSTR
(
MP_QSTR_readchar
),
MP_ROM_PTR
(
&
pyb_uart_readchar_obj
)
},
{
MP_ROM_QSTR
(
MP_QSTR_readchar
),
MP_ROM_PTR
(
&
pyb_uart_readchar_obj
)
},
...
@@ -437,6 +551,9 @@ STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = {
...
@@ -437,6 +551,9 @@ STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = {
// class constants
// class constants
{
MP_ROM_QSTR
(
MP_QSTR_RTS
),
MP_ROM_INT
(
UART_HWCONTROL_RTS
)
},
{
MP_ROM_QSTR
(
MP_QSTR_RTS
),
MP_ROM_INT
(
UART_HWCONTROL_RTS
)
},
{
MP_ROM_QSTR
(
MP_QSTR_CTS
),
MP_ROM_INT
(
UART_HWCONTROL_CTS
)
},
{
MP_ROM_QSTR
(
MP_QSTR_CTS
),
MP_ROM_INT
(
UART_HWCONTROL_CTS
)
},
// IRQ flags
{
MP_ROM_QSTR
(
MP_QSTR_IRQ_RXIDLE
),
MP_ROM_INT
(
UART_FLAG_IDLE
)
},
};
};
STATIC
MP_DEFINE_CONST_DICT
(
pyb_uart_locals_dict
,
pyb_uart_locals_dict_table
);
STATIC
MP_DEFINE_CONST_DICT
(
pyb_uart_locals_dict
,
pyb_uart_locals_dict_table
);
...
...
This diff is collapsed.
Click to expand it.
ports/stm32/uart.c
+
24
−
0
View file @
372e7a4d
...
@@ -33,6 +33,7 @@
...
@@ -33,6 +33,7 @@
#include
"py/mperrno.h"
#include
"py/mperrno.h"
#include
"py/mphal.h"
#include
"py/mphal.h"
#include
"lib/utils/interrupt_char.h"
#include
"lib/utils/interrupt_char.h"
#include
"lib/utils/mpirq.h"
#include
"uart.h"
#include
"uart.h"
#include
"irq.h"
#include
"irq.h"
#include
"pendsv.h"
#include
"pendsv.h"
...
@@ -327,6 +328,9 @@ bool uart_init(pyb_uart_obj_t *uart_obj,
...
@@ -327,6 +328,9 @@ bool uart_init(pyb_uart_obj_t *uart_obj,
uart_obj
->
char_width
=
CHAR_WIDTH_8BIT
;
uart_obj
->
char_width
=
CHAR_WIDTH_8BIT
;
}
}
uart_obj
->
mp_irq_trigger
=
0
;
uart_obj
->
mp_irq_obj
=
NULL
;
return
true
;
return
true
;
}
}
...
@@ -697,4 +701,24 @@ void uart_irq_handler(mp_uint_t uart_id) {
...
@@ -697,4 +701,24 @@ void uart_irq_handler(mp_uint_t uart_id) {
}
}
}
}
}
}
// Set user IRQ flags
self
->
mp_irq_flags
=
0
;
#if defined(STM32F4)
if
(
self
->
uartx
->
SR
&
USART_SR_IDLE
)
{
(
void
)
self
->
uartx
->
SR
;
(
void
)
self
->
uartx
->
DR
;
self
->
mp_irq_flags
|=
UART_FLAG_IDLE
;
}
#else
if
(
self
->
uartx
->
ISR
&
USART_ISR_IDLE
)
{
self
->
uartx
->
ICR
=
USART_ICR_IDLECF
;
self
->
mp_irq_flags
|=
UART_FLAG_IDLE
;
}
#endif
// Check the flags to see if the user handler should be called
if
(
self
->
mp_irq_trigger
&
self
->
mp_irq_flags
)
{
mp_irq_handler
(
self
->
mp_irq_obj
);
}
}
}
This diff is collapsed.
Click to expand it.
ports/stm32/uart.h
+
5
−
0
View file @
372e7a4d
...
@@ -26,6 +26,8 @@
...
@@ -26,6 +26,8 @@
#ifndef MICROPY_INCLUDED_STM32_UART_H
#ifndef MICROPY_INCLUDED_STM32_UART_H
#define MICROPY_INCLUDED_STM32_UART_H
#define MICROPY_INCLUDED_STM32_UART_H
struct
_mp_irq_obj_t
;
typedef
enum
{
typedef
enum
{
PYB_UART_NONE
=
0
,
PYB_UART_NONE
=
0
,
PYB_UART_1
=
1
,
PYB_UART_1
=
1
,
...
@@ -57,6 +59,9 @@ typedef struct _pyb_uart_obj_t {
...
@@ -57,6 +59,9 @@ typedef struct _pyb_uart_obj_t {
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)
byte
*
read_buf
;
// byte or uint16_t, depending on char size
byte
*
read_buf
;
// byte or uint16_t, depending on char size
uint16_t
mp_irq_trigger
;
// user IRQ trigger mask
uint16_t
mp_irq_flags
;
// user IRQ active IRQ flags
struct
_mp_irq_obj_t
*
mp_irq_obj
;
// user IRQ object
}
pyb_uart_obj_t
;
}
pyb_uart_obj_t
;
extern
const
mp_obj_type_t
pyb_uart_type
;
extern
const
mp_obj_type_t
pyb_uart_type
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment