Skip to content

Commit 9bb98a2

Browse files
committed
minimal version of Wire compiles (but no function yet)
1 parent 0115fd6 commit 9bb98a2

7 files changed

Lines changed: 153 additions & 60 deletions

File tree

docs/usage/status-todo.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* `tone()`
5050
* `noTone()`
5151
* `pulseIn()`
52+
* handling of write error in `Print` (methods `setWriteError` etc.)
5253
* module WCharacter
5354
* module WString
5455

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BOARD_TAG = stm8sblue
2+
3+
include ../../../../../../../sduino.mk

sduino/hardware/sduino/stm8/libraries/Wire/examples/digital_potentiometer/digital_potentiometer.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
#include <Wire.h>
1616

1717
void setup() {
18-
Wire.begin(); // join i2c bus (address optional for master)
18+
Wire_begin(); // join i2c bus (address optional for master)
1919
}
2020

2121
byte val = 0;
2222

2323
void loop() {
24-
Wire.beginTransmission(44); // transmit to device #44 (0x2c)
24+
Wire_beginTransmission(44); // transmit to device #44 (0x2c)
2525
// device address is specified in datasheet
26-
Wire.write(byte(0x00)); // sends instruction byte
27-
Wire.write(val); // sends potentiometer value byte
28-
Wire.endTransmission(); // stop transmitting
26+
Wire_write(0x00); // sends instruction byte
27+
Wire_write(val); // sends potentiometer value byte
28+
Wire_endTransmission(); // stop transmitting
2929

3030
val++; // increment value
3131
if (val == 64) { // if reached 64th position (max)

sduino/hardware/sduino/stm8/libraries/Wire/src/Wire.cpp renamed to sduino/hardware/sduino/stm8/libraries/Wire/src/Wire.c

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,46 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
1919
Modified 2012 by Todd Krein ([email protected]) to implement repeated starts
20+
Modified 2017 by Michael Mayer to plain C for use with Sduino
2021
*/
2122

22-
extern "C" {
23+
//extern "C" {
2324
#include <stdlib.h>
2425
#include <string.h>
25-
#include <inttypes.h>
26+
// #include <inttypes.h>
2627
#include "utility/twi.h"
27-
}
28+
//}
2829

2930
#include "Wire.h"
3031

3132
// Initialize Class Variables //////////////////////////////////////////////////
3233

33-
uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
34-
uint8_t TwoWire::rxBufferIndex = 0;
35-
uint8_t TwoWire::rxBufferLength = 0;
34+
static uint8_t rxBuffer[BUFFER_LENGTH];
35+
static uint8_t rxBufferIndex = 0;
36+
static uint8_t rxBufferLength = 0;
3637

37-
uint8_t TwoWire::txAddress = 0;
38-
uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
39-
uint8_t TwoWire::txBufferIndex = 0;
40-
uint8_t TwoWire::txBufferLength = 0;
38+
static uint8_t txAddress = 0;
39+
static uint8_t txBuffer[BUFFER_LENGTH];
40+
static uint8_t txBufferIndex = 0;
41+
static uint8_t txBufferLength = 0;
4142

42-
uint8_t TwoWire::transmitting = 0;
43+
static uint8_t transmitting = 0;
44+
/*
4345
void (*TwoWire::user_onRequest)(void);
4446
void (*TwoWire::user_onReceive)(int);
47+
*/
4548

4649
// Constructors ////////////////////////////////////////////////////////////////
4750

51+
/*
4852
TwoWire::TwoWire()
4953
{
5054
}
55+
*/
5156

5257
// Public Methods //////////////////////////////////////////////////////////////
5358

54-
void TwoWire::begin(void)
59+
void Wire_begin(void)
5560
{
5661
rxBufferIndex = 0;
5762
rxBufferLength = 0;
@@ -62,6 +67,7 @@ void TwoWire::begin(void)
6267
twi_init();
6368
}
6469

70+
/*
6571
void TwoWire::begin(uint8_t address)
6672
{
6773
twi_setAddress(address);
@@ -79,12 +85,40 @@ void TwoWire::end(void)
7985
{
8086
twi_disable();
8187
}
88+
*/
8289

90+
/*
8391
void TwoWire::setClock(uint32_t clock)
8492
{
8593
twi_setFrequency(clock);
8694
}
95+
*/
96+
97+
uint8_t Wire_requestFrom(uint8_t address, uint8_t quantity)
98+
{
99+
return Wire_requestFrom3(address, quantity, (uint8_t)true);
100+
}
101+
102+
103+
uint8_t Wire_requestFrom3(uint8_t address, uint8_t quantity, uint8_t sendStop)
104+
{
105+
uint8_t read;
106+
107+
// clamp to buffer length
108+
if(quantity > BUFFER_LENGTH){
109+
quantity = BUFFER_LENGTH;
110+
}
111+
// perform blocking read into buffer
112+
read = twi_readFrom(address, rxBuffer, quantity, sendStop);
113+
// set rx buffer iterator vars
114+
rxBufferIndex = 0;
115+
rxBufferLength = read;
116+
117+
return read;
118+
}
119+
87120

121+
/*
88122
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop)
89123
{
90124
if (isize > 0) {
@@ -136,8 +170,9 @@ uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
136170
{
137171
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
138172
}
173+
*/
139174

140-
void TwoWire::beginTransmission(uint8_t address)
175+
void Wire_beginTransmission(uint8_t address)
141176
{
142177
// indicate that we are transmitting
143178
transmitting = 1;
@@ -148,10 +183,12 @@ void TwoWire::beginTransmission(uint8_t address)
148183
txBufferLength = 0;
149184
}
150185

186+
/*
151187
void TwoWire::beginTransmission(int address)
152188
{
153189
beginTransmission((uint8_t)address);
154190
}
191+
*/
155192

156193
//
157194
// Originally, 'endTransmission' was an f(void) function.
@@ -166,7 +203,7 @@ void TwoWire::beginTransmission(int address)
166203
// no call to endTransmission(true) is made. Some I2C
167204
// devices will behave oddly if they do not see a STOP.
168205
//
169-
uint8_t TwoWire::endTransmission(uint8_t sendStop)
206+
uint8_t Wire_endTransmission1(uint8_t sendStop)
170207
{
171208
// transmit buffer (blocking)
172209
uint8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1, sendStop);
@@ -178,24 +215,26 @@ uint8_t TwoWire::endTransmission(uint8_t sendStop)
178215
return ret;
179216
}
180217

218+
/*
181219
// This provides backwards compatibility with the original
182220
// definition, and expected behaviour, of endTransmission
183221
//
184222
uint8_t TwoWire::endTransmission(void)
185223
{
186224
return endTransmission(true);
187225
}
226+
*/
188227

189228
// must be called in:
190229
// slave tx event callback
191230
// or after beginTransmission(address)
192-
size_t TwoWire::write(uint8_t data)
231+
size_t Wire_write(uint8_t data)
193232
{
194233
if(transmitting){
195234
// in master transmitter mode
196235
// don't bother if buffer is full
197236
if(txBufferLength >= BUFFER_LENGTH){
198-
setWriteError();
237+
//FIXME setWriteError();
199238
return 0;
200239
}
201240
// put byte in tx buffer
@@ -211,6 +250,7 @@ size_t TwoWire::write(uint8_t data)
211250
return 1;
212251
}
213252

253+
/*
214254
// must be called in:
215255
// slave tx event callback
216256
// or after beginTransmission(address)
@@ -228,22 +268,23 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity)
228268
}
229269
return quantity;
230270
}
271+
*/
231272

232273
// must be called in:
233274
// slave rx event callback
234275
// or after requestFrom(address, numBytes)
235-
int TwoWire::available(void)
276+
int Wire_available(void)
236277
{
237278
return rxBufferLength - rxBufferIndex;
238279
}
239280

240281
// must be called in:
241282
// slave rx event callback
242283
// or after requestFrom(address, numBytes)
243-
int TwoWire::read(void)
284+
int Wire_read(void)
244285
{
245286
int value = -1;
246-
287+
247288
// get each successive byte on each call
248289
if(rxBufferIndex < rxBufferLength){
249290
value = rxBuffer[rxBufferIndex];
@@ -253,6 +294,7 @@ int TwoWire::read(void)
253294
return value;
254295
}
255296

297+
/*
256298
// must be called in:
257299
// slave rx event callback
258300
// or after requestFrom(address, numBytes)
@@ -328,3 +370,4 @@ void TwoWire::onRequest( void (*function)(void) )
328370
329371
TwoWire Wire = TwoWire();
330372
373+
*/

sduino/hardware/sduino/stm8/libraries/Wire/src/Wire.h

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
1919
Modified 2012 by Todd Krein ([email protected]) to implement repeated starts
20+
Modified 2017 by Michael Mayer to plain C for use with Sduino
2021
*/
2122

2223
#ifndef TwoWire_h
2324
#define TwoWire_h
2425

25-
#include <inttypes.h>
26-
#include "Stream.h"
26+
//#include <stdint.h>
27+
//#include "Stream.h"
2728

2829
#define BUFFER_LENGTH 32
2930

3031
// WIRE_HAS_END means Wire has end()
3132
#define WIRE_HAS_END 1
3233

34+
/*
3335
class TwoWire : public Stream
3436
{
3537
private:
@@ -80,6 +82,36 @@ class TwoWire : public Stream
8082
};
8183
8284
extern TwoWire Wire;
85+
*/
86+
87+
/* only this minimal interface is currently implemented: */
88+
void Wire_begin(void);
89+
90+
void Wire_beginTransmission(uint8_t);
91+
uint8_t Wire_endTransmission1(uint8_t sendStop);
92+
inline uint8_t Wire_endTransmission(void){Wire_endTransmission1(true);}
93+
94+
size_t Wire_write(uint8_t);
95+
int Wire_available(void);
96+
int Wire_read(void);
97+
98+
uint8_t Wire_requestFrom(uint8_t address, uint8_t quantity);
99+
uint8_t Wire_requestFrom3(uint8_t address, uint8_t quantity, uint8_t sendStop);
100+
83101

84102
#endif
85103

104+
/* required low level functions:
105+
106+
twi_init(void);
107+
108+
// perform blocking read into buffer
109+
uint8_t read = twi_readFrom(address, rxBuffer, quantity, sendStop);
110+
111+
// transmit buffer (blocking)
112+
uint8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1, sendStop);
113+
114+
// reply to master
115+
twi_transmit(&data, 1);
116+
117+
*/

0 commit comments

Comments
 (0)