123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- #include "drv_iic_04.h"
- #include "nrf_delay.h"
- #define delayTime 1
- static void IIC_04_Start(void)
- {
- IIC_04_SDA_SET;
- IIC_04_SCL_SET;
- nrf_delay_us(5);
- IIC_04_SDA_CLR;
- nrf_delay_us(5);
- IIC_04_SCL_CLR;
- }
- static void IIC_04_Stop(void)
- {
- IIC_04_SCL_CLR;
- IIC_04_SDA_CLR;
- nrf_delay_us(5);
- IIC_04_SCL_SET;
- IIC_04_SDA_SET;
- nrf_delay_us(5);
- }
- static void IIC_04_ACK(void)
- {
- IIC_04_SCL_CLR;
- nrf_delay_us(1);
- IIC_04_SDA_CLR;
- nrf_delay_us(1);
- IIC_04_SCL_SET;
- nrf_delay_us(1);
- IIC_04_SCL_CLR;
- }
- static void IIC_04_NoACK(void)
- {
- IIC_04_SCL_CLR;
- IIC_04_SDA_SET;
- nrf_delay_us(1);
- IIC_04_SCL_SET;
- nrf_delay_us(1);
- IIC_04_SCL_CLR;
- }
- static bool IIC_04_WaitACK(void)
- {
- uint8_t tim = 0;
- // IIC_04_SCL_CLR;
- IIC_04_SDA_SET;
- nrf_delay_us(1);
- IIC_04_SCL_SET;
- // nrf_delay_us(1);
- while(IIC_04_SDA_READ){
- if(++tim>=50){
- IIC_04_Stop();
- return false;
- }
- nrf_delay_us(1);
- }
- IIC_04_SCL_CLR;
- return true;
- }
- static void IIC_04_SendByte(uint8_t _byte)
- {
- uint8_t i = 0;
- for (i=0; i<8; i++){
- IIC_04_SCL_CLR;
- // nrf_delay_us(1);
- if (_byte&0x80) IIC_04_SDA_SET;
- else IIC_04_SDA_CLR;
- _byte <<= 1;
- IIC_04_SCL_SET;
- // nrf_delay_us(1);
- }
- IIC_04_SCL_CLR;
- }
- static uint8_t IIC_04_RecByte(void)
- {
- uint8_t i = 0;
- uint8_t rec_byte;
-
- IIC_04_SDA_SET;
- for (i=0; i<8; i++){
- rec_byte <<= 1;
- IIC_04_SCL_CLR;
- IIC_04_SCL_CLR;
- IIC_04_SCL_CLR;
- IIC_04_SCL_CLR;
- // nrf_delay_us(1);
- IIC_04_SCL_SET;
- // nrf_delay_us(1);
- if (IIC_04_SDA_READ) rec_byte |= 0x01;
- }
- IIC_04_SCL_CLR;
- return rec_byte;
- }
- /********************************************/
- bool IIC_04_WriteBytes(uint8_t add,uint8_t reg,uint8_t* p,uint8_t len)
- {
- uint8_t i = 0;
- IIC_04_Start();
- IIC_04_SendByte(add);
- if(!IIC_04_WaitACK()) return false;
- IIC_04_SendByte(reg);
- if(!IIC_04_WaitACK()) return false;
- for(i=0;i<len;i++){
- IIC_04_SendByte(p[i]);
- if(!IIC_04_WaitACK()) return false;
- }
- IIC_04_Stop();
- return true;
- }
- bool IIC_04_ReadBytes(uint8_t add,uint8_t reg,uint8_t* p,uint8_t len)
- {
- uint8_t i = 0;
- IIC_04_Start();
- IIC_04_SendByte(add);
- if(!IIC_04_WaitACK()) return false;
- IIC_04_SendByte(reg);
- if(!IIC_04_WaitACK()) return false;
- IIC_04_Start();
- IIC_04_SendByte(add+1);
- if(!IIC_04_WaitACK()) return false;
- for(i=0;i<len-1;i++){
- p[i] = IIC_04_RecByte(); IIC_04_ACK();
- }
- p[i] = IIC_04_RecByte(); IIC_04_NoACK();
- IIC_04_Stop();
- return true;
- }
- void IIC_04_Init(void)
- {
- #if SHAREPIN_CONFIG == 1
- nrf_gpio_cfg(
- PIN_SCL_1,
- NRF_GPIO_PIN_DIR_OUTPUT,
- NRF_GPIO_PIN_INPUT_DISCONNECT,
- NRF_GPIO_PIN_NOPULL,
- NRF_GPIO_PIN_H0D1,
- NRF_GPIO_PIN_NOSENSE);
-
- nrf_gpio_cfg_watcher(PIN_SCL_1);
- nrf_gpio_pin_set(PIN_SCL_1);
-
- nrf_gpio_cfg(
- PIN_SDA_1,
- NRF_GPIO_PIN_DIR_OUTPUT,
- NRF_GPIO_PIN_INPUT_DISCONNECT,
- NRF_GPIO_PIN_NOPULL,
- NRF_GPIO_PIN_H0D1,
- NRF_GPIO_PIN_NOSENSE);
-
- nrf_gpio_cfg_watcher(PIN_SDA_1);
- nrf_gpio_pin_set(PIN_SDA_1);
- #elif SHAREPIN_CONFIG == 2
- nrf_gpio_cfg_output(PIN_QMC6310_EN_01);
- nrf_gpio_pin_write(PIN_QMC6310_EN_01,1);
-
- nrf_gpio_cfg_output(PIN_QMC6310_SCL_01);
- nrf_gpio_cfg_watcher(PIN_QMC6310_SCL_01);
- nrf_gpio_pin_set(PIN_QMC6310_SCL_01);
- nrf_gpio_cfg_output(PIN_QMC6310_SDA_01);
- nrf_gpio_cfg_watcher(PIN_QMC6310_SDA_01);
- nrf_gpio_pin_set(PIN_QMC6310_SDA_01);
-
- #elif SHAREPIN_CONFIG == 3
- nrf_gpio_cfg(
- PIN_SDA_BACK,
- NRF_GPIO_PIN_DIR_OUTPUT,
- NRF_GPIO_PIN_INPUT_DISCONNECT,
- NRF_GPIO_PIN_NOPULL,
- NRF_GPIO_PIN_S0S1,
- NRF_GPIO_PIN_NOSENSE);
-
- nrf_gpio_cfg_watcher(PIN_SDA_BACK);
- nrf_gpio_pin_set(PIN_SDA_BACK);
-
- nrf_gpio_cfg(
- PIN_SCL_MAG,
- NRF_GPIO_PIN_DIR_OUTPUT,
- NRF_GPIO_PIN_INPUT_DISCONNECT,
- NRF_GPIO_PIN_NOPULL,
- NRF_GPIO_PIN_S0S1,
- NRF_GPIO_PIN_NOSENSE);
-
- nrf_gpio_cfg_watcher(PIN_SCL_MAG);
- nrf_gpio_pin_set(PIN_SCL_MAG);
- #endif
- }
|