adc_external.c File Reference

The new adc_external module. More...

Go to the source code of this file.

Defines

#define ADCX_BUSY   (SSPSR & (1<<4))
 The SSP bus is busy.
#define ADCX_RNE   (SSPSR & (1<<2))
 SSP receive buffer is not empty.
#define ADCX_TNF   (SSPSR & (1<<1))
 SSP transmit buffer is not full.

Functions

void adcx_add_config (unsigned char channel, unsigned char gain)
 Adds the given channel and gain to the schedule of conversions.
void adcx_conversion_wait (void)
 Blocks until the conversions are complete.
void adcx_convert_all (void)
 Begins the conversion of all the channels in the schedule.
void adcx_convert_cfg (ADCX_CHAN_CFG *config)
 Begins the conversion of the given ADCX configuration.
void adcx_convert_next (void)
 Begins the conversion of the next channel.
signed short adcx_get_result (char channel)
 Gets the result of the latest conversion of a data channel.
void adcx_init (void)
 Initializes the external adc module.
void adcx_isr (void)
 The interrupt service routine for the external adc.
unsigned short adcx_read_buffer (void)
 Reads in a byte from the receive buffer.
unsigned char adcx_register_read (unsigned char reg)
 Reads in the value of a register on the ADCX chip.
void adcx_register_write (unsigned char reg, unsigned char data)
 Writes a byte to a register on the ADCX chip.
void adcx_write (unsigned short data)
 Write the given 16 bits to the ADCX chip.

Variables

static ADCX_CHAN_CFG adcx_configs [16]
 The configurations for the channel conversions.
static int adcx_count = 0
static volatile int adcx_index = 0
 The index of the channel being converted.
static int adcx_num_configs = 0
 The total number of channel configurations set up.
static signed short adcx_results [16]
 Most recent results of the conversions.
static volatile int adcx_status = ADCX_DONE
 The status of the external adc module.
static volatile int adcx_wait_count = 0
 Counter for adcx_conversion_wait.

Detailed Description

The new adc_external module.

Has a schedule of channels to convert using the external ads7871 analog to digital converter chip on the B2A_MC.

Example Hardware setup for the SSP communications:

  // *******************************************************************************
  // Initialize SSP/SPI1 For External ADC
  // *******************************************************************************
  PCONP &= ~(1<<10);    // power setting: disable SPI1  (bit 10)
  PCONP |= (1<<21);   // power setting: enable SSP (bit 21!!!!!!!!!!!!!!!!!!!!!!)
  SSPCR1 = 0;       // Disable SSP to allow setting changes
  SSPCR0 = 0x00000000;
  SSPCR0 = (15<<0)    // data size: 16 bits (bits 0-3 = 15 means 16bit data)
  | (1<<6)      // sclk high when idle
  | (1<<7)        // sample on the second edge (rising edge)
  | (2<<8);       // bit frequency = PCLK/(CPSDVSR*(SCR+1)) =
//  SSPCR0 &= ~(3<<4);    // SPI mode (bits 4-5 = 0)
//  SSPCPSR &= ~(3<<0);   //clear prescale divider
  SSPCPSR = (2<<0);   // prescale divider  
  PINSEL1 &= ~0x3FC;    // clear P0.17~P0.20;
//  PINSEL1 |= (2<<2)|(2<<4)|(2<<6);    // P0.17: SCK1, P0.18: MISO1, P0.19: MOSI1, P0.20: Manual SSEL 
  PINSEL1 |= (2<<2)|(2<<4)|(2<<6)|(2<<8);  // P0.17: SCK1, P0.18: MISO1, P0.19: MOSI1, P0.20: auto SSEL
