diff options
author | Alex <colomar.6.4.3@gmail.com> | 2019-02-01 16:24:17 +0000 |
---|---|---|
committer | Alex <colomar.6.4.3@gmail.com> | 2019-02-01 16:24:17 +0000 |
commit | 13d27041fa73f96e6a52f9b4347104410d1c67e5 (patch) | |
tree | 5d71fa608519c0d92598225c33cfabc6e69ed769 | |
parent | d388c340da55621ed284811b1c89a601248be9c1 (diff) |
Improve style
-rwxr-xr-x | src/actuators.c | 128 | ||||
-rwxr-xr-x | src/ctrl.c | 133 | ||||
-rwxr-xr-x | src/main.c | 38 | ||||
m--------- | stm32l4-modules | 0 |
4 files changed, 136 insertions, 163 deletions
diff --git a/src/actuators.c b/src/actuators.c index 5d65a64..dcb64d0 100755 --- a/src/actuators.c +++ b/src/actuators.c @@ -77,6 +77,7 @@ static float yaw; static int modules_init (void); static int modules_deinit (void); static int proc_actuators_it (void); +static int proc_actuators_ev (void); static int proc_actuators_delay (void); static int proc_ref_read (void); static int proc_actuators_set (void); @@ -93,22 +94,18 @@ static int proc_actuators_set (void); int proc_actuators_init (void) { - if (init_pending) { - init_pending = false; - } else { + if (!init_pending) return ERROR_OK; - } - if (modules_init()) { - goto err_mod; - } + if (modules_init()) + goto err; - return ERROR_OK; + init_pending = false; + return ERROR_OK; -err_mod: - init_pending = true; +err: return ERROR_NOK; } @@ -121,17 +118,15 @@ int proc_actuators_deinit (void) { int status; - status = ERROR_OK; - - if (!init_pending) { - init_pending = true; - } else { + if (init_pending) return status; - } - if (modules_deinit()) { + init_pending = true; + + status = ERROR_OK; + + if (modules_deinit()) status = ERROR_NOK; - } return status; } @@ -145,26 +140,20 @@ int proc_actuators (void) { if (init_pending) { - if (proc_actuators_init()) { + if (proc_actuators_init()) return ERROR_NOK; - } } switch (PROJECT_ACT_TASKING) { case PROJECT_ACT_DEL: - if (proc_actuators_it()) { - return ERROR_NOK; - } - break; + return proc_actuators_delay(); case PROJECT_ACT_IT: + return proc_actuators_it(); case PROJECT_ACT_EV: - if (proc_actuators_delay()) { - return ERROR_NOK; - } - break; + return proc_actuators_ev(); + default: + return ERROR_NOK; } - - return ERROR_OK; } @@ -176,21 +165,17 @@ static int modules_init (void) led_init(); button_init(); - if (delay_us_init()) { + if (delay_us_init()) goto err_delay; - } - if (can_init()) { + if (can_init()) goto err_can; - } - if (servo_init()) { + if (servo_init()) goto err_servo; - } if (PROJECT_ACT_TASKING != PROJECT_ACT_DEL) { - if (tim_it_init(ACT_REFRESH_PERIOD_US)) { + if (tim_it_init(ACT_REFRESH_PERIOD_US)) goto err_tim; - } } return ERROR_OK; @@ -215,18 +200,14 @@ static int modules_deinit (void) status = ERROR_OK; - if (servo_deinit()) { + if (servo_deinit()) status = ERROR_NOK; - } - if (can_deinit()) { + if (can_deinit()) status = ERROR_NOK; - } - if (tim_it_deinit()) { + if (tim_it_deinit()) status = ERROR_NOK; - } - if (delay_us_deinit()) { + if (delay_us_deinit()) status = ERROR_NOK; - } button_deinit(); led_deinit(); @@ -237,20 +218,33 @@ static int proc_actuators_it (void) { while (true) { - switch (PROJECT_ACT_TASKING) { - case PROJECT_ACT_IT: - __WFI(); - break; - case PROJECT_ACT_EV: - __WFE(); - break; + __WFI(); + + if (*tim_it_timx_interrupt_ptr) { + if (!proc_ref_read()) { + if (proc_actuators_set()) + return ERROR_NOK; + } + + *tim_it_timx_interrupt_ptr = false; + } else if (button_interrupt) { + led_toggle(); + + button_interrupt = false; } + } +} + +static int proc_actuators_ev (void) +{ + + while (true) { + __WFE(); if (*tim_it_timx_interrupt_ptr) { if (!proc_ref_read()) { - if (proc_actuators_set()) { + if (proc_actuators_set()) return ERROR_NOK; - } } *tim_it_timx_interrupt_ptr = false; @@ -269,9 +263,8 @@ static int proc_actuators_delay (void) delay_us(ACT_REFRESH_PERIOD_US); if (!proc_ref_read()) { - if (proc_actuators_set()) { + if (proc_actuators_set()) return ERROR_NOK; - } } } } @@ -280,9 +273,8 @@ static int proc_ref_read (void) { int8_t plane_pos [CAN_DATA_LEN]; - if (can_msg_read((uint8_t *)plane_pos)) { + if (can_msg_read((uint8_t *)plane_pos)) return ERROR_NOK; - } pitch = plane_pos[0]; roll = plane_pos[1]; @@ -296,33 +288,27 @@ static int proc_actuators_set (void) float tmp; tmp = alx_scale_linear_f(pitch, -40, 40, -90, 90); - if (servo_position_set(SERVO_S1, tmp)) { + if (servo_position_set(SERVO_S1, tmp)) return ERROR_NOK; - } tmp = alx_scale_linear_f(roll, -35, 35, -90, -20); - if (servo_position_set(SERVO_S2, tmp)) { + if (servo_position_set(SERVO_S2, tmp)) return ERROR_NOK; - } if (yaw > 0) { - if (servo_position_set(SERVO_S3, 90)) { + if (servo_position_set(SERVO_S3, 90)) return ERROR_NOK; - } tmp = alx_scale_linear_f(yaw, 0, 10, -90, 90); - if (servo_position_set(SERVO_S4, tmp)) { + if (servo_position_set(SERVO_S4, tmp)) return ERROR_NOK; - } } else { tmp = alx_scale_linear_f(yaw, -10, 0, -90, 90); - if (servo_position_set(SERVO_S3, tmp)) { + if (servo_position_set(SERVO_S3, tmp)) return ERROR_NOK; - } - if (servo_position_set(SERVO_S4, -90)) { + if (servo_position_set(SERVO_S4, -90)) return ERROR_NOK; - } } return ERROR_OK; @@ -81,6 +81,7 @@ static bool level; static int modules_init (void); static int modules_deinit (void); static int proc_ctrl_it (void); +static int proc_ctrl_ev (void); static int proc_ctrl_delay (void); static int proc_ctrl_read (void); static int proc_ctrl_report (void); @@ -97,24 +98,19 @@ static int proc_ctrl_report (void); int proc_ctrl_init (void) { - if (init_pending) { - init_pending = false; - } else { + if (!init_pending) return ERROR_OK; - } - if (modules_init()) { + if (modules_init()) goto err_mod; - } - level = true; + init_pending = false; + return ERROR_OK; err_mod: - init_pending = true; - return ERROR_NOK; } @@ -127,17 +123,15 @@ int proc_ctrl_deinit (void) { int status; - status = ERROR_OK; + if (init_pending) + return ERROR_OK; - if (!init_pending) { - init_pending = true; - } else { - return status; - } + status = ERROR_OK; - if (modules_deinit()) { + if (modules_deinit()) status = ERROR_NOK; - } + + init_pending = true; return status; } @@ -151,23 +145,17 @@ int proc_ctrl (void) { if (init_pending) { - if (proc_ctrl_init()) { + if (proc_ctrl_init()) return ERROR_NOK; - } } switch (PROJECT_CTRL_TASKING) { case PROJECT_CTRL_DEL: - if (proc_ctrl_it()) { - return ERROR_NOK; - } - break; + return proc_ctrl_delay(); case PROJECT_CTRL_IT: + return proc_ctrl_it(); case PROJECT_CTRL_EV: - if (proc_ctrl_delay()) { - return ERROR_NOK; - } - break; + return proc_ctrl_ev(); } return ERROR_OK; @@ -181,21 +169,16 @@ static int modules_init (void) { led_init(); - if (delay_us_init()) { + if (delay_us_init()) goto err_delay; - } - - if (can_init()) { + if (can_init()) goto err_can; - } - if (nunchuk_init()) { + if (nunchuk_init()) goto err_nunchuk; - } if (PROJECT_CTRL_TASKING != PROJECT_CTRL_DEL) { - if (tim_it_init(CTRL_REFRESH_PERIOD_US)) { + if (tim_it_init(CTRL_REFRESH_PERIOD_US)) goto err_tim; - } } return ERROR_OK; @@ -219,21 +202,16 @@ static int modules_deinit (void) status = ERROR_OK; - if (nunchuk_deinit()) { + if (nunchuk_deinit()) status = ERROR_NOK; - } - if (can_deinit()) { + if (can_deinit()) status = ERROR_NOK; - } - if (tim_it_deinit()) { + if (tim_it_deinit()) status = ERROR_NOK; - } - if (delay_us_deinit()) { + if (delay_us_deinit()) status = ERROR_NOK; - } led_deinit(); - return status; } @@ -241,22 +219,32 @@ static int proc_ctrl_it (void) { while (true) { - switch (PROJECT_CTRL_TASKING) { - case PROJECT_CTRL_IT: - __WFI(); - break; - case PROJECT_CTRL_EV: - __WFE(); - break; + __WFI(); + + if (*tim_it_timx_interrupt_ptr) { + if (proc_ctrl_read()) + return ERROR_NOK; + + if (proc_ctrl_report()) + return ERROR_NOK; + + *tim_it_timx_interrupt_ptr = false; } + } +} + +static int proc_ctrl_ev (void) +{ + + while (true) { + __WFI(); if (*tim_it_timx_interrupt_ptr) { - if (proc_ctrl_read()) { + if (proc_ctrl_read()) return ERROR_NOK; - } - if (proc_ctrl_report()) { + + if (proc_ctrl_report()) return ERROR_NOK; - } *tim_it_timx_interrupt_ptr = false; } @@ -269,12 +257,10 @@ static int proc_ctrl_delay (void) while (true) { delay_us(CTRL_REFRESH_PERIOD_US); - if (proc_ctrl_read()) { + if (proc_ctrl_read()) return ERROR_NOK; - } - if (proc_ctrl_report()) { + if (proc_ctrl_report()) return ERROR_NOK; - } } } @@ -283,9 +269,8 @@ static int proc_ctrl_read (void) Nunchuk_Data_s nunchuk; float tmp; - if (nunchuk_read(&nunchuk)) { + if (nunchuk_read(&nunchuk)) return ERROR_NOK; - } tmp = nunchuk.jst.y; pitch = -alx_scale_linear_f(tmp, 0, UINT8_MAX, -40, 40); @@ -314,34 +299,30 @@ static int proc_ctrl_report (void) } else { led_reset(); - if (pitch > INT8_MAX) { + if (pitch > INT8_MAX) plane_pos[0] = INT8_MAX; - } else if (pitch < INT8_MIN) { + else if (pitch < INT8_MIN) plane_pos[0] = INT8_MIN; - } else { + else plane_pos[0] = (int8_t)pitch; - } - if (roll > INT8_MAX) { + if (roll > INT8_MAX) plane_pos[1] = INT8_MAX; - } else if (roll < INT8_MIN) { + else if (roll < INT8_MIN) plane_pos[1] = INT8_MIN; - } else { + else plane_pos[1] = (int8_t)roll; - } - if (yaw > INT8_MAX) { + if (yaw > INT8_MAX) plane_pos[2] = INT8_MAX; - } else if (yaw < INT8_MIN) { + else if (yaw < INT8_MIN) plane_pos[2] = INT8_MIN; - } else { + else plane_pos[2] = (int8_t)yaw; - } } - if (can_msg_write((uint8_t *)plane_pos)) { + if (can_msg_write((uint8_t *)plane_pos)) return ERROR_NOK; - } return ERROR_OK; } @@ -48,11 +48,16 @@ /****************************************************************************** ******* macros *************************************************************** ******************************************************************************/ -#define DEBUG_MODE (true) +#define DEBUG_MODE (false) +/* Can be a value of <enum Project_SysClk_Source> */ #define PROJECT_SYSCLK_SOURCE (PROJECT_SYSCLK_FROM_PLL_MSI) -#define PROJECT_COMPONENT (PROJECT_COMPONENT_OFF) +/* Can be a value of <enum Project_Component> */ +#define PROJECT_COMPONENT (PROJECT_COMPONENT_CTRL) + +/* Can be a value of <enum Stuck_4ever_LED_State> */ +#define STUCK_4EVER_LED_STATE (STUCK_4EVER_LED_ON) /****************************************************************************** @@ -71,6 +76,11 @@ PROJECT_COMPONENT_ACT }; + enum Stuck_4ever_LED_State { + STUCK_4EVER_LED_OFF = false, + STUCK_4EVER_LED_ON = true + }; + /****************************************************************************** ******* structs ************************************************************** @@ -101,29 +111,25 @@ noreturn int main (void) { HAL_Init(); - if (sysclk_config()) { + if (sysclk_config()) goto err_clk; - } if (DEBUG_MODE) { - if (test()) { + if (test()) goto err; - } } - if (proc_init()) { + if (proc_init()) goto err; - } - if (proc()) { + if (proc()) goto err; - } err: prj_error_handle(); err_clk: - stuck_forever(false); + stuck_forever(STUCK_4EVER_LED_STATE); } @@ -142,6 +148,8 @@ static int sysclk_config (void) return sysclk_config_pll_hsi(); case PROJECT_SYSCLK_FROM_PLL_MSI: return sysclk_config_pll_msi(); + default: + return ERROR_NOK; } } @@ -178,15 +186,13 @@ static int proc (void) static noreturn void stuck_forever (bool led) { - if (led) { + if (led) led_set(); - } else { + else led_reset(); - } - while (true) { + while (true) __WFI(); - } } diff --git a/stm32l4-modules b/stm32l4-modules -Subproject 4381e1c9b157d4ebfcf71b7cfff2df6efbcfd2b +Subproject b79f4ce88568eca1b1c089d9c3d8b86bbcab8f6 |