This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Strange problem with Scheduler plus SPI

Hello everyone! Frist of all, sorry for my broken English.

I had a timeout handler, which call by application timer every 50ms. In this function I took data from accelerometer via SPI, this method doesn't work. As I know becouse than SPI doesn't want work in interrupt context. Ok, I have made flag in timeout handler, and take data from MAIN loop.

for (;;)
    {   
        power_manage();
        if (app_timer_50ms_timeout)
        {        
            lis3dh_readAcc_uint16(&acc_int_x, &acc_int_y, &acc_int_z);
            acc_int_x = acc_int_x/10;
            acc_int_y = acc_int_y/10;
            acc_int_z = acc_int_z/10;
            uint8_t send_message[1];
            sprintf((char*)send_message, "A%dB%dC%d\n", acc_int_x, acc_int_y, acc_int_z);
            NRF_LOG_PRINTF("%s", send_message);
            ble_nus_string_send(&m_nus, send_message, strlen((char*)send_message));
            app_timer_50ms_timeout = false;
        }
    }

And all worked. Take string via UART and NUS.

Now I add Scheduler for app timer, and see strange behavior. First, my timeout function

static void timer_a_handler(void * p_context)
{
        nrf_gpio_pin_toggle(LED_12);
        lis3dh_readAcc_int16(&acc_int_x, &acc_int_y, &acc_int_z);
        
        acc_int_x = acc_int_x/10;
        acc_int_y = acc_int_y/10;
        acc_int_z = acc_int_z/10;
        
        uint8_t send_message[1];
        sprintf((char*)send_message, "A%dB%dC%d\n", acc_int_x, acc_int_y, acc_int_z);
        NRF_LOG_PRINTF("%s", send_message);
        ble_nus_string_send(&m_nus, send_message, strlen((char*)send_message));   

}

In this state, all works fine! I recive data via UART and NUS. But when I touch accelerometer OR shake him my program stops! In debug i see HardFault.

Second, i started analyze code. Take some changes. Place code from my func lis3dh_readAcc_uint16(&acc_int_x, &acc_int_y, &acc_int_z); to timout function and result is:

static void timer_a_handler(void * p_context)
{
        nrf_gpio_pin_toggle(LED_12);
        // lis3dh_readAcc_int16(&acc_int_x, &acc_int_y, &acc_int_z);
        while(lis3dh_read(LIS3DH_STATUS_REG) & 0x8 == 0 );
        acc_int_x = lis3dh_read(LIS3DH_OUT_X_H) * 256 +  lis3dh_read(LIS3DH_OUT_X_L);
        acc_int_y = lis3dh_read(LIS3DH_OUT_Y_H) * 256 +  lis3dh_read(LIS3DH_OUT_Y_L);
        acc_int_z = lis3dh_read(LIS3DH_OUT_Z_H) * 256 +  lis3dh_read(LIS3DH_OUT_Z_L);
        acc_int_x = acc_int_x/10;
        acc_int_y = acc_int_y/10;
        acc_int_z = acc_int_z/10;
        
        uint8_t send_message[1];
        sprintf((char*)send_message, "A%dB%dC%d\n", acc_int_x, acc_int_y, acc_int_z);
        NRF_LOG_PRINTF("%s", send_message);
        ble_nus_string_send(&m_nus, send_message, strlen((char*)send_message)); 
}

In this case all work fine! No stops and HardFault when I shake or touch accelerometer chip.

And finaly, please help me to understand this strange behavior. I beginer in ARM MPU, I am hope for your help!

Related