//  FIO0DIR |= (1<<20); //SSEL is output
//  FIO0CLR = (1<<20); //SSEL low
  SSPCR1 = (1<<1);    // enable SSP/SPI1   
  PINSEL0 &= ~(3<<24);  //ADC reset line set for GPIO operation (P0.12 bits 24/25)
  PINSEL2 &= ~(1<<3);    // set trace port for GPIO use (bit 3 = 0)
  //Should happen automatically at startup, but just in case...
  //GPIO P1_16 is the ADC convert line
  FIO0DIR |= (1<<12);    // set P0_12 (ADC reset) to be a digital output
  FIO1DIR |= (1<<16);    // set P1_16 (ADC convert) to be a digital output  
  FIO0SET = (1<<12);    //set ADC reset line high
  FIO1CLR = (1<<16);    //set ADC convert line low
  // adc_external interrupts
  PINSEL0 &=~(3<<6);
  PINSEL0 |= (3<<6);        // Set Pin3 to EINT1 (ADCBUSY line)
  EXTMODE = (EXTMODE|(1<<1)) & ~EXTMODE_RB;         // Set EINT1 (Bit 1) to be edge-sensitive
  EXTPOLAR &= ~(1<<1)&0x0F;       // Set EINT1 to be sensitive on the falling-edge
  EXTINT = 0x0F;//1;
Author:
Nicolas Williamson
Thomas Craig
Date:
Summer 2009

Definition in file adc_external.c.


Define Documentation

#define ADCX_BUSY   (SSPSR & (1<<4))

The SSP bus is busy.

Definition at line 55 of file adc_external.c.

Referenced by adcx_init(), and adcx_register_read().

#define ADCX_RNE   (SSPSR & (1<<2))

SSP receive buffer is not empty.

Definition at line 57 of file adc_external.c.

Referenced by adcx_init(), adcx_isr(), and adcx_read_buffer().

#define ADCX_TNF   (SSPSR & (1<<1))

SSP transmit buffer is not full.

Definition at line 56 of file adc_external.c.

Referenced by adcx_register_read(), adcx_register_write(), and adcx_write().


Function Documentation

void adcx_add_config ( unsigned char  channel,
unsigned char  gain 
)

Adds the given channel and gain to the schedule of conversions.

Parameters:
channel The adcx channel to convert.
gain The gain the ads7871 should use during conversion.

Definition at line 225 of file adc_external.c.

References adcx_num_configs, and error_occurred().

00225                                                                 {
00226   if(adcx_num_configs<16) {
00227     adcx_configs[adcx_num_configs].chan = channel;
00228     adcx_configs[adcx_num_configs].gain = gain;
00229     adcx_num_configs++;
00230   } else {
00231     error_occurred(ERROR_ADCX_NUM_CFGS);
00232   }
00233 }

signed short adcx_get_result ( char  channel  ) 

Gets the result of the latest conversion of a data channel.

Parameters:
channel The adcx channel to get the result of.
Returns:
The 16 bit conversion.

Definition at line 199 of file adc_external.c.

References adcx_results.

00200 {
00201   return adcx_results[channel];
00202 }

void adcx_init ( void   ) 

Initializes the external adc module.

Call from init_software.

Definition at line 208 of file adc_external.c.

References ADCX_BUSY, adcx_read_buffer(), adcx_register_write(), and ADCX_RNE.

00209 { 
00210   // Setup ADC registers
00211   adcx_register_write(24,0x00); // SPI: MSB first, 3-wire mode, DIN stuff   
00212   adcx_register_write(3,0x00);  // 2s-comp output, manual readback, CCLK div = 1
00213   adcx_register_write(7,0x00);  // Vref=2.5V, buffer and ref powered down (using external 5V reference)
00214   while(ADCX_BUSY){} //Wait for data to be sent
00215   while(ADCX_RNE){   //Read returning dummy byte to clear out buffer.
00216       adcx_read_buffer();
00217   } 
00218 }

void adcx_isr ( void   ) 

The interrupt service routine for the external adc.

Reads in the result of the last requested operation, and begins the next conversion.

Definition at line 75 of file adc_external.c.

References adcx_convert_next(), adcx_index, adcx_num_configs, adcx_read_buffer(), adcx_register_read(), adcx_results, and ADCX_RNE.

