Sony's LANC camera control protocol project.

Dependencies:   aconno_LANC aconno_bsp aconno_SEGGER_RTT

Committer:
jurica238814
Date:
Wed Nov 01 12:13:07 2017 +0000
Revision:
3:eb5b87baf6cf
Parent:
2:b28027b9c099
Child:
4:6560e2966186
Record command works! Wuhu :D

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 1:3e3dded8192f 24 #define REC_CMD_1 (0xE7) // 0xE7 or 0x18
jurica238814 1:3e3dded8192f 25 #define REC_CMD_2 (0xCC) // 0xCC or 0x33
jurica238814 1:3e3dded8192f 26
jurica238814 2:b28027b9c099 27 #define LANC_COMMAND_PIN (p2) // Pin connected to Tr to pull lanc bus down/up 2/26
jurica238814 2:b28027b9c099 28 #define LANC_PIN (p3) // Lanc bus pin (to scan for START/STOP bits) 3/25
jurica238814 0:d616ca59aad8 29
jurica238814 1:3e3dded8192f 30 DigitalOut led(p22);
jurica238814 2:b28027b9c099 31 DigitalOut alive(p24);
jurica238814 2:b28027b9c099 32
jurica238814 2:b28027b9c099 33 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 2:b28027b9c099 34 //uint8_t startStop[MY_BUF_SIZE] __attribute__ ((aligned (32))) = {LANC_L,LANC_L,LANC_H,LANC_H,LANC_L,LANC_L,LANC_H,LANC_H_L};
jurica238814 2:b28027b9c099 35 //uint8_t normalCommand[MY_BUF_SIZE] __attribute__ ((aligned (32))) = {LANC_L,LANC_H,LANC_H,LANC_L, LANC_L,LANC_H,LANC_H,LANC_H};
jurica238814 2:b28027b9c099 36 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 1:3e3dded8192f 37
jurica238814 2:b28027b9c099 38 uint32_t *normalCmdAddr = (uint32_t*)normalCommand;
jurica238814 2:b28027b9c099 39 uint32_t *startStopAddr = (uint32_t*)startStop;
jurica238814 2:b28027b9c099 40
jurica238814 2:b28027b9c099 41 uint8_t firstInt = 1;
jurica238814 2:b28027b9c099 42 uint8_t flag = 0;
jurica238814 2:b28027b9c099 43 uint8_t state = 0; /* 0 -> Send command type, 1-> send command */
jurica238814 1:3e3dded8192f 44 InterruptIn button(LANC_PIN);
jurica238814 2:b28027b9c099 45 Timer lancTimer;
jurica238814 2:b28027b9c099 46 Timer frameTimer;
jurica238814 1:3e3dded8192f 47
jurica238814 0:d616ca59aad8 48
jurica238814 2:b28027b9c099 49 void sendCommand(void){
jurica238814 2:b28027b9c099 50 static int timePassed_ms = 0;
jurica238814 3:eb5b87baf6cf 51 static int lastIntTime_us = 0;
jurica238814 1:3e3dded8192f 52
jurica238814 2:b28027b9c099 53 /*
jurica238814 2:b28027b9c099 54 * Na prvi interrupt pokreni frameTimer.
jurica238814 2:b28027b9c099 55 * Na svaki interrupt (falling edge na Lanc busu) izmjeri vrijeme od zadnjeg eventa
jurica238814 2:b28027b9c099 56 * Ako je to vrijeme > 5ms, onda je upravo taj prekid izazvao start bit novog framea
jurica238814 2:b28027b9c099 57 * U tom slučaju, kreni s donjim kodom, inaće nemoj ništa slati
jurica238814 2:b28027b9c099 58 */
jurica238814 2:b28027b9c099 59 if(firstInt){
jurica238814 2:b28027b9c099 60 firstInt = 0;
jurica238814 2:b28027b9c099 61 frameTimer.start();
jurica238814 2:b28027b9c099 62 }
jurica238814 2:b28027b9c099 63
jurica238814 3:eb5b87baf6cf 64 lastIntTime_us = frameTimer.read_us();
jurica238814 2:b28027b9c099 65 frameTimer.reset();
jurica238814 1:3e3dded8192f 66
jurica238814 3:eb5b87baf6cf 67 if(lastIntTime_us > 10000 || state == 1){
jurica238814 2:b28027b9c099 68 state++;
jurica238814 2:b28027b9c099 69
jurica238814 2:b28027b9c099 70 if(state == 1){
jurica238814 2:b28027b9c099 71 NRF_I2S->TXD.PTR = (uint32_t)normalCmdAddr;
jurica238814 2:b28027b9c099 72 }
jurica238814 2:b28027b9c099 73 else if(state == 2){
jurica238814 2:b28027b9c099 74 NRF_I2S->TXD.PTR = (uint32_t)startStopAddr;
jurica238814 3:eb5b87baf6cf 75 state = 0;
jurica238814 2:b28027b9c099 76 }
jurica238814 2:b28027b9c099 77
jurica238814 2:b28027b9c099 78 if(state<=2){
jurica238814 2:b28027b9c099 79 __disable_irq();
jurica238814 2:b28027b9c099 80 // First or second start bit
jurica238814 2:b28027b9c099 81 wait_us(60); // Small delay for first bit after start bit
jurica238814 2:b28027b9c099 82 NRF_I2S->EVENTS_TXPTRUPD = 0;
jurica238814 2:b28027b9c099 83 NRF_I2S->ENABLE = 1;
jurica238814 2:b28027b9c099 84 NRF_I2S->TASKS_START = 1;
jurica238814 2:b28027b9c099 85 flag = 1;
jurica238814 2:b28027b9c099 86 }
jurica238814 2:b28027b9c099 87 }
jurica238814 1:3e3dded8192f 88 }
jurica238814 1:3e3dded8192f 89
jurica238814 1:3e3dded8192f 90
jurica238814 1:3e3dded8192f 91
jurica238814 0:d616ca59aad8 92 int main(void){
jurica238814 1:3e3dded8192f 93 led = 1;
jurica238814 2:b28027b9c099 94 alive = 0;
jurica238814 1:3e3dded8192f 95
jurica238814 0:d616ca59aad8 96 NRF_I2S->CONFIG.RXEN = 0; // Disable reception
jurica238814 0:d616ca59aad8 97 NRF_I2S->CONFIG.MCKEN = 1; // Enable MCK generator
jurica238814 0:d616ca59aad8 98
jurica238814 0:d616ca59aad8 99 //NRF_I2S->CONFIG.MCKFREQ = 0x04100000; // DIV 63
jurica238814 0:d616ca59aad8 100 //NRF_I2S->CONFIG.RATIO = 0; // Ratio = 32x
jurica238814 1:3e3dded8192f 101 NRF_I2S->CONFIG.MCKFREQ = 0x10000000; // DIV 31
jurica238814 1:3e3dded8192f 102 NRF_I2S->CONFIG.RATIO = 0; // Ratio = 32x
jurica238814 0:d616ca59aad8 103
jurica238814 1:3e3dded8192f 104 NRF_I2S->CONFIG.SWIDTH = 0; // Sample width = 8 bit
jurica238814 1:3e3dded8192f 105 NRF_I2S->CONFIG.ALIGN = 0; // Alignment = Right
jurica238814 0:d616ca59aad8 106 NRF_I2S->CONFIG.FORMAT = 0; // Format = I2S
jurica238814 0:d616ca59aad8 107 NRF_I2S->CONFIG.CHANNELS = 0; // Use stereo
jurica238814 0:d616ca59aad8 108
jurica238814 0:d616ca59aad8 109
jurica238814 1:3e3dded8192f 110
jurica238814 0:d616ca59aad8 111 // In debug mode
jurica238814 1:3e3dded8192f 112 NRF_I2S->PSEL.LRCK = 27; // LRCK routed to pin 26
jurica238814 1:3e3dded8192f 113 NRF_I2S->PSEL.SDOUT = LANC_COMMAND_PIN; // SDOUT routed to pin 28
jurica238814 1:3e3dded8192f 114 NRF_I2S->PSEL.SCK = 30; // SCK routed to pin 30
jurica238814 1:3e3dded8192f 115 NRF_I2S->PSEL.MCK = 0x80000000; // MCK disconnected
jurica238814 1:3e3dded8192f 116 NRF_I2S->PSEL.SDIN = 0x80000000; // SDIN disconnected
jurica238814 0:d616ca59aad8 117
jurica238814 2:b28027b9c099 118 NRF_I2S->TXD.PTR = (uint32_t)normalCmdAddr;
jurica238814 2:b28027b9c099 119 NRF_I2S->RXTXD.MAXCNT = MY_BUF_SIZE/4; // Div with 4 cuz that's number of 32 bit words
jurica238814 2:b28027b9c099 120
jurica238814 2:b28027b9c099 121
jurica238814 1:3e3dded8192f 122
jurica238814 1:3e3dded8192f 123 //NRF_I2S->ENABLE = 1;
jurica238814 0:d616ca59aad8 124 //NRF_I2S->TASKS_START = 1;
jurica238814 0:d616ca59aad8 125
jurica238814 1:3e3dded8192f 126 button.fall(sendCommand);
jurica238814 0:d616ca59aad8 127 while(1){
jurica238814 2:b28027b9c099 128 if(flag){
jurica238814 2:b28027b9c099 129 /*
jurica238814 2:b28027b9c099 130 if(state == 1){
jurica238814 2:b28027b9c099 131 NRF_I2S->TXD.PTR = (uint32_t)startStopAddr;
jurica238814 2:b28027b9c099 132 }
jurica238814 2:b28027b9c099 133 else if(state == 2){
jurica238814 2:b28027b9c099 134 NRF_I2S->TXD.PTR = (uint32_t)normalCmdAddr;
jurica238814 2:b28027b9c099 135 }
jurica238814 2:b28027b9c099 136 */
jurica238814 2:b28027b9c099 137
jurica238814 1:3e3dded8192f 138 led = 0;
jurica238814 1:3e3dded8192f 139 while(!NRF_I2S->EVENTS_TXPTRUPD); // Wait for the data to be send
jurica238814 1:3e3dded8192f 140 NRF_I2S->EVENTS_TXPTRUPD = 0;
jurica238814 1:3e3dded8192f 141 while(!NRF_I2S->EVENTS_TXPTRUPD); // Wait for the data to be send
jurica238814 2:b28027b9c099 142 NRF_I2S->EVENTS_TXPTRUPD = 0;
jurica238814 1:3e3dded8192f 143
jurica238814 2:b28027b9c099 144 NRF_I2S->TASKS_STOP = 1;
jurica238814 2:b28027b9c099 145 while(!NRF_I2S->EVENTS_STOPPED);
jurica238814 1:3e3dded8192f 146 NRF_I2S->ENABLE = 0;
jurica238814 0:d616ca59aad8 147 flag = 0;
jurica238814 1:3e3dded8192f 148 led = 1;
jurica238814 2:b28027b9c099 149 __enable_irq();
jurica238814 0:d616ca59aad8 150 }
jurica238814 2:b28027b9c099 151 }
jurica238814 0:d616ca59aad8 152 }
jurica238814 0:d616ca59aad8 153
jurica238814 0:d616ca59aad8 154