0

I'm currently trying to access a Bosch BMP280 pressure sensor from Haskell using the HPi wrapper for the bcm2835 library.

I have no experience in handling SPI at all and as documentation is extremely brief / sparse, I had no success trying to troubleshoot the problem on my own.

As far as I understand, to read from the chip's registers, I have to clock out the address of the register to be read (with the first bit of this being replaced by a '0' to enter read mode)and then clock in as many bits as I want to read (by clocking out the corresponding number of Bits / Word8's of zeros while returning the bits being clocked in at the same time). In code (following the spitest.hs from the HPi GitHub repo):

import System.RaspberryPi.GPIO
import Control.Concurrent
import Control.Monad
import Data.Bits
import qualified Data.ByteString as BS
import Data.Word

main = withGPIO . withSPI $ do
    chipSelectSPI CS0   --set the Chip select pin to the CS0 pin
    setChipSelectPolaritySPI CS0 False --The BMP chip needs CS to be pulled low
    setDataModeSPI (False,False)
    transferManySPI [244,47] -- write 0xF4 = 244 settings 00101111 = 47
    transferManySPI [245,160] -- write 0xF5 = 245 settings 10100000 = 160
    threadDelay 10000
    printReadout

printReadout :: IO()
printReadout = do
    transferManySPI [122,0] >>= (print) -- read 0xFA = 0x7A = 122

Is that correct? With this code I get nothing than [0,0] back from the sensor.

Starting to guess what the problem might be, as the bcm2835SPIClockDivider enumeration type from the library seems not to be handled in any way in HPi, I wonder about if there is a default clock divider / clock speed used by the library and if it possibly exceeds the maximum of 10 MHz mentioned in the sensor's manual. Is that the case?

0 Answers0