cob.forth 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. \ program the on-board flash then run some tests
  2. base @ decimal
  3. \ specify which board is valid (V1..V4)
  4. BOARD-V3 constant REQUIRED-BOARD
  5. : display-board-revision ( -- )
  6. get-board-revision
  7. case
  8. BOARD-V1 of
  9. s" V1"
  10. endof
  11. BOARD-V2 of
  12. s" V2"
  13. endof
  14. BOARD-V3 of
  15. s" V3"
  16. endof
  17. BOARD-V4 of
  18. s" V4"
  19. endof
  20. s" UNKNOWN" swap \ since endcase will do a drop (for selector value)
  21. endcase
  22. lcd-." Board revision: "
  23. lcd-type
  24. lcd-cr
  25. ;
  26. : get-image-file ( b u addr -- flag )
  27. >r
  28. r/o bin open-file ?dup
  29. if
  30. lcd-." open error = " lcd-dec. lcd-cr
  31. r> 2drop
  32. false exit
  33. then
  34. r> \ file-id buffer
  35. swap >r \ buffer
  36. flash-rom-size
  37. begin
  38. \ b u1
  39. 2dup
  40. r@ read-file ?dup \ b u1 u2 ior ior?
  41. if
  42. lcd-." read error = " lcd-dec. lcd-cr
  43. 2drop drop
  44. r> close-file drop
  45. false exit
  46. then
  47. \ b u1 u2
  48. swap over - \ b u2 (u1-u2)
  49. >r + r> \ (b+u2) (u1-u2)
  50. dup 0=
  51. until
  52. 2drop
  53. r> close-file drop
  54. true
  55. ;
  56. create serial-number-buffer
  57. flash-serial-number-length allot
  58. : program-rom ( b u -- )
  59. flash-select-internal
  60. lcd-." Load File: " 2dup lcd-type lcd-cr
  61. here get-image-file 0=
  62. if
  63. lcd-." Read file failed" lcd-cr
  64. exit
  65. then
  66. lcd-." S/N: "
  67. \ display the serial number
  68. serial-number-buffer flash-serial-number-offset +
  69. flash-serial-number-length
  70. flash-serial-number-offset flash-read
  71. if
  72. flash-serial-number-length 0
  73. ?do
  74. serial-number-buffer flash-serial-number-offset + i + c@
  75. dup bl 127 within
  76. if
  77. lcd-emit
  78. else
  79. drop
  80. then
  81. loop
  82. else
  83. lcd-." FAIL" lcd-cr
  84. exit
  85. then
  86. lcd-cr lcd-." Erase: "
  87. flash-write-enable
  88. if
  89. flash-chip-erase
  90. if
  91. lcd-." Done"
  92. else
  93. lcd-." FAIL"
  94. lcd-cr exit
  95. then
  96. else
  97. lcd-." FAIL"
  98. lcd-cr exit
  99. then
  100. lcd-cr lcd-." Program: "
  101. flash-rom-size 0 ?do
  102. flash-write-enable 0=
  103. if
  104. lcd-." FAIL" lcd-cr
  105. unloop
  106. exit
  107. then
  108. here i + flash-page-size i flash-write 0=
  109. if
  110. lcd-." FAIL" lcd-cr
  111. unloop
  112. exit
  113. then
  114. i flash-sector-size 1- and 0=
  115. if
  116. [char] . lcd-emit
  117. then
  118. flash-page-size +loop
  119. lcd-cr lcd-." Verify: "
  120. flash-rom-size 0 ?do
  121. here i + flash-sector-size i flash-verify
  122. if
  123. [char] . lcd-emit
  124. else
  125. [char] E lcd-emit
  126. then
  127. flash-sector-size +loop
  128. lcd-cr
  129. ;
  130. \ ===========================================
  131. : within-box ( x y x0 y0 x1 y1 -- flag )
  132. swap >r rot >r \ x y y0 y1
  133. within \ x flag
  134. swap r> r> \ flag x x0 x1
  135. within and
  136. ;
  137. variable sector
  138. variable error-count
  139. : scan-sd-sector ( -- )
  140. sector @ 1+ $fffff and dup sector !
  141. 8 lcd-text-rows 3 - lcd-at-xy
  142. dup 8 lcd-u.r
  143. >r here 1024 + 1 r> read-sectors ?dup
  144. if
  145. drop
  146. 8 lcd-text-rows 2 - lcd-at-xy
  147. 1 error-count +! error-count @ 8 lcd-u.r
  148. filesystem-init
  149. then
  150. ;
  151. 50 constant box-width
  152. 50 constant box-height
  153. lcd-width-pixels 2/ 15 - dup
  154. constant touch-x0
  155. box-width +
  156. constant touch-x1
  157. lcd-height-pixels 2/ 15 - dup
  158. constant touch-y0
  159. box-height +
  160. constant touch-y1
  161. 1 constant event-no-touch
  162. 2 constant event-touch
  163. 3 constant event-release
  164. 4 constant event-k1
  165. 5 constant event-k2
  166. 6 constant event-k3
  167. 7 constant event-timeout
  168. variable touched
  169. variable time-limit
  170. : read-event ( -- e )
  171. 0 time-limit !
  172. begin
  173. ctp-pos? if
  174. ctp-pos dup 0<
  175. if
  176. 2drop
  177. touched @
  178. if
  179. false touched !
  180. touch-x0 touch-y0 lcd-move-to
  181. 2 2 lcd-move-rel
  182. box-width 4 - box-height 4 - lcd-white lcd-box
  183. lcd-black
  184. event-no-touch exit
  185. then
  186. else
  187. touch-x0 touch-y0
  188. touch-x1 touch-y1
  189. within-box
  190. touched @ 0= and
  191. if
  192. true touched !
  193. touch-x0 touch-y0 lcd-move-to
  194. 2 2 lcd-move-rel
  195. box-width 4 - box-height 4 - lcd-box
  196. event-touch exit
  197. then
  198. then
  199. then
  200. button? if
  201. button
  202. case
  203. button-none of
  204. event-release exit
  205. endof
  206. button-left of
  207. event-k1 exit
  208. endof
  209. button-centre of
  210. event-k2 exit
  211. endof
  212. button-right of
  213. event-k3 exit
  214. endof
  215. endcase
  216. then
  217. key? if
  218. key-flush
  219. then
  220. scan-sd-sector
  221. 1 time-limit +!
  222. time-limit @ 500 >
  223. if
  224. event-timeout exit
  225. then
  226. again
  227. ;
  228. variable bitset
  229. variable in-ok
  230. : cob-clear-log ( -- )
  231. 12 6 ?do
  232. 0 i lcd-at-xy 11 lcd-spaces
  233. loop
  234. ;
  235. : cob-ok ( -- )
  236. true in-ok !
  237. 21 7 lcd-at-xy lcd-." OO k "
  238. 21 8 lcd-at-xy lcd-." O O k k"
  239. 21 9 lcd-at-xy lcd-." O O kk "
  240. 21 10 lcd-at-xy lcd-." OO k k"
  241. ;
  242. : cob-ok-off ( -- )
  243. in-ok @
  244. if
  245. 11 7 ?do
  246. 21 i lcd-at-xy 8 lcd-spaces
  247. loop
  248. false in-ok !
  249. then
  250. ;
  251. : cob-test ( -- )
  252. false touched !
  253. 0 bitset !
  254. begin
  255. read-event
  256. case
  257. event-timeout of
  258. 0 bitset !
  259. cob-clear-log
  260. cob-ok-off
  261. endof
  262. event-no-touch of
  263. 0 7 lcd-at-xy lcd-." CTP release"
  264. $01 bitset @ or bitset !
  265. endof
  266. event-touch of
  267. 0 6 lcd-at-xy lcd-." CTP touch "
  268. 0 7 lcd-at-xy lcd-." "
  269. $02 bitset @ or bitset !
  270. $01 invert bitset @ and bitset !
  271. cob-ok-off
  272. endof
  273. event-release of
  274. 0 8 lcd-at-xy lcd-." Key release"
  275. $04 bitset @ or bitset !
  276. endof
  277. event-k1 of
  278. 0 8 lcd-at-xy lcd-." "
  279. 0 9 lcd-at-xy lcd-." Key 1 "
  280. $08 bitset @ or bitset !
  281. $04 invert bitset @ and bitset !
  282. cob-ok-off
  283. endof
  284. event-k2 of
  285. 0 8 lcd-at-xy lcd-." "
  286. 0 10 lcd-at-xy lcd-." Key 2 "
  287. $10 bitset @ or bitset !
  288. $04 invert bitset @ and bitset !
  289. cob-ok-off
  290. endof
  291. event-k3 of
  292. 0 8 lcd-at-xy lcd-." "
  293. 0 11 lcd-at-xy lcd-." Key 3 "
  294. $20 bitset @ or bitset !
  295. $04 invert bitset @ and bitset !
  296. cob-ok-off
  297. endof
  298. endcase
  299. bitset @ $3f and $3f =
  300. until
  301. cob-ok
  302. ;
  303. : cob-main ( -- )
  304. button-flush
  305. key-flush
  306. ctp-flush
  307. lcd-cls
  308. lcd-." COB Testing" lcd-cr
  309. get-board-revision REQUIRED-BOARD xor if
  310. display-board-revision
  311. lcd-cr
  312. lcd-." FAIL: Incorrect board revision"
  313. begin
  314. again
  315. then
  316. \ s" flash.rom" program-rom
  317. 10 lcd-text-rows 1- lcd-at-xy lcd-." Key1"
  318. 17 lcd-text-rows 1- lcd-at-xy lcd-." Key2"
  319. 24 lcd-text-rows 1- lcd-at-xy lcd-." Key3"
  320. 0 lcd-text-rows 3 - lcd-at-xy
  321. lcd-." sector: " lcd-cr
  322. lcd-." errors: "
  323. touch-x0 touch-y0 lcd-move-to
  324. box-width box-height lcd-box
  325. begin
  326. cob-clear-log
  327. cob-test
  328. again
  329. ;
  330. base !