123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583 |
- #include <stdbool.h>
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #include "m0110.h"
- #include "debug.h"
- static inline uint8_t raw2scan(uint8_t raw);
- static inline uint8_t inquiry(void);
- static inline uint8_t instant(void);
- static inline void clock_lo(void);
- static inline void clock_hi(void);
- static inline bool clock_in(void);
- static inline void data_lo(void);
- static inline void data_hi(void);
- static inline bool data_in(void);
- static inline uint16_t wait_clock_lo(uint16_t us);
- static inline uint16_t wait_clock_hi(uint16_t us);
- static inline uint16_t wait_data_lo(uint16_t us);
- static inline uint16_t wait_data_hi(uint16_t us);
- static inline void idle(void);
- static inline void request(void);
- #define WAIT_US(stat, us, err) \
- do { \
- if (!wait_##stat(us)) { \
- m0110_error = err; \
- goto ERROR; \
- } \
- } while (0)
- #define WAIT_MS(stat, ms, err) \
- do { \
- uint16_t _ms = ms; \
- while (_ms) { \
- if (wait_##stat(1000)) { \
- break; \
- } \
- _ms--; \
- } \
- if (_ms == 0) { \
- m0110_error = err; \
- goto ERROR; \
- } \
- } while (0)
- #define KEY(raw) ((raw)&0x7f)
- #define IS_BREAK(raw) (((raw)&0x80) == 0x80)
- uint8_t m0110_error = 0;
- void m0110_init(void) {
- idle();
- _delay_ms(1000);
-
- }
- uint8_t m0110_send(uint8_t data) {
- m0110_error = 0;
- request();
- WAIT_MS(clock_lo, 250, 1);
- for (uint8_t bit = 0x80; bit; bit >>= 1) {
- WAIT_US(clock_lo, 250, 3);
- if (data & bit) {
- data_hi();
- } else {
- data_lo();
- }
- WAIT_US(clock_hi, 200, 4);
- }
- _delay_us(100);
- idle();
- return 1;
- ERROR:
- print("m0110_send err: ");
- phex(m0110_error);
- print("\n");
- _delay_ms(500);
- idle();
- return 0;
- }
- uint8_t m0110_recv(void) {
- uint8_t data = 0;
- m0110_error = 0;
- WAIT_MS(clock_lo, 250, 1);
- for (uint8_t i = 0; i < 8; i++) {
- data <<= 1;
- WAIT_US(clock_lo, 200, 2);
- WAIT_US(clock_hi, 200, 3);
- if (data_in()) {
- data |= 1;
- }
- }
- idle();
- return data;
- ERROR:
- print("m0110_recv err: ");
- phex(m0110_error);
- print("\n");
- _delay_ms(500);
- idle();
- return 0xFF;
- }
- uint8_t m0110_recv_key(void) {
- static uint8_t keybuf = 0x00;
- static uint8_t keybuf2 = 0x00;
- static uint8_t rawbuf = 0x00;
- uint8_t raw, raw2, raw3;
- if (keybuf) {
- raw = keybuf;
- keybuf = 0x00;
- return raw;
- }
- if (keybuf2) {
- raw = keybuf2;
- keybuf2 = 0x00;
- return raw;
- }
- if (rawbuf) {
- raw = rawbuf;
- rawbuf = 0x00;
- } else {
- raw = instant();
- }
- switch (KEY(raw)) {
- case M0110_KEYPAD:
- raw2 = instant();
- switch (KEY(raw2)) {
- case M0110_ARROW_UP:
- case M0110_ARROW_DOWN:
- case M0110_ARROW_LEFT:
- case M0110_ARROW_RIGHT:
- if (IS_BREAK(raw2)) {
-
- keybuf = (raw2scan(raw2) | M0110_CALC_OFFSET);
- return (raw2scan(raw2) | M0110_KEYPAD_OFFSET);
- }
- break;
- }
-
- return (raw2scan(raw2) | M0110_KEYPAD_OFFSET);
- break;
- case M0110_SHIFT:
- raw2 = instant();
- switch (KEY(raw2)) {
- case M0110_SHIFT:
-
- rawbuf = raw2;
- return raw2scan(raw);
- break;
- case M0110_KEYPAD:
-
- raw3 = instant();
- switch (KEY(raw3)) {
- case M0110_ARROW_UP:
- case M0110_ARROW_DOWN:
- case M0110_ARROW_LEFT:
- case M0110_ARROW_RIGHT:
- if (IS_BREAK(raw)) {
- if (IS_BREAK(raw3)) {
-
- print("(4)\n");
- keybuf2 = raw2scan(raw);
- keybuf = (raw2scan(raw3) | M0110_CALC_OFFSET);
- return (raw2scan(raw3) | M0110_KEYPAD_OFFSET);
- } else {
-
- print("(3)\n");
- return (raw2scan(raw));
- }
- } else {
- if (IS_BREAK(raw3)) {
-
- print("(2)\n");
- keybuf = (raw2scan(raw3) | M0110_CALC_OFFSET);
- return (raw2scan(raw3) | M0110_KEYPAD_OFFSET);
- } else {
-
- print("(1)\n");
- return (raw2scan(raw3) | M0110_CALC_OFFSET);
- }
- }
- break;
- default:
-
- keybuf = (raw2scan(raw3) | M0110_KEYPAD_OFFSET);
- return raw2scan(raw);
- break;
- }
- break;
- default:
-
- keybuf = raw2scan(raw2);
- return raw2scan(raw);
- break;
- }
- break;
- default:
-
- return raw2scan(raw);
- break;
- }
- }
- static inline uint8_t raw2scan(uint8_t raw) { return (raw == M0110_NULL) ? M0110_NULL : ((raw == M0110_ERROR) ? M0110_ERROR : (((raw & 0x80) | ((raw & 0x7F) >> 1)))); }
- static inline uint8_t inquiry(void) {
- m0110_send(M0110_INQUIRY);
- return m0110_recv();
- }
- static inline uint8_t instant(void) {
- m0110_send(M0110_INSTANT);
- uint8_t data = m0110_recv();
- if (data != M0110_NULL) {
- debug_hex(data);
- debug(" ");
- }
- return data;
- }
- static inline void clock_lo() {
- M0110_CLOCK_PORT &= ~(1 << M0110_CLOCK_BIT);
- M0110_CLOCK_DDR |= (1 << M0110_CLOCK_BIT);
- }
- static inline void clock_hi() {
-
- M0110_CLOCK_DDR &= ~(1 << M0110_CLOCK_BIT);
- M0110_CLOCK_PORT |= (1 << M0110_CLOCK_BIT);
- }
- static inline bool clock_in() {
- M0110_CLOCK_DDR &= ~(1 << M0110_CLOCK_BIT);
- M0110_CLOCK_PORT |= (1 << M0110_CLOCK_BIT);
- _delay_us(1);
- return M0110_CLOCK_PIN & (1 << M0110_CLOCK_BIT);
- }
- static inline void data_lo() {
- M0110_DATA_PORT &= ~(1 << M0110_DATA_BIT);
- M0110_DATA_DDR |= (1 << M0110_DATA_BIT);
- }
- static inline void data_hi() {
-
- M0110_DATA_DDR &= ~(1 << M0110_DATA_BIT);
- M0110_DATA_PORT |= (1 << M0110_DATA_BIT);
- }
- static inline bool data_in() {
- M0110_DATA_DDR &= ~(1 << M0110_DATA_BIT);
- M0110_DATA_PORT |= (1 << M0110_DATA_BIT);
- _delay_us(1);
- return M0110_DATA_PIN & (1 << M0110_DATA_BIT);
- }
- static inline uint16_t wait_clock_lo(uint16_t us) {
- while (clock_in() && us) {
- asm("");
- _delay_us(1);
- us--;
- }
- return us;
- }
- static inline uint16_t wait_clock_hi(uint16_t us) {
- while (!clock_in() && us) {
- asm("");
- _delay_us(1);
- us--;
- }
- return us;
- }
- static inline uint16_t wait_data_lo(uint16_t us) {
- while (data_in() && us) {
- asm("");
- _delay_us(1);
- us--;
- }
- return us;
- }
- static inline uint16_t wait_data_hi(uint16_t us) {
- while (!data_in() && us) {
- asm("");
- _delay_us(1);
- us--;
- }
- return us;
- }
- static inline void idle(void) {
- clock_hi();
- data_hi();
- }
- static inline void request(void) {
- clock_hi();
- data_lo();
- }
|