aboutsummaryrefslogtreecommitdiffstats
path: root/demos/ATSAMA5D2/RT-SAMA5D2-XPLAINED-NSEC/tsclient.c
blob: dcf3ed0647684e27df54b5d3841ecdec64c2ac47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
    ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio.

    This file is part of ChibiOS.

    ChibiOS is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file    tsclient.c
 * @brief   TSSI client module code.
 */

#include "ch.h"
#include "tsclient.h"

/*===========================================================================*/
/* Module local definitions.                                                 */
/*===========================================================================*/

/*===========================================================================*/
/* Module exported variables.                                                */
/*===========================================================================*/
EVENTSOURCE_DECL(stubsEventSource);

/*===========================================================================*/
/* Module local types.                                                       */
/*===========================================================================*/

/*===========================================================================*/
/* Module local variables.                                                   */
/*===========================================================================*/

/*===========================================================================*/
/* Module local functions.                                                   */
/*===========================================================================*/

/*===========================================================================*/
/* Module exported functions.                                                */
/*===========================================================================*/

/*
 * @brief   Yields all the idle time to secure world.
 * @note    see CH_CFG_IDLE_LOOP_HOOK in chconf.h
 *
 * @notapi
 */
void tsIdle(void) {
  (void)tsInvoke1(TS_HND_IDLE, 0, 0, TS_GRANTED_TIMESLICE * 10);
}

/**
 * @brief   Call a service via smc instruction with yield times.
 * @details a time slice will be yielded also to the lower prio NSEC threads,
 *          whenever the service call is interrupted.
 *          This avoids the starvation of NSEC threads that run to lower prio,
 *          due to continue polling of the called service status.
 *
 * @param[in] handle        The handle of the service to invoke.
 *                          The handle is obtained by an invoke to discovery
 *                          service.
 * @param[in,out] svc_data  Service request data, often a reference to a more
 *                          complex structure.
 * @param[in] svc_datalen   Size of the svc_data memory area.
 * @param[in] svc_nsec_time The time slice that will be yielded to the lower
 *                          prio NSEC threads, whenever the service call is
 *                          interrupted, in microseconds.
 *                          This avoids the starvation of NSEC threads that run
 *                          to lower prio, due to continue polling of the
 *                          called service status.
 *                          0 means no time slice is yielded.
 *
 * @return                  The service status. The value depends on the service.
 *
 * @retval SMC_SVC_OK       generic success value.
 * @retval SMC_SVC_BUSY     the service has a pending request.
 * @retval SMC_SVC_INVALID  bad parameters.
 * @retval SMC_SVC_NOENT    no such service.
 * @retval SMC_SVC_BADH     bad handle.
 *
 * @api
 */
msg_t tsInvokeService(ts_service_t handle, ts_params_area_t data,
                       size_t size)
{
  msg_t result;

  result = tsInvoke1(handle, data, size, TS_GRANTED_TIMESLICE);
  while (result == SMC_SVC_INTR) {
    chThdSleepMicroseconds(TS_GRANTED_TIMESLICE);
    result = tsInvoke1(TS_HND_STQRY, handle, 0, TS_GRANTED_TIMESLICE);
  }
  return result;
}

/**
 * @brief   Call a service via smc instruction without yield time to NSEC.
 *
 * @param[in] handle        The handle of the service to invoke.
 *                          The handle is obtained by an invoke to discovery
 *                          service.
 * @param[in,out] svc_data  Service request data, often a reference to a more
 *                          complex structure.
 * @param[in] svc_datalen   Size of the svc_data memory area.
 *
 * @return                  The service status. The value depends on the service.
 *
 * @retval SMC_SVC_OK       generic success value.
 * @retval SMC_SVC_BUSY     the service has a pending request.
 * @retval SMC_SVC_INVALID  bad parameters.
 * @retval SMC_SVC_NOENT    no such service.
 * @retval SMC_SVC_BADH     bad handle.
 *
 * @api
 */
msg_t tsInvokeServiceNoYield(ts_service_t handle, ts_params_area_t data,
                       size_t size)
{
  msg_t result;

  result = tsInvoke1(handle, data, size, TS_GRANTED_TIMESLICE);
  while (result == SMC_SVC_INTR) {
    result = tsInvoke1(TS_HND_STQRY, handle, 0, TS_GRANTED_TIMESLICE);
  }
  return result;
}