[sdiy] Beware Magic Numbers!`
rsdio at audiobanshee.com
rsdio at audiobanshee.com
Thu Dec 28 01:24:15 CET 2017
Sounds like SPI_MODE0 is one of those defines/equates from the provided headers/includes, but it’s worth mentioning anyway. I often build my own defines/equates based on the system ones so that I can quickly make changes where necessary.
I’ll second (third) the logic analyzer. I’ve noticed that some chips have behaviors associated with some SPI modes that aren’t in place for other SPI modes. If the device that you’re communicating with needs one mode but a different behavior, then things can get very unpredictable. An example is the behavior of SPI Select lines on Analog Devices Blackfin SHARC processors, where the Select is automatically updated in some modes but not others. Also, the Linux driver doesn’t even make full use of the hardware support in all setups, so you might find varying degrees of success depending upon the peripheral. In other words, it’s always good to be well-versed in both the hardware documentation and the system driver documentation - and many companies provide completely separate documentation for each realm, probably because you can potentially swap in a different software system with the same hardware.
Brian Willoughby
On Dec 27, 2017, at 4:13 PM, Tim Ressel <timr at circuitabbey.com> wrote:
> Agreed on all points, including the logic analyzer. You can't fix it if you can't see it.
>
> --tr
>
> On 12/27/2017 4:10 PM, Jay Schwichtenberg wrote:
>> Tim,
>>
>> The proper way of doing it is use defines (C) or EQUs (asm) in a header file
>> and not use numbers directly in the code.
>>
>> 1) Using a name for the number gives you a better idea of what the number is
>> used for.
>> 2) If the number is used multiple places in the code you change it in one
>> place and all the code is changed. This avoids the problem of changing the
>> code in the 3 places you remember its used and not changing it in the 4th
>> you forgot.
>>
>> Also having something like a cheap logic analyzer or scope that decodes
>> serial, I2C or SPI will save you a lot of time trying to track down bus
>> issues. I have an older saleae and that has saved my ass so many times when
>> doing embedded stuff.
>>
>> Happy coding.
>> Jay S.
>>
>>
>> -----Original Message-----
>> From: Tim Ressel
>>
>> Yo,
>>
>> I just spent days chasing down a bug with SPI and a flash memory. I
>> finally nailed it, and it was something dumb: a magic number. This when
>> you set a variable or register to a number without any annotation of
>> what that number is. In my case it was an Arduino call to set the SPI
>> clock mode. It was being set to '0' in the code I borrowed. My squishy
>> brain decided that must be SPI mode 0. Wrong-o. When I set it the the
>> hard coded constant 'SPI_MODE0' it started working.
>>
>> The moral of this story is: don't be a lazy schlub like me. Annotate all
>> your numbers so they are clear. A simple test: ask yourself, "If I look
>> at this code a year from now, will I know that this means?
More information about the Synth-diy
mailing list