Sony's LANC camera control protocol project.

Dependencies:   aconno_LANC aconno_bsp aconno_SEGGER_RTT

Revision:
1:3e3dded8192f
Parent:
0:d616ca59aad8
Child:
2:b28027b9c099
--- a/main.cpp	Fri Oct 13 16:34:29 2017 +0000
+++ b/main.cpp	Mon Oct 30 12:09:10 2017 +0000
@@ -12,67 +12,112 @@
 #include "mbed.h"
 #include "acd52832_bsp.h"
 
-#define MY_BUF_SIZE     3*8*2
-#define LANC_H      0xFFFF, 0xFFFF, 0xFFFF
-#define LANC_L      0x0000, 0x0000, 0x0000
+#define MY_BUF_SIZE     13*8*2      
+#define LANC_H      0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF    /* 13 B */
+#define LANC_H_L    0xAA, 0xAA, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xAA    /* 13 B */
+#define LANC_L      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00    /* 13 B */
 
 /*
  *  Bitovi na I2S bus idu od MSBa do LSBa
  */
 
-#define REC_CMD_1           (0x18)      // 0xE7  or 0x18
-#define REC_CMD_2           (0x33)      // 0xCC  or 0x33
+#define REC_CMD_1           (0xE7)      // 0xE7  or 0x18
+#define REC_CMD_2           (0xCC)      // 0xCC  or 0x33
+
+#define LANC_COMMAND_PIN    (p26)   // Pin connected to Tr to pull lanc bus down/up     2/26
+#define LANC_PIN            (p25)   // Lanc bus pin (to scan for START/STOP bits)        3/25
 
-uint16_t my_tx_buf[MY_BUF_SIZE] = {LANC_L,LANC_L,LANC_L,LANC_H, LANC_H,LANC_L,LANC_L,LANC_L,  LANC_L,LANC_L,LANC_H,LANC_H,LANC_L,LANC_L,LANC_H,LANC_H};
+DigitalOut led(p22);
+//uint8_t my_tx_buf[MY_BUF_SIZE] = {LANC_H,LANC_L,LANC_L,LANC_H, LANC_H,LANC_L,LANC_L,LANC_L,  LANC_L,LANC_L,LANC_H,LANC_H,LANC_L,LANC_L,LANC_H,LANC_L};
+//uint8_t my_tx_buf[MY_BUF_SIZE] = {LANC_H_L,LANC_H_L,LANC_H,LANC_L, LANC_H,LANC_L,LANC_H,LANC_L,  LANC_H,LANC_L,LANC_H,LANC_L,LANC_H,LANC_L,LANC_L,LANC_H_L};
+uint8_t my_tx_buf[4] __attribute__ ((aligned (32))) = {0x01, 0x00, 0x00, 0x90};
+uint32_t *adrMyTxBuf = (uint32_t*)my_tx_buf;
+
+uint8_t empty_tx_buf[MY_BUF_SIZE] = {LANC_L,LANC_L,LANC_L,LANC_L, LANC_L,LANC_L,LANC_L,LANC_L, LANC_L,LANC_L,LANC_L,LANC_L, LANC_L,LANC_L,LANC_L,LANC_L};
 uint8_t my_rx_buf[10] = {};
 uint8_t flag = 0;
-InterruptIn button(PIN_BUTTON);
+InterruptIn button(LANC_PIN);
+
 
-void sendCommand(void){
+void startLanc(){
+        NRF_I2S->EVENTS_TXPTRUPD = 0;
+    
+        NRF_I2S->ENABLE = 1;
+    //NRF_I2S->TXD.PTR = (uint32_t)my_tx_buf;
+    
+    NRF_I2S->CONFIG.TXEN = 1;       // Enable transmission
     NRF_I2S->TASKS_START = 1;
-    while(!NRF_I2S->EVENTS_TXPTRUPD);    // Wait for the data to be send
-    NRF_I2S->EVENTS_TXPTRUPD = 0;
+        //NRF_I2S->TXD.PTR = (uint32_t)my_tx_buf;
     flag = 1;
 }
 
+void sendCommand(void){
+    //lancTicker.attach_us(startLanc, 10);
+    //startLanc();
+        NRF_I2S->EVENTS_TXPTRUPD = 0;
+        NRF_I2S->ENABLE = 1;
+    //NRF_I2S->TXD.PTR = (uint32_t)my_tx_buf;
+    
+    //NRF_I2S->CONFIG.TXEN = 1;       // Enable transmission
+    NRF_I2S->TASKS_START = 1;
+        //NRF_I2S->TXD.PTR = (uint32_t)my_tx_buf;
+    flag = 1;
+}
+
+
+
 int main(void){
+    led = 1;
+    
     NRF_I2S->CONFIG.RXEN = 0;       // Disable reception
-    NRF_I2S->CONFIG.TXEN = 1;       // Enable transmission
     NRF_I2S->CONFIG.MCKEN = 1;      // Enable MCK generator
     
     //NRF_I2S->CONFIG.MCKFREQ = 0x04100000; // DIV 63
     //NRF_I2S->CONFIG.RATIO = 0;        // Ratio = 32x
-    NRF_I2S->CONFIG.MCKFREQ = 0x08200000; // DIV 31
-    NRF_I2S->CONFIG.RATIO = 2;          // Ratio = 64x
+    NRF_I2S->CONFIG.MCKFREQ = 0x10000000; // DIV 31
+    NRF_I2S->CONFIG.RATIO = 0;          // Ratio = 32x
     
-    NRF_I2S->CONFIG.SWIDTH = 1;         // Sample width = 16 bit
-    NRF_I2S->CONFIG.ALIGN = 1;          // Alignment = Right
+    NRF_I2S->CONFIG.SWIDTH = 0;         // Sample width = 8 bit
+    NRF_I2S->CONFIG.ALIGN = 0;          // Alignment = Right
     NRF_I2S->CONFIG.FORMAT = 0;         // Format = I2S
     NRF_I2S->CONFIG.CHANNELS = 0;       // Use stereo
     
     
+    
     // In debug mode
-    NRF_I2S->PSEL.LRCK = 26;            // LRCK routed to pin 26
-    NRF_I2S->PSEL.SDOUT = 28;           // SDOUT routed to pin 28
-    NRF_I2S->PSEL.SCK = 30;             // SCK routed to pin 30
-    NRF_I2S->PSEL.MCK = 0x80000000;     // MCK disconnected
-    NRF_I2S->PSEL.SDIN = 0x80000000;    // SDIN disconnected
+    NRF_I2S->PSEL.LRCK = 27;                // LRCK routed to pin 26
+    NRF_I2S->PSEL.SDOUT = LANC_COMMAND_PIN; // SDOUT routed to pin 28
+    NRF_I2S->PSEL.SCK = 30;                 // SCK routed to pin 30
+    NRF_I2S->PSEL.MCK = 0x80000000;         // MCK disconnected
+    NRF_I2S->PSEL.SDIN = 0x80000000;        // SDIN disconnected
     
-    NRF_I2S->TXD.PTR = (uint32_t)my_tx_buf;
-    NRF_I2S->RXD.PTR = (uint32_t)my_rx_buf;
-    NRF_I2S->RXTXD.MAXCNT = MY_BUF_SIZE/2;      // Ajde ustanovi zašto pobogu /2
-    
-    NRF_I2S->ENABLE = 1;
+    NRF_I2S->TXD.PTR = (uint32_t)adrMyTxBuf;
+    //NRF_I2S->RXTXD.MAXCNT = MY_BUF_SIZE/4;  // Div with 4 cuz that's number of 32 bit words
+    //NRF_I2S->RXTXD.MAXCNT = 52;
+        NRF_I2S->RXTXD.MAXCNT = 1;
+        
+    //NRF_I2S->ENABLE = 1;
     //NRF_I2S->TASKS_START = 1;
     
-    button.rise(sendCommand);
+    button.fall(sendCommand);
     while(1){
         if(flag){
-            wait_ms(50);
-            NRF_I2S->TASKS_STOP = 1;
-            while(!NRF_I2S->EVENTS_STOPPED);
-            NRF_I2S->EVENTS_STOPPED = 0;
+                        //NRF_I2S->TXD.PTR = (uint32_t)(my_tx_buf);
+            led = 0;
+            while(!NRF_I2S->EVENTS_TXPTRUPD);    // Wait for the data to be send
+            NRF_I2S->EVENTS_TXPTRUPD = 0;
+            while(!NRF_I2S->EVENTS_TXPTRUPD);    // Wait for the data to be send
+            NRF_I2S->EVENTS_TXPTRUPD = 0;
+            //NRF_I2S->TXD.PTR = (uint32_t)empty_tx_buf;
+            //wait_us(2000);
+            
+            //NRF_I2S->TASKS_STOP = 1;
+            //while(!NRF_I2S->EVENTS_STOPPED);
+            //NRF_I2S->EVENTS_STOPPED = 0;
+            NRF_I2S->ENABLE = 0;
+            //NRF_I2S->TXD.PTR = (uint32_t)my_tx_buf;
             flag = 0;
+            led = 1;
         }
     }   
 }