uint8_t W5100_MemReadWord(uint16_t addr, uint16_t *val) { uint8_t low, high; SPI_RequestSPIBus(); W5100_CS_ENABLE(); SPI_WriteByte(W5100_CMD_READ); SPI_WriteByte(addr>>8); /* high address */ SPI_WriteByte(addr&0xff); /* low address */ high = SPI_ReadByte(); /* data */ W5100_CS_DISABLE(); addr++; W5100_CS_ENABLE(); SPI_WriteByte(W5100_CMD_READ); SPI_WriteByte(addr>>8); /* high address */ SPI_WriteByte(addr&0xff); /* low address */ low = SPI_ReadByte(); /* data */ W5100_CS_DISABLE(); SPI_ReleaseSPIBus(); *val = (high<<8)|low; return ERR_OK; }
/* **函數 : MPU9250_ReadReg **功能 : 讀暫存器 **輸入 : ReadAddr **輸出 : ReadData **使用 : MPU9250_ReadReg(ReadAddr, &DeviceID); */ static void MPU9250_ReadReg( u8 ReadAddr, u8 *ReadData ) { IMU_CSM = 0; SPI_WriteByte(SPIx, 0x80 | ReadAddr); *ReadData = SPI_ReadByte(SPIx); IMU_CSM = 1; }
/** * @brief MPU9250_ReadReg * @note MPU9250 read one byte * @param ReadAddr=reg address ReadData=save the data of reading * @retval None */ static void MPU9250_ReadReg( uint8_t ReadAddr, uint8_t *ReadData ) { SPIx_CSM_PIN_L(); SPI_WriteByte(SPIx, 0x80 | ReadAddr); *ReadData = SPI_ReadByte(SPIx); SPIx_CSM_PIN_H(); }
/*向传感器读取压差值*/ unsigned char MS4525DO_read(unsigned char * p,unsigned char* temperature) { unsigned char error = 0; SS2= 0; SCL = 1; //SDP600 系列传感器(从机) 可以拉低SCL线来强制主机进入等待状态。传感器通过释放SCL线指示内部处理已完成 *p = SPI_ReadByte(); //读取传感器第一个字节数据 *(p+1) = SPI_ReadByte(); //读取传感器数据第二个字节数据 *temperature = SPI_ReadByte(); *(temperature+1) = SPI_ReadByte(); SS2 = 1; if((((*p)&0xc0) == 0xc0) || (((*p)&0x40) == 0x40)) { error = 1; } *p = (*p)&0x3f; return error; }
/*=====================================================================================================*/ static void NRF_ReadBuf( u8 ReadAddr, u8 *ReadBuf, u8 Bytes ) { u8 i = 0; NRF_CSN = 0; SPI_WriteByte(NRF_SPI, ReadAddr); for(i=0; i<Bytes; i++) ReadBuf[i] = SPI_ReadByte(NRF_SPI); NRF_CSN = 1; }
static void MPU9250_ReadRegs( u8 ReadAddr, u8 *ReadBuf, u8 Bytes ) { u8 i = 0; IMU_CSM = 0; SPI_WriteByte(SPIx, 0x80 | ReadAddr); for(i=0; i<Bytes; i++) ReadBuf[i] = SPI_ReadByte(SPIx); IMU_CSM = 1; }
/*=====================================================================================================*/ static u8 NRF_ReadReg( u8 ReadAddr ) { u8 ReadData; NRF_CSN = 0; SPI_WriteByte(NRF_SPI, ReadAddr); ReadData = SPI_ReadByte(NRF_SPI); NRF_CSN = 1; return ReadData; }
/******************************************************************************* * 函数名 : MCP2515_ReadByte * 描述 : 通过SPI从MCP2515指定地址寄器读1个字节数据 * 输入 : addr:MCP2515寄存器地址 * 输出 : 无 * 返回值 : rByte:读取到寄存器的1个字节数据 * 说明 : 无 *******************************************************************************/ unsigned char MCP2515_ReadByte(unsigned char addr) { unsigned char rByte; MCP2515_CS=0; //置MCP2515的CS为低电平 SPI_SendByte(CAN_READ); //发送读命令 SPI_SendByte(addr); //发送地址 rByte=SPI_ReadByte(); //读取数据 MCP2515_CS=1; //置MCP2515的CS为高电平 return rByte; //返回读到的一个字节数据 }
uint8_t W5100_MemReadByte(uint16_t addr, uint8_t *val) { SPI_RequestSPIBus(); W5100_CS_ENABLE(); SPI_WriteByte(W5100_CMD_READ); SPI_WriteByte(addr>>8); /* high address */ SPI_WriteByte(addr&0xff); /* low address */ *val = SPI_ReadByte(); /* data */ W5100_CS_DISABLE(); SPI_ReleaseSPIBus(); return ERR_OK; }
// unsigned int read_MSB = SPI2_transfer( 0x00); //data shifted on SO pin---reading data on MISO pin // unsigned int read_LSB = SPI2_transfer( 0x00); //data shifted on SO pin---reading data on MISO pin // DISABLE_CS() //chip deselect // SPI2STATCLR = 0x40; //clear overflow flag // SPI2CONCLR=0x00008000; //OFF SPI // return ((read_MSB<<8)+read_LSB); //SPI_WriteByte(address>>8); // Output MSB of address // SPI_WriteByte(address & 0xFF); // Output LSB of address/ void SPIWELPolling(void) { int a=0; do{ ENABLE_CS(); //Select Device SPI_WriteByte(rdsr); //Read Status Reg OpCode a=SPI_ReadByte(); //dummy DISABLE_CS(); Delay(100); }while ((a & 0x02) != 0); //Check for WIP bit Set }
/** * @brief MPU9250_ReadRegs * @note MPU9250 read bytes * @param ReadAddr=reg address ReadBuf=Buffer of read data Bytes=length of data to read * @retval None */ static void MPU9250_ReadRegs( uint8_t ReadAddr, uint8_t *ReadBuf, uint8_t Bytes ) { uint8_t i = 0; SPIx_CSM_PIN_L(); SPI_WriteByte(SPIx, 0x80 | ReadAddr); for(i=0; i<Bytes; i++) { ReadBuf[i] = SPI_ReadByte(SPIx); } SPIx_CSM_PIN_H(); }
u8 MPU_ReadReg( u8 ReadAddr ) { u8 ReadData; MPU_CS = 0; //The first bit of the first byte:Read(1) Write(0) ReadAddr|=0x80; SPI_WriteByte(MPU_SPI, ReadAddr); ReadData = SPI_ReadByte(MPU_SPI); MPU_CS = 1; return ReadData; }
/*=====================================================================================================*/ void nRF_ReadBuf( u8 ReadAddr, u8 *ReadBuf, u8 Bytes ) { u8 i = 0; NRF_CE = 0; NRF_CSN = 0; SPI_WriteByte(SPI2, ReadAddr); for(i=0; i<Bytes; i++) ReadBuf[i] = SPI_ReadByte(SPI2); NRF_CSN = 1; }
void MPU_ReadBuf( u8 ReadAddr, u8 *ReadBuf, u8 Bytes ) { u8 i = 0; MPU_CS = 0; //The first bit of the first byte:Read(1) Write(0) ReadAddr|=0x80; SPI_WriteByte(MPU_SPI, ReadAddr); for(i=0; i<Bytes; i++) ReadBuf[i] = SPI_ReadByte(MPU_SPI); MPU_CS = 1; }
status_t LSM303C::MAG_ReadReg(MAG_REG_t reg, uint8_t& data) { debug_print("Reading register 0x"); debug_printlns(reg, HEX); status_t ret = IMU_GENERIC_ERROR; if (interfaceMode == MODE_I2C) { ret = I2C_ByteRead(MAG_I2C_ADDR, reg, data); } else if (interfaceMode == MODE_SPI) { data = SPI_ReadByte(MAG, reg); ret = IMU_SUCCESS; } else { ret = IMU_GENERIC_ERROR; // Shouldn't get here } return ret; }
status_t LSM303C::ACC_ReadReg(ACC_REG_t reg, uint8_t& data) { debug_print("Reading address 0x"); debug_printlns(reg, HEX); status_t ret; if (interfaceMode == MODE_I2C) { ret = I2C_ByteRead(ACC_I2C_ADDR, reg, data); } else if (interfaceMode == MODE_SPI) { data = SPI_ReadByte(ACC, reg); ret = IMU_SUCCESS; } else { ret = IMU_HW_ERROR; } return ret; }
unsigned char read_ext_eeprom(unsigned long address) { while (SPI2ASTATbits.SPIBUSY == 1) continue; SPI2CON=0x00008060; //enable the spi peripheral-8160 BYTE lsb,msb; //SPIWIPPolling(); int data2=0; ENABLE_CS(); //chip select send_8_bit(read); send_8_bit(0x00); //read operation starts send_8_bit(address>>8); //read operation starts send_8_bit((address & 0xFF)); //read operation starts // send_8_bit(0x00); //data2=SPI_ReadByte(); msb = SPI_ReadByte(); // lsb = SPI_ReadByte(); DISABLE_CS(); // data2=((msb<<8) + lsb); // SPI2STATCLR = 0x40; //clear overflow flag // SPI2CONCLR=0x00008000; //OFF SPI return msb; }
void HandleInputChar(char c) { int i; int result; result = PROTO_HandleInputCharacter(c); if (result == RESULT_ERROR) { UART_PrintString(kErrorResponse); } else if( result == RESULT_ACCEPT ) { const ProtocolHandler *handler = PROTO_GetCurrentHandler(); if (handler) { switch (handler->id) { case PROTO_ID_DEFAULT: UART_PrintString(kOkResponse); UART_PrintString(kNewLine); break; case PROTO_ID_SYS: if (PROTO_SYS_GetCommand() == PROTO_SYS_COMMAND_RESET) { UART_PrintString(kOkResponse); while(UART_CheckBusy(LPC_UART) != RESET); NVIC_SystemReset(); } else if (PROTO_SYS_GetCommand() == PROTO_SYS_COMMAND_VERSION) { PrintVersionString(UART_WriteChar); } else { UART_PrintString(kNotAvailableResponse); } UART_PrintString(kNewLine); break; /* case COMMAND_BACKLITE: UART_PrintString( "+BL\r\n" ); backlite_power = PROTO_GetBacklitePower(); break; */ case PROTO_ID_SPI: if (0 && PROTO_SPI_GetAction() == PROTO_SPI_ACTION_READ) { int len = PROTO_SPI_GetLength(); func_printf_nofloat(UART_WriteChar, "%02X:", len); for (i = 0; i < len; i++) { uint8_t b; b = SPI_ReadByte(); func_printf_nofloat(UART_WriteChar, " %02X", b); } } else if (PROTO_SPI_GetAction() == PROTO_SPI_ACTION_WRITE) { uint8_t *data = PROTO_SPI_GetData(); int len = PROTO_SPI_GetLength(); SPI_Write(data, len); UART_PrintString(kOkResponse); } else { UART_PrintString(kNotAvailableResponse); } UART_PrintString(kNewLine); break; case PROTO_ID_RFID: if (PROTO_RF_GetAction() == PROTO_RF_ACTION_READ) { int len = PROTO_RF_GetLength(); func_printf_nofloat(UART_WriteChar, "%02X:", len); for (i = 0; i < len; i++) { int addr = PROTO_RF_GetAddress(); uint8_t b; if (PROTO_RF_GetMode() == PROTO_RF_MODE_INCREMENT_ADDRESS) addr += i; if (PROTO_RF_GetTarget() == PROTO_RF_TARGET_PORT) b = CL632_SpiReadByte(addr); else if (PROTO_RF_GetTarget() == PROTO_RF_TARGET_MEMORY) CL632_ReadE2(addr, &b, 1); func_printf_nofloat(UART_WriteChar, " %02X", b); } } else if (PROTO_RF_GetAction() == PROTO_RF_ACTION_WRITE) { int addr = PROTO_RF_GetAddress(); uint8_t *data = PROTO_RF_GetData(); int len = PROTO_RF_GetLength(); int same_address = PROTO_RF_GetMode() == PROTO_RF_MODE_SAME_ADDRESS; if (PROTO_RF_GetTarget() == PROTO_RF_TARGET_PORT) { CL632_SpiWrite(addr, same_address, data, len); } else if (PROTO_RF_GetTarget() == PROTO_RF_TARGET_MEMORY) { CL632_WriteE2(addr, data, len); } UART_PrintString(kOkResponse); } else { UART_PrintString(kNotAvailableResponse); } UART_PrintString(kNewLine); break; case PROTO_ID_RGB: switch (PROTO_RGB_GetCommand()) { case PROTO_RGB_COMMAND_CAPABILITIES: UART_PrintString("RGB,TRAN"); break; case PROTO_RGB_COMMAND_PRINT_SEQUENCE: UART_PrintString("S"); for (i = 0; i < SEQ_GetSequenceLength(); i++) { const COMMAND *cmd = &(SEQ_GetSequence()[i]); func_printf_nofloat(UART_WriteChar, " L%d#%02X%02X%02X", cmd->interval, cmd->color.red, cmd->color.green, cmd->color.blue); } break; case PROTO_RGB_COMMAND_SET_SEQUENCE: SEQ_SetSequence(PROTO_RGB_GetSequence(), PROTO_RGB_GetSequenceLength()); UART_PrintString(kOkResponse); break; default: UART_PrintString(kNotAvailableResponse); break; } UART_PrintString(kNewLine); break; } } } }