OpenCores
URL https://opencores.org/ocsvn/xenie/xenie/trunk

Subversion Repositories xenie

[/] [xenie/] [trunk/] [examples/] [Eth_example/] [mb_fw/] [drivers/] [iic_v3_4/] [examples/] [xiic_slave_example.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 DFC
/******************************************************************************
2
*
3
* Copyright (C) 2006 - 2014 Xilinx, Inc.  All rights reserved.
4
*
5
* Permission is hereby granted, free of charge, to any person obtaining a copy
6
* of this software and associated documentation files (the "Software"), to deal
7
* in the Software without restriction, including without limitation the rights
8
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
* copies of the Software, and to permit persons to whom the Software is
10
* furnished to do so, subject to the following conditions:
11
*
12
* The above copyright notice and this permission notice shall be included in
13
* all copies or substantial portions of the Software.
14
*
15
* Use of the Software is limited solely to applications:
16
* (a) running on a Xilinx device, or
17
* (b) that interact with a Xilinx device through a bus or interconnect.
18
*
19
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22
* XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
* SOFTWARE.
26
*
27
* Except as contained in this notice, the name of the Xilinx shall not be used
28
* in advertising or otherwise to promote the sale, use or other dealings in
29
* this Software without prior written authorization from Xilinx.
30
*
31
******************************************************************************/
32
/*****************************************************************************/
33
/**
34
* @file xiic_slave_example.c
35
*
36
* This file consists of a Interrupt mode design example which uses the Xilinx
37
* IIC device and XIic driver to exercise the slave functionality of the IIC
38
* device.
39
*
40
* The XIic_SlaveSend() API is used to transmit the data and
41
* XIic_SlaveRecv() API is used to receive the data.
42
*
43
* The example is tested on ML300/ML310/ML403/ML501 Xilinx boards.
44
*
45
* The IIC devices that are present on the Xilinx boards donot support the Master
46
* functionality. This example has been tested with an off board external IIC
47
* Master device and the IIC device configured as a Slave.
48
*
49
* This code assumes that no Operating System is being used.
50
*
51
* @note
52
*
53
* None.
54
*
55
* <pre>
56
* MODIFICATION HISTORY:
57
*
58
* Ver   Who  Date        Changes
59
* ----- ---- -------- -----------------------------------------------
60
* 1.00a mta  03/01/06 Created.
61
* 2.00a ktn  11/17/09 Updated to use the HAL APIs and replaced call to
62
*                     XIic_Initialize API with XIic_LookupConfig and
63
*                     XIic_CfgInitialize. Some of the macros have been
64
*                     renamed in the IIC driver and some renamed macros are
65
*                     used in this example.
66
* </pre>
67
*
68
******************************************************************************/
69
 
70
/***************************** Include Files *********************************/
71
 
72
#include "xparameters.h"
73
#include "xiic.h"
74
#include "xintc.h"
75
#include "xil_exception.h"
76
 
77
/************************** Constant Definitions *****************************/
78
 
79
/*
80
 * The following constants map to the XPAR parameters created in the
81
 * xparameters.h file. They are defined here such that a user can easily
82
 * change all the needed parameters in one place.
83
 */
84
#define IIC_DEVICE_ID           XPAR_IIC_0_DEVICE_ID
85
#define INTC_DEVICE_ID          XPAR_INTC_0_DEVICE_ID
86
#define IIC_INTR_ID             XPAR_INTC_0_IIC_0_VEC_ID
87
 
88
/*
89
 * The following constant defines the address of the IIC device on the IIC bus.
90
 * Since the address is only 7 bits, this constant is the address divided by 2.
91
 */
92
#define SLAVE_ADDRESS           0x70    /* 0xE0 as an 8 bit number. */
93
 
94
#define RECEIVE_COUNT           25
95
#define SEND_COUNT              25
96
 
97
 
98
/**************************** Type Definitions *******************************/
99
 
100
/***************** Macros (Inline Functions) Definitions *********************/
101
 
102
/************************** Function Prototypes ******************************/
103
 
104
int IicSlaveExample();
105
int SlaveWriteData(u16 ByteCount);
106
int SlaveReadData(u8 *BufferPtr, u16 ByteCount);
107
static int SetupInterruptSystem(XIic * IicInstPtr);
108
static void StatusHandler(XIic *InstancePtr, int Event);
109
static void SendHandler(XIic *InstancePtr);
110
static void ReceiveHandler(XIic *InstancePtr);
111
 
112
/************************** Variable Definitions *****************************/
113
 
114
XIic IicInstance;               /* The instance of the IIC device. */
115
XIntc InterruptController;      /* The instance of the Interrupt Controller */
116
 
117
 
118
u8 WriteBuffer[SEND_COUNT];     /* Write buffer for writing a page. */
119
u8 ReadBuffer[RECEIVE_COUNT];   /* Read buffer for reading a page. */
120
 
121
volatile u8 TransmitComplete;
122
volatile u8 ReceiveComplete;
123
 
124
volatile u8 SlaveRead;
125
volatile u8 SlaveWrite;
126
 
127
/************************** Function Definitions *****************************/
128
 
129
/*****************************************************************************/
130
/**
131
* Main function to call the IIC Slave example.
132
*
133
* @param        None.
134
*
135
* @return       XST_SUCCESS if successful else XST_FAILURE.
136
*
137
* @note         None.
138
*
139
******************************************************************************/
140
int main(void)
141
{
142
        int Status;
143
 
144
        /*
145
         * Run the IIC Slave example.
146
         */
147
        Status = IicSlaveExample();
148
        if (Status != XST_SUCCESS) {
149
                return XST_FAILURE;
150
        }
151
 
152
        return XST_SUCCESS;
153
}
154
 
155
/*****************************************************************************/
156
/**
157
* This function writes and reads the data as a slave. The IIC master on the bus
158
* initiates the transfers.
159
*
160
* @param        None.
161
*
162
* @return       XST_SUCCESS if successful else XST_FAILURE.
163
*
164
* @note         None.
165
*
166
******************************************************************************/
167
int IicSlaveExample()
168
{
169
        int Status;
170
        u8 Index;
171
        XIic_Config *ConfigPtr; /* Pointer to configuration data */
172
 
173
 
174
        /*
175
         * Initialize the IIC driver so that it is ready to use.
176
         */
177
        ConfigPtr = XIic_LookupConfig(IIC_DEVICE_ID);
178
        if (ConfigPtr == NULL) {
179
                return XST_FAILURE;
180
        }
181
 
182
        Status = XIic_CfgInitialize(&IicInstance, ConfigPtr,
183
                                        ConfigPtr->BaseAddress);
184
        if (Status != XST_SUCCESS) {
185
                return XST_FAILURE;
186
        }
187
 
188
        /*
189
         * Setup the Interrupt System.
190
         */
191
        Status = SetupInterruptSystem(&IicInstance);
192
        if (Status != XST_SUCCESS) {
193
                return XST_FAILURE;
194
        }
195
 
196
        /*
197
         * Include the Slave functions.
198
         */
199
        XIic_SlaveInclude();
200
 
201
        /*
202
         * Set the Transmit, Receive and Status Handlers.
203
         */
204
        XIic_SetStatusHandler(&IicInstance, &IicInstance,
205
                                  (XIic_StatusHandler) StatusHandler);
206
        XIic_SetSendHandler(&IicInstance, &IicInstance,
207
                                (XIic_Handler) SendHandler);
208
        XIic_SetRecvHandler(&IicInstance, &IicInstance,
209
                                (XIic_Handler) ReceiveHandler);
210
 
211
        /*
212
         * Set the Address as a RESPOND type.
213
         */
214
        Status = XIic_SetAddress(&IicInstance, XII_ADDR_TO_RESPOND_TYPE,
215
                                 SLAVE_ADDRESS);
216
        if (Status != XST_SUCCESS) {
217
                return XST_FAILURE;
218
        }
219
 
220
        /*
221
         * The IIC Master on this bus should initiate the transfer
222
         * and write data to the slave at this instance.
223
         */
224
        SlaveReadData(ReadBuffer, RECEIVE_COUNT);
225
 
226
        for (Index = 0; Index < SEND_COUNT; Index++) {
227
                WriteBuffer[Index] = Index;
228
        }
229
 
230
        /*
231
         * The IIC Master on this bus should initiate the transfer
232
         * and read data from the slave.
233
         */
234
        SlaveWriteData(SEND_COUNT);
235
 
236
        return XST_SUCCESS;
237
}
238
 
239
/*****************************************************************************/
240
/**
241
* This function reads a buffer of bytes  when the IIC Master on the bus writes
242
* data to the slave device.
243
*
244
* @param        BufferPtr contains the address of the data buffer to be filled.
245
* @param        ByteCount contains the number of bytes in the buffer to be read.
246
*
247
* @return       XST_SUCCESS if successful else XST_FAILURE.
248
*
249
* @note         None
250
*
251
******************************************************************************/
252
int SlaveReadData(u8 *BufferPtr, u16 ByteCount)
253
{
254
        int Status;
255
 
256
        /*
257
         * Set the defaults.
258
         */
259
        ReceiveComplete = 1;
260
 
261
        /*
262
         * Start the IIC device.
263
         */
264
        Status = XIic_Start(&IicInstance);
265
        if (Status != XST_SUCCESS) {
266
                return XST_FAILURE;
267
        }
268
 
269
        /*
270
         * Set the Global Interrupt Enable.
271
         */
272
        XIic_IntrGlobalEnable(IicInstance.BaseAddress);
273
 
274
        /*
275
         * Wait for AAS interrupt and completion of data reception.
276
         */
277
        while ((ReceiveComplete) || (XIic_IsIicBusy(&IicInstance) == TRUE)) {
278
                if (SlaveRead) {
279
                        XIic_SlaveRecv(&IicInstance, ReadBuffer, RECEIVE_COUNT);
280
                        SlaveRead = 0;
281
                }
282
        }
283
 
284
        /*
285
         * Disable the Global Interrupt Enable.
286
         */
287
        XIic_IntrGlobalDisable(IicInstance.BaseAddress);
288
 
289
        /*
290
         * Stop the IIC device.
291
         */
292
        Status = XIic_Stop(&IicInstance);
293
        if (Status != XST_SUCCESS) {
294
                return XST_FAILURE;
295
        }
296
 
297
        return XST_SUCCESS;
298
}
299
 
300
/*****************************************************************************/
301
/**
302
* This function writes a buffer of bytes to the IIC bus when the IIC master
303
* initiates a read operation.
304
*
305
* @param        ByteCount contains the number of bytes in the buffer to be
306
*               written.
307
*
308
* @return       XST_SUCCESS if successful else XST_FAILURE.
309
*
310
* @note         None.
311
*
312
******************************************************************************/
313
int SlaveWriteData(u16 ByteCount)
314
{
315
        int Status;
316
 
317
        /*
318
         * Set the defaults.
319
         */
320
        TransmitComplete = 1;
321
 
322
        /*
323
         * Start the IIC device.
324
         */
325
        Status = XIic_Start(&IicInstance);
326
        if (Status != XST_SUCCESS) {
327
                return XST_FAILURE;
328
        }
329
 
330
        /*
331
         * Set the Global Interrupt Enable.
332
         */
333
        XIic_IntrGlobalEnable(IicInstance.BaseAddress);
334
 
335
        /*
336
         * Wait for AAS interrupt and transmission to complete.
337
         */
338
        while ((TransmitComplete) || (XIic_IsIicBusy(&IicInstance) == TRUE)) {
339
                if (SlaveWrite) {
340
                        XIic_SlaveSend(&IicInstance, WriteBuffer, SEND_COUNT);
341
                        SlaveWrite = 0;
342
                }
343
        }
344
 
345
        /*
346
         * Disable the Global Interrupt Enable bit.
347
         */
348
        XIic_IntrGlobalDisable(IicInstance.BaseAddress);
349
 
350
        /*
351
         * Stop the IIC device.
352
         */
353
        Status = XIic_Stop(&IicInstance);
354
        if (Status != XST_SUCCESS) {
355
                return XST_FAILURE;
356
        }
357
 
358
        return XST_SUCCESS;
359
}
360
 
361
/****************************************************************************/
362
/**
363
* This Status handler is called asynchronously from an interrupt context and
364
* indicates the events that have occurred.
365
*
366
* @param        InstancePtr is not used, but contains a pointer to the IIC
367
*               device driver instance which the handler is being called for.
368
* @param        Event indicates whether it is a request for a write or read.
369
*
370
* @return       None.
371
*
372
* @note         None.
373
*
374
****************************************************************************/
375
static void StatusHandler(XIic *InstancePtr, int Event)
376
{
377
        /*
378
         * Check whether the Event is to write or read the data from the slave.
379
         */
380
        if (Event == XII_MASTER_WRITE_EVENT) {
381
                /*
382
                 * Its a Write request from Master.
383
                 */
384
                SlaveRead = 1;
385
        } else {
386
                /*
387
                 * Its a Read request from the master.
388
                 */
389
                SlaveWrite = 1;
390
        }
391
}
392
 
393
/****************************************************************************/
394
/**
395
* This Send handler is called asynchronously from an interrupt
396
* context and indicates that data in the specified buffer has been sent.
397
*
398
* @param        InstancePtr is a pointer to the IIC driver instance for which
399
*               the handler is being called for.
400
*
401
* @return       None.
402
*
403
* @note         None.
404
*
405
****************************************************************************/
406
static void SendHandler(XIic *InstancePtr)
407
{
408
        TransmitComplete = 0;
409
}
410
 
411
/****************************************************************************/
412
/**
413
* This Receive handler is called asynchronously from an interrupt
414
* context and indicates that data in the specified buffer has been Received.
415
*
416
* @param        InstancePtr is a pointer to the IIC driver instance for which
417
*               the handler is being called for.
418
*
419
* @return       None.
420
*
421
* @note         None.
422
*
423
****************************************************************************/
424
static void ReceiveHandler(XIic *InstancePtr)
425
{
426
        ReceiveComplete = 0;
427
}
428
 
429
/****************************************************************************/
430
/**
431
* This function setups the interrupt system so interrupts can occur for the
432
* IIC. The function is application-specific since the actual system may or
433
* may not have an interrupt controller. The IIC device could be directly
434
* connected to a processor without an interrupt controller. The user should
435
* modify this function to fit the application.
436
*
437
* @param        IicInstPtr contains a pointer to the instance of the IIC  which
438
*               is going to be connected to the interrupt controller.
439
*
440
* @return       XST_SUCCESS if successful else XST_FAILURE.
441
*
442
* @note         None.
443
*
444
****************************************************************************/
445
static int SetupInterruptSystem(XIic * IicInstPtr)
446
{
447
        int Status;
448
 
449
        if (InterruptController.IsStarted == XIL_COMPONENT_IS_STARTED) {
450
                return XST_SUCCESS;
451
        }
452
 
453
        /*
454
         * Initialize the interrupt controller driver so that it's ready to use.
455
         */
456
        Status = XIntc_Initialize(&InterruptController, INTC_DEVICE_ID);
457
        if (Status != XST_SUCCESS) {
458
                return XST_FAILURE;
459
        }
460
 
461
        /*
462
         * Connect the device driver handler that will be called when an
463
         * interrupt for the device occurs, the handler defined above
464
         * performs the specific interrupt processing for the device.
465
         */
466
        Status = XIntc_Connect(&InterruptController, IIC_INTR_ID,
467
                                   (XInterruptHandler) XIic_InterruptHandler,
468
                                   IicInstPtr);
469
        if (Status != XST_SUCCESS) {
470
                return XST_FAILURE;
471
        }
472
 
473
        /*
474
         * Start the interrupt controller so interrupts are enabled for all
475
         * devices that cause interrupts.
476
         */
477
        Status = XIntc_Start(&InterruptController, XIN_REAL_MODE);
478
        if (Status != XST_SUCCESS) {
479
                return XST_FAILURE;
480
        }
481
 
482
        /*
483
         * Enable the interrupts for the IIC device.
484
         */
485
        XIntc_Enable(&InterruptController, IIC_INTR_ID);
486
 
487
        /*
488
         * Initialize the exception table.
489
         */
490
        Xil_ExceptionInit();
491
 
492
        /*
493
         * Register the interrupt controller handler with the exception table.
494
         */
495
        Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
496
                                 (Xil_ExceptionHandler) XIntc_InterruptHandler,
497
                                 &InterruptController);
498
 
499
        /*
500
         * Enable non-critical exceptions.
501
         */
502
        Xil_ExceptionEnable();
503
 
504
 
505
        return XST_SUCCESS;
506
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.