firmware  v0.1.2
Chromation Spectrometer Dev-Kit
Usb.h
Go to the documentation of this file.
1 
42 #ifndef _USB_H
43 #define _USB_H
44 #include <stdint.h>
45 #include <stdbool.h>
46 #include "ReadWriteBits.h"
47 #ifdef USE_FAKES
48 #include "Usb_faked.h" // declare fakes
49 #endif
50 //---Hardware types: register addresses, pin numbers, bit numbers---
51 typedef uint8_t volatile * const usb_reg; // i/o reg address
52 typedef uint8_t const usb_pin; // bit index into i/o reg for an i/o pin
53 
54 // Register address, pin number, and bit definitions depend on compiler:
55  // "gcc" uses test/HardwareFake.h
56  // "avr-gcc" uses src/Lis-Hardware.h
57 // ---Registers---
58 // control lines
59 extern usb_reg FtCtrl_port;
60 extern usb_reg FtCtrl_pin;
61 // bi-directional data lines (driven by both master and slave)
62 extern usb_reg FtData_pin;
63 extern usb_reg FtData_ddr;
64 extern usb_reg FtData_port;
65 // ---Pins---
66 // control lines
67 extern usb_pin FtMiso;
68 extern usb_pin FtChipSelect;
69 extern usb_pin FtClock;
70 // bi-directional data lines (driven by both master and slave)
71 extern usb_pin FtMiosio0;
72 // FtMiosio0 is used on its own for flow control.
73 // FtMiosio[0:7] are used together as a single port for data.
74 // Write access: *FtData_port
75 // Read access: *FtData_pin
76 
77 // TODO: change to variables in style of LisConfig and LisConfigs
78 enum ft_cmd {FtReadCmd=0xC6, FtWriteCmd=0x86};
79 enum ft_databus_pin_direction {FtIn=0x00, FtOut=0xFF};
80 enum ft_status {FtError=0, FtOk=1};
81 enum ft_clock_edge {FtSample=0, FtDrive=1};
82 
83 // ---Private---
84 inline void _FtClockDatabus(uint8_t direction)
85 { // TODO: check compiler eliminates the conditional
90  direction ?
91  SetBit(FtCtrl_port, FtClock) : // FtDrive==direction
92  ClearBit(FtCtrl_port, FtClock); // FtSample==direction
93 }
94 inline void _FtReadDatabus(uint8_t * pbyte)
95 {
99  *pbyte = *FtData_pin;
100 }
101 inline void _FtWriteDatabus(uint8_t byte)
102 {
106  *FtData_port = byte;
107 }
108 inline void _FtDatabusPinDirection(uint8_t pin_direction)
109 {
114  *FtData_ddr = pin_direction;
115 }
116 
117 // ---Ft API used internally by Usb---
118 inline void FtSelectFT221X(void)
119 {
123  ClearBit(FtCtrl_port, FtChipSelect);
124 }
125 inline void FtUnselectFT221X(void)
126 {
130  SetBit(FtCtrl_port, FtChipSelect);
131 }
132 
133 #ifdef USE_FAKES
134 #define _FtClockDatabus _FtClockDatabus_fake
135 #endif
136 inline void FtBusTurnaround(void)
137 {
141  // Clock the FtStatus onto FtMiso
142  _FtClockDatabus(FtDrive); // FtClock rising edge
143  _FtClockDatabus(FtSample); // FtClock falling edge
144 }
145 #ifdef USE_FAKES
146 #undef _FtClockDatabus
147 #endif
148 
149 inline bool FtIsOk(void)
150 {
188  return BitIsClear(FtCtrl_pin, FtMiso);
189 }
190 
191 #ifdef USE_FAKES
192 #define _FtClockDatabus _FtClockDatabus_fake
193 #define _FtReadDatabus _FtReadDatabus_fake
194 #define _FtDatabusPinDirection _FtDatabusPinDirection_fake
195 #define _FtWriteDatabus _FtWriteDatabus_fake
196 #endif
197 inline void FtRead(uint8_t * pbyte)
198 {
203  // Clock a byte out of the FT221X
204  _FtClockDatabus(FtDrive); // FtClock rising edge
205  _FtClockDatabus(FtSample); // FtClock falling edge
206  // Read the byte into pbyte
207  _FtReadDatabus(pbyte);
208 }
209 inline void FtWrite(uint8_t byte)
210 {
218  // Set databus for driving data
219  _FtClockDatabus(FtDrive); // FtClock rising edge
220  // Take control of databus
221  _FtDatabusPinDirection(FtOut);
222  // Write the byte
223  _FtWriteDatabus(byte);
224  // Clock the byte into the FT221X
225  _FtClockDatabus(FtSample); // FtClock falling edge
226  // Relinquish databus to FT221X
228 }
229 #ifdef USE_FAKES
230 #undef _FtClockDatabus
231 #undef _FtReadDatabus
232 #undef _FtDatabusPinDirection
233 #undef _FtWriteDatabus
234 #endif
235 
236 // ---API---
237 inline bool UsbRxbufferIsEmpty(void)
238 {
243  return BitIsSet(FtCtrl_pin, FtMiso);
244 }
245 inline bool UsbTxbufferIsFull(void)
246 {
251  return BitIsSet(FtData_pin, FtMiosio0);
252 }
253 
254 #ifdef USE_FAKES
255 #define FtSelectFT221X FtSelectFT221X_fake
256 #define FtBusTurnaround FtBusTurnaround_fake
257 #define FtIsOk FtIsOk_fake
258 #define FtRead FtRead_fake
259 #define FtWrite FtWrite_fake
260 #define FtUnselectFT221X FtUnselectFT221X_fake
261 #endif
262 inline uint8_t UsbReadByte(uint8_t * pbyte)
263 {
269  uint8_t status = FtError;
270  FtSelectFT221X(); // activates FT1248 interface
271  FtWrite(FtReadCmd); // Clock FtReadCmd into FT221X
272  FtBusTurnaround(); // Relinquish bus to FT221X
273  if (FtIsOk())
274  {
275  status = FtOk; // Buffer is not empty, OK to read a byte
276  FtRead(pbyte); // Clock byte out of FT221X UsbRx buffer
277  }
278  FtUnselectFT221X(); // deactivates FT1248 interface
279  return status; // up to caller to check status
280 }
281 inline uint8_t UsbWriteByte(uint8_t byte)
282 {
283  uint8_t status = FtError;
284  FtSelectFT221X(); // activates FT1248 interface
285  FtWrite(FtWriteCmd); // Clock FtWriteCmd into FT221X
286  FtBusTurnaround(); // Relinquish bus to FT221X
287  if (FtIsOk())
288  {
289  status = FtOk; // Buffer is not full, OK to write a byte
290  FtWrite(byte); // Clock byte into FT221X UsbTx buffer
291  }
292  FtUnselectFT221X(); // deactivates FT1248 interface
293  return status; // up to caller to check status
294 }
295 #ifdef USE_FAKES
296 #undef FtSelectFT221X
297 #undef FtBusTurnaround
298 #undef FtIsOk
299 #undef FtRead
300 #undef FtWrite
301 #undef FtUnselectFT221X
302 #endif
303 
304 #endif // _USB_H
bool FtIsOk(void)
Definition: Usb.h:149
void _FtClockDatabus(uint8_t direction)
Definition: Usb.h:84
bool UsbRxbufferIsEmpty(void)
Definition: Usb.h:237
bool UsbTxbufferIsFull(void)
Definition: Usb.h:245
void FtSelectFT221X(void)
Definition: Usb.h:118
uint8_t UsbReadByte(uint8_t *pbyte)
Definition: Usb.h:262
void _FtWriteDatabus(uint8_t byte)
Definition: Usb.h:101
void FtUnselectFT221X(void)
Definition: Usb.h:125
void _FtReadDatabus(uint8_t *pbyte)
Definition: Usb.h:94
void FtWrite(uint8_t byte)
Definition: Usb.h:209
void FtRead(uint8_t *pbyte)
Definition: Usb.h:197
void FtBusTurnaround(void)
Definition: Usb.h:136
void _FtDatabusPinDirection(uint8_t pin_direction)
Definition: Usb.h:108