Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 1 | #include "board.h" |
| 2 | #include <stdbool.h> |
| 3 | |
| 4 | /* Low-Level I2C Routines */ |
| 5 | |
| 6 | static const Pin pin_sda = {PIO_PA30, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_OPENDRAIN }; |
| 7 | static const Pin pin_sda_in = {PIO_PA30, PIOA, ID_PIOA, PIO_INPUT, PIO_DEFAULT }; |
| 8 | static const Pin pin_scl = {PIO_PA31, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_OPENDRAIN }; |
| 9 | |
Harald Welte | c6ae98c | 2016-09-01 11:57:09 +0200 | [diff] [blame] | 10 | static void i2c_delay() |
| 11 | { |
| 12 | volatile int v; |
| 13 | int i; |
| 14 | |
Harald Welte | 73d697d | 2016-09-01 18:58:41 +0200 | [diff] [blame^] | 15 | /* 100 cycles results in SCL peak length of 44us, so it's about |
| 16 | * 440ns per cycle here */ |
| 17 | for (i = 0; i < 14; i++) { |
Harald Welte | c6ae98c | 2016-09-01 11:57:09 +0200 | [diff] [blame] | 18 | v = 0; |
| 19 | } |
| 20 | } |
| 21 | |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 22 | void i2c_pin_init(void) |
| 23 | { |
| 24 | PIO_Configure(&pin_scl, PIO_LISTSIZE(pin_scl)); |
| 25 | PIO_Configure(&pin_sda, PIO_LISTSIZE(pin_sda)); |
| 26 | } |
| 27 | |
| 28 | static void set_scl(void) |
| 29 | { |
| 30 | PIO_Set(&pin_scl); |
Harald Welte | c6ae98c | 2016-09-01 11:57:09 +0200 | [diff] [blame] | 31 | i2c_delay(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static void set_sda(void) |
| 35 | { |
| 36 | PIO_Set(&pin_sda); |
Harald Welte | c6ae98c | 2016-09-01 11:57:09 +0200 | [diff] [blame] | 37 | i2c_delay(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static void clear_scl(void) |
| 41 | { |
| 42 | PIO_Clear(&pin_scl); |
Harald Welte | c6ae98c | 2016-09-01 11:57:09 +0200 | [diff] [blame] | 43 | i2c_delay(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static void clear_sda(void) |
| 47 | { |
| 48 | PIO_Clear(&pin_sda); |
Harald Welte | c6ae98c | 2016-09-01 11:57:09 +0200 | [diff] [blame] | 49 | i2c_delay(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static bool read_sda(void) |
| 53 | { |
| 54 | bool ret; |
| 55 | |
| 56 | PIO_Configure(&pin_sda_in, PIO_LISTSIZE(pin_sda_in)); |
Harald Welte | 0aea9ff | 2016-09-01 18:39:12 +0200 | [diff] [blame] | 57 | if (PIO_Get(&pin_sda_in)) |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 58 | ret = true; |
| 59 | else |
| 60 | ret = false; |
| 61 | PIO_Configure(&pin_sda, PIO_LISTSIZE(pin_sda)); |
| 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | /* Core I2C Routines */ |
| 67 | |
| 68 | static bool i2c_started = false; |
| 69 | |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 70 | static void i2c_start_cond(void) |
| 71 | { |
| 72 | if (i2c_started) { |
| 73 | set_sda(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 74 | set_scl(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 77 | clear_sda(); |
| 78 | i2c_delay(); |
| 79 | clear_scl(); |
| 80 | i2c_started = true; |
| 81 | } |
| 82 | |
| 83 | static void i2c_stop_cond(void) |
| 84 | { |
| 85 | clear_sda(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 86 | set_scl(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 87 | set_sda(); |
| 88 | i2c_delay(); |
| 89 | i2c_started = false; |
| 90 | } |
| 91 | |
| 92 | static void i2c_write_bit(bool bit) |
| 93 | { |
| 94 | if (bit) |
| 95 | set_sda(); |
| 96 | else |
| 97 | clear_sda(); |
Harald Welte | c6ae98c | 2016-09-01 11:57:09 +0200 | [diff] [blame] | 98 | i2c_delay(); // ? |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 99 | set_scl(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 100 | clear_scl(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static bool i2c_read_bit(void) |
| 104 | { |
| 105 | bool bit; |
| 106 | |
| 107 | set_sda(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 108 | set_scl(); |
Harald Welte | 226b40a | 2016-08-21 19:33:24 +0200 | [diff] [blame] | 109 | bit = read_sda(); |
| 110 | clear_scl(); |
| 111 | |
| 112 | return bit; |
| 113 | } |
| 114 | |
| 115 | bool i2c_write_byte(bool send_start, bool send_stop, uint8_t byte) |
| 116 | { |
| 117 | uint8_t bit; |
| 118 | bool nack; |
| 119 | |
| 120 | if (send_start) |
| 121 | i2c_start_cond(); |
| 122 | |
| 123 | for (bit = 0; bit < 8; bit++) { |
| 124 | i2c_write_bit((byte & 0x80) != 0); |
| 125 | byte <<= 1; |
| 126 | } |
| 127 | |
| 128 | nack = i2c_read_bit(); |
| 129 | |
| 130 | if (send_stop) |
| 131 | i2c_stop_cond(); |
| 132 | |
| 133 | return nack; |
| 134 | } |
| 135 | |
| 136 | uint8_t i2c_read_byte(bool nack, bool send_stop) |
| 137 | { |
| 138 | uint8_t byte = 0; |
| 139 | uint8_t bit; |
| 140 | |
| 141 | for (bit = 0; bit < 8; bit++) { |
| 142 | byte = (byte << 1) | i2c_read_bit(); |
| 143 | } |
| 144 | |
| 145 | i2c_write_bit(nack); |
| 146 | |
| 147 | if (send_stop) |
| 148 | i2c_stop_cond(); |
| 149 | |
| 150 | return byte; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /* EEPROM related code */ |
| 155 | |
| 156 | int eeprom_write_byte(uint8_t slave, uint8_t addr, uint8_t byte) |
| 157 | { |
| 158 | bool nack; |
| 159 | |
| 160 | /* Write slave address */ |
| 161 | nack = i2c_write_byte(true, false, slave << 1); |
| 162 | if (nack) |
| 163 | goto out_stop; |
| 164 | nack = i2c_write_byte(false, false, addr); |
| 165 | if (nack) |
| 166 | goto out_stop; |
| 167 | nack = i2c_write_byte(false, true, byte); |
| 168 | if (nack) |
| 169 | goto out_stop; |
| 170 | |
| 171 | out_stop: |
| 172 | i2c_stop_cond(); |
| 173 | if (nack) |
| 174 | return -1; |
| 175 | else |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | int eeprom_read_byte(uint8_t slave, uint8_t addr) |
| 180 | { |
| 181 | bool nack; |
| 182 | |
| 183 | /* dummy write cycle */ |
| 184 | nack = i2c_write_byte(true, false, slave << 1); |
| 185 | if (nack) |
| 186 | goto out_stop; |
| 187 | nack = i2c_write_byte(false, false, addr); |
| 188 | if (nack) |
| 189 | goto out_stop; |
| 190 | /* Re-start with read */ |
| 191 | nack = i2c_write_byte(true, false, (slave << 1) | 1); |
| 192 | if (nack) |
| 193 | goto out_stop; |
| 194 | |
| 195 | return i2c_read_byte(true, true); |
| 196 | |
| 197 | out_stop: |
| 198 | i2c_stop_cond(); |
| 199 | if (nack) |
| 200 | return -1; |
| 201 | else |
| 202 | return 0; |
| 203 | } |