doubao6936 2017-06-14 10:51
浏览 66

Raspberry Pi GPIO和IRQ处理

I am experimenting with the implementation of the manchester protocol for a 433 mhz transceiver. So I wrote a simple program that uses 2 pins - one for sending, another one for receiving the signal. The receiver part uses IRQ to intercept the edge change on the pin, and read its value to pass down to the manchester decoder. So far it looks good, in theory and tests, but in reality as soon as I start writing some data to the transmitter pin - the interrupt watcher on the receiver pin stops working. I use the same board for connecting the transmitter and the receiver parts.

I created some simple program to simply watch on the edge changes - and it intercepts the changes just fine, unless you use mem (wiringpi gpio) or sysfs to write a value to another pin. At this point the interrupts stop to happen.

Am I missing something?

Below is the original sample that uses GPIO 17 for reading and watching the edge change, and GPIO 21 for transmitting the signal.

package main

import (
    "github.com/jdevelop/go-manchester/manchester"
    "github.com/davecheney/gpio"
    "fmt"
    "os"
    "strconv"
    "time"
)

func receive(bps int) {
    pin, err := gpio.OpenPin(17, gpio.ModeInput)
    if err != nil {
        panic(err)
    }

    frame := manchester.NewDataFrame()
    driver := manchester.NewManchesterDriver(int64(bps), 1000000)



    if err = pin.BeginWatch(gpio.EdgeBoth, func() {
        driver.ReadBit(manchester.Up, time.Now().UnixNano(), func(bool) {
            val := pin.Get()
            println(val)
            if frame.ReadBit(val) {
                println("DONE")
            }
        })
    }); err != nil {
        panic(err)
    }

    defer pin.EndWatch()
    defer pin.Close()

    time.Sleep(10 * time.Second)

    fmt.Println(frame)

}

func main() {

    text := os.Args[2]
    bps, _ := strconv.Atoi(os.Args[1])

    driver := manchester.NewManchesterDriver(int64(bps), 100)
    frame := manchester.BuildDataFrame([]byte(text))

    pin, _ := gpio.OpenPin(21, gpio.ModeOutput)
    pin.Clear()

    defer pin.Close()

    logger := make(chan bool, 100)

    loggerF := func() {
        for {
            select {
            case lvl := <-logger:
                if lvl {
                    fmt.Print(1)
                } else {
                    fmt.Print(0)
                }
            }
        }
    }

    signal := func(level bool) {
        if level {
            pin.Set()
        } else {
            pin.Clear()
        }
    }

    go loggerF()
    go receive(bps)

    frame.WriteFrame(func(bit bool) {
        logger <- bit
        driver.WriteBit(bit, signal)
    })

    pin.Clear()

    fmt.Println()
    fmt.Println("Done")

}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?