技術情報」カテゴリーアーカイブ

PSoC 42xx を使って MCP2515 の命令フレームを生成する

PSoC 42xx は SPI コンポーネントが提供されていて、クロックは 4MHz まで設定できます。しかしながら、これを使って可能な限り速く MCP2515 と通信することはそれほど単純ではありません。

問題は、MCP2515 の命令は複数のバイトでできていることです。一つの命令は CS 信号で束ねられていて、命令を送っている最中はずっと L (イネーブル)にしておかないといけません。しかしながら、PSoC の SPI は CS 信号を直接制御できません。コンポーネントとの通信は FIFO を通じて行われており、FIFO がデータを受け取ると、内部で自動的に CS をイネーブルに変え、データの送信が終わって FIFO が空になると自動的に CS をディスエイブルに戻します。自動生成される API 関数はあまり効率が良くなくて、SPI のクロック周波数が高いとスピードに追い付けず、送信の間が空いてしまいます。そのためクロック周波数が高いと API 関数を使って MCP2515 の命令フレームを作ることができません。

datasheet_read_instruction

この問題を解決するために、API 関数を使わずに SPI を制御する関数を書きました。方針は

  • より低レベルのインタフェースを使って SPI とやり取りを行う
  • いったん送信が始まったら可能な限り速く Tx FIFO にデータを送り込み、送信が途絶えないようにする
  • データを送るのと並行して可能な限り早く Rx FIFO からデータを取り出す

いくつか制限事項があります

  • SPI 設定の Rx および Tx バッファサイズは 4 でないといけない。これより大きいと、PSoC Creator がソフトウェアバッファを生成してしまいソースコードの管理が難しくなる
  • 取り出したデータの最初の2バイトはダミーで意味をなさない。実際のデータは3バイト目から始まる。
  • まだ実装していないがたぶんこの関数を実行中は割り込みを停止しておかないといけない

以下がソースコードです。SPI コンポーネント名は SPIM_CAN で種類は SPI マスタ (SCB を使わない) です。

#define CAN_CTL_READ 0x03

void mcp2515_read(uint8_t address, uint8_t data[], uint8_t length)
{
    /* initialization */
    uint8_t to_write = length;
    length += 2;

    /* flush rx buffer */
    while (SPIM_CAN_GetRxBufferSize())
        SPIM_CAN_ReadRxData();

    /* wait until Tx FIFO becomes empty */    
    while (0u == (SPIM_CAN_TX_STATUS_REG & SPIM_CAN_STS_TX_FIFO_EMPTY)) {}

    CY_SET_REG8(SPIM_CAN_TXDATA_PTR, CAN_CTL_READ); // push instruction
    CY_SET_REG8(SPIM_CAN_TXDATA_PTR, address);      // push address

    // loop until all bytes are retrieved
    while (length > 0) {
        // transmit 0 to receive a byte
        if (to_write > 0 && (SPIM_CAN_TX_STATUS_REG & SPIM_CAN_STS_TX_FIFO_NOT_FULL)) {
            CY_SET_REG8(SPIM_CAN_TXDATA_PTR, 0);
            --to_write;
        }
        // retrieve a byte if there is any in Rx FIFO
        if (SPIM_CAN_RX_STATUS_REG & SPIM_CAN_STS_RX_FIFO_NOT_EMPTY) {
            *data++ = CY_GET_REG8(SPIM_CAN_RXDATA_PTR);
            --length;
        }
    }
}

MCP2515 から 16 バイト取り出してみました。4MHz のクロックで想定通りに動いています。

read_instruction

read_instruction2

組み込み向け exp() 関数の実装方法

楽器系の作り物をマイクロプロセッサでするときには、指数関数が必要になる場合が頻繁にあります。汎用の実装は遅くてでかいので、組み込みには向きません。正確さが必要な場合は少ないですが、スピードと小ささは重要です。

組み込み向けの実装をいくつか探したので忘れないようにここにリンクをはっておきます。

http://www.convict.lu/Jeunes/ultimate_stuff/exp_ln_2.htm

http://www.quinapalus.com/efunc.html

