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_tenbitaddr_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_tenbitaddr_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 10-bit Address functionality of the
38
* IIC device.
39
*
40
* The XIic_MasterSend() API is used to transmit the data and
41
* XIic_MasterRecv() 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 10-bit
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. Made minor modifications as
64
*                     per coding guidelines.
65
* </pre>
66
*
67
******************************************************************************/
68
 
69
/***************************** Include Files *********************************/
70
 
71
#include "xparameters.h"
72
#include "xiic.h"
73
#include "xintc.h"
74
#include "xil_exception.h"
75
 
76
/************************** Constant Definitions *****************************/
77
 
78
/*
79
 * The following constants map to the XPAR parameters created in the
80
 * xparameters.h file. They are defined here such that a user can easily
81
 * change all the needed parameters in one place.
82
 */
83
#define IIC_DEVICE_ID           XPAR_IIC_0_DEVICE_ID
84
#define INTC_DEVICE_ID          XPAR_INTC_0_DEVICE_ID
85
#define IIC_INTR_ID             XPAR_INTC_0_IIC_0_VEC_ID
86
 
87
/*
88
 * The following constant defines the address of the IIC device on the IIC bus.
89
 * Since the address is 10 bits, this constant is the address divided by 2.
90
 */
91
#define SLAVE_ADDRESS           0x270   /* 0x4E0 as an 10 bit number. */
92
#define RECEIVE_COUNT           16
93
#define SEND_COUNT              16
94
 
95
 
96
/**************************** Type Definitions *******************************/
97
 
98
/***************** Macros (Inline Functions) Definitions *********************/
99
 
100
/************************** Function Prototypes ******************************/
101
 
102
int IicTenBitAddrExample();
103
int TenBitAddrWriteData(u16 ByteCount);
104
int TenBitAddrReadData(u8 *BufferPtr, u16 ByteCount);
105
static int SetupInterruptSystem(XIic *IicInstPtr);
106
static void StatusHandler(XIic *InstancePtr, int Event);
107
static void SendHandler(XIic *InstancePtr);
108
static void ReceiveHandler(XIic *InstancePtr);
109
 
110
/************************** Variable Definitions *****************************/
111
 
112
XIic IicInstance;               /* The instance of the IIC device. */
113
XIntc InterruptController;      /* The instance of the Interrupt Controller */
114
 
115
 
116
u8 WriteBuffer[SEND_COUNT];     /* Write buffer for writing a page. */
117
u8 ReadBuffer[RECEIVE_COUNT];   /* Read buffer for reading a page. */
118
 
119
volatile u8 TransmitComplete;
120
volatile u8 ReceiveComplete;
121
 
122
volatile u8 SlaveRead;
123
volatile u8 SlaveWrite;
124
 
125
/************************** Function Definitions *****************************/
126
 
127
/*****************************************************************************/
128
/**
129
*
130
* Main function to call the IIC Slave example.
131
*
132
* @param        None.
133
*
134
* @return       XST_SUCCESS if successful else XST_FAILURE.
135
*
136
* @note         None.
137
*
138
******************************************************************************/
139
int main(void)
140
{
141
        int Status;
142
 
143
        /*
144
         * Run the IIC Slave example for 10 bit addressing.
145
         */
146
        Status = IicTenBitAddrExample();
147
        if (Status != XST_SUCCESS) {
148
                return XST_FAILURE;
149
        }
150
 
151
        return XST_SUCCESS;
152
}
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 IicTenBitAddrExample()
168
{
169
        int Status;
170
        u8 Index;
171
        XIic_Config *ConfigPtr; /* Pointer to configuration data */
172
 
173
        /*
174
         * Initialize the IIC driver so that it is ready to use.
175
         */
176
        ConfigPtr = XIic_LookupConfig(IIC_DEVICE_ID);
177
        if (ConfigPtr == NULL) {
178
                return XST_FAILURE;
179
        }
180
 
181
        Status = XIic_CfgInitialize(&IicInstance, ConfigPtr,
182
                                        ConfigPtr->BaseAddress);
183
        if (Status != XST_SUCCESS) {
184
                return XST_FAILURE;
185
        }
186
 
187
        /*
188
         * Set the options to enable 10-bit addressing.
189
         */
190
        XIic_SetOptions(&IicInstance, XII_SEND_10_BIT_OPTION);
191
 
192
        /*
193
         * Setup the Interrupt System.
194
         */
195
        Status = SetupInterruptSystem(&IicInstance);
196
        if (Status != XST_SUCCESS) {
197
                return XST_FAILURE;
198
        }
199
 
200
        /*
201
         * Include the Slave functions.
202
         */
203
        XIic_SlaveInclude();
204
 
205
        /*
206
         * Set the Transmit, Receive and Status Handlers.
207
         */
208
        XIic_SetStatusHandler(&IicInstance, &IicInstance,
209
                                  (XIic_StatusHandler) StatusHandler);
210
        XIic_SetSendHandler(&IicInstance, &IicInstance,
211
                                (XIic_Handler) SendHandler);
212
        XIic_SetRecvHandler(&IicInstance, &IicInstance,
213
                                (XIic_Handler) ReceiveHandler);
214
 
215
        /*
216
         * Set the Address as a RESPOND type.
217
         */
218
        Status = XIic_SetAddress(&IicInstance, XII_ADDR_TO_RESPOND_TYPE,
219
                                 SLAVE_ADDRESS);
220
        if (Status != XST_SUCCESS) {
221
                return XST_FAILURE;
222
        }
223
 
224
        /*
225
         * The IIC Master on this bus should initiate the transfer
226
         * and write data to the slave at this instance.
227
         */
228
        TenBitAddrReadData(ReadBuffer, RECEIVE_COUNT);
229
 
230
        for (Index = 0; Index < SEND_COUNT; Index++) {
231
                WriteBuffer[Index] = Index;
232
        }
233
 
234
        /*
235
         * The IIC Master on this bus should initiate the transfer
236
         * and read data from the slave.
237
         */
238
        TenBitAddrWriteData(SEND_COUNT);
239
 
240
        return XST_SUCCESS;
241
}
242
 
