Shop OBEX P1 Docs P2 Docs Learn Events
SPI help — Parallax Forums

SPI help

TCTC Posts: 1,019
edited 2016-08-26 00:21 in Propeller 1
Hello all,

I am working on a project for a friend. He is making an art project that has 1000 cast concrete faces. He wants to have it when someone places there hand on a face, an audio file of that person will play. We are going to use Microchip's CAP1188 for detecting proximity oh a hand. The chip can communicate with SPI, I2C/SMBUS. I am going with SPI. That way all the chips can share the same "MOSI", "MISO", and "CLK" lines. And all I have to do is select the correct "CS" line.

But I am having trouble figuring out what I am doing wrong. I am just trying to read the Product ID ($50) to see if my code currently works. And it doesn't...

Could someone please tell me where I am going wrong? Please excuse the messy code, I have been trying to get this to work for 2 days now, and I have gotten to the point where I don't care about how clean it is.

Thanks
TC
CON
        _clkmode = xtal1 + pll16x                                               'Standard clock mode * crystal frequency = 80 MHz
        _xinfreq = 5_000_000

  CLK_FREQ = ((_clkmode - xtal1) >> 6) * _xinfreq
  MS_001   = CLK_FREQ / 1_000
  US_001   = CLK_FREQ / 1_000_000

      
CON

  {*** PINS ***}

  {*** Audio output ***}

  Audio_Left    = 0
  Audio_Right   = 1

  {*** SD card ***}

  SD_CS         = 16
  SD_MOSI       = 17
  SD_CLK        = 18
  SD_MISO       = 19

  {*** CAP1188 I2C ***}

  Cap_SDA       = 20
  Cap_SCL       = 22

  {*** CAP1188 SPI ***}

  Cap_MISO      = 20
  Cap_MOSI      = 21
  Cap_CLK       = 22
  Cap_CS        = 23

  {*** CAP1188 MISC ***}

  Cap_IRQ       = 24
  Cap_reset     = 25

  {*** Prop ***}

  SCL           = 28
  SDA           = 29

  TX            = 30
  RX            = 31

CON

  {*** CAP1188 I2S address ***}
k_82            = %0101_1000
k_100           = %0101_0110
k_120           = %0101_0100
k_150           = %0101_0010
VDD             = %0101_0000

  {*** CAP1188 registers ***}

Main_Control            = $00
General                 = $02
Sensor_input            = $03
LED_Status              = $04
Noise_flag              = $0A
Input_1_Delta           = $10
Input_2_Delta           = $11
Input_3_Delta           = $12
Input_4_Delta           = $13
Input_5_Delta           = $14
Input_6_Delta           = $15
Input_7_Delta           = $16
Input_8_Delta           = $17
Sens_Control            = $1F
Config                  = $20
Input_Enable            = $21
Input_Config_1          = $22
Input_Config_2          = $23
Ave_Samp_Config         = $24
Cal_Activate            = $26
Int_Enable              = $27
Repeat_Enable           = $28
Mulit_touch_Config      = $2A
Multi_touch_Pattern_config  = $2B
Mulit_touch_Pattern         = $2D
Recalibration_Config        = $2F
Input_1_Threshold           = $30
Input_2_Threshold           = $31
Input_3_Threshold           = $32
Input_4_Threshold           = $33
Input_5_Threshold           = $34
Input_6_Threshold           = $35
Input_7_Threshold           = $36
Input_8_Threshold           = $37
Input_Noise_Threshold       = $38

{***  Standby Config Registers  ***}

Standby_Channel             = $40
Standby_Config              = $41
Standby_Sensitivity         = $42
Standby_Threshold           = $43
Config_2                    = $44

{***  Base Count Registers  ***}

Input_1_Count               = $50
Input_2_Count               = $51
Input_3_Count               = $52
Input_4_Count               = $53
Input_5_Count               = $54
Input_6_Count               = $55
Input_7_Count               = $56
Input_8_Count               = $57

{***  LED Controls  ***}

LED_Output_Type             = $71
Input_LED_Linking           = $72
LED_Polarity                = $73
LED_Output_Control          = $74
LED_Linked_Trans_Control    = $77
LED_Mirror_Control          = $79
LED_Behavior_1              = $81
LED_Behavior_2              = $82
LED_Pulse_1_Period          = $84
LED_Pulse_2_Period          = $85
LED_Breathe_Period          = $86
LED_Config                  = $88
LED_Pulse_1_Duty_Cycle      = $90
LED_Pulse_2_Duty_Cycle      = $91
LED_Breathe_Duty_Cycle      = $92
LED_Direct_Duty_Cycle       = $93
LED_Direct_Ramp_Rates       = $94
LED_Off_Delay               = $95

{***  Sensor Input Calibration  ***}

Sensor_1_cal                = $B1
Sensor_2_cal                = $B2
Sensor_3_cal                = $B3
Sensor_4_cal                = $B4
Sensor_5_cal                = $B5
Sensor_6_cal                = $B6
Sensor_7_cal                = $B7
Sensor_8_cal                = $B8
Sensor_cal_LSB_1            = $B9
Sensor_cal_LSB_2            = $BA

