teamALI / Mbed 2 deprecated HB2018

Dependencies:   mbed FreeRTOS

Committer:
takeru0x1103
Date:
Fri Nov 23 07:27:43 2018 +0000
Revision:
0:ecd925601fc6
Child:
1:15ab74f0d0f1
????????????????????

Who changed what in which revision?

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