Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[ARM] Interfacing STM32F103ZE eval board with TDC GP22

Status
Not open for further replies.

Ayan

Newbie level 1
Newbie level 1
Joined
Jun 5, 2014
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
9
Hello. I am trying to interface GP22 with stm32f103ze microcontroller.
I am unable to figure out if proper communication has been established between GP22 and uC.
All the necessary components have been added to the GP22 IC following the datasheet.
Also all the connections have been made between the GP22 and the Micro controller.
I am a bit confused regarding the connection of pin 13(RSTN)and PIN 9 (SSN) of GP22.
I have tried with grounding both the pins. I have also tried with connecting them to PA4 and PD3 respectively.
I code that I have used to test the communication is pasted below. But the result is wrong (FF). Please help to troubleshoot the problems. Thanks.


Code:
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stdio.h"
#include "stdbool.h"
#include "GLCD.h"
#include "math.h"


/* Private variables ---------------------------------------------------------*/
#define LOOP_DLY_100US    2000
#define LOOP_DLY_250ns    2
#define LOOP_DLY_1ms      3299

u32       CriticalSecCntr;
bool      configured_true=false;

void* Bus_Type = SPI1;
float result;

///* Opcodes -------------------------------------------------------------------*/
uint8_t   Init =                0x70;
uint8_t   Power_On_Reset =      0x50;

uint32_t  Dummy_var = 0;

//// For mathematical calculations
int       i,n;
int       j;
char text[10]="";

///* Device functions ----------------------------------------------------------*/
void      gp22_send_1byte(void *bus_type, uint8_t gp22_opcode_byte);
void      gp22_wr_config_reg(void *bus_type, uint8_t opcode_address,
                             uint32_t config_reg_data);
float     gp22_read_n_bytes(void *bus_type, uint8_t n, uint8_t read_opcode,
                            uint8_t read_addr, uint8_t fractional_bits);

///* Bus functions -------------------------------------------------------------*/
void      SPIx_GPIOs_Init(void* bus_type);
void      SPIx_Interface_Init(void* bus_type);

///* Private functions ---------------------------------------------------------*/
void      Dly100us(void *arg);
void      Dly250ns(void *arg);
void      Dly1ms(void *arg);
void      Simple_delay_750ns(void *arg);
void      Ext_Interrupt_Init (void);


int main(void)
{

//  /* Setup STM32 system (clock, PLL and Flash configuration) */
  SystemInit();

//  /* controlled loop */
  while (Dummy_var!=11) // To control the loop, e.g. (Dummy_var!=7)
  {
    if (Dummy_var==10) Dummy_var=0; // Infinite loop

    if(configured_true==false)
    {
      configured_true = true;
      SPIx_GPIOs_Init(Bus_Type);
      SPIx_Interface_Init(Bus_Type);

      gp22_send_1byte(Bus_Type, Power_On_Reset);
      Dly100us((void*)5);              // 500 us wait for GP22

      // Writing to the configuration registers (CR)
      
      // CR1: EN_FAST_INIT = 1, HITIN2 = 1, HITIN1 = 1, ...
      gp22_wr_config_reg(Bus_Type, 0x81, 0x19C90000);
      result = gp22_read_n_bytes(Bus_Type,1,0xB5,0x81,0);
      
      sprintf(text, "%f ", result);
	GLCD_Initialize();
			GLCD_SetBackColor(White);
		  GLCD_SetTextColor(Red);
		  GLCD_DisplayString(1, 0, 1, "result: ");
			GLCD_DisplayString(1, 13, 1, text);
        }
    }
    Dummy_var++; // To Control the loop
} //End main

/*******************************************************************************
 * Function Name: gp22_send_1byte
 * Parameters: Opcode byte
 *
 * Return: none
 *
 * Description: Writes the Opcode to GP21
 *
 ******************************************************************************/
void gp22_send_1byte (void *bus_type, uint8_t gp22_opcode_byte)
{
     // Deactivating Reset SPIx
     if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET);
     if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);

     while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==0) {}
     SPI_I2S_SendData(bus_type, gp22_opcode_byte);     // OPCODE TO Device
     while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==0) {}
  Simple_delay_750ns((void*)10); // important delay (16) at SPI freq.=750kHz

     // Reset to device SPIx
     if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET);
     if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
}

