Compare commits
2 Commits
e5303e4090
...
main
Author | SHA1 | Date | |
---|---|---|---|
da2fd0d2a8 | |||
8bc9f02ad8 |
Binary file not shown.
Binary file not shown.
106
README.md
106
README.md
@@ -1,7 +1,36 @@
|
|||||||
# zh_avr_i2c
|
# FreeRTOS based AVR library for I2C bus
|
||||||
|
|
||||||
AVR library for I2C bus.
|
## Features
|
||||||
|
|
||||||
|
1. Simple read and write data.
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
|
||||||
|
1. [zh_avr_free_rtos](http://git.zh.com.ru/avr_libraries/zh_avr_free_rtos)
|
||||||
|
2. [zh_avr_common](http://git.zh.com.ru/avr_libraries/zh_avr_common)
|
||||||
|
|
||||||
|
## Using
|
||||||
|
|
||||||
|
In an existing project, run the following command to install the component:
|
||||||
|
|
||||||
|
```text
|
||||||
|
cd ../your_project/lib
|
||||||
|
git clone http://git.zh.com.ru/avr_libraries/zh_avr_free_rtos
|
||||||
|
git clone http://git.zh.com.ru/avr_libraries/zh_avr_i2c
|
||||||
|
git clone http://git.zh.com.ru/avr_libraries/zh_avr_common
|
||||||
|
```
|
||||||
|
|
||||||
|
In the application, add the component:
|
||||||
|
|
||||||
|
```c
|
||||||
|
#include "zh_avr_i2c.h"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
Master read and write data:
|
||||||
|
|
||||||
|
```c
|
||||||
#include "avr/io.h"
|
#include "avr/io.h"
|
||||||
#include "stdio.h"
|
#include "stdio.h"
|
||||||
#include "zh_avr_i2c.h"
|
#include "zh_avr_i2c.h"
|
||||||
@@ -11,41 +40,58 @@ AVR library for I2C bus.
|
|||||||
|
|
||||||
int usart(char byte, FILE *stream)
|
int usart(char byte, FILE *stream)
|
||||||
{
|
{
|
||||||
while ((UCSR0A & (1 << UDRE0)) == 0)
|
while ((UCSR0A & (1 << UDRE0)) == 0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
UDR0 = byte;
|
UDR0 = byte;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
FILE uart = FDEV_SETUP_STREAM(usart, NULL, _FDEV_SETUP_WRITE);
|
FILE uart = FDEV_SETUP_STREAM(usart, NULL, _FDEV_SETUP_WRITE);
|
||||||
|
|
||||||
void i2c_example_task(void *pvParameters)
|
void i2c_example_task(void *pvParameters)
|
||||||
{
|
{
|
||||||
zh_avr_i2c_master_init(false);
|
zh_avr_i2c_master_init(false);
|
||||||
avr_err_t err = zh_avr_i2c_master_probe(0x38, 100 / portTICK_PERIOD_MS);
|
for (;;)
|
||||||
switch (err)
|
{
|
||||||
{
|
avr_err_t err = zh_avr_i2c_master_probe(0x38, 100 / portTICK_PERIOD_MS);
|
||||||
case AVR_OK:
|
if (err == AVR_OK)
|
||||||
printf("Slave Answered.\n");
|
{
|
||||||
break;
|
uint8_t data_send = 111;
|
||||||
case AVR_ERR_INVALID_RESPONSE:
|
uint8_t data_read = 0;
|
||||||
printf("Slave Not Answer.\n");
|
printf("Data Send %d.\n", data_send);
|
||||||
break;
|
zh_avr_i2c_master_transmit(0x38, (uint8_t *)&data_send, sizeof(data_send), 100 / portTICK_PERIOD_MS);
|
||||||
default:
|
zh_avr_i2c_master_receive(0x38, (uint8_t *)&data_read, sizeof(data_read), 100 / portTICK_PERIOD_MS);
|
||||||
printf("Bus Error.\n");
|
printf("Data Read %d.\n", data_read);
|
||||||
break;
|
data_send = 55;
|
||||||
}
|
printf("Data Send %d.\n", data_send);
|
||||||
vTaskDelete(NULL);
|
zh_avr_i2c_master_transmit(0x38, (uint8_t *)&data_send, sizeof(data_send), 100 / portTICK_PERIOD_MS);
|
||||||
|
zh_avr_i2c_master_receive(0x38, (uint8_t *)&data_read, sizeof(data_read), 100 / portTICK_PERIOD_MS);
|
||||||
|
printf("Data Read %d.\n", data_read);
|
||||||
|
data_send = 14;
|
||||||
|
printf("Data Send %d.\n", data_send);
|
||||||
|
zh_avr_i2c_master_transmit(0x38, (uint8_t *)&data_send, sizeof(data_send), 100 / portTICK_PERIOD_MS);
|
||||||
|
zh_avr_i2c_master_receive(0x38, (uint8_t *)&data_read, sizeof(data_read), 100 / portTICK_PERIOD_MS);
|
||||||
|
printf("Data Read %d.\n", data_read);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Device Not Found.\n");
|
||||||
|
}
|
||||||
|
printf("Task Remaining Stack Size %d.\n", uxTaskGetStackHighWaterMark(NULL));
|
||||||
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
UBRR0H = (BAUD_PRESCALE >> 8);
|
UBRR0H = (BAUD_PRESCALE >> 8);
|
||||||
UBRR0L = BAUD_PRESCALE;
|
UBRR0L = BAUD_PRESCALE;
|
||||||
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
|
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
|
||||||
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
|
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
|
||||||
stdout = &uart;
|
stdout = &uart;
|
||||||
xTaskCreate(i2c_example_task, "i2c example task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
|
xTaskCreate(i2c_example_task, "i2c example task", 110, NULL, tskIDLE_PRIORITY, NULL);
|
||||||
vTaskStartScheduler();
|
vTaskStartScheduler();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
@@ -5,30 +5,14 @@
|
|||||||
#include "avr/io.h"
|
#include "avr/io.h"
|
||||||
#include "avr/interrupt.h"
|
#include "avr/interrupt.h"
|
||||||
#include "stdbool.h"
|
#include "stdbool.h"
|
||||||
|
#include "avr_err.h"
|
||||||
|
#include "avr_bit_defs.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
AVR_FAIL = -1,
|
|
||||||
AVR_OK,
|
|
||||||
AVR_ERR_NO_MEM,
|
|
||||||
AVR_ERR_INVALID_ARG,
|
|
||||||
AVR_ERR_INVALID_STATE,
|
|
||||||
AVR_ERR_INVALID_SIZE,
|
|
||||||
AVR_ERR_NOT_FOUND,
|
|
||||||
AVR_ERR_NOT_SUPPORTED,
|
|
||||||
AVR_ERR_TIMEOUT,
|
|
||||||
AVR_ERR_INVALID_RESPONSE,
|
|
||||||
AVR_ERR_INVALID_CRC,
|
|
||||||
AVR_ERR_INVALID_VERSION,
|
|
||||||
AVR_ERR_NOT_FINISHED,
|
|
||||||
AVR_ERR_NOT_ALLOWED
|
|
||||||
} avr_err_t;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initialize I2C bus.
|
* @brief Initialize I2C bus.
|
||||||
*
|
*
|
||||||
@@ -39,15 +23,37 @@ extern "C"
|
|||||||
avr_err_t zh_avr_i2c_master_init(const bool pullup);
|
avr_err_t zh_avr_i2c_master_init(const bool pullup);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Probe I2C address, if address is correct and ACK is received, this function will return AVR_OK.
|
* @brief Probe I2C address.
|
||||||
*
|
*
|
||||||
* @param[in] addr Address I2C device address that you want to probe.
|
* @param[in] addr Address I2C device.
|
||||||
* @param[in] xTicksToWait Wait timeout in FreeRTOS ticks.
|
* @param[in] xTicksToWait Wait timeout in FreeRTOS ticks.
|
||||||
*
|
*
|
||||||
* @return AVR_OK if success or an error code otherwise.
|
* @return AVR_OK if success or an error code otherwise.
|
||||||
*/
|
*/
|
||||||
avr_err_t zh_avr_i2c_master_probe(const uint8_t addr, TickType_t xTicksToWait);
|
avr_err_t zh_avr_i2c_master_probe(const uint8_t addr, TickType_t xTicksToWait);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Send data to I2C address.
|
||||||
|
*
|
||||||
|
* @param[in] addr Address I2C device.
|
||||||
|
* @param[in] data Pointer to data for send.
|
||||||
|
* @param[in] size Data size.
|
||||||
|
* @param[in] xTicksToWait Wait timeout in FreeRTOS ticks.
|
||||||
|
*
|
||||||
|
* @return AVR_OK if success or an error code otherwise.
|
||||||
|
*/
|
||||||
avr_err_t zh_avr_i2c_master_transmit(const uint8_t addr, uint8_t *data, uint8_t size, TickType_t xTicksToWait);
|
avr_err_t zh_avr_i2c_master_transmit(const uint8_t addr, uint8_t *data, uint8_t size, TickType_t xTicksToWait);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Read data from I2C address.
|
||||||
|
*
|
||||||
|
* @param[in] addr Address I2C device.
|
||||||
|
* @param[out] data Pointer to buffer for read.
|
||||||
|
* @param[in] size Data size.
|
||||||
|
* @param[in] xTicksToWait Wait timeout in FreeRTOS ticks.
|
||||||
|
*
|
||||||
|
* @return AVR_OK if success or an error code otherwise.
|
||||||
|
*/
|
||||||
avr_err_t zh_avr_i2c_master_receive(const uint8_t addr, uint8_t *data, uint8_t size, TickType_t xTicksToWait);
|
avr_err_t zh_avr_i2c_master_receive(const uint8_t addr, uint8_t *data, uint8_t size, TickType_t xTicksToWait);
|
||||||
|
|
||||||
avr_err_t zh_avr_i2c_master_transmit_register(const uint8_t addr, uint16_t *reg, uint8_t *data, uint8_t size, TickType_t xTicksToWait); // To Do
|
avr_err_t zh_avr_i2c_master_transmit_register(const uint8_t addr, uint16_t *reg, uint8_t *data, uint8_t size, TickType_t xTicksToWait); // To Do
|
||||||
|
@@ -1 +1 @@
|
|||||||
1.0.0
|
1.1.0
|
18
zh_avr_i2c.c
18
zh_avr_i2c.c
@@ -1,15 +1,9 @@
|
|||||||
#include "zh_avr_i2c.h"
|
#include "zh_avr_i2c.h"
|
||||||
|
|
||||||
#define ZH_ERROR_CHECK(cond, err, ...) \
|
#define I2C_OK AVR_BIT0
|
||||||
if (!(cond)) \
|
#define I2C_NACK AVR_BIT1
|
||||||
{ \
|
#define I2C_COLLISION AVR_BIT2
|
||||||
return err; \
|
#define I2C_BUS_FAIL AVR_BIT3
|
||||||
}
|
|
||||||
|
|
||||||
#define I2C_OK 0x01
|
|
||||||
#define I2C_NACK 0x02
|
|
||||||
#define I2C_COLLISION 0x04
|
|
||||||
#define I2C_BUS_FAIL 0x08
|
|
||||||
|
|
||||||
#define I2C_START ((1 << TWINT) | (1 << TWEN) | (1 << TWIE))
|
#define I2C_START ((1 << TWINT) | (1 << TWEN) | (1 << TWIE))
|
||||||
#define I2C_MASTER_READ 1
|
#define I2C_MASTER_READ 1
|
||||||
@@ -80,12 +74,12 @@ avr_err_t zh_avr_i2c_master_receive(const uint8_t addr, uint8_t *data, uint8_t s
|
|||||||
avr_err_t zh_avr_i2c_master_transmit_register(const uint8_t addr, uint16_t *reg, uint8_t *data, uint8_t size, TickType_t xTicksToWait)
|
avr_err_t zh_avr_i2c_master_transmit_register(const uint8_t addr, uint16_t *reg, uint8_t *data, uint8_t size, TickType_t xTicksToWait)
|
||||||
{
|
{
|
||||||
// To Do.
|
// To Do.
|
||||||
return _zh_avr_i2c_master_start(xTicksToWait);
|
return AVR_OK;
|
||||||
}
|
}
|
||||||
avr_err_t zh_avr_i2c_master_receive_register(const uint8_t addr, uint16_t *reg, uint8_t *data, uint8_t size, TickType_t xTicksToWait)
|
avr_err_t zh_avr_i2c_master_receive_register(const uint8_t addr, uint16_t *reg, uint8_t *data, uint8_t size, TickType_t xTicksToWait)
|
||||||
{
|
{
|
||||||
// To Do.
|
// To Do.
|
||||||
return _zh_avr_i2c_master_start(xTicksToWait);
|
return AVR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
avr_err_t _zh_avr_i2c_master_start(TickType_t xTicksToWait)
|
avr_err_t _zh_avr_i2c_master_start(TickType_t xTicksToWait)
|
||||||
|
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user