Skip to content

Instantly share code, notes, and snippets.

@Jack2
Last active August 29, 2015 14:18
Show Gist options
  • Save Jack2/3e7b6b7d633d32b20c93 to your computer and use it in GitHub Desktop.
Save Jack2/3e7b6b7d633d32b20c93 to your computer and use it in GitHub Desktop.
플래시 메모리 펌웨어 덤프
/*
* FlashDumper.c
*
* Created: 2015-04-04 오후 5:17:30
* Author: Jaeki
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
#include <stdio.h>
void UART_INIT(){
/* Status Register 0A */
/* 송수신되는 데이터의 현황을 기록하는 레지스터 */
UCSR0A = 0x00;
/* Status Register 0B */
/* RX/TX Enable = 10011000 */
UCSR0B = 0x98;
/* Status Register 0C */
/* No parity, 8bit = 0110 */
UCSR0C = 0x06;
/* 중요 : Baud Rate 설정 */
/* BPS = 115200 */
UBRR0H = 0;
UBRR0L = 8;
}
void mongii_Puts(char *data)
{
int i;
for(i=0; i<strlen(data); i++)
{
while((UCSR0A & 0x20) == 0);
/* 0x20 -> 0b00100000 으로 송신버퍼가 비어있을 경우를 의미 */
UDR0 = data[i];
}
}
void darwin_Puts(char *data)
{
int i;
for(i=0; i<strlen(data); i++)
{
while((UCSR0A & 0x20) == 0);
UDR0 = data[i];
}
}
void JACK2_FALL(){
PORTD = 0xff;
_delay_ms(1);
PORTD = 0x00;
_delay_ms(1);
}
void JACK2_RISE(){
PORTD = 0xff;
_delay_ms(10);
PORTD = 0x00;
_delay_ms(10);
}
void JACK2_SEND(int sig){
PORTC = sig;
JACK2_RISE();
}
char darwin_buf[8];
int index;
void JACK2_RECV(){
char data;
char str[20]={0};
JACK2_FALL();
if (PINB & 1) darwin_buf[index++] = 1;
else darwin_buf[index++] = 0;
if (index == 8) {
index = 0;
data = 0;
data += 1 * darwin_buf[7];
data += 2 * darwin_buf[6];
data += 4 * darwin_buf[5];
data += 8 * darwin_buf[4];
data += 16 * darwin_buf[3];
data += 32 * darwin_buf[2];
data += 64 * darwin_buf[1];
data += 128 * darwin_buf[0];
sprintf(str, "%02X", data);
mongii_Puts(str);
}
//if (PINB & 0x01) mongii_Puts("1");
//else mongii_Puts("0");
//_delay_ms(10);
}
void JACK2_ID_CHK(){
//0x90 Read Manufacturer / Device ID (90h)
JACK2_SEND(1);
JACK2_SEND(0);
JACK2_SEND(0);
JACK2_SEND(1);
JACK2_SEND(0);
JACK2_SEND(0);
JACK2_SEND(0);
JACK2_SEND(0);
//CLK
for(int i = 0; i<24; i++){
JACK2_SEND(0);
}
}
void JACK2_RD(){
//0x03
JACK2_SEND(0);
JACK2_SEND(0);
JACK2_SEND(0);
JACK2_SEND(0);
JACK2_SEND(0);
JACK2_SEND(0);
JACK2_SEND(1);
JACK2_SEND(1);
//CLK
for(int i = 0; i<24; i++){
JACK2_SEND(0);
}
}
int main(void)
{
DDRA = 0xff;
DDRB = 0x00; // out -> 우리는 받음
DDRC = 0xff; // in <- 우리는 출력
DDRD = 0xff; // clock
PORTD = 0x00;
// /HOLD VCC GND /WP /CS
PORTA = 0b11011000;
PORTD = 0x00;
_delay_ms(10);
UART_INIT();
mongii_Puts("ID CHECKED : ");
//mongii_Puts("*\r\n");
//PORTA = 0b11010000;
PORTA = 0b11010000;
_delay_ms(10);
//SEND 0x90 0b10010000 ID CHK
JACK2_ID_CHK();
// H/W 에서 응답이 타이밍 표 기준으로 보낸 후 받기 시작할 때 엇갈려서 한 템포 내줘야 하닌 이슈
if (PINB & 0x01) darwin_buf[index++] = 1;//mongii_Puts("1");
else darwin_buf[index++] = 0;//mongii_Puts("0");
//PRINT
for (int cnt=0; cnt<15; cnt++){
JACK2_RECV();
/**
PORTD = 0x01;
_delay_ms(10);
PORTD = 0x00;
_delay_ms(10);
if (PINB & 0x01) mongii_Puts("1");
else mongii_Puts("0");
**/
}
mongii_Puts("\r\n");
//CS OFF
PORTA = 0b11011000;
_delay_ms(10);
//CS ON
PORTA = 0b11010000;
_delay_ms(10);
//SEND 0x03 0b00000011 READ DATA
JACK2_RD();
if (PINB & 0x01) darwin_buf[index++] = 1;//mongii_Puts("1");
else darwin_buf[index++] = 0;//mongii_Puts("0");
//UART PRINT
for (;;){
JACK2_RECV();
}
while(1)
{
_delay_ms(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment