1 /*
2 Copyright (c) 2018, MIPI Alliance, Inc.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 
9 * Redistributions of source code must retain the above copyright
10   notice, this list of conditions and the following disclaimer.
11 
12 * Redistributions in binary form must reproduce the above copyright
13   notice, this list of conditions and the following disclaimer in
14   the documentation and/or other materials provided with the
15   distribution.
16 
17 * Neither the name of the copyright holder nor the names of its
18   contributors may be used to endorse or promote products derived
19   from this software without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 /*
35  * Contributors:
36  * Norbert Schulz (Intel Corporation) - Initial API and implementation
37  */
38 
39 /* Example platform adaptation
40  * This "platform" shows how to implement a SyS-T library platform module
41  * to simulate MIPI STP data protocol generation for SyS-T messages.
42  */
43 #ifndef MIPI_SYST_PLATFORM_INCLUDED
44 #define MIPI_SYST_PLATFORM_INCLUDED
45 
46 
47 /* Uncomment to turn code inlining off.
48  *
49  * #undef MIPI_SYST_PCFG_ENABLE_INLINE
50  */
51 
52 #if defined(MIPI_SYST_PCFG_ENABLE_HEAP_MEMORY)
53 #include <stdlib.h>
54 #endif
55 
56 #include <stdio.h>
57 
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61 
62 	struct MipiStpWriter;
63 
64 	struct mipi_syst_platform_state {
65 		void (*write_d32ts)(struct mipi_syst_handle * systh, mipi_syst_u32 v);
66 		void (*write_d32mts)(struct mipi_syst_handle * systh, mipi_syst_u32 v);
67 		void (*write_d64mts)(struct mipi_syst_handle * systh, mipi_syst_u64 v);
68 
69 		void (*write_d8)(struct mipi_syst_handle * systh, mipi_syst_u8 v);
70 		void (*write_d16)(struct mipi_syst_handle * systh, mipi_syst_u16 v);
71 		void (*write_d32)(struct mipi_syst_handle * systh, mipi_syst_u32 v);
72 #if defined(MIPI_SYST_PCFG_ENABLE_64BIT_IO)
73 		void (*write_d64)(struct mipi_syst_handle * systh, mipi_syst_u64 v);
74 #endif
75 		void (*write_flag)(struct mipi_syst_handle * systh);
76 
77 		struct stp_writer_data * stpWriter;
78 	};
79 
80 	extern MIPI_SYST_EXPORT void MIPI_SYST_CALLCONV
81 		mipi_syst_platform_init(struct mipi_syst_header* , const void *);
82 	extern MIPI_SYST_EXPORT void MIPI_SYST_CALLCONV
83 		mipi_syst_platform_destroy(struct mipi_syst_header* systh);
84 
85 	struct mipi_syst_platform_handle {
86 		mipi_syst_u32 master;
87 		mipi_syst_u32 channel;
88 	};
89 
90 
91 
92 #if defined(MIPI_SYST_PCFG_ENABLE_HEAP_MEMORY)
93 
94 /**
95 * Map heap memory allocation to platform malloc() implementation.
96 *
97 * This function is used for handle allocations if heap usage
98 * is supported by the platform.
99 *
100 * @param s number of bytes to allocate
101 * @see MIPI_SYST_HEAP_FREE
102 */
103 #define MIPI_SYST_HEAP_MALLOC(s) mipi_syst_platform_alloc(s)
104 
105 /**
106 * Map heap memory free function  to platform free() implementation.
107 *
108 * This function is used for handle release if heap usage
109 * is supported by the platform.
110 *
111 * @param p pointer previously returned from MIPI_SYST_HEAP_MALLOC or NULL.
112 * @see MIPI_SYST_HEAP_MALLOC
113 */
114 #define MIPI_SYST_HEAP_FREE(p)   mipi_syst_platform_free(p)
115 
116 extern MIPI_SYST_EXPORT void * MIPI_SYST_CALLCONV  mipi_syst_platform_alloc(size_t s);
117 extern MIPI_SYST_EXPORT void MIPI_SYST_CALLCONV mipi_syst_platform_free(void *);
118 #endif
119 
120 #if defined(MIPI_SYST_PCFG_ENABLE_TIMESTAMP)
121 /* This example uses UNIX epoch time in micro second resolution
122  * as own clock.
123  */
124 #define MIPI_SYST_PLATFORM_CLOCK() mipi_syst_get_epoch_us()
125 #define MIPI_SYST_PLATFORM_FREQ()  1000000
126 
127 MIPI_SYST_EXPORT mipi_syst_u64  MIPI_SYST_CALLCONV mipi_syst_get_epoch_us();
128 #endif /* defined(MIPI_SYST_PCFG_ENABLE_TIMESTAMP) */
129 
130 /* IO output routine mapping
131  * Call the function pointers in the global SyS-T state
132  */
133 #define MIPI_SYST_OUTPUT_D32TS(syst_handle, data) \
134 	(syst_handle)->systh_header->systh_platform.write_d32ts((syst_handle), (data))
135 #define MIPI_SYST_OUTPUT_D32MTS(syst_handle, data) \
136 	(syst_handle)->systh_header->systh_platform.write_d32mts((syst_handle), (data))
137 #define MIPI_SYST_OUTPUT_D64MTS(syst_handle, data) \
138 	(syst_handle)->systh_header->systh_platform.write_d64mts((syst_handle), (data))
139 #define MIPI_SYST_OUTPUT_D8(syst_handle, data) \
140 	(syst_handle)->systh_header->systh_platform.write_d8((syst_handle), (data))
141 #define MIPI_SYST_OUTPUT_D16(syst_handle, data) \
142 	(syst_handle)->systh_header->systh_platform.write_d16((syst_handle), (data))
143 #define MIPI_SYST_OUTPUT_D32(syst_handle, data) \
144 	(syst_handle)->systh_header->systh_platform.write_d32((syst_handle), (data))
145 #if defined(MIPI_SYST_PCFG_ENABLE_64BIT_IO)
146 #define MIPI_SYST_OUTPUT_D64(syst_handle, data) \
147 	(syst_handle)->systh_header->systh_platform.write_d64((syst_handle), (data))
148 #endif
149 #define MIPI_SYST_OUTPUT_FLAG(syst_handle) \
150 	(syst_handle)->systh_header->systh_platform.write_flag((syst_handle))
151 
152 #ifdef __cplusplus
153 } /* extern C */
154 #endif
155 
156 #endif
157