Lines Matching +full:slave +full:- +full:mode

4  * SPDX-License-Identifier: Apache-2.0
15 #define DEV_BASE(dev) (((struct lpc11u6x_i2c_config *)(dev->config))->base)
23 clock_control_get_rate(clk_dev, (clock_control_subsys_t) cfg->clkid, in lpc11u6x_i2c_set_bus_speed()
27 cfg->base->sclh = div / 2; in lpc11u6x_i2c_set_bus_speed()
28 cfg->base->scll = div - (div / 2); in lpc11u6x_i2c_set_bus_speed()
34 const struct lpc11u6x_i2c_config *cfg = dev->config; in lpc11u6x_i2c_configure()
35 struct lpc11u6x_i2c_data *data = dev->data; in lpc11u6x_i2c_configure()
53 return -ENOTSUP; in lpc11u6x_i2c_configure()
55 return -EINVAL; in lpc11u6x_i2c_configure()
59 return -ENOTSUP; in lpc11u6x_i2c_configure()
62 k_mutex_lock(&data->mutex, K_FOREVER); in lpc11u6x_i2c_configure()
63 lpc11u6x_i2c_set_bus_speed(cfg, cfg->clock_dev, speed); in lpc11u6x_i2c_configure()
65 ret = pinctrl_apply_state(cfg->pincfg, mux_selection); in lpc11u6x_i2c_configure()
67 k_mutex_unlock(&data->mutex); in lpc11u6x_i2c_configure()
70 k_mutex_unlock(&data->mutex); in lpc11u6x_i2c_configure()
78 const struct lpc11u6x_i2c_config *cfg = dev->config; in lpc11u6x_i2c_transfer()
79 struct lpc11u6x_i2c_data *data = dev->data; in lpc11u6x_i2c_transfer()
86 k_mutex_lock(&data->mutex, K_FOREVER); in lpc11u6x_i2c_transfer()
88 data->transfer.msgs = msgs; in lpc11u6x_i2c_transfer()
89 data->transfer.curr_buf = msgs->buf; in lpc11u6x_i2c_transfer()
90 data->transfer.curr_len = msgs->len; in lpc11u6x_i2c_transfer()
91 data->transfer.nr_msgs = num_msgs; in lpc11u6x_i2c_transfer()
92 data->transfer.addr = addr; in lpc11u6x_i2c_transfer()
95 cfg->base->con_clr = LPC11U6X_I2C_CONTROL_SI | in lpc11u6x_i2c_transfer()
99 data->transfer.status = LPC11U6X_I2C_STATUS_BUSY; in lpc11u6x_i2c_transfer()
100 cfg->base->con_set = LPC11U6X_I2C_CONTROL_START; in lpc11u6x_i2c_transfer()
102 k_sem_take(&data->completion, K_FOREVER); in lpc11u6x_i2c_transfer()
104 if (data->transfer.status != LPC11U6X_I2C_STATUS_OK) { in lpc11u6x_i2c_transfer()
105 ret = -EIO; in lpc11u6x_i2c_transfer()
107 data->transfer.status = LPC11U6X_I2C_STATUS_INACTIVE; in lpc11u6x_i2c_transfer()
109 /* If a slave is registered, put the controller in slave mode */ in lpc11u6x_i2c_transfer()
110 if (data->slave) { in lpc11u6x_i2c_transfer()
111 cfg->base->con_set = LPC11U6X_I2C_CONTROL_AA; in lpc11u6x_i2c_transfer()
114 k_mutex_unlock(&data->mutex); in lpc11u6x_i2c_transfer()
121 const struct lpc11u6x_i2c_config *dev_cfg = dev->config; in lpc11u6x_i2c_slave_register()
122 struct lpc11u6x_i2c_data *data = dev->data; in lpc11u6x_i2c_slave_register()
126 return -EINVAL; in lpc11u6x_i2c_slave_register()
129 if (cfg->flags & I2C_TARGET_FLAGS_ADDR_10_BITS) { in lpc11u6x_i2c_slave_register()
130 return -ENOTSUP; in lpc11u6x_i2c_slave_register()
133 k_mutex_lock(&data->mutex, K_FOREVER); in lpc11u6x_i2c_slave_register()
134 if (data->slave) { in lpc11u6x_i2c_slave_register()
135 ret = -EBUSY; in lpc11u6x_i2c_slave_register()
139 data->slave = cfg; in lpc11u6x_i2c_slave_register()
140 /* Configure controller to act as slave */ in lpc11u6x_i2c_slave_register()
141 dev_cfg->base->addr0 = (cfg->address << 1); in lpc11u6x_i2c_slave_register()
142 dev_cfg->base->con_clr = LPC11U6X_I2C_CONTROL_START | in lpc11u6x_i2c_slave_register()
144 dev_cfg->base->con_set = LPC11U6X_I2C_CONTROL_AA; in lpc11u6x_i2c_slave_register()
147 k_mutex_unlock(&data->mutex); in lpc11u6x_i2c_slave_register()
155 const struct lpc11u6x_i2c_config *dev_cfg = dev->config; in lpc11u6x_i2c_slave_unregister()
156 struct lpc11u6x_i2c_data *data = dev->data; in lpc11u6x_i2c_slave_unregister()
159 return -EINVAL; in lpc11u6x_i2c_slave_unregister()
161 if (data->slave != cfg) { in lpc11u6x_i2c_slave_unregister()
162 return -EINVAL; in lpc11u6x_i2c_slave_unregister()
165 k_mutex_lock(&data->mutex, K_FOREVER); in lpc11u6x_i2c_slave_unregister()
166 data->slave = NULL; in lpc11u6x_i2c_slave_unregister()
167 dev_cfg->base->con_clr = LPC11U6X_I2C_CONTROL_AA; in lpc11u6x_i2c_slave_unregister()
168 k_mutex_unlock(&data->mutex); in lpc11u6x_i2c_slave_unregister()
175 struct lpc11u6x_i2c_data *data = dev->data; in lpc11u6x_i2c_isr()
177 struct lpc11u6x_i2c_current_transfer *transfer = &data->transfer; in lpc11u6x_i2c_isr()
182 switch (i2c->stat) { in lpc11u6x_i2c_isr()
186 i2c->dat = (transfer->addr << 1) | in lpc11u6x_i2c_isr()
187 (transfer->msgs->flags & I2C_MSG_READ); in lpc11u6x_i2c_isr()
189 transfer->curr_buf = transfer->msgs->buf; in lpc11u6x_i2c_isr()
190 transfer->curr_len = transfer->msgs->len; in lpc11u6x_i2c_isr()
195 if (!transfer->curr_len) { in lpc11u6x_i2c_isr()
196 transfer->msgs++; in lpc11u6x_i2c_isr()
197 transfer->nr_msgs--; in lpc11u6x_i2c_isr()
198 if (!transfer->nr_msgs) { in lpc11u6x_i2c_isr()
199 transfer->status = LPC11U6X_I2C_STATUS_OK; in lpc11u6x_i2c_isr()
205 i2c->dat = transfer->curr_buf[0]; in lpc11u6x_i2c_isr()
206 transfer->curr_buf++; in lpc11u6x_i2c_isr()
207 transfer->curr_len--; in lpc11u6x_i2c_isr()
213 transfer->msgs++; in lpc11u6x_i2c_isr()
214 transfer->nr_msgs--; in lpc11u6x_i2c_isr()
215 set |= (transfer->nr_msgs ? LPC11U6X_I2C_CONTROL_START : in lpc11u6x_i2c_isr()
217 if (!transfer->nr_msgs) { in lpc11u6x_i2c_isr()
218 transfer->status = LPC11U6X_I2C_STATUS_OK; in lpc11u6x_i2c_isr()
222 transfer->curr_buf[0] = i2c->dat; in lpc11u6x_i2c_isr()
223 transfer->curr_buf++; in lpc11u6x_i2c_isr()
224 transfer->curr_len--; in lpc11u6x_i2c_isr()
227 if (transfer->curr_len <= 1) { in lpc11u6x_i2c_isr()
234 /* Slave States */ in lpc11u6x_i2c_isr()
239 if (data->slave->callbacks->write_requested(data->slave)) { in lpc11u6x_i2c_isr()
246 val = i2c->dat; in lpc11u6x_i2c_isr()
247 if (data->slave->callbacks->write_received(data->slave, val)) { in lpc11u6x_i2c_isr()
254 val = i2c->dat; in lpc11u6x_i2c_isr()
255 data->slave->callbacks->write_received(data->slave, val); in lpc11u6x_i2c_isr()
256 data->slave->callbacks->stop(data->slave); in lpc11u6x_i2c_isr()
261 data->slave->callbacks->stop(data->slave); in lpc11u6x_i2c_isr()
267 if (data->slave->callbacks->read_requested(data->slave, &val)) { in lpc11u6x_i2c_isr()
270 i2c->dat = val; in lpc11u6x_i2c_isr()
273 if (data->slave->callbacks->read_processed(data->slave, &val)) { in lpc11u6x_i2c_isr()
276 i2c->dat = val; in lpc11u6x_i2c_isr()
280 data->slave->callbacks->stop(data->slave); in lpc11u6x_i2c_isr()
289 transfer->status = LPC11U6X_I2C_STATUS_FAIL; in lpc11u6x_i2c_isr()
298 i2c->con_clr = clear; in lpc11u6x_i2c_isr()
299 i2c->con_set = set; in lpc11u6x_i2c_isr()
300 if ((transfer->status != LPC11U6X_I2C_STATUS_BUSY) && in lpc11u6x_i2c_isr()
301 (transfer->status != LPC11U6X_I2C_STATUS_INACTIVE)) { in lpc11u6x_i2c_isr()
302 k_sem_give(&data->completion); in lpc11u6x_i2c_isr()
309 const struct lpc11u6x_i2c_config *cfg = dev->config; in lpc11u6x_i2c_init()
310 struct lpc11u6x_i2c_data *data = dev->data; in lpc11u6x_i2c_init()
313 err = pinctrl_apply_state(cfg->pincfg, PINCTRL_STATE_DEFAULT); in lpc11u6x_i2c_init()
318 if (!device_is_ready(cfg->clock_dev)) { in lpc11u6x_i2c_init()
319 return -ENODEV; in lpc11u6x_i2c_init()
322 /* Configure clock and de-assert reset for I2Cx */ in lpc11u6x_i2c_init()
323 clock_control_on(cfg->clock_dev, (clock_control_subsys_t) cfg->clkid); in lpc11u6x_i2c_init()
326 lpc11u6x_i2c_set_bus_speed(cfg, cfg->clock_dev, 100000); in lpc11u6x_i2c_init()
329 cfg->base->con_clr = LPC11U6X_I2C_CONTROL_AA | LPC11U6X_I2C_CONTROL_SI | in lpc11u6x_i2c_init()
331 cfg->base->con_set = LPC11U6X_I2C_CONTROL_I2C_EN; in lpc11u6x_i2c_init()
334 k_mutex_init(&data->mutex); in lpc11u6x_i2c_init()
335 k_sem_init(&data->completion, 0, 1); in lpc11u6x_i2c_init()
337 data->transfer.status = LPC11U6X_I2C_STATUS_INACTIVE; in lpc11u6x_i2c_init()
339 cfg->irq_config_func(dev); in lpc11u6x_i2c_init()