今の所、過去に試作したエンベロープジェネレータの指数関数計算部分を流用していて、まだでかくて遅いですがとりあえず今やっているプロジェクトで使っている PSoC 4200 に収まってちゃんと走っているので困るまでこのままにしておくのではあります。

Memo: Maven

Installation

How to install Maven on Windows

Maven is a Java application, so we are just fine with expanding product zip package and setting environment variables M2_HOME and JAVA_HOME and set PATH.

Creating a Maven Project

Maven in 5 Minutes

Maven in 5 seconds… dothis:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Stub Synth Module for MOM Development

MOM (Master Of Modules) manages synthesizer modules to make them work as a single musical instrument.  The main responsibilities of MOM are

  • Control patching.
  • Control parameters.
  • Organize voices.
  • (possibly) Organize modules such as device ID management.

Designing and implementing MOM and synth module data model is yet another challenge out of building CAN network.  Basically, the inter-module communication depends on CAN physical layer, but it’s a pain to dragging such a dependency during the MOM development.  I’m sure the CAN network would be quite unstable at first and I don’t want to stop and troubleshoot CAN while I’m working on MOM.

So, in order to remove dependency on CAN network, I’ll build the initial version of MOM based on TCP/IP with dummy (stub) synth modules that are purely software oriented and talk TCP/IP.  Once the MOM design is fixed, I can replace the TCP/IP driver by CAN driver later.  It will not ruin the module data model.

MCP2515 CAN Controller

As I mentioned in the previous article, I’ll use MCP2515 CAN controller for inter-module communication (initially, at least) for the Analog3 Project.

Datasheet of this device is available at the product page.

MCP2515 consists of CAN protocol engine, data buffers,  controller, and SPI interface as illustrated in the picture below.

MCP2515_block_diagram

a3_networkYou need to attach a processor to make this device functional.  In early phase of the project, I’m thinking of using a Raspberry Pi as the Master Of Modules (mom) and Arduino’s for dummy synthesizer modules.  Arduino is not a realistic solution for Analog3 both in terms of cost and performance.  So, I’m thinking of implementing a common synthesizer module driver based on PSoC or CAN enabled AVR.  MCP2551 is CAN transceiver.

It may take a while to get used to MCP2515.  Here are several links that are useful for getting started:

MCP2515 Linux Device Driver (probably for Raspberry Pi)

Arduino Example Sketch

https://gist.github.com/rechargecar/4177820

AVR Example Code (C)

Changing the Strategy for Analog3

I’ve been trying for a year to make my own serial interface protocol to exchange data among synth modules.  Though it showed some progress, I kind of giving up this approach.  The problem is complexity of serial interface controller.  A controller for multi-master serial interface is complex.  Implementing using a generic device is more costly in many sense than I expected.  I started with Arduino.  This was the easiest approach but channel was too slow.  I could only achieve 50kbps.  Then I tried implementing it into AVR using assembler language.  It went 100kbps but it was about the limit.  The most serious problem with MPU was that the processing is always on single thread.  Some data processing has to be done in parallel during data reception, but it was pretty inefficient to run such concurrent tasks on 8-bit simple processor.  Overhead for such multi-tasking killed the speed.  Then, I moved onto PSoC.  It achieves bit rate 400kbps easily and it might go higher if it’s tuned well.  However, the logic to handle collision is so complex that I eventually am suffered from lack of resources in PSoC.

So at this point, it seems more practical to use a ready-made implementation of an existing protocol, which is CAN.

The reason I didn’t go for CAN at first was speed.  I wanted to use the interface even for synching oscillators.  I was not sure if 44bit-at-shortest message from at 1 Mbps is short enough for that usage.  (I’m not sure yet.  It’s about 22kHz rate if you keep repeating shortest CAN message at the highest rate.  That’s pretty close to audio frequency.  If an oscillator keeps sending such messages, the communication channel would be pretty much occupied.)  So probably I need to give up some usage of the common data bus.  I will go with following approach:

  • Use cheap CAN controller and transceiver: MCP2515 and MCP2551.
  • MCP2515 is controlled by SPI.  Most processors can use it.
  • I’ll use Raspberry Pi for the master synth controller module.
  • Synth modules can be based on PSoC or AVR.  These two are ones I am familiar with.
  • I’ll give up some features such as oscillator sync network.

