Advertisement
Guest User

modified pifm

a guest
Dec 10th, 2012
1,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.24 KB | None | 0 0
  1. // To run:
  2. //  wget -O - http://beta.etherpad.org/p/pihackfm/export/txt 2>/dev/null | gcc -lm -std=c99 -g -xc - && time ./a.out
  3. // Access from ARM Running Linux
  4.  
  5. #define BCM2708_PERI_BASE        0x20000000
  6. #define GPIO_BASE                (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <dirent.h>
  13. #include <math.h>
  14. #include <fcntl.h>
  15. #include <assert.h>
  16. #include <sys/mman.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19.  
  20. #include <unistd.h>
  21.  
  22. #define PAGE_SIZE (4*1024)
  23. #define BLOCK_SIZE (4*1024)
  24.  
  25. int freq_const;
  26.  
  27. int  mem_fd;
  28. char *gpio_mem, *gpio_map;
  29. char *spi0_mem, *spi0_map;
  30.  
  31.  
  32. // I/O access
  33. volatile unsigned *gpio;
  34. volatile unsigned *allof7e;
  35.  
  36. // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
  37. #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  38. #define OUT_GPIO(g) *(gpio+((g)/10)) |=  (1<<(((g)%10)*3))
  39. #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  40.  
  41. #define GPIO_SET *(gpio+7)  // sets   bits which are 1 ignores bits which are 0
  42. #define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
  43. #define GPIO_GET *(gpio+13)  // sets   bits which are 1 ignores bits which are 0
  44.  
  45. #define ACCESS(base) *(volatile int*)((int)allof7e+base-0x7e000000)
  46. #define SETBIT(base, bit) ACCESS(base) |= 1<<bit
  47. #define CLRBIT(base, bit) ACCESS(base) &= ~(1<<bit)
  48.  
  49. void setup_io();
  50.  
  51.  
  52. #define CM_GP0CTL (0x7e101070)
  53. #define GPFSEL0 (0x7E200000)
  54. #define CM_GP0DIV (0x7e101074)
  55. #define DMABASE (0x7E007000)
  56.  
  57. struct GPCTL {
  58.     char SRC         : 4;
  59.     char ENAB        : 1;
  60.     char KILL        : 1;
  61.     char             : 1;
  62.     char BUSY        : 1;
  63.     char FLIP        : 1;
  64.     char MASH        : 2;
  65.     unsigned int     : 13;
  66.     char PASSWD      : 8;
  67. };
  68.  
  69. void setup_fm()
  70. {
  71.  
  72.     allof7e = (unsigned *)mmap(
  73.                   NULL,
  74.                   0x01000000,  //len
  75.                   PROT_READ|PROT_WRITE,
  76.                   MAP_SHARED,
  77.                   mem_fd,
  78.                   0x20000000  //base
  79.               );
  80.  
  81.     if ((int)allof7e==-1) exit(-1);
  82.  
  83.     SETBIT(GPFSEL0 , 14);
  84.     CLRBIT(GPFSEL0 , 13);
  85.     CLRBIT(GPFSEL0 , 12);
  86.  
  87.  
  88.     struct GPCTL setupword = {6/*SRC*/, 1, 0, 0, 0, 1,0x5a};
  89.  
  90.     ACCESS(CM_GP0CTL) = *((int*)&setupword);
  91.  
  92.  
  93. }
  94.  
  95. /*
  96. void delay(int i) {
  97.     for(volatile unsigned int j = 1<<i; j; j--);
  98. }
  99. */
  100.  
  101. void delay( int i)
  102. {
  103.     for(volatile unsigned int j = 1.25*(1<<i); j; j--);
  104. }
  105.  
  106.  
  107. //function slightly modified by [email protected] to allow for changing frequencies
  108. void modulate(int m)
  109. {
  110.     ACCESS(CM_GP0DIV) = (0x5a << 24) + freq_const  + m;
  111. }
  112.  
  113.  
  114. void playWav(char* filename)
  115. {
  116.     int fp = open(filename, 'r');
  117.     int sz = lseek(fp, 0L, SEEK_END);
  118.     lseek(fp, 0L, SEEK_SET);
  119.    
  120.     short* data = (short*)malloc(sz);
  121.     read(fp, data, sz);
  122.     unsigned int rnd=1;
  123.    
  124.     for (int j=22; j<sz/2; j++){ //while (read(fp, &data, 2)) {
  125.         float dval = (float)(data[j])/65536.0*25.0;
  126.         int intval = (int)(floor(dval));
  127.         float frac = dval - (float)intval;
  128.         unsigned int fracval = (unsigned int)(frac*((float)(1<<16))*((float)(1<<16)));
  129.  
  130.         for (int i=0; i<270; i++) {
  131.           rnd = (rnd >> 1) ^ (-(rnd & 1u) & 0xD0000001u);
  132.           modulate( intval + (fracval>rnd?1:0));
  133.         }
  134.         //printf("%8f  %8x %8f  %08x\n", dval, intval,  frac, fracval);
  135.         //delay(9);
  136.     }
  137.  
  138. }
  139.  
  140. //the plan is:
  141. //use PWM to trigger DMA transfer
  142.  
  143. struct CB {
  144.     unsigned int TI;
  145.     unsigned int SOURCE_AD;
  146.     unsigned int DEST_AD;
  147.     unsigned int TXFR_LEN;
  148.     unsigned int STRIDE;
  149.     unsigned int NEXTCONBK;
  150. };
  151.  
  152. struct DMAregs {
  153.     unsigned int CS;
  154.     unsigned int CONBLK_AD;
  155.     unsigned int TI;
  156.     unsigned int SOURCE_AD;
  157.     unsigned int DEST_AD;
  158.     unsigned int TXFR_LEN;
  159.     unsigned int STRIDE;
  160.     unsigned int NEXTCONBK;
  161.     unsigned int DEBUG;
  162. };
  163.  
  164. /*
  165. //TODO! Make DMA work. We decided that this needs to be a kernel mode driver
  166. void setupDMA(){
  167.    
  168.  
  169. DMAregs* DMA0 = &(ACCESS(DMABASE ));
  170.  
  171.  
  172. //activate
  173. DMA0->CS = (1<<20) | (1<<20) | 1
  174. }
  175. */
  176.  
  177.  
  178. int main(int argc, char **argv)
  179. {
  180.     int g,rep;
  181.  
  182.     // Set up gpi pointer for direct register access
  183.     setup_io();
  184.  
  185.     // Switch GPIO 7..11 to output mode
  186.  
  187.     /************************************************************************\
  188.      * You are about to change the GPIO settings of your computer.          *
  189.      * Mess this up and it will stop working!                               *
  190.      * It might be a good idea to 'sync' before running this program        *
  191.      * so at least you still have your code changes written to the SD-card! *
  192.     \************************************************************************/
  193.  
  194.     // Set GPIO pins 7-11 to output
  195.     for (g=7; g<=11; g++) {
  196.         INP_GPIO(g); // must use INP_GPIO before we can use OUT_GPIO
  197.         //OUT_GPIO(g);
  198.     }
  199.  
  200.     setup_fm();
  201.     modulate(0);
  202.  
  203.  
  204.    //if statement modified by [email protected] to check for new argument, and to set freq_const
  205.     if (argc==3){
  206.       float temp = 500.0 / atof(argv[2]);
  207.       temp *= (16*16*16);
  208.       freq_const = (int)temp;
  209.       playWav(argv[1]);
  210.    }
  211.     else
  212.       fprintf(stderr, "Usage:   program wavfile.wav frequency \n\nWhere wavfile is 16 bit 44.1kHz Monon and frequency is a floating point number\n");
  213.    
  214.     return 0;
  215.    
  216.     //setup_DMA()
  217.  
  218.     /*
  219.     int lastregs[9*15];
  220.     int inuse[9*15];
  221.     memset(&inuse, 0, 9*15*4);
  222.     for(int i = 0; i < 1000000; i++)
  223.         for (int ch = 0; ch <= 14; ch++)
  224.             for (int reg = 0; reg <= 0x20/4; reg++) {
  225.                 int curr = ACCESS((DMABASE + 0x100*ch + reg*4));
  226.                 if (lastregs[ch*9+reg] != curr) {
  227.                     inuse[ch*9+reg]++;
  228.                     lastregs[ch*9+reg] = curr;
  229.                 }
  230.  
  231.                     //printf("addr %#010x:  %#010x\n",0x100*ch + reg, ACCESS(DMABASE + 0x100*ch + reg));
  232.             }
  233.  
  234.  
  235.     for (int ch = 0; ch <= 14; ch++)
  236.         for (int reg = 0; reg <= 0x20/4; reg++)
  237.             printf("addr %#010x inuse:  %#010x\n",0x100*ch + reg, inuse[ch*9+reg]);
  238.     */
  239.  
  240.     printf("addr %#010x:  %#010x\n",0x200, ACCESS(DMABASE + 0x200));
  241.     while(!(ACCESS(DMABASE + 0x200) & 0xF << 16));
  242.     printf("addr %#010x:  %#010x\n",0x200, ACCESS(DMABASE + 0x200));
  243.  
  244.  
  245.     /* printf("start\n");
  246.      // frequency counter on gpio 7
  247.      int oldval=0;
  248.      for (int i=0; i<1000000;) {
  249.         int newval = GPIO_GET & 1<<10;
  250.         if (oldval^newval) {
  251.             oldval=newval;
  252.             i++;
  253.             }
  254.  
  255.      };
  256.      printf("done\n");
  257.      return 0;
  258.      */
  259.  
  260.  
  261.     //Sawtooth wave!
  262.     while(1) {
  263.         for(int i = -40; i <= 40; i++) {
  264.             modulate(i);
  265.             printf("Modulating: %d\n", i);
  266.             delay(25);
  267.         }
  268.     }
  269.  
  270.     for (rep=0; rep<10; rep++) {
  271.         for (g=7; g<=11; g++) {
  272.             GPIO_SET = 1<<g;
  273.         }
  274.         sleep(1);
  275.         for (g=7; g<=11; g++) {
  276.             GPIO_CLR = 1<<g;
  277.         }
  278.         sleep(1);
  279.     }
  280.  
  281.     return 0;
  282.  
  283. } // main
  284.  
  285.  
  286. //
  287. // Set up a memory regions to access GPIO
  288. //
  289. void setup_io()
  290. {
  291.     /* open /dev/mem */
  292.     if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
  293.         printf("can't open /dev/mem \n");
  294.         exit (-1);
  295.     }
  296.  
  297.     /* mmap GPIO */
  298.  
  299.     // Allocate MAP block
  300.     if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) {
  301.         printf("allocation error \n");
  302.         exit (-1);
  303.     }
  304.  
  305.     // Make sure pointer is on 4K boundary
  306.     if ((unsigned long)gpio_mem % PAGE_SIZE)
  307.         gpio_mem += PAGE_SIZE - ((unsigned long)gpio_mem % PAGE_SIZE);
  308.  
  309.     // Now map it
  310.     gpio_map = (unsigned char *)mmap(
  311.                    gpio_mem,
  312.                    BLOCK_SIZE,
  313.                    PROT_READ|PROT_WRITE,
  314.                    MAP_SHARED|MAP_FIXED,
  315.                    mem_fd,
  316.                    GPIO_BASE
  317.                );
  318.  
  319.     if ((long)gpio_map < 0) {
  320.         printf("mmap error %d\n", (int)gpio_map);
  321.         exit (-1);
  322.     }
  323.  
  324.     // Always use volatile pointer!
  325.     gpio = (volatile unsigned *)gpio_map;
  326.  
  327.  
  328. } // setup_io
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement