Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/Linux_HAL_Essentials/pru/aiopru/RcAioPRUTest.c
Views: 1800
// This program is free software: you can redistribute it and/or modify1// it under the terms of the GNU General Public License as published by2// the Free Software Foundation, either version 3 of the License, or3// (at your option) any later version.4// This program is distributed in the hope that it will be useful,5// but WITHOUT ANY WARRANTY; without even the implied warranty of6// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the7// GNU General Public License for more details.8// You should have received a copy of the GNU General Public License9// along with this program. If not, see <http://www.gnu.org/licenses/>.1011#include <stdio.h>12#include <stdint.h>13#include <stdlib.h>14#include <string.h>15#include <unistd.h>16#include <fcntl.h>17#include <sys/mman.h>1819//Comment/uncomment the #includes statements depending on your BeagleBone version:20//#include "RcAioPRU_POCKET_bin.h"21//#include "RcAioPRU_BBBMINI_bin.h"22#include "RcAioPRU_BBBLUE_bin.h"2324#define NUM_RING_ENTRIES 30025#define RCOUT_PRUSS_RAM_BASE 0x4a30200026#define RCOUT_PRUSS_CTRL_BASE 0x4a32400027#define RCOUT_PRUSS_IRAM_BASE 0x4a33800028#define RCIN_PRUSS_RAM_BASE 0x4a3030002930#define ARRAY_SIZE(_arr) (sizeof(_arr) / sizeof(_arr[0]))3132#define PWM_FREQ 503334struct ring_buffer {35volatile uint16_t ring_head;36volatile uint16_t ring_tail;37struct {38volatile uint32_t s1;39volatile uint32_t s0;40} buffer[NUM_RING_ENTRIES];41};4243struct pwm {44volatile uint32_t enable;45volatile uint32_t ch1_hi_time;46volatile uint32_t ch1_t_time;47volatile uint32_t ch2_hi_time;48volatile uint32_t ch2_t_time;49volatile uint32_t ch3_hi_time;50volatile uint32_t ch3_t_time;51volatile uint32_t ch4_hi_time;52volatile uint32_t ch4_t_time;53volatile uint32_t ch5_hi_time;54volatile uint32_t ch5_t_time;55volatile uint32_t ch6_hi_time;56volatile uint32_t ch6_t_time;57volatile uint32_t ch7_hi_time;58volatile uint32_t ch7_t_time;59volatile uint32_t ch8_hi_time;60volatile uint32_t ch8_t_time;61volatile uint32_t ch9_hi_time;62volatile uint32_t ch9_t_time;63volatile uint32_t ch10_hi_time;64volatile uint32_t ch10_t_time;65volatile uint32_t ch11_hi_time;66volatile uint32_t ch11_t_time;67volatile uint32_t ch12_hi_time;68volatile uint32_t ch12_t_time;69volatile uint32_t time;70volatile uint32_t max_cycle_time;71};7273volatile struct ring_buffer *ring_buffer;74volatile struct pwm *pwm;7576static const uint32_t TICK_PER_US = 200;77static const uint32_t TICK_PER_S = 200000000;78static const uint32_t TICK_DURATION_NS = 5;7980int main (void)81{82unsigned int ret, s0, s1, min_s0 = 0xffffffff, min_s1 = 0xffffffff, max_s0 = 0, max_s1 = 0;83uint32_t mem_fd = open("/dev/mem", O_RDWR|O_SYNC|O_CLOEXEC);84ring_buffer = (struct ring_buffer*) mmap(0, 0x1000, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, RCIN_PRUSS_RAM_BASE);85pwm = (struct pwm*) mmap(0, 0x1000, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, RCOUT_PRUSS_RAM_BASE);86uint32_t *iram = (uint32_t*)mmap(0, 0x2000, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, RCOUT_PRUSS_IRAM_BASE);87uint32_t *ctrl = (uint32_t*)mmap(0, 0x1000, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, RCOUT_PRUSS_CTRL_BASE);88uint64_t time_ns;89close(mem_fd);9091// This loop checks that the IEP counter is really started. If not, the PRU is reset, the program is reload and PRU restarted92// To report pwm->time and pwm->max_cycle_time, the PRU program must be compiled with -DDEBUG option, for example:93// pasm -V3 -c RcAioPRU.p RcAioPRU_BBBLUE -DBBBLUE -DDEBUG94// This is made for you by 'make debug' followed by 'make test'95do {96printf("The PRU will be reset\n");97// Reset PRU 198*ctrl = 0;99//You might uncomment this to identify more easily where the program ends in the IRAM100//memset(iram, '\0', sizeof(PRUcode) + 128);101// Load firmware102memcpy(iram, PRUcode, sizeof(PRUcode));103// Start PRU 1104*ctrl |= 2;105pwm->ch1_t_time = TICK_PER_S / PWM_FREQ;106pwm->ch2_t_time = TICK_PER_S / PWM_FREQ;107pwm->ch3_t_time = TICK_PER_S / PWM_FREQ;108pwm->ch4_t_time = TICK_PER_S / PWM_FREQ;109pwm->ch5_t_time = TICK_PER_S / PWM_FREQ;110pwm->ch6_t_time = TICK_PER_S / PWM_FREQ;111pwm->ch7_t_time = TICK_PER_S / PWM_FREQ;112pwm->ch8_t_time = TICK_PER_S / PWM_FREQ;113pwm->ch9_t_time = TICK_PER_S / PWM_FREQ;114pwm->ch10_t_time = TICK_PER_S / PWM_FREQ;115pwm->ch11_t_time = TICK_PER_S / PWM_FREQ;116pwm->ch12_t_time = TICK_PER_S / PWM_FREQ;117pwm->enable=0xffffffff;118printf("IEP counter: 0x%08x\n", pwm->time);119} while (pwm->time == 0xffffffff);120121while(1) {122for(unsigned int a = 0; a < NUM_RING_ENTRIES; a++) {123s0 = ring_buffer->buffer[a].s0;124s1 = ring_buffer->buffer[a].s1;125if(s0 > max_s0) {max_s0 = s0;}126if(s1 > max_s1) {max_s1 = s1;}127if(s0 < min_s0) {min_s0 = s0;}128if(s1 < min_s1) {min_s1 = s1;}129}130s0 = ring_buffer->buffer[ring_buffer->ring_tail].s0;131s1 = ring_buffer->buffer[ring_buffer->ring_tail].s1;132time_ns = ((double)pwm->time) * ((double)TICK_DURATION_NS);133printf("max ct: %3u cycles time: %11lluns head: %u tail: %3u s0: %7u s1: %7u s01: %7u jitter_s0: %uns jitter_s1: %uns\n", pwm->max_cycle_time, time_ns, ring_buffer->ring_head, ring_buffer->ring_tail, s0 * TICK_DURATION_NS, s1 * TICK_DURATION_NS, (s0+s1) * TICK_DURATION_NS, ((max_s0-min_s0) * TICK_DURATION_NS), ((max_s1-min_s1) * TICK_DURATION_NS));134// uint32_t value = (uint32_t)((rand() % 1001 + 900) * TICK_PER_US);135// pwm->ch1_hi_time = value;136// pwm->ch2_hi_time = value;137//pwm->ch1_hi_time = 1500 * TICK_PER_US;138pwm->ch1_hi_time = (uint32_t)((rand() % 1001 + 900) * TICK_PER_US);139pwm->ch2_hi_time = 1500 * TICK_PER_US;140pwm->ch3_hi_time = (uint32_t)((rand() % 1001 + 900) * TICK_PER_US);141pwm->ch4_hi_time = (uint32_t)((rand() % 1001 + 900) * TICK_PER_US);142pwm->ch5_hi_time = (uint32_t)((rand() % 1001 + 900) * TICK_PER_US);143pwm->ch6_hi_time = (uint32_t)((rand() % 1001 + 900) * TICK_PER_US);144pwm->ch7_hi_time = (uint32_t)((rand() % 1001 + 900) * TICK_PER_US);145pwm->ch8_hi_time = (uint32_t)((rand() % 1001 + 900) * TICK_PER_US);146pwm->ch9_hi_time = (uint32_t)((rand() % 1001 + 900) * TICK_PER_US);147pwm->ch10_hi_time = (uint32_t)((rand() % 1001 + 900) * TICK_PER_US);148pwm->ch11_hi_time = (uint32_t)((rand() % 1001 + 900) * TICK_PER_US);149pwm->ch12_hi_time = (uint32_t)((rand() % 1001 + 900) * TICK_PER_US);150usleep(1000000);151min_s0 = 0xffffffff;152min_s1 = 0xffffffff;153max_s0 = 0;154max_s1 = 0;155}156return 0;157}158159160