/* Copyright 2011,2012 Jun WAKO This software is licensed with a Modified BSD License. All of this is supposed to be Free Software, Open Source, DFSG-free, GPL-compatible, and OK to use in both free and proprietary applications. Additions and corrections to this file are welcome. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* M0110A Support was contributed by skagon@github */ #include #include #include #include #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) { uint8_t data; idle(); _delay_ms(1000); m0110_send(M0110_MODEL); data = m0110_recv(); print("m0110_init model: "); phex(data); print("\n"); m0110_send(M0110_TEST); data = m0110_recv(); print("m0110_init test: "); phex(data); print("\n"); } uint8_t m0110_send(uint8_t data) { m0110_error = 0; request(); WAIT_MS(clock_lo, 250, 1); // keyboard may block long time 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); // hold last bit for 80us 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); // keyboard may block long time 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; } /* Handling for exceptional case of key combinations for M0110A Shift and Calc/Arrow key could be operated simultaneously: Case Shift Arrow Events Interpret ------------------------------------------------------------------- 1 Down Down 71, 79, DD Calc(d)*a *b 2 Down Up 71, 79, UU Arrow&Calc(u)*a 3 Up Down F1, 79, DD Shift(u) *c 4 Up Up F1, 79, UU Shift(u) and Arrow&Calc(u)*a Case Shift Calc Events Interpret ------------------------------------------------------------------- 5(1) Down Down 71, 71, 79, DD Shift(d) and Cacl(d) 6(2) Down Up F1, 71, 79, UU Shift(u) and Arrow&Calc(u)*a 7(1) Up Down F1, 71, 79, DD Shift(u) and Calc(d) 8(4) Up Up F1, F1, 79, UU Shift(ux2) and Arrow&Calc(u)*a During Calc key is hold: Case Shift Arrow Events Interpret ------------------------------------------------------------------- A(3) ---- Down F1, 79, DD Shift(u) *c B ---- Up 79, UU Arrow&Calc(u)*a C Down ---- F1, 71 Shift(u) and Shift(d) D Up ---- F1 Shift(u) E Hold Down 79, DD Normal F Hold Up 79, UU Arrow&Calc(u)*a G(1) Down Down F1, 71, 79, DD Shift(u)*b and Calc(d)*a H(2) Down Up F1, 71, 79, UU Shift(u) and Arrow&Calc(u)*a I(3) Up Down F1, F1, 79, DD Shift(ux2) *c J(4) Up Up F1, 79, UU Shift(u) and Arrow&Calc(u)*a Case Shift Calc Events Interpret ------------------------------------------------------------------- K(1) ---- Down 71, 79, DD Calc(d)*a L(4) ---- Up F1, 79, UU Shift(u) and Arrow&Calc(u)*a M(1) Hold Down 71, 79, DD Calc(d)*a N Hold Up 79, UU Arrow&Calc(u)*a Where DD/UU indicates part of Keypad Down/Up event. *a: Impossible to distinguish btween Arrow and Calc event. *b: Shift(d) event is ignored. *c: Arrow/Calc(d) event is ignored. */ 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(); // Use INSTANT for better response. Should be INQUIRY ? } 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)) { // Case B,F,N: keybuf = (raw2scan(raw2) | M0110_CALC_OFFSET); // Calc(u) return (raw2scan(raw2) | M0110_KEYPAD_OFFSET); // Arrow(u) } break; } // Keypad or Arrow return (raw2scan(raw2) | M0110_KEYPAD_OFFSET); break; case M0110_SHIFT: raw2 = instant(); switch (KEY(raw2)) { case M0110_SHIFT: // Case: 5-8,C,G,H rawbuf = raw2; return raw2scan(raw); // Shift(d/u) break; case M0110_KEYPAD: // Shift + Arrow, Calc, or etc. raw3 = instant(); switch (KEY(raw3)) { case M0110_ARROW_UP: case M0110_ARROW_DOWN: case M0110_ARROW_LEFT: case M0110_ARROW_RIGHT:
/*
             LUFA Library
     Copyright (C) Dean Camera, 2017.

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

/*
  Copyright 2017  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaims all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

/** \file
 *
 *  Header file for XMEGANVM.c.
 */

