qcc304x笔记之i2c运用(八)
程序员文章站
2022-03-22 08:34:09
这里以驱动ST的gsensor--lis2dh12为例流程分析首先根据自己的原理图配置pio,注意这里是16进制显示添加一个task以及注册taskHandler用于管理与外设通讯,将task注册给PioMonitor初始化pio实现中断处理即taskhandler配置gsensor的寄存器代码实现gSenorTaskData gSenorTask;/*pio中断处理函数*/static void Lis2dh12InterruptHandler(Task task,...
这里以驱动ST的gsensor--lis2dh12为例
流程分析
- 首先根据自己的原理图配置pio,注意这里是16进制显示
- 添加一个task以及注册taskHandler用于管理与外设通讯,将task注册给PioMonitor
- 初始化pio
- 实现中断处理即taskhandler
- 配置gsensor的寄存器
代码实现
gSenorTaskData gSenorTask;
/*pio中断处理函数*/
static void Lis2dh12InterruptHandler(Task task, MessageId id, Message msg)
{
gSenorTaskData *accel = (gSenorTaskData *)task;
/* Handle internal messages*/
switch (id)
{
case MESSAGE_PIO_CHANGED:
{
const MessagePioChanged *mpc = (const MessagePioChanged *)msg;
uint32 state = mpc->state16to31 << 16 | mpc->state;
uint16 bank = PIO2BANK(LIS2DH12_INTERRUPT1_PIO);
uint32 mask = PIO2MASK(LIS2DH12_INTERRUPT1_PIO);
if (mpc->bank == bank)
{
}
}
break;
}
}
/*iic与中断的初始化*/
static void Lis2dh12PioConfig(void)
{
uint16 bank;
uint32 mask;
/* Setup Interrupt as input with weak pull up */
bank = PIO2BANK(LIS2DH12_INTERRUPT1_PIO);
mask = PIO2MASK(LIS2DH12_INTERRUPT1_PIO);
PanicNotZero(PioSetMapPins32Bank(bank, mask, mask));
PanicNotZero(PioSetDir32Bank(bank, mask, 0));
/*PanicNotZero(PioSet32Bank(bank, mask, mask));*/
PanicNotZero(PioSet32Bank(bank, mask, 0));
/* Setup i2c scl as output with weak pull up */
bank = PIO2BANK(LIS2DH12_SCL_PIO);
mask = PIO2MASK(LIS2DH12_SCL_PIO);
PioSetMapPins32Bank(bank, mask, mask);
PioSetDir32Bank(bank, mask, mask);
PioSet32Bank(bank, mask, mask);
/* Setup i2c sda as output with weak pull up */
bank = PIO2BANK(LIS2DH12_SDA_PIO);
mask = PIO2MASK(LIS2DH12_SDA_PIO);
PioSetMapPins32Bank(bank, mask, mask);
PioSetDir32Bank(bank, mask, mask);
PioSet32Bank(bank, mask, mask);
}
/*配置gsensor寄存器*/
static void Lis2dh12RegsConfig(void)
{
uint16 table_length = ARRAY_DIM(lis2dh12_reg_init_table);
uint16 index = 0;
uint8 packet[2];
uint16 ack;
for(index;index < table_length;index++){
packet[0] = lis2dh12_reg_init_table[index].reg;
packet[1] = lis2dh12_reg_init_table[index].reg_val;
ack = I2cTransfer(LIS2DH12_DEFAULT_ADDRESS_H, packet, 2, NULL, 0);
PanicZero(ack);
}
}
/*读gsensor寄存器的值*/
void Lis2dh12RegRead(uint8 reg, uint8 *data, uint16 len)
{
uint16 ack = 0;
ack = I2cTransfer(LIS2DH12_DEFAULT_ADDRESS_H, ®, 1, data, len);
PanicZero(ack);
}
/*创建一个task(gSenorTask),并将task注册给PioMonitor,这样当中断上来的时候,会发io changed的消息到当前的task*/
static bool GsenorClientRegister(Task client_task)
{
gSenorTaskData *accel = GsenorGetTaskData();
if (NULL == accel->client)
{
accel->client = TaskList_Create();
Lis2dh12PioConfig();
/* Register for interrupt events */
accel->task.handler = Lis2dh12InterruptHandler;
PioMonitorRegisterTask(&accel->task, LIS2DH12_INTERRUPT1_PIO);
/*MessageSend(client_task, getMessage(PioGet32Bank(PIO2BANK(LIS2DH12_INTERRUPT1_PIO)), PIO2MASK(LIS2DH12_INTERRUPT1_PIO)), NULL);*/
return TaskList_AddTask(accel->client, client_task);
}
return FALSE;
}
注意这里用的i2c通讯接口(I2cTransfer)为低速接口,高速接口请参考touchpad使用的高速通讯接口(BitserialChangeParam—BitserialTransfer--BitserialRead)
相关的工具资料等,请到我的博客<qcc514x_qcc304x开发调试笔记>最下面的网盘链接中下载。
本文地址:https://blog.csdn.net/ylangeia/article/details/107287431