issp_routines.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. // filename: ISSP_Routines.c
  2. #include "issp_revision.h"
  3. #ifdef PROJECT_REV_304
  4. /* Copyright 2006-2007, Cypress Semiconductor Corporation.
  5. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  6. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  7. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  9. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
  10. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  11. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  12. OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  13. BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  14. LIABILITY, WHETHER IN CONRTACT, STRICT LIABILITY, OR TORT (INCLUDING
  15. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  16. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17. Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND,EXPRESS OR IMPLIED,
  18. WITH REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. Cypress reserves the right to make changes without further notice to the
  21. materials described herein. Cypress does not assume any liability arising
  22. out of the application or use of any product or circuit described herein.
  23. Cypress does not authorize its products for use as critical components in
  24. life-support systems where a malfunction or failure may reasonably be
  25. expected to result in significant injury to the user. The inclusion of
  26. Cypressï¿?product in a life-support systems application implies that the
  27. manufacturer assumes all risk of such use and in doing so indemnifies
  28. Cypress against all charges.
  29. Use may be limited by and subject to the applicable Cypress software
  30. license agreement.
  31. --------------------------------------------------------------------------*/
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/fs.h>
  35. #include <linux/i2c.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/irq.h>
  38. #include <linux/sched.h>
  39. #include <linux/pm.h>
  40. #include <linux/sysctl.h>
  41. #include <linux/proc_fs.h>
  42. #include <linux/delay.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/input.h>
  45. //#include <mach/regs-gpio.h>
  46. //#include <plat/gpio-cfg.h>
  47. #include <linux/gpio.h>
  48. #include <linux/miscdevice.h>
  49. #include <asm/uaccess.h>
  50. #ifdef CONFIG_HAS_EARLYSUSPEND
  51. #include <linux/earlysuspend.h>
  52. #endif
  53. #include <asm/io.h>
  54. #include <linux/hrtimer.h>
  55. //mhsong #include <m8c.h> // part specific constants and macros
  56. //mhsong #include "PSoCAPI.h" // PSoC API definitions for all User Modules
  57. #include "issp_defs.h"
  58. #include "issp_vectors.h"
  59. #include "issp_extern.h"
  60. #include "issp_errors.h"
  61. #include "issp_directives.h"
  62. #include "issp_delays.h"
  63. unsigned char bTargetDataIN;
  64. unsigned char abTargetDataOUT[TARGET_DATABUFF_LEN];
  65. unsigned char bTargetAddress;
  66. unsigned char bTargetDataPtr = 0;
  67. unsigned char bTargetID[10];
  68. unsigned char bTargetStatus[10]; //PTJ: created to support READ-STATUS in fReadStatus()
  69. unsigned char fIsError;
  70. /* ((((((((((((((((((((( LOW-LEVEL ISSP SUBROUTINE SECTION ))))))))))))))))))))
  71. (( The subroutines in this section use functions from the C file ))
  72. (( ISSP_Drive_Routines.c. The functions in that file interface to the ))
  73. (( processor specific hardware. So, these functions should work as is, if ))
  74. (( the routines in ISSP_Drive_Routines.c are correctly converted. ))
  75. (((((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))))*/
  76. // ============================================================================
  77. // RunClock()
  78. // Description:
  79. // Run Clock without sending/receiving bits. Use this when transitioning from
  80. // write to read and read to write "num_cycles" is number of SCLK cycles, not
  81. // number of counter cycles.
  82. //
  83. // SCLK cannot run faster than the specified maximum frequency of 8MHz. Some
  84. // processors may need to have delays added after setting SCLK low and setting
  85. // SCLK high in order to not exceed this specification. The maximum frequency
  86. // of SCLK should be measured as part of validation of the final program
  87. //
  88. // ============================================================================
  89. void RunClock(unsigned int iNumCycles)
  90. {
  91. int i;
  92. for (i = 0; i < iNumCycles; i++) {
  93. SCLKLow();
  94. SCLKHigh();
  95. }
  96. // function exits with CLK high.
  97. }
  98. // ============================================================================
  99. // bReceiveBit()
  100. // Clocks the SCLK pin (high-low-high) and reads the status of the SDATA pin
  101. // after the rising edge.
  102. //
  103. // SCLK cannot run faster than the specified maximum frequency of 8MHz. Some
  104. // processors may need to have delays added after setting SCLK low and setting
  105. // SCLK high in order to not exceed this specification. The maximum frequency
  106. // of SCLK should be measured as part of validation of the final program
  107. //
  108. // Returns:
  109. // 0 if SDATA was low
  110. // 1 if SDATA was high
  111. // ============================================================================
  112. unsigned char bReceiveBit(void)
  113. {
  114. SCLKLow();
  115. SCLKHigh();
  116. if (fSDATACheck()) {
  117. return (1);
  118. } else {
  119. return (0);
  120. }
  121. }
  122. // ============================================================================
  123. // bReceiveByte()
  124. // Calls ReceiveBit 8 times to receive one byte.
  125. // Returns:
  126. // The 8-bit values recieved.
  127. // ============================================================================
  128. unsigned char bReceiveByte(void)
  129. {
  130. unsigned char b;
  131. unsigned char bCurrByte = 0x00;
  132. for (b = 0; b < 8; b++) {
  133. bCurrByte = (bCurrByte << 1) + bReceiveBit();
  134. }
  135. return (bCurrByte);
  136. }
  137. // ============================================================================
  138. // SendByte()
  139. // This routine sends up to one byte of a vector, one bit at a time.
  140. // bCurrByte the byte that contains the bits to be sent.
  141. // bSize the number of bits to be sent. Valid values are 1 to 8.
  142. //
  143. // SCLK cannot run faster than the specified maximum frequency of 8MHz. Some
  144. // processors may need to have delays added after setting SCLK low and setting
  145. // SCLK high in order to not exceed this specification. The maximum frequency
  146. // of SCLK should be measured as part of validation of the final program
  147. //
  148. // There is no returned value.
  149. // ============================================================================
  150. void SendByte(unsigned char bCurrByte, unsigned char bSize)
  151. {
  152. unsigned char b = 0;
  153. for (b = 0; b < bSize; b++) {
  154. if (bCurrByte & 0x80) {
  155. // Send a '1'
  156. SetSDATAHigh();
  157. SCLKHigh();
  158. SCLKLow();
  159. } else {
  160. // Send a '0'
  161. SetSDATALow();
  162. SCLKHigh();
  163. SCLKLow();
  164. }
  165. bCurrByte = bCurrByte << 1;
  166. }
  167. }
  168. // ============================================================================
  169. // SendVector()
  170. // This routine sends the vector specifed. All vectors constant strings found
  171. // in ISSP_Vectors.h. The data line is returned to HiZ after the vector is
  172. // sent.
  173. // bVect a pointer to the vector to be sent.
  174. // nNumBits the number of bits to be sent.
  175. // bCurrByte scratch var to keep the byte to be sent.
  176. //
  177. // There is no returned value.
  178. // ============================================================================
  179. void SendVector(const unsigned char *bVect, unsigned int iNumBits)
  180. {
  181. SetSDATAStrong();
  182. while (iNumBits > 0) {
  183. if (iNumBits >= 8) {
  184. SendByte(*(bVect), 8);
  185. iNumBits -= 8;
  186. bVect++;
  187. } else {
  188. SendByte(*(bVect), iNumBits);
  189. iNumBits = 0;
  190. }
  191. }
  192. SetSDATALow(); // issp_test_20100709 add
  193. SetSDATAHiZ();
  194. }
  195. // ============================================================================
  196. // fDetectHiLoTransition()
  197. // Waits for transition from SDATA = 1 to SDATA = 0. Has a 100 msec timeout.
  198. // TRANSITION_TIMEOUT is a loop counter for a 100msec timeout when waiting for
  199. // a high-to-low transition. This is used in the polling loop of
  200. // fDetectHiLoTransition(). The timing of the while(1) loops can be calculated
  201. // and the number of loops is counted, using iTimer, to determine when 100
  202. // msec has passed.
  203. //
  204. //// SCLK cannot run faster than the specified maximum frequency of 8MHz. Some
  205. // processors may need to have delays added after setting SCLK low and setting
  206. // SCLK high in order to not exceed this specification. The maximum frequency
  207. // of SCLK should be measured as part of validation of the final program
  208. //
  209. // Returns:
  210. // 0 if successful
  211. // -1 if timed out.
  212. // ============================================================================
  213. signed char fDetectHiLoTransition(void)
  214. {
  215. // nTimer breaks out of the while loops if the wait in the two loops totals
  216. // more than 100 msec. Making this static makes the loop run a faster.
  217. // This is really a processor/compiler dependency and it not needed.
  218. static unsigned int iTimer;
  219. // NOTE:
  220. // These loops look unconventional, but it is necessary to check SDATA_PIN
  221. // as shown because the transition can be missed otherwise, due to the
  222. // length of the SDATA Low-High-Low after certain commands.
  223. /*printk(KERN_DEBUG"touchkey:%s\n", __func__);*/
  224. // Generate clocks for the target to pull SDATA High
  225. //dog_kick();
  226. iTimer = TRANSITION_TIMEOUT;
  227. /*printk(KERN_INFO
  228. "Generate clocks for the target to pull SDATA High\n");*/
  229. while (1) {
  230. SCLKLow();
  231. if (fSDATACheck()) // exit once SDATA goes HI
  232. break;
  233. SCLKHigh();
  234. // If the wait is too long then timeout
  235. if (iTimer-- == 0) {
  236. return (ERROR);
  237. }
  238. }
  239. /*printk(KERN_DEBUG"touchkey:%s second\n", __func__);*/
  240. //dog_kick();
  241. // Generate Clocks and wait for Target to pull SDATA Low again
  242. iTimer = TRANSITION_TIMEOUT; // reset the timeout counter
  243. /*printk(KERN_INFO
  244. "Generate Clocks and wait for Target to pull SDATA Low again\n");*/
  245. while (1) {
  246. SCLKLow(); //issp_test_20100709 unblock
  247. if (!fSDATACheck()) { // exit once SDATA returns LOW
  248. break;
  249. }
  250. SCLKHigh(); //issp_test_20100709 unblock
  251. // If the wait is too long then timeout
  252. if (iTimer-- == 0) {
  253. return (ERROR);
  254. }
  255. }
  256. //printk("fDetectHiLoTransition OUT!!!!\n");
  257. return (PASS);
  258. }
  259. /* ((((((((((((((((((((( HIGH-LEVEL ISSP ROUTINE SECTION ))))))))))))))))))))))
  260. (( These functions are mostly made of calls to the low level routines ))
  261. (( above. This should isolate the processor-specific changes so that ))
  262. (( these routines do not need to be modified. ))
  263. (((((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))))*/
  264. #ifdef RESET_MODE
  265. // ============================================================================
  266. // fXRESInitializeTargetForISSP()
  267. // Implements the intialization vectors for the device.
  268. // Returns:
  269. // 0 if successful
  270. // INIT_ERROR if timed out on handshake to the device.
  271. // ============================================================================
  272. signed char fXRESInitializeTargetForISSP(void)
  273. {
  274. // Configure the pins for initialization
  275. SetSDATAHiZ();
  276. SetSCLKStrong();
  277. SCLKLow();
  278. // Cycle reset and put the device in programming mode when it exits reset
  279. AssertXRES();
  280. usleep_range(1000, 1000);
  281. DeassertXRES();
  282. // !!! NOTE:
  283. // The timing spec that requires that the first Init-Vector happen within
  284. // 1 msec after the reset/power up. For this reason, it is not advisable
  285. // to separate the above RESET_MODE or POWER_CYCLE_MODE code from the
  286. // Init-Vector instructions below. Doing so could introduce excess delay
  287. // and cause the target device to exit ISSP Mode.
  288. //PTJ: Send id_setup_1 instead of init1_v
  289. //PTJ: both send CA Test Key and do a Calibrate1 SROM function
  290. SendVector(id_setup_1, num_bits_id_setup_1);
  291. fIsError = fDetectHiLoTransition();
  292. if (fIsError) {
  293. // TX8SW_CPutString("\r\n fDetectHiLoTransition Error");
  294. printk("\r\n fDetectHiLoTransition Error\n");
  295. return (INIT_ERROR);
  296. }
  297. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  298. // NOTE: DO NOT not wait for HiLo on SDATA after vector Init-3
  299. // it does not occur (per spec).
  300. return (PASS);
  301. }
  302. #else //else = the part is power cycle programmed
  303. // ============================================================================
  304. // fPowerCycleInitializeTargetForISSP()
  305. // Implements the intialization vectors for the device.
  306. // The first time fDetectHiLoTransition is called the Clk pin is highZ because
  307. // the clock is not needed during acquire.
  308. // Returns:
  309. // 0 if successful
  310. // INIT_ERROR if timed out on handshake to the device.
  311. // ============================================================================
  312. signed char fPowerCycleInitializeTargetForISSP(void)
  313. {
  314. // unsigned char n;
  315. // Set all pins to highZ to avoid back powering the PSoC through the GPIO
  316. // protection diodes.
  317. SetSCLKHiZ();
  318. SetSDATAHiZ();
  319. // Turn on power to the target device before other signals
  320. SetTargetVDDStrong();
  321. ApplyTargetVDD();
  322. /* wait 7msec for the power to stabilize */
  323. /*mdelay(7);*/
  324. mdelay(1); // jjlee
  325. #if 0
  326. for (n = 0; n < 10; n++) {
  327. Delay(DELAY100us);
  328. }
  329. #endif
  330. // Set SCLK to high Z so there is no clock and wait for a high to low
  331. // transition on SDAT. SCLK is not needed this time.
  332. SetSCLKHiZ();
  333. // printk(KERN_DEBUG "fDetectHiLoTransition\n");
  334. if ((fIsError = fDetectHiLoTransition())) {
  335. return (INIT_ERROR);
  336. }
  337. // Configure the pins for initialization
  338. // SetSDATAHiZ(); // issp_test_20100709 block
  339. SetSCLKStrong();
  340. SCLKLow(); //PTJ: DO NOT SET A BREAKPOINT HERE AND EXPECT SILICON ID TO PASS!
  341. // !!! NOTE:
  342. // The timing spec that requires that the first Init-Vector happen within
  343. // 1 msec after the reset/power up. For this reason, it is not advisable
  344. // to separate the above RESET_MODE or POWER_CYCLE_MODE code from the
  345. // Init-Vector instructions below. Doing so could introduce excess delay
  346. // and cause the target device to exit ISSP Mode.
  347. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end); //PTJ: rev308, added to match spec
  348. // printk("SendVector(id_setup_1)\n",0,0,0);
  349. SendVector(id_setup_1, num_bits_id_setup_1);
  350. if ((fIsError = fDetectHiLoTransition())) {
  351. return (INIT_ERROR);
  352. }
  353. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  354. // NOTE: DO NOT not wait for HiLo on SDATA after vector Init-3
  355. // it does not occur (per spec).
  356. return (PASS);
  357. }
  358. #endif
  359. // ============================================================================
  360. // fVerifySiliconID()
  361. // Returns:
  362. // 0 if successful
  363. // Si_ID_ERROR if timed out on handshake to the device.
  364. // ============================================================================
  365. signed char fVerifySiliconID(void)
  366. {
  367. SendVector(id_setup_2, num_bits_id_setup_2);
  368. //printk("fVerifySiliconID: SendVector id_stup2 END\n");
  369. if ((fIsError = fDetectHiLoTransition())) {
  370. printk("fVerifySiliconID(): fDetectHiLoTransition Error\n");
  371. return (SiID_ERROR);
  372. }
  373. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  374. SendVector(tsync_enable, num_bits_tsync_enable);
  375. //printk("fVerifySiliconID: SendVector(wait_and_poll_end) (tsync_enable) END\n");
  376. //Send Read ID vector and get Target ID
  377. SendVector(read_id_v, 11); // Read-MSB Vector is the first 11-Bits
  378. RunClock(2); // Two SCLK cycles between write & read
  379. bTargetID[0] = bReceiveByte();
  380. RunClock(1);
  381. SendVector(read_id_v + 2, 12); // 1+11 bits starting from the 3rd byte
  382. RunClock(2); // Read-LSB Command
  383. bTargetID[1] = bReceiveByte();
  384. RunClock(1);
  385. SendVector(read_id_v + 4, 1); // 1 bit starting from the 5th byte
  386. //read Revision ID from Accumulator A and Accumulator X
  387. SendVector(read_id_v + 5, 11); //11 bits starting from the 6th byte
  388. RunClock(2);
  389. bTargetID[2] = bReceiveByte(); //Read from Acc.X
  390. RunClock(1);
  391. SendVector(read_id_v + 7, 12); //1+11 bits starting from the 8th byte
  392. RunClock(2);
  393. bTargetID[3] = bReceiveByte(); //Read from Acc.A
  394. RunClock(1);
  395. SendVector(read_id_v + 4, 1); //1bit starting from the 5th byte,
  396. SendVector(tsync_disable, num_bits_tsync_disable);
  397. // Print READ-ID
  398. /*
  399. TX8SW_CPutString("\r\n Silicon-ID : ");
  400. TX8SW_PutChar(' ');
  401. TX8SW_PutSHexByte(bTargetID[0]);
  402. TX8SW_PutChar(' ');
  403. TX8SW_PutSHexByte(bTargetID[1]);
  404. TX8SW_PutChar(' ');
  405. TX8SW_PutSHexByte(bTargetID[2]);
  406. TX8SW_PutChar(' ');
  407. TX8SW_PutSHexByte(bTargetID[3]);
  408. TX8SW_PutChar(' ');
  409. */
  410. #if 0 // issp_test_20100709 block
  411. printk("issp_routines.c: ID0:0x%X, ID1:0x%X, ID2: 0x%X, ID2: 0x%X\n",
  412. bTargetID[0], bTargetID[1], bTargetID[2], bTargetID[3]);
  413. if ((bTargetID[0] != target_id_v[0]) || (bTargetID[1] != target_id_v[1])
  414. || (bTargetID[2] != target_id_v[2])
  415. || (bTargetID[3] != target_id_v[3])) {
  416. return (SiID_ERROR);
  417. } else {
  418. return (PASS);
  419. }
  420. #else
  421. return (PASS);
  422. #endif
  423. }
  424. // PTJ: =======================================================================
  425. // fReadStatus()
  426. // Returns:
  427. // 0 if successful
  428. // _____ if timed out on handshake to the device.
  429. // ============================================================================
  430. signed char fReadStatus(void)
  431. {
  432. SendVector(tsync_enable, num_bits_tsync_enable); //PTJ:
  433. //Send Read ID vector and get Target ID
  434. SendVector(read_id_v, 11); // Read-MSB Vector is the first 11-Bits
  435. RunClock(2); // Two SCLK cycles between write & read
  436. bTargetStatus[0] = bReceiveByte();
  437. RunClock(1);
  438. //SendVector(read_id_v+2, 12); // 12 bits starting from the 3rd character
  439. //RunClock(2); // Read-LSB Command
  440. //bTargetStatus[1] = bReceiveByte();
  441. //RunClock(1);
  442. SendVector(read_id_v + 4, 1); // 1 bit starting from the 5th character
  443. SendVector(tsync_disable, num_bits_tsync_disable);
  444. if (bTargetStatus[0] == target_status00_v) {
  445. return (PASS); //PTJ: Status = 00 means Success, the SROM function did what it was supposed to
  446. }
  447. if (bTargetStatus[0] == target_status01_v) {
  448. return (STATUS_ERROR); //PTJ: Status = 01 means that function is not allowed because of block level protection, for test with verify_setup (VERIFY-SETUP)
  449. }
  450. if (bTargetStatus[0] == target_status03_v) {
  451. return (STATUS_ERROR); //PTJ: Status = 03 is fatal error, SROM halted
  452. }
  453. if (bTargetStatus[0] == target_status04_v) {
  454. return (STATUS_ERROR); //PTJ: Status = 04 means there was a checksum faliure with either the smart write code checksum, or the smart write paramters checksum, for test with PROGRAM-AND-VERIFY
  455. }
  456. if (bTargetStatus[0] == target_status06_v) {
  457. return (STATUS_ERROR); //PTJ: Status = 06 means that Calibrate1 failed, for test with id_setup_1 (ID-SETUP-1)
  458. } else {
  459. return (STATUS_ERROR);
  460. }
  461. }
  462. // PTJ: =======================================================================
  463. // fReadCalRegisters()
  464. // PTJ: use this to read some cal registers that should be loaded by Calibrate1 in id_setup_1
  465. // Returns:
  466. // 0 if successful
  467. // _____ if timed out on handshake to the device.
  468. // ============================================================================
  469. signed char fReadCalRegisters(void)
  470. {
  471. SendVector(tsync_enable, num_bits_tsync_enable);
  472. SendVector(Switch_Bank1, 22);
  473. SendVector(read_IMOtrim, 11); // Read-MSB Vector is the first 11-Bits
  474. RunClock(2); // Two SCLK cycles between write & read
  475. bTargetStatus[0] = bReceiveByte();
  476. RunClock(1);
  477. // Set SDATA to Strong Drive here because SendByte() does not
  478. SetSDATAStrong();
  479. SendByte(read_reg_end, 1);
  480. SendVector(read_SPCtrim, 11); // Read-MSB Vector is the first 11-Bits
  481. RunClock(2); // Two SCLK cycles between write & read
  482. bTargetStatus[1] = bReceiveByte();
  483. RunClock(1);
  484. // Set SDATA to Strong Drive here because SendByte() does not
  485. SetSDATAStrong();
  486. SendByte(read_reg_end, 1);
  487. SendVector(read_VBGfinetrim, 11); // Read-MSB Vector is the first 11-Bits
  488. RunClock(2); // Two SCLK cycles between write & read
  489. bTargetStatus[2] = bReceiveByte();
  490. RunClock(1);
  491. // Set SDATA to Strong Drive here because SendByte() does not
  492. SetSDATAStrong();
  493. SendByte(read_reg_end, 1);
  494. SendVector(Switch_Bank0, 22);
  495. SendVector(tsync_disable, num_bits_tsync_disable);
  496. if (bTargetStatus[0] == target_status00_v) {
  497. return (PASS); //PTJ: Status = 00 means Success, the SROM function did what it was supposed to
  498. }
  499. return PASS;
  500. }
  501. // PTJ: =======================================================================
  502. // fReadWriteSetup()
  503. // PTJ: The READ-WRITE-SETUP vector will enable TSYNC and switches the device
  504. // to SRAM bank1 for PROGRAM-AND-VERIFY, SECURE and VERIFY-SETUP.
  505. // Returns:
  506. // 0 if successful
  507. // _____ if timed out on handshake to the device.
  508. // ============================================================================
  509. signed char fReadWriteSetup(void)
  510. {
  511. SendVector(read_write_setup, num_bits_read_write_setup);
  512. return (PASS); //PTJ: is there anything else that should be done?
  513. }
  514. // PTJ: =======================================================================
  515. // fSyncEnable()
  516. // PTJ: The SYNC-ENABLE vector will enable TSYNC
  517. //
  518. // Returns:
  519. // 0 if successful
  520. // _____ if timed out on handshake to the device.
  521. // ============================================================================
  522. signed char fSyncEnable(void)
  523. {
  524. SendVector(tsync_enable, num_bits_tsync_enable); //PTJ: 307 for tsync enable testing
  525. return (PASS); //PTJ: is there anything else that should be done?
  526. }
  527. // PTJ: =======================================================================
  528. // fSyncDisable()
  529. // PTJ: The SYNC-ENABLE vector will enable TSYNC
  530. //
  531. // Returns:
  532. // 0 if successful
  533. // _____ if timed out on handshake to the device.
  534. // ============================================================================
  535. signed char fSyncDisable(void)
  536. {
  537. SendVector(tsync_disable, num_bits_tsync_disable); //PTJ: 307 for tsync enable testing
  538. return (PASS);
  539. }
  540. // ============================================================================
  541. // fEraseTarget()
  542. // Perform a bulk erase of the target device.
  543. // Returns:
  544. // 0 if successful
  545. // ERASE_ERROR if timed out on handshake to the device.
  546. // ============================================================================
  547. signed char fEraseTarget(void)
  548. {
  549. SendVector(erase, num_bits_erase);
  550. if ((fIsError = fDetectHiLoTransition())) {
  551. // TX8SW_CPutString("\r\n fDetectHiLoTransition");
  552. //printk("\r\n fDetectHiLoTransition\n"); // issp_test_2010 block
  553. return (ERASE_ERROR);
  554. }
  555. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  556. return (PASS);
  557. }
  558. extern unsigned int iBlockCounter;
  559. // ============================================================================
  560. // LoadTarget()
  561. // Transfers data from array in Host to RAM buffer in the target.
  562. // Returns the checksum of the data.
  563. // ============================================================================
  564. unsigned int iLoadTarget(void)
  565. {
  566. unsigned char bTemp;
  567. unsigned int iChecksumData = 0;
  568. // Set SDATA to Strong Drive here because SendByte() does not
  569. SetSDATAStrong();
  570. // Transfer the temporary RAM array into the target.
  571. // In this section, a 128-Byte array was specified by #define, so the entire
  572. // 128-Bytes are written in this loop.
  573. bTargetAddress = 0x00;
  574. bTargetDataPtr = 0x00;
  575. while (bTargetDataPtr < TARGET_DATABUFF_LEN) {
  576. bTemp = abTargetDataOUT[bTargetDataPtr];
  577. iChecksumData += bTemp;
  578. SendByte(write_byte_start, 4); //PTJ: we need to be able to write 128 bytes from address 0x80 to 0xFF
  579. SendByte(bTargetAddress, 7); //PTJ: we need to be able to write 128 bytes from address 0x80 to 0xFF
  580. SendByte(bTemp, 8);
  581. SendByte(write_byte_end, 3);
  582. // !!!NOTE:
  583. // SendByte() uses MSbits, so inc by '2' to put the 0..128 address into
  584. // the seven MSBit locations.
  585. //
  586. // This can be confusing, but check the logic:
  587. // The address is only 7-Bits long. The SendByte() subroutine will
  588. // send however-many bits, BUT...always reads them bits from left-to-
  589. // right. So in order to pass a value of 0..128 as the address using
  590. // SendByte(), we have to left justify the address by 1-Bit.
  591. // This can be done easily by incrementing the address each time by
  592. // '2' rather than by '1'.
  593. bTargetAddress += 2; //PTJ: inc by 2 in order to support a 128 byte address space, MSB~1 for address
  594. bTargetDataPtr++;
  595. }
  596. return (iChecksumData);
  597. }
  598. #ifdef MULTI_BANK
  599. // ============================================================================
  600. // SetBankNumber()
  601. // Set the bank number in the target device.
  602. // Returns:
  603. // none
  604. // ============================================================================
  605. void SetBankNumber(unsigned char bBankNumber)
  606. {
  607. // Send the bank-select vector.
  608. SendVector(set_bank_number, 33);
  609. // Set the drive here because SendByte() does not.
  610. SetSDATAStrong();
  611. SendByte(bBankNumber, 8);
  612. SendVector(set_bank_number_end, 25);
  613. }
  614. #endif
  615. // ============================================================================
  616. // fProgramTargetBlock()
  617. // Program one block with data that has been loaded into a RAM buffer in the
  618. // target device.
  619. // Returns:
  620. // 0 if successful
  621. // BLOCK_ERROR if timed out on handshake to the device.
  622. // ============================================================================
  623. signed char fProgramTargetBlock(unsigned char bBankNumber,
  624. unsigned char bBlockNumber)
  625. {
  626. SendVector(tsync_enable, num_bits_tsync_enable);
  627. SendVector(set_block_num, num_bits_set_block_num);
  628. // Set the drive here because SendByte() does not.
  629. SetSDATAStrong();
  630. SendByte(bBlockNumber, 8);
  631. SendByte(set_block_num_end, 3);
  632. SendVector(tsync_disable, num_bits_tsync_disable); //PTJ:
  633. // Send the program-block vector.
  634. SendVector(program_and_verify, num_bits_program_and_verify); //PTJ: PROGRAM-AND-VERIFY
  635. // wait for acknowledge from target.
  636. if ((fIsError = fDetectHiLoTransition())) {
  637. return (BLOCK_ERROR);
  638. }
  639. // Send the Wait-For-Poll-End vector
  640. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  641. return (PASS);
  642. //PTJ: Don't do READ-STATUS here because that will
  643. //PTJ: require that we return multiple error values, if error occurs
  644. }
  645. // ============================================================================
  646. // fAddTargetBankChecksum()
  647. // Reads and adds the target bank checksum to the referenced accumulator.
  648. // Returns:
  649. // 0 if successful
  650. // VERIFY_ERROR if timed out on handshake to the device.
  651. // ============================================================================
  652. signed char fAccTargetBankChecksum(unsigned int *pAcc)
  653. {
  654. unsigned int wCheckSumData;
  655. SendVector(checksum_setup, num_bits_checksum_setup); //PTJ:CHECKSUM-SETUP, it is taking 100ms > time > 200ms to complete the checksum
  656. if ((fIsError = fDetectHiLoTransition())) { //100ms is default
  657. return (VERIFY_ERROR);
  658. }
  659. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  660. SendVector(tsync_enable, num_bits_tsync_enable);
  661. //Send Read Checksum vector and get Target Checksum
  662. SendVector(read_checksum_v, 11); // first 11-bits is ReadCKSum-MSB
  663. RunClock(2); // Two SCLKs between write & read
  664. bTargetDataIN = bReceiveByte();
  665. wCheckSumData = bTargetDataIN << 8;
  666. RunClock(1); // See Fig. 6
  667. SendVector(read_checksum_v + 2, 12); // 12 bits starting from 3rd character
  668. RunClock(2); // Read-LSB Command
  669. bTargetDataIN = bReceiveByte();
  670. wCheckSumData |= (bTargetDataIN & 0xFF);
  671. RunClock(1);
  672. SendVector(read_checksum_v + 3, 1); // Send the final bit of the command //PTJ: read_checksum_v may have to change if TSYNC needs to be enabled
  673. SendVector(tsync_disable, num_bits_tsync_disable);
  674. *pAcc = wCheckSumData;
  675. return (PASS);
  676. }
  677. // ============================================================================
  678. // ReStartTarget()
  679. // After programming, the target PSoC must be reset to take it out of
  680. // programming mode. This routine performs a reset.
  681. // ============================================================================
  682. void ReStartTarget(void)
  683. {
  684. #ifdef RESET_MODE
  685. // Assert XRES, then release, then disable XRES-Enable
  686. AssertXRES();
  687. Delay(XRES_CLK_DELAY);
  688. DeassertXRES();
  689. #else
  690. // Set all pins to highZ to avoid back powering the PSoC through the GPIO
  691. // protection diodes.
  692. SetSCLKHiZ();
  693. SetSDATAHiZ();
  694. // Cycle power on the target to cause a reset
  695. RemoveTargetVDD();
  696. mdelay(300);
  697. ApplyTargetVDD();
  698. #endif
  699. }
  700. // ============================================================================
  701. // fVerifySetup()
  702. // Verify the block just written to. This can be done byte-by-byte before the
  703. // protection bits are set.
  704. // Returns:
  705. // 0 if successful
  706. // BLOCK_ERROR if timed out on handshake to the device.
  707. // ============================================================================
  708. signed char fVerifySetup(unsigned char bBankNumber, unsigned char bBlockNumber)
  709. {
  710. SendVector(tsync_enable, num_bits_tsync_enable);
  711. SendVector(set_block_num, num_bits_set_block_num);
  712. //Set the drive here because SendByte() does not
  713. SetSDATAStrong();
  714. SendByte(bBlockNumber, 8);
  715. SendByte(set_block_num_end, 3); //PTJ:
  716. SendVector(tsync_disable, num_bits_tsync_disable); //PTJ:
  717. SendVector(verify_setup, num_bits_my_verify_setup); //PTJ:
  718. if ((fIsError = fDetectHiLoTransition())) {
  719. return (BLOCK_ERROR);
  720. }
  721. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  722. return (PASS);
  723. }
  724. // ============================================================================
  725. // fReadByteLoop()
  726. // Reads the data back from Target SRAM and compares it to expected data in
  727. // Host SRAM
  728. // Returns:
  729. // 0 if successful
  730. // BLOCK_ERROR if timed out on handshake to the device.
  731. // ============================================================================
  732. signed char fReadByteLoop(void)
  733. {
  734. bTargetAddress = 0;
  735. bTargetDataPtr = 0;
  736. while (bTargetDataPtr < TARGET_DATABUFF_LEN) {
  737. //Send Read Byte vector and then get a byte from Target
  738. SendVector(read_byte_v, 4);
  739. // Set the drive here because SendByte() does not
  740. SetSDATAStrong();
  741. SendByte(bTargetAddress, 7);
  742. RunClock(2); // Run two SCLK cycles between writing and reading
  743. SetSDATAHiZ(); // Set to HiZ so Target can drive SDATA
  744. bTargetDataIN = bReceiveByte();
  745. RunClock(1);
  746. SendVector(read_byte_v + 1, 1); // Send the ReadByte Vector End
  747. // Test the Byte that was read from the Target against the original
  748. // value (already in the 128-Byte array "abTargetDataOUT[]"). If it
  749. // matches, then bump the address & pointer,loop-back and continue.
  750. // If it does NOT match abort the loop and return and error.
  751. if (bTargetDataIN != abTargetDataOUT[bTargetDataPtr]) {
  752. #ifdef TX_ON
  753. UART_PutCRLF();
  754. UART_CPutString("bTargetDataIN : ");
  755. UART_PutHexByte(bTargetDataIN);
  756. UART_CPutString(" abTargetDataOUT : ");
  757. UART_PutHexByte(abTargetDataOUT[bTargetDataPtr]);
  758. #endif
  759. return (BLOCK_ERROR);
  760. }
  761. bTargetDataPtr++;
  762. // Increment the address by 2 to accomodate 7-Bit addressing
  763. // (puts the 7-bit address into MSBit locations for "SendByte()").
  764. bTargetAddress += 2;
  765. }
  766. return (PASS);
  767. }
  768. // ============================================================================
  769. // fVerifyTargetBlock()
  770. // Verify the block just written to. This can be done byte-by-byte before the
  771. // protection bits are set.
  772. // Returns:
  773. // 0 if successful
  774. // BLOCK_ERROR if timed out on handshake to the device.
  775. // ============================================================================
  776. signed char fVerifyTargetBlock(unsigned char bBankNumber,
  777. unsigned char bBlockNumber)
  778. {
  779. SendVector(set_block_number, 11);
  780. //Set the drive here because SendByte() does not
  781. SetSDATAStrong();
  782. SendByte(bBlockNumber, 8);
  783. SendByte(set_block_number_end, 3);
  784. SendVector(verify_setup_v, num_bits_verify_setup);
  785. if ((fIsError = fDetectHiLoTransition())) {
  786. return (BLOCK_ERROR);
  787. }
  788. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  789. bTargetAddress = 0;
  790. bTargetDataPtr = 0;
  791. while (bTargetDataPtr < TARGET_DATABUFF_LEN) {
  792. //Send Read Byte vector and then get a byte from Target
  793. SendVector(read_byte_v, 4); //PTJ 308: this was changed from sending the first 5 bits to sending the first 4
  794. // Set the drive here because SendByte() does not
  795. SetSDATAStrong();
  796. SendByte(bTargetAddress, 6);
  797. RunClock(2); // Run two SCLK cycles between writing and reading
  798. SetSDATAHiZ(); // Set to HiZ so Target can drive SDATA
  799. bTargetDataIN = bReceiveByte();
  800. RunClock(1);
  801. SendVector(read_byte_v + 1, 1); // Send the ReadByte Vector End
  802. // Test the Byte that was read from the Target against the original
  803. // value (already in the 128-Byte array "abTargetDataOUT[]"). If it
  804. // matches, then bump the address & pointer,loop-back and continue.
  805. // If it does NOT match abort the loop and return an error.
  806. if (bTargetDataIN != abTargetDataOUT[bTargetDataPtr])
  807. return (BLOCK_ERROR);
  808. bTargetDataPtr++;
  809. // Increment the address by four to accomodate 6-Bit addressing
  810. // (puts the 6-bit address into MSBit locations for "SendByte()").
  811. bTargetAddress += 4;
  812. }
  813. return (PASS);
  814. }
  815. // ============================================================================
  816. // fSecureTargetFlash()
  817. // Before calling, load the array, abTargetDataOUT, with the desired security
  818. // settings using LoadArrayWithSecurityData(StartAddress,Length,SecurityType).
  819. // The can be called multiple times with different SecurityTypes as needed for
  820. // particular Flash Blocks. Or set them all the same using the call below:
  821. // LoadArrayWithSecurityData(0,SECURITY_BYTES_PER_BANK, 0);
  822. // Returns:
  823. // 0 if successful
  824. // SECURITY_ERROR if timed out on handshake to the device.
  825. // ============================================================================
  826. signed char fSecureTargetFlash(void)
  827. {
  828. unsigned char bTemp;
  829. // Transfer the temporary RAM array into the target
  830. bTargetAddress = 0x00;
  831. bTargetDataPtr = 0x00;
  832. SetSDATAStrong();
  833. while (bTargetDataPtr < SECURITY_BYTES_PER_BANK) {
  834. bTemp = abTargetDataOUT[bTargetDataPtr];
  835. SendByte(write_byte_start, 4);
  836. SendByte(bTargetAddress, 7);
  837. SendByte(bTemp, 8);
  838. SendByte(write_byte_end, 3);
  839. // SendBytes() uses MSBits, so increment the address by '2' to put
  840. // the 0..n address into the seven MSBit locations
  841. bTargetAddress += 2; //PTJ: inc by 2 in order to support a 128 byte address space
  842. bTargetDataPtr++;
  843. }
  844. SendVector(secure, num_bits_secure); //PTJ:
  845. if ((fIsError = fDetectHiLoTransition())) {
  846. return (SECURITY_ERROR);
  847. }
  848. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  849. return (PASS);
  850. }
  851. // ============================================================================
  852. // PTJ: fReadSecurity()
  853. // This reads from SM0 with Read Supervisory SPC command.
  854. // Need to have SPC Test Mode enabled before using these commands?
  855. // Returns:
  856. // 0 if successful
  857. // __________ if timed out on handshake to the device.
  858. // ============================================================================
  859. signed char fReadSecurity(void)
  860. {
  861. SendVector(ReadSecuritySetup, num_bits_ReadSecuritySetup);
  862. // SendVector(SPCTestMode_enable, num_bits_SPCTestMode_enable);
  863. bTargetAddress = 0x00;
  864. while (bTargetAddress < (SECURITY_BYTES_PER_BANK * 2)) { //PTJ: we do SECURITY_BYTES_PER_BANK * 2 because we bTargetAddress += 2
  865. //PTJ: TSYNC Enable
  866. SendVector(tsync_enable, num_bits_tsync_enable);
  867. SendVector(read_security_pt1, num_bits_read_security_pt1); //PTJ:
  868. // Set the drive here because SendByte() does not.
  869. SetSDATAStrong();
  870. SendByte(bTargetAddress, 7); //PTJ: hardcode MSb of address as 0 in bit stream
  871. SendVector(read_security_pt1_end,
  872. num_bits_read_security_pt1_end);
  873. //PTJ: TSYNC Disable
  874. SendVector(tsync_disable, num_bits_tsync_disable);
  875. SendVector(read_security_pt2, num_bits_read_security_pt2);
  876. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  877. SendVector(read_security_pt3, num_bits_read_security_pt3);
  878. SetSDATAStrong();
  879. SendByte(bTargetAddress, 7);
  880. SendVector(read_security_pt3_end,
  881. num_bits_read_security_pt3_end);
  882. SendVector(wait_and_poll_end, num_bits_wait_and_poll_end);
  883. bTargetAddress += 2;
  884. }
  885. bTargetAddress = 0x00;
  886. bTargetDataPtr = 0x00;
  887. SendVector(tsync_enable, num_bits_tsync_enable); //PTJ: 307, added for tsync testing
  888. while (bTargetAddress < (SECURITY_BYTES_PER_BANK * 2)) { //PTJ: we do SECURITY_BYTES_PER_BANK * 2 because we bTargetAddress += 2
  889. //Send Read Byte vector and then get a byte from Target
  890. SendVector(read_byte_v, 4);
  891. // Set the drive here because SendByte() does not
  892. SetSDATAStrong();
  893. SendByte(bTargetAddress, 7);
  894. RunClock(2); // Run two SCLK cycles between writing and reading
  895. SetSDATAHiZ(); // Set to HiZ so Target can drive SDATA
  896. bTargetDataIN = bReceiveByte();
  897. RunClock(1);
  898. SendVector(read_byte_v + 1, 1); // Send the ReadByte Vector End
  899. // Test the Byte that was read from the Target against the original
  900. // value (already in the 128-Byte array "abTargetDataOUT[]"). If it
  901. // matches, then bump the address & pointer,loop-back and continue.
  902. // If it does NOT match abort the loop and return and error.
  903. if (bTargetDataIN != abTargetDataOUT[bTargetDataPtr])
  904. // return(BLOCK_ERROR);
  905. // Increment the address by two to accomodate 7-Bit addressing
  906. // (puts the 7-bit address into MSBit locations for "SendByte()").
  907. bTargetDataPtr++;
  908. bTargetAddress += 2;
  909. }
  910. SendVector(tsync_disable, num_bits_tsync_disable); //PTJ: 307, added for tsync testing
  911. return (PASS);
  912. }
  913. #endif //(PROJECT_REV_)
  914. // end of file ISSP_Routines.c