AnsweredAssumed Answered

STM32L-Discovery SPI to ADS1118

Question asked by moore.chuck on Aug 28, 2013
Latest reply on Aug 28, 2013 by crt
 I am trying to use the following code to send and receive SPI data from and ADS1118 SPI Temperature sensor.  However I don't see any activity on the bus when I connect my logic analyser.  I used the blink program to help me figure out where my code stops.  I think it my be the NVIC setting but not sure/. I'm still new to the STM line and I know you guys have this stuff DOWN..  Any help would greatly be appreciated.  Thanks again... Chuck
  
ADS1118 to STM32L-Discovery Board.
main.c

#include "stm32l1xx.h" 
#include "stm32l1xx_conf.h" 


/***********Std Prepheral Drivers****************************************/


//#include "stm32l1xx_usart.h"
#include "stm32l1xx_gpio.h"
#include "stm32l1xx_rcc.h"
#include "stm32l1xx_spi.h" 
#include "misc.h"  
#include "ads1118.h" 
#include "stdio.h"
#include "stdarg.h"
#include "stdint.h"


void SPI_Configuration (void) 
{
GPIO_InitTypeDef GPIO_InitStructure;     
 
SPI_InitTypeDef SPI_InitStructure; 
     
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_AHBPeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
     
     
// ADS1118 
// AD_SPI1_CS PA4 
// AD_SPI1_SCK PA5 
// AD_SPI1_MISO PA11
// AD_SPI1_MOSI PA12


GPIO_PinAFConfig(GPIOA, GPIO_Pin_4, GPIO_AF_SPI1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; // 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; // push-pull output 
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //  push-pull output 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_Init (GPIOA, & GPIO_InitStructure); 


GPIO_PinAFConfig(GPIOA, GPIO_Pin_5 | GPIO_Pin_12, GPIO_AF_SPI1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; // push-pull output 
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //  push-pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; 
GPIO_Init (GPIOA, & GPIO_InitStructure); 


GPIO_PinAFConfig(GPIOA, GPIO_Pin_11, GPIO_AF_SPI1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;  
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; 


/* Stuff I added from my blink program */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //  push-pull output 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz; 
GPIO_Init (GPIOB, & GPIO_InitStructure); // B port 
/* to here*/

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // full duplex 
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; // master mode 
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // 8  
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // Clock Polarity idle, SCK is held low 
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // clock phase data sampled from the first clock edge 
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // Software generates NSS 
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; // Baud Rate Control SYSCLK/16 
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // data MSB first 
SPI_InitStructure.SPI_CRCPolynomial = 7; // CRC polynomial register initial value of 7 
SPI_Init (SPI1, &SPI_InitStructure); 
/* Enable SPI1 */ 

     /* Enable the Tx buffer in use interrupt*/
  SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, ENABLE);
          /* Enable the Rx buffer in use empty interrupt */
  SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);
  /* Enable the SPI Error interrupt */
  SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_ERR, ENABLE);
  /* Data transfer is performed in the SPI interrupt routine */
  /* Enable the SPI peripheral */
  SPI_Cmd(SPI1, ENABLE);



void RCC_Configuration (void) 

/* Setup the microcontroller system. Initialize the Embedded Flash Interface, 
initialize the PLL and update the SystemFrequency variable. */ 
SystemInit (); 

/*********************************************************************** 
*Function: /NVIC / 
*Description: / / 
*Input: / / None 
*Output: / / None 
*Others: / / 
************************************************** ********************/ 
 
void NVIC_Configuration (void) 

NVIC_InitTypeDef NVIC_InitStructure; 
/* Configure the NVIC Preemption Priority Bits */ 
/* Configure one bit for preemption priority */  
/* Priority preemption priority group shows the number of bits used, and sub-priority bits used here is 1, 7 */ 
NVIC_PriorityGroupConfig (NVIC_PriorityGroup_0); 
/* Enable the USART1 Interrupt */ 
NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn; // changed from USART1 to SPI1 set the serial port 1 interrupt 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // preemption priority 0 
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // child priority 0 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable 
NVIC_Init (& NVIC_InitStructure); 



/************************************************************************* 
*Function: / / 
*Description: / / 
*Input: / / None 
*Output: / / None 
*Others: / / 
************************************************** *********************/ 
void SysTick_Configuration (void) 

/* SystemFrequency / 1000 1ms ........ 
* SystemFrequency / 100000 10us ........ 
* SystemFrequency / 1000000 1us ........ 
*/ 
if (SysTick_Config (SystemCoreClock / 1000)) // SysTick configuration function 

/* Capture error */ 
while (1) 




/* Configure the SysTick handler priority */ 
 NVIC_SetPriority (SysTick_IRQn, 0x0) ;// SysTick interrupt priority setting 



/************************************************************************ 
*Function: / / 
*Description: / / 
*Input: / / None 
*Output: / / None 
*Others: / / 
************************************************** *********************/ 
int main (void) 

volatile uint32_t a = 0; 
volatile uint8_t b = 1; 
ADS_InitTypeDef ADS_InitStructure; 
GPIO_InitTypeDef ledInit;     /* Stuff I added from my blink program */
RCC_Configuration (); // set the system clock 


NVIC_Configuration (); // interrupt source configuration 


       // configure pins 6 and 7 as GPIO output
     ledInit.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
     ledInit.GPIO_Mode = GPIO_Mode_OUT;
       // initialize the peripheral
     GPIO_Init(GPIOB, &ledInit);     
       // turn pins 6 and 7 on
     GPIO_SetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_7);
