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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
****************************************************************************
PROJECT : VLIB Macro Driver
FILE : $Id: r_pbg_main.c 14596 2017-08-30 04:29:06Z shinya.tomari $
============================================================================
DESCRIPTION
Driver for the XC GUARD macro
============================================================================
C O P Y R I G H T
============================================================================
Copyright (c) 2012
by
Renesas Electronics (Europe) GmbH.
Arcadiastrasse 10
D-40472 Duesseldorf
Germany
All rights reserved.
============================================================================
Purpose: only for testing
DISCLAIMER
This software is supplied by Renesas Electronics Corporation and is only
intended for use with Renesas products. No other uses are authorized. This
software is owned by Renesas Electronics Corporation and is protected under
all applicable laws, including copyright laws.
THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
Renesas reserves the right, without notice, to make changes to this software
and to discontinue the availability of this software. By using this software,
you agree to the additional terms and conditions found by accessing the
following link:
http://www.renesas.com/disclaimer *
Copyright (C) 2011 Renesas Electronics Corporation. All rights reserved.
****************************************************************************
*/
/*******************************************************************************
Title: PBG Main Module
Implementation of the API and other global functions.
*/
/*******************************************************************************
Section: Includes
*/
#include "r_typedefs.h"
#include "r_dev_api.h"
#include "r_config_pbg.h"
#include "r_pbg_api.h"
#define PBG_MAIN_GLOBALS_
#include "r_pbg_main.h"
#include "r_pbg_sys.h"
/*******************************************************************************
Section: Local Constants
*/
#define LOC_PBG_UNUSED_FUNC (0) /* 0:disable 1:enable */
/*******************************************************************************
Constant: loc_PBGVersion
A string containig the macro driver version information
*/
static const int8_t loc_pbgDrvVer[] = R_VERSION(PBG, R_PBG_VERSION_HI, R_PBG_VERSION_LO);
/*******************************************************************************
Section: Local Variables
*/
/*******************************************************************************
Variable: loc_ErrorHandler
A function pointer to a user callback (Applications error handler)
The value is assigned by the application using the driver
*/
static void (*loc_ErrorHandler )(uint32_t Unit, uint32_t Error) = 0u;
/*******************************************************************************
Section: Global Functions
*/
/*******************************************************************************
Function: R_PBG_CheckMacroParams
See: <PBG_main.h>
*/
r_pbg_Error_t R_PBG_CheckMacroParams(uint32_t Unit);
r_pbg_Error_t R_PBG_CheckMacroParams(uint32_t Unit)
{
/* Feel free to add any more usefull parameter checks */
if (R_PBG_MACRO_NUM <= Unit)
{
return R_PBG_ERR_RANGE;
}
return R_PBG_ERR_OK;
}
/*******************************************************************************
Function: R_PBG_ErrorHandler
See: <PBG_main.h>
*/
void R_PBG_ErrorHandler(uint32_t Unit, r_pbg_Error_t Error)
{
if (0 != loc_ErrorHandler)
{ /* we've got a registered error handler here ...*/
loc_ErrorHandler(Unit, (uint32_t) Error); /* ... and we gonna use it!*/
}
else
{
for(;;)
{ /* we don't have a handler */
} /* so, let's wait here */
}
}
/*******************************************************************************
Section: Global API Functions
See: <r_pbg_api.h>
*/
/*******************************************************************************
Function: R_PBG_GetVersionStr
See: <r_pbg_api.h>
*/
const int8_t * R_PBG_GetVersionStr(void)
{
return (const int8_t *) loc_pbgDrvVer;
}
/*******************************************************************************
Function: R_PBG_GetMajorVersion
See: <r_pbg_api.h>
*/
const uint16_t R_PBG_GetMajorVersion(void)
{
return (uint16_t)R_PBG_VERSION_HI;
}
/*******************************************************************************
Function: R_PBG_GetMinorVersion
See: <r_pbg_api.h>
*/
const uint16_t R_PBG_GetMinorVersion(void)
{
return (uint16_t)R_PBG_VERSION_LO;
}
/*******************************************************************************
Function: R_PBG_Init
See: <r_pbg_api.h>
*/
r_pbg_Error_t R_PBG_Init(void)
{
r_dev_PbgInstance_t Instance;
for (Instance = (r_dev_PbgInstance_t)0; Instance < R_DEV_PBG_NUM; Instance++)
{
R_PBG_InitInstance(Instance);
}
return R_PBG_ERR_OK;
}
/*******************************************************************************
Function: R_PBG_InitInstance
See: <r_pbg_api.h>
*/
r_pbg_Error_t R_PBG_InitInstance(r_dev_PbgInstance_t Instance)
{
r_pbg_Id_t Id;
r_pbg_Error_t Err = R_PBG_ERR_NG;
r_pbg_Error_t LoopErr;
Id.Instance = Instance;
for (Id.Channel = 0; Id.Channel < 16; Id.Channel++)
{
/* Try to initialialize a certain channel, risking that it might not exist */
LoopErr = R_PBG_InitChannel(Id);
/* If at least one channel was set up correctly, return OK. */
if (R_PBG_ERR_OK == LoopErr)
{
Err = R_PBG_ERR_OK;
}
}
return Err;
}
/*******************************************************************************
Function: R_PBG_InitChannel
See: <r_pbg_api.h>
*/
r_pbg_Error_t R_PBG_InitChannel(r_pbg_Id_t Id)
{
r_pbg_t Instance;
r_pbg_Error_t err = R_PBG_SYS_GetInstance(Id, &Instance);
/* Initialize channel if it exists */
if (R_PBG_ERR_OK == err)
{
R_PBG_WRITE32(Instance.Base0 + (4 * Id.Channel), R_PBG_DEFAULT_SETTING);
}
return err;
}
/*******************************************************************************
Function: R_PBG_ConfigureGuard
See: <r_pbg_api.h>
*/
r_pbg_Error_t R_PBG_ConfigureGuard(r_pbg_Id_t Id, r_pbg_Config_t Config)
{
r_pbg_t Instance;
r_pbg_Error_t err = R_PBG_SYS_GetInstance(Id, &Instance);
uint32_t prot_reg_value = 0;
uint32_t prot_reg_address = 0;
if (R_PBG_ERR_OK != err)
{
/* ERROR - Guard does not exist */
}
else
{
prot_reg_value |= (Config.PROTLOCK << PROTLOCK_BIT);
prot_reg_value |= (Config.PROTVM << PROTVM_BIT);
prot_reg_value |= (Config.PROTUM << PROTUM_BIT);
prot_reg_value |= (Config.PROTPEID << PROTPEID_BIT);
prot_reg_value |= (Config.VCID << VCID_BIT);
prot_reg_value |= (Config.PROTSPID << PROTSPID_BIT);
prot_reg_value |= (Config.PROTDEB << PROTDEB_BIT);
prot_reg_value |= (Config.PROTRDPDEF << PROTRDPDEF_BIT);
prot_reg_value |= (Config.PROTWRPDEF << PROTWRPDEF_BIT);
prot_reg_value |= (Config.PROTRD << PROTRD_BIT);
prot_reg_value |= (Config.PROTWR << PROTWR_BIT);
prot_reg_address = Instance.Base0 + (4 * Id.Channel);
R_PBG_WRITE32(prot_reg_address, prot_reg_value);
}
return err;
}
/*******************************************************************************
Function: R_PBG_ConfigureGuardSysCall
See: <r_pbg_api.h>
*/
r_pbg_Error_t R_PBG_ConfigureGuardSysCall(r_pbg_Id_t Id, r_pbg_Config_t Config)
{
return R_PBG_ConfigureGuardSysCall_Internal(Id, Config);
}
/*******************************************************************************
Function: R_PBG_ClearErrorStatus
See: <r_pbg_api.h>
*/
void R_PBG_ClearErrorStatus(r_pbg_Id_t Id)
{
r_pbg_t Instance;
r_pbg_Error_t err = R_PBG_SYS_GetInstance(Id, &Instance);
uint32_t ctl_reg_address = 0;
if (R_PBG_ERR_OK != err)
{
/* ERROR - Guard does not exist */
}
else
{
ctl_reg_address = Instance.Base1 + R_PBG_REG_CTL;
R_PBG_WRITE32(ctl_reg_address, 0x03);
}
}
/*******************************************************************************
Function: R_PBG_GetErrorStatus
See: <r_pbg_api.h>
*/
uint32_t R_PBG_GetErrorStatus(r_pbg_Id_t Id)
{
r_pbg_t Instance;
r_pbg_Error_t err = R_PBG_SYS_GetInstance(Id, &Instance);
uint32_t stat_reg_address = 0;
if (R_PBG_ERR_OK != err)
{
/* ERROR - Guard does not exist */
return 0;
}
else
{
stat_reg_address = Instance.Base1 + R_PBG_REG_STAT;
return R_PBG_READ32(stat_reg_address);
}
}
/*******************************************************************************
Function: R_PBG_GetErrorDetails
See: <r_pbg_api.h>
*/
uint32_t R_PBG_GetErrorDetails(r_pbg_Id_t Id, uint32_t *Addr, uint32_t *Type)
{
r_pbg_t Instance;
r_pbg_Error_t err = R_PBG_SYS_GetInstance(Id, &Instance);
uint32_t addr_reg_address = 0;
uint32_t type_reg_address = 0;
if (R_PBG_ERR_OK != err)
{
/* ERROR - Guard does not exist */
return 0;
}
else
{
addr_reg_address = Instance.Base1 + R_PBG_REG_ADDR;
type_reg_address = Instance.Base1 + R_PBG_REG_TYPE;
if (0 != Addr)
{
*Addr = R_PBG_READ32(addr_reg_address);
}
if (0 != Type)
{
*Type = R_PBG_READ32(type_reg_address);
}
return 0;
}
}
#if LOC_PBG_UNUSED_FUNC
/*******************************************************************************
Function: R_PBG_DeInit
See: <r_pbg_api.h>
*/
r_pbg_Error_t R_PBG_DeInit(uint32_t Unit)
{
r_pbg_Error_t err;
r_pbg_Interrupt_t interrupt;
err = R_PBG_CheckMacroParams(Unit);
if (R_PBG_ERR_OK != err)
{
return err;
}
if (0 != r_pbg_Dev[Unit].BaseAddr)
{
for (interrupt = (r_pbg_Interrupt_t)0; interrupt < R_PBG_INT_NUM; interrupt++)
{
R_PBG_DisableInt(Unit, interrupt);
R_PBG_SetIsrCallback(Unit, interrupt, 0);
}
r_pbg_Dev[Unit].BaseAddr = 0;
err = R_PBG_SYS_HardwareDeInit(Unit);
}
else
{
/* nothing to do at this point of time */
err = R_PBG_ERR_NOTLOCKED;
}
return err;
}
#endif
#if LOC_PBG_UNUSED_FUNC
/*******************************************************************************
Function: R_PBG_SetErrorCallback
*/
void R_PBG_SetErrorCallback(void(*ErrorCallback )(uint32_t Unit, uint32_t Error))
{
loc_ErrorHandler = ErrorCallback;
}
#endif