Sony's LANC camera control protocol project.

Dependencies:   aconno_LANC aconno_bsp aconno_SEGGER_RTT

Committer:
jurica238814
Date:
Wed Nov 01 12:52:30 2017 +0000
Revision:
4:6560e2966186
Parent:
3:eb5b87baf6cf
Child:
5:ecb7712b0825
Start/Stop rec, wide, tele works =)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 0:d616ca59aad8 1 /*
jurica238814 0:d616ca59aad8 2 * Example to demonstrate usage of the nrf52's I2S interface
jurica238814 0:d616ca59aad8 3 *
jurica238814 0:d616ca59aad8 4 * Made by Jurica Resetar @ aconno
jurica238814 0:d616ca59aad8 5 * jurica_resetar@yahoo.com
jurica238814 0:d616ca59aad8 6 * More info @ aconno.de
jurica238814 0:d616ca59aad8 7 *
jurica238814 0:d616ca59aad8 8 * All rights reserved
jurica238814 0:d616ca59aad8 9 *
jurica238814 0:d616ca59aad8 10 */
jurica238814 0:d616ca59aad8 11
jurica238814 0:d616ca59aad8 12 #include "mbed.h"
jurica238814 0:d616ca59aad8 13 #include "acd52832_bsp.h"
jurica238814 0:d616ca59aad8 14
jurica238814 2:b28027b9c099 15 #define MY_BUF_SIZE 13*8
jurica238814 1:3e3dded8192f 16 #define LANC_H 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF /* 13 B */
jurica238814 2:b28027b9c099 17 #define LANC_H_L 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF /* 13 B */
jurica238814 1:3e3dded8192f 18 #define LANC_L 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /* 13 B */
jurica238814 0:d616ca59aad8 19
jurica238814 0:d616ca59aad8 20 /*
jurica238814 0:d616ca59aad8 21 * Bitovi na I2S bus idu od MSBa do LSBa
jurica238814 0:d616ca59aad8 22 */
jurica238814 0:d616ca59aad8 23
jurica238814 2:b28027b9c099 24 #define LANC_COMMAND_PIN (p2) // Pin connected to Tr to pull lanc bus down/up 2/26
jurica238814 2:b28027b9c099 25 #define LANC_PIN (p3) // Lanc bus pin (to scan for START/STOP bits) 3/25
jurica238814 0:d616ca59aad8 26
jurica238814 2:b28027b9c099 27 DigitalOut alive(p24);
jurica238814 2:b28027b9c099 28
jurica238814 2:b28027b9c099 29 uint8_t normalCommand[MY_BUF_SIZE] __attribute__ ((aligned (32))) = {LANC_L,LANC_L,LANC_L,LANC_H, LANC_H,LANC_L,LANC_L,LANC_L};
jurica238814 4:6560e2966186 30 uint8_t specialCommand[MY_BUF_SIZE] __attribute__ ((aligned (32))) = {LANC_L,LANC_L,LANC_L,LANC_H, LANC_L,LANC_H,LANC_L,LANC_L};
jurica238814 2:b28027b9c099 31 uint8_t startStop[MY_BUF_SIZE] __attribute__ ((aligned (32))) = {LANC_H,LANC_H,LANC_L,LANC_L,LANC_H,LANC_H,LANC_L,LANC_L};
jurica238814 4:6560e2966186 32 uint8_t zoomIn[MY_BUF_SIZE] __attribute__ ((aligned (32))) = {LANC_H,LANC_L,LANC_L,LANC_H,LANC_H,LANC_H,LANC_L,LANC_L}; // Tele
jurica238814 4:6560e2966186 33 uint8_t zoomOut[MY_BUF_SIZE] __attribute__ ((aligned (32))) = {LANC_H,LANC_H,LANC_L,LANC_H,LANC_H,LANC_H,LANC_L,LANC_L}; // Wide
jurica238814 1:3e3dded8192f 34
jurica238814 4:6560e2966186 35 uint32_t *normalCmdAddr = (uint32_t*)specialCommand;
jurica238814 4:6560e2966186 36 uint32_t *startStopAddr = (uint32_t*)zoomOut;
jurica238814 2:b28027b9c099 37
jurica238814 2:b28027b9c099 38 uint8_t firstInt = 1;
jurica238814 2:b28027b9c099 39 uint8_t flag = 0;
jurica238814 4:6560e2966186 40 uint8_t state = 0; /* 1 -> Send command type, 0 -> send command */
jurica238814 1:3e3dded8192f 41 InterruptIn button(LANC_PIN);
jurica238814 2:b28027b9c099 42 Timer frameTimer;
jurica238814 1:3e3dded8192f 43
jurica238814 0:d616ca59aad8 44
jurica238814 2:b28027b9c099 45 void sendCommand(void){
jurica238814 3:eb5b87baf6cf 46 static int lastIntTime_us = 0;
jurica238814 1:3e3dded8192f 47
jurica238814 2:b28027b9c099 48 /*
jurica238814 2:b28027b9c099 49 * Na prvi interrupt pokreni frameTimer.
jurica238814 2:b28027b9c099 50 * Na svaki interrupt (falling edge na Lanc busu) izmjeri vrijeme od zadnjeg eventa
jurica238814 2:b28027b9c099 51 * Ako je to vrijeme > 5ms, onda je upravo taj prekid izazvao start bit novog framea
jurica238814 2:b28027b9c099 52 * U tom slučaju, kreni s donjim kodom, inaće nemoj ništa slati
jurica238814 2:b28027b9c099 53 */
jurica238814 2:b28027b9c099 54 if(firstInt){
jurica238814 2:b28027b9c099 55 firstInt = 0;
jurica238814 2:b28027b9c099 56 frameTimer.start();
jurica238814 2:b28027b9c099 57 }
jurica238814 2:b28027b9c099 58
jurica238814 3:eb5b87baf6cf 59 lastIntTime_us = frameTimer.read_us();
jurica238814 2:b28027b9c099 60 frameTimer.reset();
jurica238814 1:3e3dded8192f 61
jurica238814 3:eb5b87baf6cf 62 if(lastIntTime_us > 10000 || state == 1){
jurica238814 2:b28027b9c099 63 state++;
jurica238814 2:b28027b9c099 64
jurica238814 2:b28027b9c099 65 if(state == 1){
jurica238814 2:b28027b9c099 66 NRF_I2S->TXD.PTR = (uint32_t)normalCmdAddr;
jurica238814 2:b28027b9c099 67 }
jurica238814 2:b28027b9c099 68 else if(state == 2){
jurica238814 2:b28027b9c099 69 NRF_I2S->TXD.PTR = (uint32_t)startStopAddr;
jurica238814 3:eb5b87baf6cf 70 state = 0;
jurica238814 2:b28027b9c099 71 }
jurica238814 2:b28027b9c099 72
jurica238814 2:b28027b9c099 73 if(state<=2){
jurica238814 2:b28027b9c099 74 __disable_irq();
jurica238814 2:b28027b9c099 75 // First or second start bit
jurica238814 2:b28027b9c099 76 wait_us(60); // Small delay for first bit after start bit
jurica238814 2:b28027b9c099 77 NRF_I2S->EVENTS_TXPTRUPD = 0;
jurica238814 2:b28027b9c099 78 NRF_I2S->ENABLE = 1;
jurica238814 2:b28027b9c099 79 NRF_I2S->TASKS_START = 1;
jurica238814 2:b28027b9c099 80 flag = 1;
jurica238814 2:b28027b9c099 81 }
jurica238814 2:b28027b9c099 82 }
jurica238814 1:3e3dded8192f 83 }
jurica238814 1:3e3dded8192f 84
jurica238814 1:3e3dded8192f 85
jurica238814 1:3e3dded8192f 86
jurica238814 0:d616ca59aad8 87 int main(void){
jurica238814 2:b28027b9c099 88 alive = 0;
jurica238814 1:3e3dded8192f 89
jurica238814 0:d616ca59aad8 90 NRF_I2S->CONFIG.RXEN = 0; // Disable reception
jurica238814 0:d616ca59aad8 91 NRF_I2S->CONFIG.MCKEN = 1; // Enable MCK generator
jurica238814 4:6560e2966186 92
jurica238814 1:3e3dded8192f 93 NRF_I2S->CONFIG.MCKFREQ = 0x10000000; // DIV 31
jurica238814 1:3e3dded8192f 94 NRF_I2S->CONFIG.RATIO = 0; // Ratio = 32x
jurica238814 0:d616ca59aad8 95
jurica238814 1:3e3dded8192f 96 NRF_I2S->CONFIG.SWIDTH = 0; // Sample width = 8 bit
jurica238814 1:3e3dded8192f 97 NRF_I2S->CONFIG.ALIGN = 0; // Alignment = Right
jurica238814 0:d616ca59aad8 98 NRF_I2S->CONFIG.FORMAT = 0; // Format = I2S
jurica238814 0:d616ca59aad8 99 NRF_I2S->CONFIG.CHANNELS = 0; // Use stereo
jurica238814 0:d616ca59aad8 100
jurica238814 1:3e3dded8192f 101 NRF_I2S->PSEL.LRCK = 27; // LRCK routed to pin 26
jurica238814 1:3e3dded8192f 102 NRF_I2S->PSEL.SDOUT = LANC_COMMAND_PIN; // SDOUT routed to pin 28
jurica238814 1:3e3dded8192f 103 NRF_I2S->PSEL.SCK = 30; // SCK routed to pin 30
jurica238814 1:3e3dded8192f 104 NRF_I2S->PSEL.MCK = 0x80000000; // MCK disconnected
jurica238814 1:3e3dded8192f 105 NRF_I2S->PSEL.SDIN = 0x80000000; // SDIN disconnected
jurica238814 0:d616ca59aad8 106
jurica238814 2:b28027b9c099 107 NRF_I2S->TXD.PTR = (uint32_t)normalCmdAddr;
jurica238814 2:b28027b9c099 108 NRF_I2S->RXTXD.MAXCNT = MY_BUF_SIZE/4; // Div with 4 cuz that's number of 32 bit words
jurica238814 2:b28027b9c099 109
jurica238814 1:3e3dded8192f 110 button.fall(sendCommand);
jurica238814 0:d616ca59aad8 111 while(1){
jurica238814 4:6560e2966186 112 if(flag){
jurica238814 1:3e3dded8192f 113 while(!NRF_I2S->EVENTS_TXPTRUPD); // Wait for the data to be send
jurica238814 1:3e3dded8192f 114 NRF_I2S->EVENTS_TXPTRUPD = 0;
jurica238814 1:3e3dded8192f 115 while(!NRF_I2S->EVENTS_TXPTRUPD); // Wait for the data to be send
jurica238814 2:b28027b9c099 116 NRF_I2S->EVENTS_TXPTRUPD = 0;
jurica238814 1:3e3dded8192f 117
jurica238814 2:b28027b9c099 118 NRF_I2S->TASKS_STOP = 1;
jurica238814 2:b28027b9c099 119 while(!NRF_I2S->EVENTS_STOPPED);
jurica238814 1:3e3dded8192f 120 NRF_I2S->ENABLE = 0;
jurica238814 0:d616ca59aad8 121 flag = 0;
jurica238814 2:b28027b9c099 122 __enable_irq();
jurica238814 0:d616ca59aad8 123 }
jurica238814 2:b28027b9c099 124 }
jurica238814 0:d616ca59aad8 125 }
jurica238814 0:d616ca59aad8 126
jurica238814 0:d616ca59aad8 127