I2Cwrapper v0.5.0
Generic framework for Arduino I2C target devices
firmware.ino File Reference

Generic firmware framework for I2C targets with modular functionality, built around the I2Cwrapper library. More...

Detailed Description

Generic firmware framework for I2C targets with modular functionality, built around the I2Cwrapper library.

Author

Copyright (c) 2022 juh

License

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.

Todo:

make I2C address configurable by hardware (with module?)

return messages (results) should ideally come with an id, too, so that controller can be sure it's the correct result. Currently only CRC8, i.e. correct transmission is checked.

volatile variables / ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {}

Reduce memory use to make it fit into an 8k Attiny4k ATtiny

Macros

#define MF_STAGE_includes   1
 
#define MF_STAGE_declarations   2
 
#define MF_STAGE_setup   3
 
#define MF_STAGE_loop   4
 
#define MF_STAGE_processMessage   5
 
#define MF_STAGE_reset   6
 
#define MF_STAGE_receiveEvent   7
 
#define MF_STAGE_requestEvent   8
 
#define MF_STAGE_I2CstateChange   9
 
#define MF_STAGE   MF_STAGE_includes
 
#define log(...)
 
#define MF_STAGE   MF_STAGE_I2CstateChange
 
#define MF_STAGE   MF_STAGE_declarations
 
#define MF_STAGE   MF_STAGE_setup
 
#define MF_STAGE   MF_STAGE_loop
 
#define MF_STAGE   MF_STAGE_processMessage
 
#define MF_STAGE   MF_STAGE_reset
 
#define MF_STAGE   MF_STAGE_receiveEvent
 
#define MF_STAGE   MF_STAGE_requestEvent
 

Enumerations

enum  I2Cstates {
  initializing , readyForCommand , processingCommand , readyForResponse ,
  responding , tainted
}
 

Functions

void changeI2CstateTo (I2Cstates newState)
 
void receiveEvent (int howMany)
 
void requestEvent ()
 Handle I2C request event. Will send results or information requested by the last command, as defined by the contents of the outputBuffer. More...
 
void triggerInterrupt (uint8_t source, uint8_t reason)
 Interrupt controller if an interrupt pin has been set and, optionally, define a source and reason having caused the interrupt that the controller can learn about by calling the I2Cwrapper::clearInterrupt() function. More...
 
void clearInterrupt ()
 
void initializeFirmware ()
 
void startI2C ()
 
void setup ()
 Setup system. More...
 
void loop ()
 Main loop. By default, it doesn't do a lot apart from debugging: It just checks if an ISR signaled that a new message has arrived and calls processMessage() accordingly. More...
 
void processMessage (uint8_t len)
 Process the message stored in bufferIn with lenght len which was received via I2C. Messages consist of at least three bytes: [0] CRC8 checksum [1] Command. Most commands correspond to a module's function, some are specific for I2Cwrapper core functions. Unknown commands are ignored. [2] Number of the unit (stepper, servo, ...) that is to receive the command. Not all modules use units and ignore this value accordingly. [3..maxBuffer] Optional parameter bytes. Each command that comes with too many or too few parameter bytes is ignored. More...
 
void writeOutputBuffer ()
 

Variables

uint8_t i2c_address = I2CwrapperDefaultAddress
 
uint32_t cycles = 0
 
SimpleBufferbufferIn
 
SimpleBufferbufferOut
 
volatile uint8_t newMessage = 0
 
volatile I2Cstates I2Cstate = initializing
 
int8_t interruptPin
 
bool interruptActiveHigh
 
uint8_t interruptSource
 
uint8_t interruptReason
 

Macro Definition Documentation

◆ log

#define log (   ...)

◆ MF_STAGE [1/9]

#define MF_STAGE   MF_STAGE_includes

◆ MF_STAGE [2/9]

#define MF_STAGE   MF_STAGE_I2CstateChange

◆ MF_STAGE [3/9]

#define MF_STAGE   MF_STAGE_declarations

◆ MF_STAGE [4/9]

#define MF_STAGE   MF_STAGE_setup

