1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/pkg/devices/lib/i2c/include/i2c-x1600.h Tue Sep 26 21:27:50 2023 +0200
1.3 @@ -0,0 +1,161 @@
1.4 +/*
1.5 + * I2C support for the X1600.
1.6 + *
1.7 + * Copyright (C) 2017, 2018, 2019, 2023 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#pragma once
1.26 +
1.27 +#include <l4/sys/types.h>
1.28 +#include <stdint.h>
1.29 +
1.30 +
1.31 +
1.32 +#ifdef __cplusplus
1.33 +
1.34 +#include <l4/devices/cpm-x1600.h>
1.35 +#include <l4/devices/hw_mmio_register_block.h>
1.36 +
1.37 +// I2C channel.
1.38 +
1.39 +class I2c_x1600_channel
1.40 +{
1.41 +private:
1.42 + Hw::Register_block<32> _regs;
1.43 + Cpm_x1600_chip *_cpm;
1.44 + uint32_t _frequency;
1.45 +
1.46 + // Buffer management.
1.47 +
1.48 + unsigned int _pos, _reqpos, _total;
1.49 + uint8_t *_buf;
1.50 +
1.51 + // Status conditions.
1.52 +
1.53 + int _fail;
1.54 + int _stop;
1.55 +
1.56 +public:
1.57 + I2c_x1600_channel(l4_addr_t start, Cpm_x1600_chip *cpm,
1.58 + uint32_t frequency);
1.59 +
1.60 + uint32_t get_frequency();
1.61 + void set_target(uint8_t addr);
1.62 +
1.63 + // Reading initiation and execution.
1.64 +
1.65 + void start_read(uint8_t buf[], unsigned int total, int stop = 0);
1.66 + void read();
1.67 +
1.68 + // Writing initiation and execution.
1.69 +
1.70 + void start_write(uint8_t buf[], unsigned int total, int stop = 0);
1.71 + void write();
1.72 +
1.73 + // Transaction control.
1.74 +
1.75 + void stop();
1.76 + void disable();
1.77 +
1.78 + // Specific status conditions.
1.79 +
1.80 + unsigned int have_read();
1.81 + unsigned int have_written();
1.82 + int read_done();
1.83 + int write_done();
1.84 +
1.85 + int failed();
1.86 + int read_failed();
1.87 + int write_failed();
1.88 +
1.89 +private:
1.90 + void enable();
1.91 +
1.92 + int active();
1.93 + int have_input();
1.94 + int have_output();
1.95 + int can_send();
1.96 +
1.97 + void reset_flags();
1.98 + void init_parameters();
1.99 + void set_frequency();
1.100 +
1.101 + void set_read_threshold();
1.102 + void queue_reads();
1.103 + void queue_writes();
1.104 + void store_reads();
1.105 +};
1.106 +
1.107 +// I2C device control.
1.108 +
1.109 +class I2c_x1600_chip
1.110 +{
1.111 +private:
1.112 + l4_addr_t _start, _end;
1.113 + Cpm_x1600_chip *_cpm;
1.114 + uint32_t _frequency;
1.115 +
1.116 +public:
1.117 + I2c_x1600_chip(l4_addr_t start, l4_addr_t end, Cpm_x1600_chip *cpm,
1.118 + uint32_t frequency);
1.119 +
1.120 + I2c_x1600_channel *get_channel(uint8_t channel);
1.121 +};
1.122 +
1.123 +#endif /* __cplusplus */
1.124 +
1.125 +
1.126 +
1.127 +/* C language interface. */
1.128 +
1.129 +EXTERN_C_BEGIN
1.130 +
1.131 +void *x1600_i2c_init(l4_addr_t start, l4_addr_t end, void *cpm,
1.132 + uint32_t frequency);
1.133 +
1.134 +void x1600_i2c_disable(void *i2c_channel);
1.135 +
1.136 +void *x1600_i2c_get_channel(void *i2c, uint8_t channel);
1.137 +
1.138 +uint32_t x1600_i2c_get_frequency(void *i2c_channel);
1.139 +
1.140 +void x1600_i2c_set_target(void *i2c_channel, uint8_t addr);
1.141 +
1.142 +void x1600_i2c_start_read(void *i2c_channel, uint8_t buf[], unsigned int total,
1.143 + int stop);
1.144 +
1.145 +void x1600_i2c_read(void *i2c_channel);
1.146 +
1.147 +void x1600_i2c_start_write(void *i2c_channel, uint8_t buf[], unsigned int total,
1.148 + int stop);
1.149 +
1.150 +void x1600_i2c_write(void *i2c_channel);
1.151 +
1.152 +int x1600_i2c_read_done(void *i2c_channel);
1.153 +
1.154 +int x1600_i2c_write_done(void *i2c_channel);
1.155 +
1.156 +unsigned int x1600_i2c_have_read(void *i2c_channel);
1.157 +
1.158 +unsigned int x1600_i2c_have_written(void *i2c_channel);
1.159 +
1.160 +int x1600_i2c_failed(void *i2c_channel);
1.161 +
1.162 +void x1600_i2c_stop(void *i2c_channel);
1.163 +
1.164 +EXTERN_C_END