Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
micropython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
micropython
Commits
98937dcf
Commit
98937dcf
authored
Mar 09, 2023
by
Damien George
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
stm32/i2c: Return error code and raise exception if I2C init fails.
Signed-off-by:
Damien George
<
damien@micropython.org
>
parent
b981e37c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
14 deletions
+14
-14
ports/stm32/i2c.h
ports/stm32/i2c.h
+2
-2
ports/stm32/pyb_i2c.c
ports/stm32/pyb_i2c.c
+12
-12
No files found.
ports/stm32/i2c.h
View file @
98937dcf
...
...
@@ -47,8 +47,8 @@ extern const mp_obj_type_t pyb_i2c_type;
extern
const
pyb_i2c_obj_t
pyb_i2c_obj
[
4
];
void
i2c_init0
(
void
);
void
pyb_i2c_init
(
I2C_HandleTypeDef
*
i2c
);
void
pyb_i2c_init_freq
(
const
pyb_i2c_obj_t
*
self
,
mp_int_t
freq
);
int
pyb_i2c_init
(
I2C_HandleTypeDef
*
i2c
);
int
pyb_i2c_init_freq
(
const
pyb_i2c_obj_t
*
self
,
mp_int_t
freq
);
uint32_t
pyb_i2c_get_baudrate
(
I2C_HandleTypeDef
*
i2c
);
void
i2c_ev_irq_handler
(
mp_uint_t
i2c_id
);
void
i2c_er_irq_handler
(
mp_uint_t
i2c_id
);
...
...
ports/stm32/pyb_i2c.c
View file @
98937dcf
...
...
@@ -24,10 +24,8 @@
* THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include "py/runtime.h"
#include "py/mperrno.h"
#include "py/mphal.h"
#include "irq.h"
#include "pin.h"
...
...
@@ -290,7 +288,7 @@ void i2c_init0(void) {
#endif
}
void
pyb_i2c_init
(
I2C_HandleTypeDef
*
i2c
)
{
int
pyb_i2c_init
(
I2C_HandleTypeDef
*
i2c
)
{
int
i2c_unit
;
const
pin_obj_t
*
scl_pin
;
const
pin_obj_t
*
sda_pin
;
...
...
@@ -326,7 +324,7 @@ void pyb_i2c_init(I2C_HandleTypeDef *i2c) {
#endif
}
else
{
// I2C does not exist for this board (shouldn't get here, should be checked by caller)
return
;
return
-
MP_EINVAL
;
}
// init the GPIO lines
...
...
@@ -338,10 +336,7 @@ void pyb_i2c_init(I2C_HandleTypeDef *i2c) {
// init the I2C device
if
(
HAL_I2C_Init
(
i2c
)
!=
HAL_OK
)
{
// init error
// TODO should raise an exception, but this function is not necessarily going to be
// called via Python, so may not be properly wrapped in an NLR handler
printf
(
"OSError: HAL_I2C_Init failed
\n
"
);
return
;
return
-
MP_EIO
;
}
// invalidate the DMA channels so they are initialised on first use
...
...
@@ -371,6 +366,8 @@ void pyb_i2c_init(I2C_HandleTypeDef *i2c) {
HAL_NVIC_EnableIRQ
(
I2C4_ER_IRQn
);
#endif
}
return
0
;
// success
}
void
i2c_deinit
(
I2C_HandleTypeDef
*
i2c
)
{
...
...
@@ -411,7 +408,7 @@ void i2c_deinit(I2C_HandleTypeDef *i2c) {
}
}
void
pyb_i2c_init_freq
(
const
pyb_i2c_obj_t
*
self
,
mp_int_t
freq
)
{
int
pyb_i2c_init_freq
(
const
pyb_i2c_obj_t
*
self
,
mp_int_t
freq
)
{
I2C_InitTypeDef
*
init
=
&
self
->
i2c
->
Init
;
init
->
AddressingMode
=
I2C_ADDRESSINGMODE_7BIT
;
...
...
@@ -428,7 +425,7 @@ void pyb_i2c_init_freq(const pyb_i2c_obj_t *self, mp_int_t freq) {
// init the I2C bus
i2c_deinit
(
self
->
i2c
);
pyb_i2c_init
(
self
->
i2c
);
return
pyb_i2c_init
(
self
->
i2c
);
}
STATIC
void
i2c_reset_after_error
(
I2C_HandleTypeDef
*
i2c
)
{
...
...
@@ -704,7 +701,10 @@ STATIC mp_obj_t pyb_i2c_init_helper(const pyb_i2c_obj_t *self, size_t n_args, co
// init the I2C bus
i2c_deinit
(
self
->
i2c
);
pyb_i2c_init
(
self
->
i2c
);
int
ret
=
pyb_i2c_init
(
self
->
i2c
);
if
(
ret
!=
0
)
{
mp_raise_OSError
(
-
ret
);
}
return
mp_const_none
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment