develop
esea_info 2 years ago
parent dd6f06c4a1
commit 19096cc93e
  1. 6
      DOC/21_crc.md
  2. 31
      DOC/22_rtc.md
  3. 193
      bsp/Inc/bsp_i2c.h
  4. 142
      bsp/Inc/bsp_i2c_soft.h
  5. 176
      bsp/Inc/bsp_uart.h
  6. 178
      bsp/Src/bsp_i2c.c
  7. 583
      bsp/Src/bsp_i2c_soft.c
  8. 253
      bsp/Src/bsp_uart.c
  9. 53
      device/Inc/bsp_i2c_ads1115.h
  10. 118
      device/Inc/i2c_helper.h
  11. 199
      device/Inc/pH.h
  12. 165
      device/Inc/temperature.h
  13. 89
      device/Inc/uart_helper.h
  14. 153
      device/Inc/uart_interface.h
  15. 607
      device/Src/bsp_i2c_ads1115.c
  16. 142
      device/Src/i2c_helper.c
  17. 605
      device/Src/pH.c
  18. 407
      device/Src/temperature.c
  19. 315
      device/Src/uart_helper.c
  20. 241
      device/Src/uart_interface.c

@ -0,0 +1,6 @@
#### CRC
uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength);
32位置的

@ -0,0 +1,31 @@
#### RTC
HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format);
HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format);
HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format);
HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format);
typedef struct
{
uint8_t Hours;
uint8_t Minutes;
uint8_t Seconds;
uint8_t TimeFormat;
uint32_t SubSeconds;
uint32_t SecondFraction;
uint32_t DayLightSaving;
uint32_t StoreOperation;
} RTC_TimeTypeDef;
typedef struct
{
uint8_t WeekDay;
uint8_t Month;
uint8_t Date;
uint8_t Year;
} RTC_DateTypeDef;

@ -0,0 +1,193 @@
#ifndef __BSP_I2C_H
#define __BSP_I2C_H
#include "stm32f4xx.h"
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include "main.h"
#include "stm32f4xx.h"
#include "i2c.h"
typedef enum
{
I2C_Trans_Polling = 0x00U,
I2C_Trans_IT = 0x01U,
I2C_Trans_DMA = 0x02U,
} I2C_Transmode_TypeDef;
/* ----------------------- variables ---------------------------------*/
// extern UART_HandleTypeDef huart3;
// UART_HandleTypeDef *pi2chelper = &huart3;
typedef int ( *i2c_callback_fun) (void *obj, void *buf, uint16_t size);
typedef struct
{
I2C_HandleTypeDef *hi2c;
volatile uint8_t flag_busy;
volatile uint8_t flag_send_or_rcv;
uint8_t dev_addr;
uint16_t mem_addr_size; // I2C_MEMADD_SIZE_8BIT I2C_MEMADD_SIZE_16BIT
uint16_t write_delay_ms;
// uint8_t mem_addr[2];
uint16_t mem_addr ;
// trans buf size, mem 方法不含地址
uint32_t size;
uint8_t *buf;
I2C_Transmode_TypeDef trans_mode; /* 0 :polling, 1: IT 2: DMA*/
I2C_Transmode_TypeDef rcv_mode;
// volatile uint8_t transferring;
// uint32_t timebase;
// enable_func_gpio enable_trans_gpio;
// enable_func_gpio enable_trans_cplt_gpio;
void * obj;
i2c_callback_fun callback;
// 这个是供外部调用的; 中断是统一管理的, 中断会找到这里的接收回调,然后返回回去
}I2C_HELPER_TypeDef;
// extern I2C_HELPER_TypeDef *i2chelper;
I2C_HELPER_TypeDef * I2C_HELPER_Init( );
void I2C_HELPER_Set_Hi2c(I2C_HELPER_TypeDef * i2chelper, I2C_HandleTypeDef *hi2c);
void I2C_HELPER_Set_dev_addr(I2C_HELPER_TypeDef * i2chelper, uint8_t dev_addr);
void I2C_HELPER_Set_mem_addr_size(I2C_HELPER_TypeDef * i2chelper, uint16_t mem_addr_size);
void I2C_HELPER_Set_mem_addr(I2C_HELPER_TypeDef * i2chelper, uint16_t mem_addr);
int I2C_HELPER_Setup_Trans_mode( I2C_HELPER_TypeDef * i2chelper, uint8_t trans_mode );
int I2C_HELPER_Setup_Rcv_mode( I2C_HELPER_TypeDef * i2chelper, uint8_t rcv_mode );
int I2C_HELPER_Set_Callback( I2C_HELPER_TypeDef * i2chelper, void *obj, i2c_callback_fun func);
//Trans
int I2C_HELPER_Trans(I2C_HELPER_TypeDef * i2chelper, uint8_t *buf, uint16_t size);
// int __I2C_HELPER_Trans(I2C_HELPER_TypeDef * i2chelper , uint8_t *buf, uint16_t size);
int I2C_HELPER_Trans_TxCplt_Callback( I2C_HELPER_TypeDef * i2chelper );
/* 接收方式 也有三种类型 */
int I2C_HELPER_Start_Rcv(I2C_HELPER_TypeDef * i2chelper, uint8_t *buf, uint16_t size);
int I2C_HELPER_RCV_Cplt_Callback( I2C_HELPER_TypeDef * i2chelper );
int I2C_HELPER_RCV_IDLE_Callback( I2C_HELPER_TypeDef * i2chelper, uint16_t size );
int I2C_HELPER_read_bytes(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint8_t *buf,uint32_t size);
int I2C_HELPER_write_bytes(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint8_t *buf,uint32_t size);
int I2C_HELPER_read_memory(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint16_t mem_addr,uint8_t mem_addr_size,uint8_t *buf,uint32_t size);
int I2C_HELPER_write_memory(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint16_t mem_addr,uint8_t mem_addr_size,uint8_t *buf,uint32_t size);
// struct i2c_buff
// {
// volatile uint8_t flag_busy;
// volatile uint8_t flag_send_or_rcve;
// uint8_t memory_addr_size;
// uint8_t dev_addr;
// I2C_TypeDef *i2c;
// DMA_Stream_TypeDef *dma_rx_stream;
// DMA_Stream_TypeDef *dma_tx_stream;
// uint32_t size;
// uint8_t *buf;
// uint8_t mem_addr[2];
// };
// int i2c_init(struct i2c_buff *i2c_buf,I2C_TypeDef *i2c,DMA_Stream_TypeDef *dma_rx_stream,DMA_Stream_TypeDef *dma_tx_stream);
// int i2c_read_memory(struct i2c_buff *i2c_buf,uint8_t dev_addr,uint16_t mem_addr,uint8_t mem_addr_size,uint8_t *buf,uint32_t size);
// int i2c_write_memory(struct i2c_buff *i2c_buf,uint8_t dev_addr,uint16_t mem_addr,uint8_t mem_addr_size,uint8_t *buf,uint32_t size);
// void i2c_dma_tx_it_tc_callback(struct i2c_buff *i2c_buf);
// void i2c_dma_rx_it_tc_callback(struct i2c_buff *i2c_buf);
// int i2c_clear_state(struct i2c_buff *i2c_buf);
// int i2c_get_state(struct i2c_buff *i2c_buf);
// void i2c_it_af_callback(struct i2c_buff *i2c_buf);
// void i2c_it_btf_callback(struct i2c_buff *i2c_buf);
// void i2c_it_addr_callback(struct i2c_buff *i2c_buf);
// void i2c_it_sb_callback(struct i2c_buff *i2c_buf);
// int i2c_read_bytes(struct i2c_buff *i2c_buf,uint8_t dev_addr,uint8_t *buf,uint32_t size);
// int i2c_write_bytes(struct i2c_buff *i2c_buf,uint8_t dev_addr,uint8_t *buf,uint32_t size);
// void i2c_stop(struct i2c_buff *i2c_buf);
// // int I2C_HELPER_Set_Interface_Type(I2C_HELPER_TypeDef * i2chelper, uint8_t interface_type);
// int I2C_HELPER_Setup_Trans_mode( I2C_HELPER_TypeDef * i2chelper, uint8_t trans_mode );
// int I2C_HELPER_Setup_Rcv_mode( I2C_HELPER_TypeDef * i2chelper, uint8_t rcv_mode );
// int I2C_HELPER_Set_trans_GPIO( I2C_HELPER_TypeDef * i2chelper, enable_func_gpio trans_gpio_func );
// int I2C_HELPER_Set_trans_cplt_GPIO( I2C_HELPER_TypeDef * i2chelper, enable_func_gpio trans_cplt_gpio_func );
// int I2C_HELPER_Set_enable_idle( I2C_HELPER_TypeDef * i2chelper, uint8_t enable_idle);
// int I2C_HELPER_Set_Rcv_Buf( I2C_HELPER_TypeDef * i2chelper, uint8_t * buf, uint16_t size);
// int I2C_HELPER_Set_Trans_Buf( I2C_HELPER_TypeDef * i2chelper, uint8_t * buf, uint16_t size);
#endif
/*
HAL
// st = HAL_I2C_Mem_Write(ads1115_i2c_helper->hi2c, ADS1115_WRITE_ADDRESS,
// my_ads1115.reg_data[0],ads1115_i2c_helper->mem_addr_size , reg_data,2,0xFFFF);
// st = HAL_I2C_Mem_Read(ads1115_i2c_helper->hi2c, ADS1115_WRITE_ADDRESS,
// 0x00,ads1115_i2c_helper->mem_addr_size , rx_data,2,0xFFFF);
485
enable rcv
HAL_GPIO_WritePin(i2chelper->de485_gpio, i2chelper->de485_pin, 0);
enable send
HAL_GPIO_WritePin(i2chelper->de485_gpio, i2chelper->de485_pin, 1);
3160 485 -- (232 - )
enable rcv
HAL_GPIO_WritePin(i2chelper->dplx_gpio, i2chelper->dplx_gpio , 1);
HAL_GPIO_WritePin(i2chelper->de485_gpio, i2chelper->de485_pin, 0);
enable send
HAL_GPIO_WritePin(i2chelper->dplx_gpio, i2chelper->dplx_gpio , 0);
HAL_GPIO_WritePin(i2chelper->de485_gpio, i2chelper->de485_pin, 1);
i2chelper = I2C_HELPER_Init();
I2C_HELPER_Set_Huart( i2chelper, phuart),
I2C_HELPER_Setup_Transmod( i2chelper, 2 );
I2C_HELPER_Set_enable_snd_func( i2chelper, NULL );
I2C_HELPER_Set_enable_rcv_func( i2chelper, NULL);
I2C_HELPER_Set_enable_idle( i2chelper, enable_idle);
I2C_HELPER_Set_enable_rcv_func( i2chelper, obj, (callback_fun) func);
// #if MAX3160_ENABLE
// #define DUPLEX_HALF 1
// #define DUPLEX_FULL 0
// #define SEL_485 1
// #define SEL_232 0
// #define ENABLE_485_Trans 1
// #define DISABLE_485_Trans 0
// # endif
// #if MAX485_ENABLE
// #define ENABLE_485_Trans 1
// #define DISABLE_485_Trans 0
// # endif
*/

@ -0,0 +1,142 @@
#ifndef __BSP_I2C_SOFT_H
#define __BSP_I2C_SOFT_H
#include "main.h"
#include "stm32f4xx.h"
// //使用IIC1 PB6 -> SCL, PB7 -> SDA
#define SCL_PORT GPIOB
#define SCL_PIN GPIO_PIN_6
#define SDA_PORT GPIOB
#define SDA_PIN GPIO_PIN_7
// #define SDA_IN() { SDA_PORT->MODER &= ~(3 << 1 * 2); SDA_PORT->MODER = (0 << 1 * 2); } //PC1 1*2,
// #define SDA_OUT() { SDA_PORT->MODER &= ~(3 << 1 * 2); SDA_PORT->MODER = (1 << 1 * 2); }
#define SDA_IN() {GPIOB->MODER &= ~(3<<(7*2)); GPIOB->MODER |= 0<<7*2;} //PB7输入模式
#define SDA_OUT() {GPIOB->MODER &= ~(3<<(7*2)); GPIOB->MODER |= 1<<7*2;} //PB7输出模式
//IO操作函数
#define i2c_scl_low() HAL_GPIO_WritePin(SCL_PORT, SCL_PIN, GPIO_PIN_RESET)
#define i2c_scl_high() HAL_GPIO_WritePin(SCL_PORT, SCL_PIN, GPIO_PIN_SET)
#define i2c_sda_low() HAL_GPIO_WritePin(SDA_PORT, SDA_PIN, GPIO_PIN_RESET)
#define i2c_sda_high() HAL_GPIO_WritePin(SDA_PORT, SDA_PIN, GPIO_PIN_SET)
#define i2c_read_sda() HAL_GPIO_ReadPin( SDA_PORT, SDA_PIN )
extern uint32_t delay1us;
void delay_1us(uint32_t delay_1us);
void analog_i2c_delay(void);
void bsp_analog_i2c_init(void);
void bsp_analog_i2c_start(void);
void bsp_analog_i2c_stop(void);
uint8_t bsp_analog_i2c_wait_ack(void);
void bsp_analog_i2c_ack(void);
void bsp_analog_i2c_nack(void);
void bsp_analog_i2c_send_byte(uint8_t data);
uint8_t bsp_analog_i2c_read_byte(void);
// //使用IICX PB6 -> SCL, PB7 -> SDA
// #define SCL_PORT GPIOB
// #define SCL_PIN GPIO_PIN_6
// #define SDA_PORT GPIOB
// #define SDA_PIN GPIO_PIN_7
//IO方向设置
// #define SDA_IN() {GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(u32)8<<28;} //PB7输入模式
// #define SDA_OUT() {GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(u32)3<<28;} //PB7输出模式
// #define SDA_IN() {GPIOB->MODER &= ~(3<<(7*2)); GPIOB->MODER |= 0<<7*2;} //PB7输入模式
// #define SDA_OUT() {GPIOB->MODER &= ~(3<<(7*2)); GPIOB->MODER |= 1<<7*2;} //PB7输出模式
// //IO操作
// #define IIC_SCL PBout(6) //SCL
// #define IIC_SDA PBout(7) //SDA
// #define READ_SDA PBin(7) //输入SDA
// //IIC所有操作函数
// void IIC_Init(void); //初始化IIC的IO口
// void IIC_Start(void); //发送IIC开始信号
// void IIC_Stop(void); //发送IIC停止信号
// void IIC_Send_Byte(uint8_t txd); //IIC发送一个字节
// uint8_t IIC_Read_Byte(unsigned char ack);//IIC读取一个字节
// uint8_t IIC_Wait_Ack(void); //IIC等待ACK信号
// void IIC_Ack(void); //IIC发送ACK信号
// void IIC_NAck(void); //IIC不发送ACK信号
// void IIC_Write_One_Byte(uint8_t daddr,uint8_t addr,uint8_t data);
// uint8_t IIC_Read_One_Byte(uint8_t daddr,uint8_t addr);
/*
//在AT24CXX指定地址读出一个数据
//ReadAddr:开始读数的地址
//返回值 :读到的数据
u8 AT24CXX_ReadOneByte(u16 ReadAddr)
{
u8 temp=0;
IIC_Start();
if(EE_TYPE>AT24C16)
{
IIC_Send_Byte(0XA0); //发送写命令
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr>>8);//发送高地址
}else IIC_Send_Byte(0XA0+((ReadAddr/256)<<1)); //发送器件地址0XA0,写数据
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr%256); //发送低地址
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(0XA1); //进入接收模式
IIC_Wait_Ack();
temp=IIC_Read_Byte(0);
IIC_Stop();//产生一个停止条件
return temp;
}
//在AT24CXX指定地址写入一个数据
//WriteAddr :写入数据的目的地址
//DataToWrite:要写入的数据
void AT24CXX_WriteOneByte(u16 WriteAddr,u8 DataToWrite)
{
IIC_Start();
if(EE_TYPE>AT24C16)
{
IIC_Send_Byte(0XA0); //发送写命令
IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr>>8);//发送高地址
}else IIC_Send_Byte(0XA0+((WriteAddr/256)<<1)); //发送器件地址0XA0,写数据
IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr%256); //发送低地址
IIC_Wait_Ack();
IIC_Send_Byte(DataToWrite); //发送字节
IIC_Wait_Ack();
IIC_Stop();//产生一个停止条件
delay_ms(10);
}
*/
#endif

@ -19,33 +19,71 @@
#include <stdlib.h>
#include "main.h"
// #if MAX3160_ENABLE
// #define DUPLEX_HALF 1
// #define DUPLEX_FULL 0
// #define SEL_485 1
// #define SEL_232 0
typedef enum
{
Uart_Interface_Default = 0x00U,
Uart_Interface_485 = 0x01U,
Uart_Interface_Max3160_232 = 0x02U,
Uart_Interface_Max3160_485 = 0x03U,
} Uart_Interface_Type_Typedef;
// #define ENABLE_485_Trans 1
// #define DISABLE_485_Trans 0
// # endif
// #if MAX485_ENABLE
// #define ENABLE_485_Trans 1
// #define DISABLE_485_Trans 0
// # endif
typedef enum
{
Uart_Trans_Polling = 0x00U,
Uart_Trans_IT = 0x01U,
Uart_Trans_DMA = 0x02U,
} Uart_Transmode_TypeDef;
typedef enum
{
Uart_IDLE_IT_DISABLE = 0x00U,
Uart_IDLE_IT_ENABLE = 0x01U,
} Uart_IDLE_Enable_TypeDef;
typedef struct
{
uint32_t size;
uint8_t *buf;
}Record_Trans_Rcv_TypeDef;
/* ----------------------- variables ---------------------------------*/
extern UART_HandleTypeDef huart3;
UART_HandleTypeDef *pMyUart = &huart3;
// extern UART_HandleTypeDef huart3;
// UART_HandleTypeDef *puarthelper = &huart3;
typedef void ( *enable_func) (void);
typedef void ( *enable_func_gpio) (void);
typedef int ( *callback_fun) (void *obj, void *buf, uint16_t size);
typedef struct
{
UART_HandleTypeDef *huart;
uint8_t interface_type; /* 0: common, 1: 485 ,2:3160*/
uint8_t enable_idle_it;
Uart_Interface_Type_Typedef interface_type; /* 0: default(TTL 232), 1: 485 ,2:3160-232 3: 3160-485*/
Uart_IDLE_Enable_TypeDef enable_idle_it;
Uart_Transmode_TypeDef trans_mode; /* 0 :polling, 1: IT 2: DMA*/
Uart_Transmode_TypeDef rcv_mode;
Record_Trans_Rcv_TypeDef *trans_record;
// Record_Trans_Rcv_TypeDef *rcv_record;
// uint8_t *trans_buf;
// uint16_t trans_size;
uint8_t *rcv_buf;
uint16_t rcv_size;
volatile uint8_t send_flag;
volatile uint8_t send_tc_flag; /* 0 开始发送, 1 发送完成*/
volatile uint8_t transferring;
// uint32_t timebase;
enable_func_gpio enable_trans_gpio;
enable_func_gpio enable_trans_cplt_gpio;
void * obj;
callback_fun callback;
// 这个是供外部调用的; 中断是统一管理的, 中断会找到这里的接收回调,然后返回回去
// #if MAX485_ENABLE
// GPIO_TypeDef *de485_gpio;
@ -67,77 +105,87 @@ typedef struct
// uint8_t dplx;
// uint8_t sel;
// uint8_t de485;
}UART_HELPER_TypeDef;
uint8_t trans_mode; /* 0 :polling, 1: IT 2: DMA*/
// uint8_t *send_buf;
// uint16_t size_send;
// uint8_t *rcv_buf;
// uint16_t size_rcv;
extern UART_HELPER_TypeDef *uarthelper;
volatile uint8_t send_flag;
volatile uint8_t send_tc_flag; /* 0 开始发送, 1 发送完成*/
// uint32_t timebase;
UART_HELPER_TypeDef * UART_HELPER_Init( );
void UART_HELPER_Set_Huart(UART_HELPER_TypeDef * uarthelper, UART_HandleTypeDef *huart);
int UART_HELPER_Set_Interface_Type(UART_HELPER_TypeDef * uarthelper, uint8_t interface_type);
void (*enable_snd)(void);
void (*enable_rcv)(void);
int UART_HELPER_Setup_Trans_mode( UART_HELPER_TypeDef * uarthelper, uint8_t trans_mode );
int UART_HELPER_Setup_Rcv_mode( UART_HELPER_TypeDef * uarthelper, uint8_t rcv_mode );
void * obj;
callback_fun callback;
int UART_HELPER_Set_trans_GPIO( UART_HELPER_TypeDef * uarthelper, enable_func_gpio trans_gpio_func );
int UART_HELPER_Set_trans_cplt_GPIO( UART_HELPER_TypeDef * uarthelper, enable_func_gpio trans_cplt_gpio_func );
int UART_HELPER_Set_enable_idle( UART_HELPER_TypeDef * uarthelper, uint8_t enable_idle);
// 这个是供外部调用的; 中断是统一管理的, 中断会找到这里的接收回调,然后返回回去
int UART_HELPER_Set_Rcv_Buf( UART_HELPER_TypeDef * uarthelper, uint8_t * buf, uint16_t size);
int UART_HELPER_Set_Trans_Buf( UART_HELPER_TypeDef * uarthelper, uint8_t * buf, uint16_t size);
}MYUART_TypeDef;
int UART_HELPER_Set_Callback( UART_HELPER_TypeDef * uarthelper, void *obj, callback_fun func);
extern MYUART_TypeDef *myuart;
MYUART_TypeDef * Myuart_Init( );
void Myuart_Set_Huart(MYUART_TypeDef * myuart, UART_HandleTypeDef *huart);
int Myuart_Setup_Transmod( MYUART_TypeDef * myuart, uint8_t trans_mode );
int Myuart_Set_enable_snd_func( MYUART_TypeDef * myuart, enable_func enable_snd_func );
int Myuart_Set_enable_rcv_func( MYUART_TypeDef * myuart, enable_func enable_rcv_func);
int Myuart_Set_enable_idle( MYUART_TypeDef * myuart, uint8_t enable_idle);
int UART_HELPER_Trans(UART_HELPER_TypeDef * uarthelper, uint8_t *buf, uint16_t size);
// int __UART_HELPER_Trans(UART_HELPER_TypeDef * uarthelper , uint8_t *buf, uint16_t size);
int UART_HELPER_Trans_TxCplt_Callback( UART_HELPER_TypeDef * uarthelper );
/* 接收方式 也有三种类型 */
int UART_HELPER_Start_Rcv(UART_HELPER_TypeDef * uarthelper, uint8_t *buf, uint16_t size);
int Myuart_Set_Callback( MYUART_TypeDef * myuart, void *obj, callback_fun func);
int UART_HELPER_RCV_Cplt_Callback( UART_HELPER_TypeDef * uarthelper );
int UART_HELPER_RCV_IDLE_Callback( UART_HELPER_TypeDef * uarthelper, uint16_t size );
// void Myuart_Set_Sel_GPIO_Pin(MYUART_TypeDef * myuart, GPIO_TypeDef *gpio, uint16_t pin);
// void Myuart_Set_Dplx_GPIO_Pin(MYUART_TypeDef * myuart, GPIO_TypeDef *gpio, uint16_t pin);
// void Myuart_Set_DE485_GPIO_Pin(MYUART_TypeDef * myuart, GPIO_TypeDef *gpio, uint16_t pin);
#endif
// void Myuart_Set_232(MYUART_TypeDef * myuart);
// void Myuart_Set_485(MYUART_TypeDef * myuart);
/*
GPIO
GPIO
int Myuart_Trans(MYUART_TypeDef * myuart, uint8_t *buf, uint16_t size);
// int __Myuart_Trans(MYUART_TypeDef * myuart , uint8_t *buf, uint16_t size);
int Myuart_Trans_TxCplt_Callback( MYUART_TypeDef * myuart );
/* 接收方式 也有三种类型 */
int Myuart_Start_Rcv(MYUART_TypeDef * myuart, uint8_t *buf, uint16_t size);
IDLE RXCplt
#endif
/*
485
enable rcv
HAL_GPIO_WritePin(myuart->de485_gpio, myuart->de485_pin, 0);
HAL_GPIO_WritePin(uarthelper->de485_gpio, uarthelper->de485_pin, 0);
enable send
HAL_GPIO_WritePin(myuart->de485_gpio, myuart->de485_pin, 1);
HAL_GPIO_WritePin(uarthelper->de485_gpio, uarthelper->de485_pin, 1);
3160 485 -- (232 - )
enable rcv
HAL_GPIO_WritePin(myuart->dplx_gpio, myuart->dplx_gpio , 1);
HAL_GPIO_WritePin(myuart->de485_gpio, myuart->de485_pin, 0);
HAL_GPIO_WritePin(uarthelper->dplx_gpio, uarthelper->dplx_gpio , 1);
HAL_GPIO_WritePin(uarthelper->de485_gpio, uarthelper->de485_pin, 0);
enable send
HAL_GPIO_WritePin(myuart->dplx_gpio, myuart->dplx_gpio , 0);
HAL_GPIO_WritePin(myuart->de485_gpio, myuart->de485_pin, 1);
HAL_GPIO_WritePin(uarthelper->dplx_gpio, uarthelper->dplx_gpio , 0);
HAL_GPIO_WritePin(uarthelper->de485_gpio, uarthelper->de485_pin, 1);
uarthelper = UART_HELPER_Init();
UART_HELPER_Set_Huart( uarthelper, phuart),
UART_HELPER_Setup_Transmod( uarthelper, 2 );
UART_HELPER_Set_enable_snd_func( uarthelper, NULL );
UART_HELPER_Set_enable_rcv_func( uarthelper, NULL);
UART_HELPER_Set_enable_idle( uarthelper, enable_idle);
UART_HELPER_Set_enable_rcv_func( uarthelper, obj, (callback_fun) func);
myuart = Myuart_Init();
// #if MAX3160_ENABLE
// #define DUPLEX_HALF 1
// #define DUPLEX_FULL 0
// #define SEL_485 1
// #define SEL_232 0
// #define ENABLE_485_Trans 1
// #define DISABLE_485_Trans 0
// # endif
// #if MAX485_ENABLE
// #define ENABLE_485_Trans 1
// #define DISABLE_485_Trans 0
// # endif
Myuart_Set_Huart( myuart, phuart),
Myuart_Setup_Transmod( myuart, 2 );
Myuart_Set_enable_snd_func( myuart, NULL );
Myuart_Set_enable_rcv_func( myuart, NULL);
Myuart_Set_enable_idle( myuart, enable_idle);
Myuart_Set_enable_rcv_func( myuart, obj, (callback_fun) func);
*/

@ -0,0 +1,178 @@
#include "bsp_i2c.h"
I2C_HELPER_TypeDef * I2C_HELPER_Init( )
{
I2C_HELPER_TypeDef *Handle = (I2C_HELPER_TypeDef *)malloc(sizeof(I2C_HELPER_TypeDef));
if (Handle == NULL)
{
return NULL;
}
Handle->hi2c = NULL;
Handle->flag_busy = 0;
Handle->flag_send_or_rcv = 0;
Handle->mem_addr_size = I2C_MEMADD_SIZE_8BIT;
Handle->dev_addr = 0;
Handle->size = 0;
Handle->write_delay_ms = 0;
Handle->buf = NULL;
Handle->trans_mode = I2C_Trans_Polling;
Handle->rcv_mode = I2C_Trans_Polling;
Handle->obj =NULL;
Handle->callback =NULL;
return Handle;
}
void I2C_HELPER_Set_Hi2c(I2C_HELPER_TypeDef * i2chelper, I2C_HandleTypeDef *hi2c)
{
i2chelper->hi2c = hi2c;
}
void I2C_HELPER_Set_dev_addr(I2C_HELPER_TypeDef * i2chelper, uint8_t dev_addr)
{
i2chelper->dev_addr = dev_addr;
}
void I2C_HELPER_Set_mem_addr_size(I2C_HELPER_TypeDef * i2chelper, uint16_t mem_addr_size)
{
i2chelper->mem_addr_size = mem_addr_size;
}
void I2C_HELPER_Set_mem_addr(I2C_HELPER_TypeDef * i2chelper, uint16_t mem_addr)
{
// i2chelper->mem_addr[0] = mem_addr>>8;
// i2chelper->mem_addr[1] = mem_addr&0xFF;
i2chelper->mem_addr =mem_addr;
if (i2chelper->mem_addr_size == I2C_MEMADD_SIZE_16BIT)
{
i2chelper->mem_addr =mem_addr;
}
}
int I2C_HELPER_Setup_Trans_mode( I2C_HELPER_TypeDef * i2chelper, uint8_t trans_mode )
{
i2chelper->trans_mode = trans_mode;
i2chelper->rcv_mode = trans_mode;
}
int I2C_HELPER_Setup_Rcv_mode( I2C_HELPER_TypeDef * i2chelper, uint8_t rcv_mode )
{
i2chelper->rcv_mode = rcv_mode;
}
int I2C_HELPER_Set_Callback( I2C_HELPER_TypeDef * i2chelper, void *obj, i2c_callback_fun func)
{
i2chelper->obj = obj;
i2chelper->callback = func;
return 0;
}
// TODO 添加地址
int I2C_HELPER_Trans(I2C_HELPER_TypeDef * i2chelper, uint8_t *buf, uint16_t size)
{
int st;
i2chelper->flag_busy = 1;
switch ( i2chelper->trans_mode)
{
case 0:
// st = HAL_UART_Transmit(i2chelper->huart, buf, size , 0xFFFF);
st = HAL_I2C_Mem_Write(i2chelper->hi2c, i2chelper->dev_addr,
i2chelper->mem_addr,i2chelper->mem_addr_size ,
buf,size,0xFFFF);
return st;
break;
case 1:
st = HAL_I2C_Mem_Write_IT(i2chelper->hi2c, i2chelper->dev_addr,
i2chelper->mem_addr,i2chelper->mem_addr_size ,
buf,size );
return st;
break;
case 2:
/* 会出现 st ==2 huart->gState != HAL_UART_STATE_READY */
st = HAL_I2C_Mem_Write_DMA(i2chelper->hi2c, i2chelper->dev_addr,
i2chelper->mem_addr,i2chelper->mem_addr_size ,
buf,size );
return st;
break;
default:
break;
}
return st;
}
int I2C_HELPER_Start_Rcv(I2C_HELPER_TypeDef * i2chelper, uint8_t *buf, uint16_t size)
{
int st;
switch ( i2chelper->rcv_mode)
{
case 0:
// st = HAL_UART_Receive(uarthelper->huart, buf, size , 0xFFFF);
st =HAL_I2C_Mem_Read(i2chelper->hi2c, i2chelper->dev_addr,
i2chelper->mem_addr,i2chelper->mem_addr_size ,
buf,size,0xFFFF);
return st;
break;
case 1:
st =HAL_I2C_Mem_Read_IT(i2chelper->hi2c, i2chelper->dev_addr,
i2chelper->mem_addr,i2chelper->mem_addr_size ,
buf,size );
return st;
break;
case 2:
/* 会出现 st ==2 huart->gState != HAL_UART_STATE_READY */
// if (uarthelper->enable_idle_it == 1)
// {
// // log_i("dma rcv idle en... ");
// __HAL_UART_ENABLE_IT(uarthelper->huart, UART_IT_IDLE);
// }
// st = HAL_UART_Receive_DMA(uarthelper->huart, buf, size);
// log_i("dma rcv ... ");
st =HAL_I2C_Mem_Read_DMA(i2chelper->hi2c, i2chelper->dev_addr,
i2chelper->mem_addr,i2chelper->mem_addr_size ,
buf,size );
return st;
break;
default:
break;
}
return st;
}
int I2C_HELPER_Trans_TxCplt_Callback( I2C_HELPER_TypeDef * i2chelper )
{
// log_i( "I2C_HELPER_Trans_TxCplt_Callback ... ");
i2chelper->callback( i2chelper->obj, NULL, 0);
}
// int I2C_HELPER_read_bytes(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint8_t *buf,uint32_t size)
// {
// return 0;
// }
// int I2C_HELPER_write_bytes(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint8_t *buf,uint32_t size)
// {
// return 0;
// }
// int I2C_HELPER_read_memory(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint16_t mem_addr,uint8_t mem_addr_size,uint8_t *buf,uint32_t size)
// {
// return 0;
// }
// int I2C_HELPER_write_memory(I2C_HELPER_TypeDef * i2chelper,uint8_t dev_addr,uint16_t mem_addr,uint8_t mem_addr_size,uint8_t *buf,uint32_t size)
// {
// return 0;
// }

@ -0,0 +1,583 @@
#include "bsp_i2c_soft.h"
uint32_t delay1us;
/**
* @brief: 1us延时
* @param {uint32_t} delay_1us
* @return {*}
*/
// void delay_1us(uint32_t delay_1us)
// {
// HAL_TIM_Base_Start_IT( &htim3 );
// delay1us = delay_1us;
// while (delay1us);
// HAL_TIM_Base_Stop_IT( &htim3 );
// }
void delay_1us(uint32_t delay_1us)
{
uint16_t cnt = 0;
while(delay_1us--)
{
for(cnt=168/5; cnt>0; cnt--); // TODO 168
}
}
// static void delay_us(uint32_t u_sec)
// {
// uint16_t cnt = 0;
// while(u_sec--)
// {
// for(cnt=168/5; cnt>0; cnt--);
// }
// }
/**
* @brief: IIC延时时间
* @return {*}
*/
void analog_i2c_delay(void)
{
delay_1us(2);
}
/**
* @brief: I2C初始化
* @return {*}
*/
void bsp_analog_i2c_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/*Configure GPIO pins : PD6 SCL */
GPIO_InitStruct.Pin = SCL_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(SCL_PORT, &GPIO_InitStruct);
/*Configure GPIO pins : PC1 SDA */
GPIO_InitStruct.Pin = SDA_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(SDA_PORT, &GPIO_InitStruct);
bsp_analog_i2c_stop();
}
/**
* @brief: I2C ,SCL为高电平的时候SDA产生一个下降沿信号
* @return {*}
*/
void bsp_analog_i2c_start(void)
{
/* _____
*SDA \_____________
* __________
*SCL \________
*/
i2c_sda_high();
i2c_scl_high();
analog_i2c_delay();
i2c_sda_low();
analog_i2c_delay();
i2c_scl_low();
analog_i2c_delay();
}
/**
* @brief: I2C ,SCL为高电平的时候SDA产生一个上升沿信号
* @return {*}
*/
void bsp_analog_i2c_stop(void)
{
/* _______
*SDA __________/
* ____________
*SCL _____/
*/
i2c_sda_low();
i2c_scl_high();
analog_i2c_delay();
i2c_sda_high();
analog_i2c_delay();
}
/**
* @brief: I2C
* @return {*}
*/
uint8_t bsp_analog_i2c_wait_ack(void)
{
uint32_t timeout = 0;
i2c_sda_high();
SDA_IN();
analog_i2c_delay();
i2c_scl_high();
analog_i2c_delay();
while(i2c_read_sda())
{
timeout++;
if(timeout > 2000)
{
return 1;
}
}
i2c_scl_low();
analog_i2c_delay();
return 0;
}
/**
* @brief: I2C
* @return {*}
*/
void bsp_analog_i2c_ack(void)
{
/* ____
*SCL ______/ \______
* ____ _____
*SDA \_______/
*/
i2c_sda_low();
analog_i2c_delay();
i2c_scl_high();
analog_i2c_delay();
i2c_scl_low();
analog_i2c_delay();
i2c_sda_high();
}
/**
* @brief: I2C
* @return {*}
*/
void bsp_analog_i2c_nack(void)
{
/* ____
*SCL ______/ \______
* __________________
*SDA
*/
i2c_sda_high();
analog_i2c_delay();
i2c_scl_high();
analog_i2c_delay();
i2c_scl_low();
analog_i2c_delay();
}
/**
* @brief: I2C
* @param {uint8_t} data
* @return {*}
*/
void bsp_analog_i2c_send_byte(uint8_t data)
{
uint8_t i;
SDA_OUT();
for(i = 0; i < 8; i++)
{
if(data & 0x80)
{
i2c_sda_high();
}
else
{
i2c_sda_low();
}
analog_i2c_delay();
i2c_scl_high();
analog_i2c_delay();
i2c_scl_low();
if(i == 7)
{
i2c_sda_high();
}
data <<= 1;
analog_i2c_delay();
}
}
/**
* @brief: I2C
* @return {*}
*/
uint8_t bsp_analog_i2c_read_byte(void)
{
uint8_t i, data = 0;
i2c_sda_high();
SDA_IN();
for(i = 0; i < 8; i++ )
{
data <<= 1;
i2c_scl_high();
analog_i2c_delay();
if(i2c_read_sda())
{
data++;
}
i2c_scl_low();
analog_i2c_delay();
}
return data;
}
uint8_t bsp_i2c_test_write( uint8_t slave_addr, uint8_t reg_addr, uint8_t *buf, uint8_t len)
{
bsp_analog_i2c_start();
//发送器件地址+写命令
bsp_analog_i2c_send_byte(slave_addr & 0xFE);
if(bsp_analog_i2c_wait_ack()) //等待应答
{
bsp_analog_i2c_stop();
return -1;
}
//发送写(起始)寄存器地址
bsp_analog_i2c_send_byte(reg_addr);
if(bsp_analog_i2c_wait_ack()) //等待应答
{
bsp_analog_i2c_stop();
return -1;
}
//发送写数据
for(int i=0; i<len; i++)
{
bsp_analog_i2c_send_byte(*buf++);
if(bsp_analog_i2c_wait_ack()) //等待应答
{
bsp_analog_i2c_stop();
return -1;
}
}
//停止位
bsp_analog_i2c_stop();
}
uint8_t bsp_i2c_test_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *buf, uint8_t len)
{
//发送起始位
bsp_analog_i2c_start();
//发送器件地址+写命令
bsp_analog_i2c_send_byte(slave_addr & 0xFE);
if(bsp_analog_i2c_wait_ack()) //等待应答
{
bsp_analog_i2c_stop();
return -1;
}
//发送写(起始)寄存器地址
bsp_analog_i2c_send_byte(reg_addr);
if(bsp_analog_i2c_wait_ack()) //等待应答
{
bsp_analog_i2c_stop();
return -1;
}
//发送重复起始位
bsp_analog_i2c_start();
//发送器件地址+读命令
bsp_analog_i2c_send_byte(slave_addr | 0x01);
if(bsp_analog_i2c_wait_ack()) //等待应答
{
bsp_analog_i2c_stop();
return -1;
}
//读取数据
// for(int i=0; i<len-1; i++)
// {
// *buf++ = bsp_analog_i2c_read_byte();
// }
// *buf = bsp_analog_i2c_read_byte(); //最后一位数据发送NACK
for(int i=0; i<len-1; i++)
{
*buf++ = bsp_analog_i2c_read_byte();
bsp_analog_i2c_ack(); // 应答
}
*buf = bsp_analog_i2c_read_byte(); //最后一位数据发送NACK
bsp_analog_i2c_nack(); //非应答
//停止位
bsp_analog_i2c_stop();
}
// //IIC初始化
// void IIC_Init(void)
// {
// GPIO_InitTypeDef GPIO_Initure;
// __HAL_RCC_GPIOB_CLK_ENABLE(); //使能GPIOB时钟
// //PH4,5初始化设置
// GPIO_Initure.Pin=GPIO_PIN_6|GPIO_PIN_7;
// GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP; //推挽输出
// GPIO_Initure.Pull=GPIO_PULLUP; //上拉
// GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH;//高速
// HAL_GPIO_Init(GPIOB,&GPIO_Initure);
// IIC_SDA=1;
// IIC_SCL=1;
// }
// //产生IIC起始信号
// void IIC_Start(void)
// {
// SDA_OUT(); //sda线输出
// IIC_SDA=1;
// IIC_SCL=1;
// delay_us(4);
// IIC_SDA=0;//START:when CLK is high,DATA change form high to low
// delay_us(4);
// IIC_SCL=0;//钳住I2C总线,准备发送或接收数据
// }
// //产生IIC停止信号
// void IIC_Stop(void)
// {
// SDA_OUT();//sda线输出
// IIC_SCL=0;
// IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
// delay_us(4);
// IIC_SCL=1;
// IIC_SDA=1;//发送I2C总线结束信号
// delay_us(4);
// }
// //等待应答信号到来
// //返回值:1,接收应答失败
// // 0,接收应答成功
// uint8_t IIC_Wait_Ack(void)
// {
// uint8_t ucErrTime=0;
// SDA_IN(); //SDA设置为输入
// IIC_SDA=1;delay_us(1);
// IIC_SCL=1;delay_us(1);
// while(READ_SDA)
// {
// ucErrTime++;
// if(ucErrTime>250)
// {
// IIC_Stop();
// return 1;
// }
// }
// IIC_SCL=0;//时钟输出0
// return 0;
// }
// //产生ACK应答
// void IIC_Ack(void)
// {
// IIC_SCL=0;
// SDA_OUT();
// IIC_SDA=0;
// delay_us(2);
// IIC_SCL=1;
// delay_us(2);
// IIC_SCL=0;
// }
// //不产生ACK应答
// void IIC_NAck(void)
// {
// IIC_SCL=0;
// SDA_OUT();
// IIC_SDA=1;
// delay_us(2);
// IIC_SCL=1;
// delay_us(2);
// IIC_SCL=0;
// }
// //IIC发送一个字节
// //返回从机有无应答
// //1,有应答
// //0,无应答
// void IIC_Send_Byte(uint8_t txd)
// {
// uint8_t t;
// SDA_OUT();
// IIC_SCL=0;//拉低时钟开始数据传输
// for(t=0;t<8;t++)
// {
// IIC_SDA=(txd&0x80)>>7;
// txd<<=1;
// delay_us(2); //对TEA5767这三个延时都是必须的
// IIC_SCL=1;
// delay_us(2);
// IIC_SCL=0;
// delay_us(2);
// }
// }
// //读1个字节,ack=1时,发送ACK,ack=0,发送nACK
// uint8_t IIC_Read_Byte(unsigned char ack)
// {
// unsigned char i,receive=0;
// SDA_IN();//SDA设置为输入
// for(i=0;i<8;i++ )
// {
// IIC_SCL=0;
// delay_us(2);
// IIC_SCL=1;
// receive<<=1;
// if(READ_SDA)receive++;
// delay_us(1);
// }
// if (!ack)
// IIC_NAck();//发送nACK
// else
// IIC_Ack(); //发送ACK
// return receive;
// }
// 兼容软硬i2c
// /*
// * 函数功能:I2C1写寄存器,这是提供给上层的接口
// * 输入参数:slave_addr:从机地址
// * reg_addr:寄存器地址
// * len:写入的长度
// * data_ptr:指向要写入的数据
// * 返回值 :正常为0,不正常为非0
// * 说明 :无
// */
// int Sensors_I2C1_WriteRegister(unsigned char slave_addr, unsigned char reg_addr, unsigned short len, unsigned char *data_ptr)
// {
// #ifdef HARDWARE_I2C //硬件I2C
// HAL_StatusTypeDef status = HAL_OK;
// status = HAL_I2C_Mem_Write(&hi2c1, slave_addr, reg_addr, I2C_MEMADD_SIZE_8BIT, data_ptr, len, I2Cx_FLAG_TIMEOUT);
// //检查通讯状态
// if(status != HAL_OK)
// {
// //总线出错处理
// I2C1_Error();
// }
// while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)
// {
// }
// //检查SENSOR是否就绪进行下一次读写操作
// while (HAL_I2C_IsDeviceReady(&hi2c1, slave_addr, I2Cx_FLAG_TIMEOUT, I2Cx_FLAG_TIMEOUT) == HAL_TIMEOUT);
// //等待传输结束
// while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)
// {
// }
// return status;
// #else //软件I2C1
// //发送起始位
// IIC1_Start();
// //发送器件地址+写命令
// IIC1_Write_Byte(slave_addr & 0xFE);
// if(IIC1_Wait_Ack()) //等待应答
// {
// IIC1_Stop();
// return -1;
// }
// //发送写(起始)寄存器地址
// IIC1_Write_Byte(reg_addr);
// if(IIC1_Wait_Ack()) //等待应答
// {
// IIC1_Stop();
// return -1;
// }
// //发送写数据
// for(int i=0; i<len; i++)
// {
// IIC1_Write_Byte(*data_ptr++);
// if(IIC1_Wait_Ack()) //等待应答
// {
// IIC1_Stop();
// return -1;
// }
// }
// //停止位
// IIC1_Stop();
// return 0;
// #endif
// }
// /*
// * 函数功能:I2C1读寄存器,这是提供给上层的接口
// * 输入参数:slave_addr:从机地址
// * reg_addr:寄存器地址
// * len:要读取的长度
// * data_ptr:指向要存储数据的指针
// * 返回值 :正常为0,不正常为非0
// * 说明 :无
// */
// int Sensors_I2C1_ReadRegister(unsigned char slave_addr, unsigned char reg_addr, unsigned short len, unsigned char *data_ptr)
// {
// #ifdef HARDWARE_I2C //硬件I2C
// HAL_StatusTypeDef status = HAL_OK;
// status = HAL_I2C_Mem_Read(&hi2c1, slave_addr, reg_addr, I2C_MEMADD_SIZE_8BIT, data_ptr, len, I2Cx_FLAG_TIMEOUT);
// //检查通讯状态
// if(status != HAL_OK)
// {
// //总线出错处理
// I2C1_Error();
// }
// while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)
// {
// }
// //检查SENSOR是否就绪进行下一次读写操作
// while (HAL_I2C_IsDeviceReady(&hi2c1, slave_addr, I2Cx_FLAG_TIMEOUT, I2Cx_FLAG_TIMEOUT) == HAL_TIMEOUT);
// //等待传输结束
// while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)
// {
// }
// return status;
// #else //软件I2C
// //发送起始位
// IIC1_Start();
// //发送器件地址+写命令
// IIC1_Write_Byte(slave_addr & 0xFE);
// if(IIC1_Wait_Ack()) //等待应答
// {
// IIC1_Stop();
// return -1;
// }
// //发送写(起始)寄存器地址
// IIC1_Write_Byte(reg_addr);
// if(IIC1_Wait_Ack()) //等待应答
// {
// IIC1_Stop();
// return -1;
// }
// //发送重复起始位
// IIC1_Start();
// //发送器件地址+读命令
// IIC1_Write_Byte(slave_addr | 0x01);
// if(IIC1_Wait_Ack()) //等待应答
// {
// IIC1_Stop();
// return -1;
// }
// //读取数据
// for(int i=0; i<len-1; i++)
// {
// *data_ptr++ = IIC1_Read_Byte(1);
// }
// *data_ptr = IIC1_Read_Byte(0); //最后一位数据发送NACK
// //停止位
// IIC1_Stop();
// return 0;
// #endif
// }

