/**************************************************************************** * * NAME : SpiA2D_GetValuesBySpi * * DESCRIPTION : Sends "Get A2D values" to the A2D processor and recives the readings * ****************************************************************************/ SPI_A2D_STATUS SpiA2D_GetValuesBySpi() { BYTE xdata *Message, i; // try to lock the SPI device // -------------------------- if (SpiLock() == SPI_BUSY) return A2D_SEND_FAILED; // set the device status to busy // ----------------------------- A2DTransactionStatus = A2D_BUSY; // prepare the message to be send using the spi // -------------------------------------------- Message = SpiGetTxBuffer(); Message[A2D_GET_VALUES_INST_INDEX] = SPI_GET_A2D_READINGS_CMD; for (i = 1; i <= NO_OF_ANALOG_INPUTS*2; i++) Message[i] = 0; SpiSetCommWithA2D(TRUE); // select the device (CS) // ---------------------- SpiSelectSlave(A2D_PROCESSOR_SLAVE_ID,CHIP_SELECT); // send the message using the spi // ------------------------------ SpiSend(A2D_GET_VALUES_INST_LENGTH ,SpiA2D_GetFreeBuffer(), SpiA2D_CallBack); return A2D_NO_ERROR; }
/**************************************************************************** * * NAME : SpiA2D_GetSwVersionBySpi * * DESCRIPTION : Sends "Get SW Version" to the A2D processor and recives Version two-byte value. * ****************************************************************************/ SPI_A2D_STATUS SpiA2D_GetSwVersionBySpi() { BYTE xdata *Message; // try to lock the SPI device // -------------------------- if (SpiLock() == SPI_BUSY) return A2D_SEND_FAILED; // set the device status to busy // ----------------------------- A2DTransactionStatus = A2D_BUSY; // prepare the message to be send using the spi // -------------------------------------------- Message = SpiGetTxBuffer(); // get the pointer to the transmit buffer used by the Spi_Driver. Message[A2D_GET_VALUES_INST_INDEX] = SPI_GET_A2D_SW_VERSION_CMD; // 0xFF is for debugging. change to zero. Message[1] = 0; //0xFF; // Sending 1st zero - for receiving Low SW Version BYTE. Message[2] = 0; //0xFF; // Sending 2nd zero - for receiving High SW Version BYTE. SpiSetCommWithA2D(TRUE); // select the device (CS) // ---------------------- SpiSelectSlave(A2D_PROCESSOR_SLAVE_ID,CHIP_SELECT); // send the message using the spi // ------------------------------ SpiSend(A2D_GET_SW_VERSION_INST_LENGTH, A2DRxBufferSW_Version, SpiA2D_CallBack); return A2D_NO_ERROR; }
/**************************************************************************** * * NAME : PotenmtrWriteValue * * * DESCRIPTION : Write a new value to one of the potentiometer devices. * ****************************************************************************/ POTENMTR_STATUS PotenmtrWriteValue(BYTE Value) { BYTE xdata *Message; // try to lock the SPI device // -------------------------- if (SpiLock() == SPI_BUSY) return POTENMTR_SEND_FAILED; // set the device status to busy // ----------------------------- PotenmtrTransactionStatus = POTENMTR_BUSY; // select the device (CS) // ---------------------- SpiSelectSlave(ODOR_FAN_SLAVE_ID,CHIP_SELECT); // prepare the message to be send using the spi // -------------------------------------------- Message = SpiGetTxBuffer(); Message[POTENMTR_ADDR_INDEX] = POTENMTR_ADDR; Message[POTENMTR_DATA_INDEX] = Value; // send the message using the spi // ------------------------------ SpiSend(POTENMTR_INST_LENGTH ,PotenmtrDataIn, PotenmtrCallBack); // SpiSelectSlave(ODOR_FAN_SLAVE_ID, CHIP_DESELECT); return POTENMTR_NO_ERROR; }
// // Send a byte to the display, either as a command or data depending on the "cd" flag // void LcdSend(uint8_t cd, uint8_t data) { LCD_CE_LOW; // Enable LCD ~CE if (cd==0) LCD_DI_LOW; else LCD_DI_HIGH; // Command/Data-bit TOGGLECLK; SpiEnable(1); SpiSend(data); SpiDisable(); LCD_CE_HIGH; // Disable LCD ~CE }
__attribute__((always_inline)) static inline void SpiTransmit(struct spi_periph *p, struct spi_transaction *t) { // when all byte are sent, continue until tx_idx reach input_length // needed when input_length is bigger than output_length uint8_t max_idx = Max(t->output_length, t->input_length); while (p->tx_idx_buf < max_idx && bit_is_set(((sspRegs_t *)(p->reg_addr))->sr, TNF)) { if (p->tx_idx_buf < t->output_length) { if (t->dss == SPIDss8bit) { SpiSend(p, t->output_buf[p->tx_idx_buf]); } else if (t->dss == SPIDss16bit) { uint16_t tmp1 = t->output_buf[2 * p->tx_idx_buf]; // LSB uint16_t tmp2 = t->output_buf[2 * p->tx_idx_buf + 1] << 8; // MSB SpiSend(p, tmp1 | tmp2); } } else { SpiSend(p, 0); } p->tx_idx_buf++; } if (p->tx_idx_buf == max_idx) { SpiDisableTxi(p); } }
/**************************************************************************** * * NAME : E2PROMWrite * * INPUT : E2PROM device number, * Address, * Data to be written. * * OUTPUT : E2PROM_ERROR. * * DESCRIPTION : Write one byte to an E2PROM device. * Call E2PROMWriteEnable first. * ****************************************************************************/ E2PROM_STATUS E2PROMWrite(BYTE DeviceNum, WORD Address, BYTE Data) { BYTE xdata Instruction, Addr8bit, Addr; BYTE xdata *Message; // deselect the device after the write enable inst // ----------------------------------------------- SpiSelectSlave(E2PROM_BASE_SLAVE_ID + DeviceNum,CHIP_DESELECT); // set the device status to busy // ----------------------------- E2PROMTransactionStatus = E2PROM_BUSY; // prepare the address and the instruction byte // -------------------------------------------- Addr = (BYTE) Address; // the lower 8 bit address Addr8bit = (Address & 0x0100) << 3; // take only the 9th bit of the address and shift to be the 3th bit Instruction = E2PROM_WRITE_INST | Addr8bit; //build the instruction byte from the write instruction and the 9th bit address // prepare the message to be send using the spi // -------------------------------------------- Message = SpiGetTxBuffer(); Message[E2PROM_WRITE_INST_INDEX] = Instruction; Message[E2PROM_WRITE_ADDR_INDEX] = Addr; Message[E2PROM_WRITE_DATA_INDEX] = Data; WriteEnable = FALSE; // select the device (CS) // ---------------------- SpiSelectSlave(E2PROM_BASE_SLAVE_ID + DeviceNum,CHIP_SELECT); // send the message using the spi // ------------------------------ SpiSend(E2PROM_WRITE_INST_LENGTH ,E2PROMDataIn, E2PROMCallBack); return E2PROM_NO_ERROR; }
/**************************************************************************** * * NAME : E2PROMReadByte * * INPUT : E2PROM device number, * Address, * Pointer to the data buffer. * * OUTPUT : E2PROM_ERROR. * * DESCRIPTION : Read one byte from an E2PROM device. * ****************************************************************************/ E2PROM_STATUS E2PROMReadByte(BYTE DeviceNum, WORD Address) { BYTE xdata Instruction, Addr8bit, Addr; BYTE xdata *Message; // try to lock the SPI device // -------------------------- if (SpiLock() == SPI_BUSY) return E2PROM_SEND_FAILED; // set the device status to busy // ----------------------------- E2PROMTransactionStatus = E2PROM_BUSY; // prepare the address and the instruction byte // -------------------------------------------- Addr = (BYTE) Address; // the lower 8 bit address Addr8bit = (Address & 0x0100) << 3; // take only the 9th bit of the address and shift to be the 3th bit Instruction = E2PROM_READ_INST | Addr8bit; //build the instruction byte from the read instruction and the 9th bit address // prepare the message to be send using the spi // -------------------------------------------- Message = SpiGetTxBuffer(); Message[E2PROM_READ_INST_INDEX] = Instruction; Message[E2PROM_READ_ADDR_INDEX] = Addr; Message[E2PROM_READ_DUMMY_INDEX] = 0; // Turn on the write protection (low) // -------------------------------- WRITE_PROTECT = 0; // select the device (CS) // ---------------------- SpiSelectSlave(E2PROM_BASE_SLAVE_ID + DeviceNum,CHIP_SELECT); // send the message using the spi // ------------------------------ SpiSend(E2PROM_READ_INST_LENGTH ,E2PROMDataIn, E2PROMCallBack); return E2PROM_NO_ERROR; }
E2PROM_STATUS E2PROMWriteBlock(BYTE DeviceNum, WORD Address, BYTE *Data, BYTE Length) { BYTE xdata Instruction, Addr8bit, Addr, index; BYTE xdata *Message; // deselect the device after the write enable inst. SpiSelectSlave(E2PROM_BASE_SLAVE_ID + DeviceNum,CHIP_DESELECT); // set the device status to busy // ----------------------------- E2PROMTransactionStatus = E2PROM_BUSY; // check if all the bytes are on the same page if (((Address + Length - 1)/16) > (Address/16)) return E2PROM_WRITE_BLOCK_ERORR; // prepare the address and the instruction byte Addr = (BYTE) Address; // the lower 8 bit address Addr8bit = (Address & 0x0100) << 3; // take only the 9th bit of the address and shift to be the 3th bit Instruction = E2PROM_WRITE_INST | Addr8bit; //build the instruction byte from the write instruction and the 9th bit address // prepare the message to be send using the spi Message = SpiGetTxBuffer(); Message[E2PROM_WRITE_INST_INDEX] = Instruction; Message[E2PROM_WRITE_ADDR_INDEX] = Addr; for (index = 0; index < Length; index++) Message[E2PROM_WRITE_DATA_INDEX+index] = Data[index]; WriteEnable = FALSE; // select the device (CS) SpiSelectSlave(E2PROM_BASE_SLAVE_ID + DeviceNum,CHIP_SELECT); // send the message using the spi SpiSend(E2PROM_WRITE_INST_LENGTH + Length - 1, E2PROMDataIn,E2PROMCallBack); return E2PROM_NO_ERROR; }
/**************************************************************************** * * NAME : E2PROMWriteEnable * * INPUT : E2PROM device number, * Address, * Data to be written. * * OUTPUT : E2PROM_ERROR. * * DESCRIPTION : Sends a Write enable instruction to an E2PROM device. * Call this function before calling E2PROMWrite() or E2PROMWriteBlock() * ****************************************************************************/ E2PROM_STATUS E2PROMWriteEnable(BYTE DeviceNum) { BYTE xdata *Message; // try to lock the SPI device // -------------------------- if (SpiLock() == SPI_BUSY) return E2PROM_SEND_FAILED; // set the device status to busy // ----------------------------- E2PROMTransactionStatus = E2PROM_BUSY; // Turn off the write protection (high) (should stay high until the end of the writing) // ------------------------------------------------------------------------------------ WRITE_PROTECT = 1; WriteEnable = TRUE; // prepare a write enable instruction // ---------------------------------- Message = SpiGetTxBuffer(); Message[E2PROM_WRITE_ENABLE_INST_INDEX] = E2PROM_WRITE_ENABLE_INST; // select the device (CS) // ---------------------- SpiSelectSlave(E2PROM_BASE_SLAVE_ID + DeviceNum,CHIP_SELECT); // send the message using the spi // ------------------------------ SpiSend(E2PROM_WRITE_ENABLE_INST_LENGTH ,E2PROMDataIn, E2PROMCallBack); return E2PROM_NO_ERROR; }