1313#include "genhdr/pins.h"
1414#include "spi.h"
1515
16- #if !defined(MICROPU_HW_ENABLE_SPI1 )
17- #define MICROPY_HW_ENABLE_SPI1 (1)
18- #endif
19-
16+ #if MICROPY_HW_ENABLE_SPI1
2017SPI_HandleTypeDef SPIHandle1 = {.Instance = NULL };
18+ #endif
2119SPI_HandleTypeDef SPIHandle2 = {.Instance = NULL };
20+ #if MICROPY_HW_ENABLE_SPI3
2221SPI_HandleTypeDef SPIHandle3 = {.Instance = NULL };
22+ #endif
2323
2424void spi_init0 (void ) {
2525 // reset the SPI handles
26+ #if MICROPY_HW_ENABLE_SPI1
2627 memset (& SPIHandle1 , 0 , sizeof (SPI_HandleTypeDef ));
2728 SPIHandle1 .Instance = SPI1 ;
29+ #endif
2830 memset (& SPIHandle2 , 0 , sizeof (SPI_HandleTypeDef ));
2931 SPIHandle2 .Instance = SPI2 ;
32+ #if MICROPY_HW_ENABLE_SPI3
3033 memset (& SPIHandle3 , 0 , sizeof (SPI_HandleTypeDef ));
3134 SPIHandle3 .Instance = SPI3 ;
35+ #endif
3236}
3337
3438// TODO allow to take a list of pins to use
@@ -40,53 +44,48 @@ void spi_init(SPI_HandleTypeDef *spi) {
4044 GPIO_InitStructure .Pull = GPIO_PULLUP ; // ST examples use PULLUP
4145
4246 const pin_obj_t * pins [4 ];
47+ if (0 ) {
4348#if MICROPY_HW_ENABLE_SPI1
44- if (spi -> Instance == SPI1 ) {
49+ } else if (spi -> Instance == SPI1 ) {
4550 // X-skin: X5=PA4=SPI1_NSS, X6=PA5=SPI1_SCK, X7=PA6=SPI1_MISO, X8=PA7=SPI1_MOSI
4651 pins [0 ] = & pin_A4 ;
4752 pins [1 ] = & pin_A5 ;
4853 pins [2 ] = & pin_A6 ;
4954 pins [3 ] = & pin_A7 ;
5055 GPIO_InitStructure .Alternate = GPIO_AF5_SPI1 ;
51- } else
56+ // enable the SPI clock
57+ __SPI1_CLK_ENABLE ();
5258#endif
53- if (spi -> Instance == SPI2 ) {
59+ } else if (spi -> Instance == SPI2 ) {
5460 // Y-skin: Y5=PB12=SPI2_NSS, Y6=PB13=SPI2_SCK, Y7=PB14=SPI2_MISO, Y8=PB15=SPI2_MOSI
5561 pins [0 ] = & pin_B12 ;
5662 pins [1 ] = & pin_B13 ;
5763 pins [2 ] = & pin_B14 ;
5864 pins [3 ] = & pin_B15 ;
5965 GPIO_InitStructure .Alternate = GPIO_AF5_SPI2 ;
60- } else
66+ // enable the SPI clock
67+ __SPI2_CLK_ENABLE ();
6168#if MICROPY_HW_ENABLE_SPI3
62- if (spi -> Instance == SPI3 ) {
69+ } else if (spi -> Instance == SPI3 ) {
6370 pins [0 ] = & pin_A4 ;
6471 pins [1 ] = & pin_B3 ;
6572 pins [2 ] = & pin_B4 ;
6673 pins [3 ] = & pin_B5 ;
6774 GPIO_InitStructure .Alternate = GPIO_AF6_SPI3 ;
68- } else
75+ // enable the SPI clock
76+ __SPI3_CLK_ENABLE ();
6977#endif
70- {
71- // SPI does not exist for this board
72- nlr_raise ( mp_obj_new_exception_msg ( & mp_type_ValueError , "SPI bus does not exist" )) ;
78+ } else {
79+ // SPI does not exist for this board (shouldn't get here, should be checked by caller)
80+ return ;
7381 }
7482
7583 for (uint i = 0 ; i < 4 ; i ++ ) {
7684 GPIO_InitStructure .Pin = pins [i ]-> pin_mask ;
7785 HAL_GPIO_Init (pins [i ]-> gpio , & GPIO_InitStructure );
7886 }
7987
80- // enable the SPI clock
81- if (spi -> Instance == SPI1 ) {
82- __SPI1_CLK_ENABLE ();
83- } else if (spi -> Instance == SPI2 ) {
84- __SPI2_CLK_ENABLE ();
85- } else if (spi -> Instance == SPI3 ) {
86- __SPI3_CLK_ENABLE ();
87- }
88-
89- // init the I2C device
88+ // init the SPI device
9089 if (HAL_SPI_Init (spi ) != HAL_OK ) {
9190 // init error
9291 // TODO should raise an exception, but this function is not necessarily going to be
@@ -98,29 +97,42 @@ void spi_init(SPI_HandleTypeDef *spi) {
9897
9998void spi_deinit (SPI_HandleTypeDef * spi ) {
10099 HAL_SPI_DeInit (spi );
101- if (spi -> Instance == SPI1 ) {
100+ if (0 ) {
101+ #if MICROPY_HW_ENABLE_SPI1
102+ } else if (spi -> Instance == SPI1 ) {
102103 __SPI1_CLK_DISABLE ();
104+ #endif
103105 } else if (spi -> Instance == SPI2 ) {
104106 __SPI2_CLK_DISABLE ();
107+ #if MICROPY_HW_ENABLE_SPI3
105108 } else if (spi -> Instance == SPI3 ) {
106109 __SPI3_CLK_DISABLE ();
110+ #endif
107111 }
108112}
109113
110114/******************************************************************************/
111115/* Micro Python bindings */
112116
113- #define PYB_NUM_SPI (2)
114-
115117typedef struct _pyb_spi_obj_t {
116118 mp_obj_base_t base ;
117119 SPI_HandleTypeDef * spi ;
118120} pyb_spi_obj_t ;
119121
120- STATIC const pyb_spi_obj_t pyb_spi_obj [PYB_NUM_SPI ] = {
122+ STATIC const pyb_spi_obj_t pyb_spi_obj [] = {
123+ #if MICROPY_HW_ENABLE_SPI1
121124 {{& pyb_spi_type }, & SPIHandle1 },
122- {{& pyb_spi_type }, & SPIHandle2 }
125+ #else
126+ {{& pyb_spi_type }, NULL },
127+ #endif
128+ {{& pyb_spi_type }, & SPIHandle2 },
129+ #if MICROPY_HW_ENABLE_SPI3
130+ {{& pyb_spi_type }, & SPIHandle3 },
131+ #else
132+ {{& pyb_spi_type }, NULL },
133+ #endif
123134};
135+ #define PYB_NUM_SPI (sizeof(pyb_spi_obj) / sizeof(pyb_spi_obj[0]))
124136
125137STATIC void pyb_spi_print (void (* print )(void * env , const char * fmt , ...), void * env , mp_obj_t self_in , mp_print_kind_t kind ) {
126138 pyb_spi_obj_t * self = self_in ;
@@ -223,7 +235,7 @@ STATIC mp_obj_t pyb_spi_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
223235 machine_int_t spi_id = mp_obj_get_int (args [0 ]) - 1 ;
224236
225237 // check SPI number
226- if (!(0 <= spi_id && spi_id < PYB_NUM_SPI )) {
238+ if (!(0 <= spi_id && spi_id < PYB_NUM_SPI && pyb_spi_obj [ spi_id ]. spi != NULL )) {
227239 nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError , "SPI bus %d does not exist" , spi_id + 1 ));
228240 }
229241
0 commit comments