issp_main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* filename: main.c
  2. */
  3. #include "issp_revision.h"
  4. #ifdef PROJECT_REV_304
  5. /*
  6. * Copyright 2006-2007, Cypress Semiconductor Corporation.
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. * MA 02110-1301, USA.
  21. */
  22. /* ############################################################################
  23. ################### CRITICAL PROJECT CONSTRAINTS ########################
  24. ############################################################################
  25. ISSP programming can only occur within a temperature range of 5C to 50C.
  26. - This project is written without temperature compensation and using
  27. programming pulse-widths that match those used by programmers such as the
  28. Mini-Prog and the ISSP Programmer.
  29. This means that the die temperature of the PSoC device cannot be outside
  30. of the above temperature range.
  31. If a wider temperature range is required, contact your Cypress Semi-
  32. conductor FAE or sales person for assistance.
  33. The project can be configured to program devices at 5V or at 3.3V.
  34. - Initialization of the device is different for different voltages. The
  35. initialization is hardcoded and can only be set for one voltage range.
  36. The supported voltages ranges are 3.3V (3.0V to 3.6V) and 5V (4.75V to
  37. 5.25V). See the device datasheet for more details. If varying voltage
  38. ranges must be supported, contact your Cypress Semiconductor FAE or sales
  39. person for assistance.
  40. - ISSP programming for the 2.7V range (2.7V to 3.0V) is not supported.
  41. This program does not support programming all PSoC Devices
  42. - It does not support obsoleted PSoC devices. A list of devices that are
  43. not supported is shown here:
  44. CY8C22x13 - not supported
  45. CY8C24x23 - not supported (CY8C24x23A is supported)
  46. CY8C25x43 - not supported
  47. CY8C26x43 - not supported
  48. - It does not suport devices that have not been released for sale at the
  49. time this version was created. If you need to ISSP program a newly released
  50. device, please contact Cypress Semiconductor Applications, your FAE or
  51. sales person for assistance.
  52. The CY8C20x23 devices are not supported at the time of this release.
  53. */
  54. /*
  55. PSoC In-System Serial Programming (ISSP) Template
  56. This PSoC Project is designed to be used as a template for designs that
  57. require PSoC ISSP Functions.
  58. This project is based on the AN2026 series of Application Notes. That app
  59. note should be referenced before any modifications to this project are made.
  60. The subroutines and files were created in such a way as to allow easy cut &
  61. paste as needed. There are no customer-specific functions in this project.
  62. This demo of the code utilizes a PSoC as the Host.
  63. Some of the subroutines could be merged, or otherwise reduced, but they have
  64. been written as independently as possible so that the specific steps involved
  65. within each function can easily be seen. By merging things, some code-space
  66. savings could be realized.
  67. As is, and with all features enabled, the project consumes approximately 3500
  68. bytes of code space, and 19-Bytes of RAM (not including stack usage). The
  69. Block-Verify requires a 64-Byte buffer for read-back verification. This same
  70. buffer could be used to hold the (actual) incoming program data.
  71. Please refer to the compiler-directives file "directives.h" to see the various
  72. features.
  73. The pin used in this project are assigned as shown below. The HOST pins are
  74. arbitrary and any 3 pins could be used (the masks used to control the pins
  75. must be changed). The TARGET pins cannot be changed, these are fixed function
  76. pins on the PSoC.
  77. The PWR pin is used to provide power to the target device
  78. if power cycle programming mode is used.
  79. The compiler directive RESET_MODE in ISSP_directives.h
  80. is used to select the programming mode. This pin could control the enable on
  81. a voltage regulator, or could control the gate of a FET that is used to turn
  82. the power to the PSoC on.
  83. The TP pin is a Test Point pin that can be used signal from the host processor
  84. that the program has completed certain tasks. Predefined test points are
  85. included that can be used to observe the timing for bulk erasing, block
  86. programming and security programming.
  87. SIGNAL HOST TARGET
  88. ---------------------
  89. SDATA P1.0 P1.0
  90. SCLK P1.1 P1.1
  91. XRES P2.0 XRES
  92. PWR P2.1 Vdd
  93. TP P0.7 n/a
  94. For test & demonstration, this project generates the program data internally.
  95. It does not take-in the data from an external source such as I2C, UART, SPI,
  96. etc. However, the program was written in such a way to be portable into such
  97. designs. The spirit of this project was to keep it stripped to the minimum
  98. functions required to do the ISSP functions only, thereby making a portable
  99. framework for integration with other projects.
  100. The high-level functions have been written in C in order to be portable to
  101. other processors. The low-level functions that are processor dependent, such
  102. as toggling pins and implementing specific delays, are all found in the file
  103. ISSP_Drive_Routines.c. These functions must be converted to equivalent
  104. functions for the HOST processor. Care must be taken to meet the timing
  105. requirements when converting to a new processor. ISSP timing information can
  106. be found in Application Note AN2026. All of the sections of this program
  107. that need to be modified for the host processor have "PROCESSOR_SPECIFIC" in
  108. the comments. By performing a "Find in files" using "PROCESSOR_SPECIFIC" these
  109. sections can easily be identified.
  110. The variables in this project use Hungarian notation. Hungarian prepends a
  111. lower case letter to each variable that identifies the variable type. The
  112. prefixes used in this program are defined below:
  113. b = byte length variable, signed char and unsigned char
  114. i = 2-byte length variable, signed int and unsigned int
  115. f = byte length variable used as a flag (TRUE = 0, FALSE != 0)
  116. ab = an array of byte length variables
  117. After this program has been ported to the desired host processor the timing
  118. of the signals must be confirmed. The maximum SCLK frequency must be checked
  119. as well as the timing of the bulk erase, block write and security write
  120. pulses.
  121. The maximum SCLK frequency for the target device can be found in the device
  122. datasheet under AC Programming Specifications with a Symbol of "Fsclk".
  123. An oscilloscope should be used to make sure that no half-cycles (the high
  124. time or the low time) are shorter than the half-period of the maximum
  125. freqency. In other words, if the maximum SCLK frequency is 8MHz, there can be
  126. no high or low pulses shorter than 1/(2*8MHz), or 62.5 nsec.
  127. The test point (TP) functions, enabled by the define USE_TP, provide an output
  128. from the host processor that brackets the timing of the internal bulk erase,
  129. block write and security write programming pulses. An oscilloscope, along with
  130. break points in the PSoC ICE Debugger should be used to verify the timing of
  131. the programming. The Application Note, "Host-Sourced Serial Programming"
  132. explains how to do these measurements and should be consulted for the expected
  133. timing of the erase and program pulses.
  134. */
  135. /*
  136. ----------------------------------------------------------------------------
  137. C main line
  138. ----------------------------------------------------------------------------
  139. */
  140. /*
  141. #include <m8c.h>
  142. #include "PSoCAPI.h"
  143. */
  144. /*
  145. ------ Declarations Associated with ISSP Files & Routines -------
  146. Add these to your project as needed.
  147. */
  148. #include "issp_extern.h"
  149. #include "issp_directives.h"
  150. #include "issp_defs.h"
  151. #include "issp_errors.h"
  152. #include <linux/module.h>
  153. /* ------------------------------------------------------------------------- */
  154. unsigned char bBankCounter;
  155. unsigned int iBlockCounter;
  156. unsigned int iChecksumData;
  157. unsigned int iChecksumTarget;
  158. /* Address Numbers for Bytes within a Block */
  159. unsigned char TargetDatabufLen = TARGET_DATABUFF_LEN;
  160. /* For example, radon is 2 */
  161. unsigned char NumBanks = NUM_BANKS;
  162. /* Block Numbers for Program Data */
  163. unsigned char BlocksPerBank = BLOCKS_PER_BANK;
  164. unsigned char SecurityBytesPerBank = SECURITY_BYTES_PER_BANK;
  165. /* received data counter */
  166. unsigned int RxCounter;
  167. unsigned char TopMenu = 0xFF;
  168. char MenuCountdown = 10;
  169. #ifdef TX_ON
  170. void TX8SW_PutHexHalf(char ch)
  171. {
  172. if (ch >= 10)
  173. TX8SW_PutChar(ch + 'A'-10);
  174. else
  175. TX8SW_PutChar(ch + '0');
  176. }
  177. void TX8SW_PutHexWord(unsigned int ch)
  178. {
  179. TX8SW_PutSHexByte(ch>>8);
  180. TX8SW_PutSHexByte(ch&0xff);
  181. }
  182. #endif
  183. /*
  184. =========================================================================
  185. ErrorTrap()
  186. Return is not valid from main for PSOC, so this ErrorTrap routine is used.
  187. For some systems returning an error code will work best. For those, the
  188. calls to ErrorTrap() should be replaced with a return(bErrorNumber). For
  189. other systems another method of reporting an error could be added to this
  190. function -- such as reporting over a communcations port.
  191. =========================================================================
  192. */
  193. void ErrorTrap(struct cypress_touchkey_info *info, unsigned char bErrorNumber)
  194. {
  195. #ifndef RESET_MODE
  196. /*
  197. Set all pins to highZ to avoid back powering the PSoC through the GPIO
  198. protection diodes.
  199. */
  200. SetSCLKHiZ();
  201. SetSDATAHiZ();
  202. /* If Power Cycle programming, turn off the target */
  203. RemoveTargetVDD(info);
  204. #endif
  205. #ifdef TX_ON
  206. TX8SW_PutCRLF();
  207. TX8SW_CPutString("ErrorTrap");
  208. TX8SW_PutSHexByte(bErrorNumber);
  209. #endif
  210. pr_err("[TKEY] ErrorTrap..%d\n", bErrorNumber);
  211. /* while (1)
  212. ;
  213. */
  214. }
  215. /* ========================================================================= */
  216. /* MAIN LOOP */
  217. /* Based on the diagram in the AN2026 */
  218. /* ========================================================================= */
  219. int ISSP_main(struct cypress_touchkey_info *info)
  220. {
  221. /*
  222. -- This example section of commands show the high-level calls to -------
  223. -- perform Target Initialization, SilcionID Test, Bulk-Erase, Target ---
  224. -- RAM Load, FLASH-Block Program, and Target Checksum Verification. ----
  225. */
  226. pr_err("[TKEY] start..\n");
  227. SetSCLKHiZ();
  228. SetSDATAHiZ();
  229. /* Cycle power on the target to cause a reset */
  230. RemoveTargetVDD(info);
  231. #ifdef TX_ON
  232. TX8SW_Start();
  233. TX8SW_CPutString("\r\nSee HSSP Spec. 40-95004");
  234. TX8SW_CPutString("\r\nStart HSSP....");
  235. #endif
  236. /*
  237. >>>> ISSP Programming Starts Here <<<<
  238. Acquire the device through reset or power cycle
  239. */
  240. #ifdef RESET_MODE
  241. /* Initialize the Host & Target for ISSP operations */
  242. fIsError = fXRESInitializeTargetForISSP();
  243. if (fIsError != 0)
  244. ErrorTrap(info, fIsError);
  245. #else
  246. /* Initialize the Host & Target for ISSP operations */
  247. fIsError = fPowerCycleInitializeTargetForISSP(info);
  248. if (fIsError != 0)
  249. ErrorTrap(info, fIsError);
  250. #endif
  251. /*#if 0
  252. Run the SiliconID Verification, and proceed according to result.
  253. fIsError = fVerifySiliconID();
  254. if (fIsError != 0)
  255. ErrorTrap(fIsError);
  256. #endif*/
  257. #ifdef TX_ON
  258. TX8SW_PutCRLF();
  259. TX8SW_CPutString("End VerifySiliconID");
  260. #endif
  261. /* Bulk-Erase the Device. */
  262. fIsError = fEraseTarget();
  263. if (fIsError != 0)
  264. ErrorTrap(info, fIsError);
  265. #ifdef TX_ON
  266. TX8SW_PutCRLF();
  267. TX8SW_CPutString("End EraseTarget");
  268. TX8SW_PutCRLF();
  269. TX8SW_CPutString("Program Flash Blocks Start");
  270. TX8SW_PutCRLF();
  271. #endif
  272. /* program flash block */
  273. /*
  274. Program Flash blocks with predetermined data. In the final application
  275. this data should come from the HEX output of PSoC Designer.
  276. */
  277. iChecksumData = 0; /* Calculte the device checksum as you go*/
  278. for (bBankCounter = 0; bBankCounter < NUM_BANKS; bBankCounter++) {
  279. /*PTJ: NUM_BANKS should be 1 for Krypton*/
  280. for (iBlockCounter = 0; iBlockCounter < BLOCKS_PER_BANK;
  281. iBlockCounter++) {
  282. /*printk("Program Loop : iBlockCounter %d\n"
  283. ,iBlockCounter);
  284. INTLOCK();
  285. local_irq_save(flags);
  286. PTJ: READ-WRITE-SETUP used here to select SRAM
  287. Bank 1, and TSYNC Enable*/
  288. #ifdef CY8C20x66
  289. fIsError = fSyncEnable();
  290. if (fIsError) {
  291. ErrorTrap(info, fIsError);
  292. return fIsError;
  293. }
  294. fIsError = fReadWriteSetup();
  295. if (fIsError) {
  296. /*send write command - swanhan*/
  297. ErrorTrap(info, fIsError);
  298. return fIsError;
  299. }
  300. #endif
  301. /*firmware read.
  302. //LoadProgramData(bBankCounter, (unsigned char)iBlockCounter);
  303. //PTJ: this loads the Hydra with test data, not the krypton
  304. //PTJ: this loads the Hydra with test data, not the krypton*/
  305. LoadProgramData(info, (unsigned char)iBlockCounter, bBankCounter);
  306. iChecksumData += iLoadTarget();/*PTJ: this loads the Krypton*/
  307. /*dog_kick();*/
  308. fIsError = fProgramTargetBlock(bBankCounter,
  309. (unsigned char)iBlockCounter);
  310. if (fIsError) {
  311. ErrorTrap(info, fIsError);
  312. return fIsError;
  313. }
  314. #ifdef CY8C20x66 /*PTJ: READ-STATUS after PROGRAM-AND-VERIFY*/
  315. #if defined(CONFIG_KOR_MODEL_SHV_E160S) || defined(CONFIG_KOR_MODEL_SHV_E160K)
  316. || defined(CONFIG_KOR_MODEL_SHV_E160L)
  317. msleep(50);
  318. #endif
  319. fIsError = fReadStatus();
  320. if (fIsError) {
  321. ErrorTrap(info, fIsError);
  322. return fIsError;
  323. }
  324. #endif
  325. }
  326. }
  327. /*
  328. PTJ: Doing Verify
  329. PTJ: this code isnt needed in the program flow
  330. because we use PROGRAM-AND-VERIFY (ProgramAndVerify SROM Func)
  331. PTJ: which has Verify built into it.
  332. Verify included for completeness in case host desires to do
  333. a stand-alone verify at a later date.
  334. */
  335. for (bBankCounter = 0; bBankCounter < NUM_BANKS; bBankCounter++) {
  336. /*PTJ: READ-WRITE-SETUP used here to select SRAM Bank 1*/
  337. #ifdef CY8C20x66
  338. fIsError = fSyncEnable();
  339. if (fIsError) {/*PTJ: 307, added for tsync enable testing.*/
  340. ErrorTrap(info, fIsError);
  341. return fIsError;
  342. }
  343. fIsError = fReadWriteSetup();
  344. if (fIsError) {
  345. ErrorTrap(info, fIsError);
  346. return fIsError;
  347. }
  348. #endif
  349. /*Load one bank of security data from hex file into buffer*/
  350. fIsError = fLoadSecurityData(bBankCounter);
  351. if (fIsError) {
  352. ErrorTrap(info, fIsError);
  353. return fIsError;
  354. }
  355. /*Secure one bank of the target flash*/
  356. fIsError = fSecureTargetFlash();
  357. if (fIsError) {
  358. ErrorTrap(info, fIsError);
  359. return fIsError;
  360. }
  361. }
  362. /*printk("Program security data END\n");*/
  363. /*
  364. Program security data into target PSoC. In the final application this
  365. data should come from the HEX output of PSoC Designer.
  366. */
  367. fIsError = fLoadSecurityData(bBankCounter);
  368. if (fIsError) {
  369. ErrorTrap(info, fIsError);
  370. return fIsError;
  371. }
  372. #ifdef CY8C20x66
  373. fIsError = fReadSecurity();
  374. if (fIsError) {
  375. ErrorTrap(info, fIsError);
  376. return fIsError;
  377. }
  378. #endif
  379. /* checksum */
  380. /*=======================================================//
  381. //PTJ: Doing Checksum after READ-SECURITY*/
  382. iChecksumTarget = 0;
  383. for (bBankCounter = 0; bBankCounter < NUM_BANKS; bBankCounter++) {
  384. fIsError = fAccTargetBankChecksum(&iChecksumTarget);
  385. if (fIsError) {
  386. ErrorTrap(info, fIsError);
  387. return fIsError;
  388. }
  389. }
  390. pr_err("Checksum : iChecksumTarget (0x%X)\n", (unsigned char)iChecksumTarget);
  391. pr_err("Checksum : iChecksumData (0x%X)\n", (unsigned char)iChecksumData);
  392. if ((unsigned short)(iChecksumTarget&0xffff) !=
  393. (unsigned short) (iChecksumData & 0xffff)) {
  394. ErrorTrap(info, VERIFY_ERROR);
  395. return fIsError;
  396. }
  397. /* *** SUCCESS ***
  398. // At this point, the Target has been successfully Initialize, ID-Checked,
  399. // Bulk-Erased, Block-Loaded, Block-Programmed, Block-Verified, and Device-
  400. // Checksum Verified.
  401. // You may want to restart Your Target PSoC Here.*/
  402. ReStartTarget(info);
  403. return 0;
  404. }
  405. #endif /* (PROJECT_REV_) */