118 lines
2.3 KiB
C
118 lines
2.3 KiB
C
![]() |
#include <stdio.h>
|
|||
|
#include "ohos_init.h"
|
|||
|
#include "iot_gpio.h"
|
|||
|
#include "iot_gpio_ex.h"
|
|||
|
#include "iot_i2c.h"
|
|||
|
#include "cmsis_os2.h"
|
|||
|
#define IIC_SDA 0
|
|||
|
#define IIC_SCL 1
|
|||
|
#define ADDR 0x27 // 0100111
|
|||
|
#define IIC_IDX 1
|
|||
|
|
|||
|
/*
|
|||
|
Command Register
|
|||
|
0 Input port 0
|
|||
|
1 Input port 1
|
|||
|
2 Output port 0
|
|||
|
3 Output port 1
|
|||
|
4 Polarity Inversion port 0
|
|||
|
5 Polarity Inversion port 1
|
|||
|
6 Con?guration port 0
|
|||
|
7 Con?guration port 1
|
|||
|
*/
|
|||
|
#define CMD_CFG0 6
|
|||
|
#define CMD_CFG1 7
|
|||
|
#define CMD_OUT0 2
|
|||
|
#define CMD_OUT1 3
|
|||
|
|
|||
|
int write_iic(uint8_t* data)
|
|||
|
{
|
|||
|
int ret = IoTI2cWrite(IIC_IDX, (ADDR << 1) | 0x00, data, 3);
|
|||
|
return ret;
|
|||
|
}
|
|||
|
|
|||
|
//start
|
|||
|
uint8_t CFG0[] = {CMD_CFG0,0x0,0x0}; //<2F><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
|
|||
|
uint8_t CFG1[] = {CMD_CFG1,0x0,0x0}; //<2F><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
|
|||
|
uint8_t OUT0[] = {CMD_OUT0,0x00,0x00}; // <20><><EFBFBD><EFBFBD>
|
|||
|
uint8_t OUT1[] = {CMD_OUT1,0x00,0x00}; // <20><><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
char alpha[8][9] =
|
|||
|
{
|
|||
|
"11111111",
|
|||
|
"11111111",
|
|||
|
"11100000",
|
|||
|
"11111111",
|
|||
|
"11111111",
|
|||
|
"11100000",
|
|||
|
"11100000",
|
|||
|
"11111111"
|
|||
|
};
|
|||
|
void write_data(char byte1,char byte2)
|
|||
|
{
|
|||
|
//printf("write %02x ,%02x\n",byte1,byte2);
|
|||
|
uint8_t data[3] = {CMD_OUT0,0x00,0x00};
|
|||
|
data[1] = byte1;
|
|||
|
data[2] = byte2;
|
|||
|
write_iic(data);
|
|||
|
data[0] = CMD_OUT1;
|
|||
|
write_iic(data);
|
|||
|
}
|
|||
|
void iic(void* args )
|
|||
|
{
|
|||
|
printf("iic thread running...");
|
|||
|
IoTGpioInit(IIC_SDA);
|
|||
|
IoTGpioInit(IIC_SCL);
|
|||
|
IoTGpioSetFunc(IIC_SDA, IOT_GPIO_FUNC_GPIO_0_I2C1_SDA);
|
|||
|
IoTGpioSetFunc(IIC_SCL, IOT_GPIO_FUNC_GPIO_1_I2C1_SCL);
|
|||
|
IoTI2cInit(IIC_IDX, 400000);
|
|||
|
|
|||
|
write_iic(CFG0);
|
|||
|
write_iic(CFG1);
|
|||
|
usleep(200*1000);
|
|||
|
write_iic(OUT0);
|
|||
|
write_iic(OUT1);
|
|||
|
usleep(1000*1000);
|
|||
|
|
|||
|
while(1)
|
|||
|
{
|
|||
|
for(int i=0;i<8;i++)
|
|||
|
{
|
|||
|
unsigned char hex = 0;
|
|||
|
//<2F><>alpha<68><61>i<EFBFBD><69>ת<EFBFBD><D7AA>Ϊhex
|
|||
|
for(int j=0;j<8;j++)
|
|||
|
{
|
|||
|
hex = hex <<1;
|
|||
|
if(alpha[i][j] == '1')
|
|||
|
{
|
|||
|
hex = hex | 0x1;
|
|||
|
}
|
|||
|
}
|
|||
|
OUT1[2] = hex; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
OUT0[1] = ~(1 << i); //ѡ<><D1A1><EFBFBD><EFBFBD>i<EFBFBD><69>
|
|||
|
write_data(~(1 << i),hex);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
printf("set finish!\n");
|
|||
|
}
|
|||
|
|
|||
|
void iic_entry()
|
|||
|
{
|
|||
|
printf("iic_entry called \n");
|
|||
|
osThreadAttr_t attr;
|
|||
|
attr.name = "thread_iic";
|
|||
|
attr.attr_bits = 0U; // <20><><EFBFBD><EFBFBD>Ϊ1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>osThreadJoin<69><6E><EFBFBD><EFBFBD>
|
|||
|
attr.cb_mem = NULL; //<2F><><EFBFBD>ƿ<EFBFBD><C6BF><EFBFBD>ָ<EFBFBD><D6B8>
|
|||
|
attr.cb_size = 0U;
|
|||
|
attr.stack_mem = NULL; //ջ<>ڴ<EFBFBD>ָ<EFBFBD><D6B8>
|
|||
|
attr.stack_size = 1024 * 4; //ջ<><D5BB>С
|
|||
|
attr.priority = 25; //<2F><><EFBFBD>ȼ<EFBFBD>
|
|||
|
if (osThreadNew((osThreadFunc_t)iic, NULL, &attr) == NULL)
|
|||
|
{
|
|||
|
printf("Fail to create thread!\n");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
APP_FEATURE_INIT(iic_entry);
|