libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
pruss_toggle.c
Go to the documentation of this file.
1 
17 #include "stdio.h"
18 #include "string.h"
19 #include "libpruio/pruio.h" // the library (for pinmuxing)
20 #include "libpruio/pruio_pins.h" // CPU balls human readable
21 #include "libpruio/pruio_prussdrv.h" // user space part of uio_pruss
22 
23 
56 {
57  const uint32 PRUcode[] = {
58  0x240000e0,
59  0xf100a081,
60  0x240001e0,
61  0x0801e0e1,
62  0x30820006,
63  0x14e1fefe,
64  0x240000e0,
65  0x240000e0,
66  0x240000e0,
67  0x240000e0,
68  0x1003031f,
69  0x2a000000,
70  0x21000400 };
71 
72  if(0 >= prussdrv_pru_write_memory(IRam, 0, PRUcode, sizeof(PRUcode)))
73  {printf("failed loading instructions\n"); return -1;}
74  return 0;
75 }
76 
77 
79 int main(int argc, char **argv)
80 {
81  // our configuration is for PRU-0, so PRU-1 for libpruio
83  pruIo *io = pruio_new(act, 0, 0, 0); // create new driver structure
84  do {
86 //
87 // Check libpruio init success
88 //
89  if(io->Errr) {
90  printf("initialisation failed (%s)\n", io->Errr); break;}
91 
92  if(io->PruNo) { // we use the other PRU
93  pru_num = 0;
97  else {
98  pru_num = 1;
101  pru_intr = PRU0_ARM_INTERRUPT; } // libpruio uses PRU1_ARM_INTERRUPT!
102 //
103 // Now prepare the other PRU
104 //
105  if(prussdrv_open(PRU_EVTOUT_0)) { // note: libpruio uses PRU_EVTOUT_5
106  printf("prussdrv_open failed\n"); break;}
107  //Note: no prussdrv_pruintc_init(), libpruio did it already
108 
109  if(load_firmware(pru_iram) < 0) break;
110 //
111 // Pinmuxing (some examples first)
112 //
114  //if (io->setPin(io, P8_15, 6 | PRUIO_RX_ACTIV | PRUIO_PULL_UP)) {
115  //printf("P8_15 configuration failed (%s)\n", io->Errr); break;}
116 
118  //if (io->setPin(io, SD_08, 6 | PRUIO_RX_ACTIV | PRUIO_PULL_DOWN)) {
119  //printf("SD_08 configuration failed (%s)\n", io->Errr); break;}
120 
122  //if (io->setPin(io, SD_08, 5 | PRUIO_NO_PULL)) {
123  //printf("SD_08 configuration failed (%s)\n", io->Errr); break;}
124 
125  // set PRU-0-r30 bit 15 output (mode 6), pull down resistor
126  if (io->setPin(io, P8_11, 6)) {
127  printf("P8_11 configuration failed (%s)\n", io->Errr); break;}
128 
129  /* ... */
130 //
131 // Pprepare libpruio measurement
132 //
133  if (pruio_cap_config(io, P9_42, 2.)) { // configure CAP input pin
134  printf("failed setting input P9_42 (%s)\n", io->Errr); break;}
135  if (pruio_config(io, 1, 0, 0, 0)) {
136  printf("config failed (%s)\n", io->Errr); break;}
137  float_t
138  f, // the measured frequency.
139  d; // the measured duty cycle.
140 //
141 // Pass parameters to PRU
142 //
143  uint32 *dram;
144  prussdrv_map_prumem(pru_dram, (void *) &dram); // get dram pointer
145  dram[0] = 15; // bit number, must match configured pin (P8_11)
146  dram[1] = 16; // loop count (max 16 bit = 65535)
147  dram[2] = pru_intr + 16; // the interrupt we're waiting for
148 //
149 // Execute 20 times
150 //
151  printf("instructions loaded, starting PRU-%d\n", pru_num);
152  prussdrv_pru_enable(pru_num); // start
153  for (i = 0; i < 20; i++)
154  {
155  prussdrv_pru_wait_event(PRU_EVTOUT_0); // wait until PRU finished
157 
158  if(pruio_cap_Value(io, P9_42, &f, &d)) { // get last measurement
159  printf("failed reading input P9_42 (%s)\n", io->Errr); break;}
160 
161  printf("--> Frequency: %3.0f MHz, Duty:%3.0f %c \n",
162  (f * .000001), (d * 100), '%'); // results
163 
164  prussdrv_pru_resume(pru_num); // continue after HALT
165  }
166 
167  prussdrv_pru_disable(pru_num); // Disable PRU
168  // note: no prussdrv_exit(), libpruio does it in the destructor
169  } while (0);
170 
171  pruio_destroy(io); /* destroy driver structure */
172  return 0;
173 }
VAR io
create new driver UDT
Definition: 1.bas:18
unsigned short uint16
16 bit unsigned integer data type.
Definition: pruio.h:40
unsigned int uint32
32 bit unsigned integer data type.
Definition: pruio.h:41
char * pruio_config(pruIo *Io, uint32 Samp, uint32 Mask, uint32 Tmr, uint16 Mds)
Wrapper function for PruIo::config().
char * pruio_cap_Value(pruIo *Io, uint8 Ball, float_t *Hz, float_t *Du)
Wrapper function for CapMod::Value().
float float_t
float data type.
Definition: pruio.h:42
int int32
32 bit signed integer data type.
Definition: pruio.h:38
pruIo * pruio_new(uint16 Act, uint8 Av, uint32 OpD, uint8 SaD)
Wrapper function for the constructor PruIo::PruIo().
char * pruio_cap_config(pruIo *Io, uint8 Ball, float_t FLow)
Wrapper function for CapMod::config().
void pruio_destroy(pruIo *Io)
Wrapper function for the destructor PruIo::~PruIo().
@ PRUIO_ACT_PWM0
Activate PWMSS-0 (including eCAP, eQEP, ePWM).
Definition: pruio.h:524
@ PRUIO_ACT_PRU1
Activate PRU-1 (= default, instead of PRU-0).
Definition: pruio.h:518
#define PRUSS0_PRU0_IRAM
ID for PRU-0 instruction ram.
Definition: pruio_intc.h:70
#define PRUSS0_PRU1_DRAM
ID for PRU-1 data ram.
Definition: pruio_intc.h:69
#define PRU_EVTOUT_0
ID for host interrupt 0.
Definition: pruio_intc.h:79
#define PRU0_ARM_INTERRUPT
System event PRU-0 -> ARM.
Definition: pruio_intc.h:18
#define PRUSS0_PRU1_IRAM
ID for PRU-1 instruction ram.
Definition: pruio_intc.h:71
#define PRUSS0_PRU0_DRAM
ID for PRU-0 data ram.
Definition: pruio_intc.h:68
#define P9_42
CPU ball number for double pin 42 on header 9 (second ball is 104)
Definition: pruio_pins.bi:163
#define P8_11
CPU ball number for pin 11 on header 8.
Definition: pruio_pins.bi:30
int32 prussdrv_pru_disable(uint32 prunum)
See prussdrv_pru_disable(BYVAL_AS_UInt32 PruId)
char * prussdrv_pru_resume(uint32 prunum)
See prussdrv_pru_resume(BYVAL_AS_UInt32 PruId)
#define prussdrv_pru_enable(N)
Macro for variadic parameter list.
void prussdrv_pru_clear_event(uint32 host_interrupt, uint32 sysevent)
See prussdrv_pru_clear_event(BYVAL_AS_UInt32 Irq, BYVAL_AS_UInt32 Event)
uint32 prussdrv_pru_wait_event(uint32 host_interrupt)
See prussdrv_pru_wait_event(BYVAL_AS_UInt32 Irq)
int32 prussdrv_open(uint32 host_interrupt)
See prussdrv_open(BYVAL_AS_UInt32 Irq)
int32 prussdrv_pru_write_memory(uint32 pru_ram_id, uint32 wordoffset, const uint32 *memarea, uint32 bytelength)
See prussdrv_pru_write_memory(BYVAL_AS_UInt32 RamId, BYVAL_AS_UInt32 Offs, BYVAL_AS_CONST_UInt32_PTR ...
int32 prussdrv_map_prumem(uint32 pru_ram_id, void **address)
See prussdrv_map_prumem(BYVAL_AS_UInt32 RamId, BYVAL_AS_ANY_PTR_PTR Addr)
UInt32 pru_intr
ID of its interrupt.
Definition: pruss_add.bas:67
UInt32_PTR dram
a pointer to PRU data ram
Definition: pruss_add.bas:97
UInt32 pru_num
which pru to use
Definition: pruss_add.bas:64
UInt32 pru_iram
ID of its instruction ram.
Definition: pruss_add.bas:65
UInt32 pru_dram
ID of its data ram.
Definition: pruss_add.bas:66
float_t f
the measured frequency
float_t d
the measured duty cycle
int main(int argc, char **argv)
The main function.
Definition: pruss_toggle.c:79
int32 load_firmware(uint32 IRam)
load firmware to PRU
Definition: pruss_toggle.c:55
VAR i
Start index.
Definition: rb_file.bas:62
Wrapper structure for PruIo.
Definition: pruio.h:550