OpenCores
URL https://opencores.org/ocsvn/wb4pb/wb4pb/trunk

Subversion Repositories wb4pb

[/] [wb4pb/] [trunk/] [asm/] [pbwbuart.psm] - Blame information for rev 31

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 11 ste.fis
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;; This sourcecode is released under BSD license.
3
;; Please see http://www.opensource.org/licenses/bsd-license.php for details!
4
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5
;;
6
;; Copyright (c) 2010, Stefan Fischer 
7
;; All rights reserved.
8
;;
9
;; Redistribution and use in source and binary forms, with or without
10
;; modification, are permitted provided that the following conditions are met:
11
;;
12
;;  * Redistributions of source code must retain the above copyright notice,
13
;;    this list of conditions and the following disclaimer.
14
;;  * Redistributions in binary form must reproduce the above copyright notice,
15
;;    this list of conditions and the following disclaimer in the documentation
16 31 ste.fis
;;    and/or other materials provided with the distribution.
17 11 ste.fis
;;
18
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
;; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
;; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
;; POSSIBILITY OF SUCH DAMAGE.
29
;;
30
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31
;; filename: pbwbuart.psm
32
;; description: uart example, demonstrating access to wishbone peripherals
33
;; todo4user: modify main program and uart code as needed, i. e. non-blocking
34
;;            read and write transactions or data burst transfers
35
;; version: 0.0.0
36
;; changelog: - 0.0.0, initial release
37
;;            - ...
38
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39
 
40
 
41
; wishbone variables
42
NAMEREG sF , wb_addr
43
NAMEREG sE , wb_data ; also used as tmp-reg for status polling
44
 
45
; uart variables
46
NAMEREG sD , uart_data
47
 
48
 
49
ADDRESS 000
50
 
51
; main entry point
52
;;;;;;;;;;;;;;;;;;
53
 
54
DISABLE INTERRUPT
55
 
56
CALL uart_init
57
 
58
; obligatory "Hello World!" message
59
 
60
; new line
61
LOAD uart_data , ASCII_CR_CHAR
62
CALL uart_wr_byte
63
LOAD uart_data , ASCII_LF_CHAR
64
CALL uart_wr_byte
65
; H
66
LOAD uart_data , ASCII_H_UC
67
CALL uart_wr_byte
68
; e
69
LOAD uart_data , ASCII_E_LC
70
CALL uart_wr_byte
71
; l
72
LOAD uart_data , ASCII_L_LC
73
CALL uart_wr_byte
74
; l
75
LOAD uart_data , ASCII_L_LC
76
CALL uart_wr_byte
77
; o
78
LOAD uart_data , ASCII_O_LC
79
CALL uart_wr_byte
80
; space
81
LOAD uart_data , ASCII_SP_CHAR
82
CALL uart_wr_byte
83
; W
84
LOAD uart_data , ASCII_W_UC
85
CALL uart_wr_byte
86
; o
87
LOAD uart_data , ASCII_O_LC
88
CALL uart_wr_byte
89
; r
90
LOAD uart_data , ASCII_R_LC
91
CALL uart_wr_byte
92
; l
93
LOAD uart_data , ASCII_L_LC
94
CALL uart_wr_byte
95
; d
96
LOAD uart_data , ASCII_D_LC
97
CALL uart_wr_byte
98
; !
99
LOAD uart_data , ASCII_EXCLAMATION_MARK_SIGN
100
CALL uart_wr_byte
101
; new line
102
LOAD uart_data , ASCII_CR_CHAR
103
CALL uart_wr_byte
104
LOAD uart_data , ASCII_LF_CHAR
105
CALL uart_wr_byte
106
 
107
; simple loopback of uart data
108
mainloop:
109
  CALL uart_rd_byte
110
  CALL uart_wr_byte
111
  JUMP mainloop
112
 
113
 
114
; wbs_uart module subroutines and settings
115
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
116
 
117
; usage:
118
; 1. set baud rate in uart_init subroutine
119
; 2. call uart_init subroutine to configure wbs_uart module for operation
120
; 3. use uart_wr_byte and uart_rd_byte subroutines to access uart transceiver,
121
;    all accesses are blocking, subroutines do not return, if wbs_uart tx buffer
122
;    is full or rx buffer is empty
123
;    uart write code =>
124
;    LOAD uart_data ,  ; setting up data
125
;    CALL uart_wr_byte ; writing data to uart module
126
;    
127
;    uart read code =>
128
;    CALL uart_rd_byte ; reading data from uart module
129
;    LOAD  , uart_data ; uart_data is updated now
130
;    
131
; 4. uart_clr_buff subroutine can be used to discard wbs_uart rx buffer contents
132
;    during software runtime
133
 
