// // SMART Response XE library // // LCD and keyboard routines for the SRXE handheld classroom communicator // // Copyright (c) 2018 BitBank Software, Inc. // written by Larry Bank // email: bitbank@pobox.com // Project started 8/4/2018 // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // #include #include #include #include #include "SmartResponseXE.h" //#include #ifndef SRXE_SPI_DIV #define SRXE_SPI_DIV 16 #endif // The power button is connected to PD2 (RX1) and signals INT2 #define POWER_BUTTON 0xd2 // Mapping of keyboard to GPIO pins //static byte rowPins[ROWS] = {6,35,34,8,9,0}; //static byte colPins[COLS] = {4,A1,A3,2,1,25,16,19,23,22}; const uint8_t rowPins[ROWS] = {0xe6, 0xb7, 0xb6, 0xb5, 0xb4, 0xe0}; const uint8_t colPins[COLS] = {0xe4, 0xf1, 0xf3, 0xe2, 0xe1, 0xd7, 0xa0, 0xa5, 0xd5, 0xd4}; static byte bKeyMap[COLS]; // bits indicating pressed keys static byte bOldKeyMap[COLS]; // previous map to look for pressed/released keys /* fdufnews 01/2020 Corrected second position of the array was previously : static byte bColorToByte[4] = {0, 0x4a, 0x92, 0xff}; */ static byte bColorToByte[4] = {0, 0x49, 0x92, 0xff}; extern "C" void SRXEWatchdogPoll(void) __attribute__((weak)); extern "C" void SRXEWatchdogPoll(void) { } static byte iCSPin, iDCPin, iResetPin; static int iScrollOffset; void SRXEFill(byte ucData); static void SRXEWriteCommand(unsigned char c); static unsigned char fontChar(char c) { unsigned char ch = (unsigned char)c; return (ch >= 32 && ch <= 127) ? ch : 127; } typedef enum { MODE_DATA = 0, MODE_COMMAND } DC_MODE; #ifndef HIGH #define HIGH 1 #define LOW 0 #define INPUT 0 #define INPUT_PULLUP 1 #define OUTPUT 2 #endif // Chip select for the external 1Mb flash module #define CS_FLASH 0xd3 //Keyboard //Logical Layout (SK# are screen keys: top to bottom 1-5 on left, 6-10 on right): // ROW1|ROW2|ROW3|ROW4|ROW5|ROW6|ROW7|ROW8|ROW9|ROW10 // COL1 1| 2| 3| 4| 5| 6| 7| 8| 9| 0 // COL2 Q| W| E| R| T| Y| U| I| O| P // COL3 A| S| D| F| G| H| J| K| L| Bksp // COL4 Shft| Z| X| C| V| B| N|Down|Entr| Up // COL5 Sym|Frac|Root| Exp| Spc| ,| .| M|Left|Right // COL6 SK1| SK2| SK3| SK4| SK5| SK6| SK7| SK8| SK9| SK10 byte OriginalKeys[] = {'1','2','3','4','5','6','7','8','9','0', 'q','w','e','r','t','y','u','i','o','p', 'a','s','d','f','g','h','j','k','l',8, 0 ,'z','x','c','v','b','n',0x5,0,0x4, // 5 = down, 4 = up 0 ,0xd, 0, 0,' ',',','.','m', 2, 3, // 2 = left, 3 = right 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9}; byte ShiftedKeys[] = {'1','2','3','4','5','6','7','8','9','0', 'Q','W','E','R','T','Y','U','I','O','P', 'A','S','D','F','G','H','J','K','L',8, 0 ,'Z','X','C','V','B','N',0x7,0,0x6, // shifted down/up = page down/up 0 ,0xd, 0, 0,'_',',','.','M', 2, 3, 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9}; byte SymKeys[] = {'!','2','3','$','%','6','\'','\"','(',')', 'q','w','e','r','t','y','u','i','[',']', '=','+','-','f','g','h','j',':','?',8, 0 ,'z','x','c','v','b','n',0x5,0,0x4, 0 ,0xd, 0 , 0 ,0x1,'<','>','m', 2, 3, // 1 = menu 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9}; // // Power on the LCD // const char powerup[] PROGMEM = { 1, 0x01, // soft reset 99, 120, // 120ms delay 1, 0x11, // sleep out 1, 0x28, // display off 99, 50, // 50ms delay 3, 0xc0, 0xf8, 0x00, // Vop = 0xF0 2, 0xc3, 0x04, // BIAS = 1/14 2, 0xc4, 0x05, // Booster = x8 2, 0xd0, 0x1d, // Enable analog circuit 2, 0xb3, 0x00, // Set FOSC divider 2, 0xb5, 0x8b, // N-Line = 0 1, 0x38, // Set grayscale mode (0x39 = monochrome mode) 2, 0x3a, 0x02, // Enable DDRAM interface 2, 0x36, 0x00, // Scan direction setting 2, 0xB0, 0x9f, // Duty setting (0x87?) 5, 0xf0, 0x12,0x12,0x12,0x12, // 77Hz frame rate in all temperatures 1, 0x20, // Display inversion off 1, 0x29, // Display ON 0}; // 8x8 font #ifndef SRXE_SMALL_FONT_ONLY const byte ucFont[]PROGMEM = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x5f,0x5f,0x06,0x00,0x00, 0x00,0x07,0x07,0x00,0x07,0x07,0x00,0x00,0x14,0x7f,0x7f,0x14,0x7f,0x7f,0x14,0x00, 0x24,0x2e,0x2a,0x6b,0x6b,0x3a,0x12,0x00,0x46,0x66,0x30,0x18,0x0c,0x66,0x62,0x00, 0x30,0x7a,0x4f,0x5d,0x37,0x7a,0x48,0x00,0x00,0x04,0x07,0x03,0x00,0x00,0x00,0x00, 0x00,0x1c,0x3e,0x63,0x41,0x00,0x00,0x00,0x00,0x41,0x63,0x3e,0x1c,0x00,0x00,0x00, 0x08,0x2a,0x3e,0x1c,0x1c,0x3e,0x2a,0x08,0x00,0x08,0x08,0x3e,0x3e,0x08,0x08,0x00, 0x00,0x00,0x80,0xe0,0x60,0x00,0x00,0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x00, 0x00,0x00,0x00,0x60,0x60,0x00,0x00,0x00,0x60,0x30,0x18,0x0c,0x06,0x03,0x01,0x00, 0x3e,0x7f,0x59,0x4d,0x47,0x7f,0x3e,0x00,0x40,0x42,0x7f,0x7f,0x40,0x40,0x00,0x00, 0x62,0x73,0x59,0x49,0x6f,0x66,0x00,0x00,0x22,0x63,0x49,0x49,0x7f,0x36,0x00,0x00, 0x18,0x1c,0x16,0x53,0x7f,0x7f,0x50,0x00,0x27,0x67,0x45,0x45,0x7d,0x39,0x00,0x00, 0x3c,0x7e,0x4b,0x49,0x79,0x30,0x00,0x00,0x03,0x03,0x71,0x79,0x0f,0x07,0x00,0x00, 0x36,0x7f,0x49,0x49,0x7f,0x36,0x00,0x00,0x06,0x4f,0x49,0x69,0x3f,0x1e,0x00,0x00, 0x00,0x00,0x00,0x66,0x66,0x00,0x00,0x00,0x00,0x00,0x80,0xe6,0x66,0x00,0x00,0x00, 0x08,0x1c,0x36,0x63,0x41,0x00,0x00,0x00,0x00,0x14,0x14,0x14,0x14,0x14,0x14,0x00, 0x00,0x41,0x63,0x36,0x1c,0x08,0x00,0x00,0x00,0x02,0x03,0x59,0x5d,0x07,0x02,0x00, 0x3e,0x7f,0x41,0x5d,0x5d,0x5f,0x0e,0x00,0x7c,0x7e,0x13,0x13,0x7e,0x7c,0x00,0x00, 0x41,0x7f,0x7f,0x49,0x49,0x7f,0x36,0x00,0x1c,0x3e,0x63,0x41,0x41,0x63,0x22,0x00, 0x41,0x7f,0x7f,0x41,0x63,0x3e,0x1c,0x00,0x41,0x7f,0x7f,0x49,0x5d,0x41,0x63,0x00, 0x41,0x7f,0x7f,0x49,0x1d,0x01,0x03,0x00,0x1c,0x3e,0x63,0x41,0x51,0x33,0x72,0x00, 0x7f,0x7f,0x08,0x08,0x7f,0x7f,0x00,0x00,0x00,0x41,0x7f,0x7f,0x41,0x00,0x00,0x00, 0x30,0x70,0x40,0x41,0x7f,0x3f,0x01,0x00,0x41,0x7f,0x7f,0x08,0x1c,0x77,0x63,0x00, 0x41,0x7f,0x7f,0x41,0x40,0x60,0x70,0x00,0x7f,0x7f,0x0e,0x1c,0x0e,0x7f,0x7f,0x00, 0x7f,0x7f,0x06,0x0c,0x18,0x7f,0x7f,0x00,0x1c,0x3e,0x63,0x41,0x63,0x3e,0x1c,0x00, 0x41,0x7f,0x7f,0x49,0x09,0x0f,0x06,0x00,0x1e,0x3f,0x21,0x31,0x61,0x7f,0x5e,0x00, 0x41,0x7f,0x7f,0x09,0x19,0x7f,0x66,0x00,0x26,0x6f,0x4d,0x49,0x59,0x73,0x32,0x00, 0x03,0x41,0x7f,0x7f,0x41,0x03,0x00,0x00,0x7f,0x7f,0x40,0x40,0x7f,0x7f,0x00,0x00, 0x1f,0x3f,0x60,0x60,0x3f,0x1f,0x00,0x00,0x3f,0x7f,0x60,0x30,0x60,0x7f,0x3f,0x00, 0x63,0x77,0x1c,0x08,0x1c,0x77,0x63,0x00,0x07,0x4f,0x78,0x78,0x4f,0x07,0x00,0x00, 0x47,0x63,0x71,0x59,0x4d,0x67,0x73,0x00,0x00,0x7f,0x7f,0x41,0x41,0x00,0x00,0x00, 0x01,0x03,0x06,0x0c,0x18,0x30,0x60,0x00,0x00,0x41,0x41,0x7f,0x7f,0x00,0x00,0x00, 0x08,0x0c,0x06,0x03,0x06,0x0c,0x08,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80, 0x00,0x00,0x03,0x07,0x04,0x00,0x00,0x00,0x20,0x74,0x54,0x54,0x3c,0x78,0x40,0x00, 0x41,0x7f,0x3f,0x48,0x48,0x78,0x30,0x00,0x38,0x7c,0x44,0x44,0x6c,0x28,0x00,0x00, 0x30,0x78,0x48,0x49,0x3f,0x7f,0x40,0x00,0x38,0x7c,0x54,0x54,0x5c,0x18,0x00,0x00, 0x48,0x7e,0x7f,0x49,0x03,0x06,0x00,0x00,0x98,0xbc,0xa4,0xa4,0xf8,0x7c,0x04,0x00, 0x41,0x7f,0x7f,0x08,0x04,0x7c,0x78,0x00,0x00,0x44,0x7d,0x7d,0x40,0x00,0x00,0x00, 0x60,0xe0,0x80,0x84,0xfd,0x7d,0x00,0x00,0x41,0x7f,0x7f,0x10,0x38,0x6c,0x44,0x00, 0x00,0x41,0x7f,0x7f,0x40,0x00,0x00,0x00,0x7c,0x7c,0x18,0x78,0x1c,0x7c,0x78,0x00, 0x7c,0x78,0x04,0x04,0x7c,0x78,0x00,0x00,0x38,0x7c,0x44,0x44,0x7c,0x38,0x00,0x00, 0x84,0xfc,0xf8,0xa4,0x24,0x3c,0x18,0x00,0x18,0x3c,0x24,0xa4,0xf8,0xfc,0x84,0x00, 0x44,0x7c,0x78,0x4c,0x04,0x0c,0x18,0x00,0x48,0x5c,0x54,0x74,0x64,0x24,0x00,0x00, 0x04,0x04,0x3e,0x7f,0x44,0x24,0x00,0x00,0x3c,0x7c,0x40,0x40,0x3c,0x7c,0x40,0x00, 0x1c,0x3c,0x60,0x60,0x3c,0x1c,0x00,0x00,0x3c,0x7c,0x60,0x30,0x60,0x7c,0x3c,0x00, 0x44,0x6c,0x38,0x10,0x38,0x6c,0x44,0x00,0x9c,0xbc,0xa0,0xa0,0xfc,0x7c,0x00,0x00, 0x4c,0x64,0x74,0x5c,0x4c,0x64,0x00,0x00,0x08,0x08,0x3e,0x77,0x41,0x41,0x00,0x00, 0x00,0x00,0x00,0x77,0x77,0x00,0x00,0x00,0x41,0x41,0x77,0x3e,0x08,0x08,0x00,0x00, 0x02,0x03,0x01,0x03,0x02,0x03,0x01,0x00,0x70,0x78,0x4c,0x46,0x4c,0x78,0x70,0x00}; #endif // 5x7 font (in 6x8 cell) const unsigned char ucSmallFont[]PROGMEM = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x5f,0x06,0x00,0x00,0x07,0x03,0x00, 0x07,0x03,0x00,0x24,0x7e,0x24,0x7e,0x24,0x00,0x24,0x2b,0x6a,0x12,0x00,0x00,0x63, 0x13,0x08,0x64,0x63,0x00,0x36,0x49,0x56,0x20,0x50,0x00,0x00,0x07,0x03,0x00,0x00, 0x00,0x00,0x3e,0x41,0x00,0x00,0x00,0x00,0x41,0x3e,0x00,0x00,0x00,0x08,0x3e,0x1c, 0x3e,0x08,0x00,0x08,0x08,0x3e,0x08,0x08,0x00,0x00,0xe0,0x60,0x00,0x00,0x00,0x08, 0x08,0x08,0x08,0x08,0x00,0x00,0x60,0x60,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02, 0x00,0x3e,0x51,0x49,0x45,0x3e,0x00,0x00,0x42,0x7f,0x40,0x00,0x00,0x62,0x51,0x49, 0x49,0x46,0x00,0x22,0x49,0x49,0x49,0x36,0x00,0x18,0x14,0x12,0x7f,0x10,0x00,0x2f, 0x49,0x49,0x49,0x31,0x00,0x3c,0x4a,0x49,0x49,0x30,0x00,0x01,0x71,0x09,0x05,0x03, 0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x06,0x49,0x49,0x29,0x1e,0x00,0x00,0x6c,0x6c, 0x00,0x00,0x00,0x00,0xec,0x6c,0x00,0x00,0x00,0x08,0x14,0x22,0x41,0x00,0x00,0x24, 0x24,0x24,0x24,0x24,0x00,0x00,0x41,0x22,0x14,0x08,0x00,0x02,0x01,0x59,0x09,0x06, 0x00,0x3e,0x41,0x5d,0x55,0x1e,0x00,0x7e,0x11,0x11,0x11,0x7e,0x00,0x7f,0x49,0x49, 0x49,0x36,0x00,0x3e,0x41,0x41,0x41,0x22,0x00,0x7f,0x41,0x41,0x41,0x3e,0x00,0x7f, 0x49,0x49,0x49,0x41,0x00,0x7f,0x09,0x09,0x09,0x01,0x00,0x3e,0x41,0x49,0x49,0x7a, 0x00,0x7f,0x08,0x08,0x08,0x7f,0x00,0x00,0x41,0x7f,0x41,0x00,0x00,0x30,0x40,0x40, 0x40,0x3f,0x00,0x7f,0x08,0x14,0x22,0x41,0x00,0x7f,0x40,0x40,0x40,0x40,0x00,0x7f, 0x02,0x04,0x02,0x7f,0x00,0x7f,0x02,0x04,0x08,0x7f,0x00,0x3e,0x41,0x41,0x41,0x3e, 0x00,0x7f,0x09,0x09,0x09,0x06,0x00,0x3e,0x41,0x51,0x21,0x5e,0x00,0x7f,0x09,0x09, 0x19,0x66,0x00,0x26,0x49,0x49,0x49,0x32,0x00,0x01,0x01,0x7f,0x01,0x01,0x00,0x3f, 0x40,0x40,0x40,0x3f,0x00,0x1f,0x20,0x40,0x20,0x1f,0x00,0x3f,0x40,0x3c,0x40,0x3f, 0x00,0x63,0x14,0x08,0x14,0x63,0x00,0x07,0x08,0x70,0x08,0x07,0x00,0x71,0x49,0x45, 0x43,0x00,0x00,0x00,0x7f,0x41,0x41,0x00,0x00,0x02,0x04,0x08,0x10,0x20,0x00,0x00, 0x41,0x41,0x7f,0x00,0x00,0x04,0x02,0x01,0x02,0x04,0x00,0x80,0x80,0x80,0x80,0x80, 0x00,0x00,0x03,0x07,0x00,0x00,0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x7f,0x44,0x44, 0x44,0x38,0x00,0x38,0x44,0x44,0x44,0x28,0x00,0x38,0x44,0x44,0x44,0x7f,0x00,0x38, 0x54,0x54,0x54,0x08,0x00,0x08,0x7e,0x09,0x09,0x00,0x00,0x18,0xa4,0xa4,0xa4,0x7c, 0x00,0x7f,0x04,0x04,0x78,0x00,0x00,0x00,0x00,0x7d,0x40,0x00,0x00,0x40,0x80,0x84, 0x7d,0x00,0x00,0x7f,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x7f,0x40,0x00,0x00,0x7c, 0x04,0x18,0x04,0x78,0x00,0x7c,0x04,0x04,0x78,0x00,0x00,0x38,0x44,0x44,0x44,0x38, 0x00,0xfc,0x44,0x44,0x44,0x38,0x00,0x38,0x44,0x44,0x44,0xfc,0x00,0x44,0x78,0x44, 0x04,0x08,0x00,0x08,0x54,0x54,0x54,0x20,0x00,0x04,0x3e,0x44,0x24,0x00,0x00,0x3c, 0x40,0x20,0x7c,0x00,0x00,0x1c,0x20,0x40,0x20,0x1c,0x00,0x3c,0x60,0x30,0x60,0x3c, 0x00,0x6c,0x10,0x10,0x6c,0x00,0x00,0x9c,0xa0,0x60,0x3c,0x00,0x00,0x64,0x54,0x54, 0x4c,0x00,0x00,0x08,0x3e,0x41,0x41,0x00,0x00,0x00,0x00,0x77,0x00,0x00,0x00,0x00, 0x41,0x41,0x3e,0x08,0x00,0x02,0x01,0x02,0x01,0x00,0x00,0x3c,0x26,0x23,0x26,0x3c}; uint8_t getPinInfo(uint8_t pin, volatile uint8_t **iDDR, volatile uint8_t **iPort, int bInput) { uint8_t port, bit; port = (pin & 0xf0); // hex port (A,B,D,E,F) bit = pin & 0x7; switch (port) { case 0xA0: // really port G *iPort = (bInput) ? &PING : &PORTG; *iDDR = &DDRG; break; case 0xB0: *iPort = (bInput) ? &PINB : &PORTB; *iDDR = &DDRB; break; case 0xD0: *iPort = (bInput) ? &PIND : &PORTD; *iDDR = &DDRD; break; case 0xE0: *iPort = (bInput) ? &PINE : &PORTE; *iDDR = &DDRE; break; case 0xF0: *iPort = (bInput) ? &PINF : &PORTF; *iDDR = &DDRF; break; } return bit; } /* getPinInfo() */ // // Simplified pin numbering scheme uses a hex number to specify the port number // and bit. Top 4 bits = port (B/D/E/F/G), bottom 3 bits specify the bit of the port // e.g. 0xB4 = PORTB, bit 4, 0Ax is for port G // void mypinMode(uint8_t pin, uint8_t mode) { uint8_t bit; volatile uint8_t *iPort, *iDDR; bit = getPinInfo(pin, &iDDR, &iPort, 0); switch (mode) { case INPUT: *iDDR &= ~(1< 383 || y > 159 || cx > 384 || cy > 160) return; // invalid SRXEWriteCommand(0x2a); // set column address ucTemp[0] = 0; // start column high byte ucTemp[1] = x/3; // start column low byte ucTemp[2] = 0; // end col high byte ucTemp[3] = (x+cx-1)/3; // end col low byte SRXEWriteDataBlock(ucTemp, 4); SRXEWriteCommand(0x2b); // set row address ucTemp[0] = 0; // start row high byte ucTemp[1] = y; // start row low byte ucTemp[2] = 0; // end row high byte ucTemp[3] = y+cy-1; // end row low byte SRXEWriteDataBlock(ucTemp, 4); SRXEWriteCommand(0x2c); // write RAM } /* SRXESetPosition() */ // // Scroll the screen N lines vertically (positive or negative) // The value given represents a delta which affects the current scroll offset // void SRXEScroll(int iLines) { byte b; iScrollOffset = (iScrollOffset + iLines) % LCD_HEIGHT; SRXEWriteCommand(0x37); // set scroll start line b = (byte)iScrollOffset; SRXEWriteDataBlock(&b, 1); } /* SRXEScroll() */ // // Reset the scroll position to 0 // void SRXEScrollReset(void) { byte b; iScrollOffset = 0; SRXEWriteCommand(0x37); // scroll start address b = 0; SRXEWriteDataBlock(&b, 1); } /* SRXEcdScrollReset() */ void SRXEHorizontalLine(int x, int y, int length,byte color,int thickness){ byte bTemp[128]; SRXESetPosition(x*3,y,length*3,thickness); memset(bTemp, bColorToByte[color], length); for(int i = 0; i< thickness ; i++){ SRXEWriteDataBlock(bTemp, length); } } void SRXEVerticalLine(int x, int y, int height,byte color){ byte bTemp[128]; SRXESetPosition(x*3,y,1, height ); memset(bTemp, bColorToByte[color], height); SRXEWriteDataBlock(bTemp, height); } // // Draw an outline or filled rectangle // Only draws on byte boundaries (3 pixels wide) // (display is treated as 128x136) // void SRXERectangle(int x, int y, int cx, int cy, byte color, byte bFilled) { byte bTemp[128]; if (x < 0 || x > 127 || y < 0 || y > 135) return; if (x+cx > 127 || y+cy > 135) return; if (bFilled) { SRXESetPosition(x*3, y, cx*3, cy); for (y=0; y 3) iFGColor = 3; if (iBGColor > 3) iBGColor = 3; if (iFGColor == 3) { fgColor0 = 0xe0; fgColor1 = 0x1c; fgColor2 = 0x3; } else { fgColor0 = (byte)iFGColor << 6; // first pixel 3-bit version of the color fgColor1 = (byte)iFGColor << 3; // second pixel fgColor2 = (byte)iFGColor; // 3rd pixel } bgColor = bColorToByte[iBGColor]; iLen = strlen(szMsg); #ifndef SRXE_SMALL_FONT_ONLY if (iSize == FONT_LARGE || iSize == FONT_MEDIUM) // draw 12x16 or 15x16 font { int iWidth, iDelta; iWidth = (iSize == FONT_LARGE) ? 15 : 12; iDelta = (iSize == FONT_LARGE) ? 5 : 4; if ((iWidth*iLen) + x > 384) iLen = (384 - x)/iWidth; // can't display it all if (iLen < 0) return -1; for (i=0; i 384) iLen = (384 - x)/9; // can't display it all if (iLen < 0) return -1; for (i=0; i 384) iLen = (384 - x)/6; // can't display it all if (iLen < 0) return -1; for (i=0; i> 16)); // AD1 SPI_transfer((uint8_t)(ulAddr >> 8)); // AD2 SPI_transfer((uint8_t)ulAddr); // AD3 mydigitalWrite(CS_FLASH, HIGH); // wait for the erase to complete if (bWait) { rc = 1; timeout = 0; while (rc & 1) { SRXEWatchdogPoll(); mydigitalWrite(CS_FLASH, LOW); SPI_transfer(0x05); // read status register rc = SPI_transfer(0); mydigitalWrite(CS_FLASH, HIGH); delay(1); timeout++; if (timeout >= 120) // took too long, bail out { return 0; } } } // if asked to wait return 1; } /* SRXEFlashEraseSector() */ // // Write a 256-byte flash page // Address must be on a page boundary // returns 1 for success, 0 for failure // int SRXEFlashWritePage(uint32_t ulAddr, uint8_t *pSrc) { int i, timeout; uint8_t rc; if (ulAddr & 255L) // invalid address return 0; mydigitalWrite(CS_FLASH, LOW); SPI_transfer(0x05); // read status register rc = SPI_transfer(0); mydigitalWrite(CS_FLASH, HIGH); if (rc & 1) // indicates the chip is busy in a write operation { return 0; // fail } // Disable write protect by clearing the status bits mydigitalWrite(CS_FLASH, LOW); SPI_transfer(0x01); // WRSR - write status register SPI_transfer(0x00); // set all bits to 0 mydigitalWrite(CS_FLASH, HIGH); mydigitalWrite(CS_FLASH, LOW); SPI_transfer(0x06); // WREN - Write enable mydigitalWrite(CS_FLASH, HIGH); mydigitalWrite(CS_FLASH, LOW); SPI_transfer(0x02); // PP - page program // send 3-byte address (big-endian order) SPI_transfer((uint8_t)(ulAddr >> 16)); // AD1 SPI_transfer((uint8_t)(ulAddr >> 8)); // AD2 SPI_transfer((uint8_t)ulAddr); // AD3 for (i=0; i<256; i++) { SPI_transfer(pSrc[i]); // write the 256 data bytes } mydigitalWrite(CS_FLASH, HIGH); // this executes the command internally // wait for the write to complete rc = 1; timeout = 0; while (rc & 1) { SRXEWatchdogPoll(); mydigitalWrite(CS_FLASH, LOW); SPI_transfer(0x05); // read status register rc = SPI_transfer(0); mydigitalWrite(CS_FLASH, HIGH); delay(1); timeout++; if (timeout >= 20) // took too long, bail out { return 0; } } return 1; } /* SRXEFlashWritePage() */ // // Read N bytes from SPI flash // int SRXEFlashRead(uint32_t ulAddr, uint8_t *pDest, int iLen) { int i; mydigitalWrite(CS_FLASH, LOW); SPI_transfer(0x03); // issue read instruction // send 3-byte address (big-endian order) SPI_transfer((uint8_t)(ulAddr >> 16)); // AD1 SPI_transfer((uint8_t)(ulAddr >> 8)); // AD2 SPI_transfer((uint8_t)ulAddr); // AD3 for (i=0; i