Conversation
soypat
left a comment
There was a problem hiding this comment.
lots of good stuff and also some stuff to improve. hope this helps :)
| // REG_MEM_PAGE is the MEM_PAGE address | ||
| REG_MEM_PAGE uint8 = 0xF3 | ||
| // MEM_PAGE_MSK is mask for SPI memory page | ||
| MEM_PAGE_MSK uint8 = 0x10 | ||
| // SPI_RD_MSK is the mask for reading a register in SPI | ||
| SPI_RD_MSK uint8 = 0x80 | ||
| // SPI_WR_MSK is the mask for writing a register in SPI | ||
| SPI_WR_MSK uint8 = 0x7F | ||
| // MEM_PAGE0 is the SPI memory page 0 | ||
| MEM_PAGE0 uint8 = 0x10 | ||
| // MEM_PAGE1 is the SPI memory page 1 | ||
| MEM_PAGE1 uint8 = 0x00 |
There was a problem hiding this comment.
This is specific to SPI but it defines the register address of SPI interface as well the other registers which are all exported. I don't think it's such a bit problem having them exported.
There was a problem hiding this comment.
If the user already has access to the Read/Write layer, they don't really need them. They might need the registers but not this...
| func (s *spi) read(reg uint8, data []byte) error { | ||
| reg |= SPI_RD_MSK | ||
|
|
||
| return s.bus.Tx([]byte{reg}, data) |
There was a problem hiding this comment.
how to avoid heap in this case?
There was a problem hiding this comment.
Key is always pass in the spi's in-struct array to Tx. All you gotta do is fill it with the correct data.
s.bus.wbuf[0] = reg | SPI_RD_MSK
n := copy(s.bus.wbuf[:], data)
return s.bus.Tx(s.bus.wbuf[:n], s.bus.rbuf[:n])There was a problem hiding this comment.
Also, your driver is technically incorrect, you should always pass in equal length buffers to Tx, or one of them should be nil
| data[0] &^= MEM_PAGE_MSK | ||
| data[0] |= (memoryPage & MEM_PAGE_MSK) | ||
|
|
||
| return s.bus.Tx([]byte{REG_MEM_PAGE | SPI_WR_MSK}, []byte{data[0]}) |
There was a problem hiding this comment.
how to avoid heap in this case?
There was a problem hiding this comment.
Same rules as above. use in-struct arrays. please refer to https://deploy-preview-451--tinygo.netlify.app/docs/guides/driver-design/
If the path to avoiding heap allocations is not clear please leave a comment here with what is missing or could be improved: tinygo-org/tinygo-site#451
This PR adds support for BME68X (BME680/BME688) a low power gas, pressure, temperature & humidity sensor.
It includes the support for Sleep and Forced modes as well I2C and SPI interfaces. It introduces the
Options Patternto make the initialization ofbme68xpackage more flexible.Simple example: