| 1 | /* |
| 2 | * File: ert_main.c |
| 3 | * |
| 4 | * Code generated for Simulink model 'PMSM_Controller'. |
| 5 | * |
| 6 | * Model version : 1.885 |
| 7 | * Simulink Coder version : 9.4 (R2020b) 29-Jul-2020 |
| 8 | * C/C++ source code generated on : Sat Jan 8 15:44:11 2022 |
| 9 | * |
| 10 | * Target selection: ert.tlc |
| 11 | * Embedded hardware selection: ARM Compatible->ARM Cortex-M |
| 12 | * Code generation objectives: |
| 13 | * 1. Execution efficiency |
| 14 | * 2. RAM efficiency |
| 15 | * Validation result: Not run |
| 16 | */ |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | #include <stdio.h> /* This ert_main.c example uses printf/fflush */ |
| 20 | #include "PMSM_Controller.h" /* Model's header file */ |
| 21 | #include "rtwtypes.h" |
| 22 | |
| 23 | static RT_MODEL rtM_; |
| 24 | static RT_MODEL *const rtMPtr = &rtM_; /* Real-time model */ |
| 25 | static DW rtDW; /* Observable states */ |
| 26 | |
| 27 | /* '<Root>/Vdc' */ |
| 28 | static int16_T rtU_Vdc; |
| 29 | |
| 30 | /* '<Root>/Ts' */ |
| 31 | static uint16_T rtU_Ts; |
| 32 | |
| 33 | /* '<Root>/adc_a' */ |
| 34 | static int16_T rtU_adc_a; |
| 35 | |
| 36 | /* '<Root>/adc_b' */ |
| 37 | static int16_T rtU_adc_b; |
| 38 | |
| 39 | /* '<Root>/rotor_angle' */ |
| 40 | static real_T rtU_rotor_angle; |
| 41 | |
| 42 | /* '<Root>/rotor_speed' */ |
| 43 | static int16_T rtU_rotor_speed; |
| 44 | |
| 45 | /* '<Root>/input_target' */ |
| 46 | static int16_T rtU_input_target; |
| 47 | |
| 48 | /* '<Root>/hall_a' */ |
| 49 | static uint8_T rtU_hall_a; |
| 50 | |
| 51 | /* '<Root>/hall_b' */ |
| 52 | static uint8_T rtU_hall_b; |
| 53 | |
| 54 | /* '<Root>/hall_c' */ |
| 55 | static uint8_T rtU_hall_c; |
| 56 | |
| 57 | /* '<Root>/hw_count' */ |
| 58 | static uint32_T rtU_hw_count; |
| 59 | |
| 60 | /* '<Root>/b_motor_enable' */ |
| 61 | static boolean_T rtU_b_motor_enable; |
| 62 | |
| 63 | /* '<Root>/b_cruise_enable' */ |
| 64 | static boolean_T rtU_b_cruise_enable; |
| 65 | |
| 66 | /* '<Root>/n_ctrl_type' */ |
| 67 | static uint8_T rtU_n_ctrl_type; |
| 68 | |
| 69 | /* '<Root>/pwm_counts' */ |
| 70 | static uint16_T rtY_pwm_counts[8]; |
| 71 | |
| 72 | /* '<Root>/sector' */ |
| 73 | static uint8_T rtY_sector; |
| 74 | |
| 75 | /* |
| 76 | * Associating rt_OneStep with a real-time clock or interrupt service routine |
| 77 | * is what makes the generated code "real-time". The function rt_OneStep is |
| 78 | * always associated with the base rate of the model. Subrates are managed |
| 79 | * by the base rate from inside the generated code. Enabling/disabling |
| 80 | * interrupts and floating point context switches are target specific. This |
| 81 | * example code indicates where these should take place relative to executing |
| 82 | * the generated code step function. Overrun behavior should be tailored to |
| 83 | * your application needs. This example simply sets an error status in the |
| 84 | * real-time model and returns from rt_OneStep. |
| 85 | */ |
| 86 | void rt_OneStep(RT_MODEL *const rtM); |
| 87 | void rt_OneStep(RT_MODEL *const rtM) |
| 88 | { |
| 89 | static boolean_T OverrunFlag = false; |
| 90 | |
| 91 | /* Disable interrupts here */ |
| 92 | |
| 93 | /* Check for overrun */ |
| 94 | if (OverrunFlag) { |
| 95 | rtmSetErrorStatus(rtM, "Overrun"); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | OverrunFlag = true; |
| 100 | |
| 101 | /* Save FPU context here (if necessary) */ |
| 102 | /* Re-enable timer or interrupt here */ |
| 103 | /* Set model inputs here */ |
| 104 | |
| 105 | /* Step the model for base rate */ |
| 106 | PMSM_Controller_step(rtM, rtU_Vdc, rtU_Ts, rtU_adc_a, rtU_adc_b, |
| 107 | rtU_rotor_angle, rtU_rotor_speed, rtU_input_target, |
| 108 | rtY_pwm_counts, &rtY_sector); |
| 109 | |
| 110 | /* Get model outputs here */ |
| 111 | |
| 112 | /* Indicate task complete */ |
| 113 | OverrunFlag = false; |
| 114 | |
| 115 | /* Disable interrupts here */ |
| 116 | /* Restore FPU context here (if necessary) */ |
| 117 | /* Enable interrupts here */ |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * The example "main" function illustrates what is required by your |
| 122 | * application code to initialize, execute, and terminate the generated code. |
| 123 | * Attaching rt_OneStep to a real-time clock is target specific. This example |
| 124 | * illustrates how you do this relative to initializing the model. |
| 125 | */ |
| 126 | int_T main(int_T argc, const char *argv[]) |
| 127 | { |
| 128 | RT_MODEL *const rtM = rtMPtr; |
| 129 | |
| 130 | /* Unused arguments */ |
| 131 | (void)(argc); |
| 132 | (void)(argv); |
| 133 | |
| 134 | /* Pack model data into RTM */ |
| 135 | rtM->dwork = &rtDW; |
| 136 | |
| 137 | /* Initialize model */ |
| 138 | PMSM_Controller_initialize(rtM); |
| 139 | |
| 140 | /* Attach rt_OneStep to a timer or interrupt service routine with |
| 141 | * period 6.2E-6 seconds (the model's base sample time) here. The |
| 142 | * call syntax for rt_OneStep is |
| 143 | * |
| 144 | * rt_OneStep(rtM); |
| 145 | */ |
| 146 | printf("Warning: The simulation will run forever. " |
| 147 | "Generated ERT main won't simulate model step behavior. " |
| 148 | "To change this behavior select the 'MAT-file logging' option.\n"); |
| 149 | fflush((NULL)); |
| 150 | while (rtmGetErrorStatus(rtM) == (NULL)) { |
| 151 | /* Perform other application tasks here */ |
| 152 | } |
| 153 | |
| 154 | /* Disable rt_OneStep() here */ |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * File trailer for generated code. |
| 160 | * |
| 161 | * [EOF] |
| 162 | */ |
| 163 | |