코드 정리
This commit is contained in:
@@ -133,38 +133,34 @@ static void fds_evt_handler( fds_evt_t const *p_evt )
|
|||||||
{
|
{
|
||||||
fds_last_evt = p_evt->id;
|
fds_last_evt = p_evt->id;
|
||||||
|
|
||||||
switch( p_evt->id )
|
switch (p_evt->id)
|
||||||
{
|
{
|
||||||
case FDS_EVT_INIT:
|
case FDS_EVT_INIT:
|
||||||
if( p_evt->result == NRF_SUCCESS )
|
if (p_evt->result == NRF_SUCCESS)
|
||||||
{
|
{
|
||||||
m_fds_initialized = true;
|
m_fds_initialized = true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FDS_EVT_WRITE:
|
case FDS_EVT_WRITE:
|
||||||
{
|
fds_flag_write = false;
|
||||||
fds_flag_write = false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FDS_EVT_UPDATE:
|
case FDS_EVT_UPDATE:
|
||||||
{
|
fds_flag_write = false;
|
||||||
fds_flag_write = false;
|
|
||||||
|
|
||||||
if(go_device_power_off == true)
|
if (go_device_power_off == true)
|
||||||
{
|
{
|
||||||
device_power_off();
|
device_power_off();
|
||||||
}
|
}
|
||||||
if(go_sleep_mode_enter == true)
|
if (go_sleep_mode_enter == true)
|
||||||
{
|
{
|
||||||
sleep_mode_enter();
|
sleep_mode_enter();
|
||||||
}
|
}
|
||||||
if(go_NVIC_SystemReset == true)
|
if (go_NVIC_SystemReset == true)
|
||||||
{
|
{
|
||||||
DBG_PRINTF("Off FDS_EVENT\r\n");
|
DBG_PRINTF("Off FDS_EVENT\r\n");
|
||||||
NVIC_SystemReset();
|
NVIC_SystemReset();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -191,7 +187,7 @@ static void fds_evt_handler( fds_evt_t const *p_evt )
|
|||||||
static void wait_for_fds_ready( void )
|
static void wait_for_fds_ready( void )
|
||||||
{
|
{
|
||||||
uint32_t timeout = 0;
|
uint32_t timeout = 0;
|
||||||
while( !m_fds_initialized )
|
while(!m_fds_initialized)
|
||||||
{
|
{
|
||||||
nrf_pwr_mgmt_run();
|
nrf_pwr_mgmt_run();
|
||||||
nrf_delay_ms(1);
|
nrf_delay_ms(1);
|
||||||
@@ -240,7 +236,7 @@ void config_load( void )
|
|||||||
goto cfg_load_start;
|
goto cfg_load_start;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( rc == NRF_SUCCESS )
|
if (rc == NRF_SUCCESS)
|
||||||
{
|
{
|
||||||
fds_flash_record_t config = { 0 };
|
fds_flash_record_t config = { 0 };
|
||||||
|
|
||||||
@@ -262,7 +258,7 @@ void config_load( void )
|
|||||||
|
|
||||||
DBG_PRINTF("[FDS] magic=0x%08X (expect 0x%08X)\r\n", m_config.magic_number, CONFIG_MAGIC_NUMBER_VALUE);
|
DBG_PRINTF("[FDS] magic=0x%08X (expect 0x%08X)\r\n", m_config.magic_number, CONFIG_MAGIC_NUMBER_VALUE);
|
||||||
|
|
||||||
if( m_config.magic_number != (uint32_t)CONFIG_MAGIC_NUMBER_VALUE )
|
if (m_config.magic_number != (uint32_t)CONFIG_MAGIC_NUMBER_VALUE)
|
||||||
{
|
{
|
||||||
DBG_PRINTF("[FDS] FORMAT! overwriting with defaults\r\n");
|
DBG_PRINTF("[FDS] FORMAT! overwriting with defaults\r\n");
|
||||||
rc = fds_record_delete(&desc);
|
rc = fds_record_delete(&desc);
|
||||||
@@ -272,7 +268,7 @@ void config_load( void )
|
|||||||
fds_default_value_set();
|
fds_default_value_set();
|
||||||
|
|
||||||
rc = fds_record_update(&desc, &m_dummy_record);
|
rc = fds_record_update(&desc, &m_dummy_record);
|
||||||
if( (rc != NRF_SUCCESS) && (rc == FDS_ERR_NO_SPACE_IN_FLASH) )
|
if ((rc != NRF_SUCCESS) && (rc == FDS_ERR_NO_SPACE_IN_FLASH))
|
||||||
{
|
{
|
||||||
rc = fds_gc();
|
rc = fds_gc();
|
||||||
APP_ERROR_CHECK(rc);
|
APP_ERROR_CHECK(rc);
|
||||||
@@ -304,20 +300,20 @@ void config_load( void )
|
|||||||
|
|
||||||
fds_wait_cnt = 0;
|
fds_wait_cnt = 0;
|
||||||
|
|
||||||
while(fds_flag_write && fds_wait_cnt < 3000) /* 3 second timeout */
|
while (fds_flag_write && fds_wait_cnt < 3000) /* 3 second timeout */
|
||||||
{
|
{
|
||||||
nrf_pwr_mgmt_run();
|
nrf_pwr_mgmt_run();
|
||||||
nrf_delay_ms(1);
|
nrf_delay_ms(1);
|
||||||
fds_wait_cnt++;
|
fds_wait_cnt++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(fds_flag_write)
|
if (fds_flag_write)
|
||||||
{
|
{
|
||||||
DBG_PRINTF("[FDS] write TIMEOUT! forcing flag clear\r\n");
|
DBG_PRINTF("[FDS] write TIMEOUT! forcing flag clear\r\n");
|
||||||
fds_flag_write = false;
|
fds_flag_write = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( (rc != NRF_SUCCESS) && (rc == FDS_ERR_NO_SPACE_IN_FLASH) )
|
if ((rc != NRF_SUCCESS) && (rc == FDS_ERR_NO_SPACE_IN_FLASH))
|
||||||
{
|
{
|
||||||
rc = fds_gc();
|
rc = fds_gc();
|
||||||
APP_ERROR_CHECK(rc);
|
APP_ERROR_CHECK(rc);
|
||||||
@@ -373,7 +369,7 @@ void config_save( void )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if( m_config.magic_number != (uint32_t)CONFIG_MAGIC_NUMBER_VALUE )
|
if (m_config.magic_number != (uint32_t)CONFIG_MAGIC_NUMBER_VALUE)
|
||||||
{
|
{
|
||||||
m_config.magic_number = CONFIG_MAGIC_NUMBER_VALUE;
|
m_config.magic_number = CONFIG_MAGIC_NUMBER_VALUE;
|
||||||
}
|
}
|
||||||
@@ -383,13 +379,13 @@ void config_save( void )
|
|||||||
rc = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &tok);
|
rc = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &tok);
|
||||||
DBG_PRINTF("[CFG_SAVE] find rc=%u\r\n", rc);
|
DBG_PRINTF("[CFG_SAVE] find rc=%u\r\n", rc);
|
||||||
|
|
||||||
if( rc == NRF_SUCCESS )
|
if (rc == NRF_SUCCESS)
|
||||||
{
|
{
|
||||||
fds_flag_write = true;
|
fds_flag_write = true;
|
||||||
rc = fds_record_update(&desc, &m_dummy_record);
|
rc = fds_record_update(&desc, &m_dummy_record);
|
||||||
DBG_PRINTF("[CFG_SAVE] update rc=%u\r\n", rc);
|
DBG_PRINTF("[CFG_SAVE] update rc=%u\r\n", rc);
|
||||||
|
|
||||||
if( rc == FDS_ERR_NO_SPACE_IN_FLASH )
|
if (rc == FDS_ERR_NO_SPACE_IN_FLASH)
|
||||||
{
|
{
|
||||||
fds_flag_write = false;
|
fds_flag_write = false;
|
||||||
rc = fds_gc();
|
rc = fds_gc();
|
||||||
@@ -399,7 +395,7 @@ void config_save( void )
|
|||||||
DBG_PRINTF("[CFG_SAVE] retry rc=%u\r\n", rc);
|
DBG_PRINTF("[CFG_SAVE] retry rc=%u\r\n", rc);
|
||||||
}
|
}
|
||||||
|
|
||||||
if( rc != NRF_SUCCESS )
|
if (rc != NRF_SUCCESS)
|
||||||
{
|
{
|
||||||
DBG_PRINTF("[CFG_SAVE] FAIL rc=%u\r\n", rc);
|
DBG_PRINTF("[CFG_SAVE] FAIL rc=%u\r\n", rc);
|
||||||
fds_flag_write = false;
|
fds_flag_write = false;
|
||||||
@@ -412,7 +408,7 @@ void config_save( void )
|
|||||||
rc = fds_record_write(&desc, &m_dummy_record);
|
rc = fds_record_write(&desc, &m_dummy_record);
|
||||||
DBG_PRINTF("[CFG_SAVE] write rc=%u\r\n", rc);
|
DBG_PRINTF("[CFG_SAVE] write rc=%u\r\n", rc);
|
||||||
|
|
||||||
if( rc != NRF_SUCCESS )
|
if ( rc != NRF_SUCCESS )
|
||||||
{
|
{
|
||||||
DBG_PRINTF("[CFG_SAVE] FAIL rc=%u\r\n", rc);
|
DBG_PRINTF("[CFG_SAVE] FAIL rc=%u\r\n", rc);
|
||||||
fds_flag_write = false;
|
fds_flag_write = false;
|
||||||
|
|||||||
@@ -39,10 +39,12 @@ static void twi_uninitialize(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Initialise the TWI peripheral (SCL/SDA pins, 400 kHz, blocking mode). */
|
/* Initialise the TWI peripheral (SCL/SDA pins, 400 kHz, blocking mode). */
|
||||||
static void twi_initialize(void){
|
static void twi_initialize(void)
|
||||||
|
{
|
||||||
ret_code_t err_code;
|
ret_code_t err_code;
|
||||||
|
|
||||||
const nrfx_twi_config_t twi_config = {
|
const nrfx_twi_config_t twi_config =
|
||||||
|
{
|
||||||
.scl = ICM42670_I2C_SCL_PIN,
|
.scl = ICM42670_I2C_SCL_PIN,
|
||||||
.sda = ICM42670_I2C_SDA_PIN,
|
.sda = ICM42670_I2C_SDA_PIN,
|
||||||
.frequency = NRF_TWI_FREQ_400K,
|
.frequency = NRF_TWI_FREQ_400K,
|
||||||
|
|||||||
@@ -99,8 +99,8 @@ extern bool motion_data_once;
|
|||||||
*============================================================================*/
|
*============================================================================*/
|
||||||
void safety_check_complete(float temp_c)
|
void safety_check_complete(float temp_c)
|
||||||
{
|
{
|
||||||
DBG_PRINTF("[SAFETY] Batt=%d mV, Temp=%d.%d C\r\n",
|
//DBG_PRINTF("[SAFETY] Batt=%d mV, Temp=%d.%d C\r\n",
|
||||||
(int)safety_batt_mv, (int)temp_c, ((int)(temp_c * 10)) % 10);
|
// (int)safety_batt_mv, (int)temp_c, ((int)(temp_c * 10)) % 10);
|
||||||
|
|
||||||
/* Battery check */
|
/* Battery check */
|
||||||
if (safety_batt_mv <= LOW_BATTERY_VOLTAGE)
|
if (safety_batt_mv <= LOW_BATTERY_VOLTAGE)
|
||||||
|
|||||||
@@ -74,7 +74,8 @@ int device_sleep_mode(void)
|
|||||||
*
|
*
|
||||||
* @return 0 (always succeeds)
|
* @return 0 (always succeeds)
|
||||||
*/
|
*/
|
||||||
int device_activated(void){
|
int device_activated(void)
|
||||||
|
{
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
p_order = 0; /* State machine start step (Step 0: I2C init) */
|
p_order = 0; /* State machine start step (Step 0: I2C init) */
|
||||||
lock_check =true; /* Lock: power sequence in progress */
|
lock_check =true; /* Lock: power sequence in progress */
|
||||||
@@ -117,10 +118,13 @@ void power_loop(void *p_context)
|
|||||||
|
|
||||||
/* Advance to next step or finish */
|
/* Advance to next step or finish */
|
||||||
/* Advance to next step or finish sequence */
|
/* Advance to next step or finish sequence */
|
||||||
if (p_order < 2) {
|
if (p_order < 2)
|
||||||
|
{
|
||||||
p_order++; /* Move to next step */
|
p_order++; /* Move to next step */
|
||||||
power_timer_start(); /* Execute next step after 20ms */
|
power_timer_start(); /* Execute next step after 20ms */
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
/* Power sequence fully complete */
|
/* Power sequence fully complete */
|
||||||
DBG_PRINTF("[PWR] Device Activated OK!\r\n");
|
DBG_PRINTF("[PWR] Device Activated OK!\r\n");
|
||||||
}
|
}
|
||||||
@@ -135,7 +139,8 @@ void power_loop(void *p_context)
|
|||||||
*
|
*
|
||||||
* @return 0 (always succeeds)
|
* @return 0 (always succeeds)
|
||||||
*/
|
*/
|
||||||
int device_reactivated(void){
|
int device_reactivated(void)
|
||||||
|
{
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
sw_i2c_init_once(); /* Re-initialize I2C bus */
|
sw_i2c_init_once(); /* Re-initialize I2C bus */
|
||||||
nrf_delay_ms(10); /* Stabilization delay */
|
nrf_delay_ms(10); /* Stabilization delay */
|
||||||
|
|||||||
Reference in New Issue
Block a user