This article presents a feedforward analytical solution of Field-Weakening (FW) for Surface-Mounted Permanent Magnets Synchronous Motor (SPMSM) using Karush-Kuhn-Tucker (KKT) optimality conditions. The generated current references take into consideration voltage and current constraints, load torque, and speed references. The formulated optimization problem helps ensure that the generated references stays within the feasible domain given by the constraints. Furthermore, the solution given by the KKT is implemented in real-time using a low cost rapid control prototyping microcontroller (ATSAME54P20A) without the use of look-up tables.Index Terms-Flux-weakening control, Karush-Kuhn-Tucker (KKT), surface-mounted permanent magnet synchronous motor (SPMSM), real-time control, rapid control prototyping.