///*******************************************************************************
// * Function Name: gp22_wr_config_reg
// * Parameters: Address byte, 4 bytes of Configuration
// *
// * Return: none
// *
// * Description: Writes the config.reg. specified in GP21 with the data
// *
// ******************************************************************************/
void gp22_wr_config_reg (void *bus_type, uint8_t opcode_address,
                         uint32_t config_reg_data)
{
   uint8_t Data_Byte_Lo    = config_reg_data;
   uint8_t Data_Byte_Mid1  = config_reg_data>>8;
   uint8_t Data_Byte_Mid2  = config_reg_data>>16;
   uint8_t Data_Byte_Hi    = config_reg_data>>24;

   uint32_t common_delay    = 10; // important delay (16) at SPI freq.=750kHz

      // Deactivating Reset SPIx
      if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET);
      if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);

      while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==0) {}

      SPI_I2S_SendData(bus_type, opcode_address);  // RAM WR OPCODE+ADDRESS
  Simple_delay_750ns((void*)common_delay);

      while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==0) {}
      SPI_I2S_SendData(bus_type, Data_Byte_Hi);  // DATA BYTE HIGH
  Simple_delay_750ns((void*)common_delay);

      while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==0) {}
       SPI_I2S_SendData(bus_type, Data_Byte_Mid2);  // DATA MID - 2
  Simple_delay_750ns((void*)common_delay);

      while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==0) {}
       SPI_I2S_SendData(bus_type, Data_Byte_Mid1);  // DATA MID - 1
  Simple_delay_750ns((void*)common_delay);

      while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==0) {}
       SPI_I2S_SendData(bus_type, Data_Byte_Lo);  // DATA LOW
  Simple_delay_750ns((void*)common_delay);

      while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==0) {}
  Simple_delay_750ns((void*)common_delay);

     // Reset to device SPIx
     if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET);
     if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
}

///*******************************************************************************
// * Function Name: gp22_read_n_bytes
// * Parameters: bus_type = (SPI1, SPI2)
// *             n_bytes = how many bytes should be read
// *             read_opcode = read opcode of the device
// *             read_addr = read address of the device
// *             fractional_bits = number of fractional bits of read data
// *
// * Return: n bytes from the specified read address
// *
// * Description: Reads n bytes from an address in GP21
// *
// ******************************************************************************/
float gp22_read_n_bytes(void *bus_type, uint8_t n_bytes, uint8_t read_opcode,
                   uint8_t read_addr, uint8_t fractional_bits)
{
  uint32_t    Result_read = 0;
  float       Result = 0;
  uint8_t     read_opcode_addr = read_opcode | read_addr;

  //.............. Result = n Byte = n x 8 bits......................
  if (bus_type==SPI1 | bus_type==SPI2)
  {
       // Deactivating Reset SPIx
       if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET);
       if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);

       SPI_I2S_SendData(bus_type, read_opcode_addr);  // READ OPCODE + Address

       while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE)==RESET) {};
  //Simple_delay_750ns((void*)10); // important delay (16) at SPI freq.=750kHz

       //Compulsory reads to DR and SR to clear OVR,
       //so that next incoming data is saved
       SPI_I2S_ReceiveData(bus_type);                     // To clear OVR
       SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_TXE); // To clear OVR

         //Reading byte1
         SPI_I2S_SendData(bus_type, 0x00FF);  // DUMMY WRITE
         // Wait until RX buffer is not empty, then read the received data
         while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_RXNE)==0) {}
         Result_read = SPI_I2S_ReceiveData(bus_type); //  Read

       for ( n = 1; n < n_bytes; n++)
       {
         //Reading byte2 .. byte.n
         SPI_I2S_SendData(bus_type, 0x00FF);  // DUMMY WRITE
         // Wait until RX buffer is not empty, then read the received data
         while (SPI_I2S_GetFlagStatus(bus_type, SPI_I2S_FLAG_RXNE)==0) {}

         Result_read = Result_read<<8;
         Result_read |= SPI_I2S_ReceiveData(bus_type); //  Read
       }

       // Reset to device SPIx
       if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET);
       if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
  }

  Result = Result_read / pow(2, fractional_bits);

  return Result;
}

///*******************************************************************************
// * Function Name: SPIx_GPIOs_Init
// * Parameters: Int32U Clk, Int32U Width
// * Return: none
// *
// * Description: Init GPIOs used in SPIx interface
// *
// ******************************************************************************/
void SPIx_GPIOs_Init(void* bus_type)
{
GPIO_InitTypeDef GPIO_InitStructure; // GPIO_InitTypeDef defined in library

  // Enable GPIO clock and release reset
  RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
                         RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
                         RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO,  DISABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
                         RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
                         RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO,  ENABLE);

  // Configure   SPI1_CLK  - PA5
  //             SPI1_MOSI - PA7
  //             SPI1_MISO - PA6
  // Chip select SPI1_NSS  - PA4
  // External Interrupt Input line PD4

  // SPI1_NSS
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  if (bus_type==SPI1) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
                        GPIO_Init(GPIOA, &GPIO_InitStructure);
                      } // SPI1 - PA4
  if (bus_type==SPI2) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
                        GPIO_Init(GPIOB, &GPIO_InitStructure);
                      } // SPI2 - PB12

//  SPI1_CLK
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  if (bus_type==SPI1) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
                        GPIO_Init(GPIOA, &GPIO_InitStructure);
                      } // SPI1 - PA5
  if (bus_type==SPI2) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
                        GPIO_Init(GPIOB, &GPIO_InitStructure);
                      } // SPI2 - PB13

//   SPI1_MISO
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  if (bus_type==SPI1) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
                        GPIO_Init(GPIOA, &GPIO_InitStructure);
                      } // SPI1 - PA6
  if (bus_type==SPI2) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
                        GPIO_Init(GPIOB, &GPIO_InitStructure);
                      } // SPI2 - PB14

