void LCD_ReturnHome(void) { LCD_RS = 0; LCD_RW = 0; LCD_DB = 0b00000010; LCD_TOGGLE() //Wait 1.64ms for the return to complete. Wait_us(1640); LCD_Row = 0; LCD_Col = 0; }
void LCD_PrintChar(char dat) { LCD_E = 0; LCD_RS = 1; LCD_RW = 0; LCD_DB = dat; LCD_TOGGLE() //Wait 75us Wait_us(75); if (LCD_Row) LCD_Line1[LCD_Col] = dat; else LCD_Line2[LCD_Col] = dat; LCD_Col++; if (LCD_Col > 19) LCD_Col = 19; }
void LCD_CursorShift(char ShiftDisplay, char ShiftRight) { unsigned char dat = 0b00010000; if (ShiftDisplay) dat |= 0b00001000; if (ShiftRight) dat |= 0b00000100; LCD_E = 0; LCD_RS = 0; LCD_RW = 0; LCD_DB = dat; LCD_TOGGLE() //Wait 75us Wait_us(75); }
void LCD_FunctionSet(char EightBit, char DualLine, char FiveByTenDots) { unsigned char dat = 0b00100000; if (EightBit) dat |= 0b00010000; if (DualLine) dat |= 0b00001000; if (FiveByTenDots) dat |= 0b00000100; LCD_E = 0; LCD_RS = 0; LCD_RW = 0; LCD_DB = dat; LCD_TOGGLE() //Wait 75us Wait_us(75); }
void LCD_DisplayOnOff(char DisplayOn, char CursorOn, char BlinkOn) { unsigned char dat = 0b00001000; if (DisplayOn) dat |= 0b00000100; if (CursorOn) dat |= 0b00000010; if (BlinkOn) dat |= 0b00000001; LCD_E = 0; LCD_RS = 0; LCD_RW = 0; LCD_DB = dat; LCD_TOGGLE() //Wait 75us Wait_us(75); }
void LCD_EntryModeSet(char Increment, char ShiftOn) { unsigned char dat = 0b00000100; if (Increment) dat |= 0b00000010; if (ShiftOn) dat |= 0b00000001; LCD_E = 0; LCD_RS = 0; LCD_RW = 0; LCD_DB = dat; LCD_TOGGLE() //Wait 75us Wait_us(75); }
void LCD_SetPosition(char Row, char Col) { unsigned char dat = Col; if (Row == 1) dat += 0x40; dat |= 0b10000000; LCD_E = 0; LCD_RS = 0; LCD_RW = 0; LCD_DB = dat; LCD_TOGGLE() //Wait 75us Wait_us(75); LCD_Row = Row; LCD_Col = Col; }
void LCD_ClearDisplay(void) { char idx; LCD_RS = 0; LCD_RW = 0; LCD_DB = 0b00000001; LCD_TOGGLE() //Wait 1.64ms for the clear to complete. Wait_us(1640); for (idx = 0; idx < 20; idx++) { LCD_Line1[idx] = 32; LCD_Line2[idx] = 32; } LCD_Row = 0; LCD_Col = 0; }
//----------------------------------------------------------------------------- // Wait_ms //----------------------------------------------------------------------------- // // Return Value : error code // Parameters : number of ms to wait // // This value waits for TFms to go from 0 to 1, at least <ms> times. // //----------------------------------------------------------------------------- void Wait_ms (uint16_t ms) { while (ms-- != 0) Wait_us(1000); }
//----------------------------------------------------------------------------- // Command_Decode //----------------------------------------------------------------------------- // // Return Value : Error code // Parameters : Pointer to input string // // Parses the command string and calls the appropriate functions. // //----------------------------------------------------------------------------- uint8_t CommandDecode(char * instr) { char * cp; // character pointer const DEVICE_FAMILY *dfptr; // pointer to current device family const DERIVATIVE_ENTRY *deptr; // pointer to current derivative uint8_t command_number = 99; // number of command encoded in 'instr' uint8_t result = INVALID_COMMAND; printf("Command: %s\n", instr); command_number = 0xFF; if (instr[0] >= '0' && instr[0] <= '9') { // if first char is a digit, then interpret it as a command number command_number = atoi (instr); } { // interpret command as a string and find command number // or find command by command_number const COMMAND *ctptr = Commands; while (ctptr->name != NULL) { if (command_number != 0xFF) { if (ctptr->number == command_number) break; } else if (strncmp (instr, ctptr->name, ctptr->name_size) == 0) { // we've found the command, so record its number and exit command_number = ctptr->number; break; } ctptr++; } if (ctptr->name != NULL) { if (ctptr->need_discover && !FamilyFound) { result = DEVICE_UNDISCOVERED; command_number = 0xFE; // Unknown command } } else command_number = 0xFF; // Unknown command } // Now we have a command number, so act on it. switch (command_number) { case 0: // Device Autodetect { uint8_t deviceId = 0xFF; // initialize device and derivative uint8_t revisionId = 0xFF; uint8_t derivativeId = 0xFF; // id's to invalid selections printf("Device Autodetect\n"); Start_Stopwatch(); if (NO_ERROR == (result = C2_Halt ()) && NO_ERROR == (result = C2_Discover (&deviceId, &revisionId, &derivativeId)) ) { CommandsList = KnownFamilies[FamilyNumber].InitStrings; } Stop_Stopwatch(); printf("Device ID : %02X\n", deviceId); printf("Revision ID : %02X\n", revisionId); printf("Derivative ID : %02X\n", derivativeId); break; } case 1: // Print Menu { printf("? stub\n"); Start_Stopwatch(); Display_Menu(); Stop_Stopwatch(); result = NO_ERROR; break; } case 2: // Wait ms { uint16_t wait_time; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { wait_time = atoi (cp); printf("Waiting %d ms\n", wait_time); Start_Stopwatch(); Wait_ms (wait_time); Stop_Stopwatch(); printf("Stopwatch_ms is %u\n", Stopwatch_ms); result = NO_ERROR; } break; } case 3: // Wait us { uint16_t wait_time; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { wait_time = atoi (cp); printf("Waiting %d us\n", wait_time); Start_Stopwatch(); Wait_us (wait_time); Stop_Stopwatch(); printf("Stopwatch_us is %u\n", Stopwatch_us); result = NO_ERROR; } break; } case 4: // Start Stopwatch { printf("Start Stopwatch\n"); result = Start_Stopwatch(); break; } case 5: // Stop Stopwatch { printf("Stop Stopwatch\n"); result = Stop_Stopwatch(); printf("Stopwatch_ms is %u\n", Stopwatch_ms); printf("Stopwatch_us is %u\n", Stopwatch_us); break; } case 6: // Set Timeout ms { uint16_t wait_time; printf("Set Timeout ms:\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { wait_time = atoi (cp); printf("Timing out for %d ms\n", wait_time); Start_Stopwatch(); SetTimeout_ms (wait_time); SetTimeout_us (1); while (!IsDoneTimeout_ms()) ; Stop_Stopwatch(); printf("Stopwatch_ms is %u\n", Stopwatch_ms); result = NO_ERROR; } break; } case 7: // Set Timeout us { uint16_t wait_time; printf("Set Timeout us\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { wait_time = atoi(cp); printf("Timing out for %d us\n", wait_time); Start_Stopwatch(); SetTimeout_us(wait_time); while (!IsDoneTimeout_us()) ; Stop_Stopwatch(); printf("Stopwatch_us is %u\n", Stopwatch_us); result = NO_ERROR; } break; } case 8: // Pin init { printf("Pin Init\n"); result = Pin_Init(); break; } case 9: // C2 Reset { printf("C2 Reset\n"); result = C2_Reset(); break; } case 10: // C2 Write Address { uint16_t addr; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { addr = atox (cp); printf("C2 Write Address: %02X\n", addr); Start_Stopwatch(); result = C2_WriteAR(addr); Stop_Stopwatch(); } break; } case 11: // C2 Read Address { Start_Stopwatch(); result = C2_ReadAR(); Stop_Stopwatch(); printf("C2 Read Address: %02X\n", (uint16_t) C2_AR); break; } case 12: // C2 Write Data { uint8_t data; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { data = atox (cp); printf("C2 Write Data: %02X\n", (uint16_t) data); Start_Stopwatch (); result = C2_WriteDR (data, C2_WRITE_DR_TIMEOUT_US); Stop_Stopwatch (); } break; } case 13: // C2 Read Data { Start_Stopwatch (); result = C2_ReadDR (C2_READ_DR_TIMEOUT_US); Stop_Stopwatch (); printf("C2 Read Data: %02X\n", (uint16_t) C2_DR); break; } case 14: // C2 Reset and Halt { printf("C2 Reset and Halt\n"); result = C2_Halt (); break; } case 15: // C2 Get Device ID { uint8_t devId; printf("C2 Get Device ID\n"); Start_Stopwatch (); result = C2_GetDevID (&devId); Stop_Stopwatch (); printf("Device ID is %u, 0x%04X\n", devId, devId); break; } case 16: // C2 Get Revision ID { uint8_t revid; printf("C2 Get Revision ID\n"); Start_Stopwatch (); result = C2_GetRevID (&revid); Stop_Stopwatch (); printf("Revision ID is %u, 0x%04X\n", revid, revid); break; } case 17: // C2 Read SFR { uint8_t sfr_value, sfr_address; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_address = atox (cp); Start_Stopwatch (); result = C2_ReadSFR (sfr_address, &sfr_value); Stop_Stopwatch (); printf("C2 Read SFR(%02X) %02X\n", (uint16_t) sfr_address, (uint16_t) sfr_value); } break; } case 18: // C2 Write SFR { uint8_t sfr_address, sfr_value; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_address = atox (cp); cp = GetNextWord(cp); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_value = atox (cp); printf("C2 Write %02X to SFR(%02X)\n", (uint16_t) sfr_value, (uint16_t) sfr_address); Start_Stopwatch (); result = C2_WriteSFR (sfr_address, sfr_value); Stop_Stopwatch (); } } break; } case 19: // C2 Read Direct { uint8_t sfr_value, sfr_address; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_address = atox (cp); Start_Stopwatch (); result = C2_ReadDirect (sfr_address, &sfr_value, C2_DIRECT); Stop_Stopwatch (); printf("C2 Read Direct(%02X) %02X\n", (uint16_t) sfr_address, (uint16_t) sfr_value); } break; } case 20: // C2 Write Direct <address> <value> { uint8_t sfr_address, sfr_value; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_address = atox (cp); cp = GetNextWord(cp); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_value = atox (cp); printf("C2 Write %02x to Direct(%02X)\n", (uint16_t) sfr_value, (uint16_t) sfr_address); Start_Stopwatch (); result = C2_WriteDirect (sfr_address, sfr_value, C2_DIRECT); Stop_Stopwatch (); } } break; } case 21: // C2 Read Indirect { uint8_t sfr_value, sfr_address; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_address = atox (cp); Start_Stopwatch (); result = C2_ReadDirect (sfr_address, &sfr_value, C2_INDIRECT); Stop_Stopwatch (); printf("C2 Read Indirect(%02X) %02X\n", (uint16_t) sfr_address, (uint16_t) sfr_value); } break; } case 22: // C2 Write Indirect { uint8_t sfr_address; uint8_t sfr_value; cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_address = atox (cp); cp = GetNextWord(cp); if (NO_ERROR == (result = CheckEmpty(cp))) { sfr_value = atox (cp); printf("C2 Write %02x to Indirect(%02X)\n", (uint16_t) sfr_value, (uint16_t) sfr_address); Start_Stopwatch (); result = C2_WriteDirect (sfr_address, sfr_value, C2_INDIRECT); Stop_Stopwatch (); } } break; } case 23: // C2 Discover { uint8_t j, deviceId, revisionId, derivativeId; printf("C2 Discover\n"); Start_Stopwatch (); result = C2_Discover (&deviceId, &revisionId, &derivativeId); Stop_Stopwatch (); if (result != NO_ERROR) break; dfptr = &(KnownFamilies[FamilyNumber]); deptr = &(KnownFamilies[FamilyNumber].DerivativeList[DerivativeNumber]); printf("Family Information:\n"); printf("Device ID: 0x%04X\n", dfptr->DEVICE_ID); printf("Family string: %s\n", dfptr->FAMILY_STRING); printf("Mem Type: %u\n", (uint16_t) dfptr->MEM_TYPE); printf("Page Size: %u\n", dfptr->PAGE_SIZE); printf("Has SFLE: %u\n", (uint16_t) dfptr->HAS_SFLE); printf("Security Type: %u\n", (uint16_t) dfptr->SECURITY_TYPE); printf("FPDAT address: 0x%02X\n", (uint16_t) dfptr->FPDAT); printf("Device ID: 0x%04X\n", dfptr->DEVICE_ID); printf("Init strings:\n"); for (j = 0; ; j++) { if (dfptr->InitStrings[j] == NULL) break; printf("%s\n", dfptr->InitStrings[j]); } printf("\n"); printf("Derivative Information\n"); printf("----------------------\n"); printf("Derivative ID : %02X\n", deptr->DERIVATIVE_ID); printf("Derivative String : %s\n", deptr->DERIVATIVE_STRING); printf("Features String : %s\n", deptr->FEATURES_STRING); printf("Package String : %s \n", deptr->PACKAGE_STRING); printf("Code Start Address : %05X\n", deptr->CODE_START); printf("Code Size : %05X\n", deptr->CODE_SIZE); printf("Write Lock Byte Addr : %05X\n", deptr->WRITELOCKBYTEADDR); printf("Read Lock Byte Addr : %05X\n", deptr->READLOCKBYTEADDR); printf("Code 2 Start Address : %05X\n", deptr->CODE2_START); printf("Code 2 Size : %05X\n", deptr->CODE2_SIZE); printf("\n"); break; } case 24: // Run Init String { result = NO_ERROR; printf("Execute Device Init String:\n"); if (FamilyFound == true) CommandsList = KnownFamilies[FamilyNumber].InitStrings; else printf("Device not connected.\n"); break; } case 25: // C2 Flash Read <start addr> <length> { uint32_t addr; uint16_t length; printf("C2 Flash Read\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { addr = atolx (cp); cp = GetNextWord(cp); if (NO_ERROR == (result = CheckEmpty(cp))) { length = atoi (cp); if (length > sizeof (BinDest)) length = sizeof (BinDest); printf( "Reading %u bytes starting at address 0x%05lX\n", length, (unsigned long)addr ); Start_Stopwatch (); result = C2_FLASH_Read (BinDest, addr, length); Stop_Stopwatch (); BIN2HEXSTR (HexDest, BinDest, length); printf("Memory contents are %s\n", HexDest); } } break; } case 26: // C2 OTP Read <start addr> <length> { uint32_t addr; uint16_t length; printf("C2 OTP Read\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { addr = atolx (cp); cp = GetNextWord(cp); if (NO_ERROR == (result = CheckEmpty(cp))) { length = atoi (cp); if (length > sizeof (BinDest)) length = sizeof (BinDest); printf("Reading %u bytes starting at address 0x%05lX\n", length, (unsigned long)addr ); Start_Stopwatch (); result = C2_OTP_Read (BinDest, addr, length); Stop_Stopwatch (); BIN2HEXSTR (HexDest, BinDest, length); printf("Memory contents are %s\n", HexDest); } } break; } case 27: // C2 Flash Write <start addr> <hex string> { uint32_t addr; uint8_t length; printf("C2 Flash Write\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { addr = atolx (cp); cp = GetNextWord(cp); if (NO_ERROR == (result = CheckEmpty(cp))) { // warning! 'dest' could be overtaken by a long string if (NO_ERROR == (result = HEXSTR2BIN (BinDest, cp, &length, sizeof(BinDest)))) { printf("Writing %u bytes starting at address 0x%05X\n", length, addr); // printf("Writing the following string: %s\n", cp); Start_Stopwatch (); result = C2_FLASH_Write (addr, BinDest, length); Stop_Stopwatch (); } } } break; } case 28: // C2 OTP Write <start addr> <hex string> { uint32_t addr; uint8_t length; printf("C2 OTP Write\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { addr = atolx (cp); cp = GetNextWord(cp); if (NO_ERROR == (result = CheckEmpty(cp))) { if (NO_ERROR != (result = HEXSTR2BIN (BinDest, cp, &length, sizeof(BinDest)))) { printf("Hex string too long"); break; } printf( "Writing %u bytes starting at address 0x%05lX\n", (uint16_t) length, (unsigned long)addr ); printf("Writing the following string: %s\n", cp); Start_Stopwatch (); result = C2_OTP_Write (addr, BinDest, length); Stop_Stopwatch (); } } break; } case 29: // C2 Page Erase <address in page to erase> { uint32_t addr; printf("C2 Flash Page Erase\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { addr = atolx (cp); printf("Erasing page containing address 0x%05X\n", addr); Start_Stopwatch (); result = C2_FLASH_PageErase (addr); Stop_Stopwatch (); } break; } case 30: // C2 Device Erase { printf("C2 Flash Device Erase\n"); printf("Erasing device...\n"); Start_Stopwatch (); result = C2_FLASH_DeviceErase (); Stop_Stopwatch (); break; } case 31: // C2 Flash Blank Check { uint32_t addr; uint32_t length; printf("C2 Flash Blank Check\n"); addr = KnownFamilies[FamilyNumber].DerivativeList[DerivativeNumber].CODE_START; length = KnownFamilies[FamilyNumber].DerivativeList[DerivativeNumber].CODE_SIZE; printf("Checking starting at address 0x%05X for 0x%05X bytes: ", addr, length); Start_Stopwatch (); result = C2_FLASH_BlankCheck (addr, length); Stop_Stopwatch (); printf((result == DEVICE_IS_BLANK) ? "OK\n" : "Fail\n"); break; } case 32: // C2 OTP Blank Check { uint32_t addr; uint32_t length; printf("C2 OTP Blank Check\n"); addr = KnownFamilies[FamilyNumber].DerivativeList[DerivativeNumber].CODE_START; length = KnownFamilies[FamilyNumber].DerivativeList[DerivativeNumber].CODE_SIZE; printf("Checking starting at address 0x%05X for 0x%05X bytes: ", addr, length); Start_Stopwatch (); result = C2_OTP_BlankCheck (addr, length); Stop_Stopwatch (); printf((result == NO_ERROR) ? "OK\n" : "Fail\n"); break; } case 33: // C2 Get Lock Byte value { printf("C2 Get Lock Byte\n"); break; } case 34: // Write Target to HEX { printf("Write Target to HEX:\n"); Start_Stopwatch (); result = OP_Write_TARGET2HEX(); Stop_Stopwatch (); break; } case 36: // Write HEX to Target { HEX_RECORD hex; printf("Write HEX to Target:\n"); cp = GetNextWord(instr); if (NO_ERROR == (result = CheckEmpty(cp))) { hex.Buf = BinDest; result = HEX_Decode(&hex, cp, sizeof(BinDest)); if (result == NO_ERROR && hex.RECLEN != 0) { printf("Writing %u bytes starting at address 0x%05X\n", hex.RECLEN, hex.OFFSET.U16); Start_Stopwatch (); result = C2_FLASH_Write (hex.OFFSET.U16, hex.Buf, hex.RECLEN); Stop_Stopwatch (); } else if (result == EOF_HEX_RECORD) result = NO_ERROR; } break; } case 35: // Read SFRs and directs { uint8_t row; uint8_t col; uint8_t value; Start_Stopwatch (); for (row = 0xF8; row != 0x00; row = row - 8) { for (col = 0; col != 0x08; col++) { if (NO_ERROR != (result = C2_ReadDirect ((row+col), &value, C2_DIRECT))) break; if (col == 0) printf("\n0X%02X: %02X", (uint16_t) (row), (uint16_t) value); else printf(" %02X", (uint16_t) value); } } printf("\n\n"); Stop_Stopwatch (); break; } case 0xFE: break; default: { result = INVALID_COMMAND; } } printf("Result: %02X %s\n", result, GetErrorName(result)); return result; }