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

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
    • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
    • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
    • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?