1 /*
2 * Copyright (c) 2020 - 2024 Renesas Electronics Corporation and/or its affiliates
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6 
7 /*******************************************************************************************************************//**
8  * @ingroup RENESAS_TRANSFER_INTERFACES
9  * @defgroup TRANSFER_API Transfer Interface
10  *
11  * @brief Interface for data transfer functions.
12  *
13  * @section TRANSFER_API_SUMMARY Summary
14  * The transfer interface supports background data transfer (no CPU intervention).
15  *
16  *
17  * @{
18  **********************************************************************************************************************/
19 
20 #ifndef R_TRANSFER_API_H
21 #define R_TRANSFER_API_H
22 
23 /***********************************************************************************************************************
24  * Includes
25  **********************************************************************************************************************/
26 
27 /* Common error codes and definitions. */
28 #include "bsp_api.h"
29 
30 /* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */
31 FSP_HEADER
32 
33 /**********************************************************************************************************************
34  * Macro definitions
35  **********************************************************************************************************************/
36 
37 #define TRANSFER_SETTINGS_MODE_BITS           (30U)
38 #define TRANSFER_SETTINGS_SIZE_BITS           (28U)
39 #define TRANSFER_SETTINGS_SRC_ADDR_BITS       (26U)
40 #define TRANSFER_SETTINGS_CHAIN_MODE_BITS     (22U)
41 #define TRANSFER_SETTINGS_IRQ_BITS            (21U)
42 #define TRANSFER_SETTINGS_REPEAT_AREA_BITS    (20U)
43 #define TRANSFER_SETTINGS_DEST_ADDR_BITS      (18U)
44 
45 /**********************************************************************************************************************
46  * Typedef definitions
47  **********************************************************************************************************************/
48 
49 /** Transfer control block.  Allocate an instance specific control block to pass into the transfer API calls.
50  */
51 typedef void transfer_ctrl_t;
52 
53 #ifndef BSP_OVERRIDE_TRANSFER_MODE_T
54 
55 /** Transfer mode describes what will happen when a transfer request occurs. */
56 typedef enum e_transfer_mode
57 {
58     /** In normal mode, each transfer request causes a transfer of @ref transfer_size_t from the source pointer to
59      *  the destination pointer.  The transfer length is decremented and the source and address pointers are
60      *  updated according to @ref transfer_addr_mode_t.  After the transfer length reaches 0, transfer requests
61      *  will not cause any further transfers. */
62     TRANSFER_MODE_NORMAL = 0,
63 
64     /** Repeat mode is like normal mode, except that when the transfer length reaches 0, the pointer to the
65      *  repeat area and the transfer length will be reset to their initial values.  If DMAC is used, the
66      *  transfer repeats only transfer_info_t::num_blocks times.  After the transfer repeats
67      *  transfer_info_t::num_blocks times, transfer requests will not cause any further transfers.  If DTC is
68      *  used, the transfer repeats continuously (no limit to the number of repeat transfers). */
69     TRANSFER_MODE_REPEAT = 1,
70 
71     /** In block mode, each transfer request causes transfer_info_t::length transfers of @ref transfer_size_t.
72      *  After each individual transfer, the source and destination pointers are updated according to
73      *  @ref transfer_addr_mode_t.  After the block transfer is complete, transfer_info_t::num_blocks is
74      *  decremented.  After the transfer_info_t::num_blocks reaches 0, transfer requests will not cause any
75      *  further transfers. */
76     TRANSFER_MODE_BLOCK = 2,
77 
78     /** In addition to block mode features, repeat-block mode supports a ring buffer of blocks and offsets
79      *  within a block (to split blocks into arrays of their first data, second data, etc.) */
80     TRANSFER_MODE_REPEAT_BLOCK = 3
81 } transfer_mode_t;
82 
83 #endif
84 
85 #ifndef BSP_OVERRIDE_TRANSFER_SIZE_T
86 
87 /** Transfer size specifies the size of each individual transfer.
88  *  Total transfer length = transfer_size_t * transfer_length_t
89  */
90 typedef enum e_transfer_size
91 {
92     TRANSFER_SIZE_1_BYTE = 0,          ///< Each transfer transfers a 8-bit value
93     TRANSFER_SIZE_2_BYTE = 1,          ///< Each transfer transfers a 16-bit value
94     TRANSFER_SIZE_4_BYTE = 2           ///< Each transfer transfers a 32-bit value
95 } transfer_size_t;
96 
97 #endif
98 
99 #ifndef BSP_OVERRIDE_TRANSFER_ADDR_MODE_T
100 
101 /** Address mode specifies whether to modify (increment or decrement) pointer after each transfer. */
102 typedef enum e_transfer_addr_mode
103 {
104     /** Address pointer remains fixed after each transfer. */
105     TRANSFER_ADDR_MODE_FIXED = 0,
106 
107     /** Offset is added to the address pointer after each transfer. */
108     TRANSFER_ADDR_MODE_OFFSET = 1,
109 
110     /** Address pointer is incremented by associated @ref transfer_size_t after each transfer. */
111     TRANSFER_ADDR_MODE_INCREMENTED = 2,
112 
113     /** Address pointer is decremented by associated @ref transfer_size_t after each transfer. */
114     TRANSFER_ADDR_MODE_DECREMENTED = 3
115 } transfer_addr_mode_t;
116 
117 #endif
118 
119 #ifndef BSP_OVERRIDE_TRANSFER_REPEAT_AREA_T
120 
121 /** Repeat area options (source or destination).  In @ref TRANSFER_MODE_REPEAT, the selected pointer returns to its
122  *  original value after transfer_info_t::length transfers.  In @ref TRANSFER_MODE_BLOCK and @ref TRANSFER_MODE_REPEAT_BLOCK,
123  *  the selected pointer returns to its original value after each transfer. */
124 typedef enum e_transfer_repeat_area
125 {
126     /** Destination area repeated in @ref TRANSFER_MODE_REPEAT or @ref TRANSFER_MODE_BLOCK or @ref TRANSFER_MODE_REPEAT_BLOCK. */
127     TRANSFER_REPEAT_AREA_DESTINATION = 0,
128 
129     /** Source area repeated in @ref TRANSFER_MODE_REPEAT or @ref TRANSFER_MODE_BLOCK or @ref TRANSFER_MODE_REPEAT_BLOCK. */
130     TRANSFER_REPEAT_AREA_SOURCE = 1
131 } transfer_repeat_area_t;
132 
133 #endif
134 
135 #ifndef BSP_OVERRIDE_TRANSFER_CHAIN_MODE_T
136 
137 /** Chain transfer mode options.
138  *  @note Only applies for DTC. */
139 typedef enum e_transfer_chain_mode
140 {
141     /** Chain mode not used. */
142     TRANSFER_CHAIN_MODE_DISABLED = 0,
143 
144     /** Switch to next transfer after a single transfer from this @ref transfer_info_t. */
145     TRANSFER_CHAIN_MODE_EACH = 2,
146 
147     /** Complete the entire transfer defined in this @ref transfer_info_t before chaining to next transfer. */
148     TRANSFER_CHAIN_MODE_END = 3
149 } transfer_chain_mode_t;
150 
151 #endif
152 
153 #ifndef BSP_OVERRIDE_TRANSFER_IRQ_T
154 
155 /** Interrupt options. */
156 typedef enum e_transfer_irq
157 {
158     /** Interrupt occurs only after last transfer. If this transfer is chained to a subsequent transfer,
159      *  the interrupt will occur only after subsequent chained transfer(s) are complete.
160      *  @warning  DTC triggers the interrupt of the activation source.  Choosing TRANSFER_IRQ_END with DTC will
161      *            prevent activation source interrupts until the transfer is complete. */
162     TRANSFER_IRQ_END = 0,
163 
164     /** Interrupt occurs after each transfer.
165      *  @note     Not available in all HAL drivers.  See HAL driver for details. */
166     TRANSFER_IRQ_EACH = 1
167 } transfer_irq_t;
168 
169 #endif
170 
171 #ifndef BSP_OVERRIDE_TRANSFER_CALLBACK_ARGS_T
172 
173 /** Callback function parameter data. */
174 typedef struct st_transfer_callback_args_t
175 {
176     void const * p_context;            ///< Placeholder for user data.  Set in @ref transfer_api_t::open function in ::transfer_cfg_t.
177 } transfer_callback_args_t;
178 
179 #endif
180 
181 /** Driver specific information. */
182 typedef struct st_transfer_properties
183 {
184     uint32_t block_count_max;           ///< Maximum number of blocks
185     uint32_t block_count_remaining;     ///< Number of blocks remaining
186     uint32_t transfer_length_max;       ///< Maximum number of transfers
187     uint32_t transfer_length_remaining; ///< Number of transfers remaining
188 } transfer_properties_t;
189 
190 #ifndef BSP_OVERRIDE_TRANSFER_INFO_T
191 
192 /** This structure specifies the properties of the transfer.
193  *  @warning  When using DTC, this structure corresponds to the descriptor block registers required by the DTC.
194  *            The following components may be modified by the driver: p_src, p_dest, num_blocks, and length.
195  *  @warning  When using DTC, do NOT reuse this structure to configure multiple transfers.  Each transfer must
196  *            have a unique transfer_info_t.
197  *  @warning  When using DTC, this structure must not be allocated in a temporary location.  Any instance of this
198  *            structure must remain in scope until the transfer it is used for is closed.
199  *  @note     When using DTC, consider placing instances of this structure in a protected section of memory. */
200 typedef struct st_transfer_info
201 {
202     union
203     {
204         struct
205         {
206             uint32_t : 16;
207             uint32_t : 2;
208 
209             /** Select what happens to destination pointer after each transfer. */
210             transfer_addr_mode_t dest_addr_mode : 2;
211 
212             /** Select to repeat source or destination area, unused in @ref TRANSFER_MODE_NORMAL. */
213             transfer_repeat_area_t repeat_area : 1;
214 
215             /** Select if interrupts should occur after each individual transfer or after the completion of all planned
216              *  transfers. */
217             transfer_irq_t irq : 1;
218 
219             /** Select when the chain transfer ends. */
220             transfer_chain_mode_t chain_mode : 2;
221 
222             uint32_t : 2;
223 
224             /** Select what happens to source pointer after each transfer. */
225             transfer_addr_mode_t src_addr_mode : 2;
226 
227             /** Select number of bytes to transfer at once. @see transfer_info_t::length. */
228             transfer_size_t size : 2;
229 
230             /** Select mode from @ref transfer_mode_t. */
231             transfer_mode_t mode : 2;
232         } transfer_settings_word_b;
233 
234         uint32_t transfer_settings_word;
235     };
236 
237     void const * volatile p_src;       ///< Source pointer
238     void * volatile       p_dest;      ///< Destination pointer
239 
240     /** Number of blocks to transfer when using @ref TRANSFER_MODE_BLOCK (both DTC an DMAC) or
241      * @ref TRANSFER_MODE_REPEAT (DMAC only) or
242      * @ref TRANSFER_MODE_REPEAT_BLOCK (DMAC only), unused in other modes. */
243     volatile uint16_t num_blocks;
244 
245     /** Length of each transfer.  Range limited for @ref TRANSFER_MODE_BLOCK, @ref TRANSFER_MODE_REPEAT,
246      *  and @ref TRANSFER_MODE_REPEAT_BLOCK
247      *  see HAL driver for details. */
248     volatile uint16_t length;
249 } transfer_info_t;
250 
251 #endif
252 
253 /** Driver configuration set in @ref transfer_api_t::open. All elements except p_extend are required and must be
254  *  initialized. */
255 typedef struct st_transfer_cfg
256 {
257     /** Pointer to transfer configuration options. If using chain transfer (DTC only), this can be a pointer to
258      *  an array of chained transfers that will be completed in order. */
259     transfer_info_t * p_info;
260 
261     void const * p_extend;             ///< Extension parameter for hardware specific settings.
262 } transfer_cfg_t;
263 
264 /** Select whether to start single or repeated transfer with software start. */
265 typedef enum e_transfer_start_mode
266 {
267     TRANSFER_START_MODE_SINGLE = 0,    ///< Software start triggers single transfer.
268     TRANSFER_START_MODE_REPEAT = 1     ///< Software start transfer continues until transfer is complete.
269 } transfer_start_mode_t;
270 
271 /** Transfer functions implemented at the HAL layer will follow this API. */
272 typedef struct st_transfer_api
273 {
274     /** Initial configuration.
275      *
276      * @param[in,out] p_ctrl   Pointer to control block. Must be declared by user. Elements set here.
277      * @param[in]     p_cfg    Pointer to configuration structure. All elements of this structure
278      *                                               must be set by user.
279      */
280     fsp_err_t (* open)(transfer_ctrl_t * const p_ctrl, transfer_cfg_t const * const p_cfg);
281 
282     /** Reconfigure the transfer.
283      * Enable the transfer if p_info is valid.
284      *
285      * @param[in,out] p_ctrl   Pointer to control block. Must be declared by user. Elements set here.
286      * @param[in]     p_info   Pointer to a new transfer info structure.
287      */
288     fsp_err_t (* reconfigure)(transfer_ctrl_t * const p_ctrl, transfer_info_t * p_info);
289 
290     /** Reset source address pointer, destination address pointer, and/or length, keeping all other settings the same.
291      * Enable the transfer if p_src, p_dest, and length are valid.
292      *
293      * @param[in]     p_ctrl         Control block set in @ref transfer_api_t::open call for this transfer.
294      * @param[in]     p_src          Pointer to source. Set to NULL if source pointer should not change.
295      * @param[in]     p_dest         Pointer to destination. Set to NULL if destination pointer should not change.
296      * @param[in]     num_transfers  Transfer length in normal mode or number of blocks in block mode.  In DMAC only,
297      *                               resets number of repeats (initially stored in transfer_info_t::num_blocks) in
298      *                               repeat mode.  Not used in repeat mode for DTC.
299      */
300     fsp_err_t (* reset)(transfer_ctrl_t * const p_ctrl, void const * p_src, void * p_dest,
301                         uint16_t const num_transfers);
302 
303     /** Enable transfer. Transfers occur after the activation source event (or when
304      * @ref transfer_api_t::softwareStart is called if no peripheral event is chosen as activation source).
305      *
306      * @param[in]     p_ctrl   Control block set in @ref transfer_api_t::open call for this transfer.
307      */
308     fsp_err_t (* enable)(transfer_ctrl_t * const p_ctrl);
309 
310     /** Disable transfer. Transfers do not occur after the activation source event (or when
311      * @ref transfer_api_t::softwareStart is called if no peripheral event is chosen as the DMAC activation source).
312      * @note If a transfer is in progress, it will be completed.  Subsequent transfer requests do not cause a
313      * transfer.
314      *
315      * @param[in]     p_ctrl   Control block set in @ref transfer_api_t::open call for this transfer.
316      */
317     fsp_err_t (* disable)(transfer_ctrl_t * const p_ctrl);
318 
319     /** Start transfer in software.
320      * @warning Only works if no peripheral event is chosen as the DMAC activation source.
321      * @note Not supported for DTC.
322      *
323      * @param[in]     p_ctrl   Control block set in @ref transfer_api_t::open call for this transfer.
324      * @param[in]     mode     Select mode from @ref transfer_start_mode_t.
325      */
326     fsp_err_t (* softwareStart)(transfer_ctrl_t * const p_ctrl, transfer_start_mode_t mode);
327 
328     /** Stop transfer in software. The transfer will stop after completion of the current transfer.
329      * @note Not supported for DTC.
330      * @note Only applies for transfers started with TRANSFER_START_MODE_REPEAT.
331      * @warning Only works if no peripheral event is chosen as the DMAC activation source.
332      *
333      * @param[in]     p_ctrl   Control block set in @ref transfer_api_t::open call for this transfer.
334      */
335     fsp_err_t (* softwareStop)(transfer_ctrl_t * const p_ctrl);
336 
337     /** Provides information about this transfer.
338      *
339      * @param[in]     p_ctrl         Control block set in @ref transfer_api_t::open call for this transfer.
340      * @param[out]    p_properties   Driver specific information.
341      */
342     fsp_err_t (* infoGet)(transfer_ctrl_t * const p_ctrl, transfer_properties_t * const p_properties);
343 
344     /** Releases hardware lock.  This allows a transfer to be reconfigured using @ref transfer_api_t::open.
345      *
346      * @param[in]     p_ctrl    Control block set in @ref transfer_api_t::open call for this transfer.
347      */
348     fsp_err_t (* close)(transfer_ctrl_t * const p_ctrl);
349 
350     /** To update next transfer information without interruption during transfer.
351      *  Allow further transfer continuation.
352      *
353      * @param[in]     p_ctrl         Control block set in @ref transfer_api_t::open call for this transfer.
354      * @param[in]     p_src          Pointer to source. Set to NULL if source pointer should not change.
355      * @param[in]     p_dest         Pointer to destination. Set to NULL if destination pointer should not change.
356      * @param[in]     num_transfers  Transfer length in normal mode or block mode.
357      */
358     fsp_err_t (* reload)(transfer_ctrl_t * const p_ctrl, void const * p_src, void * p_dest,
359                          uint32_t const num_transfers);
360 
361     /** Specify callback function and optional context pointer and working memory pointer.
362      *
363      * @param[in]   p_ctrl                   Control block set in @ref transfer_api_t::open call for this transfer.
364      * @param[in]   p_callback               Callback function to register
365      * @param[in]   p_context                Pointer to send to callback function
366      * @param[in]   p_callback_memory        Pointer to volatile memory where callback structure can be allocated.
367      *                                       Callback arguments allocated here are only valid during the callback.
368      */
369     fsp_err_t (* callbackSet)(transfer_ctrl_t * const p_ctrl, void (* p_callback)(transfer_callback_args_t *),
370                               void const * const p_context, transfer_callback_args_t * const p_callback_memory);
371 } transfer_api_t;
372 
373 /** This structure encompasses everything that is needed to use an instance of this interface. */
374 typedef struct st_transfer_instance
375 {
376     transfer_ctrl_t      * p_ctrl;     ///< Pointer to the control structure for this instance
377     transfer_cfg_t const * p_cfg;      ///< Pointer to the configuration structure for this instance
378     transfer_api_t const * p_api;      ///< Pointer to the API structure for this instance
379 } transfer_instance_t;
380 
381 /* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */
382 FSP_FOOTER
383 
384 #endif
385 
386 /*******************************************************************************************************************//**
387  * @} (end defgroup TRANSFER_API)
388  **********************************************************************************************************************/
389