Lines Matching +full:fixed +full:- +full:baudrate

3  * Copyright 2019-2023, NXP
6 * SPDX-License-Identifier: Apache-2.0
32 #include "i2c-priv.h"
36 #define SCAN_DELAY_US(baudrate) (12 * USEC_PER_SEC / baudrate) argument
40 ((const struct mcux_lpi2c_config *)(_dev)->config)
41 #define DEV_DATA(_dev) ((struct mcux_lpi2c_data *)(_dev)->data)
79 const struct mcux_lpi2c_config *config = dev->config; in mcux_lpi2c_configure()
80 struct mcux_lpi2c_data *data = dev->data; in mcux_lpi2c_configure()
83 uint32_t baudrate; in mcux_lpi2c_configure() local
87 return -EINVAL; in mcux_lpi2c_configure()
91 return -EINVAL; in mcux_lpi2c_configure()
96 baudrate = KHZ(100); in mcux_lpi2c_configure()
99 baudrate = KHZ(400); in mcux_lpi2c_configure()
102 baudrate = MHZ(1); in mcux_lpi2c_configure()
105 return -EINVAL; in mcux_lpi2c_configure()
108 if (clock_control_get_rate(config->clock_dev, config->clock_subsys, in mcux_lpi2c_configure()
110 return -EINVAL; in mcux_lpi2c_configure()
113 ret = k_sem_take(&data->lock, K_FOREVER); in mcux_lpi2c_configure()
118 LPI2C_MasterSetBaudRate(base, clock_freq, baudrate); in mcux_lpi2c_configure()
119 k_sem_give(&data->lock); in mcux_lpi2c_configure()
133 data->callback_status = status; in mcux_lpi2c_master_transfer_callback()
134 k_sem_give(&data->device_sync_sem); in mcux_lpi2c_master_transfer_callback()
155 const struct mcux_lpi2c_config *config = dev->config; in mcux_lpi2c_transfer()
156 struct mcux_lpi2c_data *data = dev->data; in mcux_lpi2c_transfer()
162 ret = k_sem_take(&data->lock, K_FOREVER); in mcux_lpi2c_transfer()
169 if (I2C_MSG_ADDR_10_BITS & msgs->flags) { in mcux_lpi2c_transfer()
170 ret = -ENOTSUP; in mcux_lpi2c_transfer()
175 transfer.flags = mcux_lpi2c_convert_flags(msgs->flags); in mcux_lpi2c_transfer()
180 if (i != 0 && !(msgs->flags & I2C_MSG_RESTART)) { in mcux_lpi2c_transfer()
185 transfer.direction = (msgs->flags & I2C_MSG_READ) in mcux_lpi2c_transfer()
189 transfer.data = msgs->buf; in mcux_lpi2c_transfer()
190 transfer.dataSize = msgs->len; in mcux_lpi2c_transfer()
194 &data->handle, &transfer); in mcux_lpi2c_transfer()
200 LPI2C_MasterTransferAbort(base, &data->handle); in mcux_lpi2c_transfer()
201 ret = -EIO; in mcux_lpi2c_transfer()
206 k_sem_take(&data->device_sync_sem, K_FOREVER); in mcux_lpi2c_transfer()
211 if (data->callback_status != kStatus_Success) { in mcux_lpi2c_transfer()
212 LPI2C_MasterTransferAbort(base, &data->handle); in mcux_lpi2c_transfer()
213 ret = -EIO; in mcux_lpi2c_transfer()
216 if (msgs->len == 0) { in mcux_lpi2c_transfer()
217 k_busy_wait(SCAN_DELAY_US(config->bitrate)); in mcux_lpi2c_transfer()
218 if (0 != (base->MSR & LPI2C_MSR_NDF_MASK)) { in mcux_lpi2c_transfer()
219 LPI2C_MasterTransferAbort(base, &data->handle); in mcux_lpi2c_transfer()
220 ret = -EIO; in mcux_lpi2c_transfer()
228 k_sem_give(&data->lock); in mcux_lpi2c_transfer()
238 gpio_pin_set_dt(&config->scl, state); in mcux_lpi2c_bitbang_set_scl()
245 gpio_pin_set_dt(&config->sda, state); in mcux_lpi2c_bitbang_set_sda()
252 return gpio_pin_get_dt(&config->sda) == 0 ? 0 : 1; in mcux_lpi2c_bitbang_get_sda()
257 const struct mcux_lpi2c_config *config = dev->config; in mcux_lpi2c_recover_bus()
258 struct mcux_lpi2c_data *data = dev->data; in mcux_lpi2c_recover_bus()
268 if (!gpio_is_ready_dt(&config->scl)) { in mcux_lpi2c_recover_bus()
270 return -EIO; in mcux_lpi2c_recover_bus()
273 if (!gpio_is_ready_dt(&config->sda)) { in mcux_lpi2c_recover_bus()
275 return -EIO; in mcux_lpi2c_recover_bus()
278 k_sem_take(&data->lock, K_FOREVER); in mcux_lpi2c_recover_bus()
280 error = gpio_pin_configure_dt(&config->scl, GPIO_OUTPUT_HIGH); in mcux_lpi2c_recover_bus()
286 error = gpio_pin_configure_dt(&config->sda, GPIO_OUTPUT_HIGH); in mcux_lpi2c_recover_bus()
294 bitrate_cfg = i2c_map_dt_bitrate(config->bitrate) | I2C_MODE_CONTROLLER; in mcux_lpi2c_recover_bus()
308 (void)pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT); in mcux_lpi2c_recover_bus()
310 k_sem_give(&data->lock); in mcux_lpi2c_recover_bus()
319 struct mcux_lpi2c_data *data = dev->data; in mcux_lpi2c_slave_irq_handler()
321 const struct i2c_target_callbacks *target_cb = data->target_cfg->callbacks; in mcux_lpi2c_slave_irq_handler()
326 /* Note- the HAL provides a callback-based I2C slave API, but in mcux_lpi2c_slave_irq_handler()
328 * a fixed length at the first byte received, and will not signal in mcux_lpi2c_slave_irq_handler()
339 data->first_tx = true; in mcux_lpi2c_slave_irq_handler()
341 data->send_ack = true; in mcux_lpi2c_slave_irq_handler()
346 i2c_data = (uint8_t)base->SRDR; in mcux_lpi2c_slave_irq_handler()
347 if (data->first_tx) { in mcux_lpi2c_slave_irq_handler()
348 data->first_tx = false; in mcux_lpi2c_slave_irq_handler()
349 if (target_cb->write_requested) { in mcux_lpi2c_slave_irq_handler()
350 ret = target_cb->write_requested(data->target_cfg); in mcux_lpi2c_slave_irq_handler()
353 data->send_ack = false; in mcux_lpi2c_slave_irq_handler()
357 if (target_cb->write_received) { in mcux_lpi2c_slave_irq_handler()
358 ret = target_cb->write_received(data->target_cfg, in mcux_lpi2c_slave_irq_handler()
362 data->send_ack = false; in mcux_lpi2c_slave_irq_handler()
369 if (data->first_tx) { in mcux_lpi2c_slave_irq_handler()
370 data->read_active = true; in mcux_lpi2c_slave_irq_handler()
371 data->first_tx = false; in mcux_lpi2c_slave_irq_handler()
372 if (target_cb->read_requested) { in mcux_lpi2c_slave_irq_handler()
373 ret = target_cb->read_requested(data->target_cfg, in mcux_lpi2c_slave_irq_handler()
377 data->read_active = false; in mcux_lpi2c_slave_irq_handler()
380 base->STDR = i2c_data; in mcux_lpi2c_slave_irq_handler()
383 } else if (data->read_active) { in mcux_lpi2c_slave_irq_handler()
384 if (target_cb->read_processed) { in mcux_lpi2c_slave_irq_handler()
385 ret = target_cb->read_processed(data->target_cfg, in mcux_lpi2c_slave_irq_handler()
389 data->read_active = false; in mcux_lpi2c_slave_irq_handler()
392 base->STDR = i2c_data; in mcux_lpi2c_slave_irq_handler()
400 if (target_cb->stop) { in mcux_lpi2c_slave_irq_handler()
401 target_cb->stop(data->target_cfg); in mcux_lpi2c_slave_irq_handler()
406 LPI2C_SlaveTransmitAck(base, data->send_ack); in mcux_lpi2c_slave_irq_handler()
413 const struct mcux_lpi2c_config *config = dev->config; in mcux_lpi2c_target_register()
414 struct mcux_lpi2c_data *data = dev->data; in mcux_lpi2c_target_register()
422 if (clock_control_get_rate(config->clock_dev, config->clock_subsys, in mcux_lpi2c_target_register()
424 return -EINVAL; in mcux_lpi2c_target_register()
428 return -EINVAL; in mcux_lpi2c_target_register()
431 if (data->target_attached) { in mcux_lpi2c_target_register()
432 return -EBUSY; in mcux_lpi2c_target_register()
435 data->target_attached = true; in mcux_lpi2c_target_register()
436 data->target_cfg = target_config; in mcux_lpi2c_target_register()
437 data->first_tx = false; in mcux_lpi2c_target_register()
440 slave_config.address0 = target_config->address; in mcux_lpi2c_target_register()
441 /* Note- this setting enables clock stretching to allow the in mcux_lpi2c_target_register()
462 struct mcux_lpi2c_data *data = dev->data; in mcux_lpi2c_target_unregister()
465 if (!data->target_attached) { in mcux_lpi2c_target_unregister()
466 return -EINVAL; in mcux_lpi2c_target_unregister()
469 data->target_cfg = NULL; in mcux_lpi2c_target_unregister()
470 data->target_attached = false; in mcux_lpi2c_target_unregister()
480 struct mcux_lpi2c_data *data = dev->data; in mcux_lpi2c_isr()
484 if (data->target_attached) { in mcux_lpi2c_isr()
489 LPI2C_MasterTransferHandleIRQ(LPI2C_GetInstance(base), &data->handle); in mcux_lpi2c_isr()
491 LPI2C_MasterTransferHandleIRQ(base, &data->handle); in mcux_lpi2c_isr()
497 const struct mcux_lpi2c_config *config = dev->config; in mcux_lpi2c_init()
498 struct mcux_lpi2c_data *data = dev->data; in mcux_lpi2c_init()
508 k_sem_init(&data->lock, 1, 1); in mcux_lpi2c_init()
509 k_sem_init(&data->device_sync_sem, 0, K_SEM_MAX_LIMIT); in mcux_lpi2c_init()
511 if (!device_is_ready(config->clock_dev)) { in mcux_lpi2c_init()
513 return -ENODEV; in mcux_lpi2c_init()
516 error = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT); in mcux_lpi2c_init()
521 if (clock_control_get_rate(config->clock_dev, config->clock_subsys, in mcux_lpi2c_init()
523 return -EINVAL; in mcux_lpi2c_init()
527 master_config.busIdleTimeout_ns = config->bus_idle_timeout_ns; in mcux_lpi2c_init()
529 LPI2C_MasterTransferCreateHandle(base, &data->handle, in mcux_lpi2c_init()
533 bitrate_cfg = i2c_map_dt_bitrate(config->bitrate); in mcux_lpi2c_init()
543 nxp_lp_flexcomm_setirqhandler(config->parent_dev, dev, in mcux_lpi2c_init()
547 config->irq_config_func(dev); in mcux_lpi2c_init()