//USART1_Configuration (); // initialize the serial port 1 


     
printf ("\n\r ---------------------------------------------------\n "); 
printf ("\n\r thermocouple temperature program \n"); 


// ads_cs_0 (); // CS LOW 
ADS_InitStructure.stru.OS = SINGLE_CONVER; 
ADS_InitStructure.stru.MUX = AINPN_0_1; 
ADS_InitStructure.stru.PGA = PGA_2048; 
ADS_InitStructure.stru.MODE = SINGLE_SHOT; 
ADS_InitStructure.stru.DR = DR_128_SPS; 
ADS_InitStructure.stru.TS_MODE = ADC_MODE; // ad mode 
ADS_InitStructure.stru.PULLUP = PULL_UP_DIS; 
ADS_InitStructure.stru.NOP = DATA_VALID; 
ADS_InitStructure.stru.CNV_RDY_FL = DATA_NREADY;
SPI_RW_Reg (ADS_InitStructure.word); // write 0x to ads1118, twice
while (GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_11)) // loop until DOUT / DRDY set low 

  ;
     printf ("\n\r The default Value is not ready \r"); 

a = SPI_RW_Reg (ADS_InitStructure.word); // read data 
GPIO_SetBits(GPIOB, GPIO_Pin_7);
printf ("\n\r The default Value is%08x \r", a); 


while (1) 

b = GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_11); 
ADS_InitStructure.stru.OS = SINGLE_CONVER; 
ADS_InitStructure.stru.MUX = AINPN_0_1; 
ADS_InitStructure.stru.PGA = PGA_2048; 
ADS_InitStructure.stru.MODE = CONTIOUS; 
ADS_InitStructure.stru.DR = DR_128_SPS; 
ADS_InitStructure.stru.TS_MODE = TEMPERATURE_MODE; // TEMPERATURE MODE 
ADS_InitStructure.stru.PULLUP = PULL_UP_DIS; 
ADS_InitStructure.stru.NOP = DATA_VALID; 
ADS_InitStructure.stru.CNV_RDY_FL = DATA_NREADY; 
SPI_RW_Reg (ADS_InitStructure.word); // write 0x8493 to ads1118, twice 
while (GPIO_ReadInputDataBit (GPIOA, GPIO_Pin_11)) // loop until DOUT / DRDY set low 

