Interrupt and multiple spi slaves conflicting[SOLVED]


(Daniel kirubakaran) #1

Hi,
I am working with drone project and using Hifive1 (risc-v_ micro controller)
My problem is,PPM signal from the receiver is set as an input and it is working in interrupt (RISING).
on the other side SPI slave 1 gyroscope (MpU9250) and slave 2 ADC mCP3008 (for battery voltage reading)

the following are the cases of testing

1)PPM interrupt and Slave 1 - Working

2)Slave 1 and Slave 2- working

  1. PPM interrupt and slave 2- working

  2. ppm interrupt and slave 1 and 2 - not working (Conflicting)

conflicting in the sense when the 4th case is tested it stops in the loop abruptly.

What is the reason? Interrupt pin is separate and both spi slaves are working in common spi pins ,then where is the problem lies?


(Daniel kirubakaran) #2

Kindly help me out!

This is my code

#include "encoding.h"
#include <Adafruit_MCP3008.h>

Adafruit_MCP3008 adc;

#include <SPI.h>
#include <MPU9250.h>

#define SPI_CLOCK 8000000  // 8MHz clock works.

#define SS_PIN_GYRO   9 

MPU9250 mpu(SPI_CLOCK, SS_PIN_GYRO);//9265 is 9250 compatible

////////////////////////////////
unsigned long t[8];
int pulse = 0;

int receiver_input[8];
/////////////////////////////// 

uint8_t response_acc[6];
uint8_t response_gyro[6];
uint8_t response_temp[2];

int16_t bit_data;
float data;

int16_t accel_data[3];
// float temperature;
int16_t gyro_data[3];
//////////////////////////////



int count = 0;
int channel=1;
 int   battery_voltage;
//////////////////////////////
volatile int ppmPin = 17;
volatile int ppmPinMsk;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  pinMode(2, OUTPUT);//led connected
  pinMode(3, OUTPUT);//led connected
  pinMode(15,OUTPUT);//led connected
  pinMode(ppmPin, INPUT);

delay(250);

 // (sck, mosi, miso, cs);
adc.begin(8);

delay(250);
SPI.begin(SS_PIN_GYRO);

mpu.init(false);
  ppmPinMsk = digitalPinToBitMask(ppmPin);
  
  attachInterrupt(digitalPinToInterrupt(ppmPin), ISR, RISING);
  
  set_csr(mstatus, MSTATUS_MIE);
  
uint8_t wai = mpu.whoami();
  if (wai == 0x71){
    Serial.println("Successful connection");
  }
  else{
    Serial.print("Failed connection: ");
    Serial.println(wai, HEX);
  }
//mpu.set_gyro_scale(BITS_FS_500DPS);
//mpu.set_acc_scale(BITS_FS_8G);



   delay(500);
   battery_voltage = (adc.readADC(channel)+65) * 1.2317; /
      Serial.print(battery_voltage); Serial.print("<-----<|");
   
}

void loop() {
  // put your main code here, to run repeatedly:
  


 readValue();
  

  Serial.print(gyro_data[0]);   Serial.print('\t');
  Serial.print(gyro_data[1]);   Serial.print('\t');
  Serial.print(gyro_data[2]);   Serial.print('\t');
  Serial.print(accel_data[0]);  Serial.print('\t');
  Serial.print(accel_data[1]);  Serial.print('\t');
  Serial.print(accel_data[2]);  Serial.print('\t');
Serial.print(mpu.temperature);  Serial.print('\t');Serial.print(adc.readADC(channel));  Serial.print('\t');

battery_voltage = battery_voltage * 0.92 + (adc.readADC(channel)+65) * 0.09853;
  
  
  
  Serial.print(battery_voltage); Serial.print("\t");
  Serial.print("["); Serial.print(count); Serial.print("]");
  count++;
  
 //set_csr(mstatus, MSTATUS_MIE); 
  Serial.print(receiver_input[1]); Serial.print("-");
  Serial.print(receiver_input[2]); Serial.print("-");
  Serial.print(receiver_input[3]); Serial.print("-");
  Serial.print(receiver_input[4]); Serial.print("-");
  Serial.print(receiver_input[5]); Serial.print("-");
  Serial.println(receiver_input[6]);
//Serial.println(battery_voltage);
//delay(250);
}

