Guest User

Untitled

a guest
Feb 8th, 2020
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.14 KB | None | 0 0
  1. /********************************************* ADC Controller Details ****************************************
  2.     1) Board ADC
  3.     Board:              DE1-SoC (rev. F+)
  4.     ADC Chip:           LTC2308
  5.     ADC Clock Freq:     0.01 - 20 MHz
  6.     Voltage Range:      0 - 5 V
  7.     Channels:           8
  8.     Resolution:         12 bit
  9.    
  10.     2) Available Registers
  11.      ---------------------------------------------------------------------------------------------------------
  12.     |   Offset in bytes    |    Register name      |    Read/Write     |    Purpose                           |
  13.      ---------------------------------------------------------------------------------------------------------
  14.     |           0          |         CH_0          |        R          |    Converted value of channel 0      |
  15.     |                      |        Update         |        W          |    Update the converted values       |
  16.      ---------------------------------------------------------------------------------------------------------
  17.     |           4          |         CH_1          |        R          |    Converted value of channel 1      |
  18.     |                      |      Auto-Update      |        W          |    Enables or disables auto-updating |
  19.      ---------------------------------------------------------------------------------------------------------
  20.     |           8          |         CH_2          |        R          |    Converted value of channel 2      |
  21.      ---------------------------------------------------------------------------------------------------------
  22.     |           12         |         CH_3          |        R          |    Converted value of channel 3      |
  23.      ---------------------------------------------------------------------------------------------------------
  24.     |           16         |         CH_4          |        R          |    Converted value of channel 4      |
  25.      ---------------------------------------------------------------------------------------------------------
  26.     |           20         |         CH_5          |        R          |    Converted value of channel 5      |
  27.      ---------------------------------------------------------------------------------------------------------
  28.     |           24         |         CH_6          |        R          |    Converted value of channel 6      |
  29.      ---------------------------------------------------------------------------------------------------------
  30.     |           28         |         CH_7          |        R          |    Converted value of channel 7      |
  31.      ---------------------------------------------------------------------------------------------------------
  32.                                                     Table 1
  33.     3) Software Programming Model (Theory) source: Intel University Program
  34.    
  35.         3.1) Register Map
  36.        
  37.             The ADC Controller for DE-series Boards IP Core provides eight registers for reading and two for writing, as
  38.             shown in Table 1. The eight readable registers contain the outputs from the ADC for the eight analog inputs. The
  39.             two writable registers are used to control the ADC. Writing to the Update register triggers an update of the stored
  40.             conversions, and writing to Auto-Update enables or disables the automatic update feature.
  41.            
  42.             3.1.1) Channel Registers
  43.                 These eight registers hold the 12-bit outputs from the eight ADC channels. They are refreshed upon completion of an
  44.                 update operation. An update operation can be triggered manually by writing to the Update register, or automatically
  45.                 by enabling the Auto-Update mode via the Auto-Update register. In Auto-Update mode, the 16th bit of the channel
  46.                 register acts as a refresh flag. After all channels are refreshed, the flags are high. Upon reading a channel, that
  47.                 channel’s flag is set to low.
  48.                
  49.             3.1.2) Update Register
  50.                 Writing any value to the Update register begins a conversion cycle on the ADC. During this time, all desired channels
  51.                 (as specified in the Platform Designer configuration wizard) are sampled. The new values become available in the
  52.                 Channel registers once the entire update operation has finished. If reads to the channel registers are attempted
  53.                 during the conversion cycle, the wait_request signal will be raised, causing the processor to stall until the update has
  54.                 finished.
  55.                
  56.             3.1.3) Auto-Update Register
  57.                     On system startup, this register will be loaded with a zero value. Writing a ‘1’ to this register will enable autoupdating,
  58.                 while writing a ‘0’ will disable it.
  59.                     When auto-update is enabled, the system will automatically begin another update operation after the previous one
  60.                 finishes. Additionally, if reads to the channel registers are attempted during an update operation, the stored values
  61.                 from the previous update operation will be read without waiting for the latest update to finish. This is in contrast to
  62.                 a read during an update operation triggered by the Update register, where the wait_request signal would be asserted
  63.                 until the current update operation finishes.
  64.                
  65.    
  66. ****************************************** end of ADC Controller Details ************************************/
  67.  
  68.  
  69.  
  70. #include <stdio.h>
  71. #include <string.h>
  72. #include <stdlib.h>
  73. #include <unistd.h>
  74. #include <fcntl.h>
  75. #include <sys/types.h>
  76. #include <sys/ipc.h>
  77. #include <sys/shm.h>
  78. #include <sys/mman.h>
  79. #include <sys/time.h>
  80.  
  81. #define ADC_BASE    0x000000000 // intended for pointer use
  82.  
  83.  
  84. // "/dev/mem" file id
  85. int fd;
  86.  
  87. // the adc base base
  88. void *adc_virtual_base;
  89.  
  90. // pointer to the ADC base
  91. volatile unsigned int * adc_base_ptr = NULL ;
  92.  
  93. int main(void) {
  94.     // declare volatile pointers to I/O registers (volatile means that IO load and store instructions will be used
  95.     // to access these pointer locations,
  96.     // instead of regular memory loads and stores)
  97.    
  98.     // === get FPGA addresses ==================
  99.     // Open /dev/mem. Please visit http://man7.org/linux/man-pages/man2/open.2.html for more details about mmap()
  100.     if( ( fd = open( "/dev/mem", ( O_RDWR | O_SYNC ) ) ) == -1 )    {
  101.         printf( "ERROR: could not open \"/dev/mem\"...\n" );
  102.         return( 1 );
  103.     }
  104.    
  105.     // get virtual addr that maps to physical. Please visit http://man7.org/linux/man-pages/man2/mmap.2.html for more details about mmap()
  106.     adc_virtual_base = mmap( NULL, 4096, ( PROT_READ | PROT_WRITE ), MAP_SHARED, fd, ADC_BASE );   
  107.     if( adc_virtual_base == MAP_FAILED ) {
  108.         printf( "ERROR: mmap() failed...\n" );
  109.         close( fd );
  110.         return(1);
  111.     }
  112.    
  113.     adc_base_ptr = (unsigned int *) adc_virtual_base;
  114.    
  115.     // declare & intialize utility variables before using them
  116.     int channel_values [8];     // will store channel values in each cycle
  117.    
  118.     // poll ADC
  119.     while(1) {
  120.  
  121.         // initiate an update request by writing to the UPDATE register
  122.         *(adc_base_ptr) = 0;
  123.        
  124.         // read values from all 8 channels
  125.         for(int i = 0; i < 8; i++) {
  126.             channel_values[i] = *(adc_base_ptr + i);
  127.         }
  128.        
  129.         // print values. Only bit 11 to bit 0 are printed. Rest are ignored.
  130.         for(int i = 0; i < 8; i++) {
  131.             printf("Channel %2d: %04d\n", i, channel_values[i] & 0xFFF);
  132.         }
  133.  
  134.         // new lines
  135.         printf("\n\n");
  136.         // clear screen before printing again
  137.         system("clear");
  138.         usleep(100);
  139.     }
  140.    
  141.     // clean up our memory mapping and exit
  142.    
  143.     if( munmap( adc_virtual_base, 4096 ) != 0 ) {
  144.         printf( "ERROR: munmap() failed...\n" );
  145.         close( fd );
  146.         return( 1 );
  147.     }
  148.  
  149.     close( fd );
  150.    
  151. }
Advertisement
Add Comment
Please, Sign In to add comment