Raspberry Pi can't talk to i2c Sensor - sensors

I have a gas sensor connected to my Raspberry Pi 2 (5V,GND, SDA, SCL)
I use the Raspbian OS.
I try to communicate with the sensor according to this manual:
http://co2meters.com/Documentation/AppNotes/AN142-RaspberryPi-K_series.pdf
i2cdetect delivers the following:
pi#raspberrypi ~ $ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 69 -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
The sensor should have an address of 0x68, im not sure why there is something on 69. But that is not my problem.
I wrote the following test program:
( http://www.byvac.com/downloads/sws/notsmb_1_0.zip )
GNU nano 2.2.6 File: test.py
from notsmb import notSMB
bus = notSMB(I2CBUS)
resp = bus.i2c(0x68,[0x22,0x00,0x08,0x2A],4)
print(resp)
But I get write errors all the time
pi#raspberrypi ~ $ sudo python test.py
Traceback (most recent call last):
File "test.py", line 5, in <module>
resp = bus.i2c(0x68,[0x22,0x00,0x08,0x2A],4)
TypeError: Error write fail

Related

Connect I2C bus,i2cset: write failed: Connection timed out

Hey all i am using sama5d27-som1-ek board and trying to access I2C bus but facing problem please help me to solve the problem.
1)i2cdetect 0
root#sama5d27-som1-ek-sd:~# i2cdetect 0
i2cdetect: WARNING! This program can confuse your I2C bus
Continue? [y/N] y
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- 2a -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- 5c -- -- --
60: 60 -- -- -- 64 -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- --
root#sama5d27-som1-ek-sd:~#
when i use i2cset command it show connection time out
root#sama5d27-som1-ek-sd:~# i2cset 0 0x2a 01 0x08
i2cset: WARNING! This program can confuse your I2C bus
Continue? [y/N] y
i2cset: write failed: Connection timed out
root#sama5d27-som1-ek-sd:~#
can any one suggest me what is error.

U-Boot i2c detects my device (70) but Linux i2c does not. Why?

I am in the process of migrating from Linux 3.10 to a slightly newer version, Linux 4.4.8. After the migration it seems that i2c can no longer see some of my hardware. The hardware itself has not changed whatsoever and works fine in the previous linux version (3.10).
The problem I am running into now is that my i2c commands are no longer working.
When trying to write to the device I get a failed write.
[]# i2cset -y 0 0x70 0 1 b
Error: Write failed
Then I decided to run the i2cdetect to determine what can be seen and this is the output i get
In Linux:
[]# i2cdetect -y -a 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 21 -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- 64 -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
In U-Boot:
[u-boot]# i2c probe
Valid chip addresses: 20 21 64 70
What would be the reason that I can see my device in U-Boot but I can't see my device in Linux?
I suspect the device tree might need to be modified? But I checked the old dtsi files and nothing is different between the i2c0's in the two Linux versions...
i2cdump gives all X's for output
[]# i2cdump -y 0 0x70
No size specified (using byte-data access)
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XXXXXXXXXXXXXXXX
10: XX XX XX XX XX XX XX XX XX XX...
i2c host controller: linux-4-4-8/drivers/i2c/busses/i2c-mv64xxx.c
Devices
0x20, 0x21: PCA9555 IO Port Expanders
0x70: PCA9548A i2c Multiplexer
The newer kernel has a different parent dtsi file that defines various mpp's for pinctrl
The dts file I was using contained some Ethernet devices that were defined but were never used by us, we piggy backed off another team's device tree since our hardware was ALMOST identical. Those ethernet's used the following mdio
mdio#72004 {
pinctrl-names = "default";
pinctrl-0 = <&mdio_pins>; //<==== This was the problem
phy0: ethernet-phy#0 {
reg = <0>;
};
};
The mdio uses &mdio_pins and, in the newer kernel, is defined as
mdio_pins: mdio-pins {
marvell,pins = "mpp4", "mpp5";
marvell,function = "ge";
};
It just so happens to be that the gpio reset for my device is mpp5.
I changed the ethernet section of the device tree to the following
ethernet#70000 {
status = "disabled";
};
mdio#72004 {
status = "disabled";
};
ethernet#30000 {
status = "disabled";
};
This solved the problem of the device not showing up in i2cdetect and the hardware is working as before.
It was my own fault for assuming the device tree I inherited was concrete in definition of the hardware being used.

How use a machine int for limited ops bit set in Haskell?

I would like to use a machine int to represent a set containing zero or more of the numbers 1..9. This is to feed a foolish obsession with just how fast a sudoku solving algorithm I can build with Haskell, but all in a broader desire to extend my Haskell education. I'm finding having a real problem to work on a great motivator above the book exercises I've been able to find. I have it down to a couple milliseconds for a "very hard" but solvable-without-guessing puzzle, but am hoping for a next generation performance measured in microseconds :)
I would need the following operations:
Initialize a set to include all members, which would correspond to the decimal number 511, or 0b111111111. I'm thinking something like s1 = MyBitSet(511), but any way that gets it done without having to set each bit individually would be fine.
set subtraction - like 0b111111111 - 0b10101 => 111101010. In bitwise operations I believe this would be s1 OR complement(s2).
members - like s2 = {2,4,6,7,8,9}
I've been puzzling over the code I've been able to find on search, but partly because my Haskell skills are still developing and partly because this is a bit of a specialized optimization, I'm having trouble making progress.
Can you point me in the right direction?
Based on the suggestions of #user2407038 above, I worked out the following module that seems to do the job nicely, and gave me occasion to get a number of Haskell concepts straight in my head for the first time:
--
-- A simple, hopefully very fast Sudoku markup set implementation
--
module MarkupSet where
import Data.Bits ((.&.), complement, setBit, testBit)
import Data.Word (Word)
import Test.HUnit
type MarkupSet = Word
-- initialized MarkupSet value, 511 decimal, 0b111111111 binary
defaultMarkupSet :: MarkupSet
defaultMarkupSet = 511
-- an empty set is also handy in some cases
emptyMarkupSet :: MarkupSet
emptyMarkupSet = 0
-- construct a MarkupSet from an Int (Word) value, e.g. markupSet 42
markupSet :: Word -> MarkupSet
markupSet x = x
-- construct a MarkupSet from list of values, e.g. [1,3,5]
fromValues :: [Int] -> MarkupSet
fromValues = foldl (\acc n -> setBit acc (n-1)) emptyMarkupSet
-- based on comment from #dfeuer, updated from:
-- fromValues [] = markupSet 0
-- fromValues (n:ns) = setBit (fromValues ns) (n - 1)
-- a list of the integer members 1..9 in a MarkupSet, e.g. [1,3,5]
enumMembers :: MarkupSet -> [Int]
enumMembers s = [ i+1 | i <- [0..8], testBit s i]
-- set difference, aka complement set: members of s not in s'
-- e.g difference {1,3,5,8} {1,2,3} -> {5,8}
difference :: MarkupSet -> MarkupSet -> MarkupSet
difference s s' = s .&. (complement s')
-- 9-digit binary string representation, e.g. "001011010"
showBits :: MarkupSet -> String
showBits s = [ if testBit s i then '1' else '0' | i <- [8,7..0] ]
-- ===================================================================
-- Tests
-- ===================================================================
case_default_has_all_members = TestCase $ assertEqual
"defaultMarkupSet should have all bits set"
"111111111"
(showBits defaultMarkupSet)
case_construct_from_int = TestCase $ assertEqual
"can construct from Int literal"
"000101010"
(showBits $ markupSet 42)
case_construct_from_values = TestCase $ assertEqual
"can construct from list of ints in range [1..9]"
297
(fromValues [1,4,6,9])
case_enum_int_members = TestCase $ assertEqual
"can enumerate its integer members"
[2,4,6]
(enumMembers $ markupSet 42)
case_subtract_sets = TestCase $ assertEqual
"can calculate set difference"
[1,3,5,7,8,9]
(enumMembers $ difference defaultMarkupSet (markupSet 42))
tests =
[ case_default_has_all_members
, case_construct_from_int
, case_construct_from_values
, case_enum_int_members
, case_subtract_sets
]
main = do
runTestTT $ TestList tests