@ -1,205 +1,212 @@
#include "bsp_uart.h"
#include "elog.h"
MYUART_TypeDef * Myuart_Init( )
UART_HELPER_TypeDef * UART_HELPER_Init( )
{
MYUART_TypeDef *Handle = (MYUART_TypeDef *)malloc(sizeof(MYUART_TypeDef));
UART_HELPER_TypeDef *Handle = (UART_HELPER_TypeDef *)malloc(sizeof(UART_HELPER_TypeDef));
if (Handle == NULL)
{
return NULL;
}
Handle->interface_type = Uart_Interface_Default;
Handle->enable_idle_it = Uart_IDLE_IT_DISABLE;
Handle->trans_mode = Uart_Trans_Polling;
Handle->rcv_mode = Uart_Trans_Polling;
Handle->trans_record = (Record_Trans_Rcv_TypeDef *)malloc(sizeof(Record_Trans_Rcv_TypeDef));
// Handle->rcv_record = (Record_Trans_Rcv_TypeDef *)malloc(sizeof(Record_Trans_Rcv_TypeDef));
Handle->send_flag = 0;
Handle->send_tc_flag = 0;
Handle->interface_type = 0;
Handle->enable_idle_it = 0;
Handle->enable_rcv =NULL;
Handle->enable_snd =NULL;
// #if MAX485_ENABLE
// Handle->de485_gpio = NULL;
// Handle->interface_type = 1;
// #endif
// #if MAX3160_ENABLE
// Handle->de485_gpio = NULL;
// Handle->sel_gpio = NULL;
// Handle->dplx_gpio = NULL;
// Handle->interface_type = 2;
// #endif
Handle->transferring = 0;
Handle->enable_trans_gpio =NULL;
Handle->enable_trans_cplt_gpio =NULL;
Handle->obj =NULL;
Handle->callback =NULL;
return Handle;
}
void Myuart_Set_Huart(MYUART_TypeDef * myuart, UART_HandleTypeDef *huart)
void UART_HELPER_Set_Huart(UART_HELPER_TypeDef * uarthelper, UART_HandleTypeDef *huart)
{
uarthelper->huart = huart;
}
int UART_HELPER_Set_Interface_Type(UART_HELPER_TypeDef * uarthelper, uint8_t interface_type)
{
myuart->huart = huart;
uarthelper->interface_type = interface_type;
return 0;
}
int Myuart_Setup_Transmod( MYUART_TypeDef * myuart, uint8_t trans_mode )
int UART_HELPER_Setup_Trans_mode( UART_HELPER_TypeDef * uarthelper, uint8_t trans_mode )
{
/* 0 :polling, 1: IT 2: DMA*/
myuart->trans_mode = trans_mode;
uarthelper->trans_mode = trans_mode;
uarthelper->rcv_mode = trans_mode;
return 0;
}
int Myuart_Set_enable_snd_func( MYUART_TypeDef * myuart, enable_func enable_snd_func )
int UART_HELPER_Setup_Rcv_mode( UART_HELPER_TypeDef * uarthelper, uint8_t rcv_mode )
{
/* 0 :polling, 1: IT 2: DMA*/
uarthelper->rcv_mode = rcv_mode;
return 0;
}
int UART_HELPER_Set_trans_GPIO( UART_HELPER_TypeDef * uarthelper, enable_func_gpio trans_gpio_func )
{
if (enable_snd_func != NULL) myuart->enable_snd = enable_snd_func;
uarthelper->enable_trans_gpio = trans_gpio_func;
return 0;
}
int Myuart_Set_enable_rcv_func( MYUART_TypeDef * myuart, enable_func enable_rcv_func)
int UART_HELPER_Set_trans_cplt_GPIO( UART_HELPER_TypeDef * uarthelper, enable_func_gpio trans_cplt_gpio_func )
{
if (enable_rcv_func != NULL) myuart->enable_rcv = enable_rcv_func;
// enable rcv
uarthelper->enable_trans_cplt_gpio = trans_cplt_gpio_func;
return 0;
}
int Myuart_Set_enable_idle( MYUART_TypeDef * myuart, uint8_t enable_idle)
int UART_HELPER_Set_enable_idle( UART_HELPER_TypeDef * uarthelper, uint8_t enable_idle)
{
myuart->enable_idle_it = enable_idle;
uarthelper->enable_idle_it = enable_idle;
return 0;
}
int Myuart_Set_Callback( MYUART_TypeDef * myuart, void *obj, callback_fun func)
int UART_HELPER_Set_Rcv_Buf( UART_HELPER_TypeDef * uarthelper, uint8_t * buf, uint16_t size)
{
myuart->obj = obj;
myuart->callback = func;
uarthelper->rcv_buf = buf;
uarthelper->rcv_size = size;
// uarthelper->rcv_record->buf = buf;
// uarthelper->rcv_record->size = size;
return 0;
}
int UART_HELPER_Set_Trans_Buf( UART_HELPER_TypeDef * uarthelper, uint8_t * buf, uint16_t size)
{
// uarthelper->trans_buf = buf;
// uarthelper->trans_size = size;
int Myuart_Start_Rcv(MYUART_TypeDef * myuart, uint8_t *buf, uint16_t size)
uarthelper->trans_record->buf = buf;
uarthelper->trans_record->size = size;
return 0;
}
int UART_HELPER_Set_Callback( UART_HELPER_TypeDef * uarthelper, void *obj, callback_fun func)
{
uarthelper->obj = obj;
uarthelper->callback = func;
return 0;
}
int UART_HELPER_Start_Rcv(UART_HELPER_TypeDef * uarthelper, uint8_t *buf, uint16_t size)
{
int st;
switch ( myuart->trans_mode)
switch ( uarthelper->rcv_mode)
{
case 0:
st = HAL_UART_Transmit(myuart->huart, buf, size , 0xFFFF);
st = HAL_UART_Receive(uarthelper->huart, buf, size , 0xFFFF);
return st;
break;
case 1:
st = HAL_UART_Transmit_IT(myuart->huart, buf, size);
st = HAL_UART_Receive_IT(uarthelper->huart, buf, size);
return st;
break;
case 2:
/* 会出现 st ==2 huart->gState != HAL_UART_STATE_READY */
if (myuart->enable_idle_it == 1)
if (uarthelper->enable_idle_it == 1)
{
__HAL_USART_ENABLE_IT(myuart->huart, UART_IT_IDLE);
// log_i("dma rcv idle en... ");
__HAL_UART_ENABLE_IT(uarthelper->huart, UART_IT_IDLE);
}
st = HAL_UART_Transmit_DMA(myuart->huart, buf, size);
st = HAL_UART_Receive_DMA(uarthelper->huart, buf, size);
// log_i("dma rcv ... ");
return st;
break;
default:
break;
}
return st;
}
int Myuart_Trans(MYUART_TypeDef * myuart, uint8_t *buf, uint16_t size)
int UART_HELPER_Trans(UART_HELPER_TypeDef * uarthelper, uint8_t *buf, uint16_t size )
{
int st;
if (myuart->enable_snd != NULL)
uarthelper->transferring = 1;
if (uarthelper->enable_trans_gpio != NULL)
{
myuart->enable_snd;
uarthelper->enable_trans_gpio();
}
switch ( myuart->trans_mode)
switch ( uarthelper->trans_mode)
{
case 0:
st = HAL_UART_Transmit(myuart->huart, buf, size , 0xFFFF);
st = HAL_UART_Transmit(uarthelper->huart, buf, size , 0xFFFF);
return st;
break;
case 1:
st = HAL_UART_Transmit_IT(myuart->huart, buf, size);
st = HAL_UART_Transmit_IT(uarthelper->huart, buf, size);
return st;
break;
case 2:
/* 会出现 st ==2 huart->gState != HAL_UART_STATE_READY */
st = HAL_UART_Transmit_DMA(myuart->huart, buf, size);
st = HAL_UART_Transmit_DMA(uarthelper->huart, buf, size);
return st;
break;
default:
break;
}
return 0;
return st;
}
int Myuart_Trans_TxCplt_Callback( MYUART_TypeDef * myuart )
int UART_HELPER_Trans_TxCplt_Callback( UART_HELPER_TypeDef * uarthelper )
{
if (myuart->enable_rcv != NULL)
// log_i( " uarthelper tc callback...........");
uarthelper->transferring = 0;
if (uarthelper->enable_trans_cplt_gpio != NULL)
{
myuart->enable_rcv;
// log_i( " uarthelper tc callback... not null........");
uarthelper->enable_trans_cplt_gpio();
}
return 0;
}
// void Myuart_Set_Sel_GPIO_Pin(MYUART_TypeDef * myuart, GPIO_TypeDef *gpio, uint16_t pin)
// {
// #if MAX3160_ENABLE
// myuart->sel_gpio = gpio;
// myuart->sel_pin = pin;
// #endif
// }
// void Myuart_Set_Dplx_GPIO_Pin(MYUART_TypeDef * myuart, GPIO_TypeDef *gpio, uint16_t pin)
// {
// #if MAX3160_ENABLE
// myuart->dplx_gpio = gpio;
// myuart->dplx_pin = pin;
// #endif
// }
// void Myuart_Set_DE485_GPIO_Pin(MYUART_TypeDef * myuart, GPIO_TypeDef *gpio, uint16_t pin)
// {
// #if MAX3160_ENABLE
// myuart->de485_gpio = gpio;
// myuart->de485_pin = pin;
// #endif
// #if MAX485_ENABLE
// myuart->de485_gpio = gpio;
// myuart->de485_pin = pin;
// #endif
// }
// void Myuart_Set_232(MYUART_TypeDef * myuart)
// {
// #if MAX3160_ENABLE
// myuart->sel = SEL_232;
// HAL_GPIO_WritePin( myuart->sel_gpio, myuart->sel_pin, myuart->sel );
// HAL_GPIO_WritePin( myuart->dplx_gpio, myuart->dplx_pin, DUPLEX_FULL );
// #endif
// }
// void Myuart_Set_485(MYUART_TypeDef * myuart)
// {
// #if MAX3160_ENABLE
// myuart->sel = SEL_485;
// HAL_GPIO_WritePin( myuart->sel_gpio, myuart->sel_pin, myuart->sel );
// HAL_GPIO_WritePin( myuart->dplx_gpio, myuart->dplx_pin, DUPLEX_HALF );
// HAL_GPIO_WritePin( myuart->de485_gpio, myuart->de485_pin, DISABLE_485_Trans );
// #endif
// }
// int __Myuart_Send(MYUART_TypeDef * myuart , uint8_t *buf, uint16_t size)
// {
// uint8_t st;
// switch ( myuart->trans_mode)
// {
// case 0:
// st = HAL_UART_Transmit(myuart->huart, buf, size , 0xFFFF);
// return st;
// break;
// case 1:
// st = HAL_UART_Transmit_IT(myuart->huart, buf, size);
// return st;
// break;
// case 2:
// /* 会出现 st ==2 huart->gState != HAL_UART_STATE_READY */
// st = HAL_UART_Transmit_DMA(myuart->huart, buf, size);
// return st;
// break;
// default:
// break;
// }
// return 0;
// }
int UART_HELPER_RCV_Cplt_Callback( UART_HELPER_TypeDef * uarthelper )
{
log_i( " UART_HELPER_RCV_Cplt_Callback ........");
// TODO
if (uarthelper->enable_idle_it == 0)
{
uarthelper->callback( uarthelper->obj, uarthelper->rcv_buf,uarthelper->rcv_size);
}
if (uarthelper->enable_idle_it == 1)
{
// idle 中断处理, do nothing
}
return 0;
}
int UART_HELPER_RCV_IDLE_Callback( UART_HELPER_TypeDef * uarthelper, uint16_t size )
{
// TODO
// if (uarthelper->rcv_mode == 0)
// {
// HAL_UART_DMAStop( uarthelper->huart );
// }
HAL_UART_DMAStop( uarthelper->huart );
uarthelper->callback( uarthelper->obj, uarthelper->rcv_buf, size);
UART_HELPER_Start_Rcv(uarthelper, uarthelper->rcv_buf, uarthelper->rcv_size);
return 0;
}

@ -5,7 +5,8 @@
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
#include "i2c.h"
// #include "i2c.h"
#include "bsp_i2c.h"
#define ADS1115_WRITE_ADDRESS 0x92
#define ADS1115_READ_ADDRESS 0x93
@ -15,27 +16,27 @@
/*事件开始 set, 事件完成 clear */
typedef enum
{
ADS1115_Event_READY, /*!< Startup finished. */
ADS1115_Event_INIT,
// ADS1115_Event_INIT_SUCCESS,
ADS1115_Event_READY, /*!< Startup finished. */
ADS1115_Event_TEST_SUCCESS,
ADS1115_Event_Get_Data,
ADS1115_Event_Get_Data_Convert_Register,
ADS1115_Event_Get_Data_RCV,
ADS1115_Event_TRANS_ONLY,
ADS1115_Event_TRANS_FOR_DATA,
ADS1115_Event_DATA_RCV,
ADS1115_Event_Get_Data_Set_Reg01,
ADS1115_Event_DATA_RCV_OK,
} ADS1115_Event_TypeDef;
typedef enum {
ADS1115_WORK_STATE_WAITING,
ADS1115_WORK_STATE_START,
ADS1115_WORK_STATE_INIT,
ADS1115_WORK_STATE_TEST,
ADS1115_WORK_STATE_TESTING,
ADS1115_WORK_STATE_TEST_OK,
ADS1115_WORK_STATE_GET_DATA_READY,
ADS1115_WORK_STATE_GET_DATA_START,
ADS1115_WORK_STATE_GET_DATA,
ADS1115_WORK_STATE_GET_DATA_Convert_Register,
ADS1115_WORK_STATE_GET_DATA_SET_REG01,
ADS1115_WORK_STATE_GET_DATA_SET_REG01_Wait,
// ADS1115_WORK_STATE_GET_DATA_Convert_Register,
ADS1115_WORK_STATE_GET_DATA_RCV,
ADS1115_WORK_STATE_GET_DATA_FLOAT,
ADS1115_WORK_STATE_GET_DATA_RCV_Wait,
// ADS1115_WORK_STATE_GET_DATA_FLOAT,
ADS1115_WORK_STATE_GET_DATA_OK,
ADS1115_WORK_STATE_ERROR,
ADS1115_WORK_STATE_STOP
@ -146,13 +147,15 @@ typedef enum
ADS1115_REG_CONFIG_COMP_QUE_DIS= (0x3U << 0)
} ADS1115_REG_CONFIG_COMP_QUE_TypeDef;
extern I2C_HELPER_TypeDef *ads1115_i2c_helper;
typedef struct{
int (*init)(void);
void (*port)(void);
int (*test)(void);
void (*start)(void);
I2C_HandleTypeDef *hi2c;
void (*stop)(void);
// I2C_HandleTypeDef *hi2c;
uint8_t config_os;
uint8_t config_mux;
@ -169,19 +172,39 @@ typedef struct{
uint8_t reg_data[3];
ADS1115_OUTPUT_UNIT_TypeDef unit;
uint16_t result_int16; // 多组可用于
double coeff; // 放大1000倍数
double voltage_mv;
volatile ADS1115_WORK_STATE_TypeDef state;
uint8_t event_flag;
}ADS1115_TypeDef;
extern ADS1115_TypeDef my_ads1115;
static void ads1115_get_reg_data(void)
{
my_ads1115.config_H = my_ads1115.config_os|my_ads1115.config_mux
|my_ads1115.config_PGA|my_ads1115.config_MODE;
my_ads1115.config_L = my_ads1115.config_DR|my_ads1115.config_COMP_MODE|my_ads1115.config_COMP_POL
|my_ads1115.config_COMP_LAT|my_ads1115.config_COMP_QUE ;
// my_ads1115.reg_data[0] = 0x01; // point addr 8bit 配置寄存器
my_ads1115.reg_data[0] = my_ads1115.config_H; // config_reg 高位
my_ads1115.reg_data[1] = my_ads1115.config_L; // config_reg 低位
}
int ads1115_init(void);
void ads1115_port(void);
int ads1115_test(void);
void ads1115_start(void);
void ads1115_stop(void);
int ads1115_setup(ADS1115_REG_CONFIG_MUX_Diff_TypeDef mux);
// void ads1115_config_register(I2C_HandleTypeDef *ads1115_I2cHandle,uint8_t pointADD,uint8_t configH,uint8_t configL);
int16_t ads1115_read_data( );
double ads1115_get_coeff( );
double ads1115_get_voltage_val( );
void ads1115_get_data_start(void);
void ads1115_callback(ADS1115_TypeDef *pAds1115, uint8_t *buf, uint16_t size);
#endif
/***************/

@ -1,118 +0,0 @@
#ifndef __I2C_HELPER_H
#define __I2C_HELPER_H
/**
* Master模式
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "i2c.h"
#include <stdlib.h>
/* 回调函数,将数据返回上层 */
typedef void (*it_callback)(void *obj,uint8_t *buf, uint32_t buf_size );
/* 由于可能存在多个外设,考虑将 事件类型封装在helper, 事件定义初始化不必放这里*/
typedef enum
{
I2C_HELPER_Event_READY, /*!< Startup finished. */
I2C_HELPER_Event_INIT,
I2C_HELPER_Event_INIT_SUCCESS,
I2C_HELPER_Event_TRANS_ONLY,
I2C_HELPER_Event_TRANS_FOR_DATA,
I2C_HELPER_Event_DATA_RCV,
} I2C_HELPER_Event_TypeDef;
typedef enum
{
I2C_HELPER_TRANS_POLLING,
I2C_HELPER_TRANS_IT,
I2C_HELPER_TRANS_DMA
}I2C_HELPER_TRANS_TypeDef;
typedef enum
{
I2C_HELPER_RCV_POLLING,
I2C_HELPER_RCV_IT,
I2C_HELPER_RCV_DMA
}I2C_HELPER_RCV_TypeDef;
typedef enum
{
I2C_HELPER_MODE_MASTER,
I2C_HELPER_MODE_SLAVE
}I2C_HELPER_MODE_TypeDef;
typedef struct
{
I2C_HandleTypeDef *hi2c;
uint8_t i2c_write_address;
uint8_t i2c_read_address;
I2C_HELPER_TRANS_TypeDef trans_type; /* 0 :polling, 1: IT 2: DMA*/
I2C_HELPER_RCV_TypeDef rcv_type; /* 0 :polling, 1: IT 2: DMA*/
I2C_HELPER_MODE_TypeDef mode;
uint8_t *trans_buf;
volatile uint32_t trans_size;
volatile uint32_t trans_data_con;
uint8_t *receive_buf;
volatile uint32_t receive_size;
volatile uint8_t receive_data_con;
// volatile uint8_t receive_buf_con;
// uint32_t receive_buf_half_len;
// uint8_t half_flag; /*0 : 未半满, 1:半满, 2: 半满已经处理*/
// volatile Uart_Status_TypeDef status; /* 发送状态 0: ready , 1:正在发送 busy,2:发送ok,*/
void * obj;
it_callback callback;
}I2CHelper_TypeDef;
I2CHelper_TypeDef * I2CHelper_Init( );
void I2CHelper_Set_Hi2c( I2CHelper_TypeDef *i2chelper, I2C_HandleTypeDef * hi2c );
void I2CHelper_Set_Mode( I2CHelper_TypeDef *i2chelper, I2C_HELPER_MODE_TypeDef mode );
void I2CHelper_Set_Trans_Type( I2CHelper_TypeDef *i2chelper, I2C_HELPER_TRANS_TypeDef trans_type );
void I2CHelper_Set_Rcv_Type( I2CHelper_TypeDef *i2chelper, I2C_HELPER_RCV_TypeDef rcv_type );
// void I2CHelper_Set_timeout( I2CHelper_TypeDef *i2chelper, uint32_t timeout_ms );
int I2CHelper_Set_callback_func_obj( I2CHelper_TypeDef *i2chelper, void * obj, it_callback callback );
void I2CHelper_Set_rcv_buf( I2CHelper_TypeDef *i2chelper, uint8_t *buf, uint32_t buf_size );
void I2CHelper_Set_Trans_Buf( I2CHelper_TypeDef *i2chelper, uint8_t *buf, uint32_t buf_size );
int I2CHelper_Write( I2CHelper_TypeDef *i2chelper, uint8_t * buf, uint16_t size);
int I2CHelper_Read(I2CHelper_TypeDef *i2chelper, uint8_t * buf, uint16_t size);
void I2CHelper_Snd_Cplt_Callback( );
void I2CHelper_Rcv_Cplt_Callback( );
// int I2CHelper_Flags_Set(I2CHelper_TypeDef *i2chelper, I2C_HELPER_Event_TypeDef evt_type );
// int I2CHelper_Flags_Clear(I2CHelper_TypeDef *i2chelper, I2C_HELPER_Event_TypeDef evt_type );
// int I2CHelper_Flags_Wait(I2CHelper_TypeDef *i2chelper, I2C_HELPER_Event_TypeDef evt_type );
// void I2CHelper_Set_GPIO_For_Trans_Cplt( I2CHelper_TypeDef *i2chelper );
// void I2CHelper_Rcv_Cplt_Callback( I2CHelper_TypeDef *i2chelper );
// void I2CHelper_Rcv_Idle_Callback( I2CHelper_TypeDef *i2chelper );
// void I2CHelper_Rcv_DMA_Half_Callback( I2CHelper_TypeDef *i2chelper );
int I2CHelper_copy( I2CHelper_TypeDef *i2chelper, uint8_t *buf, uint32_t buf_size );
int I2CHelper_isbusy( I2CHelper_TypeDef *i2chelper );
void I2CHelper_error( I2CHelper_TypeDef *i2chelper );
// uint16_t Get_Crc16(const char *buf, uint16_t len);
// uint8_t Check_Crc16(uint8_t *buf, uint16_t length);
#ifdef __cplusplus
}
#endif
#endif

