1 | /*
|
---|
2 | Description:
|
---|
3 | This is a example code for Sandbox Electronics' I2C/SPI to UART bridge module.
|
---|
4 | You can get one of those products on
|
---|
5 | http://sandboxelectronics.com
|
---|
6 |
|
---|
7 | Version:
|
---|
8 | V0.1
|
---|
9 |
|
---|
10 | Release Date:
|
---|
11 | 2014-02-16
|
---|
12 |
|
---|
13 | Author:
|
---|
14 | Tiequan Shao info@sandboxelectronics.com
|
---|
15 |
|
---|
16 | Lisence:
|
---|
17 | CC BY-NC-SA 3.0
|
---|
18 |
|
---|
19 | Please keep the above information when you use this code in your project.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #if defined(__AVR_ATmega328P__)
|
---|
23 |
|
---|
24 | #include "SC16IS750.h"
|
---|
25 | #include <Wire.h>
|
---|
26 |
|
---|
27 | #define WIRE Wire
|
---|
28 |
|
---|
29 | WifiData::WifiData()
|
---|
30 | {
|
---|
31 | device_address_sspin = (SC16IS750_ADDRESS_AA >> 1);
|
---|
32 | peek_flag = 0;
|
---|
33 | }
|
---|
34 |
|
---|
35 | void WifiData::begin(uint32_t baud)
|
---|
36 | {
|
---|
37 |
|
---|
38 | if ( protocol == SC16IS750_PROTOCOL_I2C) {
|
---|
39 | WIRE.begin();
|
---|
40 | }
|
---|
41 | ResetDevice();
|
---|
42 | FIFOEnable(1);
|
---|
43 | SetBaudrate(baud);
|
---|
44 | SetLine(8,0,1);
|
---|
45 | }
|
---|
46 |
|
---|
47 | int WifiData::available(void)
|
---|
48 | {
|
---|
49 | return FIFOAvailableData();
|
---|
50 | }
|
---|
51 |
|
---|
52 | int WifiData::read(void)
|
---|
53 | {
|
---|
54 | if ( peek_flag == 0) {
|
---|
55 | return ReadByte();
|
---|
56 | } else {
|
---|
57 | peek_flag = 0;
|
---|
58 | return peek_buf;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | size_t WifiData::write(uint8_t val)
|
---|
63 | {
|
---|
64 | WriteByte(val);
|
---|
65 | }
|
---|
66 |
|
---|
67 | void WifiData::pinMode(uint8_t pin, uint8_t i_o)
|
---|
68 | {
|
---|
69 | GPIOSetPinMode(pin, i_o);
|
---|
70 | }
|
---|
71 |
|
---|
72 | void WifiData::digitalWrite(uint8_t pin, uint8_t value)
|
---|
73 | {
|
---|
74 | GPIOSetPinState(pin, value);
|
---|
75 | }
|
---|
76 |
|
---|
77 | uint8_t WifiData::digitalRead(uint8_t pin)
|
---|
78 | {
|
---|
79 | return GPIOGetPinState(pin);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | uint8_t WifiData::ReadRegister(uint8_t reg_addr)
|
---|
84 | {
|
---|
85 | uint8_t result;
|
---|
86 | if ( protocol == SC16IS750_PROTOCOL_I2C ) { // register read operation via I2C
|
---|
87 |
|
---|
88 | WIRE.beginTransmission(device_address_sspin);
|
---|
89 | WIRE.write((reg_addr<<3));
|
---|
90 | WIRE.endTransmission(0);
|
---|
91 | WIRE.requestFrom(device_address_sspin,(uint8_t)1);
|
---|
92 | result = WIRE.read();
|
---|
93 | }
|
---|
94 |
|
---|
95 | return result;
|
---|
96 |
|
---|
97 | }
|
---|
98 |
|
---|
99 | void WifiData::WriteRegister(uint8_t reg_addr, uint8_t val)
|
---|
100 | {
|
---|
101 | if ( protocol == SC16IS750_PROTOCOL_I2C ) { // register read operation via I2C
|
---|
102 | WIRE.beginTransmission(device_address_sspin);
|
---|
103 | WIRE.write((reg_addr<<3));
|
---|
104 | WIRE.write(val);
|
---|
105 | WIRE.endTransmission(1);
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | return ;
|
---|
110 | }
|
---|
111 |
|
---|
112 | int16_t WifiData::SetBaudrate(uint32_t baudrate) //return error of baudrate parts per thousand
|
---|
113 | {
|
---|
114 | uint16_t divisor;
|
---|
115 | uint8_t prescaler;
|
---|
116 | uint32_t actual_baudrate;
|
---|
117 | int16_t error;
|
---|
118 | uint8_t temp_lcr;
|
---|
119 | if ( (ReadRegister(SC16IS750_REG_MCR)&0x80) == 0) { //if prescaler==1
|
---|
120 | prescaler = 1;
|
---|
121 | } else {
|
---|
122 | prescaler = 4;
|
---|
123 | }
|
---|
124 |
|
---|
125 | divisor = (SC16IS750_CRYSTCAL_FREQ/prescaler)/(baudrate*16);
|
---|
126 |
|
---|
127 | temp_lcr = ReadRegister(SC16IS750_REG_LCR);
|
---|
128 | temp_lcr |= 0x80;
|
---|
129 | WriteRegister(SC16IS750_REG_LCR,temp_lcr);
|
---|
130 | //write to DLL
|
---|
131 | WriteRegister(SC16IS750_REG_DLL,(uint8_t)divisor);
|
---|
132 | //write to DLH
|
---|
133 | WriteRegister(SC16IS750_REG_DLH,(uint8_t)(divisor>>8));
|
---|
134 | temp_lcr &= 0x7F;
|
---|
135 | WriteRegister(SC16IS750_REG_LCR,temp_lcr);
|
---|
136 |
|
---|
137 |
|
---|
138 | actual_baudrate = (SC16IS750_CRYSTCAL_FREQ/prescaler)/(16*divisor);
|
---|
139 | error = ((float)actual_baudrate-baudrate)*1000/baudrate;
|
---|
140 |
|
---|
141 | return error;
|
---|
142 |
|
---|
143 | }
|
---|
144 |
|
---|
145 | void WifiData::SetLine(uint8_t data_length, uint8_t parity_select, uint8_t stop_length )
|
---|
146 | {
|
---|
147 | uint8_t temp_lcr;
|
---|
148 | temp_lcr = ReadRegister(SC16IS750_REG_LCR);
|
---|
149 | temp_lcr &= 0xC0; //Clear the lower six bit of LCR (LCR[0] to LCR[5]
|
---|
150 |
|
---|
151 | switch (data_length) { //data length settings
|
---|
152 | case 5:
|
---|
153 | break;
|
---|
154 | case 6:
|
---|
155 | temp_lcr |= 0x01;
|
---|
156 | break;
|
---|
157 | case 7:
|
---|
158 | temp_lcr |= 0x02;
|
---|
159 | break;
|
---|
160 | case 8:
|
---|
161 | temp_lcr |= 0x03;
|
---|
162 | break;
|
---|
163 | default:
|
---|
164 | temp_lcr |= 0x03;
|
---|
165 | break;
|
---|
166 | }
|
---|
167 |
|
---|
168 | if ( stop_length == 2 ) {
|
---|
169 | temp_lcr |= 0x04;
|
---|
170 | }
|
---|
171 |
|
---|
172 | switch (parity_select) { //parity selection length settings
|
---|
173 | case 0: //no parity
|
---|
174 | break;
|
---|
175 | case 1: //odd parity
|
---|
176 | temp_lcr |= 0x08;
|
---|
177 | break;
|
---|
178 | case 2: //even parity
|
---|
179 | temp_lcr |= 0x18;
|
---|
180 | break;
|
---|
181 | case 3: //force '1' parity
|
---|
182 | temp_lcr |= 0x03;
|
---|
183 | break;
|
---|
184 | case 4: //force '0' parity
|
---|
185 | break;
|
---|
186 | default:
|
---|
187 | break;
|
---|
188 | }
|
---|
189 |
|
---|
190 | WriteRegister(SC16IS750_REG_LCR,temp_lcr);
|
---|
191 | }
|
---|
192 |
|
---|
193 | void WifiData::GPIOSetPinMode(uint8_t pin_number, uint8_t i_o)
|
---|
194 | {
|
---|
195 | uint8_t temp_iodir;
|
---|
196 |
|
---|
197 | temp_iodir = ReadRegister(SC16IS750_REG_IODIR);
|
---|
198 | if ( i_o == OUTPUT ) {
|
---|
199 | temp_iodir |= (0x01 << pin_number);
|
---|
200 | } else {
|
---|
201 | temp_iodir &= (uint8_t)~(0x01 << pin_number);
|
---|
202 | }
|
---|
203 |
|
---|
204 | WriteRegister(SC16IS750_REG_IODIR, temp_iodir);
|
---|
205 | return;
|
---|
206 | }
|
---|
207 |
|
---|
208 | void WifiData::GPIOSetPinState(uint8_t pin_number, uint8_t pin_state)
|
---|
209 | {
|
---|
210 | uint8_t temp_iostate;
|
---|
211 |
|
---|
212 | temp_iostate = ReadRegister(SC16IS750_REG_IOSTATE);
|
---|
213 | if ( pin_state == 1 ) {
|
---|
214 | temp_iostate |= (0x01 << pin_number);
|
---|
215 | } else {
|
---|
216 | temp_iostate &= (uint8_t)~(0x01 << pin_number);
|
---|
217 | }
|
---|
218 |
|
---|
219 | WriteRegister(SC16IS750_REG_IOSTATE, temp_iostate);
|
---|
220 | return;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | uint8_t WifiData::GPIOGetPinState(uint8_t pin_number)
|
---|
225 | {
|
---|
226 | uint8_t temp_iostate;
|
---|
227 |
|
---|
228 | temp_iostate = ReadRegister(SC16IS750_REG_IOSTATE);
|
---|
229 | if ( temp_iostate & (0x01 << pin_number)== 0 ) {
|
---|
230 | return 0;
|
---|
231 | }
|
---|
232 | return 1;
|
---|
233 | }
|
---|
234 |
|
---|
235 | uint8_t WifiData::GPIOGetPortState(void)
|
---|
236 | {
|
---|
237 |
|
---|
238 | return ReadRegister(SC16IS750_REG_IOSTATE);
|
---|
239 |
|
---|
240 | }
|
---|
241 |
|
---|
242 | void WifiData::GPIOSetPortMode(uint8_t port_io)
|
---|
243 | {
|
---|
244 | WriteRegister(SC16IS750_REG_IODIR, port_io);
|
---|
245 | return;
|
---|
246 | }
|
---|
247 |
|
---|
248 | void WifiData::GPIOSetPortState(uint8_t port_state)
|
---|
249 | {
|
---|
250 | WriteRegister(SC16IS750_REG_IOSTATE, port_state);
|
---|
251 | return;
|
---|
252 | }
|
---|
253 |
|
---|
254 | void WifiData::SetPinInterrupt(uint8_t io_int_ena)
|
---|
255 | {
|
---|
256 | WriteRegister(SC16IS750_REG_IOINTENA, io_int_ena);
|
---|
257 | return;
|
---|
258 | }
|
---|
259 |
|
---|
260 | void WifiData::ResetDevice(void)
|
---|
261 | {
|
---|
262 | uint8_t reg;
|
---|
263 |
|
---|
264 | reg = ReadRegister(SC16IS750_REG_IOCONTROL);
|
---|
265 | reg |= 0x08;
|
---|
266 | WriteRegister(SC16IS750_REG_IOCONTROL, reg);
|
---|
267 |
|
---|
268 | return;
|
---|
269 | }
|
---|
270 |
|
---|
271 | void WifiData::ModemPin(uint8_t gpio) //gpio == 0, gpio[7:4] are modem pins, gpio == 1 gpio[7:4] are gpios
|
---|
272 | {
|
---|
273 | uint8_t temp_iocontrol;
|
---|
274 |
|
---|
275 | temp_iocontrol = ReadRegister(SC16IS750_REG_IOCONTROL);
|
---|
276 | if ( gpio == 0 ) {
|
---|
277 | temp_iocontrol |= 0x02;
|
---|
278 | } else {
|
---|
279 | temp_iocontrol &= 0xFD;
|
---|
280 | }
|
---|
281 | WriteRegister(SC16IS750_REG_IOCONTROL, temp_iocontrol);
|
---|
282 |
|
---|
283 | return;
|
---|
284 | }
|
---|
285 |
|
---|
286 | void WifiData::GPIOLatch(uint8_t latch)
|
---|
287 | {
|
---|
288 | uint8_t temp_iocontrol;
|
---|
289 |
|
---|
290 | temp_iocontrol = ReadRegister(SC16IS750_REG_IOCONTROL);
|
---|
291 | if ( latch == 0 ) {
|
---|
292 | temp_iocontrol &= 0xFE;
|
---|
293 | } else {
|
---|
294 | temp_iocontrol |= 0x01;
|
---|
295 | }
|
---|
296 | WriteRegister(SC16IS750_REG_IOCONTROL, temp_iocontrol);
|
---|
297 |
|
---|
298 | return;
|
---|
299 | }
|
---|
300 |
|
---|
301 | void WifiData::InterruptControl(uint8_t int_ena)
|
---|
302 | {
|
---|
303 | WriteRegister(SC16IS750_REG_IER, int_ena);
|
---|
304 | }
|
---|
305 |
|
---|
306 | uint8_t WifiData::InterruptPendingTest(void)
|
---|
307 | {
|
---|
308 | return (ReadRegister(SC16IS750_REG_IIR) & 0x01);
|
---|
309 | }
|
---|
310 |
|
---|
311 | void WifiData::__isr(void)
|
---|
312 | {
|
---|
313 | uint8_t irq_src;
|
---|
314 |
|
---|
315 | irq_src = ReadRegister(SC16IS750_REG_IIR);
|
---|
316 | irq_src = (irq_src >> 1);
|
---|
317 | irq_src &= 0x3F;
|
---|
318 |
|
---|
319 | switch (irq_src) {
|
---|
320 | case 0x06: //Receiver Line Status Error
|
---|
321 | break;
|
---|
322 | case 0x0c: //Receiver time-out interrupt
|
---|
323 | break;
|
---|
324 | case 0x04: //RHR interrupt
|
---|
325 | break;
|
---|
326 | case 0x02: //THR interrupt
|
---|
327 | break;
|
---|
328 | case 0x00: //modem interrupt;
|
---|
329 | break;
|
---|
330 | case 0x30: //input pin change of state
|
---|
331 | break;
|
---|
332 | case 0x10: //XOFF
|
---|
333 | break;
|
---|
334 | case 0x20: //CTS,RTS
|
---|
335 | break;
|
---|
336 | default:
|
---|
337 | break;
|
---|
338 | }
|
---|
339 | return;
|
---|
340 | }
|
---|
341 |
|
---|
342 | void WifiData::FIFOEnable(uint8_t fifo_enable)
|
---|
343 | {
|
---|
344 | uint8_t temp_fcr;
|
---|
345 |
|
---|
346 | temp_fcr = ReadRegister(SC16IS750_REG_FCR);
|
---|
347 |
|
---|
348 | if (fifo_enable == 0){
|
---|
349 | temp_fcr &= 0xFE;
|
---|
350 | } else {
|
---|
351 | temp_fcr |= 0x01;
|
---|
352 | }
|
---|
353 | WriteRegister(SC16IS750_REG_FCR,temp_fcr);
|
---|
354 |
|
---|
355 | return;
|
---|
356 | }
|
---|
357 |
|
---|
358 | void WifiData::FIFOReset(uint8_t rx_fifo)
|
---|
359 | {
|
---|
360 | uint8_t temp_fcr;
|
---|
361 |
|
---|
362 | temp_fcr = ReadRegister(SC16IS750_REG_FCR);
|
---|
363 |
|
---|
364 | if (rx_fifo == 0){
|
---|
365 | temp_fcr |= 0x04;
|
---|
366 | } else {
|
---|
367 | temp_fcr |= 0x02;
|
---|
368 | }
|
---|
369 | WriteRegister(SC16IS750_REG_FCR,temp_fcr);
|
---|
370 |
|
---|
371 | return;
|
---|
372 |
|
---|
373 | }
|
---|
374 |
|
---|
375 | void WifiData::FIFOSetTriggerLevel(uint8_t rx_fifo, uint8_t length)
|
---|
376 | {
|
---|
377 | uint8_t temp_reg;
|
---|
378 |
|
---|
379 | temp_reg = ReadRegister(SC16IS750_REG_MCR);
|
---|
380 | temp_reg |= 0x04;
|
---|
381 | WriteRegister(SC16IS750_REG_MCR,temp_reg); //SET MCR[2] to '1' to use TLR register or trigger level control in FCR register
|
---|
382 |
|
---|
383 | temp_reg = ReadRegister(SC16IS750_REG_EFR);
|
---|
384 | WriteRegister(SC16IS750_REG_EFR, temp_reg|0x10); //set ERF[4] to '1' to use the enhanced features
|
---|
385 | if (rx_fifo == 0) {
|
---|
386 | WriteRegister(SC16IS750_REG_TLR, length<<4); //Tx FIFO trigger level setting
|
---|
387 | } else {
|
---|
388 | WriteRegister(SC16IS750_REG_TLR, length); //Rx FIFO Trigger level setting
|
---|
389 | }
|
---|
390 | WriteRegister(SC16IS750_REG_EFR, temp_reg); //restore EFR register
|
---|
391 |
|
---|
392 | return;
|
---|
393 | }
|
---|
394 |
|
---|
395 | uint8_t WifiData::FIFOAvailableData(void)
|
---|
396 | {
|
---|
397 | return ReadRegister(SC16IS750_REG_RXLVL);
|
---|
398 |
|
---|
399 | }
|
---|
400 |
|
---|
401 | uint8_t WifiData::FIFOAvailableSpace(void)
|
---|
402 | {
|
---|
403 | return ReadRegister(SC16IS750_REG_TXLVL);
|
---|
404 |
|
---|
405 | }
|
---|
406 |
|
---|
407 | void WifiData::WriteByte(uint8_t val)
|
---|
408 | {
|
---|
409 | uint8_t tmp_lsr;
|
---|
410 | do {
|
---|
411 | tmp_lsr = ReadRegister(SC16IS750_REG_LSR);
|
---|
412 | } while ((tmp_lsr&0x20) ==0);
|
---|
413 |
|
---|
414 | WriteRegister(SC16IS750_REG_THR,val);
|
---|
415 |
|
---|
416 | }
|
---|
417 |
|
---|
418 | int WifiData::ReadByte(void)
|
---|
419 | {
|
---|
420 | volatile uint8_t val;
|
---|
421 | if (FIFOAvailableData() == 0) {
|
---|
422 |
|
---|
423 | return -1;
|
---|
424 |
|
---|
425 | } else {
|
---|
426 |
|
---|
427 | val = ReadRegister(SC16IS750_REG_RHR);
|
---|
428 | return val;
|
---|
429 | }
|
---|
430 |
|
---|
431 | }
|
---|
432 |
|
---|
433 | void WifiData::EnableTransmit(uint8_t tx_enable)
|
---|
434 | {
|
---|
435 | uint8_t temp_efcr;
|
---|
436 | temp_efcr = ReadRegister(SC16IS750_REG_EFCR);
|
---|
437 | if ( tx_enable == 0) {
|
---|
438 | temp_efcr |= 0x04;
|
---|
439 | } else {
|
---|
440 | temp_efcr &= 0xFB;
|
---|
441 | }
|
---|
442 | WriteRegister(SC16IS750_REG_EFCR,temp_efcr);
|
---|
443 |
|
---|
444 | return;
|
---|
445 | }
|
---|
446 |
|
---|
447 | uint8_t WifiData::ping()
|
---|
448 | {
|
---|
449 | WriteRegister(SC16IS750_REG_SPR,0x55);
|
---|
450 | if (ReadRegister(SC16IS750_REG_SPR) !=0x55) {
|
---|
451 | return 0;
|
---|
452 | }
|
---|
453 |
|
---|
454 | WriteRegister(SC16IS750_REG_SPR,0xAA);
|
---|
455 | if (ReadRegister(SC16IS750_REG_SPR) !=0xAA) {
|
---|
456 | return 0;
|
---|
457 | }
|
---|
458 |
|
---|
459 | return 1;
|
---|
460 |
|
---|
461 | }
|
---|
462 |
|
---|
463 | void WifiData::flush()
|
---|
464 | {
|
---|
465 | uint8_t tmp_lsr;
|
---|
466 |
|
---|
467 | do {
|
---|
468 | tmp_lsr = ReadRegister(SC16IS750_REG_LSR);
|
---|
469 | } while ((tmp_lsr&0x20) ==0);
|
---|
470 |
|
---|
471 |
|
---|
472 | }
|
---|
473 |
|
---|
474 | int WifiData::peek()
|
---|
475 | {
|
---|
476 | if ( peek_flag == 0 ) {
|
---|
477 | peek_buf =ReadByte();
|
---|
478 | if ( peek_buf >= 0 ) {
|
---|
479 | peek_flag = 1;
|
---|
480 | }
|
---|
481 | }
|
---|
482 |
|
---|
483 | return peek_buf;
|
---|
484 |
|
---|
485 | }
|
---|
486 |
|
---|
487 | #endif
|
---|