@@ -1078,8 +1078,6 @@ func compileUploadSketch(
10781078 arduinoApp * app.ArduinoApp ,
10791079 w io.Writer ,
10801080) error {
1081- const fqbn = "arduino:zephyr:unoq"
1082-
10831081 logrus .SetLevel (logrus .ErrorLevel ) // Reduce the log level of arduino-cli
10841082 srv := commands .NewArduinoCoreServer ()
10851083
@@ -1101,6 +1099,9 @@ func compileUploadSketch(
11011099 }
11021100 sketch := sketchResp .GetSketch ()
11031101 profile := sketch .GetDefaultProfile ().GetName ()
1102+ if profile == "" {
1103+ return fmt .Errorf ("sketch %q has no default profile" , sketchPath )
1104+ }
11041105 initReq := & rpc.InitRequest {
11051106 Instance : inst ,
11061107 SketchPath : sketchPath ,
@@ -1140,18 +1141,13 @@ func compileUploadSketch(
11401141
11411142 // build the sketch
11421143 server , getCompileResult := commands .CompilerServerToStreams (ctx , w , w , nil )
1143-
1144- // TODO: add build cache
11451144 compileReq := rpc.CompileRequest {
11461145 Instance : inst ,
1147- Fqbn : fqbn ,
1146+ Fqbn : "arduino:zephyr:unoq" ,
11481147 SketchPath : sketchPath ,
11491148 BuildPath : buildPath ,
11501149 Jobs : 2 ,
11511150 }
1152- if profile == "" {
1153- compileReq .Libraries = []string {sketchPath + "/../../sketch-libraries" }
1154- }
11551151
11561152 err = srv .Compile (& compileReq , server )
11571153 if err != nil {
@@ -1174,12 +1170,62 @@ func compileUploadSketch(
11741170 slog .Info ("Used library " + lib .GetName () + " (" + lib .GetVersion () + ") in " + lib .GetInstallDir ())
11751171 }
11761172
1173+ if err := uploadSketchInRam (ctx , w , srv , inst , sketchPath , buildPath ); err != nil {
1174+ slog .Warn ("failed to upload in ram mode, trying to configure the board in ram mode, and retry" , slog .String ("error" , err .Error ()))
1175+ if err := configureMicroInRamMode (ctx , w , srv , inst ); err != nil {
1176+ return err
1177+ }
1178+ return uploadSketchInRam (ctx , w , srv , inst , sketchPath , buildPath )
1179+ }
1180+ return nil
1181+ }
1182+
1183+ func uploadSketchInRam (ctx context.Context ,
1184+ w io.Writer ,
1185+ srv rpc.ArduinoCoreServiceServer ,
1186+ inst * rpc.Instance ,
1187+ sketchPath string ,
1188+ buildPath string ,
1189+ ) error {
11771190 stream , _ := commands .UploadToServerStreams (ctx , w , w )
1178- return srv .Upload (& rpc.UploadRequest {
1191+ if err := srv .Upload (& rpc.UploadRequest {
11791192 Instance : inst ,
1180- Fqbn : fqbn ,
1193+ Fqbn : "arduino:zephyr:unoq:flash_mode=ram" ,
11811194 SketchPath : sketchPath ,
11821195 ImportDir : buildPath ,
1196+ }, stream ); err != nil {
1197+ return err
1198+ }
1199+ return nil
1200+ }
1201+
1202+ // configureMicroInRamMode uploads an empty binary overing any sketch previously uploaded in flash.
1203+ // This is required to be able to upload sketches in ram mode after if there is already a sketch in flash.
1204+ func configureMicroInRamMode (
1205+ ctx context.Context ,
1206+ w io.Writer ,
1207+ srv rpc.ArduinoCoreServiceServer ,
1208+ inst * rpc.Instance ,
1209+ ) error {
1210+ zeros , err := os .Open ("/dev/zero" )
1211+ if err != nil {
1212+ return err
1213+ }
1214+ defer zeros .Close ()
1215+ empty , err := os .Create ("/tmp/empty.elf-zsk.bin" ) //nolint:gosec
1216+ if err != nil {
1217+ return err
1218+ }
1219+ defer empty .Close ()
1220+ if _ , err := io .CopyN (empty , zeros , 50 ); err != nil {
1221+ return err
1222+ }
1223+
1224+ stream , _ := commands .UploadToServerStreams (ctx , w , w )
1225+ return srv .Upload (& rpc.UploadRequest {
1226+ Instance : inst ,
1227+ Fqbn : "arduino:zephyr:unoq:flash_mode=flash" ,
1228+ ImportFile : "/tmp/empty" ,
11831229 }, stream )
11841230}
11851231
0 commit comments