BC-ANT-SERIAL communication library.
Dependents: bc_ant-sample bc_ant-receiver
BeatcraftさんのBC-ANT-SERIAL を mbedで動かしてみました。
BC-ANT-SERIAL については、以下を参照ください。
http://labs.beatcraft.com/ja/index.php?ANT%2FBC-ANT-SERIAL%20%C0%E2%CC%C0%BD%F1
以下のような構成で、BC-ANT-SERIALを 9600bps に設定し、 BC-ANT-USB相手を相手に通信を行いました。 (下図は受信例ですが、逆方向の送信も確認しました。)
なお、BC-ANT-SERIALを9600bpsの設定にする場合は、BR2をショートさせます。
本ライブラリは、@ryuchi さんの ANT/LPC1114用ライブラリ をベタに持って来てmbed用にしました。
(UARTを使っている部分をSerialクラスに置き換えた程度で動いちゃいました。。。あんまり中身を見ていません。。)
ライブラリ使用例
受信で使用する場合
受信で使用する場合のmain.cpp例
#include "mbed.h" #include "bc_ant.h" DigitalOut myled(LED1); // LEDチカチカ Serial pc(USBTX, USBRX); // USBへのデータ通信 bc_ant antPort(p13, p14, p15); // for BC-ANT-SERIALの接続。(9600bps, p13, p14, ctsにp15) /** ANT用の定数定義 **/ #define MAX_BUFSIZE 64 // 受信バッファサイズ用の定義 #define ANT_CH 0 // チャンネル番号 #define DEVICE_NUMBER 65 // デバイス番号 /** * BC-ANT-SERIALでデータを受信した時の処理 */ void receivedFromAnt() { // 受信データを受け取る領域の確保 uint8_t receivedLength = 0; uint8_t receivedBuffer[MAX_BUFSIZE]; // データを受信する receivedLength = antPort.BC_ANT_RecvPacket(receivedBuffer, MAX_BUFSIZE); // 受信したデータを printf()して、PCのターミナルに表示する。 printf("RX(%3d):", receivedLength); for (int index = 0; index < receivedLength; index++) { printf("%02x ", receivedBuffer[index]); } printf("\r\n"); } /** * BC-ANT-SERIALの(受信)初期化処理 */ int initialize_ANTport() { antPort.ANT_ResetSystem(); antPort.ANT_AssignChannel(ANT_CH, ANT_Bidirectional_Slave, 0); // チャンネルタイプの設定 antPort.ANT_SetChannelId(ANT_CH, DEVICE_NUMBER, 1, 1); // チャンネルIDの設定 antPort.ANT_SetChannelPeriod_Hz(ANT_CH, 4); // チャンネルピリオドの設定 antPort.ANT_SetChannelRFFreq(ANT_CH, 4); // 通信周波数の設定: 2404 MHz antPort.ANT_OpenChannel(ANT_CH); // コールバック関数の設定 (データを受信したら receivedFromAnt() を呼び出してもらうよう設定する) antPort.attach(&receivedFromAnt); return (0); } /** * メイン処理 */ int main() { pc.baud(9600); // mbed と PCの間の通信速度を 9600bpsに設定する。 initialize_ANTport(); // BC-ANT-SERIALを受信用に初期化する。 printf("--- READY ----\r\n"); // 準備が出来たことを PCのターミナルに通知する。 // 以下は、LEDチカチカ。 (デフォルトで生成される処理をそのまま残してます) while(1) { myled = 1; wait(0.2); myled = 0; wait(0.2); } }
送信で使用する場合
送信で使用する場合は、受信と比較して antPort.ANT_AssignChannel() と ANT_OpenChannel()のあとの処理が異なるぐらいです。
送受信で、antPort.ANT_SetChannelId(), antPort.ANT_SetChannelPeriod_Hz(), antPort.ANT_SetChannelRFFreq() の設定は合わせておく必要がありそうです。
送信で使用する場合のmain.cpp例
#include "mbed.h" #include "bc_ant.h" DigitalOut myled(LED1); // LEDチカチカ Serial pc(USBTX, USBRX); // USBへのデータ通信 bc_ant antPort(p13, p14, p15); // for BC-ANT-SERIALの接続。(9600bps, p13, p14, ctsにp15) /** ANT用の定数定義 **/ #define MAX_BUFSIZE 64 // 受信バッファサイズ用の定義 #define ANT_CH 0 // チャンネル番号 #define DEVICE_NUMBER 65 // デバイス番号 /** * PCのターミナルからキー入力された時の処理 * (キー入力の情報をAntポートに送る) */ void receivedPc() { uint8_t buffer[MAX_BUFSIZE] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; while (pc.readable()) { // データを取り出して、それをAntに送る unsigned char receivedChar = (pc.getc() & 0xff); buffer[0] = receivedChar; antPort.ANT_SendAcknowledgedData(0, buffer); } } /** * 初期化処理 */ int initialize_ANTport() { antPort.ANT_ResetSystem(); antPort.ANT_AssignChannel(ANT_CH, ANT_Bidirectional_Slave, 0); // チャンネルタイプの設定 antPort.ANT_SetChannelId(ANT_CH, DEVICE_NUMBER, 1, 1); // チャンネルIDの設定 antPort.ANT_SetChannelPeriod_Hz(ANT_CH, 4); // チャンネルピリオドの設定 antPort.ANT_SetChannelRFFreq(ANT_CH, 4); // 通信周波数の設定: 2404 MHz antPort.ANT_OpenChannel(ANT_CH); { uint8_t buffer[MAX_BUFSIZE] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; antPort.ANT_SendBroadcastData(0, buffer); // データ送信? } return (0); } /** * メイン処理 */ int main() { pc.baud(9600); // PCとの通信速度を 9600bps に設定する pc.attach(&receivedPc,Serial::RxIrq); // PCからデータが送られてきた場合、receviedPc() 関数を呼び出す用に設定する initialize_ANTport() // BC-ANT-SERIALの初期化を行う printf("--- READY ----\r\n"); // 準備が出来たことを PCのターミナルに通知する。 // 以下は、LEDチカチカ。 (デフォルトで生成される処理をそのまま残してます) while(1) { myled = 1; wait(0.2); myled = 0; wait(0.2); } }
参考:BC-ANT-USB側
動作確認をした BC-ANT-USB 側は、ANT/BCA_Lib のページにある BCA_Lib.zipのSendCB.exe, RecvCB.exe を 利用させて頂きました。
ただし、BCA_OpenChannel() の引数は以下のように変更しました。
SendCB.cppで変更した箇所(SendCB.cpp:52行目あたり)
res = BCA_OpenChannel(dev, // device 0, // channel no BCA_CHANNEL_TYPE_MASTER, // channel type BCA_CHANNEL_NOSHARED, // shared channel 65, // device no 1, // device type 1, // trans type 4, // freq 4); // hz
RecvCB.cppで変更した箇所(RecvCB.cpp:54行目あたり)
res = BCA_OpenChannel(dev, // device 0, // channel no BCA_CHANNEL_TYPE_SLAVE, // channel type BCA_CHANNEL_NOSHARED, // shared channel 0, // device no 0, // device type 0, // trans type 4, // freq 4.0); // hz
備忘録
- ほんとにベタ移植で動いた、という段階なので、中身を確認して理解しないといけない。
- きれいにクラス化したい。
- ちゃんとコードに説明を書きたい。
Diff: bc_ant.h
- Revision:
- 0:97c748c69103
- Child:
- 1:b166aab45b3f
diff -r 000000000000 -r 97c748c69103 bc_ant.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bc_ant.h Sun Mar 17 09:05:20 2013 +0000 @@ -0,0 +1,234 @@ +/* + * ant.h + * + * Created on: 2012/10/12 + * Author: ryuchi + * + * Adopted for mbed by MRSa (17.Mar.2013) + */ + +#ifndef ANT_H_ +#define ANT_H_ + +#include "mbed.h" + +#define ANT_SYNCBYTE 0xA4 + +#define ANT_UNASSIGN_CHANNEL 0x41 +#define ANT_ASSIGN_CHANNEL 0x42 +#define ANT_SET_CHANNEL_ID 0x51 +#define ANT_CHANNEL_MESSAGING_PREIOD 0x43 +#define ANT_CHANNEL_SEARCH_TIMEOUT 0x44 +#define ANT_CHANNEL_RF_FREQUENCY 0x45 +#define ANT_SET_NETWORK_KEY 0x46 +#define ANT_TRANSMIT_POWER 0x47 +#define ANT_ADD_CHANNEL_ID 0x59 +#define ANT_CONFIG_LIST_ID 0x5A +#define ANT_SET_CHANNEL_TX_POWER 0x60 +#define ANT_CHANNEL_LOW_PRIORITY_SERARCH_TIMEOUT 0x63 +#define ANT_SERIAL_NUMBER_CHANNEL_ID 0x65 +#define ANT_ENABLE_EXTENDED_MESSAGGES 0x66 +#define ANT_ENABLE_LED 0x68 +#define ANT_ENABLE_CRYSTAL 0x6D +#define ANT_FREQUENCY_AGILITY 0x70 +#define ANT_PROXIMITY_SEARCH 0x71 +#define ANT_SET_USB_DESCRIPTOR_STRING 0xC7 +#define ANT_STARTUP_MESSAGE 0x6F +#define ANT_RESET_SYSTEM 0x4A +#define ANT_OPEN_CHANNEL 0x4B +#define ANT_CLOSE_CHANNEL 0x4C +#define ANT_REQUEST_MESSAGE 0x4D +#define ANT_OPEN_RX_SCAN_MODE 0x5B +#define ANT_SLEEP_MESSAGE 0xC5 +#define ANT_BROADCAST_DATA 0x4E +#define ANT_ACKNOWLEDGED_DATA 0x4F +#define ANT_BURST_DATA 0x50 +#define ANT_CHANNEL_RESPONSE 0x40 +#define ANT_CHANNEL_EVENT 0x40 +#define ANT_CHANNEL_STATUS 0x52 +#define ANT_CHANNEL_ID 0x51 +#define ANT_VERSION 0x31 +#define ANT_CAPABILITIES 0x54 +#define ANT_DEVICE_SERIAL_NUMBER 0x61 +#define ANT_INIT_CW_TEST_MODE 0x53 +#define ANT_CW_TEST_MODE 0x48 +#define ANT_EXTENDED_BROADCAST_DATA 0x5D +#define ANT_EXTENDED_ACKNOWLEDGED_DATA 0x5E +#define ANT_EXTENDED_BURST_DATA 0x5F + +// Assign Channel: parameters (Channel Type) +#define ANT_Bidirectional_Slave 0x00 +#define ANT_Bidirectional_Master 0x10 +#define ANT_Shared_Bidirectional_Slave 0x20 +#define ANT_Slave_Receive_Only 0x40 +#define ANT_Master_Transmission_Only 0x50 + +// Assign Channel: parameters (Extened Assignment) +#define ANT_BACKGRAND_SCANNING_CHANNEL_ENABLE 0x01 +#define ANT_FREQUENCY_AGILITY_ENABLE 0x04 + +// Transmit Power: parameters (Transmission Power) +#define ANT_TRANSMIT_POWER_MINUS_20DBM 0x00 +#define ANT_TRANSMIT_POWER_MINUS_10DBM 0x01 +#define ANT_TRANSMIT_POWER_MINUS_5DBM 0x02 +#define ANT_TRANSMIT_POWER_0DBM 0x03 + +// Set Channle Tx Power: parameters (Transmission Power) +#define ANT_SET_CHANNEL_TRANSMIT_POWER_MINUS_20DBM 0x00 +#define ANT_SET_CHANNEL_TRANSMIT_POWER_MINUS_10DBM 0x01 +#define ANT_SET_CHANNEL_TRANSMIT_POWER_MINUS_5DBM 0x02 +#define ANT_SET_CHANNEL_TRANSMIT_POWER_0DBM 0x03 + +// Enable Extended Message: parameters +#define ANT_EXTENDED_MESSAGE_ENABLE 1 +#define ANT_EXTENDED_MESSAGE_DISABLE 0 + +// Enable LED Message: parameters +#define ANT_LED_ENABLE 1 +#define ANT_LED_DISABLE 0 + +#define ANT_CRYSTAL_ENABLE 0 + +// Set USB Descriptor: +#define ANT_USB_DESCRIPTOR_PID_VID 0 +#define ANT_USB_DESCRIPTOR_MANUFACTURE 1 +#define ANT_USB_DESCRIPTOR_DEVICE 2 +#define ANT_USB_DESCRIPTOR_SERIAL_NUMBER 3 + +// Startup Message +#define ANT_STARTUP_MESSAGE 0x6F +#define ANT_STARTUP_MESSAGE_POWER_ON_RESET 0x00 +#define ANT_STARTUP_MESSAGE_HARDWARE_RESET_LINE 0x01 +#define ANT_STARTUP_MESSAGE_WATCH_DOG_RESET 0x02 +#define ANT_STARTUP_MESSAGE_COMMAND_RESET 0x20 +#define ANT_STARTUP_MESSAGE_SYNCHRONOUS_RESET 0x40 +#define ANT_STARTUP_MESSAGE_SUSPEND_RESET 0x80 + +#define ANT_DATA_MESSAGE_EXTENDED_FLAG 0x80 + +#define ANT_MESSAGE_RESPONSE_NO_ERROR 0 +#define ANT_MESSAGE_EVENT_RX_SEARCH_TIMEOUT 1 +#define ANT_MESSAGE_EVENT_RX_FAIL 2 +#define ANT_MESSAGE_EVENT_TX 3 +#define ANT_MESSAGE_EVENT_TRANSFER_RX_FAILED 4 +#define ANT_MESSAGE_EVENT_TRANSFER_TX_COMPLETED 5 +#define ANT_MESSAGE_EVENT_TRANSFER_TX_FAILED 6 +#define ANT_MESSAGE_CHANNEL_CLOSED 7 +#define ANT_MESSAGE_EVENT_RX_FAIL_GO_TO_SEARCH 8 +#define ANT_MESSAGE_EVNET_CHANNEL_COLLISION 9 +#define ANT_MESSAGE_EVENT_TRANSFER_TX_START 10 +#define ANT_MESSAGE_CHANNEL_IN_WRONG_STATE 21 +#define ANT_MESSAGE_CHANNEL_NOT_OPEND 22 +#define ANT_MESSAGE_CHANNEL_ID_NOT_SET 24 +#define ANT_MESSAGE_CLOSE_ALL_CHANNELS 25 +#define ANT_MESSAGE_TRANSFER_IN_PROGRESS 31 +#define ANT_MESSAGE_TRANSFER_SEQUENCE_NUMBER_ERROR 32 +#define ANT_MESSAGE_TRANSFER_IN_ERROR 33 +#define ANT_MESSAGE_INVALID_MESSAGE 40 +#define ANT_MESSAGE_INVALID_NETWORK_NUMBER 41 +#define ANT_MESSAGE_LIST_ID 48 +#define ANT_MESSAGE_INVALID_SCAN_TX_CHANNEL 49 +#define ANT_MESSAGE_INVALID_PARAMETER_PROVIDED 51 +#define ANT_MESSAGE_EVENT_QUE_OVERFLOW 53 +#define ANT_MESSAGE_NVM_FULL_ERROR 64 +#define ANT_MESSAGE_WRITE_ERROR 65 + +#define ANT_CHANNEL_STATUS_UN_ASSIGNED 0 +#define ANT_CHANNEL_STATUS_ASSIGNED 1 +#define ANT_CHANNEL_STATUS_SEARCHING 2 +#define ANT_CHANNEL_STATUS_TRACKING 3 + +#define ANT_CAPABILITIES_NO_RECEIVE_CHANNELS 0x01 +#define ANT_CAPABILITIES_NO_TRANSMIT_CHANNELS 0x02 +#define ANT_CAPABILITIES_NO_RECEIVE_MESSAGE 0x04 +#define ANT_CAPABILITIES_NO_TRANSMIT_MESSAGE 0x08 +#define ANT_CAPABILITIES_NO_ACKD_MESSAGE 0x10 +#define ANT_CAPABILITIES_NO_BURST_MESSAGE 0x20 +#define ANT_CAPABILITIES_NETWORK_ENABLED 0x01 +#define ANT_CAPABILITIES_SERIAL_NUMBER_ENABLED 0x04 +#define ANT_CAPABILITIES_PER_CHANNEL_TX_POWER_ENABLED 0x10 +#define ANT_CAPABILITIES_LOW_PRIORITY_SEARCH_ENABLED 0x20 +#define ANT_CAPABILITIES_SCRIPT_ENABLED 0x40 +#define ANT_CAPABILITIES_SEARCH_LIST_ENABLED 0x80 +#define ANT_CAPABILITIES_LED_ENABLED 0x01 +#define ANT_CAPABILITIES_EXT_MESSAGE_ENABLED 0x02 +#define ANT_CAPABILITIES_SCAN_MODE_ENABLED 0x04 +#define ANT_CAPABILITIES_PROX_SRARCH_ENABLED 0x10 +#define ANT_CAPABILITIES_EXT_ASSIGN_ENABLED 0x20 + +#define ANT_DEVICE_SERIAL_NUMBER 0x61 + +#define ANT_PADDING_BYTE 0 + + +namespace mbed { + +class bc_ant +{ +public: + bc_ant(PinName rx, PinName tx, PinName cts, int baud = 9600); + +uint32_t ANT_UnAssignChannel( uint8_t Channel ); +uint32_t ANT_AssignChannel( uint8_t Channel, uint8_t ChannelType, uint8_t NetworkNumber) ; +uint32_t ANT_AssignChannelExt( uint8_t Channel, uint8_t ChannelType, uint8_t NetworkNumber, uint8_t Extend ); +uint32_t ANT_SetChannelId( uint8_t Channel, uint16_t DeviceNum, uint8_t DeviceType, uint8_t TransmissionType ); +uint32_t ANT_SetChannelPeriod( uint8_t Channel, uint16_t MessaggePeriod ); +uint32_t ANT_SetChannelPeriod_Hz( uint8_t Channel, uint16_t Period ); +uint32_t ANT_SetChannelSearchTimeout( uint8_t ChannelNum, uint8_t SearcchTimeout ); +uint32_t ANT_SetChannelRFFreq( uint8_t Channel, uint8_t RFFreq ); +uint32_t ANT_SetNetworkKey( uint8_t NetworkNumber, uint8_t *pucKey); +uint32_t ANT_SetTransmitPower( uint8_t TransmitPower ); +uint32_t ANT_AddChannelID( uint8_t Channel, uint16_t DeviceNum, uint8_t DeviceType, uint8_t TransmissionType, uint8_t ListIndex ); +uint32_t ANT_ConfigList( uint8_t Channel, uint8_t ListSize, uint8_t Exclude ); +uint32_t ANT_SetChannelTxPower( uint8_t Channel, uint8_t TxPower ); +uint32_t ANT_SetLowPriorityChannelSearchTimeout( uint8_t ChannelNum, uint8_t SearchTimeout ); +uint32_t ANT_SetSerialNumChannelId( uint8_t Channel, uint8_t DeviceType, uint8_t TransmissionType ); +uint32_t ANT_RxExtMesgsEnable( uint8_t Enable ); +uint32_t ANT_EnableLED( uint8_t Enable ); +uint32_t ANT_CrystalEnable( uint8_t Enable ); +uint32_t ANT_ConfigFrequencyAgility( uint8_t Channel, uint8_t Frequency1, uint8_t Frequency2, uint8_t Frequency3 ); +uint32_t ANT_SetProximitySearch( uint8_t Channel, uint8_t SearchThreshold ); +uint32_t ANT_SetUSBDescriptorString( uint8_t StringNum, uint8_t *pucDescString, uint8_t StringSize ); +uint32_t ANT_ResetSystem ( void ); +uint32_t ANT_OpenChannel(uint32_t Channel ); +uint32_t ANT_CloseChannel(uint32_t Channel ); +uint32_t ANT_RequestMessage(uint32_t Channel, uint8_t MessageID ); +uint32_t ANT_OpenRxScanMode( void ); +uint32_t ANT_SleepMessage( void ); +uint32_t ANT_SendBroadcastData(uint8_t Channel, uint8_t * BroadcastData ); +uint32_t ANT_SendBroadcastDataExt(uint8_t Channel, uint8_t *BroadcastData, uint16_t DeviceNumber, uint8_t DeviceType, uint8_t TransmissionType ); +uint32_t ANT_SendAcknowledgedData(uint8_t Channel, uint8_t *BroadcastData ); +uint32_t ANT_SendAcknowledgedDataExt( uint8_t Channel, uint8_t *BroadcastData, uint16_t DeviceNumber, uint8_t DeviceType, uint8_t TransmissionType ); +uint32_t ANT_SendBurstTransferPacket( uint8_t ChannelSeq, uint8_t *BurstData ); +uint32_t ANT_InitCWTestMode( void ); +uint32_t ANT_SetCWTestMode( uint8_t TransmitPower, uint8_t RFChannel ); +uint32_t ANT_SendExtBroadcastData( uint8_t Channel, uint16_t DeviceNum, uint8_t DeviceType, uint8_t TransmissionType, uint8_t *Data ); +uint32_t ANT_SendExtAcknowledgedData( uint8_t Channel, uint16_t DeviceNum, uint8_t DeviceType, uint8_t TransmissionType, uint8_t *Data ); +uint32_t ANT_SendExtBurstData( uint8_t Channel, uint16_t DeviceNum, uint8_t DeviceType, uint8_t TransmissionType, uint8_t *Data ); + + +uint32_t BC_ANT_build_packet( uint8_t *souce, uint32_t size, uint8_t *dist ); + +uint32_t BC_ANT_ResetSystem( void ); +uint32_t BC_ANT_OpenChannel( uint8_t Channel ); +uint32_t BC_ANT_CloseChannel( uint8_t Channel ); +uint32_t BC_ANT_RequestMessage( uint8_t Channel, uint8_t MessageID ); + +void attach(void(*fptr)(void), Serial::IrqType type = Serial::RxIrq); +uint32_t BC_ANT_RecvPacket(uint8_t *buffer, uint32_t size); + +private: + uint32_t BC_ANT_SendPacket(uint8_t *packet, uint32_t size); + +protected: + #define BC_ANT_BUFFSIZE 72 + uint8_t BC_ANT_buffer[BC_ANT_BUFFSIZE]; + uint8_t BC_ANT_raw_buffer[BC_ANT_BUFFSIZE]; + + Serial _serialPort; + DigitalIn _cts; + +}; + +} +#endif /* ANT_H_ */