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
6c8b19c7
Commit
6c8b19c7
authored
Mar 09, 2023
by
Damien George
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
stm32/spi: Return error code and raise exception if SPI init fails.
Signed-off-by:
Damien George
<
damien@micropython.org
>
parent
98937dcf
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
20 additions
and
15 deletions
+20
-15
ports/stm32/machine_spi.c
ports/stm32/machine_spi.c
+8
-2
ports/stm32/pyb_spi.c
ports/stm32/pyb_spi.c
+4
-1
ports/stm32/spi.c
ports/stm32/spi.c
+7
-11
ports/stm32/spi.h
ports/stm32/spi.h
+1
-1
No files found.
ports/stm32/machine_spi.c
View file @
6c8b19c7
...
...
@@ -91,7 +91,10 @@ mp_obj_t machine_hard_spi_make_new(const mp_obj_type_t *type, size_t n_args, siz
args
[
ARG_firstbit
].
u_int
);
// init the SPI bus
spi_init
(
self
->
spi
,
false
);
int
ret
=
spi_init
(
self
->
spi
,
false
);
if
(
ret
!=
0
)
{
mp_raise_OSError
(
-
ret
);
}
return
MP_OBJ_FROM_PTR
(
self
);
}
...
...
@@ -116,7 +119,10 @@ STATIC void machine_hard_spi_init(mp_obj_base_t *self_in, size_t n_args, const m
args
[
ARG_firstbit
].
u_int
);
// re-init the SPI bus
spi_init
(
self
->
spi
,
false
);
int
ret
=
spi_init
(
self
->
spi
,
false
);
if
(
ret
!=
0
)
{
mp_raise_OSError
(
-
ret
);
}
}
STATIC
void
machine_hard_spi_deinit
(
mp_obj_base_t
*
self_in
)
{
...
...
ports/stm32/pyb_spi.c
View file @
6c8b19c7
...
...
@@ -112,7 +112,10 @@ STATIC mp_obj_t pyb_spi_init_helper(const pyb_spi_obj_t *self, size_t n_args, co
}
// init the SPI bus
spi_init
(
self
->
spi
,
init
->
NSS
!=
SPI_NSS_SOFT
);
int
ret
=
spi_init
(
self
->
spi
,
init
->
NSS
!=
SPI_NSS_SOFT
);
if
(
ret
!=
0
)
{
mp_raise_OSError
(
-
ret
);
}
return
mp_const_none
;
}
...
...
ports/stm32/spi.c
View file @
6c8b19c7
...
...
@@ -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 "spi.h"
#include "extmod/machine_spi.h"
...
...
@@ -296,7 +294,7 @@ void spi_set_params(const spi_t *spi_obj, uint32_t prescale, int32_t baudrate,
}
// TODO allow to take a list of pins to use
void
spi_init
(
const
spi_t
*
self
,
bool
enable_nss_pin
)
{
int
spi_init
(
const
spi_t
*
self
,
bool
enable_nss_pin
)
{
SPI_HandleTypeDef
*
spi
=
self
->
spi
;
uint32_t
irqn
=
0
;
const
pin_obj_t
*
pins
[
4
]
=
{
NULL
,
NULL
,
NULL
,
NULL
};
...
...
@@ -396,7 +394,7 @@ void spi_init(const spi_t *self, bool enable_nss_pin) {
#endif
}
else
{
// SPI does not exist for this board (shouldn't get here, should be checked by caller)
return
;
return
-
MP_EINVAL
;
}
// init the GPIO lines
...
...
@@ -412,10 +410,7 @@ void spi_init(const spi_t *self, bool enable_nss_pin) {
// init the SPI device
if
(
HAL_SPI_Init
(
spi
)
!=
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_SPI_Init failed
\n
"
);
return
;
return
-
MP_EIO
;
}
// After calling HAL_SPI_Init() it seems that the DMA gets disconnected if
...
...
@@ -430,6 +425,8 @@ void spi_init(const spi_t *self, bool enable_nss_pin) {
#else
(
void
)
irqn
;
#endif
return
0
;
// success
}
void
spi_deinit
(
const
spi_t
*
spi_obj
)
{
...
...
@@ -715,8 +712,7 @@ STATIC int spi_proto_ioctl(void *self_in, uint32_t cmd) {
self
->
spi
->
spi
->
Init
.
CRCCalculation
=
SPI_CRCCALCULATION_DISABLE
;
spi_set_params
(
self
->
spi
,
0xffffffff
,
self
->
baudrate
,
self
->
polarity
,
self
->
phase
,
self
->
bits
,
self
->
firstbit
);
spi_init
(
self
->
spi
,
false
);
break
;
return
spi_init
(
self
->
spi
,
false
);
case
MP_SPI_IOCTL_DEINIT
:
spi_deinit
(
self
->
spi
);
...
...
ports/stm32/spi.h
View file @
6c8b19c7
...
...
@@ -66,7 +66,7 @@ extern const mp_obj_type_t machine_spi_type;
#define SPI_TRANSFER_TIMEOUT(len) ((len) + 100)
void
spi_init0
(
void
);
void
spi_init
(
const
spi_t
*
spi
,
bool
enable_nss_pin
);
int
spi_init
(
const
spi_t
*
spi
,
bool
enable_nss_pin
);
void
spi_deinit
(
const
spi_t
*
spi_obj
);
int
spi_find_index
(
mp_obj_t
id
);
void
spi_set_params
(
const
spi_t
*
spi_obj
,
uint32_t
prescale
,
int32_t
baudrate
,
...
...
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