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_tempsensor_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_tempsensor_example.c
35
*
36
* This file contains an interrupt based design example which uses the Xilinx
37
* IIC device and driver to exercise the temperature sensor on the ML300 board.
38
* This example only performs read operations (receive) from the IIC temperature
39
* sensor of the platform.
40
*
41
* The XIic_MasterRecv() API is used to receive the data.
42
*
43
* This example assumes that there is an interrupt controller in the hardware
44
* system and the IIC device is connected to the interrupt controller.
45
*
46
* @note
47
*
48
* 7-bit addressing is used to access the tempsensor.
49
*
50
* None
51
*
52
* <pre>
53
* MODIFICATION HISTORY:
54
*
55
* Ver   Who  Date        Changes
56
* ----- ---- -------- -----------------------------------------------
57
* 1.00a jhl  09/10/03 Created
58
* 1.00a sv   05/09/05 Minor changes to comply to Doxygen and coding guidelines
59
* </pre>
60
*
61
*****************************************************************************/
62
 
63
/***************************** Include Files ********************************/
64
 
65
#include "xparameters.h"
66
#include "xiic.h"
67
#include "xintc.h"
68
#include "xil_exception.h"
69
 
70
 
71
/************************** Constant Definitions *****************************/
72
 
73
/*
74
 * The following constants map to the XPAR parameters created in the
75
 * xparameters.h file. They are defined here such that a user can easily
76
 * change all the needed parameters in one place.
77
 */
78
#define IIC_DEVICE_ID           XPAR_IIC_0_DEVICE_ID
79
#define INTC_DEVICE_ID          XPAR_INTC_0_DEVICE_ID
80
#define INTC_IIC_INTERRUPT_ID   XPAR_INTC_0_IIC_0_VEC_ID
81
 
82
 
83
/*
84
 * The following constant defines the address of the IIC
85
 * temperature sensor device on the IIC bus. Note that since
86
 * the address is only 7 bits, this  constant is the address divided by 2.
87
 */
88
#define TEMP_SENSOR_ADDRESS     0x18 /* The actual address is 0x30 */
89
 
90
 
91
/**************************** Type Definitions *******************************/
92
 
93
/***************** Macros (Inline Functions) Definitions *********************/
94
 
95
/************************** Function Prototypes ****************************/
96
 
97
int TempSensorExample(u16 IicDeviceId, u8 TempSensorAddress,
98
                                                  u8 *TemperaturePtr);
99
 
100
static int SetupInterruptSystem(XIic *IicPtr);
101
 
102
static void RecvHandler(void *CallbackRef, int ByteCount);
103
 
104
static void StatusHandler(void *CallbackRef, int Status);
105
 
106
 
107
/************************** Variable Definitions **************************/
108
 
109
XIic Iic;                 /* The instance of the IIC device */
110
 
111
XIntc InterruptController;  /* The instance of the Interrupt controller */
112
 
113
/*
114
 * The following structure contains fields that are used with the callbacks
115
 * (handlers) of the IIC driver. The driver asynchronously calls handlers
116
 * when abnormal events occur or when data has been sent or received. This
117
 * structure must be volatile to work when the code is optimized.
118
 */
119
volatile struct {
120
        int  EventStatus;
121
        int  RemainingRecvBytes;
122
        int EventStatusUpdated;
123
        int RecvBytesUpdated;
124
} HandlerInfo;
125
 
126
/*****************************************************************************/
127
/**
128
*
129
* The purpose of this function is to illustrate how to use the IIC driver to
130
* read the temperature.
131
*
132
* @param        None
133
*
134
* @return       XST_SUCCESS if successful, XST_FAILURE if unsuccessful
135
*
136
* @note         None
137
*
138
*******************************************************************************/
139
int main(void)
140
{
141
        int Status;
142
        u8 TemperaturePtr;
143
 
144
        /*
145
         * Call the TempSensorExample.
146
         */
147
        Status =  TempSensorExample(IIC_DEVICE_ID, TEMP_SENSOR_ADDRESS,
148
                                                        &TemperaturePtr);
149
        if (Status != XST_SUCCESS) {
150
                return XST_FAILURE;
151
        }
152
 
153
        return XST_SUCCESS;
154
 
155
}
156
 
