/* ********************************************************************************************************* * 函 数 名: TM7705_WriteByte * 功能说明: 写入1个字节。带CS控制 * 形 参: _data :将要写入的数据 * 返 回 值: 无 ********************************************************************************************************* */ static void ICACHE_FLASH_ATTR TM7705_WriteByte(u8 data) { CS_0(); TM7705_Send8Bit(data); CS_1(); }
/* * 函数:user_spi_read_byte * 说明:SPI读取一个字节 */ u8 ICACHE_FLASH_ATTR user_spi_read_byte(void) { u8 read = 0; #if defined(HARD_SPI) u32 recv_data[1]; SpiData spiData; spiData.cmd = MASTER_READ_DATA_FROM_SLAVE_CMD; spiData.cmdLen = 0; spiData.addr = NULL; spiData.addrLen = 0; spiData.data = recv_data; spiData.dataLen = 1; SPIMasterRecvData(SpiNum_HSPI, &spiData); read = (u8)(recv_data[0]&0xFF); #elif defined(SOFT_SPI) u8 i; CS_0(); for (i = 0; i < 8; i++){ SCK_0(); //user_spi_delay_us(50); read = read<<1; if (MISO_IS_HIGH()){ read++; } SCK_1(); //user_spi_delay_us(50); } CS_1(); #endif return read; }
/* * 函数:user_spi_write_byte * 说明:SPI写一个字节 */ void ICACHE_FLASH_ATTR user_spi_write_byte(u8 data) { #if defined(HARD_SPI) u32 send_data[1] = {data}; SpiData spiData; spiData.cmd = MASTER_WRITE_DATA_TO_SLAVE_CMD; spiData.cmdLen = 0; spiData.addr = NULL; spiData.addrLen = 0; spiData.data = send_data; spiData.dataLen = 1; SPIMasterSendData(SpiNum_HSPI, &spiData); #elif defined(SOFT_SPI) u8 i; CS_0(); for(i = 0; i < 8; i++){ if (data & 0x80){ MOSI_1(); }else{ MOSI_0(); } SCK_0(); data <<= 1; //user_spi_delay_us(50); SCK_1(); } CS_1(); #endif }
/* * 函数:user_spi_read_4byte * 说明:SPI读取4个字节 */ u32 ICACHE_FLASH_ATTR user_spi_read_4byte(void) { u32 read; #if defined(HARD_SPI) u32 recv_data[1]; SpiData spiData; spiData.cmd = MASTER_READ_DATA_FROM_SLAVE_CMD; spiData.cmdLen = 0; spiData.addr = NULL; spiData.addrLen = 0; spiData.data = recv_data; spiData.dataLen = 4; SPIMasterRecvData(SpiNum_HSPI, &spiData); read = recv_data[0]; #elif defined(SOFT_SPI) CS_0(); read = user_spi_read_byte(); read <<= 8; read += user_spi_read_byte(); read <<= 8; read += user_spi_read_byte(); read <<= 8; read += user_spi_read_byte(); CS_1(); #endif return read; }
//发送3个byte数据,带片选CS static void TM7705_Write3Byte(unsigned long _data) { CS_0(); TM7705_Send8Bit((_data >> 16) & 0xFF); TM7705_Send8Bit((_data >> 8) & 0xFF); TM7705_Send8Bit(_data); CS_1(); }
// 功能说明: 从AD芯片读取一个字(8位) static unsigned char TM7705_ReadByte() { unsigned char read; CS_0(); read = TM7705_Recive8Bit(); CS_1(); return read; }
//同步SPI时序 static void TM7705_SyncSPI() { /* AD7705串行接口失步后将其复位。复位后要延时500us再访问 */ CS_0(); TM7705_Send8Bit(0xFF); TM7705_Send8Bit(0xFF); TM7705_Send8Bit(0xFF); TM7705_Send8Bit(0xFF); CS_1(); }
// 功能说明: 读2字节数据 static unsigned short TM7705_Read2Byte() { unsigned short read; CS_0(); read = TM7705_Recive8Bit(); read <<= 8; read += TM7705_Recive8Bit(); CS_1(); return read; }
/* ********************************************************************************************************* * 函 数 名: TM7705_SyncSPI * 功能说明: 同步TM7705芯片SPI接口时序 * 形 参: 无 * 返 回 值: 无 ********************************************************************************************************* */ static void ICACHE_FLASH_ATTR TM7705_SyncSPI(void) { /* AD7705串行接口失步后将其复位。复位后要延时500us再访问 */ CS_0(); TM7705_Send8Bit(0xFF); TM7705_Send8Bit(0xFF); TM7705_Send8Bit(0xFF); TM7705_Send8Bit(0xFF); CS_1(); }
/* ********************************************************************************************************* * 函 数 名: TM7705_ReadByte * 功能说明: 从AD芯片读取一个字(16位) * 形 参: 无 * 返 回 值: 读取的字(16位) ********************************************************************************************************* */ static uint8_t ICACHE_FLASH_ATTR TM7705_ReadByte(void) { u8 read; CS_0(); read = TM7705_Recive8Bit(); CS_1(); return read; }
// 功能说明: 读3字节数据 static unsigned long TM7705_Read3Byte() { unsigned long read; CS_0(); read = TM7705_Recive8Bit(); read <<= 8; read += TM7705_Recive8Bit(); read <<= 8; read += TM7705_Recive8Bit(); CS_1(); return read; }
/* ********************************************************************************************************* * 函 数 名: TM7705_Send8Bit * 功能说明: 向SPI总线发送8个bit数据。 不带CS控制。 * 形 参: _data : 数据 * 返 回 值: 无 ********************************************************************************************************* */ static void ICACHE_FLASH_ATTR TM7705_Send8Bit(u8 data) { //user_spi_write_byte(data); #if defined(SOFT_SPI) u8 i; CS_0(); for(i = 0; i < 8; i++){ if (data & 0x80){ MOSI_1(); }else{ MOSI_0(); } TM7705_Delay_us(50); SCK_0(); data <<= 1; TM7705_Delay_us(50); SCK_1(); } CS_1(); #endif }
/* ********************************************************************************************************* * 函 数 名: TM7705_Read2Byte * 功能说明: 读2字节数据 * 形 参: 无 * 返 回 值: 读取的数据(16位) ********************************************************************************************************* */ static uint16_t ICACHE_FLASH_ATTR TM7705_Read2Byte(void) { u16 read = 0; #if 0 read = user_spi_read_2byte(); #else u8 i; CS_0(); for (i = 0; i < 16; i++){ SCK_0(); //TM7705_Delay_us(20); read = read<<1; if (MISO_IS_HIGH()){ read++; } SCK_1(); //TM7705_Delay_us(50); } CS_1(); #endif return read; }
/* ********************************************************************************************************* * 函 数 名: TM7705_Recive8Bit * 功能说明: 从SPI总线接收8个bit数据。 不带CS控制。 * 形 参: 无 * 返 回 值: 无 ********************************************************************************************************* */ static u8 ICACHE_FLASH_ATTR TM7705_Recive8Bit(void) { u8 read = 0; //read = user_spi_read_byte(); #if defined(SOFT_SPI) u8 i; CS_0(); for (i = 0; i < 8; i++){ SCK_0(); TM7705_Delay_us(50); read = read<<1; if (MISO_IS_HIGH()){ read++; } SCK_1(); TM7705_Delay_us(50); } CS_1(); #endif return read; }
//发送1个byte数据,带片选CS static void TM7705_WriteByte(unsigned char _data) { CS_0(); TM7705_Send8Bit(_data); CS_1(); }
static void rtgui_touch_calculate() { if (touch != RT_NULL) { rt_sem_take(&spi1_lock, RT_WAITING_FOREVER); /* SPI1 configure */ rt_hw_spi1_baud_rate(SPI_BaudRatePrescaler_64);/* 72M/64=1.125M */ //读取触摸值 { rt_uint16_t tmpx[10]; rt_uint16_t tmpy[10]; unsigned int i; for(i=0; i<10; i++) { CS_0(); WriteDataTo7843(TOUCH_MSR_X); /* read X */ tmpx[i] = SPI_WriteByte(0x00)<<4; /* read MSB bit[11:8] */ tmpx[i] |= ((SPI_WriteByte(TOUCH_MSR_Y)>>4)&0x0F ); /* read LSB bit[7:0] */ tmpy[i] = SPI_WriteByte(0x00)<<4; /* read MSB bit[11:8] */ tmpy[i] |= ((SPI_WriteByte(0x00)>>4)&0x0F ); /* read LSB bit[7:0] */ WriteDataTo7843( 1<<7 ); /* 打开中断 */ CS_1(); } //去最高值与最低值,再取平均值 { rt_uint32_t min_x = 0xFFFF,min_y = 0xFFFF; rt_uint32_t max_x = 0,max_y = 0; rt_uint32_t total_x = 0; rt_uint32_t total_y = 0; unsigned int i; for(i=0;i<10;i++) { if( tmpx[i] < min_x ) { min_x = tmpx[i]; } if( tmpx[i] > max_x ) { max_x = tmpx[i]; } total_x += tmpx[i]; if( tmpy[i] < min_y ) { min_y = tmpy[i]; } if( tmpy[i] > max_y ) { max_y = tmpy[i]; } total_y += tmpy[i]; } total_x = total_x - min_x - max_x; total_y = total_y - min_y - max_y; touch->x = total_x / 8; touch->y = total_y / 8; }//去最高值与最低值,再取平均值 }//读取触摸值 rt_sem_release(&spi1_lock); /* if it's not in calibration status */ if (touch->calibrating != RT_TRUE) { if (touch->max_x > touch->min_x) { touch->x = (touch->x - touch->min_x) * X_WIDTH/(touch->max_x - touch->min_x); } else { touch->x = (touch->min_x - touch->x) * X_WIDTH/(touch->min_x - touch->max_x); } if (touch->max_y > touch->min_y) { touch->y = (touch->y - touch->min_y) * Y_WIDTH /(touch->max_y - touch->min_y); } else { touch->y = (touch->min_y - touch->y) * Y_WIDTH /(touch->min_y - touch->max_y); } } }
static void rtgui_touch_calculate() { if (touch != RT_NULL) { rt_sem_take(&spi1_lock, RT_WAITING_FOREVER); /* SPI1 configure */ rt_hw_spi1_baud_rate(SPI_BaudRatePrescaler_64);/* 72M/64=1.125M */ //读取触摸值 { rt_uint16_t tmpx[10]; rt_uint16_t tmpy[10]; unsigned int i; /* From the datasheet: * When the very first CLK after the control byte comes in, the * DOUT of ADS7843 is not valid. So we could only get 7bits from * the first SPI_WriteByte. And the got the following 5 bits from * another SPI_WriteByte.(aligned MSB) */ for(i=0; i<10; i++) { CS_0(); WriteDataTo7843(TOUCH_MSR_X); tmpx[i] = (SPI_WriteByte(0x00) & 0x7F) << 5; tmpx[i] |= (SPI_WriteByte(TOUCH_MSR_Y) >> 3) & 0x1F; tmpy[i] = (SPI_WriteByte(0x00) & 0x7F) << 5; tmpy[i] |= (SPI_WriteByte(0x00) >> 3) & 0x1F; WriteDataTo7843( 1<<7 ); /* 打开中断 */ CS_1(); } //去最高值与最低值,再取平均值 { rt_uint32_t min_x = 0xFFFF,min_y = 0xFFFF; rt_uint32_t max_x = 0,max_y = 0; rt_uint32_t total_x = 0; rt_uint32_t total_y = 0; unsigned int i; for(i=0;i<10;i++) { if( tmpx[i] < min_x ) { min_x = tmpx[i]; } if( tmpx[i] > max_x ) { max_x = tmpx[i]; } total_x += tmpx[i]; if( tmpy[i] < min_y ) { min_y = tmpy[i]; } if( tmpy[i] > max_y ) { max_y = tmpy[i]; } total_y += tmpy[i]; } total_x = total_x - min_x - max_x; total_y = total_y - min_y - max_y; touch->x = total_x / 8; touch->y = total_y / 8; }//去最高值与最低值,再取平均值 }//读取触摸值 rt_sem_release(&spi1_lock); /* if it's not in calibration status */ if (touch->calibrating != RT_TRUE) { if (touch->max_x > touch->min_x) { touch->x = (touch->x - touch->min_x) * X_WIDTH/(touch->max_x - touch->min_x); } else if (touch->max_x < touch->min_x) { touch->x = (touch->min_x - touch->x) * X_WIDTH/(touch->min_x - touch->max_x); } if (touch->max_y > touch->min_y) { touch->y = (touch->y - touch->min_y) * Y_WIDTH /(touch->max_y - touch->min_y); } else if (touch->max_y < touch->min_y) { touch->y = (touch->min_y - touch->y) * Y_WIDTH /(touch->min_y - touch->max_y); } // normalize the data if (touch->x & 0x8000) touch->x = 0; else if (touch->x > X_WIDTH) touch->x = X_WIDTH - 1; if (touch->y & 0x8000) touch->y = 0; else if (touch->y > Y_WIDTH) touch->y = Y_WIDTH - 1; } }