void readValue(){
    mpu.ReadRegs(MPUREG_GYRO_XOUT_H,response_gyro,6);
  for(int i = 0; i < 3; i++) {
        bit_data = ((int16_t)response_gyro[i*2]<<8)|response_gyro[i*2+1];
        data = (float)bit_data;
        gyro_data[i] = data;//data/acc_divider - a_bias[i];
    }
   mpu.ReadRegs(MPUREG_ACCEL_XOUT_H,response_acc,6);
   for(int i = 0; i < 3; i++) {
        bit_data = ((int16_t)response_acc[i*2]<<8)|response_acc[i*2+1];
        data = (float)bit_data;
        accel_data[i] = data;//data/acc_divider - a_bias[i];
    }
   // mpu.ReadRegs(MPUREG_TEMP_OUT_H,response_temp,2);
  mpu.read_temp();
  
}
void ISR() {
  
  if (!(GPIO_REG(GPIO_INPUT_VAL) & ppmPinMsk)) {

    t[pulse] = micros();

    switch (pulse) {
      case 1:
        receiver_input[1] = t[1] - t[0];
        pulse++;
        if (receiver_input[1] > 3000) {
          t[0] = t[1];
          pulse = 1;
        }
        GPIO_REG(GPIO_RISE_IP) = ppmPinMsk;
        break;
      case 2:
        receiver_input[2] = t[2] - t[1];
        pulse++;
        if (receiver_input[2] > 3000) {
          t[0] = t[2];
          pulse = 1;
        }
        GPIO_REG(GPIO_RISE_IP) = ppmPinMsk;
        break;
      case 3:
        receiver_input[3] = t[3] - t[2];
        pulse++;
        if (receiver_input[3] > 3000) {
          t[0] = t[3];
          pulse = 1;
        }
        GPIO_REG(GPIO_RISE_IP) = ppmPinMsk;
        break;
      case 4:
        receiver_input[4] = t[4] - t[3];
        pulse++;
        if (receiver_input[4] > 3000) {
          t[0] = t[4];
          pulse = 1;
        }
        GPIO_REG(GPIO_RISE_IP) = ppmPinMsk;
        break;
      case 5:
        receiver_input[5] = t[5] - t[4];
        pulse++;
        if (receiver_input[5] > 3000) {
          t[0] = t[5];
          pulse = 1;
        }
        GPIO_REG(GPIO_RISE_IP) = ppmPinMsk;
        break;
      case 6:
        receiver_input[6] = t[6] - t[5];
        pulse++;
        if (receiver_input[6] > 3000) {
          t[0] = t[6];
          pulse = 1;
        }
        GPIO_REG(GPIO_RISE_IP) = ppmPinMsk;
        break;
      case 7:
        receiver_input[7] = t[7] - t[6];
        pulse++;
        if (receiver_input[7] > 3000) {
          t[0] = t[7];
          pulse = 1;
        }
        GPIO_REG(GPIO_RISE_IP) = ppmPinMsk;
        break;

      default:
        pulse++;
        break;
    }

  }


}

when i use set_csr(mstatus,MSTATUS_MIE)
it stops abruptly. why multiple spi slaves could not be used when set_csr(mstatus,MSTATUS_MIE) declared?


(Daniel kirubakaran) #3

issue got solved. When using digital pins 15,16 and 17 for ppm interrupt it stops the execution, after that i changed using dig pin 4 it is working properly.
I don’t know the internal reason of why! i have gone through schematics as well nothing that found to be different in connection perspective.