//   SPI1_MOSI
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  if (bus_type==SPI1) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
                        GPIO_Init(GPIOA, &GPIO_InitStructure);
                      } // SPI1 - PA7
  if (bus_type==SPI2) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
                        GPIO_Init(GPIOB, &GPIO_InitStructure);
                      } // SPI1 - PB15

  SPI_I2S_DeInit(bus_type);

// External Interrupt Input
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  if (bus_type==SPI1) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
                        GPIO_Init(GPIOD, &GPIO_InitStructure);
                      } // SPI1 - PD4
  if (bus_type==SPI2) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
                        GPIO_Init(GPIOE, &GPIO_InitStructure);
                      } // SPI2 - PE11

// SPI ENABLE Output for the evaluation kit
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  if (bus_type==SPI1) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
                        GPIO_Init(GPIOD, &GPIO_InitStructure);
                      } // SPI1 - PD3
  if (bus_type==SPI2) { GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
                        GPIO_Init(GPIOE, &GPIO_InitStructure);
                      } // SPI2 - PE10

  // SPIx Enable = RSN for GP22
  if (bus_type==SPI1) GPIO_WriteBit(GPIOD, GPIO_Pin_3, Bit_SET); // SPI1 - PD3
  if (bus_type==SPI2) GPIO_WriteBit(GPIOE, GPIO_Pin_10, Bit_SET); // SPI2 - PE10
}

///*******************************************************************************
// * Function Name: SPIx_Interface_Init
// * Description: Init SPI1 or SPI2 Interface
// *
// ******************************************************************************/
void SPIx_Interface_Init(void* bus_type)
{
  // Initialising the SPIx interface
  SPI_InitTypeDef SPI_InitStructure;

    /* Configures the system clock (SYSCLK) */
  //RCC_SYSCLKConfig (RCC_SYSCLKSource_HSI); // Source-freq. 8.000MHz
//  RCC_SYSCLKConfig (RCC_SYSCLKSource_HSE); // Source-freq. 20.000MHz
  RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK); // Source-freq. 57.6MHz ( (72MHz/25MHz) * HSE) )

    /* Adjusts the Internal High Speed oscillator (HSI) calibration value.
    * @param  HSICalibrationValue: specifies the calibration trimming value.
    *   This parameter must be a number between 0 and 0x1F. */
  //RCC_AdjustHSICalibrationValue(0x10); //0x00..0x0F // 3.8..4.2MHZ

    /* Configures the AHB clock (HCLK) */
  RCC_HCLKConfig (RCC_SYSCLK_Div1);


  // Clock Enable and Reset release
  if (bus_type==SPI1)
  {
    RCC_APB2PeriphResetCmd (RCC_APB2Periph_SPI1, DISABLE);
    RCC_APB2PeriphClockCmd (RCC_APB2Periph_SPI1, ENABLE);
  }

  if (bus_type==SPI2)
  {
    RCC_APB1PeriphResetCmd (RCC_APB1Periph_SPI2, DISABLE);
    RCC_APB1PeriphClockCmd (RCC_APB1Periph_SPI2, ENABLE);
    RCC_PCLK1Config (RCC_HCLK_Div1); // in order to adapt the clock frequenz
  }

  // All are defined in stm32f10x_spi.h
  SPI_InitStructure.SPI_Direction         = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode              = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize          = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL              = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA              = SPI_CPHA_2Edge;
  SPI_InitStructure.SPI_NSS               = SPI_NSS_Soft;
  // SPI frequence devider
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  SPI_InitStructure.SPI_FirstBit          = SPI_FirstBit_MSB;

  SPI_Init(bus_type, &SPI_InitStructure);
  SPI_Cmd(bus_type, ENABLE); // Enabling the SPIx Interface

  // Enabling the NSS Output during transmission
  SPI_SSOutputCmd (bus_type, ENABLE);
  // SPIx - SSN to Device - Set to High for reset
  if (bus_type==SPI1) GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET);
  if (bus_type==SPI2) GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
}

///*******************************************************************************
// * Function Name: Dly100us, Dly250ns, Dly1ms, Simple_delay_750ns
// * Description: Delay Dly * (100us, 250ns, 1ms, 750ns)
// *
// ******************************************************************************/
void Dly100us(void *arg)                        // Gives 100us delay with arg 1
{
u32 Dely = (u32)arg;
 while(Dely--)
 {
   for( i = LOOP_DLY_100US; i; i--);
 }
}

void Dly250ns(void *arg)                        // Gives 250ns delay with arg 1
{
u32 Dely = (u32)arg;
 while(Dely--)
 {
   for( i = LOOP_DLY_250ns; i; i--);
 }
}

void Dly1ms(void *arg)                            // Gives 1ms delay with arg 1
{
  u32 Dely = (u32)arg;
 while(Dely--)
 {
   for( i = LOOP_DLY_1ms; i; i--);
 }
}

void Simple_delay_750ns(void *arg)             // Gives 750ns delay, with arg 1
{
u32 Dely = (u32)arg;
   for( i = Dely; (i!=0); i--);

}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top