Interaction of forkIO/killThread with forkProcess

I've written the code below, and noticed that killThread blocks and the thread still continues. That only happens if I do it in the forkProcess, if I remove the forkProcess, everything works as expected.
Code
{-# LANGUAGE TupleSections #-}
module Main where
import Control.Concurrent
import Control.Monad
import System.Posix.Process
{-# NOINLINE primes #-}
primes :: [Integer]
primes = 2:[x | x <- [3..], all (not . flip isDivisorOf x) (takeWhile (< truncate (sqrt $ fromInteger x :: Double)) primes)]
where x `isDivisorOf` y = y `rem` x == 0
evaluator :: Show a => [a] -> IO ()
evaluator xs = do
putStrLn "[Evaluator] Started evaluator."
forM_ xs $ \x -> putStrLn $ "[Evaluator] Got result: " ++ show x
putStrLn "[Evaluator] Evaluator exited."
test :: IO ThreadId
test = forkIO (evaluator $ filter ((== 13) . flip rem (79 * 5 * 7 * 3 * 3 * 2 * 3)) primes) -- Just some computation that doesn't finsish too fast
main :: IO ()
main = do
pid <- forkProcess $ do
a <- test
threadDelay $ 4000 * 1000
putStrLn "Canceling ..."
killThread a
putStrLn "Canceled"
void $ getProcessStatus True False pid
Output
$ ghc test.hs -O -fforce-recomp -threaded -eventlog -rtsopts # I also tried with -threaded
$ ./test +RTS -N2 # I also tried without -N
[Evaluator] Started evaluator.
[Evaluator] Got result: 13
[Evaluator] Got result: 149323
[Evaluator] Got result: 447943
[Evaluator] Got result: 597253
[Evaluator] Got result: 746563
[Evaluator] Got result: 1045183
Canceling ...
[Evaluator] Got result: 1194493
[Evaluator] Got result: 1642423
[Evaluator] Got result: 1791733
[Evaluator] Got result: 2090353
[Evaluator] Got result: 2687593
[Evaluator] Got result: 3135523
[Evaluator] Got result: 3284833
[Evaluator] Got result: 4777933
[Evaluator] Got result: 5375173
^C[Evaluator] Got result: 5524483
^C
This is not the usual problem that there is no memory allocation and thus GHC's thread scheduler doesn't run. I verified that by running the program with +RTS -sstderr, which shows that the garbage collector is running very often. I'm running this on linux 64bit.
This bug report notes that forkProcess masks asynchronous exceptions in the child process despite no indication of such in the documentation. The behavior should be fixed in 7.8.1 when it is released.
Of course, if asynchronous exceptions are masked, the throw inside the killThread will block indefinitely. If you simply delete the lines in main containing forkProcess and getProcessStatus, the program works as intended:
module Main where
import Control.Concurrent
import Control.Monad
import System.Posix.Process
{-# NOINLINE primes #-}
primes :: [Integer]
primes = 2:[ x | x <- [3..], all (not . flip isDivisorOf x) (takeWhile (< truncate (sqrt $ fromInteger x :: Double)) primes)]
where x `isDivisorOf` y = y `rem` x == 0
evaluator :: Show a => [a] -> IO ()
evaluator = mapM_ $ \x ->
putStrLn $ "[Evaluator] Got result: " ++ show x
test :: IO ThreadId
test = forkIO (evaluator $ filter ((== 13) . flip rem (79 * 5 * 7 * 3 * 3 * 2 * 3)) primes) -- Just some computation that doesn't finsish too fast
main :: IO ()
main = do
a <- test
threadDelay $ 4000 * 1000
putStrLn "Canceling ..."
killThread a
putStrLn "Canceled"
I build it with ghc --make -threaded async.hs and run with ./async +RTS -N4.
If for some reason you need a separate process, you will have to manually unmask asynchronous exceptions in the child process in GHC 7.6.3.

Haskell: What monad did I just reinvent?

I just reinvented some monad, but I'm not sure which. It lets you model steps of a computation, so you can interleave the steps of numerous computations to find which one finishes first.
{-# LANGUAGE ExistentialQuantification #-}
module Computation where
-- model the steps of a computation
data Computation a = forall b. Step b (b -> Computation a) | Done a
instance Monad Computation where
(Step b g) >>= f = Step b $ (>>=f) . g
(Done b) >>= f = Step b f
return = Done
runComputation :: Computation a -> a
runComputation (Step b g) = runComputation (g b)
runComputation (Done a) = a
isDone :: Computation a -> Bool
isDone (Done _) = True
isDone _ = False
-- an order for a set of computations
data Schedule a = a :> Computation (Schedule a) | Last
toList :: Schedule a -> [a]
toList Last = []
toList (a :> c) = a : (toList . runComputation) c
-- given a set of computations, find a schedule to generate all their results
type Strategy a = [Computation a] -> Computation (Schedule a)
-- schedule all the completed computations, and step the rest,
-- passing the remaining to the given function
scheduleOrStep :: (Queue (Computation a) -> Computation (Schedule a)) -> Strategy a
scheduleOrStep s cs = scheduleOrStep' id cs
where scheduleOrStep' q ((Done a):cs) = Done $ a :> scheduleOrStep' q cs
scheduleOrStep' q ((Step b g):cs) = scheduleOrStep' (q . (g b:)) cs
scheduleOrStep' q [] = s q
-- schedule all completed compuations, step all the rest once, and repeat
-- (may never complete for infinite lists)
-- checking each row of
-- [ [ c0s0, c1s0, c2s0, ... ]
-- , [ c0s1, c1s1, c2s1, ... ]
-- , [ c0s2, c1s2, c2s2, ... ]
-- ...
-- ]
-- (where cNsM is computation N stepped M times)
fair :: Strategy a
fair [] = Done Last
fair cs = scheduleOrStep (fair . ($[])) cs
-- schedule more steps for earlier computations rather than later computations
-- (works on infinite lists)
-- checking the sw-ne diagonals of
-- [ [ c0s0, c1s0, c2s0, ... ]
-- , [ c0s1, c1s1, c2s1, ... ]
-- , [ c0s2, c1s2, c2s2, ... ]
-- ...
-- ]
-- (where cNsM is computation N stepped M times)
diag :: Enqueue (Computation a)-> Strategy a
diag _ [] = Done Last
diag enq cs = diag' cs id
where diag' (c:cs) q = scheduleOrStep (diag' cs) (enq c q $ [])
diag' [] q = fair (q [])
-- diagonal downwards :
-- [ c0s0,
-- c1s0, c0s1,
-- c2s0, c1s1, c0s2,
-- ...
-- cNs0, c{N-1}s1, ..., c1s{N-1}, c0sN,
-- ...
-- ]
diagd :: Strategy a
diagd = diag prepend
-- diagonal upwards :
-- [ c0s0,
-- c0s1, c1s0,
-- c0s2, c1s1, c2s0,
-- ...
-- c0sN, c1s{N-1}, ..., c{s1N-1}, cNs0,
-- ...
-- ]
diagu :: Strategy a
diagu = diag append
-- a queue type
type Queue a = [a] -> [a]
type Enqueue a = a -> Queue a -> Queue a
append :: Enqueue a
append x q = q . (x:)
prepend :: Enqueue a
prepend x q = (x:) . q
I feel like this is probably some kind of threading monad?
It looks like a resumption-with-state monad. I think there used to be a resumption monad in MTL around GHC 6.6 but if there was it disappeared. William Harrison at the University of Missouri has a number of papers about resumption monads - http://people.cs.missouri.edu/~harrisonwl/publications.html
I don't understand why not
data Computation a = Step (Computation a) | Done a
instance Monad Computation where
(Step g) >>= f = Step $ g >>= f
(Done b) >>= f = Step (f b)
return = Done
I'm not sure what this monad is, but it's definitely simpler and seems to be equivalent in most respects.
I haven't spent too much time understanding your code, but it really sounds like the coroutine monad from the monad-coroutine package, which may be a bit more general.
This looks similar to the definition of stream fusion used by Don Stewart did a while ago, and also somewhat related to iteratees (though wihtout the notion of pushing data into the iteratee using an enumerator), but less so than stream fusion I guess.

Resources