157
/*****************************************************************************/
158
/**
159
* The function reads the temperature of the IIC temperature sensor on the
160
* IIC bus. It initializes the IIC device driver and sets it up to communicate
161
* with the temperature sensor. This function does contain a loop that polls
162
* for completion of the IIC processing such that it may not return if
163
* interrupts or the hardware are not working.
164
*
165
* @param        IicDeviceId is the XPAR_<IIC_instance>_DEVICE_ID value from
166
*               xparameters.h for the IIC Device
167
* @param        TempSensorAddress is the address of the Temperature Sensor device
168
*               on the IIC bus
169
* @param        TemperaturePtr is the data byte read from the temperature sensor
170
*
171
* @return       XST_SUCCESS to indicate success, else XST_FAILURE to indicate
172
*               a Failure.
173
*
174
* @note         None.
175
*
176
*******************************************************************************/
177
int TempSensorExample(u16 IicDeviceId, u8 TempSensorAddress, u8 *TemperaturePtr)
178
{
179
        int Status;
180
        static int Initialized = FALSE;
181
        XIic_Config *ConfigPtr; /* Pointer to configuration data */
182
 
183
        if (!Initialized) {
184
                Initialized = TRUE;
185
 
186
                /*
187
                 * Initialize the IIC driver so that it is ready to use.
188
                 */
189
                ConfigPtr = XIic_LookupConfig(IicDeviceId);
190
                if (ConfigPtr == NULL) {
191
                        return XST_FAILURE;
192
                }
193
 
194
                Status = XIic_CfgInitialize(&Iic, ConfigPtr,
195
                                                ConfigPtr->BaseAddress);
196
                if (Status != XST_SUCCESS) {
197
                        return XST_FAILURE;
198
                }
199
 
200
 
201
                /*
202
                 * Setup handler to process the asynchronous events which occur,
203
                 * the driver is only interrupt driven such that this must be
204
                 * done prior to starting the device.
205
                 */
206
                XIic_SetRecvHandler(&Iic, (void *)&HandlerInfo, RecvHandler);
207
                XIic_SetStatusHandler(&Iic, (void *)&HandlerInfo,
208
                                                StatusHandler);
209
 
210
                /*
211
                 * Connect the ISR to the interrupt and enable interrupts.
212
                 */
213
                Status = SetupInterruptSystem(&Iic);
214
                if (Status != XST_SUCCESS) {
215
                        return XST_FAILURE;
216
                }
217
 
218
                /*
219
                 * Start the IIC driver such that it is ready to send and
220
                 * receive messages on the IIC interface, set the address
221
                 * to send to which is the temperature sensor address
222
                 */
223
                XIic_Start(&Iic);
224
                XIic_SetAddress(&Iic, XII_ADDR_TO_SEND_TYPE, TempSensorAddress);
225
        }
226
 
227
        /*
228
         * Clear updated flags such that they can be polled to indicate
229
         * when the handler information has changed asynchronously and
230
         * initialize the status which will be returned to a default value
231
         */
232
        HandlerInfo.EventStatusUpdated = FALSE;
233
        HandlerInfo.RecvBytesUpdated = FALSE;
234
        Status = XST_FAILURE;
235
 
236
        /*
237
         * Attempt to receive a byte of data from the temperature sensor
238
         * on the IIC interface, ignore the return value since this example is
239
         * a single master system such that the IIC bus should not ever be busy
240
         */
241
        (void)XIic_MasterRecv(&Iic, TemperaturePtr, 1);
242
 
243
        /*
244
         * The message is being received from the temperature sensor,
245
         * wait for it to complete by polling the information that is
246
         * updated asynchronously by interrupt processing
247
         */
248
        while(1) {
249
                if(HandlerInfo.RecvBytesUpdated == TRUE) {
250
                        /*
251
                         * The device information has been updated for receive
252
                         * processing,if all bytes received (1), indicate
253
                         * success
254
                         */
255
                        if (HandlerInfo.RemainingRecvBytes == 0) {
256
                                Status = XST_SUCCESS;
257
                        }
258
                        break;
259
                }
260
 
261
                /*
262
                 * Any event status which occurs indicates there was an error,
263
                 * so return unsuccessful, for this example there should be no
264
                 * status events since there is a single master on the bus
265
                 */
266
                if (HandlerInfo.EventStatusUpdated == TRUE) {
267
                        break;
268
                }
269
        }
270
 
271
        return Status;
272
}
273
 