134
; uart start-up configuration, i. e. baudrate
135
uart_init:
136
  ; setting baud rate
137
  LOAD wb_addr , UART_BAUD_LO_ADDR
138
  LOAD wb_data , UART_BAUD_LO_115200_VALUE
139
  CALL wb_wr
140
  LOAD wb_addr , UART_BAUD_HI_ADDR
141
  LOAD wb_data , UART_BAUD_HI_115200_VALUE
142
  CALL wb_wr
143
  ; clear uart receive buffer, after power up and change of baud rate, there is
144
  ; maybe invalid data in it
145
  CALL uart_clr_buff
146
RETURN
147
 
148
; blocking write byte to uart
149
uart_wr_byte:
150
  LOAD wb_addr , UART_RXTX_ADDR
151
  LOAD wb_data , uart_data
152
  CALL wb_wr
153
RETURN
154
 
155
; blocking read byte from uart
156
uart_rd_byte:
157
  LOAD wb_addr , UART_RXTX_ADDR
158
  CALL wb_rd
159
  LOAD uart_data , wb_data
160
RETURN
161
 
162
; uart rx buffer clear
163
uart_clr_buff:
164
  ; uart receive buffer software reset, checking status register for level and
165
  ; reading out all available data
166
  LOAD wb_addr , UART_SR_ADDR ; setting status register address
167
  CALL wb_rd
168
  ; checking data present flag
169
  TEST wb_data , UART_SR_RX_DP_FLAG
170
  ; if flag is not set, returning immediately
171 22 ste.fis
  RETURN Z
172 11 ste.fis
  ; else reading out next byte and checking flag again
173 22 ste.fis
  CALL uart_rd_byte
174
  JUMP uart_clr_buff
175 11 ste.fis
 
176
; register and flag addressing
177
CONSTANT UART_RXTX_ADDR , 00 ; receive/transmit data pipe
178
CONSTANT UART_SR_ADDR , 01 ; status register
179
CONSTANT UART_SR_RX_F_FLAG , 01 ; status rx full
180
CONSTANT UART_SR_RX_HF_FLAG , 02 ; status rx half full
181
CONSTANT UART_SR_RX_DP_FLAG , 04 ; status rx data present
182
CONSTANT UART_SR_TX_F_FLAG , 10 ; status tx full
183
CONSTANT UART_SR_TX_HF_FLAG , 20 ; status tx half full
184
CONSTANT UART_BAUD_LO_ADDR , 02 ; baud rate cfg. register / low byte
185
CONSTANT UART_BAUD_HI_ADDR , 03 ; baud rate cfg. register / high byte
186
 
187
; baud rate configuration:
188
; baud_limit = round( system clock frequency / (16 * baud rate) ) - 1
189
; i. e. 9600 baud at 50 MHz system clock =>
190
; baud_limit = round( 50.0E6 / (16 * 9600) ) - 1 = 325 = 0x0145
191
 
192
; WARNING, baud rate error should not exceed 1.0 % for reliable operation!
193
 