GPIO_SetBits(GPIOB, GPIO_Pin_6);     
printf ("\n\r The Value is not ready \r"); 

a = SPI_RW_Reg (ADS_InitStructure.word); // read data 
GPIO_SetBits(GPIOB, GPIO_Pin_7);
printf ("\n\r The Value is%08x \r", a); 




**********************************************************************
//******************************************************************************
// ads1118.c
//Source file for ADS1118 SPI Tempreture Sensor
//July 30,2013
//******************************************************************************
#include "stdio.h"
#include "stdarg.h"
#include "stdint.h"
#include "stm32l1xx.h"
#include "stm32l1xx_conf.h"
#include "stm32l1xx_gpio.h"
#include "stm32l1xx_spi.h"
#include "stm32l1xx_rcc.h"
#include "stm32l1xx_rtc.h"
#include "misc.h"
#include "ads1118.h" 




volatile uint8_t readata1; 
volatile uint8_t readata2; 
volatile uint8_t readata3; 
volatile uint8_t readata4; 
volatile uint32_t readata; 
 
//------------------------------------------------------------------------------
//  Delay function: Delays number of tick Systicks (happens every 10 ms)
//------------------------------------------------------------------------------




void Delay(__IO uint32_t nCount)
{
  for(; nCount != 0; nCount--)
{
;
}
}
/*********************************************************************************************/ 
uint8_t SPI1_SendByte (uint8_t byte) 

 /* Loop detection transmit buffer is empty */ 
while (SPI_I2S_GetFlagStatus (SPI1, SPI_I2S_FLAG_TXE) == RESET) 



 /*Send data through the SPI1 peripheral */ 
SPI_I2S_SendData (SPI1, byte); 
 /* Wait for receiving data, the loop checks the received data buffer */ 
 while (SPI_I2S_GetFlagStatus (SPI1, SPI_I2S_FLAG_RXNE) == RESET) 
 { 
 ; 
 } 
 /* Return the read data */ 
return SPI_I2S_ReceiveData (SPI1); 


 } 
