github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/machine/machine_generic.go (about)

     1  //go:build !baremetal
     2  
     3  package machine
     4  
     5  // Dummy machine package that calls out to external functions.
     6  
     7  const deviceName = "generic"
     8  
     9  var (
    10  	USB = &UART{100}
    11  )
    12  
    13  // The Serial port always points to the default UART in a simulated environment.
    14  //
    15  // TODO: perhaps this should be a special serial object that outputs via WASI
    16  // stdout calls.
    17  var Serial = hardwareUART0
    18  
    19  const (
    20  	PinInput PinMode = iota
    21  	PinOutput
    22  	PinInputPullup
    23  	PinInputPulldown
    24  )
    25  
    26  func (p Pin) Configure(config PinConfig) {
    27  	gpioConfigure(p, config)
    28  }
    29  
    30  func (p Pin) Set(value bool) {
    31  	gpioSet(p, value)
    32  }
    33  
    34  func (p Pin) Get() bool {
    35  	return gpioGet(p)
    36  }
    37  
    38  //export __tinygo_gpio_configure
    39  func gpioConfigure(pin Pin, config PinConfig)
    40  
    41  //export __tinygo_gpio_set
    42  func gpioSet(pin Pin, value bool)
    43  
    44  //export __tinygo_gpio_get
    45  func gpioGet(pin Pin) bool
    46  
    47  type SPI struct {
    48  	Bus uint8
    49  }
    50  
    51  type SPIConfig struct {
    52  	Frequency uint32
    53  	SCK       Pin
    54  	SDO       Pin
    55  	SDI       Pin
    56  	Mode      uint8
    57  }
    58  
    59  func (spi SPI) Configure(config SPIConfig) error {
    60  	spiConfigure(spi.Bus, config.SCK, config.SDO, config.SDI)
    61  	return nil
    62  }
    63  
    64  // Transfer writes/reads a single byte using the SPI interface.
    65  func (spi SPI) Transfer(w byte) (byte, error) {
    66  	return spiTransfer(spi.Bus, w), nil
    67  }
    68  
    69  //export __tinygo_spi_configure
    70  func spiConfigure(bus uint8, sck Pin, SDO Pin, SDI Pin)
    71  
    72  //export __tinygo_spi_transfer
    73  func spiTransfer(bus uint8, w uint8) uint8
    74  
    75  // InitADC enables support for ADC peripherals.
    76  func InitADC() {
    77  	// Nothing to do here.
    78  }
    79  
    80  // Configure configures an ADC pin to be able to be used to read data.
    81  func (adc ADC) Configure(ADCConfig) {
    82  }
    83  
    84  // Get reads the current analog value from this ADC peripheral.
    85  func (adc ADC) Get() uint16 {
    86  	return adcRead(adc.Pin)
    87  }
    88  
    89  //export __tinygo_adc_read
    90  func adcRead(pin Pin) uint16
    91  
    92  // I2C is a generic implementation of the Inter-IC communication protocol.
    93  type I2C struct {
    94  	Bus uint8
    95  }
    96  
    97  // I2CConfig is used to store config info for I2C.
    98  type I2CConfig struct {
    99  	Frequency uint32
   100  	SCL       Pin
   101  	SDA       Pin
   102  }
   103  
   104  // Configure is intended to setup the I2C interface.
   105  func (i2c *I2C) Configure(config I2CConfig) error {
   106  	i2cConfigure(i2c.Bus, config.SCL, config.SDA)
   107  	return nil
   108  }
   109  
   110  // SetBaudRate sets the I2C frequency.
   111  func (i2c *I2C) SetBaudRate(br uint32) error {
   112  	i2cSetBaudRate(i2c.Bus, br)
   113  	return nil
   114  }
   115  
   116  // Tx does a single I2C transaction at the specified address.
   117  func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
   118  	i2cTransfer(i2c.Bus, &w[0], len(w), &r[0], len(r))
   119  	// TODO: do something with the returned error code.
   120  	return nil
   121  }
   122  
   123  //export __tinygo_i2c_configure
   124  func i2cConfigure(bus uint8, scl Pin, sda Pin)
   125  
   126  //export __tinygo_i2c_set_baud_rate
   127  func i2cSetBaudRate(bus uint8, br uint32)
   128  
   129  //export __tinygo_i2c_transfer
   130  func i2cTransfer(bus uint8, w *byte, wlen int, r *byte, rlen int) int
   131  
   132  type UART struct {
   133  	Bus uint8
   134  }
   135  
   136  // Configure the UART.
   137  func (uart *UART) Configure(config UARTConfig) {
   138  	uartConfigure(uart.Bus, config.TX, config.RX)
   139  }
   140  
   141  // Read from the UART.
   142  func (uart *UART) Read(data []byte) (n int, err error) {
   143  	return uartRead(uart.Bus, &data[0], len(data)), nil
   144  }
   145  
   146  // Write to the UART.
   147  func (uart *UART) Write(data []byte) (n int, err error) {
   148  	return uartWrite(uart.Bus, &data[0], len(data)), nil
   149  }
   150  
   151  // Buffered returns the number of bytes currently stored in the RX buffer.
   152  func (uart *UART) Buffered() int {
   153  	return 0
   154  }
   155  
   156  // ReadByte reads a single byte from the UART.
   157  func (uart *UART) ReadByte() (byte, error) {
   158  	var b byte
   159  	uartRead(uart.Bus, &b, 1)
   160  	return b, nil
   161  }
   162  
   163  // WriteByte writes a single byte to the UART.
   164  func (uart *UART) WriteByte(b byte) error {
   165  	uartWrite(uart.Bus, &b, 1)
   166  	return nil
   167  }
   168  
   169  //export __tinygo_uart_configure
   170  func uartConfigure(bus uint8, tx Pin, rx Pin)
   171  
   172  //export __tinygo_uart_read
   173  func uartRead(bus uint8, buf *byte, bufLen int) int
   174  
   175  //export __tinygo_uart_write
   176  func uartWrite(bus uint8, buf *byte, bufLen int) int
   177  
   178  var (
   179  	hardwareUART0 = &UART{0}
   180  	hardwareUART1 = &UART{1}
   181  )
   182  
   183  // Some objects used by Atmel SAM D chips (samd21, samd51).
   184  // Defined here (without build tag) for convenience.
   185  var (
   186  	sercomUSART0 = UART{0}
   187  	sercomUSART1 = UART{1}
   188  	sercomUSART2 = UART{2}
   189  	sercomUSART3 = UART{3}
   190  	sercomUSART4 = UART{4}
   191  	sercomUSART5 = UART{5}
   192  
   193  	sercomI2CM0 = &I2C{0}
   194  	sercomI2CM1 = &I2C{1}
   195  	sercomI2CM2 = &I2C{2}
   196  	sercomI2CM3 = &I2C{3}
   197  	sercomI2CM4 = &I2C{4}
   198  	sercomI2CM5 = &I2C{5}
   199  	sercomI2CM6 = &I2C{6}
   200  	sercomI2CM7 = &I2C{7}
   201  
   202  	sercomSPIM0 = SPI{0}
   203  	sercomSPIM1 = SPI{1}
   204  	sercomSPIM2 = SPI{2}
   205  	sercomSPIM3 = SPI{3}
   206  	sercomSPIM4 = SPI{4}
   207  	sercomSPIM5 = SPI{5}
   208  	sercomSPIM6 = SPI{6}
   209  	sercomSPIM7 = SPI{7}
   210  )