fork

Dependencies:   mbed

Fork of LG by igor Apu

Committer:
Kovalev_D
Date:
Mon Sep 04 12:55:13 2017 +0000
Revision:
217:15cd8752bb6c
Parent:
209:224e7331a061
dd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
igor_v 1:f2adcae3d304 1 #include "Global.h"
igor_v 2:2d0b80ed9216 2 INPUT Input;
igor_v 2:2d0b80ed9216 3 OUTPUT Output;
igor_v 0:8ad47e2b6f00 4
igor_v 0:8ad47e2b6f00 5 volatile uint32_t I2CMasterState = I2C_IDLE;
igor_v 0:8ad47e2b6f00 6 volatile uint32_t I2CMasterBuffer[I2C_WRITELENGTH];
igor_v 0:8ad47e2b6f00 7
igor_v 21:bc8c1cec3da6 8 int ExchangeErr = 0;
igor_v 0:8ad47e2b6f00 9 //----------------------temp---------------------
igor_v 0:8ad47e2b6f00 10 extern char test;
igor_v 0:8ad47e2b6f00 11 /******************************************************************************
igor_v 0:8ad47e2b6f00 12 ** Function name: G_Photo_Exchange
igor_v 0:8ad47e2b6f00 13 **
igor_v 21:bc8c1cec3da6 14 ** Descriptions: Driver for I2C exchange
igor_v 0:8ad47e2b6f00 15 **
igor_v 0:8ad47e2b6f00 16 ** parameters: None
igor_v 0:8ad47e2b6f00 17 ** Returned value: None
igor_v 21:bc8c1cec3da6 18 **
igor_v 0:8ad47e2b6f00 19 ******************************************************************************/
igor_v 0:8ad47e2b6f00 20
igor_v 21:bc8c1cec3da6 21 void G_Photo_Exchange(void)
igor_v 0:8ad47e2b6f00 22 {
igor_v 21:bc8c1cec3da6 23 uint32_t StatValue;
igor_v 21:bc8c1cec3da6 24 static uint32_t WrIndex;
igor_v 21:bc8c1cec3da6 25 static int32_t time_out = TIMEOUT;
igor_v 0:8ad47e2b6f00 26
igor_v 21:bc8c1cec3da6 27 if (I2CMasterState)
igor_v 21:bc8c1cec3da6 28 return; //e.transmitting is not active, go away
igor_v 0:8ad47e2b6f00 29
igor_v 21:bc8c1cec3da6 30 if (--time_out < 0) //e. valid time period elapsed, go away
igor_v 21:bc8c1cec3da6 31 {
igor_v 21:bc8c1cec3da6 32 time_out = TIMEOUT;
igor_v 21:bc8c1cec3da6 33 I2CMasterState = I2C_IDLE; //e. timer elapsed, go away
igor_v 21:bc8c1cec3da6 34 LPC_I2C0->CONSET = I2CONSET_STO; //e. Set Stop flag
igor_v 21:bc8c1cec3da6 35 LPC_I2C0->CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC;
igor_v 21:bc8c1cec3da6 36 return;
igor_v 21:bc8c1cec3da6 37 }
igor_v 21:bc8c1cec3da6 38 else if (!(LPC_I2C0->CONSET & I2CONSET_SI)) //e. state of I2C bus has not been changed
igor_v 21:bc8c1cec3da6 39 return;
igor_v 0:8ad47e2b6f00 40
igor_v 21:bc8c1cec3da6 41 StatValue = LPC_I2C0->STAT;
igor_v 0:8ad47e2b6f00 42
igor_v 21:bc8c1cec3da6 43 switch ( StatValue )
igor_v 21:bc8c1cec3da6 44 {
igor_v 21:bc8c1cec3da6 45 case 0x08: // A Start condition is issued (write data for the first potentiometer)
igor_v 21:bc8c1cec3da6 46 WrIndex = 0;
igor_v 21:bc8c1cec3da6 47 time_out = TIMEOUT; //e. enable countdown
igor_v 21:bc8c1cec3da6 48 LPC_I2C0->DAT = I2CMasterBuffer[WrIndex++]; //e. send address
igor_v 21:bc8c1cec3da6 49 LPC_I2C0->CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC); //e .clear interrupt bit and start bit
igor_v 21:bc8c1cec3da6 50 break;
igor_v 21:bc8c1cec3da6 51
igor_v 21:bc8c1cec3da6 52 case 0x10: // A repeated started is issued (write data for the second potentiometer)
igor_v 21:bc8c1cec3da6 53 LPC_I2C0->DAT = I2CMasterBuffer[WrIndex++]; //e. send address
igor_v 21:bc8c1cec3da6 54 LPC_I2C0->CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC); //e .clear interrupt bit and start bit
igor_v 21:bc8c1cec3da6 55 break;
igor_v 0:8ad47e2b6f00 56
igor_v 21:bc8c1cec3da6 57 case 0x18: //e. Regardless, it's a ACK after slave address reading
igor_v 21:bc8c1cec3da6 58 LPC_I2C0->DAT = I2CMasterBuffer[WrIndex++]; //e. send another byte
igor_v 21:bc8c1cec3da6 59 LPC_I2C0->CONCLR = I2CONCLR_SIC; //e. clear interrupt bit
igor_v 21:bc8c1cec3da6 60 break;
igor_v 0:8ad47e2b6f00 61
igor_v 21:bc8c1cec3da6 62 case 0x28: //e. Regardless it's a ACK after data byte
igor_v 21:bc8c1cec3da6 63 if (WrIndex == I2C_WRITELENGTH) //e. we have transmitted the data for the B potentiometer
igor_v 21:bc8c1cec3da6 64 {
igor_v 21:bc8c1cec3da6 65 I2CMasterState = I2C_IDLE;
igor_v 21:bc8c1cec3da6 66 LPC_I2C0->CONSET = I2CONSET_STO; //e. Set Stop flag
igor_v 21:bc8c1cec3da6 67 Output.Str.Cnt_Dif = 300;
igor_v 21:bc8c1cec3da6 68 }
igor_v 21:bc8c1cec3da6 69 else if (WrIndex == 3)
igor_v 21:bc8c1cec3da6 70 {
igor_v 21:bc8c1cec3da6 71 LPC_I2C0->CONSET = I2CONSET_STA;
igor_v 21:bc8c1cec3da6 72 Output.Str.Cnt_Dif = 200;
igor_v 21:bc8c1cec3da6 73 }
igor_v 21:bc8c1cec3da6 74 else
igor_v 21:bc8c1cec3da6 75 {
igor_v 21:bc8c1cec3da6 76 LPC_I2C0->DAT = I2CMasterBuffer[WrIndex++]; //e. send another byte
igor_v 21:bc8c1cec3da6 77 Output.Str.Cnt_Dif = 100;
igor_v 21:bc8c1cec3da6 78 }
igor_v 21:bc8c1cec3da6 79 LPC_I2C0->CONCLR = I2CONCLR_SIC; //e. clear interrupt bit
igor_v 21:bc8c1cec3da6 80 break;
igor_v 0:8ad47e2b6f00 81
igor_v 21:bc8c1cec3da6 82 case 0x20: //e. no aknowledgement after address transmitting
igor_v 21:bc8c1cec3da6 83 case 0x30: //e. no aknowledgement after data block transmitting
igor_v 21:bc8c1cec3da6 84 LPC_I2C0->CONSET = I2CONSET_STO; //e. Set Stop flag
igor_v 21:bc8c1cec3da6 85 LPC_I2C0->CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
igor_v 21:bc8c1cec3da6 86 I2CMasterState = I2C_IDLE; //e. fix new state
igor_v 21:bc8c1cec3da6 87 break;
igor_v 21:bc8c1cec3da6 88 }
igor_v 21:bc8c1cec3da6 89 return;
igor_v 0:8ad47e2b6f00 90 }
igor_v 0:8ad47e2b6f00 91 /******************************************************************************
igor_v 0:8ad47e2b6f00 92 ** Function name: DAC_ADC_Exchange
igor_v 0:8ad47e2b6f00 93 **
igor_v 21:bc8c1cec3da6 94 ** Descriptions: Loading data to DACs and initialization of ADC reading
igor_v 0:8ad47e2b6f00 95 **
igor_v 0:8ad47e2b6f00 96 ** parameters: None
igor_v 0:8ad47e2b6f00 97 ** Returned value: None
igor_v 21:bc8c1cec3da6 98 **
igor_v 0:8ad47e2b6f00 99 ******************************************************************************/
igor_v 0:8ad47e2b6f00 100 void DAC_ADC_Exchange()
igor_v 0:8ad47e2b6f00 101 {
igor_v 21:bc8c1cec3da6 102 //-------------------------loading data from ADC to buffer---------------------------------------
igor_v 0:8ad47e2b6f00 103 //int x;
igor_v 21:bc8c1cec3da6 104 ExchangeErr &= ~ADC_ERR_MSK; //e. сбросить флаг ошибки
igor_v 21:bc8c1cec3da6 105 if (LPC_SSP0->SR & SSP_BUSY) //e. если буфер передатчика не полон
igor_v 21:bc8c1cec3da6 106 {
igor_v 21:bc8c1cec3da6 107 ExchangeErr |= ADC_ERR_MSK; //e. установить флаг ошибки
igor_v 21:bc8c1cec3da6 108 }
igor_v 21:bc8c1cec3da6 109 else
igor_v 21:bc8c1cec3da6 110 {
igor_v 21:bc8c1cec3da6 111 LPC_SSP0->DR = 0x8001; //записать 0x8001 в регистр передачи.
igor_v 21:bc8c1cec3da6 112 if (Sys_Clock & 1)//если нечетный такт то
igor_v 21:bc8c1cec3da6 113 {
igor_v 21:bc8c1cec3da6 114 LPC_SSP0->DR = WRITE_DAC0; //e.команда для ЦАП_0 передавать.
igor_v 21:bc8c1cec3da6 115 LPC_SSP0->DR = (-Output.ArrayOut[3]+32767); //e. передача 12 бит
igor_v 21:bc8c1cec3da6 116 }
igor_v 21:bc8c1cec3da6 117 else //если такт четный.
igor_v 21:bc8c1cec3da6 118 {
igor_v 21:bc8c1cec3da6 119 LPC_SSP0->DR = WRITE_DAC1 ; //e.команда для ЦАП_1 передавать.
igor_v 21:bc8c1cec3da6 120 LPC_SSP0->DR = (Output.ArrayOut[0]<<1) & 0xFFF0; //e. передача 12 бит
igor_v 21:bc8c1cec3da6 121 }
igor_v 0:8ad47e2b6f00 122 }
igor_v 0:8ad47e2b6f00 123 }
igor_v 0:8ad47e2b6f00 124 /******************************************************************************
igor_v 0:8ad47e2b6f00 125 ** Function name: ADC_Input
igor_v 0:8ad47e2b6f00 126 **
igor_v 21:bc8c1cec3da6 127 ** Descriptions: Reading data from ADC
igor_v 0:8ad47e2b6f00 128 **
igor_v 0:8ad47e2b6f00 129 ** parameters: None
igor_v 0:8ad47e2b6f00 130 ** Returned value: None
igor_v 21:bc8c1cec3da6 131 **
igor_v 0:8ad47e2b6f00 132 ******************************************************************************/
igor_v 0:8ad47e2b6f00 133 void ADC_Input()
igor_v 21:bc8c1cec3da6 134 {
igor_v 21:bc8c1cec3da6 135 uint32_t Dummy;
igor_v 0:8ad47e2b6f00 136
igor_v 21:bc8c1cec3da6 137 //---------------------read data from ADC buffer---------------------------------------------
igor_v 21:bc8c1cec3da6 138 Dummy = Dummy;
igor_v 21:bc8c1cec3da6 139 ExchangeErr &= ~ADC_ERR_MSK;
igor_v 0:8ad47e2b6f00 140
igor_v 21:bc8c1cec3da6 141 if (LPC_SSP0->SR & SSP_BUSY) //если буфер фифо не полный(передача ативна)
igor_v 21:bc8c1cec3da6 142 { ExchangeErr |= ADC_ERR_MSK; //установить флаг ошибки
igor_v 21:bc8c1cec3da6 143
igor_v 21:bc8c1cec3da6 144 if (!(LPC_SSP0->SR & TX_SSP_EMPT)) //буфер передачи не пуст.
igor_v 21:bc8c1cec3da6 145 ExchangeErr |= ADC_ERR_MSK; //установить флаг ошибки
igor_v 21:bc8c1cec3da6 146 }
igor_v 21:bc8c1cec3da6 147 else //обмен закончен прочитать буфер.
igor_v 21:bc8c1cec3da6 148 {
igor_v 21:bc8c1cec3da6 149 Input.ArrayIn[0] = LPC_SSP0->DR; //чтениеThermo2
igor_v 21:bc8c1cec3da6 150 Input.ArrayIn[1] = LPC_SSP0->DR; //чтение Thermo1
igor_v 21:bc8c1cec3da6 151 Input.ArrayIn[2] = LPC_SSP0->DR; //чтение HF_out
igor_v 0:8ad47e2b6f00 152
igor_v 21:bc8c1cec3da6 153 while (LPC_SSP0->SR & RX_SSP_notEMPT) //если буфер SPI не пуст.
igor_v 21:bc8c1cec3da6 154 Dummy = LPC_SSP0->DR; //очистить буфер.
igor_v 21:bc8c1cec3da6 155 }
igor_v 21:bc8c1cec3da6 156
igor_v 0:8ad47e2b6f00 157
igor_v 0:8ad47e2b6f00 158 }
igor_v 0:8ad47e2b6f00 159 /*****************************************************************************
igor_v 0:8ad47e2b6f00 160 ** Function name: DAC_Output
igor_v 0:8ad47e2b6f00 161 **
igor_v 0:8ad47e2b6f00 162 ** Descriptions: Output data to intrnal DAC
igor_v 0:8ad47e2b6f00 163 **
igor_v 0:8ad47e2b6f00 164 ** parameters: output - code for output
igor_v 0:8ad47e2b6f00 165 ** Returned value: None
igor_v 21:bc8c1cec3da6 166 **
igor_v 0:8ad47e2b6f00 167 *****************************************************************************/
igor_v 0:8ad47e2b6f00 168 void DAC_Output(int output)
igor_v 0:8ad47e2b6f00 169 {
igor_v 21:bc8c1cec3da6 170 // LPC_DAC->CR = (output<<6);
igor_v 21:bc8c1cec3da6 171 return;
igor_v 0:8ad47e2b6f00 172 }
igor_v 0:8ad47e2b6f00 173 /******************************************************************************
igor_v 0:8ad47e2b6f00 174 ** Function name: G_Photo_Init
igor_v 0:8ad47e2b6f00 175 **
igor_v 0:8ad47e2b6f00 176 ** Descriptions: Initialization of exchange with digital potentiometers
igor_v 0:8ad47e2b6f00 177 **
igor_v 0:8ad47e2b6f00 178 ** parameters: None
igor_v 0:8ad47e2b6f00 179 ** Returned value: None
igor_v 21:bc8c1cec3da6 180 **
igor_v 0:8ad47e2b6f00 181 ******************************************************************************/
igor_v 0:8ad47e2b6f00 182
igor_v 0:8ad47e2b6f00 183 /******************************************************************************
igor_v 0:8ad47e2b6f00 184 ** Function name: DAC_ADC_Exchange_Init
igor_v 0:8ad47e2b6f00 185 **
igor_v 0:8ad47e2b6f00 186 ** Descriptions: Initialization of data exchange with DACs and ADCs
igor_v 0:8ad47e2b6f00 187 **
igor_v 0:8ad47e2b6f00 188 ** parameters: None
igor_v 0:8ad47e2b6f00 189 ** Returned value: None
igor_v 21:bc8c1cec3da6 190 **
igor_v 0:8ad47e2b6f00 191 ******************************************************************************/
igor_v 0:8ad47e2b6f00 192
igor_v 0:8ad47e2b6f00 193 /*****************************************************************************
igor_v 0:8ad47e2b6f00 194 ** Function name: DACInit
igor_v 0:8ad47e2b6f00 195 **
igor_v 0:8ad47e2b6f00 196 ** Descriptions: initialize DAC channel
igor_v 0:8ad47e2b6f00 197 **
igor_v 0:8ad47e2b6f00 198 ** parameters: None
igor_v 0:8ad47e2b6f00 199 ** Returned value: None
igor_v 21:bc8c1cec3da6 200 **
igor_v 0:8ad47e2b6f00 201 *****************************************************************************/
igor_v 0:8ad47e2b6f00 202 void DACInit( void )
igor_v 0:8ad47e2b6f00 203 {
igor_v 21:bc8c1cec3da6 204 /* setup the related pin to DAC output */
igor_v 21:bc8c1cec3da6 205 LPC_PINCON->PINSEL1 |= 0x00200000; /* установить p0.26 как выход ЦАП */
igor_v 21:bc8c1cec3da6 206 // LPC_GPIO0->FIODIR |= (1<<26);
igor_v 21:bc8c1cec3da6 207 LPC_DAC->CNTVAL = 0; // ккаието параметры
igor_v 21:bc8c1cec3da6 208 LPC_DAC->CTRL = 0; // для ДМА
igor_v 21:bc8c1cec3da6 209 return;
igor_v 0:8ad47e2b6f00 210 }
igor_v 0:8ad47e2b6f00 211 /******************************************************************************
igor_v 0:8ad47e2b6f00 212 ** Function name: Out_G_photo
igor_v 0:8ad47e2b6f00 213 **
igor_v 0:8ad47e2b6f00 214 ** Descriptions: Start of potentiometer data writing process
igor_v 0:8ad47e2b6f00 215 **
igor_v 0:8ad47e2b6f00 216 ** parameters: pointer to gain factors
igor_v 0:8ad47e2b6f00 217 ** Returned value: None
igor_v 21:bc8c1cec3da6 218 **
igor_v 0:8ad47e2b6f00 219 ******************************************************************************/
igor_v 0:8ad47e2b6f00 220 void Out_G_photo(uint32_t Ph_A, uint32_t Ph_B)
igor_v 0:8ad47e2b6f00 221 {
Kovalev_D 208:19150d2b528f 222 I2CMasterBuffer[2] = 255-Ph_A;// запись в буфер I2C мастера данных с А канала фото датчика.(уточнить)
Kovalev_D 208:19150d2b528f 223 I2CMasterBuffer[5] = 255-Ph_B;// запись в буфер I2C мастера данных с В канала фото датчика.(уточнить)
Kovalev_D 209:224e7331a061 224 GyroP.Str.Gain_Sin = Ph_A;
Kovalev_D 209:224e7331a061 225 GyroP.Str.Gain_Cos = Ph_B;
Kovalev_D 208:19150d2b528f 226 LPC_I2C0->CONSET = I2CONSET_STA; // установить старт флаг.
igor_v 21:bc8c1cec3da6 227 I2CMasterState = I2C_BUSY; // установить флаг занятой шины.
igor_v 21:bc8c1cec3da6 228 }
igor_v 0:8ad47e2b6f00 229