Skip to content

Commit 5dbe322

Browse files
decsnynashif
authored andcommitted
drivers: spi_mcux_lpspi: Clean up configure func
Do all input validation etc BEFORE setting up configuration, instead of mixing these things everywhere. Also condense the formatting of the code. Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
1 parent b7c35fb commit 5dbe322

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

drivers/spi/spi_mcux_lpspi.c

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -160,49 +160,25 @@ static int spi_mcux_configure(const struct device *dev, const struct spi_config
160160
const struct spi_mcux_config *config = dev->config;
161161
struct spi_mcux_data *data = dev->data;
162162
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
163+
uint32_t word_size = SPI_WORD_SIZE_GET(spi_cfg->operation);
163164
lpspi_master_config_t master_config;
164165
uint32_t clock_freq;
165-
uint32_t word_size;
166166

167167
if (spi_cfg->operation & SPI_HALF_DUPLEX) {
168168
LOG_ERR("Half-duplex not supported");
169169
return -ENOTSUP;
170170
}
171171

172-
LPSPI_MasterGetDefaultConfig(&master_config);
173-
174172
if (spi_cfg->slave > CHIP_SELECT_COUNT) {
175173
LOG_ERR("Slave %d is greater than %d", spi_cfg->slave, CHIP_SELECT_COUNT);
176174
return -EINVAL;
177175
}
178176

179-
word_size = SPI_WORD_SIZE_GET(spi_cfg->operation);
180177
if (word_size > MAX_DATA_WIDTH) {
181178
LOG_ERR("Word size %d is greater than %d", word_size, MAX_DATA_WIDTH);
182179
return -EINVAL;
183180
}
184181

185-
master_config.bitsPerFrame = word_size;
186-
187-
master_config.cpol = (SPI_MODE_GET(spi_cfg->operation) & SPI_MODE_CPOL)
188-
? kLPSPI_ClockPolarityActiveLow
189-
: kLPSPI_ClockPolarityActiveHigh;
190-
191-
master_config.cpha = (SPI_MODE_GET(spi_cfg->operation) & SPI_MODE_CPHA)
192-
? kLPSPI_ClockPhaseSecondEdge
193-
: kLPSPI_ClockPhaseFirstEdge;
194-
195-
master_config.direction =
196-
(spi_cfg->operation & SPI_TRANSFER_LSB) ? kLPSPI_LsbFirst : kLPSPI_MsbFirst;
197-
198-
master_config.baudRate = spi_cfg->frequency;
199-
200-
master_config.pcsToSckDelayInNanoSec = config->pcs_sck_delay;
201-
master_config.lastSckToPcsDelayInNanoSec = config->sck_pcs_delay;
202-
master_config.betweenTransferDelayInNanoSec = config->transfer_delay;
203-
204-
master_config.pinCfg = config->data_pin_config;
205-
206182
if (!device_is_ready(config->clock_dev)) {
207183
LOG_ERR("clock control device not ready");
208184
return -ENODEV;
@@ -226,17 +202,32 @@ static int spi_mcux_configure(const struct device *dev, const struct spi_config
226202
}
227203
}
228204

229-
LPSPI_MasterInit(base, &master_config, clock_freq);
230-
231205
if (IS_ENABLED(CONFIG_DEBUG)) {
232206
base->CR |= LPSPI_CR_DBGEN_MASK;
233207
}
234208

235-
LPSPI_MasterTransferCreateHandle(base, &data->handle, spi_mcux_master_callback, data);
209+
data->ctx.config = spi_cfg;
236210

237-
LPSPI_SetDummyData(base, 0);
211+
LPSPI_MasterGetDefaultConfig(&master_config);
238212

239-
data->ctx.config = spi_cfg;
213+
master_config.bitsPerFrame = word_size;
214+
master_config.cpol = (SPI_MODE_GET(spi_cfg->operation) & SPI_MODE_CPOL)
215+
? kLPSPI_ClockPolarityActiveLow
216+
: kLPSPI_ClockPolarityActiveHigh;
217+
master_config.cpha = (SPI_MODE_GET(spi_cfg->operation) & SPI_MODE_CPHA)
218+
? kLPSPI_ClockPhaseSecondEdge
219+
: kLPSPI_ClockPhaseFirstEdge;
220+
master_config.direction =
221+
(spi_cfg->operation & SPI_TRANSFER_LSB) ? kLPSPI_LsbFirst : kLPSPI_MsbFirst;
222+
master_config.baudRate = spi_cfg->frequency;
223+
master_config.pcsToSckDelayInNanoSec = config->pcs_sck_delay;
224+
master_config.lastSckToPcsDelayInNanoSec = config->sck_pcs_delay;
225+
master_config.betweenTransferDelayInNanoSec = config->transfer_delay;
226+
master_config.pinCfg = config->data_pin_config;
227+
228+
LPSPI_MasterInit(base, &master_config, clock_freq);
229+
LPSPI_MasterTransferCreateHandle(base, &data->handle, spi_mcux_master_callback, data);
230+
LPSPI_SetDummyData(base, 0);
240231

241232
return 0;
242233
}

0 commit comments

Comments
 (0)