Advertisement
RuiViana

Camera

Jan 18th, 2020
1,358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.57 KB | None | 0 0
  1. #include "driver/i2s.h"
  2. #include "rom/lldesc.h"
  3. #define bot 4
  4.  
  5. // Meu ESP  3V3 GND D15 D2 D4 RX2 TX2 D5 D18 D19 D21 RX0 TX0 D22 D23
  6. //          VIN GND D13 D12 D14 D27 D26 D25 D33 D32 D35 D34 VN VP EN
  7.  
  8. // Port livres  D2 D13 D12 D14 D32 D35 D34
  9.  
  10. long ledClock = 20000000;                      // determine a frequencia do PWM
  11. bool roda = false;
  12.  
  13. DMA_ATTR uint32_t dma_buff[2][100];
  14. DMA_ATTR lldesc_t dma_descriptor[2];
  15. //----------------------------------------------------------------------------
  16. void setup()
  17. {
  18.   Serial.begin(115200);
  19.   Serial.println(ESP.getSdkVersion());
  20.   pinMode(33, OUTPUT);                        // GPIO_ as Output de PWM
  21.   //  pinMode(5, OUTPUT);                        // GPIO_ as Output de PWM
  22.  
  23.   pinMode(bot, INPUT_PULLUP);                   // GPIO_ as Input button
  24.  
  25.   ledcAttachPin(33, 0);                       // GPIO_18 attached to PWM Channel 0
  26.   ledcSetup(0, ledClock, 1);                  // Channel 0 , freq ledClock , 1 bit resolution = 2
  27.   ledcWrite(0, 1);                            // Enable frequency with dutty cycle 50%
  28.   configue();
  29. }
  30. //----------------------------------------------------------------------------
  31. void configue()
  32. {
  33.   //buff 0
  34.   dma_descriptor[0].length = 0; //number of byte written to the buffer
  35.   dma_descriptor[0].size = sizeof(dma_buff[0]); //max size of the buffer in bytes
  36.   dma_descriptor[0].owner = 1;
  37.   dma_descriptor[0].sosf = 1;
  38.   dma_descriptor[0].buf = (uint8_t*)&dma_buff[0][0];
  39.   dma_descriptor[0].offset = 0;
  40.   dma_descriptor[0].empty = 0;
  41.   dma_descriptor[0].eof = 0;
  42.   //pointer to the next descriptor
  43.   dma_descriptor[0].qe.stqe_next = &dma_descriptor[1];
  44.  
  45.   //buff 1
  46.   dma_descriptor[1].length = 0; //number of byte written to the buffer
  47.   dma_descriptor[1].size = sizeof(dma_buff[1]); //max size of the buffer in bytes
  48.   dma_descriptor[1].owner = 1;
  49.   dma_descriptor[1].sosf = 1;
  50.   dma_descriptor[1].buf = (uint8_t*)&dma_buff[1][0];
  51.   dma_descriptor[1].offset = 0;
  52.   dma_descriptor[1].empty = 0;
  53.   dma_descriptor[1].eof = 0;
  54.   //pointer to the next descriptor
  55.   dma_descriptor[1].qe.stqe_next = &dma_descriptor[0];
  56.  
  57.   //data inputs
  58.   pinMode(18, INPUT);
  59.   pinMode(23, INPUT);
  60.   pinMode(19, INPUT);
  61.   pinMode(21, INPUT);
  62.   pinMode(22, INPUT);
  63.   pinMode(25, INPUT);
  64.   pinMode(26, INPUT);
  65.   pinMode(27, INPUT);
  66.   pinMode(15, INPUT);
  67.  
  68.   gpio_matrix_in(18,    I2S0I_DATA_IN0_IDX, false);
  69.   gpio_matrix_in(23,    I2S0I_DATA_IN1_IDX, false);
  70.   gpio_matrix_in(19,    I2S0I_DATA_IN2_IDX, false);
  71.   gpio_matrix_in(21,    I2S0I_DATA_IN3_IDX, false);
  72.   gpio_matrix_in(22,    I2S0I_DATA_IN4_IDX, false);
  73.   gpio_matrix_in(25,    I2S0I_DATA_IN5_IDX, false);
  74.   gpio_matrix_in(26,    I2S0I_DATA_IN6_IDX, false);
  75.   gpio_matrix_in(27,    I2S0I_DATA_IN7_IDX, false);
  76.  
  77.   gpio_matrix_in(15/*XCLK*/, I2S0I_WS_IN_IDX, false);
  78.  
  79.   //for i2s in parallel camera input mode data is receiver only when V_SYNC = H_SYNC = H_ENABLE = 1.
  80.   //We don't use these inputs so simply set them High
  81.   gpio_matrix_in(0x38, I2S0I_V_SYNC_IDX, false);
  82.   gpio_matrix_in(0x38, I2S0I_H_SYNC_IDX, false);  //0x30 sends 0, 0x38 sends 1
  83.   gpio_matrix_in(0x38, I2S0I_H_ENABLE_IDX, false);
  84. }
  85. //----------------------------------------------------------------------------
  86. void capture()
  87. {
  88.   // Enable and configure I2S peripheral
  89.   periph_module_enable(PERIPH_I2S0_MODULE);
  90.  
  91.   // Enable slave mode (sampling clock is external)
  92.   I2S0.conf.rx_slave_mod = 1;
  93.   // Enable parallel mode
  94.   I2S0.conf2.lcd_en = 1;
  95.  
  96.   // Use HSYNC/VSYNC/HREF to control sampling
  97.   I2S0.conf2.camera_en = 1;
  98.  
  99.   // Configure clock divider
  100.   I2S0.clkm_conf.clkm_div_a = 1;
  101.   I2S0.clkm_conf.clkm_div_b = 0;
  102.   I2S0.clkm_conf.clkm_div_num = 2;
  103.   I2S0.sample_rate_conf.rx_bck_div_num = 2; //i2s clk is divided before reaches BCK output
  104.  
  105.   // FIFO will sink data to DMA
  106.   I2S0.fifo_conf.dscr_en = 1;
  107.   //  I2S0.fifo_conf.tx_fifo_mod_force_en = 1;
  108.   I2S0.fifo_conf.rx_fifo_mod_force_en = 1;
  109.   // FIFO configuration
  110.   //two bytes per dword packing
  111.   I2S0.fifo_conf.rx_fifo_mod = 0;
  112.   I2S0.conf_chan.rx_chan_mod = 1;
  113.   // Clear flags which are used in I2S serial mode
  114.   I2S0.sample_rate_conf.rx_bits_mod = 0;
  115.   I2S0.conf.rx_right_first = 0;
  116.   I2S0.conf.rx_msb_right = 0;
  117.   I2S0.conf.rx_msb_shift = 0;
  118.   I2S0.conf.rx_mono = 0;
  119.   I2S0.conf.rx_short_sync = 0;
  120.   I2S0.timing.val = 0;
  121.  
  122.   //start i2s
  123.   I2S0.rx_eof_num = 0xFFFFFFFF;
  124.   I2S0.in_link.addr = (uint32_t)&dma_descriptor[0];
  125.   I2S0.in_link.start = 1;
  126.   I2S0.int_clr.val = I2S0.int_raw.val;
  127.   I2S0.int_ena.val = 0;
  128.  
  129.   //start i2s + dma
  130.   I2S0.conf.rx_start = 1;
  131.  
  132.   //   delay(10000);
  133.   delay(500);
  134.  
  135.   //stop i2s + dma
  136.   I2S0.conf.rx_start = 0;
  137.  
  138.   //  Serial.print( sizeof(dma_buff[0]));
  139.   for (uint16_t i = 0; i < sizeof(dma_buff[0]) / 2; i++)
  140.   {
  141.     //   for (int j = 0 ; j < 1; j++)
  142.     //   {
  143.     Serial.print(*(((uint16_t *)&dma_buff[0][0]) + i), HEX);
  144.     //     Serial.print(bitRead(*(((uint16_t *)&dma_buff[0][0]) + i), j));
  145.     //     Serial.print(",");
  146.     //     Serial.print(*(((uint16_t *)&dma_buff[1][1]) + i));
  147.     //     Serial.print(bitRead(*(((uint16_t *)&dma_buff[1][1]) + i), j));
  148.     // digitalWrite(5, bitRead(*(((uint16_t *)&dma_buff[0][0]) + i), j));
  149.     // Serial.print(",");
  150.     //    }
  151.     Serial.println(" ");
  152.     delay(10);
  153.   }
  154. }
  155. //----------------------------------------------------------------------------
  156. void loop()
  157. {
  158.   if (digitalRead(bot) == LOW)
  159.   {
  160.     delay(30);
  161.     if (digitalRead(bot) == LOW)
  162.     {
  163.       roda = true;
  164.     }
  165.   }
  166.   if (roda == true)
  167.   {
  168.     if (digitalRead(bot) == HIGH)
  169.     {
  170.       capture();
  171.       roda = false;
  172.     }
  173.   }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement