8 Replies Latest reply on Apr 1, 2016 7:52 AM by user_1377889

    SPI Master




      I am having problems getting data out of a ADXL362 accelerometer.  Here's the datasheet of the ADXL362:




      I am using CYBLE-01400-800 running on CY8CKIT-042-BLE. Yet I am not dealing with BLE. I am just trying to get the SPI communication between MCU and ADXL362 working. Also, I am using Miniprog3 to program and debug (since on my final prototype, I can only connect Miniprog3). It delivers 2.5 V to the system (which should be fine for both, PSoC MCU and ADXL362)


      I am using the UDB Version of SPI Master. I read lots of forum posts about PSoC SPI Master and its API's. Most of them included answers from Bob Marlowe and hli (both Cypress employees?). In one forum entrance they mention that it's better to manually control the Slave Select of the SPI. I did this (using a control register) --> using my logic analyzer, I found that the SS line is not going high or low at all (even using the control register). I attached a picture of the logic analyzer output as well. So I guess that's the reason why my ADXL362 does not return any data. However, it might also be that the way I use the SPI Master API is still faulty. Anyway, I attached my project. Please have a look at it and tell me if you see where I am going wrong.



        • 1. Re: SPI Master

          Bob and /me are not Cypress employees. We just happen to have quite some experience with PSoCs, and probably too much time at our hands :)


          Regarding your problem: did you try to trigger at slave select? When you control it manually, there will be a noticeable delay between changing its state and the actual SPI transmission. (apart from checking twice that you actually look at the correct pin...) You can change the default state of the control register output to 1, then you can know that your code handles it correctly.

          • 2. Re: SPI Master

            I can see several small bugs in your code which will prevent the SPI interface from working.


            uint8 SPIreadOneRegister(uint8 regAddress)
                Control_Write(0); //SS low --> Communication between Master and Slave starts
                SPIM_WriteTxData(0x00); /* The master sends a dummy byte and the slave sends back the value that's in the "regAddress" register.*/


            // At this point you have got 3 bytes in the receive buffer, you need the last byte only


                while(!(SPIM_ReadTxStatus() & SPIM_STS_SPI_DONE)); /*wait until SPI status = done (SPIM_STS_SPI_DONE flag raised). */
                Control_Write(1);    // should be past the following wait
                while(!(SPIM_GetRxBufferSize())); /*returns 0 = FIFO empty or 1 = FIFO not empty. */
                uint8 rtData = SPIM_ReadRxData();  /*read: Returns the next byte of received data available in the receive buffer. */
                return rtData;
                SPIM_ClearRxBuffer(); //This line will never be executed, leaving bytes in receiver


            SPIM_ClearRxBuffer(); misplaced in function transfer(uint8 data)


            AccelData readXYZTData() forgot to clear receiver.


            transfer(uint8 data) forgot to clear reveiver


            The control register works, but less resources will be used when writing to the pin directly. Configure the pin for no Hardware connection and set the output to initially high to avoid glitches after power-up. Use SS_Write(0) to activate the SS-Line. If in doubt whether the line works correctly, toggle it a few times during initialization and watch with logic analyzer.





            1 of 1 people found this helpful
            • 3. Re: SPI Master

              Hi hli and Bob


              Thank you so much. From your comments I was able to get it running. Also, from this document: http://www.cypress.com/file/157791/download --> p.73 note 19 --> I realized that P0[1] from CYBLE-014008-EVAL routes to P2[0] on the CY8CKIT- 042-BLE. Since I  connected the SS of my sensor to P0[1] it didn't work. As soon as I connected SS to P2[0] and changed the things you proposed in Firmware, it worked...





              • 4. Re: SPI Master






                • 5. Re: SPI Master

                  Dear Bob and hli


                  So as mentioned my code works. However, every now and then (like every 1000th measurement or so) I get short spikes on my signal. The spikes appear randomly on all 4 signals the accelerometer provides. I don't have a clue where those spikes come from. Do you?


                  I attached a picture of the 4 signals recorded in UC/Probe (Micrium) and also attached the code I am currently using to read my accelerometer.


                  best regards



                  • 6. Re: SPI Master

                    I changed the buffer size of the SPIM and modified the read-out. UNTESTED


                    Give it a try.





                    • 7. Re: SPI Master

                      You're help is fantastic! I got rid of the spikes, nice smooth signal. Everything working perfectly fine now :-)


                      I understand how you modified the firmware. I kind of understand why you made the RX/TX buffers 15 bytes each. However, I probably would have made it 8 bytes (or maybe I would have tried with 16 bytes) --> then it would probably not have worked. Can you give a short summary of why you made them 15 bytes?


                      Thanks and best regards



                      • 8. Re: SPI Master

                        ;-) Just a generous value greater than your required 6 bytes. Aamof the overhead of the buffer sizes is not more tha just some ram bytes, I could have used 50 bytes unless you are short of sram.