eeprom_test

Dependencies:   mbed FastPWM

Committer:
GiJeongKim
Date:
Mon Jul 20 06:58:38 2020 +0000
Revision:
68:135a2d087198
Parent:
16:903b5a4433b4
control;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GiJeongKim 0:51c43836c1d7 1 #include "mbed.h"
GiJeongKim 0:51c43836c1d7 2 #include "setting.h"
GiJeongKim 0:51c43836c1d7 3 #include "SPI_EEP_ENC.h"
GiJeongKim 0:51c43836c1d7 4
GiJeongKim 0:51c43836c1d7 5 // EEPROM
GiJeongKim 68:135a2d087198 6 void spi_eeprom_ready(void){
GiJeongKim 68:135a2d087198 7 int temp1, temp2;
GiJeongKim 68:135a2d087198 8 do{
GiJeongKim 68:135a2d087198 9 eeprom_cs=0;
GiJeongKim 68:135a2d087198 10 eeprom.write(0x06); //write enable
GiJeongKim 68:135a2d087198 11 eeprom_cs=1;
GiJeongKim 68:135a2d087198 12
GiJeongKim 68:135a2d087198 13 eeprom_cs=0;
GiJeongKim 68:135a2d087198 14 temp1 = eeprom.write(0x05);
GiJeongKim 68:135a2d087198 15 temp2 = eeprom.write(0x00);
GiJeongKim 68:135a2d087198 16 eeprom_cs=1;
GiJeongKim 68:135a2d087198 17 temp2=(temp2&(0x03))!= 0x02;
GiJeongKim 68:135a2d087198 18 } while(temp2); // before writing or reading
GiJeongKim 68:135a2d087198 19 }
GiJeongKim 68:135a2d087198 20 //
GiJeongKim 68:135a2d087198 21 void spi_eeprom_write(unsigned short add, unsigned int data){
GiJeongKim 68:135a2d087198 22 spi_eeprom_ready();
GiJeongKim 68:135a2d087198 23 add=add*4;
GiJeongKim 68:135a2d087198 24 eeprom_cs=0;
GiJeongKim 68:135a2d087198 25 eeprom.write(0x02);
GiJeongKim 68:135a2d087198 26 eeprom.write(0xff&(add>>8));
GiJeongKim 68:135a2d087198 27 eeprom.write(0xff&add);
GiJeongKim 68:135a2d087198 28 eeprom.write(0xff&data);
GiJeongKim 68:135a2d087198 29 eeprom.write(0xff&(data>>8));
GiJeongKim 68:135a2d087198 30 eeprom.write(0xff&(data>>16));
GiJeongKim 68:135a2d087198 31 eeprom.write(0xff&(data>>24));
GiJeongKim 68:135a2d087198 32 eeprom_cs=1;
GiJeongKim 68:135a2d087198 33 }
Lightvalve 16:903b5a4433b4 34 //
GiJeongKim 68:135a2d087198 35 unsigned int spi_eeprom_read(unsigned short add){
GiJeongKim 68:135a2d087198 36 add=add*4;
GiJeongKim 68:135a2d087198 37 eeprom_cs=0;
GiJeongKim 68:135a2d087198 38 eeprom.write(0x03);
GiJeongKim 68:135a2d087198 39 eeprom.write(0xff&(add>>8));
GiJeongKim 68:135a2d087198 40 eeprom.write(0xff&add);
Lightvalve 16:903b5a4433b4 41 //
GiJeongKim 68:135a2d087198 42 int a1 = eeprom.write(0x00);
GiJeongKim 68:135a2d087198 43 int a2 = eeprom.write(0x00);
GiJeongKim 68:135a2d087198 44 int a3 = eeprom.write(0x00);
GiJeongKim 68:135a2d087198 45 int a4 = eeprom.write(0x00);
GiJeongKim 68:135a2d087198 46 eeprom_cs=1;
GiJeongKim 68:135a2d087198 47 //unsigned int final = (a4<<24)+(a3<<16) + (a2<<8) + a1;
GiJeongKim 68:135a2d087198 48 unsigned int final = (int32_t) (a1 | a2 << 8 | a3 << 16 | a4 << 24);
GiJeongKim 68:135a2d087198 49 return final;
GiJeongKim 68:135a2d087198 50 }
GiJeongKim 68:135a2d087198 51
GiJeongKim 68:135a2d087198 52 void spi_eeprom_call_data(void)
GiJeongKim 68:135a2d087198 53 {
GiJeongKim 68:135a2d087198 54 BNO = spi_eeprom_read(RID_BNO);
GiJeongKim 68:135a2d087198 55 BNO = 11;
GiJeongKim 68:135a2d087198 56 OPERATING_MODE = spi_eeprom_read(RID_OPERATING_MODE);
GiJeongKim 68:135a2d087198 57 SENSING_MODE = spi_eeprom_read(RID_SENSING_MODE);
GiJeongKim 68:135a2d087198 58 SENSING_MODE = 1;
GiJeongKim 68:135a2d087198 59 CURRENT_CONTROL_MODE = spi_eeprom_read(RID_CURRENT_CONTROL_MODE);
GiJeongKim 68:135a2d087198 60 CURRENT_CONTROL_MODE = 1;
GiJeongKim 68:135a2d087198 61 FLAG_VALVE_DEADZONE = spi_eeprom_read(RID_FLAG_VALVE_DEADZONE);
GiJeongKim 68:135a2d087198 62 CAN_FREQ = spi_eeprom_read(RID_CAN_FREQ);
GiJeongKim 68:135a2d087198 63 CAN_FREQ = 500;
GiJeongKim 68:135a2d087198 64 DIR_JOINT_ENC = spi_eeprom_read(RID_JOINT_ENC_DIR);
GiJeongKim 68:135a2d087198 65 DIR_VALVE = spi_eeprom_read(RID_VALVE_DIR);
GiJeongKim 68:135a2d087198 66 DIR_VALVE_ENC = spi_eeprom_read(RID_VALVE_ENC_DIR);
GiJeongKim 68:135a2d087198 67 SUPPLY_VOLTAGE = (float) (spi_eeprom_read(RID_VOLATGE_SUPPLY)) *0.1f;
GiJeongKim 68:135a2d087198 68 VALVE_VOLTAGE_LIMIT = (float) (spi_eeprom_read(RID_VOLTAGE_VALVE)) * 0.1f;
GiJeongKim 68:135a2d087198 69 P_GAIN_VALVE_POSITION = spi_eeprom_read(RID_P_GAIN_VALVE_POSITION);
GiJeongKim 68:135a2d087198 70 I_GAIN_VALVE_POSITION = spi_eeprom_read(RID_I_GAIN_VALVE_POSITION);
GiJeongKim 68:135a2d087198 71 D_GAIN_VALVE_POSITION = spi_eeprom_read(RID_D_GAIN_VALVE_POSITION);
GiJeongKim 68:135a2d087198 72 P_GAIN_JOINT_POSITION = spi_eeprom_read(RID_P_GAIN_JOINT_POSITION);
GiJeongKim 68:135a2d087198 73 I_GAIN_JOINT_POSITION = spi_eeprom_read(RID_I_GAIN_JOINT_POSITION);
GiJeongKim 68:135a2d087198 74 D_GAIN_JOINT_POSITION = spi_eeprom_read(RID_D_GAIN_JOINT_POSITION);
GiJeongKim 68:135a2d087198 75 P_GAIN_JOINT_TORQUE = spi_eeprom_read(RID_P_GAIN_JOINT_TORQUE);
GiJeongKim 68:135a2d087198 76 I_GAIN_JOINT_TORQUE = spi_eeprom_read( RID_I_GAIN_JOINT_TORQUE);
GiJeongKim 68:135a2d087198 77 D_GAIN_JOINT_TORQUE = spi_eeprom_read(RID_D_GAIN_JOINT_TORQUE);
GiJeongKim 68:135a2d087198 78 VALVE_DEADZONE_PLUS = (float) (spi_eeprom_read( RID_VALVE_DEADZONE_PLUS)) * 0.1f;
GiJeongKim 68:135a2d087198 79 VALVE_DEADZONE_MINUS = (float) (spi_eeprom_read(RID_VALVE_DEADZONE_MINUS)) * 0.1f;
GiJeongKim 68:135a2d087198 80 VELOCITY_COMP_GAIN = spi_eeprom_read(RID_VELOCITY_COMP_GAIN);
GiJeongKim 68:135a2d087198 81 COMPLIANCE_GAIN = spi_eeprom_read(RID_COMPLIANCE_GAIN);
GiJeongKim 68:135a2d087198 82 VALVE_CENTER = spi_eeprom_read(RID_VALVE_CNETER);
GiJeongKim 68:135a2d087198 83 VALVE_FF = spi_eeprom_read(RID_VALVE_FF);
GiJeongKim 68:135a2d087198 84 BULK_MODULUS = spi_eeprom_read(RID_BULK_MODULUS);
GiJeongKim 68:135a2d087198 85 CHAMBER_VOLUME_A = spi_eeprom_read(RID_CHAMBER_VOLUME_A);
GiJeongKim 68:135a2d087198 86 CHAMBER_VOLUME_B = spi_eeprom_read(RID_CHAMBER_VOLUME_B);
GiJeongKim 68:135a2d087198 87 PISTON_AREA_A = spi_eeprom_read(RID_PISTON_AREA_A);
GiJeongKim 68:135a2d087198 88 PISTON_AREA_B = spi_eeprom_read(RID_PISTON_AREA_B);
GiJeongKim 68:135a2d087198 89 PISTON_AREA_alpha = (float)PISTON_AREA_A/(float)PISTON_AREA_B;
GiJeongKim 68:135a2d087198 90 alpha3 = PISTON_AREA_alpha * PISTON_AREA_alpha*PISTON_AREA_alpha;
GiJeongKim 68:135a2d087198 91 PRES_SUPPLY = spi_eeprom_read(RID_PRES_SUPPLY);
GiJeongKim 68:135a2d087198 92 PRES_RETURN = spi_eeprom_read(RID_PRES_RETURN);
GiJeongKim 68:135a2d087198 93 ENC_LIMIT_MINUS = spi_eeprom_read(RID_ENC_LIMIT_MINUS);
GiJeongKim 68:135a2d087198 94 ENC_LIMIT_PLUS = spi_eeprom_read(RID_ENC_LIMIT_PLUS);
GiJeongKim 68:135a2d087198 95 STROKE = spi_eeprom_read(RID_STROKE);
GiJeongKim 68:135a2d087198 96 //VALVE_LIMIT_MINUS = flashReadInt(Rom_Sector, RID_VALVE_LIMIT_MINUS);
GiJeongKim 68:135a2d087198 97 //VALVE_LIMIT_PLUS = flashReadInt(Rom_Sector, RID_VALVE_LIMIT_PLUS);
GiJeongKim 68:135a2d087198 98 ENC_PULSE_PER_POSITION = (float) (spi_eeprom_read(RID_ENC_PULSE_PER_POSITION)) * 0.1f;
GiJeongKim 68:135a2d087198 99 // ENC_PULSE_PER_POSITION = (float) 1024.0f;
GiJeongKim 68:135a2d087198 100 TORQUE_SENSOR_PULSE_PER_TORQUE = (float) (spi_eeprom_read(RID_TORQUE_SENSOR_PULSE_PER_TORQUE)) * 0.0001f;
GiJeongKim 68:135a2d087198 101 //TORQUE_SENSOR_PULSE_PER_TORQUE = (float) 0.41928f; //for ankle
GiJeongKim 68:135a2d087198 102 // TORQUE_SENSOR_PULSE_PER_TORQUE = (float) 10000.0f/2048.0f; //for knee
GiJeongKim 68:135a2d087198 103 PRES_SENSOR_A_PULSE_PER_BAR = (float) (spi_eeprom_read(RID_PRES_SENSOR_A_PULSE_PER_BAR)) * 0.01f;
GiJeongKim 68:135a2d087198 104 PRES_SENSOR_A_PULSE_PER_BAR = 4096.0f * 946.0f / 3.3f / 300.0f / 210.0f;
GiJeongKim 68:135a2d087198 105 PRES_SENSOR_B_PULSE_PER_BAR = (float) (spi_eeprom_read(RID_PRES_SENSOR_B_PULSE_PER_BAR)) * 0.01f;
GiJeongKim 68:135a2d087198 106 PRES_SENSOR_B_PULSE_PER_BAR = 4096.0f * 946.0f / 3.3f / 300.0f / 210.0f;
GiJeongKim 68:135a2d087198 107 FRICTION = (float) (spi_eeprom_read(RID_FRICTION)) * 0.1f;
GiJeongKim 68:135a2d087198 108 HOMEPOS_OFFSET = spi_eeprom_read(RID_HOMEPOS_OFFSET);
GiJeongKim 68:135a2d087198 109 HOMEPOS_VALVE_OPENING = spi_eeprom_read(RID_HOMEPOS_VALVE_OPENING);
GiJeongKim 68:135a2d087198 110 TORQUE_VREF = (float) (spi_eeprom_read(RID_TORQUE_SENSOR_VREF)) *0.001f;
GiJeongKim 68:135a2d087198 111 PRES_A_VREF = (float) spi_eeprom_read(RID_PRES_A_SENSOR_VREF) * 0.001f;
GiJeongKim 68:135a2d087198 112 PRES_B_VREF = (float) spi_eeprom_read(RID_PRES_B_SENSOR_VREF) * 0.001f;
GiJeongKim 68:135a2d087198 113 VALVE_GAIN_LPM_PER_V[0] = (float) (spi_eeprom_read(RID_VALVE_GAIN_PLUS_1)) * 0.01f;
GiJeongKim 68:135a2d087198 114 VALVE_GAIN_LPM_PER_V[2] = (float) (spi_eeprom_read(RID_VALVE_GAIN_PLUS_2)) * 0.01f;
GiJeongKim 68:135a2d087198 115 VALVE_GAIN_LPM_PER_V[4] = (float) (spi_eeprom_read(RID_VALVE_GAIN_PLUS_3)) * 0.01f;
GiJeongKim 68:135a2d087198 116 VALVE_GAIN_LPM_PER_V[6] = (float) (spi_eeprom_read(RID_VALVE_GAIN_PLUS_4)) * 0.01f;
GiJeongKim 68:135a2d087198 117 VALVE_GAIN_LPM_PER_V[8] = (float) (spi_eeprom_read(RID_VALVE_GAIN_PLUS_5)) * 0.01f;
GiJeongKim 68:135a2d087198 118 VALVE_GAIN_LPM_PER_V[1] = (float) (spi_eeprom_read(RID_VALVE_GAIN_MINUS_1)) * 0.01f;
GiJeongKim 68:135a2d087198 119 VALVE_GAIN_LPM_PER_V[3] = (float) (spi_eeprom_read(RID_VALVE_GAIN_MINUS_2)) * 0.01f;
GiJeongKim 68:135a2d087198 120 VALVE_GAIN_LPM_PER_V[5] = (float) (spi_eeprom_read(RID_VALVE_GAIN_MINUS_3)) * 0.01f;
GiJeongKim 68:135a2d087198 121 VALVE_GAIN_LPM_PER_V[7] = (float) (spi_eeprom_read(RID_VALVE_GAIN_MINUS_4)) * 0.01f;
GiJeongKim 68:135a2d087198 122 VALVE_GAIN_LPM_PER_V[9] = (float) (spi_eeprom_read(RID_VALVE_GAIN_MINUS_5)) * 0.01f;
GiJeongKim 68:135a2d087198 123 for(int i=0; i<25; i++) {
GiJeongKim 68:135a2d087198 124 VALVE_POS_VS_PWM[i] = (float) (spi_eeprom_read(RID_VALVE_POS_VS_PWM_0 + i));
GiJeongKim 68:135a2d087198 125 }
GiJeongKim 68:135a2d087198 126 for(int i=0; i<100; i++) {
GiJeongKim 68:135a2d087198 127 JOINT_VEL[i] = ( ((spi_eeprom_read( RID_VALVE_POS_VS_FLOWRATE_0 + i)) & 0xFFFF) | ((spi_eeprom_read(RID_VALVE_POS_VS_FLOWRATE_0_1 + i)) & 0xFFFF) << 16 ) ;
GiJeongKim 68:135a2d087198 128 }
GiJeongKim 68:135a2d087198 129 VALVE_MAX_POS = spi_eeprom_read(RID_VALVE_MAX_POS);
GiJeongKim 68:135a2d087198 130 VALVE_MIN_POS = spi_eeprom_read(RID_VALVE_MIN_POS);
GiJeongKim 68:135a2d087198 131 //DDV_CENTER = (float) (flashReadInt(Rom_Sector, RID_DDV_CENTER)) * 0.1f;
GiJeongKim 68:135a2d087198 132 VALVE_POS_NUM = spi_eeprom_read(RID_VALVE_POS_NUM);
GiJeongKim 68:135a2d087198 133
GiJeongKim 68:135a2d087198 134 K_SPRING = spi_eeprom_read(RID_K_SPRING);
GiJeongKim 68:135a2d087198 135 D_DAMPER = spi_eeprom_read(RID_D_DAMPER);
GiJeongKim 68:135a2d087198 136
GiJeongKim 68:135a2d087198 137
GiJeongKim 68:135a2d087198 138 }
GiJeongKim 0:51c43836c1d7 139
GiJeongKim 0:51c43836c1d7 140 // ENCODER
GiJeongKim 0:51c43836c1d7 141 void spi_enc_set_clear(void){
GiJeongKim 0:51c43836c1d7 142 unsigned int temp;
GiJeongKim 0:51c43836c1d7 143 enc_cs = 0;
GiJeongKim 0:51c43836c1d7 144 temp = enc.write(0b00100000);
GiJeongKim 0:51c43836c1d7 145 enc_cs = 1;
GiJeongKim 0:51c43836c1d7 146 }
GiJeongKim 0:51c43836c1d7 147
GiJeongKim 0:51c43836c1d7 148 void spi_enc_set_init(void){
GiJeongKim 0:51c43836c1d7 149 unsigned int temp, i, temp1, temp2;
GiJeongKim 0:51c43836c1d7 150
GiJeongKim 0:51c43836c1d7 151 // write MDR0 -> 0b11 -> x4 quadrature count mode
GiJeongKim 0:51c43836c1d7 152 enc_cs = 0;
GiJeongKim 0:51c43836c1d7 153 temp = enc.write(0b10001000); // WR + MDR0
GiJeongKim 0:51c43836c1d7 154 temp = enc.write(0b00000011); // quadratue mode
GiJeongKim 0:51c43836c1d7 155 enc_cs = 1;
GiJeongKim 0:51c43836c1d7 156
GiJeongKim 0:51c43836c1d7 157 // write MDR1 -> 0b10 -> 2-byte counter mode
GiJeongKim 0:51c43836c1d7 158 for(i=0;i<100;i++);
GiJeongKim 0:51c43836c1d7 159 enc_cs = 0;
GiJeongKim 0:51c43836c1d7 160 temp = enc.write(0b10010000); // WR + MDR1
GiJeongKim 0:51c43836c1d7 161 //temp = enc.write(0b00000010); // 2 byte mode
GiJeongKim 0:51c43836c1d7 162 temp = enc.write(0b00000000); // 4 byte mode
GiJeongKim 0:51c43836c1d7 163 enc_cs = 1;
GiJeongKim 0:51c43836c1d7 164
GiJeongKim 0:51c43836c1d7 165 // clear
GiJeongKim 0:51c43836c1d7 166 spi_enc_set_clear();
GiJeongKim 0:51c43836c1d7 167 }
GiJeongKim 0:51c43836c1d7 168
GiJeongKim 0:51c43836c1d7 169
GiJeongKim 0:51c43836c1d7 170 int spi_enc_read(void){
GiJeongKim 0:51c43836c1d7 171 //for(t_i=0;t_i<100;t_i++);
GiJeongKim 0:51c43836c1d7 172 unsigned int t_dummy, t_b1, t_b2, t_b3, t_b4, t_i;
GiJeongKim 0:51c43836c1d7 173 enc_cs = 0;
GiJeongKim 0:51c43836c1d7 174 t_dummy = enc.write(0b01100000); // Read Commend
GiJeongKim 0:51c43836c1d7 175 t_b1 = enc.write(0x00); // Dummy data for clock
GiJeongKim 0:51c43836c1d7 176 t_b2 = enc.write(0x00); // Dummy data for clock
GiJeongKim 0:51c43836c1d7 177 t_b3 = enc.write(0x00); // Dummy data for clock
GiJeongKim 0:51c43836c1d7 178 t_b4 = enc.write(0x00); // Dummy data for clock
GiJeongKim 0:51c43836c1d7 179 enc_cs = 1;
GiJeongKim 0:51c43836c1d7 180
GiJeongKim 0:51c43836c1d7 181 return((t_b1<<24) + (t_b2<<16) + (t_b3<<8) + t_b4);
GiJeongKim 0:51c43836c1d7 182 }