@ -5,154 +5,135 @@
extern "C" {
#endif
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
#include "usart.h"
#include "elog.h"
#ifdef __cplusplus
extern "C" {
#endif
#include "elog.h"
#include <string.h>
#include "uart_helper.h"
#include "uart_interface.h"
// #include "config.h"
#include "bsp_uart.h"
#define PH_USE_MAX485 0
#define PH_USE_MAX3160_232 0
#define PH_USE_MAX3160_485 1
#define PH_Rcv_Buf_Size 9
#define PH_Command_Number 1
static void PH_MAX3160_232(void)
{
HAL_GPIO_WritePin(SEL_232_485_GPIO_Port,SEL_232_485_Pin, RESET);
HAL_GPIO_WritePin(HDPLX_GPIO_Port,HDPLX_Pin, RESET);
}
typedef enum
{
PH_Status_Waiting ,
PH_Status_Ready ,
PH_Status_Sending ,
PH_Status_Send_Cplt ,
PH_Status_RCV_OK ,
PH_Status_DATA_OK ,
PH_Status_Timeout ,
PH_Status_Error ,
} PH_Status_TypeDef;
PH_Event_Ready ,
PH_Event_Test,
PH_Event_Test_OK ,
PH_Event_Get_Data,
PH_Event_Check_Data ,
} Module_PH_Event_TypeDef;
typedef enum
{
PH_State_Waiting ,
PH_State_Test ,
PH_State_Test_Start ,
PH_State_Testing ,
PH_State_Test_OK ,
PH_State_Ready ,
PH_State_Get_DATA ,
PH_State_Get_DATA_Wait ,
PH_State_Get_DATA_OK,
PH_Statee_Get_DATA_Check ,
PH_State_Stop ,
PH_State_Timeout ,
PH_State_Error ,
} Module_PH_State_TypeDef;
extern UART_HELPER_TypeDef *ph_uart_helper;
typedef struct
{
UartHelper_TypeDef *uarthelper;
// UART_HandleTypeDef *huart;
uint8_t interface_type; /* 0: common, 1: 485 ,2:3160*/
uint8_t *send_buf;
uint16_t size_send;
uint8_t *rcv_buf;
uint16_t size_rcv;
uint8_t mode_232_485; /* 0: common, 1: 485 */
uint8_t trans_type; /* 0 :polling, 1: IT 2: DMA*/
uint8_t rcv_type; /* 0 :polling, 1: IT 2: DMA*/
uint8_t idle_enable_disable; /* 0 不开启空闲中断 , 1 开启空闲中断 */
uint8_t trans_mode; /* 0 :polling, 1: IT 2: DMA*/
uint64_t timebase;
uint16_t timeout_ms;
volatile uint8_t send_flag; /* 发送标志位 */
Uart_Status_TypeDef send_status; /* 发送状态 1:正在发送 busy,0:发送ok,*/
Uart_Status_TypeDef status; /* 发送状态 1:正在发送 busy,0:发送ok,*/
uint8_t command_seq ; /* 命令序号 */
uint8_t mode; /* 0 : normal , 1 : get wavelength , 2 : get sn */
uint8_t vipersn_buf[15];
uint8_t PH_Wavelength_Buf[2][ PH_Rcv_Buf_Size ];
uint8_t PH_Data_Buf[2][ PH_Rcv_Buf_Size ];
uint8_t rcv_ok; /* 接收数据完整可以处理 */
uint8_t data_begin_flag; /* 接收数据完整可以处理 */
int (*init)(void);
void (*port)(void);
int (*test)(void);
void (*start)(void);
void (*stop)(void);
osMessageQueueId_t cmdQueue; // 队列可选
volatile Module_PH_State_TypeDef state;
uint8_t data_ok; /* 接收数据完整可以处理 */
volatile PH_Status_TypeDef state;
uint8_t result_buf[1024];
uint8_t size_received;
uint64_t timebase_ticks;
uint64_t timeout_ticks;
uint8_t event_flag;
// int (*Init)(void); //初始化触摸屏控制器
// // uint8_t (*Setup)( ); //扫描触摸屏.0,屏幕扫描;1,物理坐标;
// uint8_t (*Port)( ); //扫描触摸屏.0,屏幕扫描;1,物理坐标;
// uint8_t command_seq ; /* 命令序号 */
// uint8_t mode; /* 0 : normal , 1 : get wavelength , 2 : get sn */
// uint8_t vipersn_buf[15];
// uint8_t PH_Wavelength_Buf[2][ PH_Rcv_Buf_Size ];
// uint8_t PH_Data_Buf[2][ PH_Rcv_Buf_Size ];
}PH_TypeDef;
extern PH_TypeDef *ph ;
extern PH_TypeDef ph ;
int PH_Init( );
int PH_Init( void );
void PH_Task( void);
void PH_Port( void);
int PH_Test( );
void PH_Start( );
void PH_Stop( );
void PH_Set_Uarthelper( PH_TypeDef *ph, UartHelper_TypeDef * uarthelper );
void PH_Set_Interface( PH_TypeDef *ph, UartInterface_TypeDef * interface );
void PH_Set_Huart( PH_TypeDef *ph, UART_HandleTypeDef * huart );
void PH_Set_Interface_Type( PH_TypeDef *ph, Uart_Interface_Type_Typedef interface_type );
void PH_Set_Trans_Type( PH_TypeDef *ph, Uart_Transmode_TypeDef trans_type );
void PH_Set_Rcv_Type( PH_TypeDef *ph, Uart_Transmode_TypeDef rcv_type );
void PH_Set_Idle_Enable( PH_TypeDef *ph, Uart_IDLE_Enable_TypeDef idle_enable_disable );
void PH_Set_TransMode( PH_TypeDef *ph, Uart_Transmode_TypeDef trans_mode );
void PH_Set_RsMode_232_485( PH_TypeDef *ph, Uart_RS_Mode_TypeDef rs_232_485 );
void PH_Set_Timeout( PH_TypeDef *ph, uint16_t timeout_ms );
void PH_Set_Sendbuf( PH_TypeDef *ph, uint8_t * buf, uint16_t size );
void PH_Set_Rcvbuf( PH_TypeDef *ph, uint8_t * buf, uint16_t size );
void PH_Set_Sendbuf( uint8_t * buf, uint16_t size );
void PH_Set_Rcvbuf( uint8_t * buf, uint16_t size );
void PH_Set_Timeout_ms( uint64_t ms_ticks );
int PH_Validate( );
int PH_Transmit( PH_TypeDef *ph, uint8_t * buf, uint16_t size);
int PH_Transmit();
int PH_Receive();
void PH_Begin_Rcv(PH_TypeDef *ph, uint8_t * buf, uint16_t size);
void PH_Trans_GPIO(void) ;
extern void PH_Rcv_Cplt_Callback( PH_TypeDef *ph );
extern void PH_Send_Cplt_Callback( PH_TypeDef *ph );
void PH_Trans_Cplt_GPIO(void) ;
int PH_Get_Data_OK(PH_TypeDef *ph );
void PH_Set_Send_Flag( PH_TypeDef *ph );
int PH_CallBack( PH_TypeDef *pPH, uint8_t *buf, uint16_t size );
// extern void PH_Setup( );
extern void PH_Port( );
// extern void PH_SN( );
// extern void PH_Wavelength( );
void PH_Task( );
/* 再封装演示 */
typedef struct
{
uint8_t (*init)( );
uint8_t (*port)(void);
int (*test)(void);
void (*start)(void);
int (*stop)(void);
//扫描触摸屏.0,屏幕扫描;1,物理坐标;
GPIO_TypeDef *sel_gpio;
GPIO_TypeDef *dplx_gpio;
GPIO_TypeDef *de_gpio;
uint16_t sel_pin;
uint16_t dplx_pin;
uint16_t de_pin;
Uart_Interface_Type_Typedef interface_type; /* 0: common, 1: 485 ,2:3160*/
Uart_RS_Mode_TypeDef mode_232_485; /* 0 commome 1:485*/
Uart_Transmode_TypeDef trans_type; /* 0 :polling, 1: IT 2: DMA*/
Uart_Transmode_TypeDef rcv_type; /* 0 :polling, 1: IT 2: DMA*/
Uart_IDLE_Enable_TypeDef idle_enable_disable; /* 0 :不启用空闲 , 1: 启用空闲*/
uint16_t ph_val;
uint16_t temp_val;
PH_TypeDef *ph; /*不是常量 不能直接初始化*/
}My_PH_TypeDef;
extern My_PH_TypeDef my_pH ;
#ifdef __cplusplus
}
#endif
#endif
/*
ph.init();
ph.port();
ph.test();
// uint64_t ticks = osKernelGetTickCount();
// while ( ph.state != PH_State_Ready )
// {
// if ( ( osKernelGetTickCount() -ticks) > 5000 )
// {
// log_e(" pH test error.... %d", ph.state) ;
// break;
// }
// }
osDelay(1000);
log_w(" pH test ok.... %d", ph.state) ;
*/

@ -0,0 +1,165 @@
/*
*/
#ifndef __TEMPERATURE_H
#define __TEMPERATURE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
#include "usart.h"
#include "elog.h"
#include <string.h>
#include "bsp_uart.h"
#define TEMP_USE_MAX485 0
#define TEMP_USE_MAX3160_232 0
#define TEMP_USE_MAX3160_485 1
#define TEMP_Rcv_Buf_Size 9
#define TEMP_Command_Number 1
static void TEMP_MAX3160_232(void)
{
HAL_GPIO_WritePin(SEL_232_485_GPIO_Port,SEL_232_485_Pin, RESET);
HAL_GPIO_WritePin(HDPLX_GPIO_Port,HDPLX_Pin, RESET);
}
typedef enum
{
TEMP_Event_Ready ,
TEMP_Event_Test,
TEMP_Event_Test_OK ,
TEMP_Event_Get_Data,
TEMP_Event_Check_Data ,
} Module_TEMP_Event_TypeDef;
typedef enum
{
TEMP_State_Waiting ,
TEMP_State_Test ,
TEMP_State_Test_Start ,
TEMP_State_Testing ,
TEMP_State_Test_OK ,
TEMP_State_Ready ,
TEMP_State_Get_DATA ,
TEMP_State_Get_DATA_Wait ,
TEMP_State_Get_DATA_OK,
TEMP_State_Get_DATA_Check ,
TEMP_State_Stop ,
TEMP_State_Timeout ,
TEMP_State_Error ,
} Module_TEMP_State_TypeDef;
extern UART_HELPER_TypeDef *temp_uart_helper;
typedef struct
{
int (*init)(void);
void (*port)(void);
int (*test)(void);
void (*start)(void);
void (*stop)(void);
osMessageQueueId_t transQueue; // 队列可选
volatile Module_TEMP_State_TypeDef state;
uint8_t data_ok; /* 接收数据完整可以处理 */
uint8_t result_buf[1024];
uint8_t size_received;
uint64_t timebase_ticks;
uint64_t timeout_ticks;
uint8_t event_flag;
Record_Trans_Rcv_TypeDef transRecord[3];
// uint64_t timebase;
// uint16_t timeout_ms;
// volatile uint8_t send_flag; /* 发送标志位 */
// Uart_Status_TypeDef send_status; /* 发送状态 1:正在发送 busy,0:发送ok,*/
// Uart_Status_TypeDef status; /* 发送状态 1:正在发送 busy,0:发送ok,*/
// uint8_t command_seq ; /* 命令序号 */
// uint8_t mode; /* 0 : normal , 1 : get wavelength , 2 : get sn */
// uint8_t vipersn_buf[15];
// uint8_t TEMP_Wavelength_Buf[2][ TEMP_Rcv_Buf_Size ];
// uint8_t TEMP_Data_Buf[2][ TEMP_Rcv_Buf_Size ];
}TEMP_TypeDef;
extern TEMP_TypeDef temp ;
int TEMP_Init( void );
void TEMP_Task( void);
void TEMP_Port( void);
int TEMP_Test( );
void TEMP_Start( );
void TEMP_Stop( );
void TEMP_Set_Sendbuf( uint8_t * buf, uint16_t size );
void TEMP_Set_Rcvbuf( uint8_t * buf, uint16_t size );
void TEMP_Set_Timeout_ms( uint64_t ms_ticks );
int TEMP_Validate( );
int TEMP_Transmit();
int TEMP_Receive();
void TEMP_Trans_GPIO(void) ;
void TEMP_Trans_Cplt_GPIO(void) ;
int TEMP_CallBack( TEMP_TypeDef *pPH, uint8_t *buf, uint16_t size );
#ifdef __cplusplus
}
#endif
#endif
/*
temp.init();
temp.port();
temp.test();
uint64_t ticks = osKernelGetTickCount();
while ( temp.state != PH_State_Ready )
{
if ( ( osKernelGetTickCount() -ticks) > 3000 )
{
log_e(" pH test error.... ") ;
break;
}
}
log_w(" pH test ok.... ") ;
// osMessageQueueId_t TestQQueueueueHandle;
// osMessageQueueId_t osMessageQueueNew (uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr);
// osMessageQueueGet(TestQueueHandle,osWaitForever);
// osStatus_t osMessageQueueGet ( osMessageQueueId_t mq_id,
// void * msg_ptr, //储存读取结果的变量地址
// uint8_t * msg_prio, // ==NULL
// uint32_t timeout //阻塞超时时间
// );
// osStatus_t osMessageQueuePut ( osMessageQueueId_t mq_id,
// const void * msg_ptr, //储存写入内容的变量地址
// uint8_t msg_prio, //==0U
// uint32_t timeout //阻塞超时时间
// );
*/

@ -1,89 +0,0 @@
#ifndef __UART_HELPER_H
#define __UART_HANDLE_H
/**
* uart
* ?? buf?
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "uart_interface.h"
/* 回调函数,将数据返回上层 */
typedef void (*it_callback)(void *obj,uint8_t *buf, uint32_t buf_size );
// typedef struct uart_tx_record
// {
// uint32_t size;
// uint8_t *buf;
// } Uart_Trans_Record_TypeDef;
typedef struct
{
UART_HandleTypeDef *huart;
UartInterface_TypeDef *interface;
uint8_t interface_type; /* 0: common, 1: 485 ,2:3160*/
uint8_t mode_232_485; /* 0: common, 1: 485 */
uint8_t trans_type; /* 0 :polling, 1: IT 2: DMA*/
uint8_t rcv_type; /* 0 :polling, 1: IT 2: DMA*/
uint8_t idle_enable_disable; /* 0 不开启空闲中断 , 1 开启空闲中断 */
uint8_t trans_mode; /* 0 :polling, 1: IT 2: DMA*/
volatile uint32_t trans_size;
uint8_t *trans_buf;
volatile uint8_t receive_buf_con;
// volatile uint8_t transferring_flag;
volatile uint32_t receive_data_size;
uint8_t *receive_buf;
uint32_t receive_buf_half_len;
uint8_t half_flag; /*0 : 未半满, 1:半满, 2: 半满已经处理*/
volatile Uart_Status_TypeDef status; /* 发送状态 0: ready , 1:正在发送 busy,2:发送ok,*/
void * obj;
it_callback callback;
}UartHelper_TypeDef;
UartHelper_TypeDef * UartHelper_Init( );
void UartHelper_Set_Interface( UartHelper_TypeDef *uarthelper, UartInterface_TypeDef * interface );
void UartHelper_Set_Huart( UartHelper_TypeDef *uarthelper, UART_HandleTypeDef * huart );
void UartHelper_Set_Interface_Type( UartHelper_TypeDef *uarthelper, Uart_Interface_Type_Typedef interface_type );
void UartHelper_Set_Trans_Type( UartHelper_TypeDef *uarthelper, Uart_Transmode_TypeDef trans_type );
void UartHelper_Set_Rcv_Type( UartHelper_TypeDef *uarthelper, Uart_Transmode_TypeDef rcv_type );
void UartHelper_Set_Idle_Enable( UartHelper_TypeDef *uarthelper, Uart_IDLE_Enable_TypeDef idle_enable_disable );
void UartHelper_Set_TransMode( UartHelper_TypeDef *uarthelper, Uart_Transmode_TypeDef trans_mode );
void UartHelper_Set_RsMode_232_485( UartHelper_TypeDef *uarthelper, Uart_RS_Mode_TypeDef mode_232_485 );
// void UartHelper_Set_timeout( UartHelper_TypeDef *uarthelper, uint32_t timeout_ms );
int UartHelper_Set_callback_func_obj( UartHelper_TypeDef *uarthelper, void * obj, it_callback callback );
void UartHelper_Set_rcv_buf( UartHelper_TypeDef *uarthelper, uint8_t *buf, uint32_t buf_size );
void UartHelper_Set_Trans_Buf( UartHelper_TypeDef *uarthelper, uint8_t *buf, uint32_t buf_size );
int UartHelper_Transmit( UartHelper_TypeDef *uarthelper, uint8_t * buf, uint16_t size);
int UartHelper_Begin_Rcv(UartHelper_TypeDef *uarthelper, uint8_t * buf, uint16_t size);
void UartHelper_Set_GPIO_For_Trans_Cplt( UartHelper_TypeDef *uarthelper );
void UartHelper_Rcv_Cplt_Callback( UartHelper_TypeDef *uarthelper );
void UartHelper_Rcv_Idle_Callback( UartHelper_TypeDef *uarthelper );
void UartHelper_Rcv_DMA_Half_Callback( UartHelper_TypeDef *uarthelper );
int UartHelper_copy(UartHelper_TypeDef *uarthelper,uint8_t *buf,uint32_t buf_size);
int UartHelper_isbusy(UartHelper_TypeDef *uarthelper );
void UartHelper_error(UartHelper_TypeDef *uarthelper );
uint16_t Get_Crc16(const char *buf, uint16_t len);
uint8_t Check_Crc16(uint8_t *buf, uint16_t length);
#ifdef __cplusplus
}
#endif
#endif

@ -1,153 +0,0 @@
#ifndef __UART_INTERFACE_H
#define __UART_INTERFACE_H
/**
* 使 232 485 max3160
* GPIO操作
*/
/* 485 3160 启用一个, 两个接口都有 MAX3160_ENABLE */
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include "main.h"
#define MAX485_ENABLE 0
#define MAX3160_ENABLE 1
typedef enum
{
Uart_Status_Ready = 0x00U,
Uart_Status_Sending = 0x01U,
Uart_Status_Send_Cplt = 0x02U,
Uart_Status_Timeout = 0x10U,
Uart_Status_Error = 0x11U,
} Uart_Status_TypeDef;
typedef enum
{
Uart_Interface_Default = 0x00U,
Uart_Interface_485 = 0x01U,
Uart_Interface_Max3160 = 0x02U,
} Uart_Interface_Type_Typedef;
typedef enum
{
Uart_Trans_Polling = 0x00U,
Uart_Trans_IT = 0x01U,
Uart_Trans_DMA = 0x02U,
} Uart_Transmode_TypeDef;
typedef enum
{
Uart_RSMODE_232 = 0x00U,
Uart_RSMODE_485 = 0x01U,
} Uart_RS_Mode_TypeDef;
typedef enum
{
Uart_IDLE_IT_DISABLE = 0x00U,
Uart_IDLE_IT_ENABLE = 0x01U,
} Uart_IDLE_Enable_TypeDef;
#if MAX3160_ENABLE
typedef enum{
SEL_232 = 0,
SEL_485 = 1,
}Max3160_Sel_TypeDef;
typedef enum{
DUPLEX_FULL = 0,
DUPLEX_HALF = 1,
}Max3160_Duplex_TypeDef; /* 发送时切换全双工(拉低),否则回显, 接收为半双工(拉高 默认)*/
typedef enum{
DISABLE_485_SEND = 0,
ENABLE_485_SEND = 1,
}Max3160_DE_TypeDef;
# endif
#if MAX485_ENABLE
typedef enum{
DISABLE_485_SEND = 0,
ENABLE_485_SEND = 1,
}Max_DE_TypeDef;
# endif
typedef struct
{
// UART_HandleTypeDef *huart;
uint8_t interface_type; /* 0: common, 1: 485 ,2:3160*/
uint8_t mode_232_485; /* 0: common, 1: 485 */
#if MAX485_ENABLE
GPIO_TypeDef *de485_gpio;
uint16_t de485_pin;
uint8_t de485;
#endif
#if MAX3160_ENABLE
GPIO_TypeDef *de485_gpio;
uint16_t de485_pin;
GPIO_TypeDef *sel_gpio;
GPIO_TypeDef *dplx_gpio;
uint16_t sel_pin;
uint16_t dplx_pin;
uint8_t sel;
uint8_t dplx;
#endif
uint8_t trans_mode; /* 0 :polling, 1: IT 2: DMA*/
}UartInterface_TypeDef;
// extern UartInterface_TypeDef *uart_helper;
UartInterface_TypeDef * UartInterface_Init( );
int UartInterface_Setup_Mode_232_485( UartInterface_TypeDef * interface, Uart_RS_Mode_TypeDef mode_232_485 );
int UartInterface_Setup_Transmod( UartInterface_TypeDef * interface, Uart_Transmode_TypeDef trans_mode );
int UartInterface_Setup_Interface_type( UartInterface_TypeDef * interface, Uart_Interface_Type_Typedef interface_type );
// int UartInterface_Setup_Woek_Mode( UartInterface_TypeDef * interface, uint8_t rs_232_485 );
void UartInterface_Set_Sel_GPIO_Pin(UartInterface_TypeDef * interface, GPIO_TypeDef *gpio, uint16_t pin);
void UartInterface_Set_Dplx_GPIO_Pin(UartInterface_TypeDef * interface, GPIO_TypeDef *gpio, uint16_t pin);
void UartInterface_Set_DE485_GPIO_Pin(UartInterface_TypeDef * interface, GPIO_TypeDef *gpio, uint16_t pin);
void UartInterface_Set_232(UartInterface_TypeDef * interface);
void UartInterface_Set_485(UartInterface_TypeDef * interface);
int UartInterface_Set_GPIO_For_Transmit( UartInterface_TypeDef * interface );
int UartInterface_Set_GPIO_For_Trans_Cplt( UartInterface_TypeDef * interface );
/* 接收方式 也有三种类型 */
// int UartInterface_Start_Rcv(UartInterface_TypeDef * interface, uint8_t *buf, uint16_t size);
// void UartInterface_Set_Huart(UartInterface_TypeDef * interface, UART_HandleTypeDef *huart);
// int UartInterface_Send(UartInterface_TypeDef * interface, uint8_t *buf, uint16_t size);
// int __UartInterface_Send(UartInterface_TypeDef * interface , uint8_t *buf, uint16_t size);
#ifdef __cplusplus
}
#endif
#endif
/**
// int UartInterface_Send(UartInterface_TypeDef * interface, uint8_t *buf, uint16_t size);
// int __UartInterface_Send(UartInterface_TypeDef * interface , uint8_t *buf, uint16_t size);
// int UartInterface_Send_TxCplt_Callback( UartInterface_TypeDef * interface );
// int UartInterface_Send_Cplt(UartInterface_TypeDef * interface);
// void UartInterface_Set_tc_flag(UartInterface_TypeDef * interface);
*/

