diff --git a/ports/psoc6/machine_i2s.c b/ports/psoc6/machine_i2s.c index afb91d2fd04c4..38d4a3675ae80 100644 --- a/ports/psoc6/machine_i2s.c +++ b/ports/psoc6/machine_i2s.c @@ -323,6 +323,25 @@ static void mp_machine_i2s_init_helper(machine_i2s_obj_t *self, mp_arg_val_t *ar mp_raise_ValueError(MP_ERROR_TEXT("invalid format")); } + // uint32_t audio_clock_freq_hz; + uint32_t rate = args[ARG_rate].u_int; + + if (rate == 8000 || + rate == 16000 || + rate == 32000 || + rate == 48000) { + if (freq_peri != AUDIO_I2S_98_MHZ) { + mp_raise_ValueError(MP_ERROR_TEXT("Invalid clock frequency set for the sample rate/ I2S Clock not set . Set the right clock before initialising I2S")); + } + } else if (rate == 22050 || + rate == 44100) { + if (freq_peri != AUDIO_I2S_90_MHZ) { + mp_raise_ValueError(MP_ERROR_TEXT("Invalid clock frequency set for the sample rate/ I2S Clock not set. Set the right clock before initialising I2S")); + } + } else { + mp_raise_ValueError(MP_ERROR_TEXT("rate not supported")); + } + // is valid buf size ? int32_t ring_buffer_len = args[ARG_ibuf].u_int; if (ring_buffer_len < 0) { @@ -343,9 +362,6 @@ static void mp_machine_i2s_init_helper(machine_i2s_obj_t *self, mp_arg_val_t *ar self->ring_buffer_storage = m_new(uint8_t, ring_buffer_len); ringbuf_init(&self->ring_buffer, self->ring_buffer_storage, ring_buffer_len); - if (!clock_set_i2s) { - mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("I2S clock not set. Set the clock before initialising I2S")); - } i2s_init(self, &audio_clock); i2s_dma_init(self); } diff --git a/ports/psoc6/modmachine.c b/ports/psoc6/modmachine.c index 4ab9facce18b3..1f61331122cf3 100644 --- a/ports/psoc6/modmachine.c +++ b/ports/psoc6/modmachine.c @@ -61,19 +61,8 @@ enum { MACHINE_SOFT_RESET }; -// enums to hold the frequency constants -enum clock_freq_type { - AUDIO_I2S_98_MHZ = 98000000, - AUDIO_I2S_90_MHZ = 90000000, - AUDIO_PDM_24_576_000_HZ = 24576000, - AUDIO_PDM_22_579_000_HZ = 22579000, - CM4, - CM4_FLL -}; - uint32_t reset_cause; -bool clock_set_i2s = false; -bool clock_set_pdm = false; +enum clock_freq_type freq_peri; // function to return 64-bit silicon ID of given PSoC microcontroller // A combined 64-bit unique ID. [63:57] - DIE_YEAR [56:56] - DIE_MINOR [55:48] - DIE_SORT [47:40] - DIE_Y [39:32] - DIE_X [31:24] - DIE_WAFER [23:16] - DIE_LOT[2] [15: 8] - DIE_LOT[1] [ 7: 0] - DIE_LOT[0] @@ -523,7 +512,7 @@ void audio_pdm_set_frequency(uint32_t freq) { static void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) { - enum clock_freq_type freq_peri = mp_obj_get_int(args[0]); // Assuming the enum values are used as integers + freq_peri = mp_obj_get_int(args[0]); // Assuming the enum values are used as integers switch (freq_peri) { case AUDIO_I2S_90_MHZ: audio_i2s_set_frequency(freq_peri); // i2s audio fz diff --git a/ports/psoc6/modmachine.h b/ports/psoc6/modmachine.h index 0e18e76cad5bc..8719d2c77f9cf 100644 --- a/ports/psoc6/modmachine.h +++ b/ports/psoc6/modmachine.h @@ -77,4 +77,16 @@ extern cyhal_clock_t audio_clock; extern bool clock_set_i2s; extern bool clock_set_pdm; +// enums to hold the frequency constants +enum clock_freq_type { + AUDIO_I2S_98_MHZ = 98000000, + AUDIO_I2S_90_MHZ = 90000000, + AUDIO_PDM_24_576_000_HZ = 24576000, + AUDIO_PDM_22_579_000_HZ = 22579000, + CM4, + CM4_FLL +}; + +extern enum clock_freq_type freq_peri; + #endif // MICROPY_INCLUDED_PSOC6_MODMACHINE_H