◆ MF_STAGE [5/9]

#define MF_STAGE   MF_STAGE_loop

◆ MF_STAGE [6/9]

#define MF_STAGE   MF_STAGE_processMessage

◆ MF_STAGE [7/9]

#define MF_STAGE   MF_STAGE_reset

◆ MF_STAGE [8/9]

#define MF_STAGE   MF_STAGE_receiveEvent

◆ MF_STAGE [9/9]

#define MF_STAGE   MF_STAGE_requestEvent

◆ MF_STAGE_declarations

#define MF_STAGE_declarations   2

◆ MF_STAGE_I2CstateChange

#define MF_STAGE_I2CstateChange   9

◆ MF_STAGE_includes

#define MF_STAGE_includes   1

◆ MF_STAGE_loop

#define MF_STAGE_loop   4

◆ MF_STAGE_processMessage

#define MF_STAGE_processMessage   5

◆ MF_STAGE_receiveEvent

#define MF_STAGE_receiveEvent   7

◆ MF_STAGE_requestEvent

#define MF_STAGE_requestEvent   8

◆ MF_STAGE_reset

#define MF_STAGE_reset   6

◆ MF_STAGE_setup

#define MF_STAGE_setup   3

Enumeration Type Documentation

◆ I2Cstates

enum I2Cstates
Enumerator
initializing 
readyForCommand 
processingCommand 
readyForResponse 
responding 
tainted 

Function Documentation

◆ changeI2CstateTo()

void changeI2CstateTo ( I2Cstates  newState)

◆ clearInterrupt()

void clearInterrupt ( )

◆ initializeFirmware()

void initializeFirmware ( )

◆ loop()

void loop ( void  )

Main loop. By default, it doesn't do a lot apart from debugging: It just checks if an ISR signaled that a new message has arrived and calls processMessage() accordingly.

◆ processMessage()

void processMessage ( uint8_t  len)

Process the message stored in bufferIn with lenght len which was received via I2C. Messages consist of at least three bytes: [0] CRC8 checksum [1] Command. Most commands correspond to a module's function, some are specific for I2Cwrapper core functions. Unknown commands are ignored. [2] Number of the unit (stepper, servo, ...) that is to receive the command. Not all modules use units and ignore this value accordingly. [3..maxBuffer] Optional parameter bytes. Each command that comes with too many or too few parameter bytes is ignored.

◆ receiveEvent()

void receiveEvent ( int  howMany)

@ brief Handle I2C receive event. Just read the message and inform main loop.

◆ requestEvent()

void requestEvent ( )

Handle I2C request event. Will send results or information requested by the last command, as defined by the contents of the outputBuffer.

◆ setup()

void setup ( void  )

Setup system.

◆ startI2C()

void startI2C ( )

◆ triggerInterrupt()

void triggerInterrupt ( uint8_t  source,
uint8_t  reason 
)

Interrupt controller if an interrupt pin has been set and, optionally, define a source and reason having caused the interrupt that the controller can learn about by calling the I2Cwrapper::clearInterrupt() function.

Parameters
source4-bit value that can be used to signal the controller where in the target device the interrupt occured, e.g. which end stop switch was triggered or which touch button was touched. 0xF is reserved to signal "unknown source"
reason4-bit value that can be used to differentiate between different interrupt causing events, e.g. end stop hit vs. target reached. 0xF is reserved to signal "unknown reason"

◆ writeOutputBuffer()

void writeOutputBuffer ( )

Variable Documentation

◆ bufferIn

SimpleBuffer* bufferIn

◆ bufferOut

SimpleBuffer* bufferOut

◆ cycles

uint32_t cycles = 0

◆ i2c_address

uint8_t i2c_address = I2CwrapperDefaultAddress

◆ I2Cstate

volatile I2Cstates I2Cstate = initializing

◆ interruptActiveHigh

bool interruptActiveHigh

◆ interruptPin

int8_t interruptPin

◆ interruptReason

uint8_t interruptReason

◆ interruptSource

uint8_t interruptSource

◆ newMessage

volatile uint8_t newMessage = 0