274
/*****************************************************************************/
275
/**
276
*
277
* This function setups the interrupt system such that interrupts can occur
278
* for IIC. This function is application specific since the actual system may
279
* or may not have an interrupt controller. The IIC device could be directly
280
* connected to a processor without an interrupt controller. The user should
281
* modify this function to fit the application.
282
*
283
* @param        IicPtr contains a pointer to the instance of the IIC component
284
*               which is going to be connected to the interrupt controller.
285
*
286
* @return       XST_SUCCESS if successful, otherwise XST_FAILURE.
287
*
288
* @notes        None.
289
*
290
****************************************************************************/
291
static int SetupInterruptSystem(XIic *IicPtr)
292
{
293
        int Status;
294
 
295
        /*
296
         * Initialize the interrupt controller driver so that it's ready to use,
297
         * specify the device ID that is generated in xparameters.h
298
         */
299
        Status = XIntc_Initialize(&InterruptController, INTC_DEVICE_ID);
300
        if (Status != XST_SUCCESS) {
301
                return XST_FAILURE;
302
        }
303
 
304
 
305
        /*
306
         * Connect a device driver handler that will be called when an interrupt
307
         * for the device occurs, the device driver handler performs the
308
         * specific interrupt processing for the device
309
         */
310
        Status = XIntc_Connect(&InterruptController, INTC_IIC_INTERRUPT_ID,
311
                                        XIic_InterruptHandler, IicPtr);
312
        if (Status != XST_SUCCESS) {
313
                return XST_FAILURE;
314
        }
315
 
316
        /*
317
         * Start the interrupt controller such that interrupts are recognized
318
         * and handled by the processor.
319
         */
320
        Status = XIntc_Start(&InterruptController, XIN_REAL_MODE);
321
        if (Status != XST_SUCCESS) {
322
                return XST_FAILURE;
323
        }
324
 
325
        /*
326
         * Enable the interrupts for the IIC device.
327
         */
328
        XIntc_Enable(&InterruptController, INTC_IIC_INTERRUPT_ID);
329
 
330
        /*
331
         * Initialize the exception table.
332
         */
333
        Xil_ExceptionInit();
334
 
335
        /*
336
         * Register the interrupt controller handler with the exception table.
337
         */
338
        Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
339
                                 (Xil_ExceptionHandler) XIntc_InterruptHandler,
340
                                 &InterruptController);
341
 
342
        /*
343
         * Enable non-critical exceptions.
344
         */
345
        Xil_ExceptionEnable();
346
 
347
        return XST_SUCCESS;
348
 
349
}
350
 
351
 
352
/*****************************************************************************/
353
/**
354
* This receive handler is called asynchronously from an interrupt context and
355
* and indicates that data in the specified buffer was received. The byte count
356
* should equal the byte count of the buffer if all the buffer was filled.
357
*
358
* @param        CallBackRef is a pointer to the IIC device driver instance which
359
*               the handler is being called for.
360
* @param        ByteCount indicates the number of bytes remaining to be received of
361
*               the requested byte count. A value of zero indicates all requested
362
*               bytes were received.
363
*
364
* @return       None.
365
*
366
* @notes        None.
367
*
368
****************************************************************************/
369
static void RecvHandler(void *CallbackRef, int ByteCount)
370
{
371
        HandlerInfo.RemainingRecvBytes = ByteCount;
372
        HandlerInfo.RecvBytesUpdated = TRUE;
373
}
374
 
375
/*****************************************************************************/
376
/**
377
* This status handler is called asynchronously from an interrupt context and
378
* indicates that the conditions of the IIC bus changed. This  handler should
379
* not be called for the application unless an error occurs.
380
*
381
* @param        CallBackRef is a pointer to the IIC device driver instance which the
382
*               handler is being called for.
383
* @param        Status contains the status of the IIC bus which changed.
384
*
385
* @return       None.
386
*
387
* @notes        None.
388
*
389
****************************************************************************/
390
static void StatusHandler(void *CallbackRef, int Status)
391
{
392
        HandlerInfo.EventStatus |= Status;
393
        HandlerInfo.EventStatusUpdated = TRUE;
394
}

powered by: WebSVN 2.1.0

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