image/svg+xml
Brteve's API for EveApps
Version 0.0.1
The reference document for common folder of EveApps project.
 
Loading...
Searching...
No Matches
Hal_I2C.cpp File Reference

This file contains apis related to i2c. More...

#include "Platform.h"
#include <Wire.h>
#include "Hal_I2C.h"

Go to the source code of this file.

Functions

int16_t hal_rtc_i2c_init ()
 
int16_t hal_rtc_i2c_read (uint8_t addr, uint8_t *buffer, uint16_t length)
 
int16_t hal_rtc_i2c_write (uint8_t addr, uint8_t *buffer, uint16_t length)
 

Detailed Description

This file contains apis related to i2c.

Author
Bridgetek
Date
2018

MIT License

Copyright (c) [2019] [Bridgetek Pte Ltd (BRTChip)]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Definition in file Hal_I2C.cpp.

Function Documentation

◆ hal_rtc_i2c_init()

int16_t hal_rtc_i2c_init ( )

Definition at line 39 of file Hal_I2C.cpp.

40{
41 Wire.begin();
42 delay(100);
43 return 0;
44}
#define delay(x)
Definition Gpu_Hal.h:468

◆ hal_rtc_i2c_read()

int16_t hal_rtc_i2c_read ( uint8_t  addr,
uint8_t buffer,
uint16_t  length 
)

Definition at line 47 of file Hal_I2C.cpp.

48{
49 uint16_t i;
50 short count = 0;
51 uint8_t writeResult = 0;
52 while (length > 28)
53 {
55 buffer += 28;
56 addr += 28;
57 length -= 28;
58 }
59
60 Wire.beginTransmission(0x6f); // transmit to device (0x23)
61
62 /* address bytes for rtc are from 00 to 0xff */
63 Wire.write(addr); // sends value byte
64
65 /* end the transmission but do not release the bus - usage is random data read use case from rtc */
66 writeResult = Wire.endTransmission(false);//hold the bus to read the next data
67
68 if (0 != writeResult)
69 {
70 return -1;//error case
71 }
72
73 Wire.requestFrom(0x6f, length);// request length bytes from slave device and end the transmission after this
74 for(i=0;i<length;i++)
75 {
76 /* need to consider timout here */
77 while(0 == Wire.available());//blocking call - at least one byte must be available
78 buffer[i] = Wire.read();
79 }
80
81 return 0;
82}
unsigned short uint16_t
unsigned char uint8_t
static ft_uint32_t ft_uint8_t * buffer
Definition FT_Gpu_Hal.h:139
static ft_uint32_t ft_uint8_t ft_uint32_t length
Definition FT_Gpu_Hal.h:140
static ft_uint32_t addr
Definition FT_Gpu_Hal.h:139
int16_t hal_rtc_i2c_read(uint8_t addr, uint8_t *buffer, uint16_t length)
Definition Hal_I2C.cpp:47

◆ hal_rtc_i2c_write()

int16_t hal_rtc_i2c_write ( uint8_t  addr,
uint8_t buffer,
uint16_t  length 
)

Definition at line 85 of file Hal_I2C.cpp.

86{
87 uint16_t i;
88 byte writeResult = 0;
89 if(0 == length)
90 {
91 return -1;
92 }
93
94 /* for read the lower bit must be set to 1 and for write set to 0 */
95 Wire.beginTransmission(0x6f);
96
97 /* address bytes for rtc are from 00 to 0xff */
98 Wire.write(addr); // sends value byte
99
100 /* check for each byte */
101 for(i=0;i<length;i++)
102 {
103 Wire.write(*buffer++);//send the data to slave
104 }
105
106 /* end the transmission by stop bit */
107
108 writeResult = Wire.endTransmission();//end the transmission by setting the stop bit
109
110 if(0 != writeResult)
111 {
112 return writeResult;
113 }
114 return 0;
115}