Initial commit
[csrprogrammer:csrremote.git] / devicemanager.cpp
1 #include "devicemanager.h"
2 #include "stopwatch.h"
3 #include "programmer.h"
4 #include "bc_def.h"
5 #include <cstring>
6
7 DeviceManager::DeviceManager(Programmer *prog) : programmer(prog)
8 {
9
10 }
11
12 bool DeviceManager::IsSupported()
13 {
14     uint16_t id;
15     if(!programmer->Read(GBL_CHIP_VERSION, &id)) return false;
16     return id == 0x4826;
17 }
18
19 bool DeviceManager::XapResetAndGo()
20 {
21     if(!IsSupported()) return false;
22
23     int reset_count = 0;
24     StopWatch timer;
25     uint16_t tmp;
26
27     programmer->SetTransferSpeed(20);
28     programmer->Write(SPI_EMU_CMD, SPI_EMU_CMD_XAP_RUN_B_MASK);
29     timer.start();
30     while (timer.elapsedmsec() < 16 )
31       programmer->Write(SPI_EMU_CMD, SPI_EMU_CMD_XAP_RUN_B_MASK);
32
33     programmer->SetTransferSpeed(1000);
34     for(int i=0;i<100;i++)
35     {
36       if ( !programmer->Read(MMU_FLASH_BANK_SELECT, &tmp) )
37       {
38         if ( tmp >= 0x200 )
39           programmer->Write(MMU_FLASH_BANK_SELECT, 0);
40         if(++reset_count >= 10)
41             break;
42       }
43     }
44
45     /* write instructions - endless loop and execute it */
46     uint32_t xx = 0x540000;
47     uint32_t address = 0xa000 + xx - 0x140000;
48     programmer->Write(address,   0x0000); //nop
49     programmer->Write(address+1, 0x0000); //nop
50     programmer->Write(address+2, 0x00E0); //x: bra x
51     programmer->Write(XAP_PCH, xx >> 16);
52     programmer->Write(XAP_PCL, xx);
53
54     programmer->Read(ANA_CONFIG2, &tmp);
55     tmp &= 0xEFFF;
56     programmer->Write(ANA_CONFIG2, tmp);
57     programmer->Write(GBL_CLK_RATE, 0);
58     programmer->Read(XAP_FLAGS, &tmp);
59     tmp &= 0x20;
60     programmer->Write(XAP_FLAGS, tmp);
61     programmer->Write(RSTGEN_WATCHDOG_DELAY, 2);
62     programmer->Write(GBL_RST_ENABLES, RST_WATCHDOG_EN_MASK |
63                                        RST_FULL_CHIP_RESET);
64     programmer->Write(RSTGEN_WATCHDOG_KICK, 1);
65     programmer->Write(SPI_EMU_CMD, 0);
66
67     return true;
68 }
69
70 bool DeviceManager::XapResetAndStop()
71 {
72     if(!IsSupported()) return false;
73
74     int status;
75     uint16_t tmp;
76     StopWatch timer;
77
78     XapResetAndGo();
79     timer.start();
80     do
81     {
82         programmer->Read(GBL_RST_ENABLES, &tmp);
83         if ( tmp )
84             status = 0;
85         else
86             ++status;
87     }
88     while ( status < 2 && timer.elapsedmsec() < 8 );
89
90     if(status < 2)
91         return false;
92
93     return XapStop();
94 }
95
96 bool DeviceManager::XapGo()
97 {
98     if(!IsSupported()) return false;
99
100     programmer->Write(SPI_EMU_CMD,SPI_EMU_CMD_XAP_RUN_B_MASK);
101     programmer->Write(SPI_EMU_CMD,SPI_EMU_CMD_XAP_STEP_MASK |
102                                   SPI_EMU_CMD_XAP_RUN_B_MASK);
103     programmer->Write(SPI_EMU_CMD,SPI_EMU_CMD_XAP_RUN_B_MASK);
104     programmer->Write(SPI_EMU_CMD,SPI_EMU_CMD_XAP_STEP_MASK);
105
106     return true;
107 }
108
109 bool DeviceManager::XapStop()
110 {
111     if(!IsSupported()) return false;
112
113     uint16_t data[0x6B];//,tmp;
114
115     for (int i = 0; i < 20; ++i )
116         programmer->Write(SPI_EMU_CMD, SPI_EMU_CMD_XAP_RUN_B_MASK);
117
118     memset(data, 0, sizeof(data));
119
120     programmer->WriteBlock(0, 0x6B, data);
121     programmer->Write(SPI_EMU_CMD, SPI_EMU_CMD_XAP_RUN_B_MASK);
122     programmer->Write(SPI_EMU_CMD, SPI_EMU_CMD_XAP_RUN_B_MASK);
123
124     /* reset all register to their default values */
125     programmer->WriteBlock(XAP_AH, 16, data);
126     programmer->Write(XAP_BRK_REGH, 0xff);
127     programmer->Write(XAP_BRK_REGL, 0xffff);
128
129     return true;
130 }