Skip to content

Commit 3771a09

Browse files
committed
stmhal: Improve USART class, to be more like SPI and I2C.
The three classes I2C, SPI and USART now have a fairly uniform (Python) API. All are constructed, initialised and deinitialised in the same way. They can have most of their parameters set, using keyword arguments. All have send and recv (although slightly different with I2C requiring an address in master mode). recv can do inplace receiving (ie store the data in a previously-created bytearray). It's just polling mode at the moment, but interrupt and DMA would be nice to add.
1 parent eb8bdf4 commit 3771a09

3 files changed

Lines changed: 216 additions & 83 deletions

File tree

stmhal/qstrdefsport.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,17 @@ Q(off)
7979
Q(toggle)
8080
Q(intensity)
8181

82-
// for USART object
82+
// for USART class
8383
Q(USART)
84-
Q(status)
85-
Q(recv_chr)
86-
Q(send_chr)
84+
Q(baudrate)
85+
Q(bits)
86+
Q(stop)
87+
Q(parity)
88+
Q(init)
89+
Q(deinit)
90+
Q(all)
8791
Q(send)
92+
Q(recv)
8893

8994
// for ExtInt class
9095
Q(ExtInt)
@@ -134,7 +139,7 @@ Q(baudrate)
134139
Q(polarity)
135140
Q(phase)
136141
Q(dir)
137-
Q(size)
142+
Q(bits)
138143
Q(nss)
139144
Q(firstbit)
140145
Q(ti)

stmhal/spi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ STATIC void pyb_spi_print(void (*print)(void *env, const char *fmt, ...), void *
167167
} else {
168168
print(env, "SPI(%u, SPI.SLAVE", spi_num);
169169
}
170-
print(env, ", polarity=%u, phase=%u, size=%u", self->spi->Init.CLKPolarity == SPI_POLARITY_LOW ? 0 : 1, self->spi->Init.CLKPhase == SPI_PHASE_1EDGE ? 1 : 2, self->spi->Init.DataSize == SPI_DATASIZE_8BIT ? 8 : 16);
170+
print(env, ", polarity=%u, phase=%u, bits=%u", self->spi->Init.CLKPolarity == SPI_POLARITY_LOW ? 0 : 1, self->spi->Init.CLKPhase == SPI_PHASE_1EDGE ? 1 : 2, self->spi->Init.DataSize == SPI_DATASIZE_8BIT ? 8 : 16);
171171
if (self->spi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED) {
172172
print(env, ", crc=0x%x", self->spi->Init.CRCPolynomial);
173173
}
@@ -181,7 +181,7 @@ STATIC const mp_arg_parse_t pyb_spi_init_accepted_args[] = {
181181
{ MP_QSTR_polarity, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = 1} },
182182
{ MP_QSTR_phase, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = 1} },
183183
{ MP_QSTR_dir, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = SPI_DIRECTION_2LINES} },
184-
{ MP_QSTR_size, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = 8} },
184+
{ MP_QSTR_bits, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = 8} },
185185
{ MP_QSTR_nss, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = SPI_NSS_SOFT} },
186186
{ MP_QSTR_firstbit, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_INT, {.u_int = SPI_FIRSTBIT_MSB} },
187187
{ MP_QSTR_ti, MP_ARG_PARSE_KW_ONLY | MP_ARG_PARSE_BOOL, {.u_bool = false} },

0 commit comments

Comments
 (0)