Fri, 18 Apr 2014 01:34:20 -0600
added RTC emulation (attempts to set the date are ignored, and the year is currently hardcoded to 1987 because UNIX PC SysV has a few Y2K bugs)
andrew@151 | 1 | #include <stdint.h> |
andrew@151 | 2 | #include <stdbool.h> |
andrew@151 | 3 | #include <malloc.h> |
andrew@151 | 4 | #include <time.h> |
andrew@151 | 5 | #include "tc8250.h" |
andrew@151 | 6 | |
andrew@151 | 7 | #ifndef TC8250_DEBUG |
andrew@151 | 8 | #define NDEBUG |
andrew@151 | 9 | #endif |
andrew@151 | 10 | #include "utils.h" |
andrew@151 | 11 | |
andrew@151 | 12 | void tc8250_init(TC8250_CTX *ctx) |
andrew@151 | 13 | { |
andrew@151 | 14 | ctx->chip_enable = false; |
andrew@151 | 15 | ctx->address_latch_enable = false; |
andrew@151 | 16 | ctx->write_enable = false; |
andrew@151 | 17 | ctx->address = 0; |
andrew@151 | 18 | } |
andrew@151 | 19 | |
andrew@151 | 20 | void tc8250_set_chip_enable(TC8250_CTX *ctx, bool enabled) |
andrew@151 | 21 | { |
andrew@151 | 22 | LOG("tc8250_set_chip_enable %d\n", enabled); |
andrew@151 | 23 | ctx->chip_enable = enabled; |
andrew@151 | 24 | } |
andrew@151 | 25 | |
andrew@151 | 26 | void tc8250_set_address_latch_enable(TC8250_CTX *ctx, bool enabled) |
andrew@151 | 27 | { |
andrew@151 | 28 | LOG("tc8250_set_address_latch_enable %d\n", enabled); |
andrew@151 | 29 | ctx->address_latch_enable = enabled; |
andrew@151 | 30 | } |
andrew@151 | 31 | |
andrew@151 | 32 | void tc8250_set_write_enable(TC8250_CTX *ctx, bool enabled) |
andrew@151 | 33 | { |
andrew@151 | 34 | LOG("tc8250_set_write_enable %d\n", enabled); |
andrew@151 | 35 | ctx->write_enable = enabled; |
andrew@151 | 36 | } |
andrew@151 | 37 | |
andrew@151 | 38 | uint8_t get_second(TC8250_CTX *ctx) |
andrew@151 | 39 | { |
andrew@151 | 40 | time_t t; |
andrew@151 | 41 | struct tm g; |
andrew@151 | 42 | uint8_t ret; |
andrew@151 | 43 | t = time(NULL); |
andrew@151 | 44 | gmtime_r(&t, &g); |
andrew@151 | 45 | ret = g.tm_sec; |
andrew@151 | 46 | return (ret); |
andrew@151 | 47 | } |
andrew@151 | 48 | |
andrew@151 | 49 | uint8_t get_minute(TC8250_CTX *ctx) |
andrew@151 | 50 | { |
andrew@151 | 51 | time_t t; |
andrew@151 | 52 | struct tm g; |
andrew@151 | 53 | uint8_t ret; |
andrew@151 | 54 | t = time(NULL); |
andrew@151 | 55 | gmtime_r(&t, &g); |
andrew@151 | 56 | ret = g.tm_min; |
andrew@151 | 57 | return (ret); |
andrew@151 | 58 | } |
andrew@151 | 59 | |
andrew@151 | 60 | uint8_t get_hour(TC8250_CTX *ctx) |
andrew@151 | 61 | { |
andrew@151 | 62 | time_t t; |
andrew@151 | 63 | struct tm g; |
andrew@151 | 64 | uint8_t ret; |
andrew@151 | 65 | t = time(NULL); |
andrew@151 | 66 | gmtime_r(&t, &g); |
andrew@151 | 67 | ret = g.tm_hour; |
andrew@151 | 68 | return (ret); |
andrew@151 | 69 | } |
andrew@151 | 70 | |
andrew@151 | 71 | uint8_t get_day(TC8250_CTX *ctx) |
andrew@151 | 72 | { |
andrew@151 | 73 | time_t t; |
andrew@151 | 74 | struct tm g; |
andrew@151 | 75 | uint8_t ret; |
andrew@151 | 76 | t = time(NULL); |
andrew@151 | 77 | gmtime_r(&t, &g); |
andrew@151 | 78 | ret = g.tm_mday; |
andrew@151 | 79 | return (ret); |
andrew@151 | 80 | } |
andrew@151 | 81 | |
andrew@151 | 82 | uint8_t get_month(TC8250_CTX *ctx) |
andrew@151 | 83 | { |
andrew@151 | 84 | time_t t; |
andrew@151 | 85 | struct tm g; |
andrew@151 | 86 | uint8_t ret; |
andrew@151 | 87 | t = time(NULL); |
andrew@151 | 88 | gmtime_r(&t, &g); |
andrew@151 | 89 | ret = g.tm_mon; |
andrew@151 | 90 | return (ret); |
andrew@151 | 91 | } |
andrew@151 | 92 | |
andrew@151 | 93 | uint8_t get_year(TC8250_CTX *ctx) |
andrew@151 | 94 | { |
andrew@151 | 95 | /*time_t t; |
andrew@151 | 96 | struct tm g; |
andrew@151 | 97 | uint8_t ret; |
andrew@151 | 98 | t = time(NULL); |
andrew@151 | 99 | gmtime_r(&t, &g); |
andrew@151 | 100 | ret = g.tm_year; |
andrew@151 | 101 | return (ret);*/ |
andrew@151 | 102 | return (87); |
andrew@151 | 103 | } |
andrew@151 | 104 | |
andrew@151 | 105 | uint8_t get_weekday(TC8250_CTX *ctx) |
andrew@151 | 106 | { |
andrew@151 | 107 | time_t t; |
andrew@151 | 108 | struct tm g; |
andrew@151 | 109 | uint8_t ret; |
andrew@151 | 110 | t = time(NULL); |
andrew@151 | 111 | gmtime_r(&t, &g); |
andrew@151 | 112 | ret = g.tm_wday; |
andrew@151 | 113 | return (ret); |
andrew@151 | 114 | } |
andrew@151 | 115 | |
andrew@151 | 116 | uint8_t tc8250_read_reg(TC8250_CTX *ctx) |
andrew@151 | 117 | { |
andrew@151 | 118 | LOG("tc8250_read_reg %x\n", ctx->address); |
andrew@151 | 119 | switch (ctx->address){ |
andrew@151 | 120 | case ONE_SEC_DIGT: |
andrew@151 | 121 | return (get_second(ctx) % 10); |
andrew@151 | 122 | case TEN_SEC_DIGT: |
andrew@151 | 123 | return (get_second(ctx) / 10); |
andrew@151 | 124 | case ONE_MIN_DIGT: |
andrew@151 | 125 | return (get_minute(ctx) % 10); |
andrew@151 | 126 | case TEN_MIN_DIGT: |
andrew@151 | 127 | return (get_minute(ctx) / 10); |
andrew@151 | 128 | case ONE_HR_DIGT: |
andrew@151 | 129 | return (get_hour(ctx) % 10); |
andrew@151 | 130 | case TEN_HR_DIGT: |
andrew@151 | 131 | return (get_hour(ctx) / 10); |
andrew@151 | 132 | case ONE_DAY_DIGT: |
andrew@151 | 133 | return (get_day(ctx) % 10); |
andrew@151 | 134 | case TEN_DAY_DIGT: |
andrew@151 | 135 | return (get_day(ctx) / 10); |
andrew@151 | 136 | case ONE_MNTH_DIGT: |
andrew@151 | 137 | return (get_month(ctx) % 10); |
andrew@151 | 138 | case TEN_MNTH_DIGT: |
andrew@151 | 139 | return (get_month(ctx) / 10); |
andrew@151 | 140 | case ONE_YR_DIGT: |
andrew@151 | 141 | return (get_year(ctx) % 10); |
andrew@151 | 142 | case TEN_YR_DIGT: |
andrew@151 | 143 | return (get_year(ctx) / 10); |
andrew@151 | 144 | case WEEK_DAY: |
andrew@151 | 145 | return (get_weekday(ctx) / 10); |
andrew@151 | 146 | case TOUT_CONTROL: |
andrew@151 | 147 | return (0); |
andrew@151 | 148 | case PROTECT_KEY: |
andrew@151 | 149 | return (0); |
andrew@151 | 150 | case RTC_STATUS: |
andrew@151 | 151 | return (0); |
andrew@151 | 152 | default: |
andrew@151 | 153 | return (0); |
andrew@151 | 154 | } |
andrew@151 | 155 | } |
andrew@151 | 156 | |
andrew@151 | 157 | void set_seconds(TC8250_CTX *ctx, uint8_t val) |
andrew@151 | 158 | { |
andrew@151 | 159 | } |
andrew@151 | 160 | |
andrew@151 | 161 | void set_minutes(TC8250_CTX *ctx, uint8_t val) |
andrew@151 | 162 | { |
andrew@151 | 163 | } |
andrew@151 | 164 | |
andrew@151 | 165 | void set_hours(TC8250_CTX *ctx, uint8_t val) |
andrew@151 | 166 | { |
andrew@151 | 167 | } |
andrew@151 | 168 | |
andrew@151 | 169 | void set_days(TC8250_CTX *ctx, uint8_t val) |
andrew@151 | 170 | { |
andrew@151 | 171 | } |
andrew@151 | 172 | |
andrew@151 | 173 | void set_months(TC8250_CTX *ctx, uint8_t val) |
andrew@151 | 174 | { |
andrew@151 | 175 | } |
andrew@151 | 176 | |
andrew@151 | 177 | void set_years(TC8250_CTX *ctx, uint8_t val) |
andrew@151 | 178 | { |
andrew@151 | 179 | } |
andrew@151 | 180 | |
andrew@151 | 181 | void set_weekday(TC8250_CTX *ctx, uint8_t val) |
andrew@151 | 182 | { |
andrew@151 | 183 | } |
andrew@151 | 184 | |
andrew@151 | 185 | void tc8250_write_reg(TC8250_CTX *ctx, uint8_t val) |
andrew@151 | 186 | { |
andrew@151 | 187 | LOG("tc8250_write_reg %x", val); |
andrew@151 | 188 | if (ctx->address_latch_enable){ |
andrew@151 | 189 | LOG(" address\n"); |
andrew@151 | 190 | ctx->address = val; |
andrew@151 | 191 | return; |
andrew@151 | 192 | } |
andrew@151 | 193 | if (ctx->chip_enable){ |
andrew@151 | 194 | LOG(" %x\n", ctx->address); |
andrew@151 | 195 | switch (ctx->address){ |
andrew@151 | 196 | case ONE_SEC_DIGT: |
andrew@151 | 197 | set_seconds(ctx, val % 10); |
andrew@151 | 198 | break; |
andrew@151 | 199 | case TEN_SEC_DIGT: |
andrew@151 | 200 | set_seconds(ctx, val % 10 * 10); |
andrew@151 | 201 | break; |
andrew@151 | 202 | case ONE_MIN_DIGT: |
andrew@151 | 203 | set_minutes(ctx, val % 10); |
andrew@151 | 204 | break; |
andrew@151 | 205 | case TEN_MIN_DIGT: |
andrew@151 | 206 | set_minutes(ctx, val % 10 * 10); |
andrew@151 | 207 | break; |
andrew@151 | 208 | case ONE_HR_DIGT: |
andrew@151 | 209 | set_hours(ctx, val % 10); |
andrew@151 | 210 | break; |
andrew@151 | 211 | case TEN_HR_DIGT: |
andrew@151 | 212 | set_hours(ctx, val % 10 * 10); |
andrew@151 | 213 | break; |
andrew@151 | 214 | case ONE_DAY_DIGT: |
andrew@151 | 215 | set_days(ctx, val % 10); |
andrew@151 | 216 | break; |
andrew@151 | 217 | case TEN_DAY_DIGT: |
andrew@151 | 218 | set_days(ctx, val % 10 * 10); |
andrew@151 | 219 | break; |
andrew@151 | 220 | case ONE_MNTH_DIGT: |
andrew@151 | 221 | set_months(ctx, val % 10); |
andrew@151 | 222 | break; |
andrew@151 | 223 | case TEN_MNTH_DIGT: |
andrew@151 | 224 | set_months(ctx, val % 10 * 10); |
andrew@151 | 225 | break; |
andrew@151 | 226 | case ONE_YR_DIGT: |
andrew@151 | 227 | set_years(ctx, val % 10); |
andrew@151 | 228 | break; |
andrew@151 | 229 | case TEN_YR_DIGT: |
andrew@151 | 230 | set_years(ctx, val % 10 * 10); |
andrew@151 | 231 | break; |
andrew@151 | 232 | case WEEK_DAY: |
andrew@151 | 233 | set_weekday(ctx, val % 10); |
andrew@151 | 234 | break; |
andrew@151 | 235 | case TOUT_CONTROL: |
andrew@151 | 236 | break; |
andrew@151 | 237 | case PROTECT_KEY: |
andrew@151 | 238 | break; |
andrew@151 | 239 | case RTC_STATUS: |
andrew@151 | 240 | break; |
andrew@151 | 241 | default: |
andrew@151 | 242 | break; |
andrew@151 | 243 | } |
andrew@151 | 244 | }else{ |
andrew@151 | 245 | LOG("\n"); |
andrew@151 | 246 | } |
andrew@151 | 247 | } |