1/**
2* \ingroup <FILEGROUP>
3*
4* \file msp430x5xx_FlashWrite.s43
5*
6* \brief Flash word write funclet
7*
8*/
9/*
10 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
11 *
12 *
13 *  Redistribution and use in source and binary forms, with or without
14 *  modification, are permitted provided that the following conditions
15 *  are met:
16 *
17 *    Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 *
20 *    Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the
23 *    distribution.
24 *
25 *    Neither the name of Texas Instruments Incorporated nor the names of
26 *    its contributors may be used to endorse or promote products derived
27 *    from this software without specific prior written permission.
28 *
29 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42#include "msp430x54x.h"
43;-------------------------------------------------------------------------------
44            NAME    FlashWordWrite
45;-------------------------------------------------------------------------------
46; Exported Symbols
47PUBLIC      MyWriteAddr
48PUBLIC      MyWriteSize
49PUBLIC      MyWriteLock
50PUBLIC      WordWrite
51;-------------------------------------------------------------------------------
52            RSEG    MYVARS
53;-------------------------------------------------------------------------------
54MyProgStart
55MyStart     DW      WordWrite - MyProgStart
56MyEnd       DW      TheEnd - MyProgStart
57MyLockTst   DW      FWKEY+WAIT
58MyFlash1    DW      0xA500
59MyFlash3    DW      0xA500
60MyR10       DL      0x000BDEAD
61MyR11       DL      0x000BDEAD
62
63
64; must be set from calling marco
65#define MyWriteAddr R5
66; must be set from calling marco
67#define MyWriteSize R6
68; load by funclet
69#define MyWriteLock R8
70; load by funclet
71#define MyEraseType R9
72;-------------------------------------------------------------------------------
73            RSEG    MYCODE
74
75;-------------------------------------------------------------------------------
76;--- [1] Write Routine Start ---------------------------------------------------
77WordWrite
78StopWDT     mov.w   #WDTPW+WDTHOLD,&WDTCTL   ; Make sure watchdog timer is stopped
79NotifyDriverStart
80            MOV     #0xABAD,&SYSJMBO1
81            MOV     #0xBABE,&SYSJMBO0
82
83;--- [2] Save Context Section --------------------------------------------------
84SaveFCTL    ; save Flash Controller Configuration
85            MOV     &FCTL1,MyFlash1
86            MOV     &FCTL3,MyFlash3
87SaveR10    ; save R10 & R11 & R12
88            MOVX.A  R10,MyR10
89            MOVX.A  R11,MyR11
90
91;--- [3] Actual Write Sequence -------------------------------------------------
92L1          BIT     #BUSY,&FCTL3
93            JNZ     L1
94HandleLockedSegment
95            MOV     MyWriteLock,&FCTL3    ; Assign value passed by driver
96            MOV     &FCTL3,MyLockTst      ; Read out register again
97            CMP.B   MyWriteLock,MyLockTst ; Is content as expected?
98            JEQ     DoTask                ; Yes, continue
99ToggleLockA
100            MOV     MyWriteLock,R10       ; No, set LOCKA bit to be toggled
101            BIS     #LOCKA,R10
102            MOV     R10,&FCTL3            ; Assign value again - LOCKA will be toggled now
103DoTask
104            MOVX.A   MyWriteAddr,R10      ; The start address to write to
105            MOVX.A   MyWriteSize,R11      ; the number of words to be written
106
107//           MOV     #FWKEY+WRT,&FCTL1     ; configure word-write access
108#ifdef SUBROUTINE
109W1          ; CMP.W   @R12,0(R10)         ; check if content is already there...
110            ; JEQ     Next                ; ...next word if already there
111
112            MOV     #FWPW,&FCTL3          ; Clear LOCK
113           ;MOV     #FWKEY+WRT,&FCTL1     ; configure word-write access
114            MOVX.W  @R12+,0(R10)
115Next
116#else
117            MOV     #JMBCLR0OFF+JMBCLR1OFF+JMBMODE,&SYSJMBC  ; disable auto-clear feature, 32bit mode
118
119W1          BIT     #JMBIN0FG,&SYSJMBC    ; Data available in mailbox register 0?
120            JZ      W1                    ; No, wait for data
121
122            MOV.W   &SYSJMBI0,R13        ;  move data into temp Register
123            BIC     #JMBIN0FG,&SYSJMBC    ; clear the mailbox input flag
124
125            ;----------Write two Words----------------------------------------;
126
127            ; Write Word one--------------------------------------------------
128L2          BIT     #BUSY,&FCTL3
129            JNZ     L2
130            MOV     #FWPW,&FCTL1        ; Done. Clear WRT
131
132             ; Flash ready for new data -> Write Word one
133            MOV     #FWPW+WRT,&FCTL1    ; Enable write
134            MOV.W   R13,0(R10)         ; WRITE, move data into Flash
135
136            INCDX.A R10                 ; count up adress
137            DECX.A  R11                 ; decrement length
138
139            JZ      Restore             ; last word was received
140
141            ; Write Word two--------------------------------------------------
142W2          BIT     #JMBIN1FG,&SYSJMBC  ; Data available in mailbox register 0?
143            JZ      W2                  ; No, wait for data
144
145            MOV.W   &SYSJMBI1,R14       ; move data into temp Register14
146            BIC     #JMBIN1FG,&SYSJMBC   ; clear the mailbox input flag
147
148            ; test if previous word was already written
149L3          BIT     #BUSY,&FCTL3
150            JNZ     L3
151            MOV     #FWPW,&FCTL1        ; Done. Clear WRT
152
153            ; Flash ready for new data -> Write Word two
154            MOV     #FWPW+WRT,&FCTL1    ; Enable write
155            MOV.W   R14,0(R10)         ; WRITE, move data into Flash
156
157            INCDX.A R10                 ; count up adress
158            DECX.A  R11                 ; decrement length
159
160            JNZ     W1
161
162
163    ;MOV       #FWKEY+BLKWRT,&FCTL1          ; configure Long-Word Write access
164
165            ;  MOV.W   &SYSJMBI0,0(R10)      ; WRITE, move data into Flash
166
167            ;  INCDX.A R10                   ; count up adress
168            ;  DECX.A  R11                   ; decrement length
169
170            ;  MOV.W   &SYSJMBI1,0(R10)      ; WRITE, move data into Flash
171
172    ;       Next     BIC     #JMBIN1FG,&SYSJMBC    ; clear the mailbox input flag
173    ;       BIC     #JMBIN0FG,&SYSJMBC    ; clear the mailbox input flag
174#endif
175;L2         BIT     #BUSY,&FCTL3
176  ;         JNZ     L2
177  ;        //MOV     #FWKEY,&FCTL1         ; disable word-write access
178
179  ;        INCDX.A R10                   ; count up adress
180  ;        DECX.A  R11                   ; decrement length
181
182  ;        JNZ     W1
183
184;--- [4] Restore Context Section -----------------------------------------------
185Restore
186
187L4          BIT     #BUSY,&FCTL3    ; wait for ast word wirrten
188            JNZ     L4
189
190            BIC    #JMBIN1FG,&SYSJMBC           ; clear the mailbox input flag
191            BIC    #JMBIN0FG,&SYSJMBC           ; clear the mailbox input flag
192
193RestoreR10  MOVX.A  MyR10,R10
194RestoreR11  MOVX.A  MyR11,R11
195
196RestoreFCTL ; restore Flash Controller Configuration
197            XOR     #0x3300,MyFlash1      ; restore password
198            XOR     #0x3300,MyFlash3      ; restore password
199            MOV     MyFlash1,&FCTL1
200            MOV     MyFlash3,&FCTL3
201RestoreLockState
202            MOV     &FCTL3,MyLockTst      ; Read out register again
203            CMP.B   MyFlash3,MyLockTst    ; Is content as expected?
204            JEQ     NotifyDriverStop      ; Yes, continue
205            BIS     #LOCKA,MyFlash3       ; No, set LOCKA bit to be toggled
206            MOV     MyFlash3,&FCTL3       ; Assign value again - LOCKA will be toggled now
207
208;--- [5] Write Routine End -----------------------------------------------------
209NotifyDriverStop
210            MOV     #0xCAFE,&SYSJMBO1
211            MOV     #0xBABE,&SYSJMBO0
212TheEnd
213#ifdef SUBROUTINE
214            RETA
215#else
216            JMP     TheEnd
217#endif
218            END
219