tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/semihosting/semihosting.go (about)

     1  // Package semihosting implements parts of the ARM semihosting specification,
     2  // for communicating over a debug connection.
     3  //
     4  // If you want to use it in OpenOCD, you have to enable it first with the
     5  // following command:
     6  //
     7  //	arm semihosting enable
     8  package semihosting
     9  
    10  import (
    11  	"device/arm"
    12  	"unsafe"
    13  )
    14  
    15  // IOError is returned by I/O operations when they fail.
    16  type IOError struct {
    17  	BytesWritten int
    18  }
    19  
    20  func (e *IOError) Error() string {
    21  	return "semihosting: I/O error"
    22  }
    23  
    24  // Write writes the given data to the given file descriptor. It returns an
    25  // *IOError if the write was not successful.
    26  func Write(fd uintptr, data []byte) error {
    27  	if len(data) == 0 {
    28  		return nil
    29  	}
    30  	params := struct {
    31  		fd   uintptr
    32  		data unsafe.Pointer
    33  		len  int
    34  	}{
    35  		fd:   fd,
    36  		data: unsafe.Pointer(&data[0]),
    37  		len:  len(data),
    38  	}
    39  	unwritten := arm.SemihostingCall(arm.SemihostingWrite, uintptr(unsafe.Pointer(&params)))
    40  	if unwritten != 0 {
    41  		// Error: unwritten is the number of bytes not written.
    42  		return &IOError{
    43  			BytesWritten: len(data) - unwritten,
    44  		}
    45  	}
    46  	return nil
    47  }