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@17:f9610f3cfa1b, 2018-11-30 (annotated)
- Committer:
- takeru0x1103
- Date:
- Fri Nov 30 05:24:27 2018 +0000
- Revision:
- 17:f9610f3cfa1b
- Parent:
- 8:1ca49cb18290
- Child:
- 18:5aa48aec9cae
1130
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 | 8:1ca49cb18290 | 3 | |
| takeru0x1103 | 8:1ca49cb18290 | 4 | #define CLK_CYC 20.83 //FPGAのクロック周期 |
| takeru0x1103 | 0:ecd925601fc6 | 5 | |
| takeru0x1103 | 8:1ca49cb18290 | 6 | //----------------------------- |
| takeru0x1103 | 8:1ca49cb18290 | 7 | //FPGAレジスタアドレスマップ |
| takeru0x1103 | 0:ecd925601fc6 | 8 | #define ADR_TEST_REG 0x0 //テストレジスタ |
| takeru0x1103 | 0:ecd925601fc6 | 9 | #define ADR_RPM_FLSB 0x1 //エンジン回転数 前 LSB |
| takeru0x1103 | 0:ecd925601fc6 | 10 | #define ADR_RPM_FMSB 0x2 //エンジン回転数 前 MSB |
| takeru0x1103 | 0:ecd925601fc6 | 11 | #define ADR_RPM_BLSB 0x3 //エンジン回転数 後ろ LSB |
| takeru0x1103 | 0:ecd925601fc6 | 12 | #define ADR_RPM_BMSB 0x4 //エンジン回転数 後ろ MSB |
| takeru0x1103 | 0:ecd925601fc6 | 13 | |
| takeru0x1103 | 0:ecd925601fc6 | 14 | #define ADR_SUB_FL_I 0x5 //前 ◀ in |
| takeru0x1103 | 0:ecd925601fc6 | 15 | #define ADR_SUB_FL_O 0x6 //前 ◀ out |
| takeru0x1103 | 0:ecd925601fc6 | 16 | #define ADR_SUB_FR_I 0x7 //前 ▶ in |
| takeru0x1103 | 0:ecd925601fc6 | 17 | #define ADR_SUB_FR_O 0x8 //前 ▶ out |
| takeru0x1103 | 0:ecd925601fc6 | 18 | |
| takeru0x1103 | 0:ecd925601fc6 | 19 | #define ADR_SUB_BL_I 0x9 //後ろ ◀ in |
| takeru0x1103 | 0:ecd925601fc6 | 20 | #define ADR_SUB_BL_O 0xA //後ろ ◀ out |
| takeru0x1103 | 0:ecd925601fc6 | 21 | #define ADR_SUB_BR_I 0xB //後ろ ▶ in |
| takeru0x1103 | 0:ecd925601fc6 | 22 | #define ADR_SUB_BR_O 0xC //後ろ ▶ out |
| takeru0x1103 | 0:ecd925601fc6 | 23 | |
| takeru0x1103 | 0:ecd925601fc6 | 24 | #define ADR_USER_SW 0xD //ユーザースイッチ |
| takeru0x1103 | 0:ecd925601fc6 | 25 | #define ADR_ACCEL_F 0xE //アクセル前 |
| takeru0x1103 | 0:ecd925601fc6 | 26 | #define ADR_ACCEL_B 0xF //アクセル後ろ |
| takeru0x1103 | 8:1ca49cb18290 | 27 | //----------------------------- |
| takeru0x1103 | 0:ecd925601fc6 | 28 | |
| takeru0x1103 | 0:ecd925601fc6 | 29 | // ################################################################ |
| takeru0x1103 | 0:ecd925601fc6 | 30 | // SPI通信フォーマットに合わせた供用体定義 |
| takeru0x1103 | 0:ecd925601fc6 | 31 | // bit |15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0| |
| takeru0x1103 | 0:ecd925601fc6 | 32 | // function | ID | Data | |
| takeru0x1103 | 0:ecd925601fc6 | 33 | // ################################################################ |
| takeru0x1103 | 0:ecd925601fc6 | 34 | typedef union{ |
| takeru0x1103 | 0:ecd925601fc6 | 35 | UINT16 spiCmd; |
| takeru0x1103 | 0:ecd925601fc6 | 36 | struct{ |
| takeru0x1103 | 0:ecd925601fc6 | 37 | UINT16 val : 12;//bit[11: 0] 書き込みデータ |
| takeru0x1103 | 0:ecd925601fc6 | 38 | UINT16 id : 4 ;//bit[15:12] レジスタアドレス |
| takeru0x1103 | 0:ecd925601fc6 | 39 | }bf; |
| takeru0x1103 | 0:ecd925601fc6 | 40 | }SPI_CMD; |
| takeru0x1103 | 0:ecd925601fc6 | 41 | |
| takeru0x1103 | 0:ecd925601fc6 | 42 | // ############################################################### |
| takeru0x1103 | 0:ecd925601fc6 | 43 | // グローバル変数 |
| takeru0x1103 | 0:ecd925601fc6 | 44 | // ############################################################### |
| takeru0x1103 | 0:ecd925601fc6 | 45 | static SPI spi (p5,p6,p7) ;//SPI通信ポート定義: mosi, miso, clk |
| takeru0x1103 | 8:1ca49cb18290 | 46 | static DigitalOut CS_n(p8) ;//SPIのチップセレクトピン |
| takeru0x1103 | 0:ecd925601fc6 | 47 | |
| takeru0x1103 | 8:1ca49cb18290 | 48 | //----------------------------------- |
| takeru0x1103 | 0:ecd925601fc6 | 49 | //SPIデータ送出 |
| takeru0x1103 | 8:1ca49cb18290 | 50 | //----------------------------------- |
| takeru0x1103 | 8:1ca49cb18290 | 51 | static UINT16 xSpi16bitSend(UINT16 dat){ |
| takeru0x1103 | 0:ecd925601fc6 | 52 | unsigned int rtn; |
| takeru0x1103 | 0:ecd925601fc6 | 53 | |
| takeru0x1103 | 0:ecd925601fc6 | 54 | CS_n= 0; //チップセレクトアサート |
| takeru0x1103 | 0:ecd925601fc6 | 55 | rtn = spi.write(dat); //データ出力 |
| takeru0x1103 | 0:ecd925601fc6 | 56 | CS_n = 1; //チップセレクトネゲート |
| takeru0x1103 | 8:1ca49cb18290 | 57 | return rtn; //SPIリードデータを返す |
| takeru0x1103 | 8:1ca49cb18290 | 58 | } |
| takeru0x1103 | 8:1ca49cb18290 | 59 | //----------------------------------- |
| takeru0x1103 | 8:1ca49cb18290 | 60 | //SPI送出データの組み立てて通信 |
| takeru0x1103 | 8:1ca49cb18290 | 61 | //----------------------------------- |
| takeru0x1103 | 8:1ca49cb18290 | 62 | static UINT16 xSpi(UCHAR id , UINT16 val){ |
| takeru0x1103 | 8:1ca49cb18290 | 63 | UINT16 spiRtn=0; |
| takeru0x1103 | 8:1ca49cb18290 | 64 | SPI_CMD cmd; |
| takeru0x1103 | 8:1ca49cb18290 | 65 | |
| takeru0x1103 | 8:1ca49cb18290 | 66 | //引数を送信するコマンドに成形 |
| takeru0x1103 | 8:1ca49cb18290 | 67 | cmd.bf.id = id; |
| takeru0x1103 | 8:1ca49cb18290 | 68 | cmd.bf.val = val; |
| takeru0x1103 | 8:1ca49cb18290 | 69 | //SPIデータ送出し読み出し値を取得 |
| takeru0x1103 | 8:1ca49cb18290 | 70 | spiRtn = xSpi16bitSend(cmd.spiCmd); |
| takeru0x1103 | 8:1ca49cb18290 | 71 | //レジスタ読み出し値を返す |
| takeru0x1103 | 8:1ca49cb18290 | 72 | return spiRtn; |
| takeru0x1103 | 0:ecd925601fc6 | 73 | } |
| takeru0x1103 | 0:ecd925601fc6 | 74 | |
| takeru0x1103 | 0:ecd925601fc6 | 75 | //================================================================ |
| takeru0x1103 | 0:ecd925601fc6 | 76 | //テストレジスタアクセス |
| takeru0x1103 | 0:ecd925601fc6 | 77 | //================================================================ |
| takeru0x1103 | 8:1ca49cb18290 | 78 | UINT16 fpgaTest(UINT16 val){ |
| takeru0x1103 | 8:1ca49cb18290 | 79 | //SPI通信 |
| takeru0x1103 | 8:1ca49cb18290 | 80 | UINT16 spiRtn = xSpi(ADR_TEST_REG , val); |
| takeru0x1103 | 8:1ca49cb18290 | 81 | //レジスタ読み出し値を返す |
| takeru0x1103 | 0:ecd925601fc6 | 82 | return spiRtn; |
| takeru0x1103 | 0:ecd925601fc6 | 83 | } |
| takeru0x1103 | 0:ecd925601fc6 | 84 | //================================================================ |
| takeru0x1103 | 0:ecd925601fc6 | 85 | //エンジン回転数を取得 |
| takeru0x1103 | 0:ecd925601fc6 | 86 | //================================================================ |
| takeru0x1103 | 0:ecd925601fc6 | 87 | UINT16 fpgaGetRpm(bool frontFlg){ |
| takeru0x1103 | 0:ecd925601fc6 | 88 | UINT16 RpmMsb=0; |
| takeru0x1103 | 0:ecd925601fc6 | 89 | UINT16 RpmLsb=0; |
| takeru0x1103 | 0:ecd925601fc6 | 90 | UINT32 Rpm=0; |
| takeru0x1103 | 0:ecd925601fc6 | 91 | double clc; |
| takeru0x1103 | 0:ecd925601fc6 | 92 | |
| takeru0x1103 | 8:1ca49cb18290 | 93 | RpmLsb = xSpi((frontFlg==true) ? ADR_RPM_FLSB : ADR_RPM_BLSB , 0);//下位12bit読み出し |
| takeru0x1103 | 8:1ca49cb18290 | 94 | RpmMsb = xSpi((frontFlg==true) ? ADR_RPM_FMSB : ADR_RPM_BMSB , 0);//上位12bit読み出し |
| takeru0x1103 | 0:ecd925601fc6 | 95 | |
| takeru0x1103 | 0:ecd925601fc6 | 96 | //24bit連結:48MHzのクロックサイクル数換算 |
| takeru0x1103 | 0:ecd925601fc6 | 97 | Rpm = (RpmMsb << 12) | RpmLsb; |
| takeru0x1103 | 0:ecd925601fc6 | 98 | |
| takeru0x1103 | 0:ecd925601fc6 | 99 | //RPM換算 |
| takeru0x1103 | 0:ecd925601fc6 | 100 | clc = (double)Rpm * CLK_CYC; |
| takeru0x1103 | 0:ecd925601fc6 | 101 | clc = clc / 1000; |
| takeru0x1103 | 0:ecd925601fc6 | 102 | clc = 30000000 / clc; |
| takeru0x1103 | 8:1ca49cb18290 | 103 | //回転数rpmを返す |
| takeru0x1103 | 0:ecd925601fc6 | 104 | return (UINT16)clc; |
| takeru0x1103 | 0:ecd925601fc6 | 105 | } |
| takeru0x1103 | 0:ecd925601fc6 | 106 | //================================================================ |
| takeru0x1103 | 8:1ca49cb18290 | 107 | //サブプロペラ設定 位置 0:前左 1:前右 2:後左 3:後右 |
| takeru0x1103 | 0:ecd925601fc6 | 108 | //================================================================ |
| takeru0x1103 | 8:1ca49cb18290 | 109 | bool fpgaSubProp(UCHAR iPos,UINT16 iSlotl){ |
| takeru0x1103 | 0:ecd925601fc6 | 110 | UCHAR reg_adr[1]; |
| takeru0x1103 | 0:ecd925601fc6 | 111 | |
| takeru0x1103 | 0:ecd925601fc6 | 112 | switch (iPos){ |
| takeru0x1103 | 0:ecd925601fc6 | 113 | case 0x0:// |
| takeru0x1103 | 0:ecd925601fc6 | 114 | reg_adr[0] = ADR_SUB_FL_I; |
| takeru0x1103 | 0:ecd925601fc6 | 115 | reg_adr[1] = ADR_SUB_FL_O; |
| takeru0x1103 | 0:ecd925601fc6 | 116 | break; |
| takeru0x1103 | 0:ecd925601fc6 | 117 | case 0x1:// |
| takeru0x1103 | 0:ecd925601fc6 | 118 | reg_adr[0] = ADR_SUB_FR_I; |
| takeru0x1103 | 0:ecd925601fc6 | 119 | reg_adr[1] = ADR_SUB_FR_O; |
| takeru0x1103 | 0:ecd925601fc6 | 120 | break; |
| takeru0x1103 | 0:ecd925601fc6 | 121 | case 0x2:// |
| takeru0x1103 | 0:ecd925601fc6 | 122 | reg_adr[0] = ADR_SUB_BL_I; |
| takeru0x1103 | 0:ecd925601fc6 | 123 | reg_adr[1] = ADR_SUB_BL_O; |
| takeru0x1103 | 0:ecd925601fc6 | 124 | break; |
| takeru0x1103 | 0:ecd925601fc6 | 125 | case 0x3:// |
| takeru0x1103 | 0:ecd925601fc6 | 126 | reg_adr[0] = ADR_SUB_BR_I; |
| takeru0x1103 | 0:ecd925601fc6 | 127 | reg_adr[1] = ADR_SUB_BR_O; |
| takeru0x1103 | 0:ecd925601fc6 | 128 | break; |
| takeru0x1103 | 0:ecd925601fc6 | 129 | default: |
| takeru0x1103 | 8:1ca49cb18290 | 130 | return false;//何もしないで終わる |
| takeru0x1103 | 0:ecd925601fc6 | 131 | } |
| takeru0x1103 | 0:ecd925601fc6 | 132 | |
| takeru0x1103 | 8:1ca49cb18290 | 133 | xSpi(reg_adr[0] , iSlotl);//吸気側 |
| takeru0x1103 | 8:1ca49cb18290 | 134 | xSpi(reg_adr[1] , iSlotl);//排気側 |
| takeru0x1103 | 8:1ca49cb18290 | 135 | |
| takeru0x1103 | 8:1ca49cb18290 | 136 | //正常終了 |
| takeru0x1103 | 8:1ca49cb18290 | 137 | return true; |
| takeru0x1103 | 8:1ca49cb18290 | 138 | } |
| takeru0x1103 | 8:1ca49cb18290 | 139 | |
| takeru0x1103 | 8:1ca49cb18290 | 140 | //================================================================ |
| takeru0x1103 | 8:1ca49cb18290 | 141 | //ユーザースイッチ読み取り |
| takeru0x1103 | 8:1ca49cb18290 | 142 | //================================================================ |
| takeru0x1103 | 8:1ca49cb18290 | 143 | UINT16 fpgaGetUserSw(){ |
| takeru0x1103 | 8:1ca49cb18290 | 144 | //SPIデータ送出/読み出し |
| takeru0x1103 | 8:1ca49cb18290 | 145 | UINT16 spiRtn = xSpi(ADR_USER_SW , 0); |
| takeru0x1103 | 8:1ca49cb18290 | 146 | //戻り値 |
| takeru0x1103 | 8:1ca49cb18290 | 147 | return spiRtn; |
| takeru0x1103 | 8:1ca49cb18290 | 148 | } |
| takeru0x1103 | 8:1ca49cb18290 | 149 | |
| takeru0x1103 | 8:1ca49cb18290 | 150 | //================================================================ |
| takeru0x1103 | 8:1ca49cb18290 | 151 | //アクセル設定 位置 0:前 1後 |
| takeru0x1103 | 8:1ca49cb18290 | 152 | //================================================================ |
| takeru0x1103 | 17:f9610f3cfa1b | 153 | void fpgaEngine(UCHAR iID , UINT16 iSlotl){ |
| takeru0x1103 | 17:f9610f3cfa1b | 154 | xSpi((iID==0)? ADR_ACCEL_F : ADR_ACCEL_B , iSlotl); |
| takeru0x1103 | 0:ecd925601fc6 | 155 | } |
| takeru0x1103 | 0:ecd925601fc6 | 156 | |
| takeru0x1103 | 0:ecd925601fc6 | 157 | //================================================================ |
| takeru0x1103 | 0:ecd925601fc6 | 158 | //FPGA初期化 |
| takeru0x1103 | 0:ecd925601fc6 | 159 | //================================================================ |
| takeru0x1103 | 0:ecd925601fc6 | 160 | void fpgaInit(){ |
| takeru0x1103 | 0:ecd925601fc6 | 161 | spi.format(16,3) ;//SPIのフォーマット指定(bit長、極性) |
| takeru0x1103 | 0:ecd925601fc6 | 162 | spi.frequency(500000) ;//クロック周波数 |
| takeru0x1103 | 0:ecd925601fc6 | 163 | CS_n =1 ;//Low有意なのでHighにしておく |
| takeru0x1103 | 0:ecd925601fc6 | 164 | } |