5454/// parameters to init the SPI bus:
5555///
5656/// from pyb import SPI
57- /// spi = SPI(2000000, bits=8, submode =0, cs =SPI.ACTIVE_LOW)
57+ /// spi = SPI(2000000, bits=8, polarity =0, phase=0, nss =SPI.ACTIVE_LOW)
5858///
59- /// Only required parameter is the baudrate, in Hz. Submode may be 0-3 .
59+ /// Only required parameter is the baudrate, in Hz. polarity and phase may be 0 or 1 .
6060/// Bit accepts 8, 16, 32. Chip select values are ACTIVE_LOW and ACTIVE_HIGH
6161///
6262/// Additional method for SPI:
@@ -77,6 +77,8 @@ typedef struct _pyb_spi_obj_t {
7777 vstr_t rx_vstr ;
7878 uint tx_index ;
7979 uint rx_index ;
80+ byte polarity ;
81+ byte phase ;
8082 byte submode ;
8183 byte wlen ;
8284} pyb_spi_obj_t ;
@@ -85,7 +87,6 @@ typedef struct _pyb_spi_obj_t {
8587 DEFINE CONSTANTS
8688 ******************************************************************************/
8789#define PYBSPI_DEF_BAUDRATE 1000000 // 1MHz
88- #define PYBSPI_CS_NONE 0xFF // spi cs is controlled by the user
8990
9091/******************************************************************************
9192 DECLARE PRIVATE DATA
@@ -167,29 +168,32 @@ STATIC void pyb_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
167168 pyb_spi_obj_t * self = self_in ;
168169
169170 if (self -> baudrate > 0 ) {
170- mp_printf (print , "<SPI1, SPI.MASTER, baudrate=%u, config=%u, submode=%u, bits=%u>" ,
171- self -> baudrate , self -> config , self -> submode , (self -> wlen * 8 ));
171+ mp_printf (print , "<SPI1, SPI.MASTER, baudrate=%u, bits=%u, polarity=%u, phase=%u, nss=%q>" ,
172+ self -> baudrate , (self -> wlen * 8 ), self -> polarity , self -> phase ,
173+ (self -> config & SPI_CS_ACTIVELOW ) ? MP_QSTR_ACTIVE_LOW : MP_QSTR_ACTIVE_HIGH );
172174 }
173175 else {
174176 mp_print_str (print , "<SPI1>" );
175177 }
176178}
177179
178- /// \method init(mode, *, baudrate=1000000, bits=8, submode =0, cs =SPI.ACTIVELOW)
180+ /// \method init(mode, *, baudrate=1000000, bits=8, polarity =0, phase=0, nss =SPI.ACTIVELOW)
179181///
180182/// Initialise the SPI bus with the given parameters:
181183///
182184/// - `mode` must be MASTER.
183185/// - `baudrate` is the SCK clock rate.
184186/// - `bits` is the transfer width size (8, 16, 32).
185- /// - `submode` is the spi mode (0, 1, 2, 3).
186- /// - `cs` can be ACTIVELOW, ACTIVEHIGH, or NONE
187+ /// - `polarity` (0, 1).
188+ /// - `phase` (0, 1).
189+ /// - `nss` can be ACTIVE_LOW or ACTIVE_HIGH.
187190static const mp_arg_t pybspi_init_args [] = {
188191 { MP_QSTR_mode , MP_ARG_REQUIRED | MP_ARG_INT , },
189- { MP_QSTR_baudrate , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 1000000 } },
192+ { MP_QSTR_baudrate , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = PYBSPI_DEF_BAUDRATE } },
190193 { MP_QSTR_bits , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 8 } },
191- { MP_QSTR_submode , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
192- { MP_QSTR_cs , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = SPI_CS_ACTIVELOW } },
194+ { MP_QSTR_polarity , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
195+ { MP_QSTR_phase , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
196+ { MP_QSTR_nss , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = SPI_CS_ACTIVELOW } },
193197};
194198
195199STATIC mp_obj_t pyb_spi_init_helper (pyb_spi_obj_t * self , mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
@@ -213,20 +217,38 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const
213217 break ;
214218 }
215219
216- uint submode = args [3 ].u_int ;
217- if (submode < SPI_SUB_MODE_0 || submode > SPI_SUB_MODE_3 ) {
218- nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
220+ uint polarity = args [3 ].u_int ;
221+ uint phase = args [4 ].u_int ;
222+ uint submode ;
223+ if (polarity ) {
224+ if (phase ) {
225+ // polarity = 1, phase = 1
226+ submode = 3 ;
227+ } else {
228+ // polarity = 1, phase = 0
229+ submode = 2 ;
230+ }
231+ } else {
232+ if (phase ) {
233+ // polarity = 0, phase = 1
234+ submode = 1 ;
235+ } else {
236+ // polarity = 0, phase = 0
237+ submode = 0 ;
238+ }
219239 }
220240
221- uint cs = args [4 ].u_int ;
222- if (cs != SPI_CS_ACTIVELOW && cs != SPI_CS_ACTIVEHIGH ) {
241+ uint nss = args [5 ].u_int ;
242+ if (nss != SPI_CS_ACTIVELOW && nss != SPI_CS_ACTIVEHIGH ) {
223243 nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
224244 }
225245
226246 // build the configuration
227247 self -> baudrate = args [1 ].u_int ;
228248 self -> wlen = args [2 ].u_int >> 3 ;
229- self -> config = bits | cs | SPI_SW_CTRL_CS | SPI_4PIN_MODE | SPI_TURBO_OFF ;
249+ self -> config = bits | nss | SPI_SW_CTRL_CS | SPI_4PIN_MODE | SPI_TURBO_OFF ;
250+ self -> polarity = polarity ;
251+ self -> phase = phase ;
230252 self -> submode = submode ;
231253
232254 // init the bus
0 commit comments