Raspberry Pi Setup Memo

Search engine helps, but I’m so lazy that I don’t like to do the same search repeatedly.

Wireless LAN setup:

Use application “Wifi Configuration” from GUI menu.

Common WIFI dongle problem of falling of network:

Power management feature is enabled in default for device 8192cu.  Having following file resolves the issue:

pi@raspberrypi ~ $ cat /etc/modprobe.d/8192cu.conf
# prevent power down of wireless when idle
options 8192cu rtw_power_mgnt=0 rtw_enusbss=0

See https://github.com/xbianonpi/xbian/issues/217

CAP/CTRL swap:

See http://raspberrypi.stackexchange.com/questions/5333/how-to-map-caps-lock-key-to-something-useful

Set static address to wifi interface:

Edit /etc/network/interfaces.  If the setup for wlan looks like following,

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

modify as follows:

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet static
address your-address
netmask your-netmask
gateway your-gateway

 Install Inconsolata font:

http://www.raspberryconnect.com/raspbian-packages-list/item/64-raspbian-fonts

Set timezone:

% sudo dpkg-reconfigure tzdata

Raspberry Pi B+ Pinout

Picture in Pi4J Project:

Raspberry Pi I2C clock-stretching problem

The I2C slave that I’m developing has been failing intermittently.  I finally noticed this was a known bug in Raspberry Pi that mishandles clock stretching.

I2C slave may delay response by holding SCL low.  However, when the slave does it, the I2C master in Raspberry Pi gives very short clock for the first bit of the next byte.  The symptom can be seen as following picture.

10678616_689709434441211_7585444260259589843_n

More detail explanation can be read in this link:

http://www.advamation.com/knowhow/raspberrypi/rpi-i2c-bug.html

In order to workaround this, I set data rate in I2C slave module higher.  The communication rate is 400 kbps, but I set the data rate 1000 kbps.  It worked for me.  Here is the setup of I2C slave module.

I2Cslave

 

Eagle 5 と Eagle 6 を混在して使う (Windows)

Analog2.0 を含め、今までのプロジェクトは CadSoft Eagle を使って回路図・基板レイアウトの設計をしています。最新のバージョンは 6.5 あたりですが、ライセンスの関係でいまだに  ver. 5 を使っています。バージョン6での大きな変更点として、データファイルの形式がバイナリからXMLに変わったことがあります。データがテキストですと、バージョン管理ソフトとの相性がよく、今まで苦しんでいた履歴管理がぐっと楽になる可能性が高いです。ですが、古いプロジェクトは「データファイルを変更せずに」参照できないといけません。なので、Eagle 5 と Eagle 6 の混在環境を作ってみることにしました。今まで混在環境を長く維持できたことないんですが、どうなることやら

続きを読む

ノイズ源に使うトランジスタ

DSC00098_2ここしばらくノイズの音源に使うトランジスタについて、廃止品になってしまった 2SC3311 の後継として使うものを探していました。

色々と比較した結果、BC547 というトランジスタを使うことにしました。理由は以下のような感じ

  • これが一番大事、ノイズ音源として性格が良いこと
  • 発生するノイズレベルが2SC3311 に比較的近く、回路変更なくそのまま差し替えて使えそう
  • 一般的に広く使われているトランジスタで、セカンドソースもあり入手が比較的容易。
  • 廃止品になる気配が今のところない。

前回の記事までは未加工のノイズを聴いて選んでいましたが、それではあまり差がわからず、ノイズ用トランジスタなんてどれも一緒なんじゃないの、と感じましたが、ちょっと考え直して、レゾナンスのかかったVCFに通してみました。ノイズをレゾナンスのかかったフィルタで強調するのは、ノイズジェネレータの重要な使い方のひとつです。

これがトランジスタによってかなり挙動の違いが出ました。音の性格が違うので試聴試験による聴き比べで優劣を決めるのは難しく、実際にシンセサイザーを操作してみて、「気持ちよいポジションが探しやすいか」を基準に選びました。

音の良し悪しはやはり人の好みなので、絶対の正解はないと思います。他にも良いトランジスタがあるかもしれません。

BC547 のデモ音: