Hello evryone,
I'm using nano every boards connected as slave via i2c to a Raspberry. Each board is used as PWM driver or analog read and centralised to Pi.
Evrything works good, I've to devices connected, I can send and get data. In another hand, I've programed an isr on TCA overflow, which works fine too.
My problem is that I can't connect device with isr running. When I'm mapping with i2cdetect from Pi, the first nano (not using ISR) is detected but not the second one.
It's not my first issue with i2c and interrupts, and I would like to know if it's common issue
Thank for your help
code (not all part of it, just issue related):
Root file:
void setup() {
Serial.begin(9600);
Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
InitPwm(res,0x2);
ratio0 = RatioMin;
ratio1 = 100;
T = timer_us();
}
///////////////////////////////////////////////////
// Loop
///////////////////////////////////////////////////
void loop()
{
SetRatio(ratio0, 0);
SetRatio(ratio1, 1);
while(1)
{
t = timer_us();
if((t - T)>1000) break;
}
T= timer_us();
ratio0++;
if(ratio0>RatioMax)
{
ratio0 = RatioMin;
}
}
Timer handling
// TCA configuration
void InitPwm(unsigned int res, byte divider)
{
pinMode(5, OUTPUT); //PWM compare output 2
pinMode(9, OUTPUT); //PWM compare output 0
pinMode(10, OUTPUT); //PWM compare output 1
// Used to be able to count time, because when changing TCA config, micros() and millis() didn't works properly
clk_per_100ps = pow(2,divider)*1e4/freq_MHz;
ovf_per_ns = (long)clk_per_100ps*(long)resolution/10;
minute_cnt = 60e10/(clk_per_100ps*resolution);
resolution = res;
//////////////////////////////////////////////////////////////////////////////
TCA0.SINGLE.PER = resolution; //Set TOP (determine frequency)
TCA0.SINGLE.CTRLA = (TCA0.SINGLE.CTRLA & 0b11110001) | divider<<1; // CLK Divider 1
TCA0.SINGLE.CTRLA = (TCA0.SINGLE.CTRLA & 0b11111110) | 0b1; // enable TCA0
TCA0.SINGLE.CTRLB = (TCA0.SINGLE.CTRLB & 0b11111000) | 0x3; // Single slope PWM mode
PORTMUX.TCAROUTEA=(PORTMUX.TCAROUTEA & 0b11111000) | 0x01; //TCA compare output on port B (32->WO.0, 33->WO.1, 28->WO.2)
TCA0.SINGLE.CTRLB = (TCA0.SINGLE.CTRLB & 0b10001111) | 0b01110000; // Compare Output 0-2 enable
TCA0.SINGLE.INTCTRL = (TCA0.SINGLE.INTCTRL & 0b11111110) | 1; // Activate overflow interrupt
}
// Interrupt handling
ISR(TCA0_OVF_vect)
{
ovf_cnt++;
}
I2c handling
void receiveData(int byteCount)
{
cli();
lenReceived = Wire.available();
while(Wire.available())
{
dataReceived[rcvcnt++] = Wire.read();
}
sei();
}
void sendData()
{
cli();
if(dataReceived[0]<=0xF) Wire.write((const uint8_t*)&I2cSend, 2);
sei();
}