diff --git a/ATC_Thermometer/app.c b/ATC_Thermometer/app.c index 7e7a8436..7070e6bd 100644 --- a/ATC_Thermometer/app.c +++ b/ATC_Thermometer/app.c @@ -68,6 +68,7 @@ void user_init_normal(void){//this will get executed one time after power up } _attribute_ram_code_ void user_init_deepRetn(void){//after sleep this will get executed + init_lcd_deepsleep(); blc_ll_initBasicMCU(); rf_set_power_level_index (RF_POWER_P3p01dBm); blc_ll_recoverDeepRetention(); diff --git a/ATC_Thermometer/i2c.c b/ATC_Thermometer/i2c.c index 94c10bf5..2beb5b1e 100644 --- a/ATC_Thermometer/i2c.c +++ b/ATC_Thermometer/i2c.c @@ -10,7 +10,6 @@ RAM bool i2c_sending; void init_i2c(){ i2c_gpio_set(I2C_GPIO_GROUP_C2C3); i2c_master_init(0x78, (uint8_t)(CLOCK_SYS_CLOCK_HZ/(4*400000)) ); - reg_i2c_mode |= FLD_I2C_HOLD_MASTER;// Enable clock stretching for Sensor } void send_i2c(uint8_t device_id, uint8_t *buffer, int dataLen){ @@ -19,4 +18,14 @@ void send_i2c(uint8_t device_id, uint8_t *buffer, int dataLen){ i2c_set_id(device_id); i2c_write_series(0, 0, (uint8_t*)buffer,dataLen); i2c_sending=false; +} + +uint8_t test_i2c_device(uint8_t address){ + reg_i2c_id = address<<1; + reg_i2c_ctrl = FLD_I2C_CMD_START | FLD_I2C_CMD_ID; + while(reg_i2c_status & FLD_I2C_CMD_BUSY); + reg_i2c_ctrl = FLD_I2C_CMD_STOP; + while(reg_i2c_status & FLD_I2C_CMD_BUSY ); + +return (reg_i2c_status & FLD_I2C_NAK)?0:1; } \ No newline at end of file diff --git a/ATC_Thermometer/i2c.h b/ATC_Thermometer/i2c.h index 96554197..ede688c2 100644 --- a/ATC_Thermometer/i2c.h +++ b/ATC_Thermometer/i2c.h @@ -4,4 +4,5 @@ void init_i2c(); void send_i2c(uint8_t device_id, uint8_t *buffer, int dataLen); +uint8_t test_i2c_device(uint8_t address); diff --git a/ATC_Thermometer/lcd.c b/ATC_Thermometer/lcd.c index 4bdc80d8..3ed5afc5 100644 --- a/ATC_Thermometer/lcd.c +++ b/ATC_Thermometer/lcd.c @@ -12,27 +12,81 @@ const uint8_t lcd_init_cmd[] = {0x80,0x3B,0x80,0x02,0x80,0x0F,0x80,0x95,0x80,0x8 RAM uint8_t display_buff[6]; const uint8_t display_numbers[16] = {0xF5,0x05,0xD3,0x97,0x27,0xb6,0xf6,0x15,0xf7,0xb7,0x77,0xe6,0xf0,0xc7,0xf2,0x72}; -void init_lcd(){ +RAM uint8_t lcd_version; +RAM uint8_t i2c_address_lcd = 0x78; // B1.4 uses Address 0x78 and B1.9 uses 0x7c - gpio_set_func(GPIO_PB6, AS_GPIO);//LCD on low temp needs this, its an unknown pin going to the LCD controller chip - gpio_set_output_en(GPIO_PB6, 0); - gpio_set_input_en(GPIO_PB6, 1); - gpio_setup_up_down_resistor(GPIO_PB6, PM_PIN_PULLUP_10K); - sleep_us(50000); +void init_lcd(){ - send_i2c(0x78,lcd_init_cmd, sizeof(lcd_init_cmd)); + if(test_i2c_device(0x3C)){// B1.4 + lcd_version = 0; + i2c_address_lcd = 0x78; + }else if(test_i2c_device(0x3E)){// B1.9 + lcd_version = 2; + i2c_address_lcd = 0x7C; + }else{// B1.6 uses UART and is not testable this way + lcd_version = 1; + } + + if(lcd_version == 0){// B1.4 Hardware + gpio_set_func(GPIO_PB6, AS_GPIO);//LCD on low temp needs this, its an unknown pin going to the LCD controller chip + gpio_set_output_en(GPIO_PB6, 0); + gpio_set_input_en(GPIO_PB6, 1); + gpio_setup_up_down_resistor(GPIO_PB6, PM_PIN_PULLUP_10K); + sleep_us(50000); + send_i2c(i2c_address_lcd,lcd_init_cmd, sizeof(lcd_init_cmd)); + + }else if(lcd_version == 1){// B1.6 Hardware + + init_lcd_deepsleep(); + + }else if(lcd_version == 2){// B1.9 Hardware + //UNKNOWN LCD VERSION + } send_to_lcd_long(0x00,0x00,0x00,0x00,0x00,0x00); } + +void init_lcd_deepsleep(){ + if(lcd_version != 1) + return; + + uart_gpio_set(UART_TX_PD7, UART_RX_PB0); + uart_reset(); + uart_init(61, 9, PARITY_NONE, STOP_BIT_ONE); + uart_dma_enable(0, 0); + dma_chn_irq_enable(0, 0); + uart_irq_enable(0,0); + uart_ndma_irq_triglevel(0,0); +} + +void uart_send_lcd(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4, uint8_t byte5, uint8_t byte6){ + uint8_t trans_buff[13] = {0x00,0x00,0x00,0x00,0xAA,byte6,byte5,byte4,byte3,byte2,byte1,(byte1 ^byte2 ^byte3 ^byte4 ^byte5 ^byte6),0x55}; + for(unsigned char i=0;i<13;i++){ + uart_ndma_send_byte(trans_buff[i]); + } +} void send_to_lcd_long(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4, uint8_t byte5, uint8_t byte6){ + +if(lcd_version == 0){// B1.4 Hardware uint8_t lcd_set_segments[] = {0x80,0x40,0xC0,byte1,0xC0,byte2,0xC0,byte3,0xC0,byte4,0xC0,byte5,0xC0,byte6,0xC0,0x00,0xC0,0x00}; - send_i2c(0x78,lcd_set_segments, sizeof(lcd_set_segments)); + send_i2c(i2c_address_lcd,lcd_set_segments, sizeof(lcd_set_segments)); +}else if(lcd_version == 1){// B1.6 Hardware + uart_send_lcd(byte1,byte2,byte3,byte4,byte5,byte6); +}else if(lcd_version == 2){// B1.9 Hardware +//UNKNOWN LCD VERSION +} } void send_to_lcd(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4, uint8_t byte5, uint8_t byte6){ +if(lcd_version == 0){// B1.4 Hardware uint8_t lcd_set_segments[] = {0x80,0x40,0xC0,byte1,0xC0,byte2,0xC0,byte3,0xC0,byte4,0xC0,byte5,0xC0,byte6}; - send_i2c(0x78,lcd_set_segments, sizeof(lcd_set_segments)); + send_i2c(i2c_address_lcd,lcd_set_segments, sizeof(lcd_set_segments)); +}else if(lcd_version == 1){// B1.6 Hardware + uart_send_lcd(byte1,byte2,byte3,byte4,byte5,byte6); +}else if(lcd_version == 2){// B1.9 Hardware +//UNKNOWN LCD VERSION +} } void update_lcd(){ diff --git a/ATC_Thermometer/lcd.h b/ATC_Thermometer/lcd.h index f3b3e623..c37d69bc 100644 --- a/ATC_Thermometer/lcd.h +++ b/ATC_Thermometer/lcd.h @@ -4,6 +4,7 @@ #include void init_lcd(); +void init_lcd_deepsleep(); void update_lcd(); void show_temp_symbol(uint8_t symbol); void show_battery_symbol(bool state); @@ -13,3 +14,4 @@ void show_smiley(uint8_t state); void show_atc_mac(); void show_ble_symbol(bool state); void send_to_lcd_long(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4, uint8_t byte5, uint8_t byte6); +void uart_send_lcd(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4, uint8_t byte5, uint8_t byte6); diff --git a/ATC_Thermometer/sensor.c b/ATC_Thermometer/sensor.c index 98d55b90..df92b566 100644 --- a/ATC_Thermometer/sensor.c +++ b/ATC_Thermometer/sensor.c @@ -12,22 +12,69 @@ const uint8_t sens_wakeup[] = {0x35,0x17}; const uint8_t sens_sleep[] = {0xB0,0x98}; const uint8_t sens_reset[] = {0x80,0x5D}; +const uint8_t measure_cmd[] = {0xfd}; + +//Since we now got version B1.4 B1.6 and B1.9 of the Thermometer we need to detect the correct sensor it is using +// B1.4 = SHTC3 = 0 = address 0x70/0xE0 +// B1.6 and B1.9 = SHV4 = 1 = address 0x44/0x88 +RAM uint8_t sensor_version; +RAM uint8_t i2c_address_sensor = 0xE0; + void init_sensor(){ - send_i2c(0xE0,sens_wakeup, sizeof(sens_wakeup)); - sleep_us(240); - send_i2c(0xE0,sens_reset, sizeof(sens_reset)); - sleep_us(240); - send_i2c(0xE0,sens_sleep, sizeof(sens_sleep)); + + if(test_i2c_device(0x70)){ + sensor_version = 0; + i2c_address_sensor = 0xE0; + }else if(test_i2c_device(0x44)){ + sensor_version = 1; + i2c_address_sensor = 0x88; + } + + + if(sensor_version == 0){ + send_i2c(i2c_address_sensor,sens_wakeup, sizeof(sens_wakeup)); + sleep_us(240); + send_i2c(i2c_address_sensor,sens_reset, sizeof(sens_reset)); + sleep_us(240); + send_i2c(i2c_address_sensor,sens_sleep, sizeof(sens_sleep)); + }else if(sensor_version == 1){ + send_i2c(i2c_address_sensor,0x94, 1); + sleep_us(1000); + }else if(sensor_version == 2){ + + }else{ + //UNKNOWN SENSOR, how did we got here ??? + } } void read_sensor(int16_t *temp, uint16_t *humi){ - send_i2c(0xE0,sens_wakeup, sizeof(sens_wakeup)); +if(sensor_version == 0){ + + send_i2c(i2c_address_sensor,sens_wakeup, sizeof(sens_wakeup)); sleep_us(240); uint8_t read_buff[5]; - i2c_set_id(0xE0); + reg_i2c_mode |= FLD_I2C_HOLD_MASTER;// Enable clock stretching for Sensor + i2c_set_id(i2c_address_sensor); i2c_read_series(0x7CA2, 2, (uint8_t*)read_buff, 5); - send_i2c(0xE0,sens_sleep, sizeof(sens_sleep)); + reg_i2c_mode &= ~FLD_I2C_HOLD_MASTER;// Disable clock stretching for Sensor + send_i2c(i2c_address_sensor,sens_sleep, sizeof(sens_sleep)); *temp = ((1750*(read_buff[0]<<8 | read_buff[1]))>>16)-450; *humi = (100 *(read_buff[3] << 8 | read_buff[4]))>>16; + +}else if(sensor_version == 1){ + send_i2c(i2c_address_sensor,measure_cmd, sizeof(measure_cmd)); + sleep_us(1000*10); + uint8_t read_buff[5]; + i2c_set_id(i2c_address_sensor); + i2c_read_series(0, 0, (uint8_t*)read_buff, 5); + + *temp = ((1750*(read_buff[0]<<8 | read_buff[1]))>>16)-450; + *humi = (((1250 *(read_buff[3] << 8 | read_buff[4]))>>16)-60)/10; + +}else if(sensor_version == 2){ + +}else{ + //UNKNOWN SENSOR, how did we got here ??? +} }