先用RASC配置IIC的两个引脚,查看原理图那个4Pin(5V、GND、SCL、SDA)的IIC接口,使用的是P408和P409,所以先在工程里配置SCI3的IIC功能。
然后添加一个线程,命名为HS3003_Task
再添加线程功能,选择SCI_IIC
添加后设置IIC功能
然后找到了官方提供的资料,有读取HS3003的相关代码,稍微做亿点点改造,改成模块的形式(模块的接口先这么处理,等后面研究ST的IIC接口形式再动一下)。
模块HS3003_Dr.c代码如下:
#include "HS3003_Dr.h"
#define HS3003_COMM_SIZE (4)
#define HS3003_REQ_DELAY (10)
#define HS3003_DATA_GET_DELAY (100)
enum emHS3003CommSta
{
HS3003COMM_STA_idle = 0,
HS3003COMM_STA_write = 1,
HS3003COMM_STA_read = 2,
HS3003COMM_STA_getdata = 3,
};
struct tagHS3003InFunc
{
uint8_t (*Init)(void *);
void (*Read)(void *,
uint8_t *,
uint32_t);
void (*Write)(void *,
uint8_t *,
uint32_t);
void *Pt;
};
struct tagHS3003Output
{
int32_t RH;
int32_t TEMP;
};
struct tagHS3003Prv
{
uint8_t Buf[HS3003_COMM_SIZE];
uint32_t Cnt:30;
uint32_t Sta:2;
};
struct tagHS3003CB
{
struct tagHS3003InFunc InFunc;
struct tagHS3003Output Output;
struct tagHS3003Prv Prv;
};
uint8_t HS3003_DeInit(uint32_t *hscb)
{
struct tagHS3003CB *cb = (struct tagHS3003CB *)hscb;
if (!cb)
{
return 0;
}
for (uint8_t i = 0; i < HS3003_COMM_SIZE; i++)
{
cb->Prv.Buf[i] = 0;
}
cb->Output.RH = 0;
cb->Output.TEMP = 0;
cb->Prv.Cnt = 0;
cb->Prv.Sta = HS3003COMM_STA_idle;
cb->InFunc.Init = (void *)0;
cb->InFunc.Read = (void *)0;
cb->InFunc.Write = (void *)0;
cb->InFunc.Pt = (void *)0;
return 1;
}
uint8_t HS3003_Init(uint32_t *hscb)
{
struct tagHS3003CB *cb = (struct tagHS3003CB *)hscb;
if (!cb)
{
return 0;
}
cb->Prv.Sta = HS3003COMM_STA_write;
return cb->InFunc.Init(cb->InFunc.Pt);
}
uint8_t HS3003_InitFunc(uint32_t *hscb,
uint8_t (*init)(void *),
void (*read)(void *, uint8_t *, uint32_t),
void (*write)(void *, uint8_t *, uint32_t),
void *pt)
{
struct tagHS3003CB *cb = (struct tagHS3003CB *)hscb;
if (!cb)
{
return 0;
}
cb->InFunc.Init = init;
cb->InFunc.Read = read;
cb->InFunc.Write = write;
cb->InFunc.Pt = pt;
return 1;
}
uint8_t HS3003_Calc(uint32_t *hscb)
{
struct tagHS3003CB *cb = (struct tagHS3003CB *)hscb;
if (!cb)
{
return 0;
}
cb->Prv.Cnt++;
switch (cb->Prv.Sta)
{
case HS3003COMM_STA_idle:
{
cb->Prv.Cnt = 0;
break;
}
case HS3003COMM_STA_write:
{
cb->InFunc.Write(cb->InFunc.Pt, cb->Prv.Buf, 4);
cb->Prv.Cnt = 0;
cb->Prv.Sta = HS3003COMM_STA_read;
break;
}
case HS3003COMM_STA_read:
{
if (cb->Prv.Cnt < HS3003_REQ_DELAY)
{
break;
}
cb->InFunc.Read(cb->InFunc.Pt, cb->Prv.Buf, 4);
cb->Prv.Cnt = 0;
cb->Prv.Sta = HS3003COMM_STA_getdata;
break;
}
case HS3003COMM_STA_getdata:
{
if (cb->Prv.Cnt < HS3003_DATA_GET_DELAY)
{
break;
}
cb->Output.RH =(int32_t)(((uint32_t)(cb->Prv.Buf[0] & 0x3f)<< 8)+ cb->Prv.Buf[1]) * 10000 / 16383;
cb->Output.TEMP =(int32_t)((((uint32_t)cb->Prv.Buf[2]<<8)+cb->Prv.Buf[3])>> 2) * 16500 / 16383 - 4000;
cb->Prv.Cnt = 0;
cb->Prv.Sta = HS3003COMM_STA_write;
break;
}
default:
{
cb->Prv.Cnt = 0;
cb->Prv.Sta = HS3003COMM_STA_idle;
break;
}
}
return 1;
}
int32_t HS3003_GetRH(uint32_t *hscb)
{
struct tagHS3003CB *cb = (struct tagHS3003CB *)hscb;
if (!cb)
{
return 0;
}
return cb->Output.RH;
}
int32_t HS3003_GetTemp(uint32_t *hscb)
{
struct tagHS3003CB *cb = (struct tagHS3003CB *)hscb;
if (!cb)
{
return 0;
}
return cb->Output.TEMP;
}
模块HS3003_Dr.h代码如下:
#ifndef _HS3003_DR_H_
#define _HS3003_DR_H_
#include <stdint.h>
#define HS3003_MODULE_SIZE (32)
#define HS3003_Declare(hscb) uint32_t hscb[(HS3003_MODULE_SIZE + 3) / 4]
uint8_t HS3003_DeInit(uint32_t *hscb);
uint8_t HS3003_Init(uint32_t *hscb);
uint8_t HS3003_InitFunc(uint32_t *hscb,
uint8_t (*init)(void *),
void (*read)(void *, uint8_t *, uint32_t),
void (*write)(void *, uint8_t *, uint32_t),
void *pt);
uint8_t HS3003_Calc(uint32_t *hscb);
int32_t HS3003_GetRH(uint32_t *hscb);
int32_t HS3003_GetTemp(uint32_t *hscb);
#endif
使用实例如下:
#include "HS3003_Task.h"
#include "HS3003_Dr.h"
#include "hal_data.h"
struct tagHS3003Task
{
HS3003_Declare(HS3003CB);
int32_t RH;
int32_t Temp;
};
struct tagHS3003Task HumTemp;
uint8_t IIC_Init(void *cb)
{
const i2c_master_instance_t *hs3003 = (const i2c_master_instance_t *)cb;
return (uint8_t)R_SCI_I2C_Open(hs3003->p_ctrl, hs3003->p_cfg);
}
void IIC_Read(void *cb, uint8_t *buf, uint32_t size)
{
const i2c_master_instance_t *hs3003 = (const i2c_master_instance_t *)cb;
R_SCI_I2C_Read(hs3003->p_ctrl, buf, size, 0);
}
void IIC_Write(void *cb, uint8_t *buf, uint32_t size)
{
const i2c_master_instance_t *hs3003 = (const i2c_master_instance_t *)cb;
R_SCI_I2C_Write(hs3003->p_ctrl, buf, size, 0);
}
void hs3003_i2c3_callback(i2c_master_callback_args_t * p_args)
{
{
}
{
}
}
void HS3003_Task_entry(void * pvParameters)
{
FSP_PARAMETER_NOT_USED(pvParameters);
HS3003_DeInit(HumTemp.HS3003CB);
HS3003_InitFunc(HumTemp.HS3003CB,
IIC_Init,
IIC_Read,
IIC_Write,
(void *)&HS3003_I2C3);
HS3003_Init(HumTemp.HS3003CB);
while(1)
{
HS3003_Calc(HumTemp.HS3003CB);
HumTemp.RH = HS3003_GetRH(HumTemp.HS3003CB);
HumTemp.Temp = HS3003_GetTemp(HumTemp.HS3003CB);
vTaskDelay(1);
}
}