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 #ifndef ZEPHYR_INCLUDE_DRIVERS_CONSOLE_IPM_CONSOLE_H_
10 #define ZEPHYR_INCLUDE_DRIVERS_CONSOLE_IPM_CONSOLE_H_
11 
12 #include <zephyr/kernel.h>
13 #include <zephyr/device.h>
14 #include <zephyr/sys/ring_buffer.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 #define IPM_CONSOLE_STDOUT	(BIT(0))
21 #define IPM_CONSOLE_PRINTK	(BIT(1))
22 
23 /*
24  * Good way to determine these numbers other than trial-and-error?
25  * using printf() in the thread seems to require a lot more stack space
26  */
27 #define IPM_CONSOLE_STACK_SIZE		CONFIG_IPM_CONSOLE_STACK_SIZE
28 #define IPM_CONSOLE_PRI			2
29 
30 struct ipm_console_receiver_config_info {
31 	/** Name of the low-level IPM driver to bind to */
32 	char *bind_to;
33 
34 	/**
35 	 * Stack for the receiver's thread, which prints out messages as
36 	 * they come in. Should be sized CONFIG_IPM_CONSOLE_STACK_SIZE
37 	 */
38 	k_thread_stack_t *thread_stack;
39 
40 	/**
41 	 * Ring buffer data area for stashing characters from the interrupt
42 	 * callback
43 	 */
44 	uint32_t *ring_buf_data;
45 
46 	/** Size of ring_buf_data in 32-bit chunks */
47 	unsigned int rb_size32;
48 
49 	/**
50 	 * Line buffer for incoming messages, characters accumulate here
51 	 * and then are sent to printk() once full (including a trailing NULL)
52 	 * or a carriage return seen
53 	 */
54 	char *line_buf;
55 
56 	/** Size in bytes of the line buffer. Must be at least 2 */
57 	unsigned int lb_size;
58 
59 	/**
60 	 * Destination for received console messages, one of
61 	 * IPM_CONSOLE_STDOUT or IPM_CONSOLE_PRINTK
62 	 */
63 	unsigned int flags;
64 };
65 
66 struct ipm_console_receiver_runtime_data {
67 	/** Buffer for received bytes from the low-level IPM device */
68 	struct ring_buf rb;
69 
70 	/** Semaphore to wake up the thread to print out messages */
71 	struct k_sem sem;
72 
73 	/** pointer to the bound low-level IPM device */
74 	const struct device *ipm_device;
75 
76 	/** Indicator that the channel is temporarily disabled due to
77 	 * full buffer
78 	 */
79 	int channel_disabled;
80 
81 	/** Receiver worker thread */
82 	struct k_thread rx_thread;
83 };
84 
85 struct ipm_console_sender_config_info {
86 	/** Name of the low-level driver to bind to */
87 	char *bind_to;
88 
89 	/**
90 	 * Source of messages to forward, hooks will be installed.
91 	 * Can be IPM_CONSOLE_STDOUT, IPM_CONSOLE_PRINTK, or both
92 	 */
93 	int flags;
94 };
95 
96 #if CONFIG_IPM_CONSOLE_RECEIVER
97 int ipm_console_receiver_init(const struct device *d);
98 #endif
99 
100 #if CONFIG_IPM_CONSOLE_SENDER
101 int ipm_console_sender_init(const struct device *d);
102 #endif
103 
104 #ifdef __cplusplus
105 }
106 #endif
107 
108 #endif /* ZEPHYR_INCLUDE_DRIVERS_CONSOLE_IPM_CONSOLE_H_ */
109