Hard fault when using my NTP client

Hello,

I use a self-made NTP client, based on a timer controlled state machine on my Argon and it seems that this client will produce a hard fault after one cycle (should be error code 1 when I see it right).

NTP Server("pool.ntp.org");

    ...
    Server.SetCallback(NTP_Callback);
    Server.Start();
   ...
NTP::NTP(const char* Server) : _mUpdateTimer(Timer(20, &NTP::_run, *this))
{
    this->_init(Server, NTP_DEFAULT_PORT, NTP_DEFAULT_TIME, NTP_DEFAULT_TIMEOUT, NULL);
}

void NTP::Start(void)
{
    this->_mState = STATE_SEND_PACKET;
    this->_mUpdateTimer.start();
}

void NTP::SetCallback(NTP::NTP_Callback Callback)
{
    this->_mCallback = Callback;
}

void NTP::_run(void)
{
    switch(_mState)
    {
        case STATE_SEND_PACKET:
        {
            Serial1.println("A");
            this->_mError = INVALID_TIME;

            // Change back to the fast update time
            this->_mUpdateTimer.changePeriodFromISR(20);

            // Prepare the package
            memset(&this->_mPacket, 0x00, sizeof(NTP::NTP_Packet));
            this->_mPacket.VN = NTP_VERSION & 0x07;
            this->_mPacket.Mode = CLIENT;

            // Request a new package from the server
            this->_mClient.begin(this->_mPort);
            this->_mClient.beginPacket(this->_mServer, this->_mPort);
            this->_mClient.write((const uint8_t*)&this->_mPacket, sizeof(NTP::NTP_Packet));
            this->_mClient.endPacket();

            this->_mSendTime = millis();

            this->_mState = STATE_WAIT_RESPONSE;

            break;
        }
        case STATE_WAIT_RESPONSE:
        {
            if(!this->_mClient.parsePacket())
            {
                if(millis() > (this->_mSendTime + this->_mTimeout))
                {
                    this->_mState = STATE_TIMEOUT;
                }

                return;
            }

            this->_mClient.read((unsigned char*)&this->_mPacket, sizeof(NTP::NTP_Packet));
            this->_mClient.stop();

            this->_mRecTime = millis();

            this->_mState = STATE_GET_TIME;

            break;
        }
        case STATE_GET_TIME:
        {
            Serial1.println("C");
            // Swap the endianess of the 32 bit fields
            for(uint8_t i = 0x01; i < (sizeof(NTP::NTP_Packet) / 0x04); i++)
            {
                *(((uint32_t*)(&this->_mPacket)) + i) = __SWAP32__(*(((uint32_t*)(&this->_mPacket)) + i));
            }

            if(this->_mPacket.Strat == 0x00)
            {
                this->_mState = STATE_TIMEOUT;

                return;
            }

            uint32_t ServerPoll = this->_power(2, this->_mPacket.Poll);
            if((this->_mUpdateTime / 60) < ServerPoll)
            {
                this->_mUpdateTime = ServerPoll;
            }

            // Ignore the time before the 01/01/1970
            this->_mPacket.TransmitTimestamp_S -= 2208988800UL;

            // Get the milliseconds
            this->_mMillis = (uint32_t)(((double)this->_mPacket.TransmitTimestamp_F) / 0xFFFFFFFF * 1000);

            // Add the routing delay for the communication with the server
            this->_mMillis += this->_mRecTime - this->_mSendTime;
            if(this->_mMillis >= 1000)
            {
                this->_mMillis -= 1000;
                this->_mPacket.TransmitTimestamp_S++;
            }

            this->_mError = NO_ERROR;
    
            this->_mState = STATE_WAIT;

            break;
        }
        case STATE_WAIT:
        {
            Serial1.println("D");
            this->_mUpdateTimer.changePeriodFromISR(this->_mUpdateTime);
            Serial1.println(this->_mUpdateTime);
/*
            if(this->_mCallback)
            {
                this->_mCallback(this->_mPacket.TransmitTimestamp_S, this->_mMillis, (NTP::Leap)this->_mPacket.LI);
            }
*/
            this->_mState = STATE_SEND_PACKET;

            break;
        }
        case STATE_TIMEOUT:
        {
            Serial1.println("E");
            this->_mClient.stop();
            this->_mUpdateTimer.stop();

            this->_mError = TIMEOUT;

            break;
        }
    }
}

uint32_t NTP::_power(uint8_t Base, uint8_t Exp)
{
    uint32_t Result = 0x01;

    for(uint8_t i = 0x00; i < Exp; i++)
    {
        Result *= Base;
    }

    return Result;
 }

This will produce the following output:

A
C
D
120000

Now the hard fault (SOS - 1x Blink) occur and the particle resets.
So what can produce this kind of error?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.