-
Notifications
You must be signed in to change notification settings - Fork 9
I2C Slave
I2C SLAVE <ADDR> ONREAD GOSUB <LINE> ONWRITE GOSUB <LINE>
LEN(I2C READ)
LEN(I2C WRITE)
READ #I2C,<VARIABLE>
CLOSE I2C
These commands are used to communicate on the CC2541 I2C lines. BlueBasic takes the role of an I2C slave device with an assignable device address . The device address represents the 7-bit I2C device address. The I2C command prevents the CC2541 from sleep until the I2C port is closed again. When an I2C telegram has been received and the master put the interface into write mode BlueBasic will be notified with a READ interrupt. The READ buffer is currently limited to 16 bytes. A data request from the I2C master issuing a WRITE interrupt in BlueBasic is not yet implemented.
The function LEN(I2C READ)
returns the number of bytes received in the data buffer. The command `READ #I2C,[,, ...] reads the data buffer. Arrays are supported as variable type. Excessive bytes in an array will be filled with the value 255.
The command CLOSE I2C
closes the port, clears the ONREAD and ONWRITE pointers and allows the CC2541 device to go to sleep again.
10 I2C SLAVE 83 ONREAD GOSUB 100
20 DELAY 10000
30 GOTO 20
// When a master sends data to the address 83
// this codes gets executed after the stop condition
100 DIM V(16)
110 Z = LEN(I2C READ)
110 READ #I2C, V
// issuing a CLOSE will disable the I2C interface and allows for sleep
// if you want to continue receiving data, either leave the below line out
// or re-enable the I2C interface e.g. via timer or any other event.
// Tying the I2C clock or data to a GPIO to generate a wake interrupt is not
// working since initialising the I2C interface is to late.
120 CLOSE I2C
130 PRINT "Number of bytes received: ", Z
140 FOR I = 1 TO Z
150 PRINT "Byte ",I, ": ", V(Z-1)
160 NEXT I
170 RETURN