航空研究会 / L6470_status_check
Committer:
imanomadao
Date:
Thu Feb 20 10:11:44 2020 +0000
Revision:
2:09ae6c1aab1a
Parent:
1:6cd6cd00aabe
added_getstatus_method

Who changed what in which revision?

UserRevisionLine numberNew contents of line
imanomadao 1:6cd6cd00aabe 1 #ifndef _L6470_H_
imanomadao 0:2f3be153718a 2 #define _L6470_H_
imanomadao 0:2f3be153718a 3
imanomadao 0:2f3be153718a 4 #include"mbed.h"
imanomadao 0:2f3be153718a 5
imanomadao 0:2f3be153718a 6 //L6470 Registers
imanomadao 0:2f3be153718a 7 #define ABS_POS 0x01 // Current position
imanomadao 0:2f3be153718a 8 #define EL_POS 0x02 // Electrical position
imanomadao 0:2f3be153718a 9 #define MARK 0x03
imanomadao 0:2f3be153718a 10 #define SPEED 0x04
imanomadao 0:2f3be153718a 11 #define ACC 0x05
imanomadao 0:2f3be153718a 12 #define DEC 0x06
imanomadao 0:2f3be153718a 13 #define MAX_SPEED 0x07
imanomadao 0:2f3be153718a 14 #define MIN_SPEED 0x08
imanomadao 0:2f3be153718a 15 #define FS_SPD 0x15
imanomadao 0:2f3be153718a 16 #define KVAL_HOLD 0x09
imanomadao 0:2f3be153718a 17 #define KVAL_RUN 0x0A
imanomadao 0:2f3be153718a 18 #define KVAL_ACC 0x0B
imanomadao 0:2f3be153718a 19 #define KVAL_DEC 0x0C
imanomadao 0:2f3be153718a 20 #define INT_SPEED 0x0D
imanomadao 0:2f3be153718a 21 #define ST_SLP 0x0E
imanomadao 0:2f3be153718a 22 #define FN_SLP_ACC 0x0F
imanomadao 0:2f3be153718a 23 #define FN_SLP_DEC 0x10
imanomadao 0:2f3be153718a 24 #define K_THERM 0x11
imanomadao 0:2f3be153718a 25 #define ADC_OUT 0x12
imanomadao 0:2f3be153718a 26 #define OCD_TH 0x13
imanomadao 0:2f3be153718a 27 #define STALL_TH 0x14
imanomadao 0:2f3be153718a 28 #define STEP_MODE 0x16
imanomadao 0:2f3be153718a 29 #define ALARM_EN 0x17
imanomadao 0:2f3be153718a 30 #define CONFIG 0x18
imanomadao 0:2f3be153718a 31 #define STATUS 0x19
imanomadao 0:2f3be153718a 32
imanomadao 0:2f3be153718a 33 //L6470 Commands
imanomadao 2:09ae6c1aab1a 34 #define NOP 0x000
imanomadao 2:09ae6c1aab1a 35 #define SetParam 0x0<<5 // ( | PARAM )
imanomadao 2:09ae6c1aab1a 36 #define GetParam 0x01<<5 // ( | PARAM )
imanomadao 2:09ae6c1aab1a 37 #define Run_Forward 0x50
imanomadao 2:09ae6c1aab1a 38 #define Run_Back 0x51
imanomadao 0:2f3be153718a 39 #define Step_Clock_Forward 0x58
imanomadao 2:09ae6c1aab1a 40 #define Step_Clock_Back 0x59
imanomadao 2:09ae6c1aab1a 41 #define Move_Forward 0x40
imanomadao 2:09ae6c1aab1a 42 #define Move_Back 0x41
imanomadao 2:09ae6c1aab1a 43 #define GoTo 0x60
imanomadao 2:09ae6c1aab1a 44 #define GoTo_DIR_Forward 0x68
imanomadao 2:09ae6c1aab1a 45 #define GoTo_DIR_Back 0x69
imanomadao 0:2f3be153718a 46 //#define GoUntill
imanomadao 0:2f3be153718a 47 //#define ReleseSW
imanomadao 2:09ae6c1aab1a 48 #define GoHome 0x70
imanomadao 2:09ae6c1aab1a 49 #define GoMark 0x78
imanomadao 2:09ae6c1aab1a 50 #define ResetPos 0xD8
imanomadao 2:09ae6c1aab1a 51 #define ResetDevice 0xC0
imanomadao 2:09ae6c1aab1a 52 #define SoftStop 0xB0
imanomadao 2:09ae6c1aab1a 53 #define HardStop 0xB8
imanomadao 2:09ae6c1aab1a 54 #define SoftHiZ 0xA0
imanomadao 2:09ae6c1aab1a 55 #define HardHiz 0xA8
imanomadao 2:09ae6c1aab1a 56 #define GetStatus 0xD8
imanomadao 0:2f3be153718a 57
imanomadao 0:2f3be153718a 58
imanomadao 0:2f3be153718a 59
imanomadao 0:2f3be153718a 60
imanomadao 0:2f3be153718a 61 class L6470
imanomadao 0:2f3be153718a 62 {
imanomadao 2:09ae6c1aab1a 63 SPI spi;
imanomadao 2:09ae6c1aab1a 64 DigitalOut cs;
imanomadao 0:2f3be153718a 65
imanomadao 0:2f3be153718a 66 public:
imanomadao 2:09ae6c1aab1a 67 L6470(PinName mosi, PinName miso, PinName sclk, PinName cs):spi(mosi, miso, sclk),cs(cs)
imanomadao 0:2f3be153718a 68 {
imanomadao 2:09ae6c1aab1a 69 spi.format(8,2);
imanomadao 2:09ae6c1aab1a 70 spi.frequency(4960000);
imanomadao 0:2f3be153718a 71 }
imanomadao 2:09ae6c1aab1a 72
imanomadao 0:2f3be153718a 73 //~L6470();
imanomadao 0:2f3be153718a 74
imanomadao 0:2f3be153718a 75
imanomadao 0:2f3be153718a 76 //L6470(PinName mosi, PinName miso, PinName sclk, PinName ssel);
imanomadao 0:2f3be153718a 77
imanomadao 0:2f3be153718a 78 void init()
imanomadao 0:2f3be153718a 79 {
imanomadao 0:2f3be153718a 80 // L6470のspi通信の有効化
imanomadao 1:6cd6cd00aabe 81 //_spi.format(8,2);
imanomadao 1:6cd6cd00aabe 82 //_spi.frequency(4960000);
imanomadao 0:2f3be153718a 83
imanomadao 0:2f3be153718a 84 // nopを送ることで残留している命令を排除
imanomadao 2:09ae6c1aab1a 85 cs = 1;
imanomadao 2:09ae6c1aab1a 86 spi.write(0x00);
imanomadao 2:09ae6c1aab1a 87 spi.write(0x00);
imanomadao 2:09ae6c1aab1a 88 spi.write(0x00);
imanomadao 2:09ae6c1aab1a 89 spi.write(0x00);
imanomadao 2:09ae6c1aab1a 90 cs = 0;
imanomadao 0:2f3be153718a 91
imanomadao 0:2f3be153718a 92 //リセット
imanomadao 2:09ae6c1aab1a 93 cs = 1;
imanomadao 2:09ae6c1aab1a 94 spi.write(0xC0);
imanomadao 2:09ae6c1aab1a 95 cs = 0;
imanomadao 0:2f3be153718a 96
imanomadao 0:2f3be153718a 97 // MAX_SPEED設定。
imanomadao 2:09ae6c1aab1a 98 cs = 1;
imanomadao 2:09ae6c1aab1a 99 spi.write(0x07);
imanomadao 0:2f3be153718a 100 // 最大回転スピード値(10bit) 初期値は 0x41
imanomadao 2:09ae6c1aab1a 101 spi.write(0x00);
imanomadao 2:09ae6c1aab1a 102 spi.write(0x23);
imanomadao 2:09ae6c1aab1a 103 cs = 0;
imanomadao 0:2f3be153718a 104
imanomadao 0:2f3be153718a 105 // KVAL_HOLD設定。
imanomadao 0:2f3be153718a 106 /// レジスタアドレス。
imanomadao 2:09ae6c1aab1a 107 cs = 1;
imanomadao 2:09ae6c1aab1a 108 spi.write(0x09);
imanomadao 2:09ae6c1aab1a 109 spi.write(0xFF);
imanomadao 2:09ae6c1aab1a 110 cs = 0;
imanomadao 0:2f3be153718a 111
imanomadao 0:2f3be153718a 112 // KVAL_spiUN設定。
imanomadao 0:2f3be153718a 113 /// レジスタアドレス。
imanomadao 2:09ae6c1aab1a 114 cs = 1;
imanomadao 2:09ae6c1aab1a 115 spi.write(0x0A);
imanomadao 2:09ae6c1aab1a 116 spi.write(0xFF);
imanomadao 2:09ae6c1aab1a 117 cs = 0;
imanomadao 0:2f3be153718a 118
imanomadao 0:2f3be153718a 119 // KVAL_ACC設定。
imanomadao 2:09ae6c1aab1a 120 cs = 1;
imanomadao 2:09ae6c1aab1a 121 spi.write(0x0B);
imanomadao 2:09ae6c1aab1a 122 spi.write(0xFF);
imanomadao 2:09ae6c1aab1a 123 cs = 0;
imanomadao 0:2f3be153718a 124
imanomadao 0:2f3be153718a 125 // KVAL_DEC設定。
imanomadao 0:2f3be153718a 126 /// レジスタアドレス。
imanomadao 2:09ae6c1aab1a 127 cs= 1;
imanomadao 2:09ae6c1aab1a 128 spi.write(0x0C);
imanomadao 2:09ae6c1aab1a 129 spi.write(0x40);
imanomadao 2:09ae6c1aab1a 130 cs = 0;
imanomadao 0:2f3be153718a 131
imanomadao 0:2f3be153718a 132 // OCD_TH設定。
imanomadao 0:2f3be153718a 133 /// レジスタアドレス。
imanomadao 2:09ae6c1aab1a 134 cs = 1;
imanomadao 2:09ae6c1aab1a 135 spi.write(0x13);
imanomadao 2:09ae6c1aab1a 136 spi.write(0x0F);
imanomadao 2:09ae6c1aab1a 137 cs = 0;
imanomadao 0:2f3be153718a 138
imanomadao 0:2f3be153718a 139 // STALL_TH設定。
imanomadao 0:2f3be153718a 140 /// レジスタアドレス。
imanomadao 2:09ae6c1aab1a 141 cs = 1;
imanomadao 2:09ae6c1aab1a 142 spi.write(0x14);
imanomadao 2:09ae6c1aab1a 143 spi.write(0x7F);
imanomadao 2:09ae6c1aab1a 144 cs = 0;
imanomadao 0:2f3be153718a 145 };
imanomadao 0:2f3be153718a 146 //void writeByte(uint8_t);
imanomadao 0:2f3be153718a 147 void writeByte(uint8_t data)
imanomadao 0:2f3be153718a 148 {
imanomadao 2:09ae6c1aab1a 149 cs = 1;
imanomadao 2:09ae6c1aab1a 150 spi.write(data);
imanomadao 2:09ae6c1aab1a 151 cs = 0;
imanomadao 0:2f3be153718a 152 }
imanomadao 0:2f3be153718a 153
imanomadao 0:2f3be153718a 154 void writeBytes(uint8_t add, int bytes, uint32_t value)
imanomadao 0:2f3be153718a 155 {
imanomadao 0:2f3be153718a 156 uint32_t data[3];
imanomadao 0:2f3be153718a 157 for (int i = 0; i < bytes; i++){
imanomadao 0:2f3be153718a 158 data[i] = value & 0xff;
imanomadao 0:2f3be153718a 159 value = value >> 8;
imanomadao 0:2f3be153718a 160 }
imanomadao 0:2f3be153718a 161 if(bytes == 3){
imanomadao 2:09ae6c1aab1a 162 cs = 1;
imanomadao 2:09ae6c1aab1a 163 spi.write(add);
imanomadao 2:09ae6c1aab1a 164 spi.write(data[2]);
imanomadao 2:09ae6c1aab1a 165 spi.write(data[1]);
imanomadao 2:09ae6c1aab1a 166 spi.write(data[0]);
imanomadao 2:09ae6c1aab1a 167 cs = 0;
imanomadao 0:2f3be153718a 168 }
imanomadao 0:2f3be153718a 169 else if ( bytes == 2 ) {
imanomadao 2:09ae6c1aab1a 170 cs = 1;
imanomadao 2:09ae6c1aab1a 171 spi.write( add );
imanomadao 2:09ae6c1aab1a 172 spi.write( data[1] ) ;
imanomadao 2:09ae6c1aab1a 173 spi.write( data[0] ) ;
imanomadao 2:09ae6c1aab1a 174 cs = 0;
imanomadao 0:2f3be153718a 175 }
imanomadao 0:2f3be153718a 176 else if ( bytes == 1 ) {
imanomadao 2:09ae6c1aab1a 177 cs = 1;
imanomadao 2:09ae6c1aab1a 178 spi.write( add );
imanomadao 2:09ae6c1aab1a 179 spi.write( data[0] ) ;
imanomadao 2:09ae6c1aab1a 180 cs = 0;
imanomadao 0:2f3be153718a 181 }
imanomadao 0:2f3be153718a 182 }
imanomadao 0:2f3be153718a 183
imanomadao 0:2f3be153718a 184 //void writeBytes(uint8_t, int, uint32_t);
imanomadao 2:09ae6c1aab1a 185 void L6470_run(long speed)
imanomadao 0:2f3be153718a 186 {
imanomadao 0:2f3be153718a 187 uint8_t dir;
imanomadao 2:09ae6c1aab1a 188 long spd;
imanomadao 0:2f3be153718a 189 uint8_t spd_h;
imanomadao 0:2f3be153718a 190 uint8_t spd_m;
imanomadao 0:2f3be153718a 191 uint8_t spd_l;
imanomadao 0:2f3be153718a 192
imanomadao 0:2f3be153718a 193 // 方向検出。
imanomadao 0:2f3be153718a 194 if (speed < 0)
imanomadao 0:2f3be153718a 195 {
imanomadao 0:2f3be153718a 196 dir = 0x50;
imanomadao 0:2f3be153718a 197 spd = -1 * speed;
imanomadao 0:2f3be153718a 198 }
imanomadao 0:2f3be153718a 199 else
imanomadao 0:2f3be153718a 200 {
imanomadao 0:2f3be153718a 201 dir = 0x51;
imanomadao 0:2f3be153718a 202 spd = speed;
imanomadao 0:2f3be153718a 203 }
imanomadao 0:2f3be153718a 204
imanomadao 0:2f3be153718a 205 // 送信バイトデータ生成。
imanomadao 2:09ae6c1aab1a 206 spd_h = (uint8_t)((0x000F0000 & (uint32_t)spd) >> 16);
imanomadao 2:09ae6c1aab1a 207 spd_m = (uint8_t)((0x0000FF00 & (uint32_t)spd) >> 8);
imanomadao 2:09ae6c1aab1a 208 spd_l = (uint8_t)(0x000000FF & (uint32_t)spd);
imanomadao 0:2f3be153718a 209 // コマンド(レジスタアドレス)送信。
imanomadao 2:09ae6c1aab1a 210 cs = 1;
imanomadao 2:09ae6c1aab1a 211 spi.write(dir);
imanomadao 2:09ae6c1aab1a 212 spi.write(spd_h);
imanomadao 2:09ae6c1aab1a 213 spi.write(spd_m);
imanomadao 2:09ae6c1aab1a 214 spi.write(spd_l);
imanomadao 2:09ae6c1aab1a 215 cs = 0;
imanomadao 0:2f3be153718a 216 }
imanomadao 0:2f3be153718a 217
imanomadao 0:2f3be153718a 218 //void L6470_run(uint32_t);
imanomadao 0:2f3be153718a 219
imanomadao 2:09ae6c1aab1a 220 int getstatus(uint8_t data)
imanomadao 2:09ae6c1aab1a 221 {
imanomadao 2:09ae6c1aab1a 222 cs = 1;
imanomadao 2:09ae6c1aab1a 223 int s = spi.write(data);
imanomadao 2:09ae6c1aab1a 224 cs = 0;
imanomadao 2:09ae6c1aab1a 225 return s;
imanomadao 2:09ae6c1aab1a 226 }
imanomadao 0:2f3be153718a 227 };
imanomadao 0:2f3be153718a 228
imanomadao 0:2f3be153718a 229
imanomadao 0:2f3be153718a 230 #endif