/********************************************************************************************************************************/
uint32_t SPI_RW_Reg (uint16_t CofigReg) 
{
Delay (10); 
readata1 = SPI1_SendByte ((uint8_t) (CofigReg >> 8)); 
readata2 = SPI1_SendByte ((uint8_t) (CofigReg)); 
readata3 = SPI1_SendByte ((uint8_t) (CofigReg >> 8)); 
readata4 = SPI1_SendByte ((uint8_t) (CofigReg));
readata = (uint32_t) readata4 | ((uint32_t) readata3 << 8) | ((uint32_t) readata2 << 16) | ((uint32_t) readata1 << 24); 
Delay (1000); 


     //ads_cs_1();
return readata; 



**********************************************************************************************************************************




/*****************************************************************
*File name: ADS1118.h
*****************************************************************/
#ifndef ADS1118_H
#define ADS1118_H


#ifdef __cplusplus
 extern "C" {
#endif


#ifdef __cplusplus
extern "C:
{
#endif


     
     /*Includes*********************************************************/
#include "stm32l1xx.h"
#include "stdint.h"


/******************************************************************/
//------------------Define and Typedefine--------------------------
// Define AD SPI1 NSS/CS  PA4
// Define AD SPI1 SCK     PA5
// Define AD SPI1 MISO       PA11
// Define AD SPI1 MOSI       PA12
//*****************************************************************
// Configuration registers (Config Registers)
//Operational status / continues onversion start
#define CONFIG_BIT_OS (0 << 15)
//MUX [2:0]: Input multiplexer configuration
#define CONFIG_BIT_MUX (0 << 12)
//PGA [2:0] Programable gain amplifier configurtaion
#define CONFIG_BIT_PGA (1 << 9)
// MODE: Device operationg mode
#define CONFIG_BIT_MODE (0 << 8)
// DR [2:0] Data rate
#define CONFIG_BIT_DR (7 << 5)
//TS_MODE: Temprature sensor mode
#define CONFIG_BIT_TS_MODE (1 << 4)
//Pull_UP_En: Pull-Up enable
#define CONFIG_BIT_PULLUP_EN (1 << 3)
//NOP: No operation
#define CONFIG_BIT_NOP (1 << 1)
// CNV_RDY_FL: Conversion ready flag
#define CONFIG_BIT_CNV_RDY_FL (1 << 0)
//************ Public Data Type Declaration*************************


typedef union SPI_RW_Reg 
{
     uint8_t Data_MSB: 8;
     uint8_t Data_LSB: 8;
 struct  
     {
     volatile uint8_t CNV_RDY_FL: 1; //low
     volatile uint8_t NOP: 2;
     volatile uint8_t PULLUP: 1;
     volatile uint8_t TS_MODE: 1;
     volatile uint8_t DR: 3;
     volatile uint8_t MODE: 1;
     volatile uint8_t PGA: 3;
     volatile uint8_t MUX: 3;
     volatile uint8_t OS: 1; //high
     } stru;
     volatile uint16_t word;


}
     ADS_InitTypeDef;
      typedef enum
          {
          CONVERING = 0x0,
          SINGLE_CONVER = 0x1
          }
          
     ADS_OS_TypeDef;
     typedef enum
          {
          AINPN_0_1 = 0x0,
          AINPN_0_3 = 0x1,
          AINPN_1_3 = 0x2,
          AINPN_2_3 = 0x3,
          AINPN_0_GND = 0x4,
          AINPN_1_GND = 0x5,
          AINPN_2_GND = 0x6,
          AINPN_3_GND = 0x7
          }


     ADS_MUX_TypeDef;
     typedef enum
          {
          PGA_6144 = 0x0,
          PGA_4096 = 0x1,
          PGA_2048 = 0x2,
          PGA_1024 = 0x3,
          PGA_512 = 0x4,
          PGA_256 = 0x5
          }
     
     ADS_PGA_TypeDef;
     typedef enum
          {
          CONTIOUS = 0x0,
          SINGLE_SHOT = 0x1
          }
     
     ADS_MODE_TypeDef;
     typedef enum
          {
          DR_8_SPS = 0x0,
          DR_16_SPS = 0x1,
          DR_32_SPS = 0x2,
          DR_64_SPS = 0x3,
          DR_128_SPS = 0x4,
          DR_250_SPS = 0x5,
          DR_475_SPS = 0x6,
          DR_860_SPS = 0x7
          }
     
     ADS_DATARATE_TypeDef;
     typedef enum
          {
          ADC_MODE = 0x0,
          TEMPERATURE_MODE = 0x1
          }
     
     ADS_TSMODE_TypeDef;
     typedef enum
          {
          PULL_UP_DIS = 0x0,
          PULL_UP_EN = 0x1
          }
     
     ADS_PULL_Type;
     typedef enum
          {
          DATA_VALID = 0x1,
          DATA_INVALID =0x0     
          }
     
     ADS_NOP_TypeDef;
     typedef enum
          {
          DATA_READY = 0x0,
          DATA_NREADY = 0x1
          }
     
     ADS_RDY_TypeDef;
     
/*----Public function prototype------------------------------*/
#define ads_cs_0 () GPIO_ResetBits (GPIOA, GPIO_Pin_4) 
#define ads_cs_1 () GPIO_SetBits (GPIOA, GPIO_Pin_4) 


          
void Delay (uint32_t nTime); 
uint8_t SPI1_SendByte (uint8_t byte); 
uint32_t SPI_RW_Reg (uint16_t CofigReg); 


#endif 
/**************************************************** *****************/


          


Outcomes