AnsweredAssumed Answered

Problem to communicate with magnetic encoder SSI, in using SPI

Question asked by moreac.erwan on Apr 29, 2014
Latest reply on May 13, 2014 by Clive One
I am working on the AEAT-6600 for 2 weeks and i have problem to communicate with. Indeed, I did the right circuit for Normal mode in the datasheet at the link :    and i never programmed it, so he should communicate with SSI in slow mode with 10bits resolution as says in the Application Note. I use a Stm32l100RC-discovery board to communicate with. I saw on the application note that this SSI 3wire is compatible with SPI and only i have in output of the sensor is 0 or random values like 65535 and 32768 if i disable internally pull-down of my microchip pins.
I saw on the datasheet that the new data is available after a half clock cycle and not an entire cycle. My clock is high when idle as the datasheet wants and in Normal mode I only connects MISO to DO/DI of the sensor. The AEAT-6600 is in 3.3V operation mode. I know the sensor is working because the MAG-HI and MAG-LO pins work well but I don't know how to use STM32L100 SPI with SSI's sensor. Can you tell me what is wrong in my code?
Here is my code to configure and communicate with :

 * Peripheral          SPI 1
 * -----------------------------------------------------------------------
 * Device                    Wind Angle by AEAT660
 * SClk                         PA5
 * MISO                         PA6
 * MOSI                         PA7
 * SS1                         PC10
 * Clock                     32 MHz
 * -----------------------------------------------------------------------*/
// this function initializes the SPI1 peripheral
void init_SPI1(void){
     GPIO_InitTypeDef GPIO_InitStruct;
     SPI_InitTypeDef SPI_InitStruct;
     // enable clock for used IO pins
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
     /* configure pins used by SPI1
      * PA5 = SCK
      * PA6 = MISO
      * PA7 = MOSI
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5;
     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
     GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;
     GPIO_Init(GPIOA, &GPIO_InitStruct);
     // connect SPI1 pins to SPI alternate function
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
     GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
     // enable clock for used IO pins
     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
     /* Configure the chip select pin
        in this case we will use PC10 */
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
     GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
     GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
     GPIO_Init(GPIOC, &GPIO_InitStruct);
     GPIOC->BSRRL |= GPIO_Pin_10; // set PC10 high
     // enable peripheral clock
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
     /* configure SPI1 in Mode 0 
      * CPOL = 1 --> clock is high when idle
      * CPHA = 0 --> data is sampled at the first edge
     SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
     SPI_InitStruct.SPI_Mode = SPI_Mode_Master;     // transmit in master mode, NSS pin has to be always high
     SPI_InitStruct.SPI_DataSize = SPI_DataSize_16b; // one packet of data is 16 bits wide
     SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;        // clock is high when idle
     SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;      // data sampled at first edge
     SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set; // set the NSS management to internal and pull internal NSS high
     SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; // SPI frequency is APB2 frequency / 256 pour être <1MHz qui est la fmax du capteur
     SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
     SPI_Init(SPI1, &SPI_InitStruct); 
     SPI_Cmd(SPI1, ENABLE); // enable SPI1

 * Function               SPI1_send
 * -----------------------------------------------------------------------
 * @descr               Send 16bits via SPI 1
 * @param               data     
 * @retval          received data from SPI data register
 * -----------------------------------------------------------------------*/
/* This funtion is used to transmit and receive data 
 * with SPI1
 *                data --> data to be transmitted
 *                returns received value
uint16_t SPI1_send(uint16_t data){

     /* Check the parameters */
     SPI1->DR = data; // write data to be transmitted to the SPI data register
     while( !(SPI1->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete
     while( !(SPI1->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete
     while( SPI1->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore
     return SPI1->DR; // return received data from SPI data register

/* Private functions ---------------------------------------------------------*/

  * @brief  Main program.
  * @param  None
  * @retval None
int main(void)
     /**** Variables ****/
      uint16_t received_val = 0;
     /* Initialise la clock et les peripheriques ----------------------------*/

printf("_n\r ************ Mode NORMAL ************");
          GPIO_WriteBit(GPIOC, GPIO_Pin_10, Bit_SET); // set PC10 (NCS) High
          GPIO_WriteBit(GPIOC, GPIO_Pin_10, Bit_RESET); // set PC10 (NCS) Low
          printf("\n\r Val de PC10 : %d",GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_10));
          Delay(2000);  //2ms of waiting
          // transmit dummy byte and receive data
          received_val = SPI1_send(0x0000);
          GPIO_WriteBit(GPIOC, GPIO_Pin_10, Bit_SET); // set PC10 (NCS) High
          printf("\n\r La valeur du capteur est %d",received_val);
          Delay(2000000); //2 seconds of waiting
          GPIO_WriteBit(GPIOC, GPIO_Pin_10, Bit_RESET); // set PC10 (NCS) Low
          printf("\n\r Val de PC10 : %d",GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_10));
          received_val = SPI1_send(0x0000);
          printf("\n\r La valeur 2 du capteur est %d",received_val);
          GPIO_WriteBit(GPIOC, GPIO_Pin_10, Bit_RESET); // set PC10 (NCS) High