diff --git a/inc/board_config.h b/inc/board_config.h index 48fa1dd..5a8314c 100644 --- a/inc/board_config.h +++ b/inc/board_config.h @@ -5,6 +5,10 @@ #ifndef BOARD_CONFIG_H #define BOARD_CONFIG_H +#define SOFTWARE_IIC + +// #define DEBUG_VERBOES + /******************************************************************************/ #define I2C_SPEED 20000 @@ -18,4 +22,15 @@ /******************************************************************************/ +#define RS485_RCU RCU_USART0 +#define RS485_GPIO_RCU RCU_GPIOA +#define RS485_GPIO_PORT GPIOA +#define RS485_TX_PIN GPIO_PIN_2 +#define RS485_RX_PIN GPIO_PIN_3 +#define RS485_PHY USART0 +#define RS485_BAUDRATE 115200U +#define RS485_EN_PIN GPIO_PIN_1 + +/******************************************************************************/ + #endif //BOARD_CONFIG_H diff --git a/inc/ldc1612.h b/inc/ldc1612.h index 2e0ada9..f11cae6 100644 --- a/inc/ldc1612.h +++ b/inc/ldc1612.h @@ -13,8 +13,12 @@ #include #include #include +#include "board_config.h" +#include "soft_i2c.h" #include "i2c.h" +/***************************************************************************/ + #define LDC1612_ADDR (0x2B << 1) /*Register Rddr*/ diff --git a/inc/soft_i2c.h b/inc/soft_i2c.h index 58442ab..5b4d23e 100644 --- a/inc/soft_i2c.h +++ b/inc/soft_i2c.h @@ -22,6 +22,12 @@ /******************************************************************************/ +#define SOFT_I2C_OK 1 +#define SOFT_I2C_FAIL 0 +#define SOFT_I2C_END 1 + +/******************************************************************************/ + void soft_i2c_delay(void); void soft_i2c_config(void); @@ -40,4 +46,8 @@ void soft_i2c_send_byte(uint8_t data); uint8_t soft_i2c_receive_byte(uint8_t ack); +uint8_t soft_i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]); + +uint8_t soft_i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data); + #endif //SOFT_I2C_H diff --git a/inc/tmp112.h b/inc/tmp112.h index 659a3d6..29b2436 100644 --- a/inc/tmp112.h +++ b/inc/tmp112.h @@ -13,7 +13,14 @@ #include #include #include + +#include "board_config.h" + +#ifdef SOFTWARE_IIC +#include "soft_i2c.h" +#else #include "i2c.h" +#endif /******************************************************************************/ diff --git a/src/i2c.c b/src/i2c.c index 3c018dd..99aa01f 100644 --- a/src/i2c.c +++ b/src/i2c.c @@ -32,6 +32,8 @@ void i2c_gpio_config(void) { \retval none */ void i2c_config(void) { + /* configure I2C GPIO */ + i2c_gpio_config(); /* enable I2C clock */ rcu_periph_clock_enable(RCU_I2C); /* configure I2C clock */ diff --git a/src/ldc1612.c b/src/ldc1612.c index 5c104ad..8580255 100644 --- a/src/ldc1612.c +++ b/src/ldc1612.c @@ -12,7 +12,12 @@ void ldc1612_set_conversion_time(uint8_t channel, uint16_t result) { uint8_t data[2] = {0}; data[0] = (result >> 8) & 0xFF; data[1] = result & 0xFF; + +#ifdef SOFTWARE_IIC + soft_i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_TIME_REG_START + channel, data); +#else i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_TIME_REG_START + channel, data); +#endif } /** @brief set conversion offset. @@ -23,7 +28,12 @@ void ldc1612_set_conversion_offset(uint8_t channel, uint16_t result) { uint8_t data[2] = {0}; data[0] = (result >> 8) & 0xFF; data[1] = result & 0xFF; +#ifdef SOFTWARE_IIC + soft_i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_OFFSET_REG_START + channel, data); +#else i2c_write_16bits(LDC1612_ADDR, SET_CONVERSION_OFFSET_REG_START + channel, data); +#endif + } /** @brief Before conversion,wait LC sensor stabilize for a short time. @@ -34,7 +44,11 @@ void ldc1612_set_LC_stabilize_time(uint8_t channel, uint16_t result) { uint8_t data[2] = {0}; data[0] = (result >> 8) & 0xFF; data[1] = result & 0xFF; +#ifdef SOFTWARE_IIC + soft_i2c_write_16bits(LDC1612_ADDR, SET_LC_STABILIZE_REG_START + channel, data); +#else i2c_write_16bits(LDC1612_ADDR, SET_LC_STABILIZE_REG_START + channel, data); +#endif } /** @brief set input frequency divide and fref divide. @@ -65,8 +79,11 @@ void ldc1612_set_freq_divide(uint8_t channel) { data[0] = (value >> 8) & 0xFF; data[1] = value & 0xFF; // printf("\tFIN_DIV: %d, FREF_DIV: %d\r\n", fin_div, freq_div); - +#ifdef SOFTWARE_IIC + soft_i2c_write_16bits(LDC1612_ADDR, SET_FREQ_REG_START + channel, data); +#else i2c_write_16bits(LDC1612_ADDR, SET_FREQ_REG_START + channel, data); +#endif } /** @brief Error output config. @@ -77,7 +94,11 @@ void ldc1612_set_error_config(uint16_t value) { data[0] = (value >> 8) & 0xFF; data[1] = value & 0xFF; +#ifdef SOFTWARE_IIC + soft_i2c_write_16bits(LDC1612_ADDR, ERROR_CONFIG_REG, data); +#else i2c_write_16bits(LDC1612_ADDR, ERROR_CONFIG_REG, data); +#endif } /** @brief mux config. @@ -88,7 +109,11 @@ void ldc1612_set_mux_config(uint16_t value) { data[0] = (value >> 8) & 0xFF; data[1] = value & 0xFF; + #ifdef SOFTWARE_IIC + soft_i2c_write_16bits(LDC1612_ADDR, MUL_CONFIG_REG, data); + #else i2c_write_16bits(LDC1612_ADDR, MUL_CONFIG_REG, data); + #endif } /** @brief reset sensor. @@ -98,7 +123,12 @@ void ldc1612_reset_sensor(void) { uint8_t data[2] = {0}; data[0] = 0x80; data[1] = 0x00; + + #ifdef SOFTWARE_IIC + soft_i2c_write_16bits(LDC1612_ADDR, SENSOR_RESET_REG, data); + #else i2c_write_16bits(LDC1612_ADDR, SENSOR_RESET_REG, data); + #endif } /** @brief set drive current of sensor. @@ -109,7 +139,11 @@ void ldc1612_set_drive_current(uint8_t channel, uint16_t value) { data[0] = (value >> 8) & 0xFF; data[1] = value & 0xFF; + #ifdef SOFTWARE_IIC + soft_i2c_write_16bits(LDC1612_ADDR, SET_DRIVER_CURRENT_REG + channel, data); + #else i2c_write_16bits(LDC1612_ADDR, SET_DRIVER_CURRENT_REG + channel, data); + #endif } /** @brief Main config part of sensor.Contains select channel、start conversion、sleep mode、sensor activation mode、INT pin disable .. @@ -120,7 +154,11 @@ void ldc1612_set_sensor_config(uint16_t value) { data[0] = (value >> 8) & 0xFF; data[1] = value & 0xFF; + #ifdef SOFTWARE_IIC + soft_i2c_write_16bits(LDC1612_ADDR, SENSOR_CONFIG_REG, data); + #else i2c_write_16bits(LDC1612_ADDR, SENSOR_CONFIG_REG, data); + #endif } void ldc1612_single_ch0_config(void) { @@ -141,23 +179,41 @@ void ldc1612_single_ch0_config(void) { void ldc1612_iic_get_sensor_infomation(void) { uint8_t data[2] = {0}; - // ldc1612_iic_read_16bits(READ_MANUFACTURER_ID, data); + +#ifdef SOFTWARE_IIC + soft_i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data); +#else i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data); +#endif printf("\tManufacturer: 0x%x", (data[0] << 8) | data[1]); - // ldc1612_iic_read_16bits(READ_DEVICE_ID, data); + +#ifdef SOFTWARE_IIC + soft_i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data); +#else i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data); +#endif printf("\tDevice: 0x%x", (data[0] << 8) | data[1]); } uint16_t ldc1612_get_manufacturer_id(void) { uint8_t data[2] = {0}; + +#ifdef SOFTWARE_IIC + soft_i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data); +#else i2c_read_16bits(LDC1612_ADDR, READ_MANUFACTURER_ID, data); +#endif return (data[0] << 8) | data[1]; } uint16_t ldc1612_get_deveice_id(void) { uint8_t data[2] = {0}; + +#ifdef SOFTWARE_IIC + soft_i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data); +#else i2c_read_16bits(LDC1612_ADDR, READ_DEVICE_ID, data); +#endif return (data[0] << 8) | data[1]; } @@ -169,11 +225,18 @@ uint32_t ldc1612_get_raw_channel_result(uint8_t channel) { uint32_t raw_value = 0; uint8_t value[2] = {0}; - // ldc1612_iic_read_16bits(CONVERTION_RESULT_REG_START + channel, value); +#ifdef SOFTWARE_IIC + soft_i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel, value); +#else i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel, value); +#endif raw_value |= (uint32_t) ((value[0] << 8) | value[1]) << 16; - // ldc1612_iic_read_16bits(CONVERTION_RESULT_REG_START + channel + 1, value); + +#ifdef SOFTWARE_IIC + soft_i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel + 1, value); +#else i2c_read_16bits(LDC1612_ADDR, CONVERTION_RESULT_REG_START + channel + 1, value); +#endif raw_value |= (uint32_t) ((value[0] << 8) | value[1]); return ldc1612_parse_raw_result(raw_value); } diff --git a/src/main.c b/src/main.c index 939e614..092d290 100644 --- a/src/main.c +++ b/src/main.c @@ -12,12 +12,14 @@ #include "rs485.h" #include "led.h" #include "i2c.h" -#include "soft_i2c.h" #include "ldc1612.h" -#include "tmp112.h" +#include "board_config.h" -uint32_t g_temperature_uint32; -uint32_t g_eddy_current_value_uint32; +#ifdef SOFTWARE_IIC +#include "soft_i2c.h" +#else +#include "i2c.h" +#endif void watchdog_init(void) { /* Enable the LSI clock */ @@ -59,73 +61,16 @@ int main(void) { // watchdog_init(); /* configure I2C */ - // i2c_gpio_config(); - // i2c_config(); + soft_i2c_config(); - // ldc1612_iic_get_sensor_infomation(); + ldc1612_iic_get_sensor_infomation(); /* configure LDC1612 */ - // ldc1612_single_ch0_config(); - - uint8_t data[2] = {0}; - - printf("Start\n"); - - + ldc1612_single_ch0_config(); /* Initialize watchdog */ watchdog_init(); while (1) { - soft_i2c_config(); - printf("111\n"); - - soft_i2c_start(); - soft_i2c_send_byte((0x2B << 1)); - if (!soft_i2c_wait_ack()) - { - printf("NACK\n"); - } - - - soft_i2c_send_byte(0x7E); - if (!soft_i2c_wait_ack()) - { - printf("NACK\n"); - } - - soft_i2c_delay(); - soft_i2c_start(); - - soft_i2c_send_byte((0x2B << 1) + 1); - if (!soft_i2c_wait_ack()) - { - printf("NACK\n"); - } - - soft_i2c_delay(); - - data[0] = soft_i2c_receive_byte(1); - data[1] = soft_i2c_receive_byte(0); - - delay_us(5); - soft_i2c_stop(); - - printf("0x%x 0x%x\n", data[0], data[1]); - - - - - - // i2c_start(); - // delay_ms(1); - // i2c_stop(); - // delay_ms(1); - delay_ms(1000); - // g_eddy_current_value_uint32 = 0; - // g_temperature_uint32 = 0; - // g_eddy_current_value_uint32 = ldc1612_get_raw_channel_result(CHANNEL_0); - // g_temperature_uint32 = tmp112a_get_raw_channel_result(); - printf("1\n"); delay_ms(99); fwdgt_counter_reload(); } diff --git a/src/rs485.c b/src/rs485.c index dbce5c4..c471eca 100644 --- a/src/rs485.c +++ b/src/rs485.c @@ -4,9 +4,6 @@ #include "rs485.h" -extern uint32_t g_temperature_uint32; -extern uint32_t g_eddy_current_value_uint32; - uint8_t package_header[3] = {0xB5, 0xF0, 0x04}; uint8_t package_data[4] = {0}; diff --git a/src/soft_i2c.c b/src/soft_i2c.c index 837c1f1..a985aef 100644 --- a/src/soft_i2c.c +++ b/src/soft_i2c.c @@ -47,11 +47,11 @@ void soft_i2c_start(void) { // sda_out(); I2C_SDA_HIGH(); I2C_SCL_HIGH(); - i2c_delay(); + soft_i2c_delay(); I2C_SDA_LOW(); - i2c_delay(); + soft_i2c_delay(); I2C_SCL_LOW(); - // i2c_delay(); + // soft_i2c_delay(); // 从全高开始,SCL为高期间,SDA下降沿表示start信号,再拉低SCL } @@ -65,9 +65,9 @@ void soft_i2c_stop(void) { // sda_out(); I2C_SCL_LOW(); I2C_SDA_LOW(); - i2c_delay(); + soft_i2c_delay(); I2C_SCL_HIGH(); - i2c_delay(); + soft_i2c_delay(); I2C_SDA_HIGH(); // 从全低开始,SCL为高期间,SDA上升沿表示stop } @@ -81,11 +81,11 @@ void soft_i2c_stop(void) { void soft_i2c_send_ack(void) { // sda_out(); I2C_SDA_LOW(); - i2c_delay(); + soft_i2c_delay(); I2C_SCL_HIGH(); - i2c_delay(); + soft_i2c_delay(); I2C_SCL_LOW(); - i2c_delay(); + soft_i2c_delay(); I2C_SDA_HIGH(); // SCL产生一个正常的时钟周期,其间SDA始终为低电平,表示ACK } @@ -99,11 +99,11 @@ void soft_i2c_send_ack(void) { void soft_i2c_send_nack(void) { // sda_out(); I2C_SDA_HIGH(); - i2c_delay(); + soft_i2c_delay(); I2C_SCL_HIGH(); - i2c_delay(); + soft_i2c_delay(); I2C_SCL_LOW(); - i2c_delay(); + soft_i2c_delay(); I2C_SDA_HIGH(); // SCL产生一个正常的时钟周期,其间SDA始终为高电平,表示NACK } @@ -117,9 +117,9 @@ void soft_i2c_send_nack(void) { uint8_t soft_i2c_wait_ack(void) { // sda_in(); I2C_SDA_HIGH(); - i2c_delay(); + soft_i2c_delay(); I2C_SCL_HIGH(); - i2c_delay(); + soft_i2c_delay(); uint8_t ack = !I2C_SDA_READ(); //ACK信号是第九个时钟期间,SDA被从机在SCL高期间,拉低并保持低电平。此处判断SDA是否被拉低,被拉低则返回0,取反为1,表示收到ACK I2C_SCL_LOW(); @@ -141,9 +141,9 @@ void soft_i2c_send_byte(uint8_t byte) { I2C_SDA_LOW(); //SCL低电平期间,SDA低电平表示0 } byte <<= 1; //左移一位,把原本第二位的数据移到第一位,再判断高低电平 - i2c_delay(); + soft_i2c_delay(); I2C_SCL_HIGH(); //SCL拉高电平,SDA电平状态保持不变 - i2c_delay(); + soft_i2c_delay(); I2C_SCL_LOW(); //SCL拉低电平 delay_us(5); } @@ -163,12 +163,12 @@ uint8_t soft_i2c_receive_byte(uint8_t ack) { for (int i = 0; i < 8; i++) { byte <<= 1; I2C_SCL_HIGH(); - i2c_delay(); + soft_i2c_delay(); if (I2C_SDA_READ()) { byte |= 0x01; } I2C_SCL_LOW(); - i2c_delay(); + soft_i2c_delay(); } if (ack) { soft_i2c_send_ack(); @@ -176,4 +176,53 @@ uint8_t soft_i2c_receive_byte(uint8_t ack) { soft_i2c_send_nack(); } return byte; +} + +uint8_t soft_i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) { + soft_i2c_start(); + soft_i2c_send_byte(slave_addr); + if (!soft_i2c_wait_ack()) { + soft_i2c_stop(); + return SOFT_I2C_FAIL; + } + soft_i2c_send_byte(reg_addr); + if (!soft_i2c_wait_ack()) { + soft_i2c_stop(); + return SOFT_I2C_FAIL; + } + soft_i2c_send_byte(data[0]); + if (!soft_i2c_wait_ack()) { + soft_i2c_stop(); + return SOFT_I2C_FAIL; + } + soft_i2c_send_byte(data[1]); + if (soft_i2c_wait_ack()){} + soft_i2c_stop(); + return SOFT_I2C_OK; +} + +uint8_t soft_i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) +{ + soft_i2c_start(); + soft_i2c_send_byte(slave_addr); + if (!soft_i2c_wait_ack()) { + soft_i2c_stop(); + return SOFT_I2C_FAIL; + } + soft_i2c_send_byte(reg_addr); + if (!soft_i2c_wait_ack()) { + soft_i2c_stop(); + return SOFT_I2C_FAIL; + } + soft_i2c_start(); + soft_i2c_send_byte(slave_addr | 0x01); + if (!soft_i2c_wait_ack()) { + soft_i2c_stop(); + return SOFT_I2C_FAIL; + } + soft_i2c_delay(); + data[0] = soft_i2c_receive_byte(1); + data[1] = soft_i2c_receive_byte(0); + soft_i2c_stop(); + return SOFT_I2C_OK; } \ No newline at end of file diff --git a/src/tmp112.c b/src/tmp112.c index 713bc46..d28ce94 100644 --- a/src/tmp112.c +++ b/src/tmp112.c @@ -9,7 +9,13 @@ uint32_t tmp112a_get_raw_channel_result(void) { uint8_t value[2] = {0}; // ldc1612_iic_read_16bits(CONVERTION_RESULT_REG_START + channel, value); + +#ifdef SOFTWARE_IIC + soft_i2c_read_16bits(TMP112A_ADDR, 0x00, value); +#else i2c_read_16bits(TMP112A_ADDR, 0x00, value); +#endif + raw_value = ((uint16_t) (value[0] << 4) | (value[1]>>4)); return (raw_value * 625); }