3636#include "lib/utils/buffer_helper.h"
3737#include "lib/utils/context_manager_helpers.h"
3838#include "py/mperrno.h"
39- #include "py/nlr .h"
39+ #include "py/objproperty .h"
4040#include "py/runtime.h"
4141
4242//| .. currentmodule:: busio
@@ -137,7 +137,9 @@ static void check_lock(busio_spi_obj_t *self) {
137137//|
138138//| Configures the SPI bus. Only valid when locked.
139139//|
140- //| :param int baudrate: the clock rate in Hertz
140+ //| :param int baudrate: the desired clock rate in Hertz. The actual clock rate may be higher or lower
141+ //| due to the granularity of available clock settings.
142+ //| Check the `frequency` attribute for the actual clock rate.
141143//| :param int polarity: the base state of the clock line (0 or 1)
142144//| :param int phase: the edge of the clock that data is captured. First (0)
143145//| or second (1). Rising or falling depends on clock polarity.
@@ -350,6 +352,25 @@ STATIC mp_obj_t busio_spi_write_readinto(size_t n_args, const mp_obj_t *pos_args
350352}
351353MP_DEFINE_CONST_FUN_OBJ_KW (busio_spi_write_readinto_obj , 2 , busio_spi_write_readinto );
352354
355+ //| .. attribute:: frequency
356+ //|
357+ //| The actual SPI bus frequency. This may not match the frequency requested
358+ //| due to internal limitations.
359+ //|
360+ STATIC mp_obj_t busio_spi_obj_get_frequency (mp_obj_t self_in ) {
361+ busio_spi_obj_t * self = MP_OBJ_TO_PTR (self_in );
362+ raise_error_if_deinited (common_hal_busio_spi_deinited (self ));
363+ return MP_OBJ_NEW_SMALL_INT (common_hal_busio_spi_get_frequency (self ));
364+ }
365+ MP_DEFINE_CONST_FUN_OBJ_1 (busio_spi_get_frequency_obj , busio_spi_obj_get_frequency );
366+
367+ const mp_obj_property_t busio_spi_frequency_obj = {
368+ .base .type = & mp_type_property ,
369+ .proxy = {(mp_obj_t )& busio_spi_get_frequency_obj ,
370+ (mp_obj_t )& mp_const_none_obj ,
371+ (mp_obj_t )& mp_const_none_obj },
372+ };
373+
353374STATIC const mp_rom_map_elem_t busio_spi_locals_dict_table [] = {
354375 { MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& busio_spi_deinit_obj ) },
355376 { MP_ROM_QSTR (MP_QSTR___enter__ ), MP_ROM_PTR (& default___enter___obj ) },
@@ -362,6 +383,7 @@ STATIC const mp_rom_map_elem_t busio_spi_locals_dict_table[] = {
362383 { MP_ROM_QSTR (MP_QSTR_readinto ), MP_ROM_PTR (& busio_spi_readinto_obj ) },
363384 { MP_ROM_QSTR (MP_QSTR_write ), MP_ROM_PTR (& busio_spi_write_obj ) },
364385 { MP_ROM_QSTR (MP_QSTR_write_readinto ), MP_ROM_PTR (& busio_spi_write_readinto_obj ) },
386+ { MP_ROM_QSTR (MP_QSTR_frequency ), MP_ROM_PTR (& busio_spi_frequency_obj ) }
365387};
366388STATIC MP_DEFINE_CONST_DICT (busio_spi_locals_dict , busio_spi_locals_dict_table );
367389
0 commit comments