243
/*****************************************************************************/
244
/**
245
* This function reads a buffer of bytes  when the IIC Master on the bus writes
246
*  data to the slave device.
247
*
248
* @param        BufferPtr contains the address of the data buffer to be filled.
249
* @param        ByteCount contains the number of bytes in the buffer to be read.
250
*
251
* @return       XST_SUCCESS if successful else XST_FAILURE.
252
*
253
* @note         None
254
*
255
******************************************************************************/
256
int TenBitAddrReadData(u8 *BufferPtr, u16 ByteCount)
257
{
258
        int Status;
259
 
260
        /*
261
         * Set the defaults.
262
         */
263
        ReceiveComplete = 1;
264
 
265
        /*
266
         * Start the IIC device.
267
         */
268
        Status = XIic_Start(&IicInstance);
269
        if (Status != XST_SUCCESS) {
270
                return XST_FAILURE;
271
        }
272
 
273
        /*
274
         * Set the Global Interrupt Enable.
275
         */
276
        XIic_IntrGlobalEnable(IicInstance.BaseAddress);
277
 
278
        /*
279
         * Wait for AAS interrupt and completion of data reception.
280
         */
281
        while ((ReceiveComplete) || (XIic_IsIicBusy(&IicInstance) == TRUE)) {
282
                if (SlaveRead) {
283
                        XIic_SlaveRecv(&IicInstance, ReadBuffer, RECEIVE_COUNT);
284
                        SlaveRead = 0;
285
                }
286
        }
287
 
288
        /*
289
         * Disable the Global Interrupt Enable.
290
         */
291
        XIic_IntrGlobalDisable(IicInstance.BaseAddress);
292
 
293
        /*
294
         * Stop the IIC device.
295
         */
296
        Status = XIic_Stop(&IicInstance);
297
        if (Status != XST_SUCCESS) {
298
                return XST_FAILURE;
299
        }
300
 
301
        return XST_SUCCESS;
302
}
303
 
304
/*****************************************************************************/
305
/**
306
* This function writes a buffer of bytes to the IIC bus when the IIC master
307
* initiates a read operation.
308
*
309
* @param        ByteCount contains the number of bytes in the buffer to be
310
*               written.
311
*
312
* @return       XST_SUCCESS if successful else XST_FAILURE.
313
*
314
* @note         None.
315
*
316
******************************************************************************/
317
int TenBitAddrWriteData(u16 ByteCount)
318
{
319
        int Status;
320
 
321
        /*
322
         * Set the defaults.
323
         */
324
        TransmitComplete = 1;
325
 
326
        /*
327
         * Start the IIC device.
328
         */
329
        Status = XIic_Start(&IicInstance);
330
        if (Status != XST_SUCCESS) {
331
                return XST_FAILURE;
332
        }
333
 
334
        /*
335
         * Set the Global Interrupt Enable.
336
         */
337
        XIic_IntrGlobalEnable(IicInstance.BaseAddress);
338
 
339
        /*
340
         * Wait for AAS interrupt and transmission to complete.
341
         */
342
        while ((TransmitComplete) || (XIic_IsIicBusy(&IicInstance) == TRUE)) {
343
                if (SlaveWrite) {
344
                        XIic_SlaveSend(&IicInstance, WriteBuffer, SEND_COUNT);
345
                        SlaveWrite = 0;
346
                }
347
        }
348
 
349
        /*
350
         * Disable the Global Interrupt Enable bit.
351
         */
352
        XIic_IntrGlobalDisable(IicInstance.BaseAddress);
353
 
354
        /*
355
         * Stop the IIC device.
356
         */
357
        Status = XIic_Stop(&IicInstance);
358
        if (Status != XST_SUCCESS) {
359
                return XST_FAILURE;
360
        }
361
 
362
        return XST_SUCCESS;
363
}
364
 
