teletypeguy
Member
Hi folks:
First-time post. New teensy user. Just picked up a 3.1 to play with. Pretty cool, and a lot of power for twenty bucks. I love that the teensy lets me easily fire off interrupts and has oodles of other great features cleanly integrated into the Arduino IDE.
I am generally off in the custom ARM/gcc world but want to make something open-source that lets folks tweak it easily (without needing to build a toolchain and get a jtag pod), so I am interested in the Arduino IDE interface to the teensy. Later, it would be cool to try some bare-metal teensy programming as well. As approachable as the standard arduino is for most folks, you need a mega/due... for any decent code space -- that was before I discovered the smaller/cheaper/more-powerful teensy. But enough preamble.
My first tests included the simple CardInfo SD example prog -- don't really need to post the code -- it's the generic arduino example from Limor, just pointing to a teensy 3.1 at a clock freq of X. The arduino IDE is 1.6.5, teensyduino is 1.24. The only change to the example is to fix the well-known bug where it screws up the size of cards over 2GB (github.com/arduino/Arduino/issues/1194). I hooked up a teensy 3.1 and an adafruit SD breakout on a breadboard and ran across the following:
Sheesh, that SPI bus is fast -- CardInfo would always init the card, since the spi bus is slowed way down for init, however, the volume init did not work. Hmm. I had the teesny set to 96MHz. I hooked up a scope and found spi clocks about 25 MHz. Sure enough, with a bit of poking through the arduino area, I found, just a few lines down, in \Program Files (x86)\Arduino\hardware\teensy\avr\libraries\SD\SD_t3.h
#define SD_SPI_SPEED SPISettings(25000000, MSBFIRST, SPI_MODE0)
which sets spi bus speed to a blazing 25MHz. Pretty fast for wires-in-air breadboarding and even for expansion pcbs/shields without a decent groundplane (which is many of them). Now my final pcb will be emi clean, but I had just patched this together on a breadboard. Perhaps I should have twisted each signal wire with a ground, but it was just a quick test. Much like most folks would do with a quick breadboard and expect to get examples running. However, I don't think it was an issue of crosstalk, or inductive undershoot, or whatever, but simply the spi bus speed. I was initially going to edit the SD_t3.h file to lower the spi clock, but I kinda hate changing libraries. I would rather define system stuff in my code.
So I started throttling back the cpu clock and found cases where CardInfo would work, and those when it would not:
- CardInfo volume fail: at 96MHz-OC: saw spi clock of about 25 MHz, delay between bytes of about 0.6 us.
- CardInfo volume pass: at 72MHz: saw spi clock of about 19 MHz, delay between bytes of about 1 us.
- CardInfo volume fail: at 48MHz: saw spi clock of about 25 MHz, delay between bytes of about 1 us.
- CardInfo volume pass: at 24MHz: saw spi clock of about 12 MHz, delay between bytes of about 2 us.
- These clocks were all "optimized"
This leads me to think that the spi rate of 25MHz is too fast for an SD card, but at about 19 MHz or so it begins to work. The delay between bytes may not be an issue, but I noted it since it differed. I realize that Paul has sped spi up on the teensy, much to the delight of folks driving big displays 'n such, but I would like to set the spi bus to something modest in the 4 to 10 MHz range, which has worked well for me for years for SD as well as other spi peripherals. As a simple fix, I just chose 24 MHz for my cpu clock.
I tried this with several different SD cards with differing capacities and different vendors -- same result.
Which leads me to a few questions:
- can I define teensy cpu clock in my program, or can it only be changed via the ide tools menu?
- can I define my spi clock (or divider) in my program, or is it just a function of the hard-coded SD_t3.h and cpu clock?
- what is an "optimized" cpu clock vs. a regular-joe clock of the same speed?
- when does arduino ide know to look for a lib (SD, SPI...) in \Program Files (x86)\Arduino\hardware\teensy\avr\libraries\... as opposed to the regular lib directory?
thx, Gil
First-time post. New teensy user. Just picked up a 3.1 to play with. Pretty cool, and a lot of power for twenty bucks. I love that the teensy lets me easily fire off interrupts and has oodles of other great features cleanly integrated into the Arduino IDE.
I am generally off in the custom ARM/gcc world but want to make something open-source that lets folks tweak it easily (without needing to build a toolchain and get a jtag pod), so I am interested in the Arduino IDE interface to the teensy. Later, it would be cool to try some bare-metal teensy programming as well. As approachable as the standard arduino is for most folks, you need a mega/due... for any decent code space -- that was before I discovered the smaller/cheaper/more-powerful teensy. But enough preamble.
My first tests included the simple CardInfo SD example prog -- don't really need to post the code -- it's the generic arduino example from Limor, just pointing to a teensy 3.1 at a clock freq of X. The arduino IDE is 1.6.5, teensyduino is 1.24. The only change to the example is to fix the well-known bug where it screws up the size of cards over 2GB (github.com/arduino/Arduino/issues/1194). I hooked up a teensy 3.1 and an adafruit SD breakout on a breadboard and ran across the following:
Sheesh, that SPI bus is fast -- CardInfo would always init the card, since the spi bus is slowed way down for init, however, the volume init did not work. Hmm. I had the teesny set to 96MHz. I hooked up a scope and found spi clocks about 25 MHz. Sure enough, with a bit of poking through the arduino area, I found, just a few lines down, in \Program Files (x86)\Arduino\hardware\teensy\avr\libraries\SD\SD_t3.h
#define SD_SPI_SPEED SPISettings(25000000, MSBFIRST, SPI_MODE0)
which sets spi bus speed to a blazing 25MHz. Pretty fast for wires-in-air breadboarding and even for expansion pcbs/shields without a decent groundplane (which is many of them). Now my final pcb will be emi clean, but I had just patched this together on a breadboard. Perhaps I should have twisted each signal wire with a ground, but it was just a quick test. Much like most folks would do with a quick breadboard and expect to get examples running. However, I don't think it was an issue of crosstalk, or inductive undershoot, or whatever, but simply the spi bus speed. I was initially going to edit the SD_t3.h file to lower the spi clock, but I kinda hate changing libraries. I would rather define system stuff in my code.
So I started throttling back the cpu clock and found cases where CardInfo would work, and those when it would not:
- CardInfo volume fail: at 96MHz-OC: saw spi clock of about 25 MHz, delay between bytes of about 0.6 us.
- CardInfo volume pass: at 72MHz: saw spi clock of about 19 MHz, delay between bytes of about 1 us.
- CardInfo volume fail: at 48MHz: saw spi clock of about 25 MHz, delay between bytes of about 1 us.
- CardInfo volume pass: at 24MHz: saw spi clock of about 12 MHz, delay between bytes of about 2 us.
- These clocks were all "optimized"
This leads me to think that the spi rate of 25MHz is too fast for an SD card, but at about 19 MHz or so it begins to work. The delay between bytes may not be an issue, but I noted it since it differed. I realize that Paul has sped spi up on the teensy, much to the delight of folks driving big displays 'n such, but I would like to set the spi bus to something modest in the 4 to 10 MHz range, which has worked well for me for years for SD as well as other spi peripherals. As a simple fix, I just chose 24 MHz for my cpu clock.
I tried this with several different SD cards with differing capacities and different vendors -- same result.
Which leads me to a few questions:
- can I define teensy cpu clock in my program, or can it only be changed via the ide tools menu?
- can I define my spi clock (or divider) in my program, or is it just a function of the hard-coded SD_t3.h and cpu clock?
- what is an "optimized" cpu clock vs. a regular-joe clock of the same speed?
- when does arduino ide know to look for a lib (SD, SPI...) in \Program Files (x86)\Arduino\hardware\teensy\avr\libraries\... as opposed to the regular lib directory?
thx, Gil