5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
23 int8_t volumeRequired
;
24 uint8_t coprocReadDataPending
;
25 uint8_t coprocWriteDataPending
;
26 uint8_t CoProc_appgo_pending
;
30 int8_t Coproc_maxtemp
=-127 ;
34 // TODO not used? static uint8_t *Twi_read_address ;
36 static uint8_t TwiOperation
;
39 #define TWI_WRITE_VOL 2
40 #define TWI_READ_COPROC 3
41 #define TWI_COPROC_APPGO 4
42 #define TWI_WAIT_STOP 5
43 #define TWI_WRITE_COPROC 6
45 // Commands to the coprocessor bootloader/application
46 #define TWI_CMD_PAGEUPDATE 0x01 // TWI Command to program a flash page
47 #define TWI_CMD_EXECUTEAPP 0x02 // TWI Command to jump to the application program
48 #define TWI_CMD_SETREAD_ADDRESS 0x03 // TWI Command to set address to read from
49 #define TWI_CMD_WRITE_DATA 0x04 // TWI Command send data to the application
50 #define COPROC_RX_BUXSIZE 22
51 uint8_t Co_proc_status
[COPROC_RX_BUXSIZE
] ;
52 uint8_t *coprocWriteDataPtr
;
53 uint32_t coprocWriteDataSize
;
56 // This is called from an interrupt routine, or
57 // interrupts must be disabled while it is called
61 if ( TWI0
->TWI_IMR
& TWI_IMR_TXCOMP
) {
65 if ( volumeRequired
>= 0 ) { // Set volume to this value
66 TWI0
->TWI_MMR
= 0x002F0000 ; // Device 5E (>>1) and master is writing
67 TwiOperation
= TWI_WRITE_VOL
;
68 TWI0
->TWI_THR
= volumeRequired
; // Send data
70 TWI0
->TWI_IER
= TWI_IER_TXCOMP
;
71 TWI0
->TWI_CR
= TWI_CR_STOP
; // Stop Tx
73 else if (coprocReadDataPending
) {
75 coprocReadDataPending
= 0 ;
76 TWI0
->TWI_MMR
= 0x00351000 ; // Device 35 and master is reading
77 TwiOperation
= TWI_READ_COPROC
;
79 TWI0
->TWI_RPR
= (uint32_t)&Co_proc_status
[0] ;
81 TWI0
->TWI_RCR
= COPROC_RX_BUXSIZE
- 1 ;
82 if ( TWI0
->TWI_SR
& TWI_SR_RXRDY
) {
83 (void) TWI0
->TWI_RHR
;
85 TWI0
->TWI_PTCR
= TWI_PTCR_RXTEN
; // Start transfers
86 TWI0
->TWI_CR
= TWI_CR_START
; // Start Rx
87 TWI0
->TWI_IER
= TWI_IER_RXBUFF
| TWI_IER_TXCOMP
;
89 else if ( CoProc_appgo_pending
) {
90 CoProc_appgo_pending
= 0 ;
91 TWI0
->TWI_MMR
= 0x00350000 ; // Device 35 and master is writing
92 TwiOperation
= TWI_COPROC_APPGO
;
93 TWI0
->TWI_THR
= TWI_CMD_EXECUTEAPP
; // Send appgo command
94 TWI0
->TWI_IER
= TWI_IER_TXCOMP
;
95 TWI0
->TWI_CR
= TWI_CR_STOP
; // Stop Tx
97 else if ( coprocWriteDataPending
) {
98 coprocWriteDataPending
= 0 ;
99 TWI0
->TWI_MMR
= 0x00350000 ; // Device 35 and master is writing
100 TwiOperation
= TWI_WRITE_COPROC
;
102 TWI0
->TWI_TPR
= (uint32_t)coprocWriteDataPtr
;
104 TWI0
->TWI_TCR
= coprocWriteDataSize
;
105 TWI0
->TWI_THR
= TWI_CMD_WRITE_DATA
; // Send write command
106 TWI0
->TWI_PTCR
= TWI_PTCR_TXTEN
; // Start data transfer
107 TWI0
->TWI_IER
= TWI_IER_TXBUFE
| TWI_IER_TXCOMP
;
111 void coprocReadData(bool onlytemp
)
113 get_onlytemp
= onlytemp
;
114 coprocReadDataPending
= 1 ;
120 void coprocWriteData(uint8_t *data
, uint32_t size
)
122 coprocWriteDataPtr
= data
;
123 coprocWriteDataSize
= size
;
124 coprocWriteDataPending
= 1;
131 extern "C" void TWI0_IRQHandler()
133 if ( TwiOperation
== TWI_READ_COPROC
)
135 if ( TWI0
->TWI_SR
& TWI_SR_RXBUFF
)
137 TWI0
->TWI_IDR
= TWI_IDR_RXBUFF
;
138 TwiOperation
= TWI_WAIT_STOP
;
139 TWI0
->TWI_CR
= TWI_CR_STOP
; // Stop Rx
140 TWI0
->TWI_RCR
= 1 ; // Last byte
149 if ( TwiOperation
== TWI_WAIT_STOP
)
152 Coproc_read
= Co_proc_status
[0] ;
153 if ( Coproc_read
& 0x80 ) {
155 CoProc_appgo_pending
= 1 ; // Action application
158 // Got data from tiny app
159 // Set the date and time
162 utm
.tm_sec
= Co_proc_status
[1] ;
163 utm
.tm_min
= Co_proc_status
[2] ;
164 utm
.tm_hour
= Co_proc_status
[3] ;
165 utm
.tm_mday
= Co_proc_status
[4] ;
166 utm
.tm_mon
= Co_proc_status
[5] - 1;
167 utm
.tm_year
= (Co_proc_status
[6] + ( Co_proc_status
[7] << 8 )) - TM_YEAR_BASE
;
168 g_rtcTime
= gmktime(&utm
);
170 Coproc_temp
= Co_proc_status
[8];
171 if (Coproc_temp
> Coproc_maxtemp
)
172 Coproc_maxtemp
=Coproc_temp
;
174 TWI0
->TWI_PTCR
= TWI_PTCR_RXTDIS
; // Stop transfers
175 if ( TWI0
->TWI_SR
& TWI_SR_RXRDY
) {
176 (void) TWI0
->TWI_RHR
; // Discard any rubbish data
180 // if ( TwiOperation == TWI_WRITE_VOL )
185 if ( TwiOperation
== TWI_WRITE_COPROC
)
187 if ( TWI0
->TWI_SR
& TWI_SR_TXBUFE
)
189 TWI0
->TWI_IDR
= TWI_IDR_TXBUFE
;
190 TWI0
->TWI_CR
= TWI_CR_STOP
; // Stop Tx
191 TWI0
->TWI_PTCR
= TWI_PTCR_TXTDIS
; // Stop transfers
192 TwiOperation
= TWI_NONE
;
197 TWI0
->TWI_IDR
= TWI_IDR_TXCOMP
| TWI_IDR_TXBUFE
| TWI_IDR_RXBUFF
;
198 TWI0
->TWI_PTCR
= TWI_PTCR_TXTDIS
| TWI_PTCR_RXTDIS
; // Stop transfers
199 if ( TWI0
->TWI_SR
& TWI_SR_NACK
)
202 TwiOperation
= TWI_NONE
;