Firmware
px4_i2c.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2015 Mark Charlebois. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
40 #pragma once
41 
42 #define PX4_I2C_M_READ 0x0001 /* read data, from slave to master */
43 
44 #if defined (__PX4_NUTTX)
45 __BEGIN_DECLS
46 
47 /*
48  * Building for NuttX
49  */
50 #include <px4_config.h>
51 #include <sys/ioctl.h>
52 #include <nuttx/arch.h>
53 #include <nuttx/wqueue.h>
54 #include <nuttx/clock.h>
55 #include <nuttx/i2c/i2c_master.h>
56 #include <nuttx/irq.h>
57 #include <nuttx/wqueue.h>
58 #include <chip.h>
59 #include <arch/board/board.h>
60 #include <arch/chip/chip.h>
61 #include "up_internal.h"
62 #include "up_arch.h"
63 
64 #define px4_i2c_msg_t i2c_msg_s
65 
66 typedef struct i2c_master_s px4_i2c_dev_t;
67 __END_DECLS
68 
69 #elif defined(__PX4_POSIX)
70 #include <stdint.h>
71 
72 #define I2C_M_READ 0x0001 /* read data, from slave to master */
73 #define I2C_M_TEN 0x0002 /* ten bit address */
74 #define I2C_M_NORESTART 0x0080 /* message should not begin with (re-)start of transfer */
75 
76 // NOTE - This is a copy of the NuttX i2c_msg_s structure
77 
78 typedef struct {
79  uint32_t frequency; /* I2C frequency */
80  uint16_t addr; /* Slave address (7- or 10-bit) */
81  uint16_t flags; /* See I2C_M_* definitions */
82  uint8_t *buffer; /* Buffer to be transferred */
83  ssize_t length; /* Length of the buffer in bytes */
84 } px4_i2c_msg_t;
85 
86 // NOTE - This is a copy of the NuttX i2c_ops_s structure
87 typedef struct {
88  const struct px4_i2c_ops_t *ops; /* I2C vtable */
89 } px4_i2c_dev_t;
90 
91 //#define SPI_SELECT(d,id,s) ((d)->ops->select(d,id,s))
92 #define SPI_SELECT(d,id,s)
93 
94 // FIXME - Stub implementation
95 // Original version commented out
96 //#define I2C_TRANSFER(d,m,c) ((d)->ops->transfer(d,m,c))
97 inline int I2C_TRANSFER(px4_i2c_dev_t *dev, px4_i2c_msg_t *msg, int count);
98 inline int I2C_TRANSFER(px4_i2c_dev_t *dev, px4_i2c_msg_t *msg, int count) { return 0; }
99 
100 #ifdef __PX4_QURT
101 
102 struct i2c_msg {
103  uint16_t addr; /* Slave address */
104  uint16_t flags; /* See I2C_M_* definitions */
105  uint8_t *buf;
106  int len;
107 };
108 
109 #define I2C_RDWR 0x0FFF
110 
111 struct i2c_rdwr_ioctl_data {
112  struct i2c_msg *msgs; /* pointers to i2c_msgs */
113  uint32_t nmsgs; /* number of i2c_msgs */
114 };
115 
116 // FIXME - The functions are not implemented on QuRT/DSPAL
117 int ioctl(int fd, int flags, unsigned long data);
118 int write(int fd, const char *buffer, int buflen);
119 #endif
120 #else
121 #error "No target platform defined"
122 #endif
Configuration flags used in code.
Definition: video_device.h:50