@ -22,31 +22,20 @@ const osEventFlagsAttr_t ads1115Event_attributes = {
.name = "ads1115Event"
};
uint16_t resultbuf[1] = {0};
double result_double = 0;
// ads1115EventHandle = osEventFlagsNew (&ads1115Event_attributes);
// typedef enum
// {
// EV_READY, /*!< Startup finished. */
// EV_FRAME_RECEIVED, /*!< Frame received. */
// EV_EXECUTE, /*!< Execute function. */
// EV_FRAME_SENT /*!< Frame sent. */
// } eMBEventType;
// osEventFlagsSet(modbusEventHandle,eEvent);
// osEventFlagsClear (osEventFlagsId_t ef_id, uint32_t flags)
// recvedEvent = osEventFlagsWait (modbusEventHandle,
// EV_READY | EV_FRAME_RECEIVED | EV_EXECUTE |
// EV_FRAME_SENT, /* 接收任务感兴趣的事件 */
// 0,
// portMAX_DELAY); /* 指定超时事件,无限等待 */
I2C_HELPER_TypeDef *ads1115_i2c_helper;
// uint16_t resultbuf[1] = {0};
// double result_double = 0;
ADS1115_TypeDef my_ads1115 =
{
ads1115_init,
ads1115_port,
ads1115_get_data_start,
NULL,
ads1115_test,
ads1115_start,
ads1115_stop,
// NULL,
ADS1115_REG_CONFIG_OS_START,
ADS1115_REG_CONFIG_MUX_SINGLE_0,
ADS1115_REG_CONFIG_PGA_4,
@ -62,14 +51,18 @@ ADS1115_TypeDef my_ads1115 =
{ 0, 0, 0 },
ADS1115_OUTPUT_UNIT_mv, // 默认输出毫伏
0,
0.0,
0.0, // coeff 放大1000
0.0, // voltage——mv
ADS1115_WORK_STATE_WAITING,
0,
};
void ads1115_task(void)
{
uint8_t rx_data[2] = {0};
uint32_t event_flags = 0;
my_ads1115.event_flag = 0;
uint64_t ticks;
int err_con = 0;
int st;
for ( ; ; )
@ -83,270 +76,141 @@ void ads1115_task(void)
case ADS1115_WORK_STATE_START:
break;
case ADS1115_WORK_STATE_INIT:
log_e( " ADS1115 init ********** %d" ,pADS1115_I2C->State );
my_ads1115.config_H = my_ads1115.config_os|my_ads1115.config_mux
|my_ads1115.config_PGA|my_ads1115.config_MODE;
my_ads1115.config_L = my_ads1115.config_DR|my_ads1115.config_COMP_MODE|my_ads1115.config_COMP_POL
|my_ads1115.config_COMP_LAT|my_ads1115.config_COMP_QUE ;
case ADS1115_WORK_STATE_TEST:
log_e( " ADS1115 test ********** %d" ,pADS1115_I2C->State );
ads1115_get_reg_data( );
my_ads1115.reg_data[0] = 0x01; // point addr 8bit 配置寄存器
my_ads1115.reg_data[1] = my_ads1115.config_H; // config_reg 高位
my_ads1115.reg_data[2] = my_ads1115.config_L; // config_reg 低位
// while( HAL_I2C_Master_Transmit(pADS1115_I2C, ADS1115_WRITE_ADDRESS, my_ads1115.reg_data, 3 ,1000)!= HAL_OK)
// {
// if (HAL_I2C_GetError(pADS1115_I2C) != HAL_I2C_ERROR_AF)
// {
// log_w("ads1115 0x01 Reg error!!!\r\n");
// }
// }
st = HAL_I2C_Master_Transmit(pADS1115_I2C, ADS1115_WRITE_ADDRESS, my_ads1115.reg_data, 3 ,1000);
// st = HAL_I2C_Master_Transmit_IT(pADS1115_I2C, ADS1115_WRITE_ADDRESS, my_ads1115.reg_data, 3 );
I2C_HELPER_Set_mem_addr( ads1115_i2c_helper, 0x01 );
st = I2C_HELPER_Trans( ads1115_i2c_helper, my_ads1115.reg_data, 2 );
if ( st != HAL_OK)
{
log_w( " ADS1115 init ********** trans error " );
log_w( " ADS1115 init ********** trans error " );
my_ads1115.state = ADS1115_WORK_STATE_ERROR;;
break;
}
my_ads1115.state =ADS1115_WORK_STATE_GET_DATA_READY;
// event_flags = osEventFlagsWait ( ads1115EventHandle, ADS1115_Event_INIT, 0 , portMAX_DELAY );
// if (event_flags && ADS1115_Event_INIT)
// {
// my_ads1115.state =ADS1115_WORK_STATE_READY_GET_DATA;
// }
ticks = osKernelGetTickCount();
err_con = 0;
my_ads1115.state = ADS1115_WORK_STATE_TESTING;
continue;
case ADS1115_WORK_STATE_GET_DATA_READY:
// log_w( " ADS1115 Ready ********** %d" ,pADS1115_I2C->State );
osDelay(20);
break;
case ADS1115_WORK_STATE_GET_DATA_START:
log_w( " ADS1115 GET_DATA_START ********** %d" ,pADS1115_I2C->State );
my_ads1115.result_int16 = 0;
my_ads1115.voltage_mv = 0.0;
while( HAL_I2C_Master_Transmit(pADS1115_I2C, ADS1115_WRITE_ADDRESS, my_ads1115.reg_data, 3 ,1000)!= HAL_OK)
case ADS1115_WORK_STATE_TESTING:
if ( osKernelGetTickCount() - ticks > 1000 )
{
if (HAL_I2C_GetError(pADS1115_I2C) != HAL_I2C_ERROR_AF)
err_con++;
if( err_con >3)
{
log_w("ads1115 01 Register error!!!\r\n");
log_w( " ADS1115 testing ******** error " );
my_ads1115.state = ADS1115_WORK_STATE_ERROR;
}
my_ads1115.state = ADS1115_WORK_STATE_TEST;
}
my_ads1115.state = ADS1115_WORK_STATE_GET_DATA_Convert_Register;
osDelay(1);
continue;
case ADS1115_WORK_STATE_TEST_OK:
log_w( " ADS1115 TEST_OK ********** %d" ,pADS1115_I2C->State );
my_ads1115.state = ADS1115_WORK_STATE_GET_DATA_READY;
// TODO Stop DMA
continue;
case ADS1115_WORK_STATE_GET_DATA_Convert_Register:
log_i( " ADS1115 Ready ********** Convert_Register " );
while (HAL_I2C_Master_Transmit(pADS1115_I2C, ADS1115_WRITE_ADDRESS, 0x00, 1, 2000) != HAL_OK)
case ADS1115_WORK_STATE_GET_DATA_READY:
osDelay(1);
continue;
case ADS1115_WORK_STATE_GET_DATA_START:
log_w( " ADS1115 GET_DATA_START ********** %d " , pADS1115_I2C->State );
my_ads1115.result_int16 = 0 ;
my_ads1115.voltage_mv = 0.0 ;
ads1115_get_reg_data( );
ads1115_get_coeff( );
my_ads1115.state = ADS1115_WORK_STATE_GET_DATA_SET_REG01;
continue;
case ADS1115_WORK_STATE_GET_DATA_SET_REG01:
I2C_HELPER_Set_mem_addr( ads1115_i2c_helper, 0x01 );
st = I2C_HELPER_Trans( ads1115_i2c_helper, my_ads1115.reg_data, 2 );
if ( st != HAL_OK)
{
if (HAL_I2C_GetError(pADS1115_I2C) != HAL_I2C_ERROR_AF)
log_w( " ADS1115 init ********** trans error " );
my_ads1115.state = ADS1115_WORK_STATE_ERROR;;
break;
}
ticks = osKernelGetTickCount();
err_con = 0;
my_ads1115.state = ADS1115_WORK_STATE_GET_DATA_SET_REG01_Wait;
continue;
case ADS1115_WORK_STATE_GET_DATA_SET_REG01_Wait:
if ( osKernelGetTickCount() - ticks > 1000 )
{
err_con++;
if( err_con >3)
{
printf("ads1115 convert Register error!!!\r\n");
log_w( " ADS1115 set reg ******** error " );
my_ads1115.state = ADS1115_WORK_STATE_ERROR;
}
my_ads1115.state = ADS1115_WORK_STATE_GET_DATA_START;
}
my_ads1115.state = ADS1115_WORK_STATE_GET_DATA_RCV;
// // osEventFlagsClear( ads1115TaskHandle, ADS1115_Event_Get_Data );
// st = HAL_I2C_Master_Transmit (pADS1115_I2C, ADS1115_WRITE_ADDRESS, 0x00, 1 ,1000);
// if ( st != HAL_OK)
// {
// log_e( " ADS1115 init ********** Convert_Register error " );
// my_ads1115.state = ADS1115_WORK_STATE_ERROR;
// break;
// }
// if ( HAL_I2C_GetError(pADS1115_I2C) != HAL_I2C_ERROR_AF )
// {
// log_e("ads1115 read data error!!!\r\n");
// break;
// }
// my_ads1115.state =ADS1115_WORK_STATE_GET_DATA_RCV;
// if (HAL_I2C_Master_Transmit_IT(pADS1115_I2C, ADS1115_WRITE_ADDRESS, 0x00, 1 ) != HAL_OK) {
// my_ads1115.state =ADS1115_WORK_STATE_ERROR;
// }
// if ( HAL_I2C_GetError(pADS1115_I2C) != HAL_I2C_ERROR_AF )
// {
// printf("ads1115 convert Register error!!!\r\n");
// }
// event_flags = osEventFlagsWait ( ads1115EventHandle,
// ADS1115_Event_Get_Data_Convert_Register, 0 , portMAX_DELAY );
// if (event_flags && ADS1115_Event_Get_Data_Convert_Register)
// {
// my_ads1115.state = ADS1115_WORK_STATE_GET_DATA_RCV;
// }
continue;
osDelay(1);
continue;
case ADS1115_WORK_STATE_GET_DATA_RCV:
log_i( " ADS1115 Ready ********** _DATA_RCV " );
int16_t data;
// uint8_t rx_data[2] = {0};
memset( rx_data,0,2);
// 读两位
// while (HAL_I2C_Master_Receive(pADS1115_I2C, ADS1115_READ_ADDRESS, (uint8_t*)resultbuf, 2, 2000) != HAL_OK)
while (HAL_I2C_Master_Receive(pADS1115_I2C, ADS1115_READ_ADDRESS, rx_data, 2, 2000) != HAL_OK)
I2C_HELPER_Set_mem_addr(ads1115_i2c_helper, 0x00);
st = I2C_HELPER_Start_Rcv(ads1115_i2c_helper, rx_data, 2);
if ( st != HAL_OK)
{
if ( HAL_I2C_GetError(pADS1115_I2C) != HAL_I2C_ERROR_AF )
log_w( " ADS1115 init ********** trans error " );
my_ads1115.state = ADS1115_WORK_STATE_ERROR;;
break;
}
ticks = osKernelGetTickCount();
err_con = 0;
my_ads1115.state = ADS1115_WORK_STATE_GET_DATA_RCV_Wait;
continue;
case ADS1115_WORK_STATE_GET_DATA_RCV_Wait:
if ( osKernelGetTickCount() - ticks > 1000 )
{
err_con++;
if( err_con >3)
{
log_e("ads1115 read data error!!!\r\n");
log_w( " ADS1115 data rcv ******** error " );
my_ads1115.state = ADS1115_WORK_STATE_ERROR;
}
my_ads1115.state = ADS1115_WORK_STATE_GET_DATA_START;
}
my_ads1115.result_int16 = rx_data[0] * 256 + rx_data[1];
log_w( " readout : %d %02X %02X %04X",my_ads1115.result_int16 , rx_data[0], rx_data[1],*(uint16_t*)rx_data);
my_ads1115.state = ADS1115_WORK_STATE_GET_DATA_FLOAT;
continue;
osDelay(80);
// st=HAL_I2C_Master_Receive_IT(pADS1115_I2C, ADS1115_READ_ADDRESS, (uint8_t*)resultbuf, 2 );
// if ( st != HAL_OK)
// {
// log_e( " ADS1115 init ********** _DATA_RCV error " );
// my_ads1115.state = ADS1115_WORK_STATE_ERROR;
// break;
// }
// if ( HAL_I2C_GetError(pADS1115_I2C) != HAL_I2C_ERROR_AF )
// {
// log_e("ads1115 read data error!!!\r\n");
// break;
// }
// my_ads1115.state =ADS1115_WORK_STATE_GET_DATA_OK;
// osEventFlagsClear( ads1115TaskHandle, ADS1115_Event_Get_Data_Convert_Register );
// if ( HAL_I2C_Master_Receive_IT(pADS1115_I2C, ADS1115_READ_ADDRESS, (uint8_t*)resultbuf, 2 ) != HAL_OK )
// {
// my_ads1115.state =ADS1115_WORK_STATE_ERROR;
// }
// if ( HAL_I2C_GetError(pADS1115_I2C) != HAL_I2C_ERROR_AF )
// {
// printf("ads1115 read data error!!!\r\n");
// }
// event_flags = osEventFlagsWait ( ads1115EventHandle,
// ADS1115_Event_Get_Data_RCV, 0 , portMAX_DELAY );
// if (event_flags && ADS1115_Event_Get_Data_RCV)
// {
// my_ads1115.state = ADS1115_WORK_STATE_GET_DATA_OK;
// }
continue;
case ADS1115_WORK_STATE_GET_DATA_FLOAT:
log_i( " ADS1115 Ready ********** DATA_FLOAT %d" ,my_ads1115.result_int16 );
// osEventFlagsClear( ads1115TaskHandle, ADS1115_Event_Get_Data_RCV );
osDelay(1);
continue;
case ADS1115_WORK_STATE_GET_DATA_OK:
log_i( " ADS1115 ********** DATA_OK %d" ,my_ads1115.result_int16 );
double val;
my_ads1115.result_int16 = rx_data[0] * 256 + rx_data[1];
if ( (my_ads1115.result_int16 == 0x7FFF) | (my_ads1115.result_int16 == 0X8000)) // 是否超量程了
{
resultbuf[0] = 0;
// printf("over PGA\r\n");
my_ads1115.result_int16 = 0;
}
switch ((0x0E & my_ads1115.config_H) >> 1) // 量程对应的分辨率
{
case (0x00):
val = (double)my_ads1115.result_int16 * 187.5 / 1000.0; //
break;
case (0x01):
val = (double)my_ads1115.result_int16 * 125 / 1000.0;
break;
case (0x02):
val = (double)my_ads1115.result_int16 * 62.5 / 1000.0;
break;
case (0x03):
val = (double)my_ads1115.result_int16 * 31.25 / 1000.0;
break;
case (0x04):
val = (double)my_ads1115.result_int16 * 15.625 / 1000.0;
break;
case (0x05):
val = (double)my_ads1115.result_int16 * 7.8125 / 1000.0;
break;
}
my_ads1115.voltage_mv = (my_ads1115.unit == ADS1115_OUTPUT_UNIT_mv)?val:(val/1000.0);
log_i( " f %06f",my_ads1115.voltage_mv);
my_ads1115.state = ADS1115_WORK_STATE_GET_DATA_OK ;
my_ads1115.voltage_mv = ((double)my_ads1115.result_int16 *(double) my_ads1115.coeff)/1000.0;;
log_i( " voltage mv : %06f",my_ads1115.voltage_mv);
my_ads1115.state = ADS1115_WORK_STATE_STOP ;
break;
case ADS1115_WORK_STATE_GET_DATA_OK:
osDelay(10);
break;
case ADS1115_WORK_STATE_ERROR:
log_w( " ADS1115 error ..... " );
// my_ads1115.state = ADS1115_WORK_STATE_WAITING ;
osDelay(10);
break;
continue;
case ADS1115_WORK_STATE_STOP:
log_w( " ADS1115 stop..... " );
// my_ads1115.state = ADS1115_WORK_STATE_WAITING ;
osDelay(10);
break;
continue;
default:
break;
}
osDelay(200);
}
// for (;;)
// {
// // 启用中断发送,判断状态,
// // 地址中断, 确认是哪I2C 设备通讯
// /* code */
// /* TODO 统一的超时机制 */
// /* 事件来管理 */
// event_flags = osEventFlagsWait (
// ads1115EventHandle
// , ADS1115_Event_READY|ADS1115_Event_INIT|ADS1115_Event_TRANS_ONLY|ADS1115_Event_DATA_RCV
// , 0
// , portMAX_DELAY );
// if ( event_flags & (ADS1115_Event_READY|ADS1115_Event_INIT)
// == (ADS1115_Event_READY|ADS1115_Event_INIT) )
// {
// // if (HAL_I2C_GetError(&hi2c1)!= HAL_I2C_ERROR_AF)
// // {
// // Error_Handler();
// // }
// // if (HAL_I2C_GetState(&hi2c1)!= HAL_I2C_STATE_READY)
// // {
// // Error_Handler();
// // }
// osEventFlagsClear(ads1115EventHandle, ADS1115_Event_INIT);
// osEventFlagsSet(ads1115EventHandle, ADS1115_Event_INIT_SUCCESS);
// }
// if ( event_flags & ( ADS1115_Event_READY|ADS1115_Event_TRANS_ONLY )
// == (ADS1115_Event_READY|ADS1115_Event_TRANS_ONLY) )
// {
// osEventFlagsClear(ads1115EventHandle, ADS1115_Event_TRANS_ONLY);
// }
// if ( event_flags & ( ADS1115_Event_READY|ADS1115_Event_TRANS_FOR_DATA )
// == (ADS1115_Event_READY|ADS1115_Event_TRANS_FOR_DATA) )
// {
// osEventFlagsClear(ads1115EventHandle, ADS1115_Event_TRANS_FOR_DATA);
// osEventFlagsSet(ads1115EventHandle, ADS1115_Event_DATA_RCV);
// }
// if ( event_flags & ( ADS1115_Event_READY|ADS1115_Event_DATA_RCV )
// == ( ADS1115_Event_READY|ADS1115_Event_DATA_RCV ) )
// {
// osEventFlagsClear( ads1115EventHandle, ADS1115_Event_DATA_RCV );
// }
// // __HAL_I2C_GET_FLAG(&hi2c1, I2C_FLAG_TXE) ;
// osDelay(20);
// }
}
}
void ads1115_port(void)
{
ads1115TaskHandle = osThreadNew(ads1115_task, NULL, &ads1115Task_attributes);
ads1115EventHandle = osEventFlagsNew (&ads1115Event_attributes);
ads1115TaskHandle = osThreadNew(ads1115_task, NULL, &ads1115Task_attributes );
// ads1115EventHandle = osEventFlagsNew (&ads1115Event_attributes );
}
/**
@ -360,28 +224,43 @@ void ads1115_port(void)
*/
int ads1115_init(void)
{
uint8_t reg_data[3];
my_ads1115.hi2c= pADS1115_I2C;
my_ads1115.config_H = my_ads1115.config_os|my_ads1115.config_mux
|my_ads1115.config_PGA|my_ads1115.config_MODE;
my_ads1115.config_L = my_ads1115.config_DR|my_ads1115.config_COMP_MODE|my_ads1115.config_COMP_POL
|my_ads1115.config_COMP_LAT|my_ads1115.config_COMP_QUE ;
// log_i( " %02X %02X %02X", 0x01,my_ads1115.config_H,my_ads1115.config_L );
my_ads1115.reg_data[0] = 0x01; // point addr 8bit 配置寄存器
my_ads1115.reg_data[1] = my_ads1115.config_H; // config_reg 高位
my_ads1115.reg_data[2] = my_ads1115.config_L; // config_reg 低位
// log_i( " %02X %02X %02X", my_ads1115.reg_data[0],my_ads1115.reg_data[1],my_ads1115.reg_data[2] );
ads1115_i2c_helper = I2C_HELPER_Init( );
I2C_HELPER_Set_Hi2c( ads1115_i2c_helper, pADS1115_I2C );
I2C_HELPER_Set_dev_addr( ads1115_i2c_helper, ADS1115_WRITE_ADDRESS);
I2C_HELPER_Set_mem_addr_size( ads1115_i2c_helper, I2C_MEMADD_SIZE_8BIT ); //I2C_MEMADD_SIZE_16BIT
// I2C_HELPER_Set_mem_addr(ads1115_i2c_helper, uint16_t mem_addr);
// TODO 阻塞式 仅init 使用
// my_ads1115.state = ADS1115_WORK_STATE_INIT;
// osEventFlagsSet ( ads1115TaskHandle, ADS1115_Event_INIT );
if (HAL_I2C_Master_Transmit(pADS1115_I2C, ADS1115_WRITE_ADDRESS, my_ads1115.reg_data, 3,1000 ) != HAL_OK)
{
return -1;
}
I2C_HELPER_Setup_Trans_mode( ads1115_i2c_helper, I2C_Trans_IT );
// 设置回调
I2C_HELPER_Set_Callback( ads1115_i2c_helper, (void *)&my_ads1115, (i2c_callback_fun) ads1115_callback);
// ads1115_get_reg_data( );
// // polling mode test, cfg register : 0x01
// int st = 0;
// st = HAL_I2C_Mem_Write( ads1115_i2c_helper->hi2c, ADS1115_WRITE_ADDRESS, 0x01,
// ads1115_i2c_helper->mem_addr_size, my_ads1115.reg_data, 2, 0xFFFF );
// if (st != HAL_OK)
// {
// return -1;
// }
return 0;
}
int ads1115_test(void)
{
my_ads1115.state = ADS1115_WORK_STATE_TEST;
}
void ads1115_start(void)
{
my_ads1115.state =ADS1115_WORK_STATE_GET_DATA_START;
// osEventFlagsSet( ads1115TaskHandle, ADS1115_Event_Get_Data );
}
void ads1115_stop(void)
{
}
int ads1115_setup(ADS1115_REG_CONFIG_MUX_Diff_TypeDef mux)
@ -391,6 +270,7 @@ int ads1115_setup(ADS1115_REG_CONFIG_MUX_Diff_TypeDef mux)
return 0;
}
int16_t ads1115_read_data( )
{
int16_t data;
@ -417,45 +297,54 @@ int16_t ads1115_read_data( )
return data;
}
double ads1115_get_voltage_val( )
double ads1115_get_coeff( )
{
double val;
int16_t ad_val;
// TODO 去掉延时,使用寄存器的指示, setup 和读取数据分开执行
// ads1115_setup( pADS1115_I2C, pointADD, configH, configL );
ads1115_setup( ADS1115_REG_CONFIG_MUX_SINGLE_0 );
HAL_Delay(10);
ad_val = ads1115_read_data( pADS1115_I2C );
if ( (ad_val == 0x7FFF) | (ad_val == 0X8000)) // 是否超量程了
{
ad_val = 0;
// printf("over PGA\r\n");
}
my_ads1115.coeff = 1.0;
switch ((0x0E & my_ads1115.config_H) >> 1) // 量程对应的分辨率
{
case (0x00):
val = (double)ad_val * 187.5 / 1000.0; //
my_ads1115.coeff =my_ads1115.coeff * 187.5 ; //
break;
case (0x01):
val = (double)ad_val * 125 / 1000.0;
my_ads1115.coeff = my_ads1115.coeff * 125 ;
break;
case (0x02):
val = (double)ad_val * 62.5 / 1000.0;
my_ads1115.coeff = my_ads1115.coeff * 62.5;
break;
case (0x03):
val = (double)ad_val * 31.25 / 1000.0;
my_ads1115.coeff = my_ads1115.coeff * 31.25 ;
break;
case (0x04):
val = (double)ad_val * 15.625 / 1000.0;
my_ads1115.coeff = my_ads1115.coeff * 15.625;
break;
case (0x05):
val = (double)ad_val * 7.8125 / 1000.0;
my_ads1115.coeff = my_ads1115.coeff * 7.8125;
break;
}
}
return (my_ads1115.unit == ADS1115_OUTPUT_UNIT_mv)?val:(val/1000.0);
double ads1115_get_voltage_val( )
{
double val;
int16_t ad_val;
// TODO 去掉延时,使用寄存器的指示, setup 和读取数据分开执行
// ads1115_setup( pADS1115_I2C, pointADD, configH, configL );
ads1115_setup( ADS1115_REG_CONFIG_MUX_SINGLE_0 );
HAL_Delay(10);
ad_val = ads1115_read_data( pADS1115_I2C );
if ( (ad_val == 0x7FFF) | (ad_val == 0X8000)) // 是否超量程了
{
ad_val = 0;
// printf("over PGA\r\n");
}
ads1115_get_coeff( );
val = ad_val * my_ads1115.coeff/1000.0;
if (my_ads1115.unit == ADS1115_OUTPUT_UNIT_v)
{
val = val/1000.0;
}
return val;
}
@ -467,64 +356,120 @@ void ads1115_get_data_start(void)
}
void I2C1_EV_IRQHandler(void)
void ads1115_callback(ADS1115_TypeDef *pAds1115, uint8_t *buf, uint16_t size)
{
switch (my_ads1115.state)
{
case ADS1115_WORK_STATE_TESTING :
my_ads1115.state++;
break;
case ADS1115_WORK_STATE_GET_DATA_SET_REG01_Wait :
my_ads1115.state++;
break;
case ADS1115_WORK_STATE_GET_DATA_RCV_Wait :
// TODO Check data?
my_ads1115.state++;
break;
default:
break;
}
}
// if (__HAL_I2C_GET_FLAG(&hi2c1, I2C_FLAG_TC) != RESET)
// ads1115EventHandle = osEventFlagsNew (&ads1115Event_attributes);
// typedef enum
// {
// EV_READY, /*!< Startup finished. */
// EV_FRAME_RECEIVED, /*!< Frame received. */
// EV_EXECUTE, /*!< Execute function. */
// EV_FRAME_SENT /*!< Frame sent. */
// } eMBEventType;
// osEventFlagsSet(modbusEventHandle,eEvent);
// osEventFlagsClear (osEventFlagsId_t ef_id, uint32_t flags)
// recvedEvent = osEventFlagsWait (modbusEventHandle,
// EV_READY | EV_FRAME_RECEIVED | EV_EXECUTE |
// EV_FRAME_SENT, /* 接收任务感兴趣的事件 */
// 0,
// portMAX_DELAY); /* 指定超时事件,无限等待 */
// TODO 阻塞式 仅init 使用
// my_ads1115.state = ADS1115_WORK_STATE_INIT;
// osEventFlagsSet ( ads1115TaskHandle, ADS1115_Event_INIT );
// I2C_HELPER_Set_mem_addr(ads1115_i2c_helper, 0x01);
// st = I2C_HELPER_Trans(ads1115_i2c_helper, reg_data,2);
// I2C_HELPER_Set_mem_addr(ads1115_i2c_helper, 0x00);
// st = I2C_HELPER_Start_Rcv(ads1115_i2c_helper, rx_data, 2);
// st = HAL_I2C_Mem_Write(ads1115_i2c_helper->hi2c, ads1115_i2c_helper->dev_addr, my_ads1115.reg_data[0],ads1115_i2c_helper->mem_addr_type , reg_data,2,0xFFFF);
// if (HAL_I2C_Master_Transmit(pADS1115_I2C, ADS1115_WRITE_ADDRESS, my_ads1115.reg_data, 3,1000 ) != HAL_OK)
// {
// log_i( " i2c master send cplt ******");
// return -1;
// }
// while (HAL_I2C_Master_Transmit(pADS1115_I2C, ADS1115_WRITE_ADDRESS, 0x00, 1, 2000) != HAL_OK)
// {
// if (HAL_I2C_GetError(pADS1115_I2C) != HAL_I2C_ERROR_AF)
// {
// printf("ads1115 convert Register error!!!\r\n");
// }
// }
// // 读两位
// while (HAL_I2C_Master_Receive(pADS1115_I2C, ADS1115_READ_ADDRESS, rx_data, 2, 2000) != HAL_OK)
// {
// if ( HAL_I2C_GetError(pADS1115_I2C) != HAL_I2C_ERROR_AF )
// {
// printf("ads1115 read data error!!!\r\n");
// }
// }
log_i( " i2c master send cplt ******");
HAL_I2C_EV_IRQHandler(&hi2c1);
}
void I2C1_ER_IRQHandler(void)
{
HAL_I2C_ER_IRQHandler(&hi2c1);
}
// void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
// {
// log_i( " i2c master send cplt ------------------- ");
// if (my_ads1115.state = ADS1115_WORK_STATE_INIT)
// {
// osEventFlagsSet( ads1115TaskHandle, ADS1115_Event_INIT );
// }
// if (my_ads1115.state = ADS1115_WORK_STATE_GET_DATA_Convert_Register)
// {
// osEventFlagsSet( ads1115TaskHandle, ADS1115_Event_Get_Data_Convert_Register );
// }
// // //发送完成回调函数
// // {
// // //一些其他操作
// // }
// // HAL_I2C_Master_Receive_IT(&I2cHandle,i2c_slave_recv,I2C_REC_BYTES);
// // {
// // //一些其他操作
// // }
// }
// case ADS1115_WORK_STATE_GET_DATA_Convert_Register:
// log_i( " ADS1115 Ready ********** Convert_Register " );
// while (HAL_I2C_Master_Transmit(pADS1115_I2C, ADS1115_WRITE_ADDRESS, 0x00, 1, 2000) != HAL_OK)
// {
// if (HAL_I2C_GetError(pADS1115_I2C) != HAL_I2C_ERROR_AF)
// {
// printf("ads1115 convert Register error!!!\r\n");
// }
// }
// my_ads1115.state = ADS1115_WORK_STATE_GET_DATA_RCV;
// void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
// {
// log_i( " i2c master rx cplt ------------------- ");
// // //接收完成回调函数
// // {
// // //一些其他操作
// // }
// // HAL_I2C_Slave_Transmit_IT(&I2cHandle,send_buffer,send_cnt);
// // {
// // //一些其他操作
// // }
// }
// // // osEventFlagsClear( ads1115TaskHandle, ADS1115_Event_Get_Data );
// // st = HAL_I2C_Master_Transmit (pADS1115_I2C, ADS1115_WRITE_ADDRESS, 0x00, 1 ,1000);
// // if ( st != HAL_OK)
// // {
// // log_e( " ADS1115 init ********** Convert_Register error " );
// // my_ads1115.state = ADS1115_WORK_STATE_ERROR;
// // break;
// // }
// // if ( HAL_I2C_GetError(pADS1115_I2C) != HAL_I2C_ERROR_AF )
// // {
// // log_e("ads1115 read data error!!!\r\n");
// // break;
// // }
// // my_ads1115.state =ADS1115_WORK_STATE_GET_DATA_RCV;
// // if (HAL_I2C_Master_Transmit_IT(pADS1115_I2C, ADS1115_WRITE_ADDRESS, 0x00, 1 ) != HAL_OK) {
// // my_ads1115.state =ADS1115_WORK_STATE_ERROR;
// // }
// // if ( HAL_I2C_GetError(pADS1115_I2C) != HAL_I2C_ERROR_AF )
// // {
// // printf("ads1115 convert Register error!!!\r\n");
// // }
// // event_flags = osEventFlagsWait ( ads1115EventHandle,
// // ADS1115_Event_Get_Data_Convert_Register, 0 , portMAX_DELAY );
// // if (event_flags && ADS1115_Event_Get_Data_Convert_Register)
// // {
// // my_ads1115.state = ADS1115_WORK_STATE_GET_DATA_RCV;
// // }
// void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
// {
// //错误异常回调函数
// }
// continue;

