# Photon 'Usage fault'

**URL:** <https://community.particle.io/t/photon-usage-fault/18599>\
**Category:** Troubleshooting\
**Created:** [December 28, 2015, 7:46pm UTC](https://community.particle.io/t/photon-usage-fault/18599 "2015-12-28T19:46:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ceido](https://avatars.discourse-cdn.com/v4/letter/c/74df32/32.png) [@ceido](https://community.particle.io/u/ceido)\
**Post date:** [December 28, 2015, 7:46pm UTC](https://community.particle.io/t/photon-usage-fault/18599/1 "2015-12-28T19:46:17Z")

</div>

Hi,

My Photon tells me I have a ‘Usage fault’ by flashing red 5 times after it’s SOS flash. What exactly does this mean?

I’ve tried to pinpoint which part of my code might be triggering it. My best guess is that it’s in the function below, as if I don’t call ‘getRawChannelLevels()’ the photon doesn’t error. If I include it, my code runs on the Photon for 1 to about 8 seconds (varies each time the program runs) before error.

Any ideas? Thanks.

```auto

void getRawChannelLevels(){
  // Cycle through each frequency band by pulsing the strobe.
  for (int i = 0; i < 7; i++) {

    digitalWrite (strobePin, LOW);
    delayMicroseconds (100); // Delay necessary due to timing diagram
    rawChannelLevels[i] = analogRead (outPin);

    if(rawChannelLevels[i]>channelCeilings[i]){
      channelCeilings[i] = rawChannelLevels[i];
      speed[i] = 60;//160;
    }
    
    channelLevels[i] = map(rawChannelLevels[i], channelBaseNoise[i], channelCeilings[i], 0, BRIGHTNESS);

    if(channelLevels[i]<40){
      channelLevels[i] =0;
    }

    if(channelLevels[i]>easedChannelLevels[i]){
      easedChannelLevels[i] = channelLevels[i];
    }
    if(speed[i]>20){
     speed[i]-=2;
    }

    if(easedChannelLevels[i]>5){
      easedChannelLevels[i] -=5;
    }

    channelLevels[i] = pgm_read_byte(&gamma[channelLevels[i] ]);

    digitalWrite (strobePin, HIGH);
    delayMicroseconds (100); // Delay necessary due to timing diagram
  }

  decreaseCeilingCounter++;
  if(decreaseCeilingCounter > decreaseCeilingCounterThreshold){
    for (int i = 0; i < 7; i++) {

      channelCeilings[i]-= decreaseCeilingAmount;
      if(channelCeilings[i]<680){
        channelCeilings[i]=680;
      }

    }
    decreaseCeilingCounter = 0;
  }
}

```

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [December 28, 2015, 7:58pm UTC](https://community.particle.io/t/photon-usage-fault/18599/2 "2015-12-28T19:58:58Z")

</div>

@ceido, can you share the declarations for your global variables like rawChannelLevels, channelLevels, etc.?

Also, since both flash and RAM are in the same “space” on the Photon, this line"

```auto
channelLevels[i] = pgm_read_byte(&gamma[channelLevels[i] ]);

```

can be rewritten to:

```auto
channelLevels[i] = gamma[channelLevels[i] ];

```

😉

---

<div class="post-metadata">

**Author:** ![ceido](https://avatars.discourse-cdn.com/v4/letter/c/74df32/32.png) [@ceido](https://community.particle.io/u/ceido)\
**Post date:** [December 28, 2015, 9:48pm UTC](https://community.particle.io/t/photon-usage-fault/18599/3 "2015-12-28T21:48:50Z")

</div>

Hi peekay123,

Thanks for the reply and the tip. I probably should have mentioned that I was running this project on an Arduino Uno before. All ran okay on there. And, yes sure, here they are:

> int rawChannelLevels[7];

> int channelBaseNoise[7];  
> int channelCeilings = {0,0,0,0,0,0,0};

> int channelLevels[7];  
> int easedChannelLevels = {0,0,0,0,0,0,0};

> uint16\_t speed = {2,2,2,2,2,2,2};

> int decreaseCeilingAmount = 1;

> int decreaseCeilingCounterThreshold = 10;  
> int decreaseCeilingCounter = 0;

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 28, 2015, 10:18pm UTC](https://community.particle.io/t/photon-usage-fault/18599/4 "2015-12-28T22:18:54Z")

</div>

It would be good to see the declaration for `gamma[]` too.  
Without seeing it and also without doing all the “maths” in your function, I could still imagine that the difference in the `analogRead()` range 0…4095 instead of 0…1023 might play a role here.  
Another reason might be the `map()` function since on the STM32 chips a div/0 error causes a system exception, while this was not the case on the AVRs.

Looking at the implementation

```cpp
long map(long value, long fromStart, long fromEnd, long toStart, long toEnd)
{
  return (value - fromStart) * (toEnd - toStart) / (fromEnd - fromStart) + toStart;
}

```

in case of `(fromEnd == fromStart)` or in your case `(channelBaseNoise[i] == channelCeilings[i])` you’d get an exception.

---

<div class="post-metadata">

**Author:** ![ceido](https://avatars.discourse-cdn.com/v4/letter/c/74df32/32.png) [@ceido](https://community.particle.io/u/ceido)\
**Post date:** [December 28, 2015, 11:36pm UTC](https://community.particle.io/t/photon-usage-fault/18599/5 "2015-12-28T23:36:03Z")

</div>

@ScruffR Looks like it was exactly what you said! Any time channelBaseNoise[i] == channelCeilings[i] it crashed. So I put something in to stop that ever happening, and… no SOS anymore! 😃 Thank you!

---

<div class="post-metadata">

**Author:** ![dklinkman](https://avatars.discourse-cdn.com/v4/letter/d/dec6dc/32.png) [@dklinkman](https://community.particle.io/u/dklinkman)\
**Post date:** [April 10, 2016, 10:29pm UTC](https://community.particle.io/t/photon-usage-fault/18599/6 "2016-04-10T22:29:01Z")

</div>

This post saved me. I searched ‘Usage fault’ and this was on top. Division by zero in my case. Of course.

I looked at the system events docs hoping I could trap on an exception that would result in a Usage fault. But I don’t see anything.

So if you all can tolerate a new post to an old thread and a question, is there any way to trap an exception like div/0 that would result in SOS an usage fault? Or any SOS at all?

Thanks so much.

---

<div class="post-metadata">

**Author:** ![rgm](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rgm/32/12079_2.png) [@rgm](https://community.particle.io/u/rgm)\
**Post date:** [December 1, 2016, 5:49am UTC](https://community.particle.io/t/photon-usage-fault/18599/7 "2016-12-01T05:49:28Z")

</div>

I just ran into this today. Is it unreasonable for Particle’s version of the map() function to protect against divide by zero like this?

```
long mymap(long x, long in_min, long in_max, long out_min, long out_max) {
  if (in_max == in_min) return in_min;
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

```

_Edit_: Ack, I can see why choosing to return in\_min is just about as bad / undefined / weird. Perhaps the behavior on the AVR should be considered the specification of the map function.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 1, 2016, 11:22am UTC](https://community.particle.io/t/photon-usage-fault/18599/8 "2016-12-01T11:22:31Z")

</div>

It's not unreasonable and thought that issue had been taken care of ages ago, since I had pointed that out as well (and I think I even filed a PR for that).  
Maybe a regression 😕

> [@rgm](#):
>
> Perhaps the behavior on the AVR should be considered the specification of the map function.

That won't work since the AVR will happily swollow a DIV/0 exception, but not so the STM32F - it'll just throw a panic SOS+13 Usage Fault.

Returning `NaN` would be a possibility but requires to `#include <math.h>`

However I've just filed a GitHub issue for that  
[https://github.com/spark/firmware/issues/1193](https://github.com/spark/firmware/issues/1193)