194
; baud rate settings for 50.0E6 Hz system reference clock
195
; max. 3125000.0 baud
196
; min. 48.0 baud (16 bit baud timer)
197
CONSTANT UART_BAUD_LO_300_VALUE , B0 ; actual baud rate 299.99
198
CONSTANT UART_BAUD_HI_300_VALUE , 28 ; => baud rate error 0.003 %
199
CONSTANT UART_BAUD_LO_600_VALUE , 57 ; actual baud rate 600.04
200
CONSTANT UART_BAUD_HI_600_VALUE , 14 ; => baud rate error 0.006 %
201
CONSTANT UART_BAUD_LO_1200_VALUE , 2B ; actual baud rate 1200.08
202
CONSTANT UART_BAUD_HI_1200_VALUE , 0A ; => baud rate error 0.006 %
203
CONSTANT UART_BAUD_LO_2400_VALUE , 15 ; actual baud rate 2400.15
204
CONSTANT UART_BAUD_HI_2400_VALUE , 05 ; => baud rate error 0.006 %
205
CONSTANT UART_BAUD_LO_4800_VALUE , 8A ; actual baud rate 4800.31
206
CONSTANT UART_BAUD_HI_4800_VALUE , 02 ; => baud rate error 0.006 %
207
CONSTANT UART_BAUD_LO_9600_VALUE , 45 ; actual baud rate 9585.89
208
CONSTANT UART_BAUD_HI_9600_VALUE , 01 ; => baud rate error 0.147 %
209
CONSTANT UART_BAUD_LO_19200_VALUE , A2 ; actual baud rate 19171.78
210
CONSTANT UART_BAUD_HI_19200_VALUE , 00 ; => baud rate error 0.147 %
211
CONSTANT UART_BAUD_LO_38400_VALUE , 50 ; actual baud rate 38580.25
212
CONSTANT UART_BAUD_HI_38400_VALUE , 00 ; => baud rate error 0.467 %
213
CONSTANT UART_BAUD_LO_57600_VALUE , 35 ; actual baud rate 57870.37
214
CONSTANT UART_BAUD_HI_57600_VALUE , 00 ; => baud rate error 0.467 %
215
CONSTANT UART_BAUD_LO_115200_VALUE , 1A ; actual baud rate 115740.74
216
CONSTANT UART_BAUD_HI_115200_VALUE , 00 ; => baud rate error 0.467 %
217
;CONSTANT UART_BAUD_LO_230400_VALUE , 0D ; actual baud rate 223214.29
218
;CONSTANT UART_BAUD_HI_230400_VALUE , 00 ; => baud rate error 3.219 %
219
;CONSTANT UART_BAUD_LO_460800_VALUE , 06 ; actual baud rate 446428.57
220
;CONSTANT UART_BAUD_HI_460800_VALUE , 00 ; => baud rate error 3.219 %
221
;CONSTANT UART_BAUD_LO_921600_VALUE , 02 ; actual baud rate 1041666.67
222
;CONSTANT UART_BAUD_HI_921600_VALUE , 00 ; => baud rate error 11.526 %
223
 
224
 
225
; wbm_picoblaze module subroutines and settings
226
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
227
 
228
; subroutines wb_wr and wb_rd are working together with external wbm_picoblaze
229
; wishbone adapter module and therefore should not be modified. wb_wait_on_ack
230
; is a supporting subroutine, which should not be called directly
231
;
232
; transfer principle wishbone write:
233
; 1. OUTPUT cycle to set up wishbone address, data and control signals from
234
;    PORT_ID, OUT_PORT and WRITE_STROBE
235
; 2. INPUT cycle(s) to poll wishbone peripheral acknowledgement using IN_PORT
236
; => at least one OUTPUT and one INPUT cycle for a write
237
;
238
; transfer principle wishbone read:
239
; 1. INPUT cycle to set up wishbone address and control signals from PORT_ID
240
;    and READ_STROBE
241
; 2. INPUT cycle(s) to poll wishbone peripheral acknowledgement using IN_PORT
242
; 3. the very next INPUT cycle after acknowledgement contains valid wishbone
243
;    data from IN_PORT
244
; => at least three INPUT cycles for a read
245
;
246
; calling examples:
247
;
248
; wishbone write code =>
249
;
250
; LOAD wb_addr ,  ; setting up address
251
; LOAD wb_data ,  ; setting up data
252
; CALL wb_wr ; starting wishbone write cycle
253
;  ; wishbone cycle finished
254
;
255
; wishbone read code =>
256
;
257
; LOAD wb_addr ,  ; setting up address
258
; CALL wb_rd ; starting wishbone read cycle
259
; LOAD  , wb_data ; wb_data is updated now
260
;  ; wishbone cycle finished
261
 
262
; wishbone write access
263
wb_wr:
264
  OUTPUT wb_data , (wb_addr)
265
  CALL wb_wait_on_ack
266
RETURN
267
 
268
; wishbone read access
269
wb_rd:
270
  CALL wb_wait_on_ack
271
  INPUT wb_data , (wb_addr)
272
RETURN
273
 
274
; waiting on wishbone cycle to complete
275
wb_wait_on_ack:
276
  INPUT wb_data , (wb_addr)
277
  TEST wb_data , WB_ACK_FLAG
278
  JUMP Z , wb_wait_on_ack
279
RETURN
280
 
281
CONSTANT WB_ACK_FLAG , 01
282
 
283
 
284
; interrupt subroutines and settings
285
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
286
 
287
; IMPORTANT NOTICE!
288
; be carefull, if using interrupts. wishbone cycles must be atomar, as any
289
; other processor local bus cycles are normally be. interrupting wishbone
290
; access may cause a crash of external wishbone master fsm, especially, if
291
; program flow through isr leads to another wishbone cycle!
292
 
293
; interrupt handling template, if needed
294
isr:
295
RETURNI DISABLE
296
ADDRESS 3FF
297
JUMP isr
298
 
299
 
300
; ascii table
301
;;;;;;;;;;;;;
302
 
303
CONSTANT ASCII_NUL_CHAR , 00 ; NUL
304
CONSTANT ASCII_SOH_CHAR , 01 ; SOH
305
CONSTANT ASCII_STX_CHAR , 02 ; STX
306
CONSTANT ASCII_ETX_CHAR , 03 ; ETX
307
CONSTANT ASCII_EOT_CHAR , 04 ; EOT
308
CONSTANT ASCII_ENQ_CHAR , 05 ; ENQ
309
CONSTANT ASCII_ACK_CHAR , 06 ; ACK
310
CONSTANT ASCII_BEL_CHAR , 07 ; BEL
311
CONSTANT ASCII_BS_CHAR , 08 ; BS
312
CONSTANT ASCII_TAB_CHAR , 09 ; TAB
313
CONSTANT ASCII_LF_CHAR , 0A ; LF
314
CONSTANT ASCII_VT_CHAR , 0B ; VT
315
CONSTANT ASCII_FF_CHAR , 0C ; FF
316
CONSTANT ASCII_CR_CHAR , 0D ; CR
317
CONSTANT ASCII_SO_CHAR , 0E ; SO
318
CONSTANT ASCII_SI_CHAR , 0F ; SI
319
CONSTANT ASCII_DLE_CHAR , 10 ; DLE
320
CONSTANT ASCII_DC1_CHAR , 11 ; DC1
321
CONSTANT ASCII_DC2_CHAR , 12 ; DC2
322
CONSTANT ASCII_DC3_CHAR , 13 ; DC3
323
CONSTANT ASCII_DC4_CHAR , 14 ; DC4
324
CONSTANT ASCII_NAK_CHAR , 15 ; NAK
325
CONSTANT ASCII_SYN_CHAR , 16 ; SYN
326
CONSTANT ASCII_ETB_CHAR , 17 ; ETB
327
CONSTANT ASCII_CAN_CHAR , 18 ; CAN
328
CONSTANT ASCII_EM_CHAR , 19 ; EM
329
CONSTANT ASCII_SUB_CHAR , 1A ; SUB
330
CONSTANT ASCII_ESC_CHAR , 1B ; ESC
331
CONSTANT ASCII_FS_CHAR , 1C ; FS
332
CONSTANT ASCII_GS_CHAR , 1D ; GS
333
CONSTANT ASCII_RS_CHAR , 1E ; RS
334
CONSTANT ASCII_US_CHAR , 1F ; US
335
CONSTANT ASCII_SP_CHAR , 20 ; SP
336
CONSTANT ASCII_EXCLAMATION_MARK_SIGN , 21 ; !
337
CONSTANT ASCII_DOUBLE_QUOTE_SIGN , 22 ; "
338
CONSTANT ASCII_NUMBER_SIGN , 23 ; #
339
CONSTANT ASCII_DOLLAR_SIGN , 24 ; $
340
CONSTANT ASCII_PERCENT_SIGN , 25 ; %
341
CONSTANT ASCII_AMPERSAND_SIGN , 26 ; &
342
CONSTANT ASCII_SINGLE_QUOTE_SIGN , 27 ; '
343
CONSTANT ASCII_OPN_PARENTHESIS_SIGN , 28 ; (
344
CONSTANT ASCII_CLS_PARENTHESIS_SIGN , 29 ; )
345
CONSTANT ASCII_ASTERISK_SIGN , 2A ; *
346
CONSTANT ASCII_PLUS_SIGN , 2B ; +
347
CONSTANT ASCII_COMMA_SIGN , 2C ; ,
348
CONSTANT ASCII_MINUS_SIGN , 2D ; -
349
CONSTANT ASCII_DOT_SIGN , 2E ; .
350
CONSTANT ASCII_SLASH_SIGN , 2F ; /
351
CONSTANT ASCII_0_DIGIT , 30 ; 0
352
CONSTANT ASCII_1_DIGIT , 31 ; 1
353
CONSTANT ASCII_2_DIGIT , 32 ; 2
354
CONSTANT ASCII_3_DIGIT , 33 ; 3
355
CONSTANT ASCII_4_DIGIT , 34 ; 4
356
CONSTANT ASCII_5_DIGIT , 35 ; 5
357
CONSTANT ASCII_6_DIGIT , 36 ; 6
358
CONSTANT ASCII_7_DIGIT , 37 ; 7
359
CONSTANT ASCII_8_DIGIT , 38 ; 8
360
CONSTANT ASCII_9_DIGIT , 39 ; 9
361
CONSTANT ASCII_COLON_SIGN , 3A ; :
362
CONSTANT ASCII_SEMICOLON_SIGN , 3B ; ;
363
CONSTANT ASCII_LESS_THAN_SIGN , 3C ; <
364
CONSTANT ASCII_EQUAL_SIGN , 3D ; =
365
CONSTANT ASCII_GREATER_THAN_SIGN , 3E ; >
366
CONSTANT ASCII_QUESTION_MARK_SIGN , 3F ; ?
367
CONSTANT ASCII_AT_SIGN , 40 ; @
368
CONSTANT ASCII_A_UC , 41 ; A
369
CONSTANT ASCII_B_UC , 42 ; B
370
CONSTANT ASCII_C_UC , 43 ; C
371
CONSTANT ASCII_D_UC , 44 ; D
372
CONSTANT ASCII_E_UC , 45 ; E
373
CONSTANT ASCII_F_UC , 46 ; F
374
CONSTANT ASCII_G_UC , 47 ; G
375
CONSTANT ASCII_H_UC , 48 ; H
376
CONSTANT ASCII_I_UC , 49 ; I
377
CONSTANT ASCII_J_UC , 4A ; J
378
CONSTANT ASCII_K_UC , 4B ; K
379
CONSTANT ASCII_L_UC , 4C ; L
380
CONSTANT ASCII_M_UC , 4D ; M
381
CONSTANT ASCII_N_UC , 4E ; N
382
CONSTANT ASCII_O_UC , 4F ; O
383
CONSTANT ASCII_P_UC , 50 ; P
384
CONSTANT ASCII_Q_UC , 51 ; Q
385
CONSTANT ASCII_R_UC , 52 ; R
386
CONSTANT ASCII_S_UC , 53 ; S
387
CONSTANT ASCII_T_UC , 54 ; T
388
CONSTANT ASCII_U_UC , 55 ; U
389
CONSTANT ASCII_V_UC , 56 ; V
390
CONSTANT ASCII_W_UC , 57 ; W
391
CONSTANT ASCII_X_UC , 58 ; X
392
CONSTANT ASCII_Y_UC , 59 ; Y
393
CONSTANT ASCII_Z_UC , 5A ; Z
394
CONSTANT ASCII_OPN_BRACKET_SIGN , 5B ; [
395
CONSTANT ASCII_BACKSLASH_SIGN , 5C ; \
396
CONSTANT ASCII_CLS_BRACKET_SIGN , 5D ; ]
397
CONSTANT ASCII_CARET_SIGN , 5E ; ^
398
CONSTANT ASCII_UNDERSCORE_SIGN , 5F ; _
399
CONSTANT ASCII_ACCENT_SIGN , 60 ; `
400
CONSTANT ASCII_A_LC , 61 ; a
401
CONSTANT ASCII_B_LC , 62 ; b
402
CONSTANT ASCII_C_LC , 63 ; c
403
CONSTANT ASCII_D_LC , 64 ; d
404
CONSTANT ASCII_E_LC , 65 ; e
405
CONSTANT ASCII_F_LC , 66 ; f
406
CONSTANT ASCII_G_LC , 67 ; g
407
CONSTANT ASCII_H_LC , 68 ; h
408
CONSTANT ASCII_I_LC , 69 ; i
409
CONSTANT ASCII_J_LC , 6A ; j
410
CONSTANT ASCII_K_LC , 6B ; k
411
CONSTANT ASCII_L_LC , 6C ; l
412
CONSTANT ASCII_M_LC , 6D ; m
413
CONSTANT ASCII_N_LC , 6E ; n
414
CONSTANT ASCII_O_LC , 6F ; o
415
CONSTANT ASCII_P_LC , 70 ; p
416
CONSTANT ASCII_Q_LC , 71 ; q
417
CONSTANT ASCII_R_LC , 72 ; r
418
CONSTANT ASCII_S_LC , 73 ; s
419
CONSTANT ASCII_T_LC , 74 ; t
420
CONSTANT ASCII_U_LC , 75 ; u
421
CONSTANT ASCII_V_LC , 76 ; v
422
CONSTANT ASCII_W_LC , 77 ; w
423
CONSTANT ASCII_X_LC , 78 ; x
424
CONSTANT ASCII_Y_LC , 79 ; y
425
CONSTANT ASCII_Z_LC , 7A ; z
426
CONSTANT ASCII_OPN_BRACE_SIGN , 7B ; {
427
CONSTANT ASCII_VERTICAL_BAR_SIGN , 7C ; |
428
CONSTANT ASCII_CLS_BRACE_SIGN , 7D ; }
429
CONSTANT ASCII_TILDE_SIGN , 7E ; ~
430
CONSTANT ASCII_DEL_CHAR , 7F ; DEL

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.