Integration Guide
SMK900 Integration Guide Revision 4
Example VM User Script using I2C Functions
Here is a VM example for a simple node that (1) at boot-up, queries the external sleep controller for
the RF channel variable, and configures the transceiver NwkId and HopTable configuration registers
accordingly (see section 7.3 for details about configuration registers); (2) when receiving an air
command from a gateway, it queries the external sleep controller voltage, and sends this back as the
gateway answer, in conjunction with the last known received packet RSSI value.
#include "SMK900.evi"
//Note that SMK900.evi include code listing is appended as an annex of this document.
//This include defines transceiver-specific constants as well as available special-purpose
//transceiver functions. See section 8.2.1 for details.
#define RFCHANNEL_MAX_RETRIES_COUNT_STD (20)
#define SLEEPCTRL_I2C_ADDR_AND_0X7F_SHIFTL_1_OR_I2CMASTER_READMODE_gc (0x91)
function exec_bootup(){
local rfChannel;
local retryCount;
local nwkid;
local hoptableid;
local i;
retryCount=0;
//Hardcode a delay of 100usec * 1000 * 60 = 6 seconds
for(i=0;i<60;i++){
Delay(1000);
}
while(retryCount<RFCHANNEL_MAX_RETRIES_COUNT_STD){
if(I2C_Start( SLEEPCTRL_I2C_ADDR_AND_0X7F_SHIFTL_1_OR_I2CMASTER_READMODE_gc ) ){
//Sleep controller answered NAK
I2C_Stop();
retryCount++;
Delay(5000);//delay 500 msec before retrying
}
else{
//Sleep controller answered ACK
I2C_ReadAck(); //1st byte is voltage. We discard
rfChannel = I2C_ReadNak(); //2nd byte is RF channel
I2C_Stop();
//Define NwkId and HopTable register values according to read RF channel value
//stored in external sleep controller
nwkid = rfChannel % NWK_COUNT;
hoptableid = HOPTABLE_50K_START + (rfChannel % HOPTABLE_50K_COUNT);
//REGISTER_xxx are VM-accessible mesh controller configuration parameters
GetRegisterRAMBUF(8, REGISTER_NWKID);
GetRegisterRAMBUF(9, REGISTER_HOPTABLE);
if( (nwkid!=GetBuffer_U8(8)) || (hoptableid!=GetBuffer_U8(9)) ){
SetBuffer(8,nwkid,1);
SetBuffer(9,hoptableid,1);
SetRegisterRAMBUF(8, REGISTER_NWKID);
SetRegisterRAMBUF(9, REGISTER_HOPTABLE);
TransferConfigEEPROM();
//force reset by letting watchdog expire (the mesh controller
//has a safety watchdog that runs at all times, and set by default at 8 sec)
while(1){}
}
break;
}
}
}
//Custom VM function executed when end node mesh controller receives an "air cmd", which is
//a command from coordinator node to execute a VM function over the air
function exec_aircmd(){
local V;
//time to read external sleep controller voltage using I2C bus
if(I2C_Start( SLEEPCTRL_I2C_ADDR_AND_0X7F_SHIFTL_1_OR_I2CMASTER_READMODE_gc ) ){ //failure
V=0;
}
else{
V = I2C_ReadNak();
}
I2C_Stop();
SetBuffer(0,GetRSSI(),1); //set first answer byte as the RSSI of the last known good packet received
SetBuffer(1,V,1); //set 2nd answer byte as voltage fetched via I2C bus
20










