static void ipm_console_receive_callback(void *context, u32_t id, volatile void *data) { struct device *d; struct ipm_console_receiver_runtime_data *driver_data; int ret; ARG_UNUSED(data); d = context; driver_data = d->driver_data; /* Should always be at least one free buffer slot */ ret = sys_ring_buf_put(&driver_data->rb, 0, id, NULL, 0); __ASSERT(ret == 0, "Failed to insert data into ring buffer"); k_sem_give(&driver_data->sem); /* If the buffer is now full, disable future interrupts for this channel * until the thread has a chance to consume characters. * * This works without losing data if the sending side tries to send * more characters because the sending side is making an ipm_send() * call with the wait flag enabled. It blocks until the receiver side * re-enables the channel and consumes the data. */ if (sys_ring_buf_space_get(&driver_data->rb) == 0) { ipm_set_enabled(driver_data->ipm_device, 0); driver_data->channel_disabled = 1; } }
int sys_ring_buf_put(struct ring_buf *buf, uint16_t type, uint8_t value, uint32_t *data, uint8_t size32) { uint32_t i, space, index, rc; space = sys_ring_buf_space_get(buf); if (space >= (size32 + 1)) { struct ring_element *header = (struct ring_element *)&buf->buf[buf->tail]; header->type = type; header->length = size32; header->value = value; if (likely(buf->mask)) { for (i = 0; i < size32; ++i) { index = (i + buf->tail + 1) & buf->mask; buf->buf[index] = data[i]; } buf->tail = (buf->tail + size32 + 1) & buf->mask; } else { for (i = 0; i < size32; ++i) { index = (i + buf->tail + 1) % buf->size; buf->buf[index] = data[i]; } buf->tail = (buf->tail + size32 + 1) % buf->size; } rc = 0; } else { buf->dropped_put_count++; rc = -EMSGSIZE; } return rc; }
static void ipm_console_thread(void *arg1, void *arg2, void *arg3) { u8_t size32; u16_t type; int ret, key; struct device *d; const struct ipm_console_receiver_config_info *config_info; struct ipm_console_receiver_runtime_data *driver_data; int pos; d = (struct device *)arg1; driver_data = d->driver_data; config_info = d->config->config_info; ARG_UNUSED(arg2); size32 = 0; pos = 0; while (1) { k_sem_take(&driver_data->sem, K_FOREVER); ret = sys_ring_buf_get(&driver_data->rb, &type, (u8_t *)&config_info->line_buf[pos], NULL, &size32); if (ret) { /* Shouldn't ever happen... */ printk("ipm console ring buffer error: %d\n", ret); size32 = 0; continue; } if (config_info->line_buf[pos] == '\n' || pos == config_info->lb_size - 2) { if (pos != config_info->lb_size - 2) { config_info->line_buf[pos] = '\0'; } else { config_info->line_buf[pos + 1] = '\0'; } if (config_info->flags & IPM_CONSOLE_PRINTK) { printk("%s: '%s'\n", d->config->name, config_info->line_buf); } if (config_info->flags & IPM_CONSOLE_STDOUT) { printf("%s: '%s'\n", d->config->name, config_info->line_buf); } pos = 0; } else { ++pos; } /* ISR may have disabled the channel due to full buffer at * some point. If that happened and there is now room, * re-enable it. * * Lock interrupts to avoid pathological scenario where * the buffer fills up in between enabling the channel and * clearing the channel_disabled flag. */ if (driver_data->channel_disabled && sys_ring_buf_space_get(&driver_data->rb)) { key = irq_lock(); ipm_set_enabled(driver_data->ipm_device, 1); driver_data->channel_disabled = 0; irq_unlock(key); } } }