Skip to content

Commit

Permalink
Merge pull request #1 from hacperme/feat_unisoc_8850_support
Browse files Browse the repository at this point in the history
feat: add unisoc 8850 support
  • Loading branch information
hacperme authored Apr 19, 2023
2 parents f82beb5 + 3582f20 commit 4ff329f
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 2 deletions.
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ That is Edited version of [Original version](https://schkorea.tistory.com/437)
原作者:https://schkorea.tistory.com/437

精简了部分代码,增加8/16bit寄存器地址的突发(连续)读写函数

移植到展锐8850平台
109 changes: 107 additions & 2 deletions sw_i2c.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include "sw_i2c.h"
#if CNIFIC_UNISOC_8850_SUPPORT
#include "ql_api_osi.h"
#include "ql_gpio.h"
#endif

#ifndef TRUE
#define TRUE 1
Expand All @@ -7,6 +11,89 @@
#define FALSE 0
#endif

#if CNIFIC_UNISOC_8850_SUPPORT

// sw_i2c 初始化
void SW_I2C_initial(void)
{
ql_gpio_init(AIP_SPI_DIO, GPIO_OUTPUT, PULL_NONE, LVL_HIGH); // DIO
ql_gpio_init(AIP_SPI_CLK, GPIO_OUTPUT, PULL_NONE, LVL_HIGH); // CLK
ql_delay_us(SW_I2C_WAIT_TIME);
}

// 引脚置位
void GPIO_SetBits(uint16_t GPIO_Pin)
{
ql_gpio_set_level(GPIO_Pin, LVL_HIGH);
}

// 引脚复位
void GPIO_ResetBits(uint16_t GPIO_Pin)
{
ql_gpio_set_level(GPIO_Pin, LVL_LOW);
}

// 读引脚状态
uint8_t GPIO_ReadInputDataBit(uint16_t GPIO_Pin)
{
ql_LvlMode l = 0;
ql_gpio_get_level(GPIO_Pin, &l);
return l;
}

// SDA引脚切换输入模式
void sda_in_mode(uint8_t sel)
{
ql_gpio_set_direction(SW_I2C1_SDA_PIN, GPIO_INPUT);
ql_gpio_set_pull(SW_I2C1_SDA_PIN, PULL_NONE);
}

// SDA引脚切换输出模式
void sda_out_mode(uint8_t sel)
{
ql_gpio_set_direction(SW_I2C1_SDA_PIN, GPIO_OUTPUT);
}

// SCL引脚切换输入模式
void scl_in_mode(uint8_t sel)
{
ql_gpio_set_direction(SW_I2C1_SCL_PIN, GPIO_INPUT);
ql_gpio_set_pull(SW_I2C1_SCL_PIN, PULL_NONE);
}

// SCL引脚切换输出模式
void scl_out_mode(uint8_t sel)
{
ql_gpio_set_direction(SW_I2C1_SCL_PIN, GPIO_OUTPUT);
}

void TIMER__Wait_us(uint32_t nCount)
{
ql_delay_us(nCount);
}

void sda_high(uint8_t sel)
{
GPIO_SetBits(SW_I2C1_SDA_PIN);
}

void sda_low(uint8_t sel)
{
GPIO_ResetBits(SW_I2C1_SDA_PIN);
}

void scl_high(uint8_t sel)
{
GPIO_SetBits(SW_I2C1_SCL_PIN);
}

void scl_low(uint8_t sel)
{
GPIO_ResetBits(SW_I2C1_SCL_PIN);
}


#else
//sw_i2c 初始化
void SW_I2C_initial(void)
{
Expand All @@ -15,10 +102,10 @@ void SW_I2C_initial(void)
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
//i2c_sw SCL
// i2c_sw SCL
GPIO_InitStruct.Pin = SW_I2C1_SCL_PIN;
HAL_GPIO_Init(SW_I2C1_SCL_PORT, &GPIO_InitStruct);
//i2c_sw SDA
// i2c_sw SDA
GPIO_InitStruct.Pin = SW_I2C1_SDA_PIN;
HAL_GPIO_Init(SW_I2C1_SDA_PORT, &GPIO_InitStruct);
}
Expand Down Expand Up @@ -131,6 +218,8 @@ void scl_low(uint8_t sel)
GPIO_ResetBits(SW_I2C1_SCL_PORT, SW_I2C1_SCL_PIN);
}

#endif

void sda_out(uint8_t sel, uint8_t out)
{
if(out)
Expand All @@ -152,6 +241,20 @@ void i2c_port_initial(uint8_t sel)
scl_high(sel);
}

#if CNIFIC_UNISOC_8850_SUPPORT
uint8_t SW_I2C_ReadVal_SDA(uint8_t sel)
{

return GPIO_ReadInputDataBit(SW_I2C1_SDA_PIN);
}

uint8_t SW_I2C_ReadVal_SCL(uint8_t sel)
{

return GPIO_ReadInputDataBit(SW_I2C1_SCL_PIN);
}
#else

uint8_t SW_I2C_ReadVal_SDA(uint8_t sel)
{
if(sel == 1)
Expand All @@ -166,6 +269,8 @@ uint8_t SW_I2C_ReadVal_SCL(uint8_t sel)
return 0;
}

#endif

void i2c_start_condition(uint8_t sel)
{
sda_high(sel);
Expand Down
24 changes: 24 additions & 0 deletions sw_i2c.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
#ifndef _SW_I2C_H_
#define _SW_I2C_H_

#define CNIFIC_UNISOC_8850_SUPPORT 1

#if !CNIFIC_UNISOC_8850_SUPPORT
/* includes */
#include "stm32f1xx_hal.h"

#endif

#define SW_I2C1 1

#if !CNIFIC_UNISOC_8850_SUPPORT

#define SW_I2C1_SCL_PORT GPIOB
#define SW_I2C1_SDA_PORT GPIOB
#define SW_I2C1_SCL_PIN GPIO_PIN_6
#define SW_I2C1_SDA_PIN GPIO_PIN_7

#else
#include <stdint.h>

#define AIP_SPI_DIO GPIO_35
#define AIP_SPI_CLK GPIO_37

#define SW_I2C1_SCL_PIN AIP_SPI_CLK
#define SW_I2C1_SDA_PIN AIP_SPI_DIO
#endif

#if !CNIFIC_UNISOC_8850_SUPPORT

#define SW_I2C_WAIT_TIME 25
#else
#define SW_I2C_WAIT_TIME 10 // 10us 100kHz

#endif

#define I2C_READ 0x01
#define READ_CMD 1
#define WRITE_CMD 0
Expand Down

0 comments on commit 4ff329f

Please sign in to comment.