/** @brief SPI initialization @return none **/ void SPI_Init(void) { DioPul(pADI_GP0, 0xf8); /* Disable the internal pull ups on P0 */ DioCfg(pADI_GP0, 0x0015); /* Configure P0[2:0] for SPI1 */ DioPulPin(AD7798_CS_PORT, AD7798_CS_PIN_NUMBER, 0); // Disable the internal pull up on AD7798 CS pin DioOenPin(AD7798_CS_PORT, AD7798_CS_PIN_NUMBER, 1); // Set CS pin for AD7798 as output DioSet(AD7798_CS_PORT, AD7798_CS_PIN); DioPulPin(ADT7310_CS_PORT, ADT7310_CS_PIN_NUMBER, 0); // Disable the internal pull up on ADT7310 CS pin DioOenPin(ADT7310_CS_PORT, ADT7310_CS_PIN_NUMBER, 1); // Set CS pin for ADT7310 as output DioSet(ADT7310_CS_PORT, ADT7310_CS_PIN); DioPulPin(AD5270_CS_PORT, AD5270_CS_PIN_NUMBER, 0); // Disable the internal pull up on ADT7310 CS pin DioOenPin(AD5270_CS_PORT, AD5270_CS_PIN_NUMBER, 1); // Set CS pin for ADT7310 as output DioSet(AD5270_CS_PORT, AD5270_CS_PIN); SpiBaud(pADI_SPI1, 9, SPIDIV_BCRST_DIS); /* Set the SPI1 clock rate in Master mode to x kHz. */ /* SPI configuration*/ SpiCfg(pADI_SPI1, SPICON_MOD_TX4RX4, SPICON_MASEN_EN, SPICON_CON_EN | SPICON_SOEN_EN | SPICON_RXOF_EN | SPICON_ZEN_EN | SPICON_TIM_TXWR | SPICON_CPOL_HIGH | SPICON_CPHA_SAMPLETRAILING | SPICON_ENABLE_EN); }
/*************************************************************************** * @brief Writes data to SPI. * * @param data - Write data buffer: * - first byte is the chip select number; * - from the second byte onwards are located data bytes to write. * @param bytesNumber - Number of bytes to write. * * @return Number of written bytes. *******************************************************************************/ void SPI_Write(unsigned char* data, unsigned char bytesNumber, enChannels ch) { uint8_t byte = 0; uint16_t ui16fifo_status = (bytesNumber << 8); /* Set FIFO status correct value */ if(convFlag == 0){ switch(ch){ case AD7798: DioClr(AD7798_CS_PORT, AD7798_CS_PIN); break; case ADT7310: DioClr(ADT7310_CS_PORT, ADT7310_CS_PIN); break; default: { if(daisyCh == 0) DioClr(AD5270_CS_PORT, AD5270_CS_PIN); } break; } } /* Flush Tx and Rx FIFOs */ SpiFifoFlush(pADI_SPI1, SPICON_TFLUSH_EN, SPICON_RFLUSH_EN); for(byte = 0;byte < bytesNumber;byte++) { SpiTx(pADI_SPI1, data[byte]); } /* Wait until x bytes are received */ while ((SpiSta(pADI_SPI1) & ui16fifo_status) != ui16fifo_status); if(convFlag == 0){ switch(ch){ case AD7798: DioSet(AD7798_CS_PORT, AD7798_CS_PIN); break; case ADT7310: DioSet(ADT7310_CS_PORT, ADT7310_CS_PIN); break; default: { if(daisyCh == 0) DioSet(AD5270_CS_PORT, AD5270_CS_PIN); } break; } } }
/** @brief Port test function @return none **/ void Test_Port(void) { if (GPIO_PINS == GPIO0) { /* Check if Port 0 is tested as GPIO */ DioSet(pADI_GP0, 0xFF); /* Set Port 0 outputs to High */ } else if (GPIO_PINS == GPIO1) { /* Check if Port 1 is tested as GPIO */ DioSet(pADI_GP1, 0xFF); /* Set Port 1 outputs to High */ } else if (GPIO_PINS == GPIO2) { /* Check if Port 2 is tested as GPIO */ DioSet(pADI_GP2, 0x07); /* Set Port 2 outputs to High */ } }
int main(void) { /* Watchdog timer disabled */ WDTCTL = WDTPW + WDTHOLD; BspInit(); DioInit(); DioSet(BUTTON0); UartInit(9600, UartCallback); LedInit(); ButtonInit(); ButtonSetup(0, Button0Cbf, 20); LedOn(0); SchedulerInit(100, 1, 2); // 1ms tick, 1 timed task, 2 loop task SetSchedulerTimedTask(LedRedFlash, 0, 500, 0); // schedule timed_task[0] as led_red_flash, set off every 500ms SetSchedulerLoopTask(ButtonLoop, 0); // schedule loop_task[0] as button_loop SetSchedulerLoopTask(UartManage, 1); // schedule loop_task[1] as uart_loop __eint(); SchedulerRun(); while(1) { } }
/** @brief Initialization @return none **/ void AD7091R_Init(void) { SPI_Init(); /* SPI initialization */ DioPulPin(CONVST_PORT, CONVST_PIN_NUMBER, 0); /* Disable the internal pull up on CONVST pin */ DioOenPin(CONVST_PORT, CONVST_PIN_NUMBER, 1); /* Set CONVST pin as output */ DioSet(CONVST_PORT, CONVST_PIN); /* Set CONVST pin as high */ AD7091R_SoftwareReset(); /* Perform an AD7091R software reset required at initialization */ u8StartCounter = 0; /* Reset start counter */ }
void SPIClass::Write(unsigned char slaveDeviceId, unsigned char* data, unsigned char bytesNumber) { uint8_t byte = 0, reset = 0; uint16_t ui16fifo_status; if(bytesNumber == 8){ bytesNumber = 4; reset = 1; } ui16fifo_status = (bytesNumber << 8); /* Set FIFO status correct value */ if(slaveDeviceId == 0) DioClr(CS_PORT, CS_PIN); /* Flush Tx and Rx FIFOs */ SpiFifoFlush(pADI_SPI1, SPICON_TFLUSH_EN, SPICON_RFLUSH_EN); for(byte = 0;byte < bytesNumber;byte++) { SpiTx(pADI_SPI1, data[byte]); } /* Wait until x bytes are received */ while ((SpiSta(pADI_SPI1) & ui16fifo_status) != ui16fifo_status); if(reset == 1){ SpiFifoFlush(pADI_SPI1, SPICON_TFLUSH_EN, SPICON_RFLUSH_EN); for(byte = 0;byte < bytesNumber;byte++) { SpiTx(pADI_SPI1, data[byte]); } while ((SpiSta(pADI_SPI1) & ui16fifo_status) != ui16fifo_status); } if(slaveDeviceId == 0) DioSet(CS_PORT, CS_PIN); }
/** @brief Calibrate ADC input channel @param ui8channel - input channel @param mode - calibration mode: CAL_INT_ZERO_MODE, CAL_INT_FULL_MODE, CAL_SYS_ZERO_MODE, CAL_SYS_FULL_MODE @return none **/ void AD7793_Calibrate(uint8_t ui8channel, enMode mode) { uint32_t ui32reg_value, back_up; AD7793_SelectChannel(ui8channel); /* Select channel */ ui32reg_value = back_up = AD7793_ReadRegister(AD7793_REG_MODE); /* Read MODE register */ ui32reg_value &= AD7793_MODE_MSK; ui32reg_value |= (uint32_t)(mode << 13); /* Set mode */ DioClr(CS_PORT, CS_PIN); AD7793_WriteRegister(AD7793_REG_MODE, ui32reg_value); /* Write MODE register */ while ((AD7793_ReadRegister(AD7793_REG_STAT)& RDY_BIT) != RDY_BIT); /* Wait until RDY bit from STATUS register is high */ DioSet(CS_PORT, CS_PIN); }
void SPIClass::Init() { DioPulPin(pADI_GP0, PIN0, 0); DioCfgPin(pADI_GP0, PIN0, 1); //MISO DioPulPin(pADI_GP0, PIN1, 0); DioCfgPin(pADI_GP0, PIN1, 1); //SCLK DioPulPin(pADI_GP0, PIN2, 0); DioCfgPin(pADI_GP0, PIN2, 1); //MOSI DioPulPin(CS_PORT, CS_PIN_NUMBER, 0); /* Disable the internal pull up on AD7798 CS pin */ DioOenPin(CS_PORT, CS_PIN_NUMBER, 1); /* Set CS pin for AD7798 as output */ DioSet(CS_PORT, CS_PIN); SpiBaud(pADI_SPI1, 9, SPIDIV_BCRST_DIS); /* Set the SPI1 clock rate in Master mode to x kHz. */ /* SPI configuration*/ SpiCfg(pADI_SPI1, SPICON_MOD_TX4RX4, SPICON_MASEN_EN, SPICON_CON_EN | SPICON_SOEN_EN | SPICON_RXOF_EN | SPICON_ZEN_EN | SPICON_TIM_TXWR | SPICON_CPOL_HIGH | SPICON_CPHA_SAMPLETRAILING | SPICON_ENABLE_EN); }
/***************************************************************************//** * @brief Reads data from SPI. * * @param data - As an input parameter, data represents the write buffer: * - first byte is the chip select number; * - from the second byte onwards are located data bytes to write. * As an output parameter, data represents the read buffer: * - from the first byte onwards are located the read data bytes. * @param bytesNumber - Number of bytes to write. * * @return Number of written bytes. *******************************************************************************/ void SPIClass::Read(unsigned char slaveDeviceId, unsigned char* data, unsigned char bytesNumber) { unsigned char writeData[4] = {0, 0, 0, 0}; unsigned char byte = 0; uint16_t ui16fifo_status; ui16fifo_status = ((bytesNumber) << 8); /* Set FIFO status correct value */ for(byte = 0;byte < bytesNumber;byte++) { if(byte == 0) writeData[byte] = data[byte]; else writeData[byte] = 0xAA; /* dummy value */ } if(slaveDeviceId == 0) DioClr(CS_PORT, CS_PIN); SpiFifoFlush(pADI_SPI1, SPICON_TFLUSH_EN, SPICON_RFLUSH_EN); for(byte = 0;byte < bytesNumber;byte++) { SpiTx(pADI_SPI1, writeData[byte]); } /* Wait until x bytes are received */ while ((SpiSta(pADI_SPI1) & ui16fifo_status) != ui16fifo_status); for(byte = 0;byte < bytesNumber;byte++) { data[byte] = SpiRx(pADI_SPI1); } if(slaveDeviceId == 0) DioSet(CS_PORT, CS_PIN); }
/** @brief Read ADC conversion results @param mode - conversion mode: SINGLE_CONV or CONTINUOUS_CONV @param ui8channel - ADC channel to scan @return uint32_t - conversion result **/ uint32_t AD7793_Scan(enMode mode, uint8_t ui8channel) { static uint32_t ui32result, ui32reg_value; uint8_t ui8reg_adrr = (AD7793_COMM_READ | AD7793_COMM_ADR(AD7793_REG_DATA)); /* Set value (read command + DATA register address) to write in COMM register */ AD7793_SelectChannel(ui8channel); /* Select channel to scan */ if(mode == SINGLE_CONV) { /* Check if single conversion mode is wanted */ ui32reg_value = AD7793_ReadRegister(AD7793_REG_MODE); ui32reg_value &= AD7793_MODE_MSK; ui32reg_value |= (uint32_t)(mode << 13); /* Set single mode operation */ DioClr(CS_PORT, CS_PIN); AD7793_WriteRegister(AD7793_REG_MODE, ui32reg_value); } if(mode == CONTINUOUS_CONV) { DioClr(CS_PORT, CS_PIN); } while ((AD7793_ReadRegister(AD7793_REG_STAT)& RDY_BIT) != RDY_BIT); SPI_Write(0xAA, 0xAAAA, 2); ui32result = SPI_Read(ui8reg_adrr, reg_size[AD7793_REG_DATA]); DioSet(CS_PORT, CS_PIN); return ui32result; }
/***************************************************************************//** * @brief Reads data from SPI. * * @param data - As an input parameter, data represents the write buffer: * - first byte is the chip select number; * - from the second byte onwards are located data bytes to write. * As an output parameter, data represents the read buffer: * - from the first byte onwards are located the read data bytes. * @param bytesNumber - Number of bytes to write. * * @return Number of written bytes. *******************************************************************************/ void SPI_Read(unsigned char* data, unsigned char bytesNumber, enChannels ch) { unsigned char writeData[4] = {0, 0, 0, 0}; unsigned char byte = 0; uint16_t ui16fifo_status = ((bytesNumber + 1) << 8); /* Set FIFO status correct value */ for(byte = 0;byte <= bytesNumber;byte++) { if(byte == 0) writeData[byte] = data[byte]; else writeData[byte] = 0xAA; /* dummy value */ } if(convFlag == 0){ switch(ch){ case AD7798: DioClr(AD7798_CS_PORT, AD7798_CS_PIN); break; case ADT7310: DioClr(ADT7310_CS_PORT, ADT7310_CS_PIN); break; default: DioClr(AD5270_CS_PORT, AD5270_CS_PIN); break; } } SpiFifoFlush(pADI_SPI1, SPICON_TFLUSH_EN, SPICON_RFLUSH_EN); for(byte = 0;byte <= bytesNumber;byte++) { SpiTx(pADI_SPI1, writeData[byte]); } /* Wait until x bytes are received */ while ((SpiSta(pADI_SPI1) & ui16fifo_status) != ui16fifo_status); data[0] = writeData[0] = SpiRx(pADI_SPI1); /* Dummy read, not needed value */ for(byte = 0;byte < bytesNumber;byte++) { data[byte] = writeData[byte] = SpiRx(pADI_SPI1); } if(convFlag == 0){ switch(ch){ case AD7798: DioSet(AD7798_CS_PORT, AD7798_CS_PIN); break; case ADT7310: DioSet(ADT7310_CS_PORT, ADT7310_CS_PIN); break; default: DioSet(AD5270_CS_PORT, AD5270_CS_PIN); break; } } }