@@ -115,7 +115,7 @@ void spi_flash_init(void) {
115115 .dpmconfig = false
116116 },
117117 .phy_if = {
118- .sck_freq = NRF_QSPI_FREQ_32MDIV16 ,
118+ .sck_freq = NRF_QSPI_FREQ_32MDIV16 , // Start at a slow 2mhz and speed up once we know what we're talking to.
119119 .sck_delay = 10 , // min time CS must stay high before going low again. in unit of 62.5 ns
120120 .spi_mode = NRF_QSPI_MODE_0 ,
121121 .dpmen = false
@@ -132,7 +132,7 @@ void spi_flash_init(void) {
132132 qspi_cfg .pins .io2_pin = MICROPY_QSPI_DATA2 ;
133133 qspi_cfg .pins .io3_pin = MICROPY_QSPI_DATA3 ;
134134 qspi_cfg .prot_if .readoc = NRF_QSPI_READOC_READ4IO ;
135- qspi_cfg .prot_if .writeoc = NRF_QSPI_WRITEOC_PP4IO ;
135+ qspi_cfg .prot_if .writeoc = NRF_QSPI_WRITEOC_PP4O ;
136136#endif
137137
138138 // No callback for blocking API
@@ -142,5 +142,17 @@ void spi_flash_init(void) {
142142void spi_flash_init_device (const external_flash_device * device ) {
143143 check_quad_enable (device );
144144
145- // TODO(tannewt): Adjust the speed for the found device.
145+ // Switch to single output line if the device doesn't support quad programs.
146+ if (!device -> supports_qspi_writes ) {
147+ NRF_QSPI -> IFCONFIG0 &= ~QSPI_IFCONFIG0_WRITEOC_Msk ;
148+ NRF_QSPI -> IFCONFIG0 |= QSPI_IFCONFIG0_WRITEOC_PP ;
149+ }
150+
151+ // Speed up as much as we can.
152+ uint8_t sckfreq = 0 ;
153+ while (32000000 / (sckfreq + 1 ) > device -> max_clock_speed_mhz * 1000000 && sckfreq < 16 ) {
154+ sckfreq += 1 ;
155+ }
156+ NRF_QSPI -> IFCONFIG1 &= ~QSPI_IFCONFIG1_SCKFREQ_Msk ;
157+ NRF_QSPI -> IFCONFIG1 |= sckfreq << QSPI_IFCONFIG1_SCKDELAY_Pos ;
146158}
0 commit comments