@ -1,142 +0,0 @@
#include "i2c_helper.h"
// static I2C_HandleTypeDef *pADS1115_I2C = &hi2c1;
// osEventFlagsId_t i2chelperEventHandle;
// const osEventFlagsAttr_t i2chelperEvent_attributes = {
// .name = "i2chelperEvent"
// };
I2CHelper_TypeDef * I2CHelper_Init( )
{
I2CHelper_TypeDef *Handle = (I2CHelper_TypeDef *)malloc(sizeof(I2CHelper_TypeDef));
if (Handle == NULL)
{
return NULL;
}
// Handle->delaytime_trans_byte = 0;
return Handle;
}
void I2CHelper_Set_Hi2c( I2CHelper_TypeDef *i2chelper, I2C_HandleTypeDef * hi2c )
{
i2chelper->hi2c = hi2c;
}
void I2CHelper_Set_Mode( I2CHelper_TypeDef *i2chelper, I2C_HELPER_MODE_TypeDef mode )
{
i2chelper->mode = mode;
}
void I2CHelper_Set_Trans_Type( I2CHelper_TypeDef *i2chelper, I2C_HELPER_TRANS_TypeDef trans_type )
{
i2chelper->trans_type = trans_type;
}
void I2CHelper_Set_Rcv_Type( I2CHelper_TypeDef *i2chelper, I2C_HELPER_RCV_TypeDef rcv_type )
{
i2chelper->rcv_type = rcv_type;
}
int I2CHelper_Set_callback_func_obj( I2CHelper_TypeDef *i2chelper, void * obj, it_callback callback )
{
i2chelper->obj = obj;
i2chelper->callback = callback;
return 0;
}
void I2CHelper_Set_rcv_buf( I2CHelper_TypeDef *i2chelper, uint8_t *buf, uint32_t buf_size )
{
i2chelper->receive_buf = buf;
i2chelper->receive_size = buf_size;
i2chelper->receive_data_con = 0;
}
void I2CHelper_Set_Trans_Buf( I2CHelper_TypeDef *i2chelper, uint8_t *buf, uint32_t buf_size )
{
i2chelper->trans_buf = buf;
i2chelper->trans_size = buf_size;
i2chelper->trans_data_con = 0;
}
int I2CHelper_Write( I2CHelper_TypeDef *i2chelper, uint8_t * buf, uint16_t size)
{
// 考虑主从机模式
int st = -1;
switch ( i2chelper->mode == I2C_HELPER_MODE_MASTER )
{
case I2C_HELPER_MODE_MASTER:
if ( i2chelper->trans_type == I2C_HELPER_TRANS_POLLING )
{
st = HAL_I2C_Master_Transmit( i2chelper->hi2c, i2chelper->i2c_write_address
, buf, size, 0xFFFF );
}
if ( i2chelper->trans_type == I2C_HELPER_TRANS_IT )
{
// st = HAL_I2C_Master_Transmit_IT( i2chelper->hi2c, i2chelper->i2c_write_address
// , buf, size );
st = HAL_I2C_Master_Transmit_IT( i2chelper->hi2c, i2chelper->i2c_write_address, buf,size );
}
if ( i2chelper->trans_type == I2C_HELPER_TRANS_DMA )
{
// st = HAL_I2C_Master_Transmit_DMA( i2chelper->hi2c, i2chelper->i2c_write_address
// , buf, size );
st = HAL_I2C_Master_Transmit_DMA( i2chelper->hi2c, i2chelper->i2c_write_address, buf, size );
}
break;
case I2C_HELPER_MODE_SLAVE:
break;
default:
break;
}
return st;
}
int I2CHelper_Read(I2CHelper_TypeDef *i2chelper, uint8_t * buf, uint16_t size)
{
int st = -1;
switch ( i2chelper->mode == I2C_HELPER_MODE_MASTER )
{
case I2C_HELPER_MODE_MASTER:
if ( i2chelper->rcv_type == I2C_HELPER_TRANS_POLLING )
{
st = HAL_I2C_Master_Receive( i2chelper->hi2c, i2chelper->i2c_write_address
, buf, size, 0xFFFF );
}
if ( i2chelper->rcv_type == I2C_HELPER_TRANS_IT )
{
// st = HAL_I2C_Master_Transmit_IT( i2chelper->hi2c, i2chelper->i2c_write_address
// , buf, size );
st = HAL_I2C_Master_Receive_IT( i2chelper->hi2c, i2chelper->i2c_write_address
, buf, size );
}
if ( i2chelper->rcv_type == I2C_HELPER_TRANS_DMA )
{
// st = HAL_I2C_Master_Transmit_DMA( i2chelper->hi2c, i2chelper->i2c_write_address
// , buf, size );
st = HAL_I2C_Master_Receive_DMA( i2chelper->hi2c, i2chelper->i2c_write_address
, buf, size );
}
break;
case I2C_HELPER_MODE_SLAVE:
break;
default:
break;
}
return st;
}
int I2CHelper_Flags_Set(I2CHelper_TypeDef *i2chelper, I2C_HELPER_Event_TypeDef evt_type )
{
return osEventFlagsSet(i2chelper, evt_type);
}
// int I2CHelper_Flags_Clear(I2CHelper_TypeDef *i2chelper, I2C_HELPER_Event_TypeDef evt_type )
// {
// return osEventFlagsClear(i2chelper, evt_type);
// }
// int I2CHelper_Flags_Wait(I2CHelper_TypeDef *i2chelper, uint32_t delay )
// {
// return osEventFlagsWait (
// i2chelperEventHandle
// , I2C_HELPER_Event_READY|I2C_HELPER_Event_INIT|I2C_HELPER_Event_TRANS_ONLY|I2C_HELPER_Event_DATA_RCV
// , 0
// , delay );
// }

