1 /* ipm_console.c - Console messages to/from another processor */
2
3 /*
4 * Copyright (c) 2015 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #include <errno.h>
10
11 #include <zephyr/kernel.h>
12 #include <zephyr/sys/ring_buffer.h>
13 #include <zephyr/sys/printk.h>
14 #include <stdio.h>
15 #include <zephyr/drivers/ipm.h>
16 #include <zephyr/drivers/console/ipm_console.h>
17 #include <zephyr/sys/__assert.h>
18
ipm_console_thread(void * arg1,void * arg2,void * arg3)19 static void ipm_console_thread(void *arg1, void *arg2, void *arg3)
20 {
21 uint8_t size32;
22 uint16_t type;
23 int ret, key;
24 const struct ipm_console_receiver_config_info *config_info;
25 struct ipm_console_receiver_runtime_data *driver_data;
26 int pos;
27
28 driver_data = (struct ipm_console_receiver_runtime_data *)arg1;
29 config_info = (const struct ipm_console_receiver_config_info *)arg2;
30 ARG_UNUSED(arg3);
31 size32 = 0U;
32 pos = 0;
33
34 while (1) {
35 k_sem_take(&driver_data->sem, K_FOREVER);
36
37 ret = ring_buf_item_get(&driver_data->rb, &type,
38 (uint8_t *)&config_info->line_buf[pos],
39 NULL, &size32);
40 if (ret) {
41 /* Shouldn't ever happen... */
42 printk("ipm console ring buffer error: %d\n", ret);
43 size32 = 0U;
44 continue;
45 }
46
47 if (config_info->line_buf[pos] == '\n' ||
48 pos == config_info->lb_size - 2) {
49 if (pos != config_info->lb_size - 2) {
50 config_info->line_buf[pos] = '\0';
51 } else {
52 config_info->line_buf[pos + 1] = '\0';
53 }
54 if (config_info->flags & IPM_CONSOLE_PRINTK) {
55 printk("ipm_console: '%s'\n",
56 config_info->line_buf);
57 }
58 if (config_info->flags & IPM_CONSOLE_STDOUT) {
59 printf("ipm_console: '%s'\n",
60 config_info->line_buf);
61 }
62 pos = 0;
63 } else {
64 ++pos;
65 }
66
67 /* ISR may have disabled the channel due to full buffer at
68 * some point. If that happened and there is now room,
69 * re-enable it.
70 *
71 * Lock interrupts to avoid pathological scenario where
72 * the buffer fills up in between enabling the channel and
73 * clearing the channel_disabled flag.
74 */
75 if (driver_data->channel_disabled &&
76 ring_buf_item_space_get(&driver_data->rb)) {
77 key = irq_lock();
78 ipm_set_enabled(driver_data->ipm_device, 1);
79 driver_data->channel_disabled = 0;
80 irq_unlock(key);
81 }
82 }
83 }
84
ipm_console_receive_callback(const struct device * ipm_dev,void * user_data,uint32_t id,volatile void * data)85 static void ipm_console_receive_callback(const struct device *ipm_dev,
86 void *user_data,
87 uint32_t id, volatile void *data)
88 {
89 struct ipm_console_receiver_runtime_data *driver_data = user_data;
90 int ret;
91
92 ARG_UNUSED(data);
93
94 /* Should always be at least one free buffer slot */
95 ret = ring_buf_item_put(&driver_data->rb, 0, id, NULL, 0);
96 __ASSERT(ret == 0, "Failed to insert data into ring buffer");
97 k_sem_give(&driver_data->sem);
98
99 /* If the buffer is now full, disable future interrupts for this channel
100 * until the thread has a chance to consume characters.
101 *
102 * This works without losing data if the sending side tries to send
103 * more characters because the sending side is making an ipm_send()
104 * call with the wait flag enabled. It blocks until the receiver side
105 * re-enables the channel and consumes the data.
106 */
107 if (ring_buf_item_space_get(&driver_data->rb) == 0) {
108 ipm_set_enabled(ipm_dev, 0);
109 driver_data->channel_disabled = 1;
110 }
111 }
112
113
ipm_console_receiver_init(const struct device * d)114 int ipm_console_receiver_init(const struct device *d)
115 {
116 const struct ipm_console_receiver_config_info *config_info =
117 d->config;
118 struct ipm_console_receiver_runtime_data *driver_data = d->data;
119 const struct device *ipm;
120
121 ipm = device_get_binding(config_info->bind_to);
122
123 if (!ipm) {
124 printk("unable to bind IPM console receiver to '%s'\n",
125 config_info->bind_to);
126 return -EINVAL;
127 }
128
129 if (ipm_max_id_val_get(ipm) < 0xFF) {
130 printk("IPM driver %s doesn't support 8-bit id values",
131 config_info->bind_to);
132 return -EINVAL;
133 }
134
135 driver_data->ipm_device = ipm;
136 driver_data->channel_disabled = 0;
137 k_sem_init(&driver_data->sem, 0, K_SEM_MAX_LIMIT);
138 ring_buf_item_init(&driver_data->rb, config_info->rb_size32,
139 config_info->ring_buf_data);
140
141 ipm_register_callback(ipm, ipm_console_receive_callback, driver_data);
142
143 k_thread_create(&driver_data->rx_thread, config_info->thread_stack,
144 CONFIG_IPM_CONSOLE_STACK_SIZE, ipm_console_thread,
145 driver_data, (void *)config_info, NULL,
146 K_PRIO_COOP(IPM_CONSOLE_PRI), 0, K_NO_WAIT);
147 ipm_set_enabled(ipm, 1);
148
149 return 0;
150 }
151