#ifndef _XMEGA_NVM_
#define _XMEGA_NVM_

	/* Includes: */
		#include <avr/io.h>
		#include <avr/interrupt.h>
		#include <stdbool.h>

		#include <LUFA/Common/Common.h>

		#include "XPROGProtocol.h"
		#include "XPROGTarget.h"
		#include "Config/AppConfig.h"

	/* Preprocessor Checks: */
		#if ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1))
			#undef ENABLE_ISP_PROTOCOL

			#if !defined(ENABLE_XPROG_PROTOCOL)
				#define ENABLE_XPROG_PROTOCOL
			#endif
		#endif

	/* Defines: */
		#define XMEGA_CRC_LENGTH_BYTES               3

		#define XMEGA_NVM_REG_ADDR0                  0x00
		#define XMEGA_NVM_REG_ADDR1                  0x01
		#define XMEGA_NVM_REG_ADDR2                  0x02
		#define XMEGA_NVM_REG_DAT0                   0x04
		#define XMEGA_NVM_REG_DAT1                   0x05
		#define XMEGA_NVM_REG_DAT2                   0x06
		#define XMEGA_NVM_REG_CMD                    0x0A
		#define XMEGA_NVM_REG_CTRLA                  0x0B
		#define XMEGA_NVM_REG_CTRLB                  0x0C
		#define XMEGA_NVM_REG_INTCTRL                0x0D
		#define XMEGA_NVM_REG_STATUS                 0x0F
		#define XMEGA_NVM_REG_LOCKBITS               0x10

		#define XMEGA_NVM_BIT_CTRLA_CMDEX            (1 << 0)

		#define XMEGA_NVM_CMD_NOOP                   0x00
		#define XMEGA_NVM_CMD_CHIPERASE              0x40
		#define XMEGA_NVM_CMD_READNVM                0x43
		#define XMEGA_NVM_CMD_LOADFLASHPAGEBUFF      0x23
		#define XMEGA_NVM_CMD_ERASEFLASHPAGEBUFF     0x26
		#define XMEGA_NVM_CMD_ERASEFLASHPAGE         0x2B
		#define XMEGA_NVM_CMD_WRITEFLASHPAGE         0x2E
		#define XMEGA_NVM_CMD_ERASEWRITEFLASH        0x2F
		#define XMEGA_NVM_CMD_FLASHCRC               0x78
		#define XMEGA_NVM_CMD_ERASEAPPSEC            0x20
		#define XMEGA_NVM_CMD_ERASEAPPSECPAGE        0x22
		#define XMEGA_NVM_CMD_WRITEAPPSECPAGE        0x24
		#define XMEGA_NVM_CMD_ERASEWRITEAPPSECPAGE   0x25
		#define XMEGA_NVM_CMD_APPCRC                 0x38
		#define XMEGA_NVM_CMD_ERASEBOOTSEC           0x68
		#define XMEGA_NVM_CMD_ERASEBOOTSECPAGE       0x2A
		#define XMEGA_NVM_CMD_WRITEBOOTSECPAGE       0x2C
		#define XMEGA_NVM_CMD_ERASEWRITEBOOTSECPAGE  0x2D
		#define XMEGA_NVM_CMD_BOOTCRC                0x39
		#define XMEGA_NVM_CMD_READUSERSIG            0x03
		#define XMEGA_NVM_CMD_ERASEUSERSIG           0x18
		#define XMEGA_NVM_CMD_WRITEUSERSIG           0x1A
		#define XMEGA_NVM_CMD_READCALIBRATION        0x02
		#define XMEGA_NVM_CMD_READFUSE               0x07
		#define XMEGA_NVM_CMD_WRITEFUSE              0x4C
		#define XMEGA_NVM_CMD_WRITELOCK              0x08
		#define XMEGA_NVM_CMD_LOADEEPROMPAGEBUFF     0x33
		#define XMEGA_NVM_CMD_ERASEEEPROMPAGEBUFF    0x36
		#define XMEGA_NVM_CMD_ERASEEEPROM            0x30
		#define XMEGA_NVM_CMD_ERASEEEPROMPAGE        0x32
		#define XMEGA_NVM_CMD_WRITEEEPROMPAGE        0x34
		#define XMEGA_NVM_CMD_ERASEWRITEEEPROMPAGE   0x35
		#define XMEGA_NVM_CMD_READEEPROM             0x06

	/* Function Prototypes: */
		bool XMEGANVM_WaitWhileNVMBusBusy(void);
		bool XMEGANVM_WaitWhileNVMControllerBusy(void);
		bool XMEGANVM_EnablePDI(void);
		void XMEGANVM_DisablePDI(void);
		bool XMEGANVM_GetMemoryCRC(const uint8_t CRCCommand,
			                       uint32_t* const CRCDest);
		bool XMEGANVM_ReadMemory(const uint32_t ReadAddress,
		                         uint8_t* ReadBuffer,
		                         uint16_t ReadSize);
		bool XMEGANVM_WriteByteMemory(const uint8_t WriteCommand,
		                              const uint32_t WriteAddress,
		                              const uint8_t Byte);
		bool XMEGANVM_WritePageMemory(const uint8_t WriteBuffCommand,
		                              const uint8_t EraseBuffCommand,
		                              const uint8_t WritePageCommand,
		                              const uint8_t PageMode,
		                              const uint32_t WriteAddress,
		                              const uint8_t* WriteBuffer,
		                              uint16_t WriteSize);
		bool XMEGANVM_EraseMemory(const uint8_t EraseCommand,
		                          const uint32_t Address);

		#if defined(INCLUDE_FROM_XMEGANVM_C)
			static void XMEGANVM_SendNVMRegAddress(const uint8_t Register);
			static void XMEGANVM_SendAddress(const uint32_t AbsoluteAddress);
		#endif

#endif