|
| 1 | +# Wire |
| 2 | + |
| 3 | +This is an improved version of the stock Arduino Wire library for I2C |
| 4 | +communication. It shares most of the hardware related code with the re-write |
| 5 | +of the [Sduino I2C library](I2C.md), but holds on to the Arduino API. |
| 6 | + |
| 7 | +The main difference between this STM8 version and the Arduino AVR version is |
| 8 | +the existance of a timeout function to prevent deadlocks due to failed |
| 9 | +communication. Polling mode is used for communication, interrupts are not |
| 10 | +supported (yet). |
| 11 | + |
| 12 | +So far, only Master Transmit and Master Receive modes are supported, slave |
| 13 | +modes are not implemented yet. |
| 14 | + |
| 15 | + |
| 16 | +## API |
| 17 | + |
| 18 | +This is a pre-instantiated singleton library. It is not possible to use more |
| 19 | +than one instance per sketch or to change the instance name. |
| 20 | + |
| 21 | +The API syntax is very similar to the original C++ syntax. Some name |
| 22 | +mangeling was needed to distinguish the different variants of the `write()` |
| 23 | +method. Apart from this replacing the dots in the method names for |
| 24 | +underscores is all it needs. |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +Arduino syntax |sduino syntax |real function name |
| 29 | +-------------------- |--------------------- |------------- |
| 30 | +`Wire.begin()` |`Wire_begin()` |same |
| 31 | +`Wire.begin(ownaddress)` |slave mode not supported yet|- |
| 32 | +`Wire.end()` |`Wire_end()` |same |
| 33 | +`Wire.setClock(clock)` |`Wire_setClock(clock)` |same |
| 34 | +not implemented |`Wire_timeOut(millisec)` |same |
| 35 | +`Wire.beginTransmission(addr)` |`Wire_beginTransmission(addr)` |same |
| 36 | +n = `Wire.endTransmission()` |n = `Wire_endTransmission()` |same (inline) |
| 37 | +n = `Wire.endTransmission(stop)` |n = `Wire_endTransmission1(stop)`|same |
| 38 | +`Wire.write(val)` |`Wire_write(val)` |same |
| 39 | +`Wire.write(*str)` |`Wire_write_s(*str)` |same |
| 40 | +`Wire.write(*data, len)` |`Wire_write_sn(*data, len)` |same |
| 41 | +`n = Wire.available()` |`n = Wire_available()` |same |
| 42 | +`val = Wire.read()` |`val = Wire_read()` |same |
| 43 | +`Wire.flush()` |`Wire_flush()` |same |
| 44 | +`val = Wire.peek()` |`val = Wire_peek()` |same |
| 45 | +`Wire.requestFrom(addr, n)` |`Wire_requestFrom(addr, n)` |`Wire_requestFrom2` |
| 46 | +`Wire.requestFrom(addr, n, sendStop)` |`Wire_requestFrom(addr, n, sendStop)` |`Wire_requestFrom3` |
| 47 | +`Wire.requestFrom(addr, n, iaddr, isize, sendStop)`|`Wire_requestFrom(addr, n, iaddr, isize, sendStop)` |`Wire_requestFrom5` |
| 48 | +`Wire.print(*str)` |`Wire_print_s(*str)` |`Print_print_s(Write_write1,*str)` |
| 49 | +`Wire.print(*data,len)` |`Wire_print_sn(*data,len)` |`Print_print_sn(Write_write1,*data,len)` |
| 50 | +`Wire.print(long)` |`Wire_print_i(long)` |`Print_print_i(Write_write1,long)` |
| 51 | +`Wire.print(ulong)` |`Wire_print_u(ulong)` |`Print_print_u(Write_write1,ulong)` |
| 52 | +`Wire.print(long,base)` |`Wire_print_ib(long,base)` |`Print_print_ib(Write_write1,long,base)` |
| 53 | +`Wire.print(ulong,base)`|`Wire_print_ub(ulong,base)` |`Print_print_ub(Write_write1,ulong,base)` |
| 54 | +`Wire.print(double)` |`Wire_print_f(float)` |`Print_print_f(Write_write1,float)` |
| 55 | +`Wire.print(double,dig)`|`Wire_print_f(float,dig)` |`Print_print_fd(Write_write1,float,dig)` |
| 56 | +`Wire.println()` |`Wire_println()` |`Print_println(Write_write1)` |
| 57 | +`Wire.println(*str)` |`Wire_println_s(*str)` |`Print_println_s(Write_write1,*str)` |
| 58 | +`Wire.println(*data,len)`|`Wire_println_sn(*data,len)` |`Print_println_sn(Write_write1,*data,len)` |
| 59 | +`Wire.println(long)` |`Wire_println_i(long)` |`Print_println_i(Write_write1,long)` |
| 60 | +`Wire.println(ulong)` |`Wire_println_u(ulong)` |`Print_println_u(Write_write1,ulong)` |
| 61 | +`Wire.println(long,base)`|`Wire_println_ib(long,base)` |`Print_println_ib(Write_write1,long,base)` |
| 62 | +`Wire.println(ulong,base)`|`Wire_println_ub(ulong,base)`|`Print_println_ub(Write_write1,ulong,base)` |
| 63 | +`Wire.println(double)` |`Wire_println_f(float)` |`Print_println_f(Write_write1,float)` |
| 64 | +`Wire.println(double,dig)`|`Wire_println_f(float,dig)` |`Print_println_fd(Write_write1,float,dig)` |
| 65 | + |
| 66 | + |
| 67 | +Please note the polymorphism of the `Wire_requestFrom()` method. This is a |
| 68 | +special case and only possible for pre-instantiated singleton libraries. |
| 69 | + |
| 70 | +Due to the way sdcc invokes the preprocessor sdcpp the same trick doesn't |
| 71 | +work for functions without arguments - so we still need |
| 72 | +`Wire_endTransmission()` and `Wire_endTransmission1(stop)`. Hopefully, this |
| 73 | +inconsistency does not spread more confusion than it is worth. |
| 74 | + |
| 75 | + |
| 76 | +## Timeout |
| 77 | + |
| 78 | +The timeout functionality was added for the Sduino port and is not part of |
| 79 | +the original library. The maximum transmission length is limited by the size |
| 80 | +of the data buffer of 32 bytes. At 100kHz this would take aprox. 3ms (1 |
| 81 | +address byte, 32 data bytes, 1 ACK bit after every byte). |
| 82 | + |
| 83 | +The default timeout is set to 20ms, that should work for most cases. If |
| 84 | +using a very slow device, the timeout can be changed using the |
| 85 | +`Wire_setTimeout()` function any time after initialzing the library by |
| 86 | +calling `Wire_begin()`. The timeout is defined in ms, so the maximum value |
| 87 | +for this 16 bit value represents about 65s - more than a minute. |
| 88 | + |
| 89 | +The value zero disables the timeout feature. |
| 90 | + |
| 91 | + |
| 92 | +## Further reading |
| 93 | + |
| 94 | +Programming the I2C communication on the register level is quite complex for |
| 95 | +the STM8, especially in receive mode. The most detailed information on this |
| 96 | +topic is the |
| 97 | + |
| 98 | +- application note AN3281 bundeled with |
| 99 | +- the related source code package [STSW-STM8004](https://www.st.com/en/embedded-software/stsw-stm8004.html) |
| 100 | + |
| 101 | +Downloading from the ST website requires a (free) registration. Somebody |
| 102 | +uploaded the full package to a [github |
| 103 | +repository](https://github.com/jiaohaitao/stsw-stm8004) |
| 104 | + |
| 105 | +There are some important notes on I2C implementation in the errata sheets: |
| 106 | + |
| 107 | +- [STM8S001J3/003xx/103xx/903xx Errata sheet, rev. 5 |
| 108 | + (CD00265449)](https://www.st.com/content/ccc/resource/technical/document/errata_sheet/c9/f9/ef/bf/63/91/4a/1f/CD00265449.pdf/files/CD00265449.pdf/jcr:content/translations/en.CD00265449.pdf) |
| 109 | +- [STM8S005xx STM8S105xx Errata sheet, rev. 7 |
| 110 | + (CD00270741)](https://www.st.com/content/ccc/resource/technical/document/errata_sheet/e3/c3/4e/24/0c/ca/4b/e7/CD00270741.pdf/files/CD00270741.pdf/jcr:content/translations/en.CD00270741.pdf) |
| 111 | +- [STM8S007xx STM8S20xxx Errata sheet, rev. 6 |
| 112 | + (CD00244749)](https://www.st.com/content/ccc/resource/technical/document/errata_sheet/7a/94/8f/fe/84/14/41/6d/CD00244749.pdf/files/CD00244749.pdf/jcr:content/translations/en.CD00244749.pdf) |
0 commit comments