Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 1 | /* I2C EEPROM memory read and write utilities |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
| 16 | */ |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 17 | #include "board.h" |
| 18 | #include <stdbool.h> |
| 19 | |
| 20 | /* Low-Level I2C Routines */ |
| 21 | |
| 22 | static const Pin pin_sda = {PIO_PA30, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_OPENDRAIN }; |
| 23 | static const Pin pin_sda_in = {PIO_PA30, PIOA, ID_PIOA, PIO_INPUT, PIO_DEFAULT }; |
| 24 | static const Pin pin_scl = {PIO_PA31, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_OPENDRAIN }; |
| 25 | |
Harald Welte | c6ae98c | 2016-09-01 11:57:09 +0200 | [diff] [blame] | 26 | static void i2c_delay() |
| 27 | { |
| 28 | volatile int v; |
| 29 | int i; |
| 30 | |
Harald Welte | 73d697d | 2016-09-01 18:58:41 +0200 | [diff] [blame] | 31 | /* 100 cycles results in SCL peak length of 44us, so it's about |
| 32 | * 440ns per cycle here */ |
| 33 | for (i = 0; i < 14; i++) { |
Harald Welte | c6ae98c | 2016-09-01 11:57:09 +0200 | [diff] [blame] | 34 | v = 0; |
| 35 | } |
| 36 | } |
| 37 | |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 38 | void i2c_pin_init(void) |
| 39 | { |
| 40 | PIO_Configure(&pin_scl, PIO_LISTSIZE(pin_scl)); |
| 41 | PIO_Configure(&pin_sda, PIO_LISTSIZE(pin_sda)); |
| 42 | } |
| 43 | |
| 44 | static void set_scl(void) |
| 45 | { |
| 46 | PIO_Set(&pin_scl); |
Harald Welte | c6ae98c | 2016-09-01 11:57:09 +0200 | [diff] [blame] | 47 | i2c_delay(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static void set_sda(void) |
| 51 | { |
| 52 | PIO_Set(&pin_sda); |
Harald Welte | c6ae98c | 2016-09-01 11:57:09 +0200 | [diff] [blame] | 53 | i2c_delay(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void clear_scl(void) |
| 57 | { |
| 58 | PIO_Clear(&pin_scl); |
Harald Welte | c6ae98c | 2016-09-01 11:57:09 +0200 | [diff] [blame] | 59 | i2c_delay(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static void clear_sda(void) |
| 63 | { |
| 64 | PIO_Clear(&pin_sda); |
Harald Welte | c6ae98c | 2016-09-01 11:57:09 +0200 | [diff] [blame] | 65 | i2c_delay(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static bool read_sda(void) |
| 69 | { |
| 70 | bool ret; |
| 71 | |
| 72 | PIO_Configure(&pin_sda_in, PIO_LISTSIZE(pin_sda_in)); |
Harald Welte | 0aea9ff | 2016-09-01 18:39:12 +0200 | [diff] [blame] | 73 | if (PIO_Get(&pin_sda_in)) |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 74 | ret = true; |
| 75 | else |
| 76 | ret = false; |
| 77 | PIO_Configure(&pin_sda, PIO_LISTSIZE(pin_sda)); |
| 78 | |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | /* Core I2C Routines */ |
| 83 | |
| 84 | static bool i2c_started = false; |
| 85 | |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 86 | static void i2c_start_cond(void) |
| 87 | { |
| 88 | if (i2c_started) { |
| 89 | set_sda(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 90 | set_scl(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 93 | clear_sda(); |
| 94 | i2c_delay(); |
| 95 | clear_scl(); |
| 96 | i2c_started = true; |
| 97 | } |
| 98 | |
| 99 | static void i2c_stop_cond(void) |
| 100 | { |
| 101 | clear_sda(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 102 | set_scl(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 103 | set_sda(); |
| 104 | i2c_delay(); |
| 105 | i2c_started = false; |
| 106 | } |
| 107 | |
| 108 | static void i2c_write_bit(bool bit) |
| 109 | { |
| 110 | if (bit) |
| 111 | set_sda(); |
| 112 | else |
| 113 | clear_sda(); |
Harald Welte | c6ae98c | 2016-09-01 11:57:09 +0200 | [diff] [blame] | 114 | i2c_delay(); // ? |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 115 | set_scl(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 116 | clear_scl(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static bool i2c_read_bit(void) |
| 120 | { |
| 121 | bool bit; |
| 122 | |
| 123 | set_sda(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 124 | set_scl(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 125 | bit = read_sda(); |
| 126 | clear_scl(); |
| 127 | |
| 128 | return bit; |
| 129 | } |
| 130 | |
| 131 | bool i2c_write_byte(bool send_start, bool send_stop, uint8_t byte) |
| 132 | { |
| 133 | uint8_t bit; |
| 134 | bool nack; |
| 135 | |
| 136 | if (send_start) |
| 137 | i2c_start_cond(); |
| 138 | |
| 139 | for (bit = 0; bit < 8; bit++) { |
| 140 | i2c_write_bit((byte & 0x80) != 0); |
| 141 | byte <<= 1; |
| 142 | } |
| 143 | |
| 144 | nack = i2c_read_bit(); |
| 145 | |
| 146 | if (send_stop) |
| 147 | i2c_stop_cond(); |
| 148 | |
| 149 | return nack; |
| 150 | } |
| 151 | |
| 152 | uint8_t i2c_read_byte(bool nack, bool send_stop) |
| 153 | { |
| 154 | uint8_t byte = 0; |
| 155 | uint8_t bit; |
| 156 | |
| 157 | for (bit = 0; bit < 8; bit++) { |
| 158 | byte = (byte << 1) | i2c_read_bit(); |
| 159 | } |
| 160 | |
| 161 | i2c_write_bit(nack); |
| 162 | |
| 163 | if (send_stop) |
| 164 | i2c_stop_cond(); |
| 165 | |
| 166 | return byte; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | /* EEPROM related code */ |
| 171 | |
| 172 | int eeprom_write_byte(uint8_t slave, uint8_t addr, uint8_t byte) |
| 173 | { |
| 174 | bool nack; |
| 175 | |
Harald Welte | c6e482d | 2017-03-05 16:24:29 +0100 | [diff] [blame] | 176 | WDT_Restart(WDT); |
| 177 | |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 178 | /* Write slave address */ |
| 179 | nack = i2c_write_byte(true, false, slave << 1); |
| 180 | if (nack) |
| 181 | goto out_stop; |
| 182 | nack = i2c_write_byte(false, false, addr); |
| 183 | if (nack) |
| 184 | goto out_stop; |
| 185 | nack = i2c_write_byte(false, true, byte); |
| 186 | if (nack) |
| 187 | goto out_stop; |
| 188 | |
| 189 | out_stop: |
| 190 | i2c_stop_cond(); |
| 191 | if (nack) |
| 192 | return -1; |
| 193 | else |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | int eeprom_read_byte(uint8_t slave, uint8_t addr) |
| 198 | { |
| 199 | bool nack; |
| 200 | |
Harald Welte | c6e482d | 2017-03-05 16:24:29 +0100 | [diff] [blame] | 201 | WDT_Restart(WDT); |
| 202 | |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 203 | /* dummy write cycle */ |
| 204 | nack = i2c_write_byte(true, false, slave << 1); |
| 205 | if (nack) |
| 206 | goto out_stop; |
| 207 | nack = i2c_write_byte(false, false, addr); |
| 208 | if (nack) |
| 209 | goto out_stop; |
| 210 | /* Re-start with read */ |
| 211 | nack = i2c_write_byte(true, false, (slave << 1) | 1); |
| 212 | if (nack) |
| 213 | goto out_stop; |
| 214 | |
| 215 | return i2c_read_byte(true, true); |
| 216 | |
| 217 | out_stop: |
| 218 | i2c_stop_cond(); |
| 219 | if (nack) |
| 220 | return -1; |
| 221 | else |
| 222 | return 0; |
| 223 | } |