|
本帖最后由 sjtitr 于 2014-6-17 11:29 编辑
总体来说,编码没什么难度的。从方案讲,从设计到编码也只需要抛砖引玉的做典型示例:
所以本辑只列举代码:
设计内容请参考 [BB方案]第五辑——方案设计
首先是PHP,作为接收命令的起始点,我使用URL参数来接收要通过红外线发送的数据串:
- <html>
- <head>
- <title>Test Pipe Page</title>
- </head>
- <body>
- <?php
- $str = $_REQUEST['str'];
- $fp = @fopen("////tmp//irfifo", 'w');
- echo $fp;
- echo nl2br("\n");
- $num = 0x5a;
- $data = pack("H2", "5a");
- echo @fwrite($fp, $data);
- echo nl2br("\n");
- $data = pack("C1", strlen($str)/2);
- echo @fwrite($fp, $data);//д
- echo nl2br("\n");
- $data = pack("H*", $str);
- echo @fwrite($fp,$data);//д
- echo nl2br("\n");
- fclose($fp);
- echo nl2br("\n");
- print("Hello World!");
- echo nl2br("\n");
- print("sjtitr");
- echo nl2br("\n");
- ?>
- <br><br>
- <a href="test_pipe.php?str=00ff48b7"><-</a><br><br>
- <a href="test_pipe.php?str=00ff4ab5">-></a><br><br>
- </body>
- </html>
复制代码
使用的时候,就像这样咯:test_pipe.php?str=00ff48b7
这样一来,C程序也很简单,目标是处理管道文件,然后启动pru:
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <prussdrv.h>
- #include <pruss_intc_mapping.h>
-
- #define PRU_NUM 0
- #define FIFO "/tmp/irfifo"
- void prurun(char *buf)
- {
- unsigned int ret;
- tpruss_intc_initdata pruss_intc_initdata = PRUSS_INTC_INITDATA;
-
- prussdrv_init ();//Initialize the PRU
- if (prussdrv_open(PRU_EVTOUT_0))//Open PRU Interrupt
- {
- printf("prussdrv_open open failed");
- return;
- }
- prussdrv_pruintc_init(&pruss_intc_initdata);
- {
- prussdrv_pru_write_memory(PRUSS0_PRU0_DATARAM, 0, buf, buf[0]+1);
- }
- prussdrv_exec_program (PRU_NUM, "./prucode.bin");//Execute example on PRU
- prussdrv_pru_wait_event (PRU_EVTOUT_0);//Waiting for this instruction: MOV r31.b0, PRU0_ARM_INTERRUPT+16
- prussdrv_pru_clear_event (PRU0_ARM_INTERRUPT,0);
- prussdrv_pru_disable (PRU_NUM);//Disable PRU and close memory mapping
- prussdrv_exit ();
-
- }
- int main(int argc, char** argv)
- {
- char buf[64];
- int fd;
- int nread;
- if((mkfifo(FIFO, O_CREAT|O_EXCL)<0) && (errno!=EEXIST))
- {
- printf("cannot create fifo\n");
- }
- fd = open(FIFO, O_RDONLY);
- if(fd==-1)
- {
- printf("cannot open fifo\n");
- exit(1);
- }
- while(1)
- {
- int i = 0;
- memset(buf, 0, sizeof(buf));
- read(fd, buf, 1);
- if(buf[0]!=0x5A)
- {
- // printf("%X,", buf[0]);
- continue;
- }
- read(fd, buf, 1);
- if(buf[0] == 0)
- {
- break;
- }
- read(fd, &buf[1], buf[0]);
- printf("%d: ", buf[0]);
- while(i < buf[0])
- {
- i++;
- printf("%X,",buf[i]);
- }
- printf("\n");
- prurun(buf);
- }
- unlink(FIFO);
- exit(0);
- }
复制代码
最后是PRU程序,使用汇编语言完成,根据给下来的数据,执行红外编码的时序:
- //prucode.p
- .origin 0
- .entrypoint START
-
- //Refer to this mapping in the file - prussdrvincludepruss_intc_mapping.h
- #define PRU0_ARM_INTERRUPT 19
- #define CONST_PRUCFG C4
-
- //Refer to AM335X Technical Reference Manual & BBB SRM
- #define GPIO1 0x4804c000
- #define GPIO_CLEARDATAOUT 0x190
- #define GPIO_SETDATAOUT 0x194
-
- START:
- // Enable OCP master port
- LBCO r0, CONST_PRUCFG, 4, 4
- CLR r0, r0, 4 // Clear SYSCFG[STANDBY_INIT] to enable OCP master port
- SBCO r0, CONST_PRUCFG, 4, 4
-
- MOV r5, 0
- LBBO r4, r5, 0, 1
- AND r4, r4, 255
- LEADER:
- MOV r1, 340
- CALL WAV_S
- MOV r0, 450000
- LEADER_T:
- SUB r0, r0, 1
- QBNE LEADER_T, r0, 0
- NEXT_BYTE:
- ADD r5, r5, 1
- LBBO r6, r5, 0, 1
- MOV r7, 8
-
- NEXT_BIT:
- MOV r1, 21
- CALL WAV_S
- AND r0, r6, 1<<7
- QBNE BIT_1, r0, 0
-
- BIT_0:
- MOV r0, 56500
- JMP BIT_T
-
- BIT_1:
- MOV r0, 169000
- BIT_T:
- SUB r0, r0, 1
- QBNE BIT_T, r0, 0
- LSL r6, r6, 1
- SUB r7, r7, 1
- QBNE NEXT_BIT, r7, 0
- BYTE_OK:
- SUB r4, r4, 1
- QBNE NEXT_BYTE, r4, 0
- STOP_S:
- MOV r1, 21
- CALL WAV_S
- MOV r0, 5000000
- STOP_T:
- SUB r0, r0, 1
- QBNE STOP_T, r0, 0
- // Send notification to Host for program completion
- MOV r31.b0, PRU0_ARM_INTERRUPT+16
-
- // Halt the processor
- HALT
- WAV_S:
- MOV r2, 1<<12
- MOV r3, GPIO1 | GPIO_SETDATAOUT
- SBBO r2, r3, 0, 4
-
- MOV r0, 850
- WAV_1:
- SUB r0, r0, 1
- QBNE WAV_1, r0, 0
- MOV r2, 1<<12
- MOV r3, GPIO1 | GPIO_CLEARDATAOUT
- SBBO r2, r3, 0, 4
- MOV r0, 1800
- WAV_0:
- SUB r0, r0, 1
- QBNE WAV_0, r0, 0
-
- SUB r1, r1, 1
- QBNE WAV_S, r1, 0
- RET
复制代码
OK。下一辑就是最后一辑了,我们来调试一下并实现一个场景,计划用视频说话~
|
|