00076 {
00077   unsigned char msb, lsb;
00078   int chan;
00079   signed short result;
00080   ADCX_CHAN_CFG * cfg;
00081   
00082   EXTINT = (1<<1); // Clear External Interrupt flag;
00083   
00084   if (adcx_num_configs != 0) {
00085     // ****** READ N-1th RESULT ****** //
00086     while (ADCX_RNE){ // If the Rx buffer is Not Empty, read data
00087       adcx_read_buffer(); //Clear out junk from command
00088     }
00089     msb = adcx_register_read(1);
00090     lsb = adcx_register_read(0);
00091     result = ((signed short)((((unsigned short)msb)<<8)+(unsigned short)lsb))>>2; //Convert (SIGNED data) to (14 bits, right justified)
00092 
00093     // ****** SAVE RESULT ****** //
00094     cfg = &(adcx_configs[adcx_index]);
00095     chan = cfg->chan;
00096     adcx_results[chan] = result;
00097   
00098     // ****** SEND Nth CONVERSION ****** //
00099     adcx_index = adcx_index + 1;
00100     adcx_convert_next();
00101   }
00102   VICVectAddr = 0;          //indicates end of interrupt service
00103 }

unsigned short adcx_read_buffer ( void   ) 

Reads in a byte from the receive buffer.

Returns:
8 bit data from receive buffer.

Definition at line 184 of file adc_external.c.

References ADCX_RNE, and error_occurred().

Referenced by adcx_init(), and adcx_isr().

00184                                      {
00185   unsigned short temp = 0;
00186   if (ADCX_RNE){ // If the Rx buffer is Not Empty (RNE), read data
00187     temp = SSPDR;
00188   } else {
00189     error_occurred(ERROR_ADCX_RX_EMPTY);
00190   }
00191   return temp; 
00192 }

unsigned char adcx_register_read ( unsigned char  reg  ) 

Reads in the value of a register on the ADCX chip.

Parameters:
reg The register of the chip to read data from.
Returns:
The value of the register.

Definition at line 171 of file adc_external.c.

References ADCX_BUSY, and ADCX_TNF.

Referenced by adcx_isr().

00171                                                    {
00172   unsigned short read_in;
00173   while (!(ADCX_TNF)){}
00174   SSPDR = ((1<<6)|reg)<<8; //16 bits reg read instuction
00175   while (ADCX_BUSY){}
00176   read_in = SSPDR;                
00177   return (unsigned char)(read_in & 0x00FF);
00178 }

void adcx_register_write ( unsigned char  reg,
unsigned char  data 
)

Writes a byte to a register on the ADCX chip.

Parameters:
reg The register to write data to.
data 8 bit data to send to the register.

Definition at line 160 of file adc_external.c.

References ADCX_TNF.

Referenced by adcx_convert_cfg(), and adcx_init().

00160                                                                {  
00161   short command = (reg<<8)|data;
00162   while(!(ADCX_TNF)){}  // wait if transmit buffer is full
00163   SSPDR = command;  // register mode, write, 16 bit, big endian
00164 }

void adcx_write ( unsigned short  data  ) 

Write the given 16 bits to the ADCX chip.

Parameters:
data 16 bit data to send to the ADCX chip.

Definition at line 148 of file adc_external.c.

References ADCX_TNF, and error_occurred().

00148                                     {
00149   if (!(ADCX_TNF)){ //Transmit buffer full; TNF (Transmit not full) bit 1
00150     error_occurred(ERROR_ADCX_TX_FULL);
00151   } 
00152   SSPDR = data;
00153 }


Variable Documentation

The configurations for the channel conversions.

Definition at line 62 of file adc_external.c.

volatile int adcx_index = 0 [static]

The index of the channel being converted.

Definition at line 60 of file adc_external.c.

Referenced by adcx_convert_all(), adcx_convert_next(), and adcx_isr().

int adcx_num_configs = 0 [static]

The total number of channel configurations set up.

Definition at line 61 of file adc_external.c.

Referenced by adcx_add_config(), adcx_convert_next(), and adcx_isr().

signed short adcx_results[16] [static]

Most recent results of the conversions.

Definition at line 63 of file adc_external.c.

Referenced by adcx_get_result(), and adcx_isr().

volatile int adcx_status = ADCX_DONE [static]

The status of the external adc module.

Definition at line 65 of file adc_external.c.

Referenced by adcx_conversion_wait(), adcx_convert_all(), and adcx_convert_next().

volatile int adcx_wait_count = 0 [static]

Counter for adcx_conversion_wait.

Definition at line 66 of file adc_external.c.

Referenced by adcx_conversion_wait().

Generated on Tue Jun 29 16:36:14 2010 by  doxygen 1.6.3