{***  Identifacation  ***}

Product_ID                  = $FD
Manufacturer_ID             = $FE
Revision                    = $FF

VAR

  byte  temp
  
OBJ

  spi   :       "SPI_Asm"
  com   :       "FullDuplexSerial.spin"

PUB init

  pause(1000)

  'serial com setup
  com.Start(rx, tx, 0, 115200)

  'SPI setup
  SPI.start(100,0)

  com.str(string("Setup done", 13))

  pause(500)

  com.str(string("Read Product ID", 13))

  reset_bus


  CS_enable(Cap_CS)
  temp := rd_byte(Product_ID)
  CS_disable(Cap_CS)

  com.hex(temp,2)

  com.str(string(13, 13, "Done"))


  repeat
PUB rd_byte(addr) | value

  set_address(addr)
  
  SPI.SHIFTOUT(Cap_MOSI, Cap_CLK, SPI#LSBFIRST, 8, $7F)
  value := SPI.SHIFTIN(Cap_MISO, Cap_CLK, SPI#LSBPRE, 8)
  
  result := value

PUB wr_byte(addr, value)

  set_address(addr)
  
  SPI.SHIFTOUT(Cap_MOSI, Cap_CLK, SPI#LSBFIRST, 8, $7E)
  SPI.SHIFTOUT(Cap_MOSI, Cap_CLK, SPI#LSBFIRST, 8, value)
  
  
PUB set_address(addr)

  SPI.SHIFTOUT(Cap_MOSI, Cap_CLK, SPI#LSBFIRST, 8, $7D)
  SPI.SHIFTOUT(Cap_MOSI, Cap_CLK, SPI#LSBFIRST, 8, addr)

PUB CS_enable(device)

  dira[device] := 1
  outa[device] := 0

PUB CS_disable(device)

  dira[device] := 0
  outa[device] := 0

PUB reset_bus

  CS_enable(Cap_CS)
  
  SPI.SHIFTOUT(Cap_MOSI, Cap_CLK, SPI#LSBFIRST, 8, $7A)
  SPI.SHIFTOUT(Cap_MOSI, Cap_CLK, SPI#LSBFIRST, 8, $7A)
  
  CS_disable(Cap_CS)
  
pub pause(ms) | t

'' Delay program in milliseconds

  if (ms < 1)                                                   ' delay must be > 0
    return
  else
    t := cnt - 1792                                             ' sync with system counter
    repeat ms                                                   ' run delay
      waitcnt(t += MS_001)

Comments

  • Do you have a pull-up resistor on CS?
  • TCTC Posts: 1,019
    Yes I do. I am using Adafruit's breakout board.

    I slowed down the code so I could see what is going on (using a quickstart board). The CS line goes from HIGH to LOW when it should. The CAP1188 is not sending anything out on the MISO line. That tells me, it is not happy about something. But I can't figure out why.
  • Do you have a logic analyzer? My technique when I can't get a "sensor" to talk is grab my Arduino UNO, grab a sample sketch usually vendor provided. Capture the timing of the Arduino code and compare it to what I think I'm sending with the prop. This has proved very successful for me when a chip just refuses to talk. Of course many times I have found a loose wire or MISO X MOSI mismatch ...

    I use Tachyon Forth on the prop but the language doesn't matter, two days work is worth the investment in a simple logic analyser like THIS if you don't have one. I've gotten the worst documented, kinda SPI-ish and I2C-ish sensors to talk this way.
  • I find the Digiview DV3100 really good value for money and since it captures edges it isn't dependent on a sample rate setting, just capture and you get full detail, every glitch etc, and the software is a breeze to use.

    On the cheap you can use a Propeller loaded up with Tachyon's SPLAT logic analyzer as long as the SPI isn't too fast.
  • A quick look of the CAP1188 datasheet indicates that it expects MSB first.
  • TCTC Posts: 1,019
    I do not have a logic analyzer, but I will use another prop as one. Either SPLAT, or propalyzer.

    I will have to try MSBFIRST again. I thought I did that at one point, but I can't remember for sure.

    Sorry about the vaug responses, I'm at work on my break, and I don't have a lot of time to make this sound correct.

    I will try everything when I get home tonight.
  • TCTC Posts: 1,019
    Thank you so much everyone. I loaded Propalyzer into another Quickstar board, and found out a couple things (things everyone said)

    1) I needed MSB, not LSB
    2) I had my pins wrong. I was counting the quickstart pins wrong.

    Because of the Propalyzer I could see things did not look right. This is why I like asking on here, it offers another set of eyes, minds, and experiences that I just dont have access to anywhere else.

    Thank you again for everything.

    TC
  • Here is a SPI code snippet that may help you. It is for a MPC3208 (ADC chip), but the code is pretty much the same for all SPI type chips
Sign In or Register to comment.