forked from fschaefer/bas-for-little-wire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
littleWire.h
239 lines (201 loc) · 10.8 KB
/
littleWire.h
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
#ifndef LITTLEWIRE_H
#define LITTLEWIRE_H
/*
http://kehribar.me/projects/Little-Wire/
Copyright (C) <2011> ihsan Kehribar <[email protected]>
Modified by Omer Kilic <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/********************************************************************************
* Header files
********************************************************************************/
#if defined(LINUX)
#include <usb.h> // this is libusb, see http://libusb.sourceforge.net/
#else
#include <lusb0_usb.h> // this is libusb, see http://libusb.sourceforge.net/
#endif
#include "opendevice.h" // common code moved to separate module
/*******************************************************************************/
/********************************************************************************
* USB details
********************************************************************************/
#define VENDOR_ID 0x1781
#define PRODUCT_ID 0x0c9f
#define USB_TIMEOUT 5000
#define RX_BUFFER_SIZE 64
/*******************************************************************************/
/********************************************************************************
* Useful constants
********************************************************************************/
#define INPUT 1
#define OUTPUT 0
#define LOW 0
#define HIGH 1
#define AUTO_CS 1
#define MANUAL_CS 0
/*******************************************************************************/
/********************************************************************************
* Pin definitions
********************************************************************************/
// General Purpose Pins
#define PIN1 1
#define PIN2 2
#define PIN3 5
#define PIN4 0
// ADC Channels
#define ADC_PIN3 0
#define ADC_PIN2 1
#define ADC_TEMP_SENS 2
// PWM Pins
#define PWM1 PIN4
#define PWM2 PIN1
// Aliases
#define ADC0 ADC_PIN3
#define ADC1 ADC_PIN2
#define ADC2 ADC_TEMP_SENS
#define PWMA PWM1
#define PWMB PWM2
// 'AVR ISP' Pins
#define SCK_PIN PIN2
#define MISO_PIN PIN1
#define MOSI_PIN PIN4
#define RESET_PIN PIN3
/*******************************************************************************/
// Typedefs
typedef usb_dev_handle littleWire;
// Function Prototypes
/********************************************************************************
* Try to connect to the device
* Returns: lwHandle for success, 0 for fail
********************************************************************************/
littleWire* littleWire_connect();
/*******************************************************************************/
/********************************************************************************
* Returns the firmware version
* Format: 0xXY => X: Primary version Y: Minor version
********************************************************************************/
unsigned char readFirmwareVersion(littleWire* lwHandle);
/*******************************************************************************/
/********************************************************************************
* Set a GPIO pin High/Low
* pin: Pin number
* state: 1 for High, 0 for Low
********************************************************************************/
void digitalWrite(littleWire* lwHandle, unsigned char pin, unsigned char state);
/*******************************************************************************/
/********************************************************************************
* Set a GPIO pin input/output
* pin: Pin number
* mode: 1 for input, 0 for output
********************************************************************************/
void pinMode(littleWire* lwHandle, unsigned char pin, unsigned char mode);
/*******************************************************************************/
/********************************************************************************
* Read a state of a GPIO pin
* pin: Pin number
* Returns: 1 for HIGH, 0 for LOW
********************************************************************************/
unsigned char digitalRead(littleWire* lwHandle, unsigned char pin);
/*******************************************************************************/
/********************************************************************************
* Read analog voltage from a spesific channel
* channel: 0 for pin3, 1 for pin2, 2 for internal Temperature sensor
* Returns: Analog voltage in 10bit resoultion
********************************************************************************/
unsigned int analogRead(littleWire* lwHandle, unsigned char channel);
/*******************************************************************************/
/********************************************************************************
* Initialize the Pwm module on the device
********************************************************************************/
void pwm_init(littleWire* lwHandle);
/*******************************************************************************/
/********************************************************************************
* Stop the PWM module on the device
********************************************************************************/
void pwm_stop(littleWire* lwHandle);
/*******************************************************************************/
/********************************************************************************
* Update the compare values of Pwm outputs
* channelA: Compare value of Channel A
* channelB: Compare value of Channel B
/*******************************************************************************/
void pwm_updateCompare(littleWire* lwHandle, unsigned char channelA, unsigned char channelB);
/*******************************************************************************/
/********************************************************************************
* Change the Pwm prescaler. Default: 1024
* value: 1024/256/64/8/1
********************************************************************************/
void pwm_updatePrescaler(littleWire* lwHandle, unsigned int value);
/*******************************************************************************/
/********************************************************************************
* Initialize SPI module
********************************************************************************/
void spi_init(littleWire* lwHandle);
/*******************************************************************************/
/********************************************************************************
* Send one byte SPI message. Chip select is manual.
* message: Message to send
* Returns: Received SPI message
********************************************************************************/
unsigned char spi_sendMessage(littleWire* lwHandle, unsigned char message);
/*******************************************************************************/
/********************************************************************************
* Send multiple SPI messages. Chip select is manual/automatic.
* sendBuffer: Message array to send
* inputBuffer: Returned answer message
* length: Message length - maximum 4
* mode: 1 for auto chip select , 0 for manual
********************************************************************************/
void spi_sendMessage_multiple(littleWire* lwHandle, unsigned char * sendBuffer, unsigned char * inputBuffer, unsigned char length ,unsigned char mode);
/*******************************************************************************/
/********************************************************************************
* Update SPI signal delay amount. Tune if neccessary to fit your requirements.
* duration: Delay amount.
********************************************************************************/
void spi_updateDelay(littleWire* lwHandle, unsigned int duration);
/*******************************************************************************/
/********************************************************************************
* Initialize i2c module on Little-Wire
********************************************************************************/
void i2c_init(littleWire* lwHandle);
/*******************************************************************************/
/********************************************************************************
* Start the i2c tranmission
* address: Slave device address
********************************************************************************/
void i2c_beginTransmission(littleWire* lwHandle, unsigned char address);
/*******************************************************************************/
/********************************************************************************
* Add new byte to the i2c send buffer
* message: A byte to send.
********************************************************************************/
void i2c_send(littleWire* lwHandle,unsigned char message);
/*******************************************************************************/
/********************************************************************************
* Send the whole message buffer to the slave at once and end the tranmssion.
********************************************************************************/
void i2c_endTransmission(littleWire* lwHandle);
/*******************************************************************************/
/********************************************************************************
* Request a reply / message from a slave device.
* address: Slave address
* numBytes: Number of bytes the slave will send.
* responseBuffer: Array pointer which will hold the response from the slave
********************************************************************************/
void i2c_requestFrom(littleWire* lwHandle,unsigned char address,unsigned char numBytes,unsigned char * responseBuffer);
/*******************************************************************************/
#endif