@ -1,412 +1,343 @@
#include "pH.h"
#include "elog.h"
#include "FreeRTOS.h"
#include "cmsis_os.h"
#include "stm32f4xx.h"
#include "usart.h"
#include "main.h"
#include "PH.h"
/****** Port ***************/
#if 1
osThreadId_t PH_Handle;
const osThreadAttr_t PH_attributes = {
.name = "PH",
.stack_size = 1024,
.priority = (osPriority_t) osPriorityBelowNormal,
};
#define PH_Uart_IRQHandler USART3_IRQHandler
// #define PH_Uart_IRQHandler USART4_IRQHandler
// 事件
osEventFlagsId_t PH_EventHandle;
const osEventFlagsAttr_t PH_Event_attributes = {
.name = "PH_Event"
};
extern UART_HandleTypeDef huart3;
UART_HandleTypeDef *pPHUart = &huart3;
PH_TypeDef *ph ;
My_PH_TypeDef my_pH =
{
uint8_t PH_TEST_BUF[8] ={0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09};
uint8_t PH_TRANS_BUF[8] ={0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09};
uint8_t PH_RCV_BUF[13] ={0};
UART_HELPER_TypeDef *ph_uart_helper;
PH_TypeDef ph ={
PH_Init,
PH_Port,
PH_Test,
PH_Start,
PH_Stop,
SEL_232_485_GPIO_Port,
HDPLX_GPIO_Port,
DE485_GPIO_Port,
SEL_232_485_Pin,
HDPLX_Pin,
DE485_Pin,
Uart_Interface_Max3160, /* 接口类型 0: common, 1: 485 ,2:3160*/
Uart_RSMODE_485, /* mode_232_485 0 commome 1:485*/
Uart_Trans_DMA, /* trans_type0 :polling, 1: IT 2: DMA*/
Uart_Trans_DMA, /* rcv_type 0 :polling, 1: IT 2: DMA*/
Uart_IDLE_IT_ENABLE, /* idle_enable_disable 0 :不启用空闲 , 1: 启用空闲*/
0, //ph
0, // temprature
NULL, /* pointer huart*/
NULL,
0, // state
0, // data_ok
{0}, // buf
0, // size_received
0, // time base
1000, // timeout ticks, 1000ms = 1S
0, // event_flag
};
extern volatile uint8_t measure_flag ;
int PH_Init( )
{
ph_uart_helper = UART_HELPER_Init( );
uint8_t PH_SND_Buf[8] = { 0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B };
uint8_t PH_RCV_Buf[PH_Rcv_Buf_Size*2] = { 0 };
if (ph_uart_helper ==NULL) return -1;
osThreadId_t phHandle;
const osThreadAttr_t PH_attributes = {
.name = "ph",
.stack_size = 1024,
.priority = (osPriority_t) osPriorityBelowNormal,
};
// TODO 接口
UART_HELPER_Set_Huart( ph_uart_helper, pPHUart );
UART_HELPER_Set_Interface_Type(ph_uart_helper, Uart_Interface_Max3160_485);
void PH_Uart_IRQHandler(void)
{
if (__HAL_UART_GET_FLAG( ph->uarthelper->huart, UART_FLAG_TC) != RESET)
{
// log_i("ph....huart3 TC callback ...");
PH_Send_Cplt_Callback( ph );
// ph->state = PH_Status_Send_Cplt;
ph->uarthelper->huart->gState = HAL_UART_STATE_READY;
__HAL_UART_CLEAR_FLAG( ph->uarthelper->huart, UART_FLAG_TC );
}
// 传输
UART_HELPER_Setup_Trans_mode( ph_uart_helper, Uart_Trans_DMA);
// UART_HELPER_Setup_Rcv_mode( ph_uart_helper, Uart_Trans_IT );
UART_HELPER_Set_enable_idle( ph_uart_helper, Uart_IDLE_IT_ENABLE);
// 回调GPIO 操作 数据操作
UART_HELPER_Set_trans_GPIO( ph_uart_helper, PH_Trans_GPIO ); // enbale rcv
UART_HELPER_Set_trans_cplt_GPIO( ph_uart_helper, PH_Trans_Cplt_GPIO );
UART_HELPER_Set_Callback( ph_uart_helper,&ph, PH_CallBack );
if (__HAL_UART_GET_FLAG( ph->uarthelper->huart, UART_FLAG_IDLE ) != RESET)
// 设置 Buf
UART_HELPER_Set_Rcv_Buf(ph_uart_helper, PH_RCV_BUF, sizeof(PH_RCV_BUF));
UART_HELPER_Set_Trans_Buf( ph_uart_helper, PH_TRANS_BUF, sizeof(PH_TRANS_BUF) );
// GPIO 操作
switch (ph_uart_helper->interface_type)
{
uint16_t count = __HAL_DMA_GET_COUNTER( ph->uarthelper->huart->hdmarx );
log_i("....huart3 IDLE.... %d site3 == 4 %02X ", count, PH_RCV_Buf[2] );
uint16_t data_length = 2*PH_Rcv_Buf_Size - count;
if (count == PH_Rcv_Buf_Size)
{
// HAL_UART_DMAStop( ph->uarthelper->huart );
ph->state = PH_Status_RCV_OK;
}
if (count < PH_Rcv_Buf_Size)
{
// HAL_UART_DMAStop( ph->uarthelper->huart );
ph->state = PH_Status_Error;
}
__HAL_UART_CLEAR_IDLEFLAG( ph->uarthelper->huart );
case Uart_Interface_Default:
break;
case Uart_Interface_485:
usart6_send_enable();
break;
case Uart_Interface_Max3160_232:
PH_MAX3160_232();
break;
case Uart_Interface_Max3160_485:
max3160_485_receive_mode();
break;
default:
break;
}
HAL_UART_IRQHandler( ph->uarthelper->huart );
ph.state = PH_State_Waiting;
ph.timebase_ticks = osKernelGetTickCount( );
}
int PH_Init( )
void PH_Port( )
{
ph = (PH_TypeDef *)malloc(sizeof(PH_TypeDef));
UartInterface_TypeDef *interface = UartInterface_Init( );
UartHelper_TypeDef *uarthelper = UartHelper_Init();
PH_Handle = osThreadNew( PH_Task, NULL, &PH_attributes );
// PH_EventHandle = osEventFlagsNew(&PH_Event_attributes);
}
PH_Set_Uarthelper( ph, uarthelper );
PH_Set_Interface( ph, interface );
PH_Set_Huart( ph, pPHUart );
/* set_GPIO UartInterface */
UartInterface_Set_Sel_GPIO_Pin( ph->uarthelper->interface, my_pH.sel_gpio, my_pH.sel_pin );
UartInterface_Set_Dplx_GPIO_Pin( ph->uarthelper->interface, my_pH.dplx_gpio, my_pH.dplx_pin );
UartInterface_Set_DE485_GPIO_Pin( ph->uarthelper->interface, my_pH.de_gpio, my_pH.de_pin );
/* interface type */
PH_Set_Interface_Type( ph, my_pH.interface_type );
PH_Set_RsMode_232_485 ( ph, my_pH.mode_232_485 ); // max3160 的232 485选择
PH_Set_Trans_Type( ph, my_pH.trans_type ) ;
PH_Set_Rcv_Type ( ph, my_pH.rcv_type ) ;
PH_Set_Idle_Enable (ph, my_pH.idle_enable_disable ) ;
// UartHelper_Setup_Interface_type( ph ,my_pH.interface_type);
// check
if ( ph->uarthelper->huart == pPHUart ){
log_i ( "ph set huart ok. trans_type -> %d rcv_type -> %d ( 0 :polling, 1: IT 2: DMA) ",ph->trans_type ,ph->rcv_type ) ;
log_i( " interface-> %d %d (0:default, 1:485, 2:3160) " ,ph->uarthelper->interface_type ,ph->uarthelper->interface->interface_type );
log_i( "ph 232_485 -> %d %d (0:232 ,1: 485) " ,ph->uarthelper->mode_232_485, ph->uarthelper->interface->mode_232_485 );
}else{
log_e ( "ph set huart failure " ) ;
}
PH_Set_Timeout( ph, 500 );
PH_Set_Sendbuf( ph, PH_SND_Buf, 8 );
PH_Set_Rcvbuf( ph, PH_RCV_Buf, sizeof(PH_RCV_Buf) );
// set paras;
ph->timebase = 0;
// ph->timeout_ms = 1000;
ph->send_status = Uart_Status_Ready;
ph->send_flag = 0;
ph->command_seq = 4;
ph->mode = 2;
ph->data_begin_flag = 0;
ph->state = PH_Status_Waiting;
int PH_Test( )
{
ph.state = PH_State_Test; // 操作状态
return 0;
}
void PH_Start( )
{
ph.state = PH_State_Get_DATA; // 操作状态
}
void PH_Port( )
void PH_Stop( )
{
phHandle = osThreadNew( PH_Task, NULL, &PH_attributes );
ph.state = PH_State_Stop;
// TODO stop task?
}
void PH_Task( )
{
// log_i( " ph tran mode : %d ", ph->->trans_mode );
memset( PH_RCV_Buf, 0, 2*PH_Rcv_Buf_Size ); /* ph*3 ph *2 否则内存泄漏 */
uint64_t time_ticks;
// log_i( " ph tran mode : %d %d " , ph->trans_mode , ph->mode );
// memset( PH_RCV_Buf, 0, 2*PH_Rcv_Buf_Size ); /* ph*3 ph *2 否则内存泄漏 */
ph.event_flag = 0;
uint64_t ticks;
int err_con = 0;
int st;
for ( ; ; )
{
switch (ph->state)
{
switch (ph.state)
{
case PH_Status_Waiting:
case PH_State_Waiting:
log_i( " ph task..... : %d %d " , ph.state , ph.state );
osDelay(20);
break;
case PH_Status_Ready:
log_i(" pH send .................. ");
my_pH.ph_val = 0;
my_pH.temp_val = 0;
memset( ph->rcv_buf, 0, 2*PH_Rcv_Buf_Size );
PH_Begin_Rcv( ph, ph->rcv_buf, ph->size_rcv );
// HAL_UARTEx_ReceiveToIdle_DMA( &huart3, PH_RCV_Buf, PH_Rcv_Buf_Size );
// HAL_UART_Receive_DMA ( &huart3, ph->rcv_buf, ph->size_rcv );
// __HAL_UART_ENABLE_IT( &huart3, UART_IT_IDLE );
ph->timebase = osKernelGetTickCount( );
// max3160_485_send_mode();
ph->state = PH_Status_Sending ;
PH_Transmit( ph, ph->send_buf, ph->size_send );
break;
case PH_Status_Sending:
time_ticks = osKernelGetTickCount() ;
// log_i( " time_ticks :%d %d" , time_ticks, ph->timebase);
if ( ( time_ticks - ph->timebase ) > ph->timeout_ms)
case PH_State_Test:
ph.state = PH_State_Test_Start;
continue;
case PH_State_Test_Start:
UART_HELPER_Set_Trans_Buf( ph_uart_helper, PH_TEST_BUF, sizeof(PH_TEST_BUF) );
PH_Receive();
PH_Transmit();
ticks = osKernelGetTickCount();
err_con = 0;
ph.state = PH_State_Testing;
continue;
case PH_State_Testing:
if ( osKernelGetTickCount() - ticks > 2000 )
{
ph->state = PH_Status_Timeout ;
err_con++;
if( err_con >3)
{
log_w( " PH testing ******** error " );
ph.state = PH_State_Error;
}
ph.state = PH_State_Test;
}
osDelay(5);
osDelay(1);
continue;
case PH_State_Test_OK:
log_i( " ph test ok..... : %d %d " , ph.state , ph.state );
ph.state = PH_State_Ready;
break;
case PH_Status_Send_Cplt:
time_ticks = osKernelGetTickCount() ;
log_i( "PH_Status_Send_Cplt ..... " );
if ( (time_ticks - ph->timebase ) > ph->timeout_ms)
case PH_State_Ready:
osDelay(1);
continue;
case PH_State_Get_DATA:
ph.data_ok = 0 ;
ph.size_received = 0 ;
memset( ph.result_buf, 0, sizeof(ph.result_buf) );
UART_HELPER_Set_Trans_Buf( ph_uart_helper, PH_TRANS_BUF, sizeof(PH_TRANS_BUF) );
PH_Receive();
PH_Transmit();
ticks = osKernelGetTickCount();
err_con = 0;
ph.state = PH_State_Get_DATA_Wait;
continue;
case PH_State_Get_DATA_Wait:
if ( osKernelGetTickCount() - ticks > 2000 )
{
ph->state = PH_Status_Timeout ;
err_con++;
if( err_con >3)
{
log_w( " PH testing ******** error " );
ph.state = PH_State_Error;
}
ph.state = PH_State_Get_DATA;
}
osDelay(5);
break;
case PH_Status_RCV_OK:
if ( my_pH.ph_val == 0 & my_pH.temp_val==0 )
osDelay(1);
continue;
case PH_State_Get_DATA_OK:
log_w( " PH data ok ******** " );
// TODO 停止DMA
if (ph_uart_helper->rcv_mode == Uart_Trans_DMA)
{
my_pH.ph_val = ph->rcv_buf[3]*256 + ph->rcv_buf[4];
my_pH.temp_val = ph->rcv_buf[5]*256 + ph->rcv_buf[6];
log_i( "PH_Status_RCV_OK ..... ph %d temp %d " , my_pH.ph_val, my_pH.temp_val);
ph->state = PH_Status_DATA_OK ;
HAL_UART_DMAStop( ph_uart_helper->huart );
}
// TODO 停止DMA
HAL_UART_DMAStop( ph->uarthelper->huart );
osDelay(20);
break;
case PH_Status_DATA_OK:
osDelay(20);
break;
case PH_Status_Timeout:
ph.data_ok = 1 ;
ph.state = PH_Statee_Get_DATA_Check ;
continue;
case PH_Statee_Get_DATA_Check:
log_w( " PH data check ******** " );
if(PH_Validate( )== 0)
{
ph.state = PH_State_Stop;
}else{
ph.state = PH_State_Error;
}
osDelay(10);
continue;
case PH_State_Stop:
log_d(" ph stop, aftetr deal, change to ready....");
osDelay(10);
continue;
case PH_State_Timeout:
log_e( " pH timeout ..... " );
ph->state = PH_Status_Waiting ;
HAL_UART_DMAStop( ph->uarthelper->huart );
ph.state = PH_State_Waiting ;
// TODO 停止DMA
if (ph_uart_helper->rcv_mode == Uart_Trans_DMA)
{
HAL_UART_DMAStop( ph_uart_helper->huart );
}
osDelay(5);
break;
case PH_Status_Error:
case PH_State_Error:
log_e( " pH error ..... " );
ph->state = PH_Status_Waiting ;
HAL_UART_DMAStop( ph->uarthelper->huart );
ph.state = PH_State_Waiting ;
// TODO 停止DMA
if (ph_uart_helper->rcv_mode == Uart_Trans_DMA)
{
HAL_UART_DMAStop( ph_uart_helper->huart );
}
osDelay(5);
break;
default:
break;
break;
}
osDelay(20);
}
}
int PH_Test( )
{
HAL_UART_Transmit_IT( &huart3, ph->send_buf, ph->size_send );
osDelay(20);
if ( HAL_UART_Receive ( &huart3, ph->rcv_buf, ph->size_rcv ,0xFF) != HAL_OK ){
return -1;
}
return 0;
}
void PH_Start( )
{
// log_e( " pH PH_Start ..... " );
ph->state = PH_Status_Ready;
}
void PH_Stop( )
{
ph->state = PH_Status_Waiting;
}
/****************** 接口 *******************/
void PH_Set_Uarthelper( PH_TypeDef *ph, UartHelper_TypeDef * uarthelper)
{
ph->uarthelper = uarthelper;
}
void PH_Set_Interface( PH_TypeDef *ph, UartInterface_TypeDef * interface )
{
UartHelper_Set_Interface( ph->uarthelper, interface );
}
void PH_Set_Huart( PH_TypeDef *ph, UART_HandleTypeDef * huart )
{
ph->uarthelper->huart = huart;
// UartHelper_Set_Huart( ph->uarthelper, huart );
}
void PH_Set_Interface_Type( PH_TypeDef *ph, Uart_Interface_Type_Typedef interface_type )
{
ph->interface_type = interface_type;
ph->uarthelper->interface_type = interface_type;
UartInterface_Setup_Interface_type( ph->uarthelper->interface ,ph->interface_type);
}
void PH_Set_RsMode_232_485( PH_TypeDef *ph, Uart_RS_Mode_TypeDef rs_232_485 )
{
ph->mode_232_485 = rs_232_485;
ph->uarthelper->mode_232_485 = rs_232_485;
UartInterface_Setup_Mode_232_485( ph->uarthelper->interface, my_pH.mode_232_485 );
}
void PH_Set_Trans_Type( PH_TypeDef *ph, Uart_Transmode_TypeDef trans_type )
void PH_Trans_GPIO(void)
{
ph->trans_type = trans_type;
// HAL_GPIO_WritePin(R_W_GPIO_Port,R_W_Pin, SET);
// HAL_GPIO_WritePin(NULL,0, SET);
HAL_GPIO_WritePin(SEL_232_485_GPIO_Port,SEL_232_485_Pin, SET);
HAL_GPIO_WritePin(HDPLX_GPIO_Port,HDPLX_Pin, RESET);
HAL_GPIO_WritePin(DE485_GPIO_Port, DE485_Pin, SET);
}
void PH_Set_Rcv_Type( PH_TypeDef *ph, Uart_Transmode_TypeDef rcv_type )
{
ph->rcv_type = rcv_type;
}
void PH_Set_Idle_Enable( PH_TypeDef *ph, Uart_IDLE_Enable_TypeDef idle_enable_disable )
void PH_Trans_Cplt_GPIO(void)
{
ph->idle_enable_disable = idle_enable_disable;
}
void PH_Set_TransMode( PH_TypeDef *ph, Uart_Transmode_TypeDef trans_mode )
// HAL_GPIO_WritePin(R_W_GPIO_Port,R_W_Pin, RESET);
// HAL_GPIO_WritePin(NULL,0, RESET);
HAL_GPIO_WritePin(SEL_232_485_GPIO_Port,SEL_232_485_Pin, SET);
HAL_GPIO_WritePin(HDPLX_GPIO_Port,HDPLX_Pin, SET);
HAL_GPIO_WritePin(DE485_GPIO_Port, DE485_Pin, RESET);
}
int PH_Transmit()
{
ph->trans_type = trans_mode;
ph->rcv_type = trans_mode;
// TODO 结合队列
ph.size_received =0;
if( ph_uart_helper->transferring == 0)
{
return UART_HELPER_Trans(ph_uart_helper, ph_uart_helper->trans_record->buf, ph_uart_helper->trans_record->size );
}
return 0;
}
void PH_Set_Timeout( PH_TypeDef *ph, uint16_t timeout_ms)
{
ph->timeout_ms = timeout_ms;
}
void PH_Set_Sendbuf( PH_TypeDef *ph, uint8_t * buf, uint16_t size)
int PH_Receive()
{
ph->send_buf = buf;
ph->size_send = size;
// log_i( " size_send %d", sensor->size_send);
return UART_HELPER_Start_Rcv(ph_uart_helper, ph_uart_helper->rcv_buf, ph_uart_helper->rcv_size);
// return 0;
}
void PH_Set_Rcvbuf( PH_TypeDef *ph, uint8_t * buf, uint16_t size)
void PH_Set_Timeout_ms( uint64_t ms_ticks )
{
ph->rcv_buf = buf;
ph->size_rcv = size;
// log_i( " size_rcv %d", demo->size_rcv);
ph.timeout_ticks = ms_ticks;
}
int PH_Transmit( PH_TypeDef *ph, uint8_t * buf, uint16_t size )
int PH_Validate( )
{
// log_i( "SensorSend size_ %d", demo->size_send );
ph->timebase = HAL_GetTick();
UartInterface_Set_GPIO_For_Transmit( ph->uarthelper->interface );
// UartHelper_Send_TxPrepare_Callback( ph->uarthelper );
// ph->send_status = 1;
if ( ph->trans_type == 0 )
return HAL_UART_Transmit( ph->uarthelper->huart, buf, size ,0XFF);
if ( ph->trans_type == 1 )
return HAL_UART_Transmit_IT( ph->uarthelper->huart, buf, size );
if ( ph->trans_type == 2 )
return HAL_UART_Transmit_DMA( ph->uarthelper->huart, buf, size );
return -1;
// ph->send_flag = 0;
return 0;
}
void PH_Begin_Rcv( PH_TypeDef *ph, uint8_t * buf, uint16_t size)
int PH_CallBack( PH_TypeDef *pPH, uint8_t *buf, uint16_t size )
{
/* 是否开启空闲中断*/
if ( ph->idle_enable_disable == 1 )
{
// log_i(" ** idle enable...");
__HAL_UART_ENABLE_IT( ph->uarthelper->huart, UART_IT_IDLE );
}
if ( ph->trans_type == 0 )
log_i( " PH_CallBack -- state %d " , ph.state);
uint16_t size_tmp;
size_tmp =size;
switch (ph.state)
{
// TODO 发生错误怎么处理
HAL_UART_Receive( ph->uarthelper->huart, buf, size ,0X0FFF);
}
if ( ph->trans_type == 1 )
{
HAL_UART_Receive_IT( ph->uarthelper->huart, buf, size);
}
if ( ph->trans_type == 2 )
{
HAL_UART_Receive_DMA( ph->uarthelper->huart, buf, size);
}
}
case PH_State_Testing:
if (size>0)
{
ph.state++;
}
break;
case PH_State_Get_DATA_Wait:
if (size == 0) return 0;
if ((size + ph.size_received) >= sizeof(ph.result_buf) )
{
size_tmp = sizeof(ph.result_buf) - ph.size_received;
}
memcpy( (uint32_t)(ph.result_buf+ph.size_received), ph_uart_helper->rcv_buf,size_tmp);
ph.size_received += size_tmp;
void PH_Send_Cplt_Callback( PH_TypeDef *ph )
{
// ph->timebase = HAL_GetTick();
ph->status = Uart_Status_Send_Cplt;
if ( ph->interface_type != 0 )
{
UartInterface_Set_GPIO_For_Trans_Cplt( ph->uarthelper->interface );
// UartHelper_Set_GPIO_For_Trans_Cplt( ph->uarthelper );
if ( ph_uart_helper->enable_idle_it == 0 )
{
// Modbus 长度校验,拷贝数据?
if( ph.size_received == ( ph.result_buf[2]+ 5 ) )
{
ph.state++;
}
return 0;
}
if ( ph_uart_helper->enable_idle_it == 1 )
{
// 长度校验,拷贝数据?
if( ph.size_received == ( ph.result_buf[2]+ 5 ) )
{
ph.state++;
}
return 0;
}
break;
default:
break;
}
}
void PH_Rcv_Idle_Callback( PH_TypeDef *ph )
{
/* 搬运 数据到 kfifo*/
ph->status = 0;
ph->send_flag = 0;
}
void PH_Rcv_Cplt_Callback( PH_TypeDef *ph )
{
/* 搬运 数据到 kfifo*/
log_i( " PH_Rcv_Cplt_Callback 1 .... " );
ph->rcv_ok = 1 ;
// ph->status = 0;
// ph->send_flag = 1;
// if ( ph->mode < 2 )
// ph->command_seq++;
}
int PH_Get_Data_OK( PH_TypeDef *ph )
{
return ph->rcv_ok;
}
void PH_Set_Send_Flag( PH_TypeDef *ph )
{
// vPortEnterCritical();
ph->send_flag = 1;
ph->command_seq = 0;
ph->data_ok = 0;
// vPortExitCritical( );
return 0;
}
#endif
// UartHelper_Set_232 485 UartInterface
// UartInterface_Setup_Mode_232_485( ph->uarthelper->interface, my_pH.mode_232_485 );
// if (ph->interface_type == 0)
// UartInterface_Set_232( ph->uarthelper->interface);
// if (ph->interface_type == 1)
// UartInterface_Set_485( ph->uarthelper->interface);
// if (ph->interface_type ==2 && my_pH.mode_232_485 ==Uart_RSMODE_232)
// UartInterface_Set_232( ph->uarthelper->interface);
// if (ph->interface_type ==2 && my_pH.mode_232_485 ==Uart_RSMODE_485)
// UartInterface_Set_485( ph->uarthelper->interface);
// log_i( " interface-> %d %d (0:default, 1:485, 2:3160) " ,ph->uarthelper->interface_type ,ph->uarthelper->interface->interface_type );
// log_i( "ph 232_485 -> %d %d (0:232 ,1: 485) " ,ph->uarthelper->mode_232_485, ph->uarthelper->interface->mode_232_485 );
// CRCValue = HAL_CRC_Calculate(&hcrc, (uint32_t *)dataBuffer, BUFFER_SIZE);
// void PH_Set_Interface( PH_TypeDef *ph, UartInterface_TypeDef * interface )
// {
// UartHelper_Set_Interface( ph.uarthelper, interface );
// }
// void PH_Set_Huart( PH_TypeDef *ph, UART_HandleTypeDef * huart )
// {
// ph.uarthelper->huart = huart;
// // UartHelper_Set_Huart( ph.uarthelper, huart );
// }
// void PH_Set_Interface_Type( PH_TypeDef *ph, Uart_Interface_Type_Typedef interface_type )
// {
// ph.interface_type = interface_type;
// ph.uarthelper->interface_type = interface_type;
// UartInterface_Setup_Interface_type( ph.uarthelper->interface ,ph.interface_type);
// }

@ -0,0 +1,407 @@
#include "temperature.h"
osThreadId_t TEMP_Handle;
const osThreadAttr_t TEMP_attributes = {
.name = "TEMP",
.stack_size = 1024,
.priority = (osPriority_t) osPriorityBelowNormal,
};
// 事件
osEventFlagsId_t TEMP_EventHandle;
const osEventFlagsAttr_t TEMP_Event_attributes = {
.name = "TEMP_Event"
};
extern UART_HandleTypeDef huart3;
UART_HandleTypeDef *pTEMPUart = &huart3;
uint8_t TEMP_TEST_BUF[8] ={0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09};
uint8_t TEMP_TEST_BUF2[8] ={0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09};
uint8_t TEMP_TRANS_BUF[8] ={0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x44, 0x09};
uint8_t TEMP_RCV_BUF[14] ={0};
UART_HELPER_TypeDef *temp_uart_helper;
TEMP_TypeDef temp ={
TEMP_Init,
TEMP_Port,
TEMP_Test,
TEMP_Start,
TEMP_Stop,
NULL,
0, // state
0, // data_ok
{0}, // buf
0, // size_received
0, // time base
1000, // timeout ticks, 1000ms = 1S
0, // event_flag
NULL, // transRecord[3]
};
int TEMP_Init( )
{
temp_uart_helper = UART_HELPER_Init( );
if (temp_uart_helper ==NULL) return -1;
// TODO 接口
UART_HELPER_Set_Huart( temp_uart_helper, pTEMPUart );
UART_HELPER_Set_Interface_Type(temp_uart_helper, Uart_Interface_Max3160_485);
// 传输
UART_HELPER_Setup_Trans_mode( temp_uart_helper, Uart_Trans_DMA);
// UART_HELPER_Setup_Rcv_mode( temp_uart_helper, Uart_Trans_IT );
UART_HELPER_Set_enable_idle( temp_uart_helper, Uart_IDLE_IT_ENABLE);
// 回调GPIO 操作 数据操作
UART_HELPER_Set_trans_GPIO( temp_uart_helper, TEMP_Trans_GPIO ); // enbale rcv
UART_HELPER_Set_trans_cplt_GPIO( temp_uart_helper, TEMP_Trans_Cplt_GPIO );
UART_HELPER_Set_Callback( temp_uart_helper,&temp, TEMP_CallBack );
// 设置 Buf
UART_HELPER_Set_Rcv_Buf(temp_uart_helper, TEMP_RCV_BUF, sizeof(TEMP_RCV_BUF));
UART_HELPER_Set_Trans_Buf( temp_uart_helper, TEMP_TRANS_BUF, sizeof(TEMP_TRANS_BUF) );
// GPIO 操作
switch (temp_uart_helper->interface_type)
{
case Uart_Interface_Default:
break;
case Uart_Interface_485:
usart6_send_enable();
break;
case Uart_Interface_Max3160_232:
TEMP_MAX3160_232();
break;
case Uart_Interface_Max3160_485:
max3160_485_receive_mode();
break;
default:
break;
}
temp.state = TEMP_State_Waiting;
temp.timebase_ticks = osKernelGetTickCount( );
// TODO enable trans queue
// 最大队列数 8, 消息类型, 值传递(内含的buf为指针,如何包含大空间的buf,可选择指针传递 )
temp.transQueue = osMessageQueueNew( 8, sizeof(Record_Trans_Rcv_TypeDef), NULL);
}
void TEMP_Port( )
{
TEMP_Handle = osThreadNew( TEMP_Task, NULL, &TEMP_attributes );
// TEMP_EventHandle = osEventFlagsNew( &TEMP_Event_attributes );
}
int TEMP_Test( )
{
// 装载要发送的消息队列
temp.transRecord[0].size = sizeof(TEMP_TEST_BUF);
temp.transRecord[0].buf = TEMP_TEST_BUF;
// log_i( " temp.transRecord[0] buf %d, addr %d " , temp.transRecord[0].buf[0], temp.transRecord[0].buf );
// log_i( " TEMP_TEST_BUF buf %d, addr %d " ,TEMP_TEST_BUF[0], TEMP_TEST_BUF );
osMessageQueuePut( temp.transQueue, (void*)&temp.transRecord[0], 0, osWaitForever );
// 改变操作状态
temp.state = TEMP_State_Test; // 操作状态
return 0;
}
void TEMP_Start( )
{
// 装载要发送的消息队列 , 注意队列大小及
temp.transRecord[0].size = sizeof(TEMP_TEST_BUF);
temp.transRecord[0].buf = TEMP_TEST_BUF;
osMessageQueuePut( temp.transQueue, (void*)&temp.transRecord[0], 0, osWaitForever );
temp.transRecord[1].size = sizeof(TEMP_TEST_BUF2);
temp.transRecord[1].buf = TEMP_TEST_BUF2;
osMessageQueuePut( temp.transQueue, (void*)&temp.transRecord[1], 0, osWaitForever );
temp.state = TEMP_State_Get_DATA; // 操作状态
// osEventFlagsSet( TEMP_EventHandle, TEMP_Event_Get_Data ); // 事件
}
void TEMP_Stop( )
{
temp.state = TEMP_State_Stop;
// TODO stop task?
}
void TEMP_Task( )
{
// log_i( " ph tran mode : %d ", temp.->trans_mode );
// memset( TEMP_RCV_Buf, 0, 2*TEMP_Rcv_Buf_Size ); /* ph*3 ph *2 否则内存泄漏 */
temp.event_flag = 0;
uint64_t ticks;
int err_con = 0;
int st;
Record_Trans_Rcv_TypeDef msgQueue;
int count_msg;
for ( ; ; )
{
switch (temp.state)
{
case TEMP_State_Waiting:
log_i( " temp task..... : %d %d " , temp.state , temp.state );
osDelay(20);
break;
case TEMP_State_Test:
// log_i( " temp test..... : %d QueueGetCount %d " , temp.state , osMessageQueueGetCount(temp.transQueue) );
if (osMessageQueueGetCount(temp.transQueue) == 0){
temp.state = TEMP_State_Error;
break;
}
// 取队列 值传递, 第三个参数为NULL 否则为消息长度
osMessageQueueGet( temp.transQueue, &msgQueue, sizeof(Record_Trans_Rcv_TypeDef), osWaitForever );
// log_i( " msgQueue %d %d " , msgQueue.size, msgQueue.buf );
// log_i( " msgQueue %d " , TEMP_TEST_BUF );
temp.state = TEMP_State_Test_Start;
continue;
case TEMP_State_Test_Start:
// log_i( " temp TEMP_State_Test_Start..... : %d %d " , temp.state , temp.state );
UART_HELPER_Set_Trans_Buf( temp_uart_helper, msgQueue.buf, msgQueue.size );
TEMP_Receive();
TEMP_Transmit();
temp.state = TEMP_State_Testing;
continue;
case TEMP_State_Testing:
if ( osKernelGetTickCount() - ticks > 2000 )
{
err_con++;
if( err_con >3)
{
log_w( " TEMP testing ******** error " );
temp.state = TEMP_State_Error;
}
temp.state = TEMP_State_Test;
}
osDelay(1);
continue;
case TEMP_State_Test_OK:
log_i( " temp test ok..... : %d %d " , temp.state , temp.state );
// TODO 判断队列是否有元素
if (osMessageQueueGetCount(temp.transQueue) == 0)
{
temp.state = TEMP_State_Ready;
// TODO 停止DMA
if (temp_uart_helper->rcv_mode == Uart_Trans_DMA)
{
HAL_UART_DMAStop( temp_uart_helper->huart );
}
osEventFlagsSet(TEMP_EventHandle,TEMP_Event_Ready);
continue;
}else{
osMessageQueueGet( temp.transQueue, &msgQueue, sizeof(Record_Trans_Rcv_TypeDef), osWaitForever );
// osEventFlagsClear(TEMP_EventHandle,TEMP_Event_Test_OK);
temp.state = TEMP_State_Testing;
continue;
}
break;
case TEMP_State_Ready:
// log_i( " temp TEMP_State_Ready ..... " );
osDelay(1);
continue;
case TEMP_State_Get_DATA:
log_i( " temp TEMP_State_Get_DATA ..... " );
temp.data_ok = 0 ;
temp.size_received = 0 ;
memset( temp.result_buf, 0, sizeof(temp.result_buf) );
osEventFlagsClear(TEMP_EventHandle,TEMP_Event_Get_Data);
// 队列是否有值
if (osMessageQueueGetCount(temp.transQueue) == 0){
temp.state = TEMP_State_Error;
break;
}
osMessageQueueGet( temp.transQueue, &msgQueue, sizeof(Record_Trans_Rcv_TypeDef), osWaitForever );
UART_HELPER_Set_Trans_Buf( temp_uart_helper, msgQueue.buf, msgQueue.size );
TEMP_Receive();
TEMP_Transmit();
temp.state = TEMP_State_Get_DATA_OK;
continue;
case TEMP_State_Get_DATA_Wait:
if ( osKernelGetTickCount() - ticks > 2000 )
{
err_con++;
if( err_con >3)
{
log_w( " TEMP testing ******** error " );
temp.state = TEMP_State_Error;
}
temp.state = TEMP_State_Get_DATA;
}
osDelay(1);
continue;
case TEMP_State_Get_DATA_OK:
log_w( " TEMP data ok ******** " );
// TODO 停止DMA
// if (ph_uart_helper->rcv_mode == Uart_Trans_DMA)
// {
// HAL_UART_DMAStop( ph_uart_helper->huart );
// }
temp.data_ok = 0 ;
temp.state = TEMP_State_Get_DATA_Check ;
continue;
case TEMP_State_Get_DATA_Check:
log_w( " PH data check ******** " );
if(TEMP_Validate( )== 0)
{
temp.state = TEMP_State_Stop;
}else{
temp.state = TEMP_State_Error;
}
osDelay(10);
continue;
case TEMP_State_Stop:
osDelay(10);
continue;
case TEMP_State_Timeout:
log_e( " pH timeout ..... " );
temp.state = TEMP_State_Waiting ;
// TODO 停止DMA
if (temp_uart_helper->rcv_mode == Uart_Trans_DMA)
{
HAL_UART_DMAStop( temp_uart_helper->huart );
}
osDelay(5);
break;
case TEMP_State_Error:
log_e( " temp error ..... " );
temp.state = TEMP_State_Waiting ;
// TODO 停止DMA
if (temp_uart_helper->rcv_mode == Uart_Trans_DMA)
{
HAL_UART_DMAStop( temp_uart_helper->huart );
}
osDelay(5);
break;
}
osDelay(20);
}
}
void TEMP_Trans_GPIO(void)
{
// HAL_GPIO_WritePin(R_W_GPIO_Port,R_W_Pin, SET);
// HAL_GPIO_WritePin(NULL,0, SET);
HAL_GPIO_WritePin(SEL_232_485_GPIO_Port,SEL_232_485_Pin, SET);
HAL_GPIO_WritePin(HDPLX_GPIO_Port,HDPLX_Pin, RESET);
HAL_GPIO_WritePin(DE485_GPIO_Port, DE485_Pin, SET);
}
void TEMP_Trans_Cplt_GPIO(void)
{
// HAL_GPIO_WritePin(R_W_GPIO_Port,R_W_Pin, RESET);
// HAL_GPIO_WritePin(NULL,0, RESET);
HAL_GPIO_WritePin(SEL_232_485_GPIO_Port,SEL_232_485_Pin, SET);
HAL_GPIO_WritePin(HDPLX_GPIO_Port,HDPLX_Pin, SET);
HAL_GPIO_WritePin(DE485_GPIO_Port, DE485_Pin, RESET);
}
int TEMP_Transmit()
{
// TODO 结合队列
temp.size_received =0;
if( temp_uart_helper->transferring == 0)
{
return UART_HELPER_Trans(temp_uart_helper, temp_uart_helper->trans_record->buf, temp_uart_helper->trans_record->size );
}
return 0;
}
int TEMP_Receive()
{
return UART_HELPER_Start_Rcv(temp_uart_helper, temp_uart_helper->rcv_buf, temp_uart_helper->rcv_size);
// return 0;
}
void TEMP_Set_Timeout_ms( uint64_t ms_ticks )
{
temp.timeout_ticks = ms_ticks;
}
int TEMP_Validate( )
{
return 0;
}
int TEMP_CallBack( TEMP_TypeDef *pPH, uint8_t *buf, uint16_t size )
{
log_i( " TEMP_CallBack -- state %d " , temp.state);
uint16_t size_tmp;
size_tmp =size;
switch (temp.state)
{
case TEMP_State_Testing:
if (size>0)
{
temp.state++;
}
break;
case TEMP_State_Get_DATA_Wait:
if (size == 0) return 0;
if ((size + temp.size_received) >= sizeof(temp.result_buf) )
{
size_tmp = sizeof(temp.result_buf) - temp.size_received;
}
memcpy( (uint32_t)(temp.result_buf+temp.size_received), temp_uart_helper->rcv_buf,size_tmp);
temp.size_received += size_tmp;
if ( temp_uart_helper->enable_idle_it == 0 )
{
// Modbus 长度校验,拷贝数据?
if( temp.size_received == ( temp.result_buf[2]+ 5 ) )
{
temp.state++;
}
return 0;
}
if ( temp_uart_helper->enable_idle_it == 1 )
{
// 长度校验,拷贝数据?
if( temp.size_received == ( temp.result_buf[2]+ 5 ) )
{
temp.state++;
}
return 0;
}
break;
default:
break;
}
return 0;
}
// void TEMP_Set_Interface( TEMP_TypeDef *ph, UartInterface_TypeDef * interface )
// {
// UartHelper_Set_Interface( temp.uarthelper, interface );
// }
// void TEMP_Set_Huart( TEMP_TypeDef *ph, UART_HandleTypeDef * huart )
// {
// temp.uarthelper->huart = huart;
// // UartHelper_Set_Huart( temp.uarthelper, huart );
// }
// void TEMP_Set_Interface_Type( TEMP_TypeDef *ph, Uart_Interface_Type_Typedef interface_type )
// {
// temp.interface_type = interface_type;
// temp.uarthelper->interface_type = interface_type;
// UartInterface_Setup_Interface_type( temp.uarthelper->interface ,temp.interface_type);
// }

@ -1,315 +0,0 @@
#include "uart_helper.h"
#include "elog.h"
UartHelper_TypeDef * UartHelper_Init( )
{
UartHelper_TypeDef *Handle = (UartHelper_TypeDef *)malloc(sizeof(UartHelper_TypeDef));
if (Handle == NULL)
{
return NULL;
}
// Handle->interface = UartInterface_Init();
Handle->interface = UartInterface_Init();
// Handle->interface_type = 0;
Handle->interface_type = 0; /* 0: common, 1: 485 ,2:3160*/
Handle->mode_232_485 = 0; /* 0: common, 1: 485 */
Handle->trans_type = 2; /* 0 :polling, 1: IT 2: DMA*/
Handle->rcv_type = 2; /* 0 :polling, 1: IT 2: DMA*/
Handle->idle_enable_disable = 1; /* 0 不开启空闲中断 , 1 开启空闲中断 */
Handle->trans_mode = 2; /* 0 :polling, 1: IT 2: DMA*/
Handle->status = Uart_Status_Ready; /* 0 :polling, 1: IT 2: DMA*/
Handle->huart = NULL;
Handle->obj = NULL;
Handle->callback = NULL;
// Handle->timeout_enable = 0;
// Handle->timeout_ms = 0;
// Handle->timebase_ticks = 0;
return Handle;
}
void UartHelper_Set_Interface( UartHelper_TypeDef *uarthelper, UartInterface_TypeDef * interface )
{
uarthelper->interface = interface;
}
void UartHelper_Set_Huart( UartHelper_TypeDef *uarthelper, UART_HandleTypeDef * huart )
{
uarthelper->huart = huart;
}
void UartHelper_Set_Interface_Type( UartHelper_TypeDef *uarthelper, Uart_Interface_Type_Typedef interface_type )
{
uarthelper->interface_type = interface_type;
}
void UartHelper_Set_Trans_Type( UartHelper_TypeDef *uarthelper, Uart_Transmode_TypeDef trans_type )
{
uarthelper->trans_type = trans_type;
}
void UartHelper_Set_Rcv_Type( UartHelper_TypeDef *uarthelper, Uart_Transmode_TypeDef rcv_type )
{
uarthelper->rcv_type = rcv_type;
}
void UartHelper_Set_Idle_Enable( UartHelper_TypeDef *uarthelper, Uart_IDLE_Enable_TypeDef idle_enable_disable )
{
uarthelper->idle_enable_disable = idle_enable_disable;
}
void UartHelper_Set_TransMode( UartHelper_TypeDef *uarthelper, Uart_Transmode_TypeDef trans_mode )
{
uarthelper->trans_mode = trans_mode;
}
void UartHelper_Set_RsMode_232_485( UartHelper_TypeDef *uarthelper, Uart_RS_Mode_TypeDef mode_232_485 )
{
uarthelper->mode_232_485 = mode_232_485;
UartInterface_Setup_Mode_232_485( uarthelper->interface, uarthelper->mode_232_485);
}
// void UartHelper_Set_timeout( UartHelper_TypeDef *uarthelper, uint32_t timeout_ms )
// {
// uarthelper->timeout_ms = timeout_ms;
// uarthelper->timeout_enable = 1;
// }
int UartHelper_Set_callback_func_obj( UartHelper_TypeDef *uarthelper, void * obj, it_callback callback )
{
if(uarthelper==NULL || callback==NULL || obj==NULL){
return -1;
}
uarthelper->callback =callback;
uarthelper->obj =obj;
return 0;
}
void UartHelper_Set_rcv_buf( UartHelper_TypeDef *uarthelper, uint8_t *buf, uint32_t buf_size )
{
uarthelper->receive_buf = buf;
uarthelper->receive_buf_half_len = buf_size/2;
}
void UartHelper_Set_Trans_Buf( UartHelper_TypeDef *uarthelper, uint8_t *buf, uint32_t buf_size )
{
uarthelper->trans_buf = buf;
uarthelper->trans_size = buf_size;
}
int UartHelper_Transmit( UartHelper_TypeDef *uarthelper, uint8_t * buf, uint16_t size)
{
// log_i( "SensorSend size_ " );
// set gpio
UartInterface_Set_GPIO_For_Transmit( uarthelper->interface );
// it idle start
// if ( uarthelper->idle_enable_disable == 1 )
// {
// log_i( "start idle .... ");
// __HAL_UART_ENABLE_IT(uarthelper->huart, UART_IT_IDLE);
// }
/* 开启 transmit */
if (buf!=NULL )
{
uarthelper->status = Uart_Status_Sending;
if ( uarthelper->trans_type == 0 )
return HAL_UART_Transmit( uarthelper->huart, buf, size ,0x0FFF);
if ( uarthelper->trans_type == 1 )
return HAL_UART_Transmit_IT( uarthelper->huart, buf, size );
if ( uarthelper->trans_type == 2 )
return HAL_UART_Transmit_DMA( uarthelper->huart, buf, size );
}
if (buf==NULL && uarthelper->trans_buf !=NULL)
{
uarthelper->status = Uart_Status_Sending;
if ( uarthelper->trans_type == 0 )
return HAL_UART_Transmit( uarthelper->huart, uarthelper->trans_buf, uarthelper->trans_size ,0x0FFF);
if ( uarthelper->trans_type == 1 )
return HAL_UART_Transmit_IT( uarthelper->huart, uarthelper->trans_buf, uarthelper->trans_size);
if ( uarthelper->trans_type == 2 )
return HAL_UART_Transmit_DMA( uarthelper->huart, uarthelper->trans_buf, uarthelper->trans_size);
}
return -1;
}
int UartHelper_Begin_Rcv(UartHelper_TypeDef *uarthelper, uint8_t * buf, uint16_t size)
{
/* 是否开启空闲中断*/
if ( uarthelper->idle_enable_disable == 1 )
{
__HAL_UART_ENABLE_IT( uarthelper->huart, UART_IT_IDLE );
}
if ( uarthelper->rcv_type == 0 )
{
// TODO 发生错误怎么处理
HAL_UART_Receive( uarthelper->huart, buf, size ,0X0FFF);
}
if ( uarthelper->rcv_type == 1 )
{
HAL_UART_Receive_IT( uarthelper->huart, buf, size);
}
if ( uarthelper->rcv_type == 2 )
{
// log_i( "start dma rcv.... . ");
HAL_UART_Receive_DMA( uarthelper->huart, buf, size);
}
}
void UartHelper_Set_GPIO_For_Trans_Cplt( UartHelper_TypeDef *uarthelper )
{
uarthelper->status = Uart_Status_Ready;
// log_i(" UartHelper_Set_GPIO_For_Trans_Cplt ... %d %d", uarthelper->interface_type, uarthelper->mode_232_485);
if (uarthelper->interface_type != Uart_Interface_Default && uarthelper->mode_232_485 ==Uart_RSMODE_485)
{
// log_i(" UartHelper_Set_GPIO_For_Trans_Cplt ... 111111");
UartHelper_Set_GPIO_For_Trans_Cplt( uarthelper->interface );
}
}
void UartHelper_Rcv_Cplt_Callback( UartHelper_TypeDef *uarthelper )
{
// TODO 超时 --> 尽量用空闲中断代替 接收指定长度完成中断
// TODO 拷贝数据
}
void UartHelper_Rcv_Idle_Callback( UartHelper_TypeDef *uarthelper )
{
/* 搬运 数据到 kfifo*/
log_i("SC6....huart3 IDLE....");
uint16_t count = __HAL_DMA_GET_COUNTER( uarthelper->huart->hdmarx );
uint16_t data_length = 2*uarthelper->receive_data_size - count;
// if (count != 0 && data_length != 0)
if ( data_length != 0 )
{
// TODO 拷贝数据 最后一位 datalenth-1
HAL_UART_DMAStop( uarthelper->huart );
uarthelper->callback( uarthelper->obj, uarthelper->receive_buf, data_length);
}
}
void UartHelper_Rcv_DMA_Half_Callback( UartHelper_TypeDef *uarthelper )
{
/* 循环模式才有 半满中断 */
}
int UartHelper_copy(UartHelper_TypeDef *uarthelper,uint8_t *buf,uint32_t buf_size)
{
int data_len;
if(uarthelper==NULL || buf==NULL){
return -1;
}
data_len = uarthelper->receive_data_size;
if(buf_size<data_len){
return -2;
}
if(data_len!=0){
// TODO
// memcpy(buf,uart_get_receive_buff(uart_buf),data_len);
uarthelper->receive_data_size=0;
}
return data_len;
}
int UartHelper_isbusy(UartHelper_TypeDef *uarthelper )
{
if (uarthelper->status==Uart_Status_Sending)
return 1;
return 0;
}
void UartHelper_error(UartHelper_TypeDef *uarthelper )
{
while(1);
}
uint16_t Get_Crc16(const char *buf, uint16_t len)
{
int crc = 0xffff;
for (int i = 0; i < len; i++)
{
crc ^= buf[i];
for (int j = 0; j < 8; j++)
{
if (crc & 1 == 1)
{
crc >>= 1;
crc ^= 0xa001;
}
else
{
crc >>= 1;
}
}
}
uint8_t cs1 = crc & 0xff; // 校验码的低字节
uint8_t cs2 = (crc & 0xff00) >> 8; // 校验码的高字节
return crc;
};
uint8_t Check_Crc16(uint8_t *buf, uint16_t length){
uint16_t len = length-2;
int crc = 0xffff;
for (int i = 0; i < len; i++)
{
crc ^= buf[i];
for (int j = 0; j < 8; j++)
{
if (crc & 1 == 1)
{
crc >>= 1;
crc ^= 0xa001;
}
else
{
crc >>= 1;
}
}
}
uint8_t cs1 = crc & 0xff; // 校验码的低字节
uint8_t cs2 = (crc & 0xff00) >> 8; // 校验码的高字节
log_i( " %d %d %d ",crc ,cs1, cs2 ) ;
log_i( " %d %d %d ",crc ,buf[length-2], buf[length-1] ) ;
if ( (crc & 0xff) == buf[length-2] && ((crc & 0xff00) >> 8) == buf[length-1])
{
return 1;
}
else
{
return 0;
}
}
// // HAL_UART_DMAStop HAL_UART_Receive_DMA 成对使用,可从指定指针开始
// // HAL_UART_DMAPause HAL_UART_DMAResume 成对使用
// HAL_UART_DMAStop( sc6->huart );
// memcpy(sc6->SC6_Data_Buf, sc6->SC6_Raw_Buf, data_length);
// sc6->size_received = data_length;
// log_i(" IDLE.... count %d data_length %d ",count,data_length );
// log_i(" IDLE.... -> %02X %02X %02X %02X ", sc6->SC6_Data_Buf[0] , sc6->SC6_Data_Buf[1], sc6->SC6_Data_Buf[2] , sc6->SC6_Data_Buf[3]);
// log_i(" IDLE.... -> %02X %02X %02X %02X ",
// sc6->SC6_Data_Buf[data_length] , sc6->SC6_Data_Buf[data_length-1], sc6->SC6_Data_Buf[data_length-2] , sc6->SC6_Data_Buf[data_length-3]);
// log_i(" IDLE.... -> %s ", sc6->SC6_Data_Buf );
// // TODO数据清零 DMA指针指向开始的地方 或双缓冲
// // memset(sc6->SC6_Data_Buf, 0, sizeof(sc6->SC6_Data_Buf));
// memset(sc6->SC6_Raw_Buf, 0, sizeof(sc6->SC6_Raw_Buf));
// // HAL_UART_DMAStop( sc6->myuart->huart );
// // SC6_Begin_Rcv( sc6, sc6->SC6_Raw_Buf, sizeof(sc6->SC6_Raw_Buf));
// __HAL_UART_ENABLE_IT( sc6->huart, UART_IT_IDLE);
// HAL_UART_Receive_DMA( sc6->huart, sc6->SC6_Raw_Buf, sizeof(sc6->SC6_Raw_Buf));
// // HAL_UART_DMAResume( sc6->myuart->huart );
/* 再次开启DMA接收 重新接收 */
// HAL_UART_Receive_DMA(&UartHandle, UartHandle.pRxBuffPtr, UartHandle.RxXferSize);

@ -1,241 +0,0 @@
#include "uart_interface.h"
#include "elog.h"
UartInterface_TypeDef * UartInterface_Init( )
{
UartInterface_TypeDef *Handle = (UartInterface_TypeDef *)malloc(sizeof(UartInterface_TypeDef));
if (Handle == NULL)
{
return NULL;
}
// Handle->send_status = 0;
Handle->interface_type = 0;
#if MAX485_ENABLE
Handle->de485_gpio = NULL;
Handle->interface_type = 1;
#endif
#if MAX3160_ENABLE
Handle->de485_gpio = NULL;
Handle->sel_gpio = NULL;
Handle->dplx_gpio = NULL;
Handle->interface_type = 2;
#endif
return Handle;
}
/**
* @brief interface-> 0:default, 1:485, 2:3160
* @param [in] myuart
* @param [in] interface
* @return int
*
* @details
*/
int UartInterface_Setup_Interface_type( UartInterface_TypeDef * interface, Uart_Interface_Type_Typedef interface_type )
{
interface->interface_type = interface_type;
return 0;
}
int UartInterface_Setup_Mode_232_485( UartInterface_TypeDef * interface, Uart_RS_Mode_TypeDef mode_232_485 )
{
interface->mode_232_485 = mode_232_485;
if (interface->interface_type == 0)
UartInterface_Set_232( interface);
if (interface->interface_type == 1)
UartInterface_Set_485( interface);
if (interface->interface_type ==2 && interface->mode_232_485 ==Uart_RSMODE_232)
UartInterface_Set_232( interface);
if (interface->interface_type ==2 && interface->mode_232_485 ==Uart_RSMODE_485)
UartInterface_Set_485( interface);
return 0;
}
void UartInterface_Set_Sel_GPIO_Pin(UartInterface_TypeDef * interface, GPIO_TypeDef *gpio, uint16_t pin)
{
if (gpio!=NULL && pin>=0)
{
#if MAX3160_ENABLE
interface->sel_gpio = gpio;
interface->sel_pin = pin;
#endif
}
}
void UartInterface_Set_Dplx_GPIO_Pin(UartInterface_TypeDef * interface, GPIO_TypeDef *gpio, uint16_t pin)
{
if (gpio!=NULL && pin>=0)
{
#if MAX3160_ENABLE
interface->dplx_gpio = gpio;
interface->dplx_pin = pin;
#endif
}
// #if MAX3160_ENABLE
// interface->dplx_gpio = gpio;
// interface->dplx_pin = pin;
// #endif
}
void UartInterface_Set_DE485_GPIO_Pin(UartInterface_TypeDef * interface, GPIO_TypeDef *gpio, uint16_t pin)
{
if (gpio!=NULL && pin>=0)
{
#if MAX3160_ENABLE
interface->de485_gpio = gpio;
interface->de485_pin = pin;
#endif
#if MAX485_ENABLE
interface->de485_gpio = gpio;
interface->de485_pin = pin;
#endif
}
// interface->de485_gpio = gpio;
// interface->de485_pin = pin;
}
void UartInterface_Set_232(UartInterface_TypeDef * interface)
{
interface->mode_232_485 = Uart_RSMODE_232;
#if MAX3160_ENABLE
if (interface->sel_gpio != NULL)
HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_FULL);
interface->sel = SEL_232;
if (interface->sel_gpio != NULL)
HAL_GPIO_WritePin( interface->sel_gpio, interface->sel_pin, interface->sel );
#endif
// interface->mode_232_485 = 0;
}
void UartInterface_Set_485(UartInterface_TypeDef * interface)
{
interface->mode_232_485 = Uart_RSMODE_485;
// log_i(".......set 485 ");
#if MAX3160_ENABLE
interface->sel = SEL_485;
if (interface->sel_gpio != NULL)
// log_i(".......set 485 set dplx ");
HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_HALF);
if (interface->de485_gpio != NULL)
// log_i(".......set 485 set de ");
HAL_GPIO_WritePin( interface->de485_gpio, interface->de485_pin, DISABLE_485_SEND );
if (interface->sel_gpio != NULL)
// log_i(".......set 485 set sel... ");
HAL_GPIO_WritePin( interface->sel_gpio, interface->sel_pin, interface->sel );
// log_i(" UartInterface_Set_485 222 interface->sel : %d duplex: %d ",interface->sel , HAL_GPIO_ReadPin(interface->dplx_gpio,interface->dplx_pin) );
#endif
#if MAX485_ENABLE
if (interface->de485_gpio != NULL)
// log_i(".......set 485 set de ");
HAL_GPIO_WritePin( interface->de485_gpio, interface->de485_pin, DISABLE_485_SEND );
#endif
}
/**
* @brief GPIO
* @param [in] myuart
* @return int
*
* @details
*/
int UartInterface_Set_GPIO_For_Transmit( UartInterface_TypeDef * interface )
// int UartInterface_Send_TxPrepare_Callback( UartInterface_TypeDef * interface )
{
#if MAX3160_ENABLE
if (interface->mode_232_485 == 0)
{
HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_FULL);
};
if ( interface->interface_type == 2 && interface->mode_232_485 == 1 )
{
// log_i( " ---- UartInterface_Set_GPIO_For_Transmit " );
HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_FULL); // 全双工启动发送
HAL_GPIO_WritePin(interface->de485_gpio, interface->de485_pin, ENABLE_485_SEND);
return 0;
}
#endif
#if MAX485_ENABLE
if (interface->interface_type == 1)
{
HAL_GPIO_WritePin(interface->de485_gpio, interface->de485_pin, ENABLE_485_SEND);
}
#endif
return 0;
// #if MAX3160_ENABLE
// if (interface->interface_type == 2 && )
// {
// switch (interface->sel)
// {
// case SEL_232:
// HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_FULL);
// // return __UartInterface_Send(myuart,buf,size);
// break;
// case SEL_485:
// log_i(" 485 mode writepin...... ");
// HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_FULL);
// HAL_GPIO_WritePin( interface->de485_gpio, interface->de485_pin, ENABLE_485_SEND);
// // return __UartInterface_Send(myuart,buf,size);
// break;
// default:
// break;
// }
// }
// #endif
// #if MAX485_ENABLE
// if (interface->interface_type == 1)
// {
// HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_FULL);
// HAL_GPIO_WritePin( interface->de485_gpio, interface->de485_pin, ENABLE_485_SEND); // return __Max3160_Send(max3160,buf,size);
// }
// #endif
}
/**
* @brief GPIO
* @param [in] myuart
* @return int
*
* @details
*/
// 接收完成,切换到接收状态, 全双工接收
int UartInterface_Set_GPIO_For_Trans_Cplt( UartInterface_TypeDef * interface )
{
// interface->send_status = 0;
#if MAX3160_ENABLE
if ( interface->interface_type == 2 && interface->mode_232_485 == 1)
{
HAL_GPIO_WritePin(interface->dplx_gpio, interface->dplx_pin , DUPLEX_HALF); // 半双工接收
HAL_GPIO_WritePin(interface->de485_gpio, interface->de485_pin, DISABLE_485_SEND);
return 0;
}
if (interface->mode_232_485 == 0) return -1;
#endif
#if MAX485_ENABLE
if (interface->interface_type == 1)
{
HAL_GPIO_WritePin(interface->de485_gpio, interface->de485_pin, DISABLE_485_SEND);
return 0;
}
if (interface->mode_232_485 == 0) return -1;
#endif
return 0;
}
Loading…
Cancel
Save