libpynq (release 5EWC0-2023 version 0.2.1 of 2023-09-01 11:02)
Loading...
Searching...
No Matches
iic.c
Go to the documentation of this file.
1/*
2Copyright (c) 2023 Eindhoven University of Technology
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21*/
22#include "iic.h"
24#include "log.h"
25#include "xiic_l.h"
26#include <platform.h>
27#include <stdio.h>
28#include <string.h>
29
30static arm_shared iic_handles[NUM_IICS];
31static volatile uint32_t *iic_ptrs[NUM_IICS] = {
32 NULL,
33};
34
35#define IIC_REG_SOFT_RESET (0x40 / 4)
36
37void iic_init(const iic_index_t iic) {
38 if (!(iic >= IIC0 && iic < NUM_IICS)) {
39 pynq_error("invalid IIC %d, must be 0..%d\n", iic, NUM_IICS);
40 }
41 if (iic == IIC0) {
42 iic_ptrs[iic] = arm_shared_init(&(iic_handles[iic]), axi_iic_0, 4096);
43 } else if (iic == IIC1) {
44 iic_ptrs[iic] = arm_shared_init(&(iic_handles[iic]), axi_iic_1, 4096);
45 }
46 // Reset
47 iic_ptrs[iic][IIC_REG_SOFT_RESET] = 0xA;
48}
49
50void iic_destroy(const iic_index_t iic) {
51 if (!(iic >= IIC0 && iic < NUM_IICS)) {
52 pynq_error("invalid IIC %d, must be 0..%d-1\n", iic, NUM_IICS);
53 }
54 arm_shared_close(&(iic_handles[iic]));
55 iic_ptrs[iic] = NULL;
56}
57
58bool iic_read_register(const iic_index_t iic, const uint8_t addr,
59 const uint8_t reg, uint8_t *data, uint16_t data_length) {
60 if (!(iic >= IIC0 && iic < NUM_IICS)) {
61 pynq_error("invalid IIC %d, must be 0..%d-1\n", iic, NUM_IICS);
62 }
63 XIic_Send((UINTPTR)iic_ptrs[iic], addr, (u8 *)&reg, 1, XIIC_REPEATED_START);
64 uint8_t ByteCount =
65 XIic_Recv((UINTPTR)iic_ptrs[iic], addr, data, data_length, XIIC_STOP);
66 return (ByteCount == data_length) ? 0 : 1;
67}
68
69bool iic_write_register(const iic_index_t iic, const uint8_t addr,
70 const uint8_t reg, uint8_t *data,
71 uint16_t data_length) {
72 if (!(iic >= IIC0 && iic < NUM_IICS)) {
73 pynq_error("invalid IIC %d, must be 0..%d-1\n", iic, NUM_IICS);
74 }
75 uint8_t buffer[1 + data_length];
76 buffer[0] = reg;
77 memcpy(&(buffer[1]), data, data_length);
78 uint8_t ByteCount = XIic_Send((UINTPTR)iic_ptrs[iic], addr, &(buffer[0]),
79 1 + data_length, XIIC_STOP);
80 return (ByteCount == (data_length + 1)) ? 0 : 1;
81}
unsigned XIic_Recv(UINTPTR BaseAddress, u8 Address, u8 *BufferPtr, unsigned ByteCount, u8 Option)
Definition xiic_l.c:2
unsigned XIic_Send(UINTPTR BaseAddress, u8 Address, u8 *BufferPtr, unsigned ByteCount, u8 Option)
Definition xiic_l.c:4
void arm_shared_close(arm_shared *handle)
void * arm_shared_init(arm_shared *handle, const uint32_t address, const uint32_t length)
iic_index_t
Enum of IICs. Functions use a switch numbered from 0..NUM_IICS-1.
Definition iic.h:42
void iic_destroy(const iic_index_t iic)
Close the shared memory handle for the specified IIC index.
Definition iic.c:3
bool iic_write_register(const iic_index_t iic, const uint8_t addr, const uint8_t reg, uint8_t *data, uint16_t length)
Definition iic.c:7
bool iic_read_register(const iic_index_t iic, const uint8_t addr, const uint8_t reg, uint8_t *data, uint16_t length)
Definition iic.c:4
void iic_init(const iic_index_t iic)
Initialize the IIC specified by the index with a shared memory handle and a buffer size of 4096 bytes...
Definition iic.c:2
@ IIC1
Definition iic.h:42
@ IIC0
Definition iic.h:42
@ NUM_IICS
Definition iic.h:42
#define pynq_error(...)
Definition log.h:118
#define IIC_REG_SOFT_RESET
Definition iic.c:35
#define XIIC_STOP
Definition xiic_l.h:216
#define XIIC_REPEATED_START
Definition xiic_l.h:219