365
/****************************************************************************/
366
/**
367
* This Status handler is called asynchronously from an interrupt context and
368
* indicates the events that have occurred.
369
*
370
* @param        InstancePtr is not used, but contains a pointer to the IIC
371
*               device driver instance which the handler is being called for.
372
* @param        Event indicates whether it is a request for a write or read.
373
*
374
* @return       None.
375
*
376
* @note         None.
377
*
378
****************************************************************************/
379
static void StatusHandler(XIic *InstancePtr, int Event)
380
{
381
        /*
382
         * Check whether the Event is to write or read the data from the slave.
383
         */
384
        if (Event == XII_MASTER_WRITE_EVENT) {
385
                /*
386
                 * Its a Write request from Master.
387
                 */
388
                SlaveRead = 1;
389
        }
390
        else {
391
                /*
392
                 * Its a Read request from the master.
393
                 */
394
                SlaveWrite = 1;
395
        }
396
}
397
 
398
/****************************************************************************/
399
/**
400
* This Send handler is called asynchronously from an interrupt
401
* context and indicates that data in the specified buffer has been sent.
402
*
403
* @param        InstancePtr is a pointer to the IIC driver instance for which
404
*               the handler is being called for.
405
*
406
* @return       None.
407
*
408
* @note         None.
409
*
410
****************************************************************************/
411
static void SendHandler(XIic *InstancePtr)
412
{
413
        TransmitComplete = 0;
414
}
415
 
416
/****************************************************************************/
417
/**
418
* This Receive handler is called asynchronously from an interrupt
419
* context and indicates that data in the specified buffer has been Received.
420
*
421
* @param        InstancePtr is a pointer to the IIC driver instance for which
422
*               the handler is being called for.
423
*
424
* @return       None.
425
*
426
* @note         None.
427
*
428
****************************************************************************/
429
static void ReceiveHandler(XIic *InstancePtr)
430
{
431
        ReceiveComplete = 0;
432
}
433
 
434
/****************************************************************************/
435
/**
436
* This function setups the interrupt system so interrupts can occur for the
437
* IIC. The function is application-specific since the actual system may or
438
* may not have an interrupt controller. The IIC device could be directly
439
* connected to a processor without an interrupt controller. The user should
440
* modify this function to fit the application.
441
*
442
* @param        IicInstPtr contains a pointer to the instance of the IIC  which
443
*               is going to be connected to the interrupt controller.
444
*
445
* @return       XST_SUCCESS if successful else XST_FAILURE.
446
*
447
* @note         None.
448
*
449
****************************************************************************/
450
static int SetupInterruptSystem(XIic * IicInstPtr)
451
{
452
        int Status;
453
 
454
        if (InterruptController.IsStarted == XIL_COMPONENT_IS_STARTED) {
455
                return XST_SUCCESS;
456
        }
457
 
458
        /*
459
         * Initialize the interrupt controller driver so that it's ready to use.
460
         */
461
        Status = XIntc_Initialize(&InterruptController, INTC_DEVICE_ID);
462
        if (Status != XST_SUCCESS) {
463
                return XST_FAILURE;
464
        }
465
 
466
        /*
467
         * Connect the device driver handler that will be called when an
468
         * interrupt for the device occurs, the handler defined above performs
469
         * the specific interrupt processing for the device.
470
         */
471
        Status = XIntc_Connect(&InterruptController, IIC_INTR_ID,
472
                                   (XInterruptHandler) XIic_InterruptHandler,
473
                                   IicInstPtr);
474
        if (Status != XST_SUCCESS) {
475
                return XST_FAILURE;
476
        }
477
 
478
        /*
479
         * Start the interrupt controller so interrupts are enabled for all
480
         * devices that cause interrupts.
481
         */
482
        Status = XIntc_Start(&InterruptController, XIN_REAL_MODE);
483
        if (Status != XST_SUCCESS) {
484
                return XST_FAILURE;
485
        }
486
 
487
        /*
488
         * Enable the interrupts for the IIC device.
489
         */
490
        XIntc_Enable(&InterruptController, IIC_INTR_ID);
491
 
492
        /*
493
         * Initialize the exception table.
494
         */
495
        Xil_ExceptionInit();
496
 
497
        /*
498
         * Register the interrupt controller handler with the exception table.
499
         */
500
        Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
501
                                 (Xil_ExceptionHandler) XIntc_InterruptHandler,
502
                                 &InterruptController);
503
 
504
        /*
505
         * Enable non-critical exceptions.
506
         */
507
        Xil_ExceptionEnable();
508
 
509
        return XST_SUCCESS;
510
}

powered by: WebSVN 2.1.0

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