1515#include <stdlib.h>
1616#include "soc/spi_periph.h"
1717#include "soc/spi_struct.h"
18+ #include "soc/pcr_struct.h"
19+ #include "hal/assert.h"
1820#include "hal/spi_types.h"
1921#include "hal/spi_flash_types.h"
2022#include <sys/param.h> // For MIN/MAX
@@ -31,6 +33,8 @@ extern "C" {
3133
3234typedef typeof (GPSPI2 .clock .val ) gpspi_flash_ll_clock_reg_t ;
3335#define GPSPI_FLASH_LL_PERIPHERAL_FREQUENCY_MHZ (80)
36+ #define GPSPI_FLASH_LL_SUPPORT_CLK_SRC_PRE_DIV (1)
37+ #define GPSPI_FLASH_LL_PERIPH_CLK_DIV_MAX ((SPI_CLKCNT_N + 1) * (SPI_CLKDIV_PRE + 1)) //peripheral internal maxmum clock divider
3438
3539/*------------------------------------------------------------------------------
3640 * Control
@@ -426,6 +430,56 @@ static inline uint32_t gpspi_flash_ll_calculate_clock_reg(uint8_t clkdiv)
426430 return div_parameter ;
427431}
428432
433+ /**
434+ * Set the clock source
435+ *
436+ * @param hw Beginning address of the peripheral registers.
437+ * @param clk_source Clock source to use
438+ */
439+ static inline void gpspi_flash_ll_set_clk_source (spi_dev_t * hw , spi_clock_source_t clk_source )
440+ {
441+ uint32_t clk_id = 0 ;
442+ switch (clk_source ) {
443+ case SOC_MOD_CLK_PLL_F160M :
444+ clk_id = 1 ;
445+ break ;
446+ case SOC_MOD_CLK_RC_FAST :
447+ clk_id = 2 ;
448+ break ;
449+ case SOC_MOD_CLK_XTAL :
450+ clk_id = 0 ;
451+ break ;
452+ default :
453+ HAL_ASSERT (false);
454+ }
455+
456+ PCR .spi2_clkm_conf .spi2_clkm_sel = clk_id ;
457+ }
458+
459+ /**
460+ * Enable/disable SPI flash module clock
461+ *
462+ * @param host_id SPI host ID
463+ * @param enable true to enable, false to disable
464+ */
465+ static inline void gpspi_flash_ll_enable_clock (spi_dev_t * hw , bool enable )
466+ {
467+ PCR .spi2_clkm_conf .spi2_clkm_en = enable ;
468+ }
469+
470+ /**
471+ * Enable/disable SPI flash module clock
472+ *
473+ * @param hw Beginning address of the peripheral registers.
474+ * @param enable true to enable, false to disable
475+ */
476+ static inline void gpspi_flash_ll_clk_source_pre_div (spi_dev_t * hw , uint8_t hs_div , uint8_t mst_div )
477+ {
478+ // In IDF master driver 'mst_div' will be const 2 and 'hs_div' is actually pre_div temporally
479+ (void ) hs_div ;
480+ HAL_FORCE_MODIFY_U32_REG_FIELD (PCR .spi2_clkm_conf , spi2_clkm_div_num , mst_div - 1 );
481+ }
482+
429483#ifdef __cplusplus
430484}
431485#endif
0 commit comments