Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
fpga.cpp@18:5aa48aec9cae, 2018-12-01 (annotated)
- Committer:
- takeru0x1103
- Date:
- Sat Dec 01 14:03:08 2018 +0000
- Revision:
- 18:5aa48aec9cae
- Parent:
- 17:f9610f3cfa1b
- Child:
- 20:0394e15412c3
??????????PID????????????
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| takeru0x1103 | 0:ecd925601fc6 | 1 | #include "mbed.h" |
| takeru0x1103 | 0:ecd925601fc6 | 2 | #include "fpga.h" |
| takeru0x1103 | 18:5aa48aec9cae | 3 | #include "globalFlags.h" |
| takeru0x1103 | 8:1ca49cb18290 | 4 | |
| takeru0x1103 | 8:1ca49cb18290 | 5 | #define CLK_CYC 20.83 //FPGAのクロック周期 |
| takeru0x1103 | 0:ecd925601fc6 | 6 | |
| takeru0x1103 | 8:1ca49cb18290 | 7 | //----------------------------- |
| takeru0x1103 | 8:1ca49cb18290 | 8 | //FPGAレジスタアドレスマップ |
| takeru0x1103 | 0:ecd925601fc6 | 9 | #define ADR_TEST_REG 0x0 //テストレジスタ |
| takeru0x1103 | 0:ecd925601fc6 | 10 | #define ADR_RPM_FLSB 0x1 //エンジン回転数 前 LSB |
| takeru0x1103 | 0:ecd925601fc6 | 11 | #define ADR_RPM_FMSB 0x2 //エンジン回転数 前 MSB |
| takeru0x1103 | 0:ecd925601fc6 | 12 | #define ADR_RPM_BLSB 0x3 //エンジン回転数 後ろ LSB |
| takeru0x1103 | 0:ecd925601fc6 | 13 | #define ADR_RPM_BMSB 0x4 //エンジン回転数 後ろ MSB |
| takeru0x1103 | 0:ecd925601fc6 | 14 | |
| takeru0x1103 | 0:ecd925601fc6 | 15 | #define ADR_SUB_FL_I 0x5 //前 ◀ in |
| takeru0x1103 | 0:ecd925601fc6 | 16 | #define ADR_SUB_FL_O 0x6 //前 ◀ out |
| takeru0x1103 | 0:ecd925601fc6 | 17 | #define ADR_SUB_FR_I 0x7 //前 ▶ in |
| takeru0x1103 | 0:ecd925601fc6 | 18 | #define ADR_SUB_FR_O 0x8 //前 ▶ out |
| takeru0x1103 | 0:ecd925601fc6 | 19 | |
| takeru0x1103 | 0:ecd925601fc6 | 20 | #define ADR_SUB_BL_I 0x9 //後ろ ◀ in |
| takeru0x1103 | 0:ecd925601fc6 | 21 | #define ADR_SUB_BL_O 0xA //後ろ ◀ out |
| takeru0x1103 | 0:ecd925601fc6 | 22 | #define ADR_SUB_BR_I 0xB //後ろ ▶ in |
| takeru0x1103 | 0:ecd925601fc6 | 23 | #define ADR_SUB_BR_O 0xC //後ろ ▶ out |
| takeru0x1103 | 0:ecd925601fc6 | 24 | |
| takeru0x1103 | 0:ecd925601fc6 | 25 | #define ADR_USER_SW 0xD //ユーザースイッチ |
| takeru0x1103 | 0:ecd925601fc6 | 26 | #define ADR_ACCEL_F 0xE //アクセル前 |
| takeru0x1103 | 0:ecd925601fc6 | 27 | #define ADR_ACCEL_B 0xF //アクセル後ろ |
| takeru0x1103 | 8:1ca49cb18290 | 28 | //----------------------------- |
| takeru0x1103 | 0:ecd925601fc6 | 29 | |
| takeru0x1103 | 0:ecd925601fc6 | 30 | // ################################################################ |
| takeru0x1103 | 0:ecd925601fc6 | 31 | // SPI通信フォーマットに合わせた供用体定義 |
| takeru0x1103 | 0:ecd925601fc6 | 32 | // bit |15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0| |
| takeru0x1103 | 0:ecd925601fc6 | 33 | // function | ID | Data | |
| takeru0x1103 | 0:ecd925601fc6 | 34 | // ################################################################ |
| takeru0x1103 | 0:ecd925601fc6 | 35 | typedef union{ |
| takeru0x1103 | 0:ecd925601fc6 | 36 | UINT16 spiCmd; |
| takeru0x1103 | 0:ecd925601fc6 | 37 | struct{ |
| takeru0x1103 | 0:ecd925601fc6 | 38 | UINT16 val : 12;//bit[11: 0] 書き込みデータ |
| takeru0x1103 | 0:ecd925601fc6 | 39 | UINT16 id : 4 ;//bit[15:12] レジスタアドレス |
| takeru0x1103 | 0:ecd925601fc6 | 40 | }bf; |
| takeru0x1103 | 0:ecd925601fc6 | 41 | }SPI_CMD; |
| takeru0x1103 | 0:ecd925601fc6 | 42 | |
| takeru0x1103 | 0:ecd925601fc6 | 43 | // ############################################################### |
| takeru0x1103 | 0:ecd925601fc6 | 44 | // グローバル変数 |
| takeru0x1103 | 0:ecd925601fc6 | 45 | // ############################################################### |
| takeru0x1103 | 0:ecd925601fc6 | 46 | static SPI spi (p5,p6,p7) ;//SPI通信ポート定義: mosi, miso, clk |
| takeru0x1103 | 8:1ca49cb18290 | 47 | static DigitalOut CS_n(p8) ;//SPIのチップセレクトピン |
| takeru0x1103 | 0:ecd925601fc6 | 48 | |
| takeru0x1103 | 8:1ca49cb18290 | 49 | //----------------------------------- |
| takeru0x1103 | 0:ecd925601fc6 | 50 | //SPIデータ送出 |
| takeru0x1103 | 8:1ca49cb18290 | 51 | //----------------------------------- |
| takeru0x1103 | 8:1ca49cb18290 | 52 | static UINT16 xSpi16bitSend(UINT16 dat){ |
| takeru0x1103 | 0:ecd925601fc6 | 53 | unsigned int rtn; |
| takeru0x1103 | 0:ecd925601fc6 | 54 | |
| takeru0x1103 | 0:ecd925601fc6 | 55 | CS_n= 0; //チップセレクトアサート |
| takeru0x1103 | 0:ecd925601fc6 | 56 | rtn = spi.write(dat); //データ出力 |
| takeru0x1103 | 0:ecd925601fc6 | 57 | CS_n = 1; //チップセレクトネゲート |
| takeru0x1103 | 8:1ca49cb18290 | 58 | return rtn; //SPIリードデータを返す |
| takeru0x1103 | 8:1ca49cb18290 | 59 | } |
| takeru0x1103 | 8:1ca49cb18290 | 60 | //----------------------------------- |
| takeru0x1103 | 8:1ca49cb18290 | 61 | //SPI送出データの組み立てて通信 |
| takeru0x1103 | 8:1ca49cb18290 | 62 | //----------------------------------- |
| takeru0x1103 | 8:1ca49cb18290 | 63 | static UINT16 xSpi(UCHAR id , UINT16 val){ |
| takeru0x1103 | 8:1ca49cb18290 | 64 | UINT16 spiRtn=0; |
| takeru0x1103 | 8:1ca49cb18290 | 65 | SPI_CMD cmd; |
| takeru0x1103 | 8:1ca49cb18290 | 66 | |
| takeru0x1103 | 8:1ca49cb18290 | 67 | //引数を送信するコマンドに成形 |
| takeru0x1103 | 8:1ca49cb18290 | 68 | cmd.bf.id = id; |
| takeru0x1103 | 8:1ca49cb18290 | 69 | cmd.bf.val = val; |
| takeru0x1103 | 8:1ca49cb18290 | 70 | //SPIデータ送出し読み出し値を取得 |
| takeru0x1103 | 8:1ca49cb18290 | 71 | spiRtn = xSpi16bitSend(cmd.spiCmd); |
| takeru0x1103 | 8:1ca49cb18290 | 72 | //レジスタ読み出し値を返す |
| takeru0x1103 | 8:1ca49cb18290 | 73 | return spiRtn; |
| takeru0x1103 | 0:ecd925601fc6 | 74 | } |
| takeru0x1103 | 0:ecd925601fc6 | 75 | |
| takeru0x1103 | 0:ecd925601fc6 | 76 | //================================================================ |
| takeru0x1103 | 0:ecd925601fc6 | 77 | //テストレジスタアクセス |
| takeru0x1103 | 0:ecd925601fc6 | 78 | //================================================================ |
| takeru0x1103 | 8:1ca49cb18290 | 79 | UINT16 fpgaTest(UINT16 val){ |
| takeru0x1103 | 8:1ca49cb18290 | 80 | //SPI通信 |
| takeru0x1103 | 8:1ca49cb18290 | 81 | UINT16 spiRtn = xSpi(ADR_TEST_REG , val); |
| takeru0x1103 | 8:1ca49cb18290 | 82 | //レジスタ読み出し値を返す |
| takeru0x1103 | 0:ecd925601fc6 | 83 | return spiRtn; |
| takeru0x1103 | 0:ecd925601fc6 | 84 | } |
| takeru0x1103 | 0:ecd925601fc6 | 85 | //================================================================ |
| takeru0x1103 | 0:ecd925601fc6 | 86 | //エンジン回転数を取得 |
| takeru0x1103 | 0:ecd925601fc6 | 87 | //================================================================ |
| takeru0x1103 | 0:ecd925601fc6 | 88 | UINT16 fpgaGetRpm(bool frontFlg){ |
| takeru0x1103 | 0:ecd925601fc6 | 89 | UINT16 RpmMsb=0; |
| takeru0x1103 | 0:ecd925601fc6 | 90 | UINT16 RpmLsb=0; |
| takeru0x1103 | 0:ecd925601fc6 | 91 | UINT32 Rpm=0; |
| takeru0x1103 | 0:ecd925601fc6 | 92 | double clc; |
| takeru0x1103 | 0:ecd925601fc6 | 93 | |
| takeru0x1103 | 8:1ca49cb18290 | 94 | RpmLsb = xSpi((frontFlg==true) ? ADR_RPM_FLSB : ADR_RPM_BLSB , 0);//下位12bit読み出し |
| takeru0x1103 | 8:1ca49cb18290 | 95 | RpmMsb = xSpi((frontFlg==true) ? ADR_RPM_FMSB : ADR_RPM_BMSB , 0);//上位12bit読み出し |
| takeru0x1103 | 0:ecd925601fc6 | 96 | |
| takeru0x1103 | 0:ecd925601fc6 | 97 | //24bit連結:48MHzのクロックサイクル数換算 |
| takeru0x1103 | 0:ecd925601fc6 | 98 | Rpm = (RpmMsb << 12) | RpmLsb; |
| takeru0x1103 | 0:ecd925601fc6 | 99 | |
| takeru0x1103 | 0:ecd925601fc6 | 100 | //RPM換算 |
| takeru0x1103 | 0:ecd925601fc6 | 101 | clc = (double)Rpm * CLK_CYC; |
| takeru0x1103 | 0:ecd925601fc6 | 102 | clc = clc / 1000; |
| takeru0x1103 | 0:ecd925601fc6 | 103 | clc = 30000000 / clc; |
| takeru0x1103 | 8:1ca49cb18290 | 104 | //回転数rpmを返す |
| takeru0x1103 | 0:ecd925601fc6 | 105 | return (UINT16)clc; |
| takeru0x1103 | 0:ecd925601fc6 | 106 | } |
| takeru0x1103 | 0:ecd925601fc6 | 107 | //================================================================ |
| takeru0x1103 | 8:1ca49cb18290 | 108 | //サブプロペラ設定 位置 0:前左 1:前右 2:後左 3:後右 |
| takeru0x1103 | 0:ecd925601fc6 | 109 | //================================================================ |
| takeru0x1103 | 18:5aa48aec9cae | 110 | bool fpgaSubProp(UCHAR iPos,INT16 iVal){ |
| takeru0x1103 | 18:5aa48aec9cae | 111 | UCHAR regAdr[1]; |
| takeru0x1103 | 18:5aa48aec9cae | 112 | UINT16 regVal; |
| takeru0x1103 | 0:ecd925601fc6 | 113 | |
| takeru0x1103 | 0:ecd925601fc6 | 114 | switch (iPos){ |
| takeru0x1103 | 0:ecd925601fc6 | 115 | case 0x0:// |
| takeru0x1103 | 18:5aa48aec9cae | 116 | regAdr[0] = ADR_SUB_FL_I; |
| takeru0x1103 | 18:5aa48aec9cae | 117 | regAdr[1] = ADR_SUB_FL_O; |
| takeru0x1103 | 0:ecd925601fc6 | 118 | break; |
| takeru0x1103 | 0:ecd925601fc6 | 119 | case 0x1:// |
| takeru0x1103 | 18:5aa48aec9cae | 120 | regAdr[0] = ADR_SUB_FR_I; |
| takeru0x1103 | 18:5aa48aec9cae | 121 | regAdr[1] = ADR_SUB_FR_O; |
| takeru0x1103 | 0:ecd925601fc6 | 122 | break; |
| takeru0x1103 | 0:ecd925601fc6 | 123 | case 0x2:// |
| takeru0x1103 | 18:5aa48aec9cae | 124 | regAdr[0] = ADR_SUB_BL_I; |
| takeru0x1103 | 18:5aa48aec9cae | 125 | regAdr[1] = ADR_SUB_BL_O; |
| takeru0x1103 | 0:ecd925601fc6 | 126 | break; |
| takeru0x1103 | 0:ecd925601fc6 | 127 | case 0x3:// |
| takeru0x1103 | 18:5aa48aec9cae | 128 | regAdr[0] = ADR_SUB_BR_I; |
| takeru0x1103 | 18:5aa48aec9cae | 129 | regAdr[1] = ADR_SUB_BR_O; |
| takeru0x1103 | 0:ecd925601fc6 | 130 | break; |
| takeru0x1103 | 0:ecd925601fc6 | 131 | default: |
| takeru0x1103 | 8:1ca49cb18290 | 132 | return false;//何もしないで終わる |
| takeru0x1103 | 0:ecd925601fc6 | 133 | } |
| takeru0x1103 | 18:5aa48aec9cae | 134 | |
| takeru0x1103 | 18:5aa48aec9cae | 135 | //レジスタ設定範囲内にリミット |
| takeru0x1103 | 18:5aa48aec9cae | 136 | if(iVal<0){ |
| takeru0x1103 | 18:5aa48aec9cae | 137 | regVal = 0; |
| takeru0x1103 | 18:5aa48aec9cae | 138 | }else if(iVal>4095){ |
| takeru0x1103 | 18:5aa48aec9cae | 139 | regVal = 4095; |
| takeru0x1103 | 18:5aa48aec9cae | 140 | }else{ |
| takeru0x1103 | 18:5aa48aec9cae | 141 | regVal = iVal; |
| takeru0x1103 | 18:5aa48aec9cae | 142 | } |
| takeru0x1103 | 18:5aa48aec9cae | 143 | |
| takeru0x1103 | 18:5aa48aec9cae | 144 | xSpi(regAdr[0] , regVal);//吸気側 |
| takeru0x1103 | 18:5aa48aec9cae | 145 | xSpi(regAdr[1] , regVal);//排気側 |
| takeru0x1103 | 8:1ca49cb18290 | 146 | |
| takeru0x1103 | 8:1ca49cb18290 | 147 | //正常終了 |
| takeru0x1103 | 8:1ca49cb18290 | 148 | return true; |
| takeru0x1103 | 8:1ca49cb18290 | 149 | } |
| takeru0x1103 | 8:1ca49cb18290 | 150 | |
| takeru0x1103 | 8:1ca49cb18290 | 151 | //================================================================ |
| takeru0x1103 | 18:5aa48aec9cae | 152 | //モーター個別 |
| takeru0x1103 | 18:5aa48aec9cae | 153 | //================================================================ |
| takeru0x1103 | 18:5aa48aec9cae | 154 | void fpgaMotorChk(){ |
| takeru0x1103 | 18:5aa48aec9cae | 155 | UCHAR id; |
| takeru0x1103 | 18:5aa48aec9cae | 156 | UINT16 val; |
| takeru0x1103 | 18:5aa48aec9cae | 157 | |
| takeru0x1103 | 18:5aa48aec9cae | 158 | sp.printf("Motor check start!!\r\n"); |
| takeru0x1103 | 18:5aa48aec9cae | 159 | |
| takeru0x1103 | 18:5aa48aec9cae | 160 | for(id=0; id<8; id++){ |
| takeru0x1103 | 18:5aa48aec9cae | 161 | sp.printf("Motor[%d] check!!\r\n",id); |
| takeru0x1103 | 18:5aa48aec9cae | 162 | for(val=0; val<500; val=val+4){ |
| takeru0x1103 | 18:5aa48aec9cae | 163 | xSpi(ADR_SUB_FL_I + id , val);// |
| takeru0x1103 | 18:5aa48aec9cae | 164 | wait(0.01); |
| takeru0x1103 | 18:5aa48aec9cae | 165 | } |
| takeru0x1103 | 18:5aa48aec9cae | 166 | for(val=500; val>10; val=val-4){ |
| takeru0x1103 | 18:5aa48aec9cae | 167 | xSpi(ADR_SUB_FL_I + id , val);// |
| takeru0x1103 | 18:5aa48aec9cae | 168 | wait(0.01); |
| takeru0x1103 | 18:5aa48aec9cae | 169 | } |
| takeru0x1103 | 18:5aa48aec9cae | 170 | xSpi(ADR_SUB_FL_I + id , 0); |
| takeru0x1103 | 18:5aa48aec9cae | 171 | } |
| takeru0x1103 | 18:5aa48aec9cae | 172 | gf_Chk.bf.mot = false; |
| takeru0x1103 | 18:5aa48aec9cae | 173 | |
| takeru0x1103 | 18:5aa48aec9cae | 174 | } |
| takeru0x1103 | 18:5aa48aec9cae | 175 | |
| takeru0x1103 | 18:5aa48aec9cae | 176 | //================================================================ |
| takeru0x1103 | 8:1ca49cb18290 | 177 | //ユーザースイッチ読み取り |
| takeru0x1103 | 8:1ca49cb18290 | 178 | //================================================================ |
| takeru0x1103 | 8:1ca49cb18290 | 179 | UINT16 fpgaGetUserSw(){ |
| takeru0x1103 | 8:1ca49cb18290 | 180 | //SPIデータ送出/読み出し |
| takeru0x1103 | 8:1ca49cb18290 | 181 | UINT16 spiRtn = xSpi(ADR_USER_SW , 0); |
| takeru0x1103 | 8:1ca49cb18290 | 182 | //戻り値 |
| takeru0x1103 | 8:1ca49cb18290 | 183 | return spiRtn; |
| takeru0x1103 | 8:1ca49cb18290 | 184 | } |
| takeru0x1103 | 8:1ca49cb18290 | 185 | |
| takeru0x1103 | 8:1ca49cb18290 | 186 | //================================================================ |
| takeru0x1103 | 8:1ca49cb18290 | 187 | //アクセル設定 位置 0:前 1後 |
| takeru0x1103 | 8:1ca49cb18290 | 188 | //================================================================ |
| takeru0x1103 | 17:f9610f3cfa1b | 189 | void fpgaEngine(UCHAR iID , UINT16 iSlotl){ |
| takeru0x1103 | 17:f9610f3cfa1b | 190 | xSpi((iID==0)? ADR_ACCEL_F : ADR_ACCEL_B , iSlotl); |
| takeru0x1103 | 0:ecd925601fc6 | 191 | } |
| takeru0x1103 | 0:ecd925601fc6 | 192 | |
| takeru0x1103 | 0:ecd925601fc6 | 193 | //================================================================ |
| takeru0x1103 | 0:ecd925601fc6 | 194 | //FPGA初期化 |
| takeru0x1103 | 0:ecd925601fc6 | 195 | //================================================================ |
| takeru0x1103 | 0:ecd925601fc6 | 196 | void fpgaInit(){ |
| takeru0x1103 | 0:ecd925601fc6 | 197 | spi.format(16,3) ;//SPIのフォーマット指定(bit長、極性) |
| takeru0x1103 | 0:ecd925601fc6 | 198 | spi.frequency(500000) ;//クロック周波数 |
| takeru0x1103 | 0:ecd925601fc6 | 199 | CS_n =1 ;//Low有意なのでHighにしておく |
| takeru0x1103 | 0:ecd925601fc6 | 200 | } |