|
| 1 | +/** |
| 2 | + * Copyright (C) 2009 Ubixum, Inc. |
| 3 | + * |
| 4 | + * This library is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU Lesser General Public |
| 6 | + * License as published by the Free Software Foundation; either |
| 7 | + * version 2.1 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This library is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public |
| 15 | + * License along with this library; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + **/ |
| 18 | +#include <stdio.h> |
| 19 | +#include <fx2regs.h> |
| 20 | +#include <fx2macros.h> |
| 21 | +#include <delay.h> |
| 22 | +#include <fx2ints.h> |
| 23 | +#include <i2c/i2c_utils.h> |
| 24 | + |
| 25 | +//DELETE once merged. |
| 26 | +void fast_uart(BYTE a); |
| 27 | + |
| 28 | + |
| 29 | +/************************************************** |
| 30 | +I2C declarations |
| 31 | +***************************************************/ |
| 32 | +//Buffer to load up the data and insert into |
| 33 | +//the i2c_client queue |
| 34 | +__xdata unsigned char write_addr[I2C_ADDR]; |
| 35 | +//This is the data buffer. |
| 36 | +__xdata unsigned char write_data[I2C_DATA]; |
| 37 | +//This is the address length |
| 38 | +__xdata unsigned char rx_addr_length; |
| 39 | +//This is the data length which needs to be read from or written to. |
| 40 | +__xdata unsigned char rx_data_length; |
| 41 | +//Implementation specific. Not needed for all examples. Used here |
| 42 | +//to read and write from different address locations. |
| 43 | +__xdata unsigned char wr_addr; |
| 44 | + |
| 45 | + |
| 46 | +void main() { |
| 47 | + SETCPUFREQ(CLK_48M); |
| 48 | + EA = 1; // global interrupt enable |
| 49 | + /******************************** |
| 50 | + I2C BLOCK BITBANG |
| 51 | + *********************************/ |
| 52 | + //Called with number of retries |
| 53 | + i2c_init(3); |
| 54 | + configure_start_timer(); |
| 55 | + ENABLE_TIMER1(); |
| 56 | + wr_addr = 0x03; |
| 57 | + while (TRUE) |
| 58 | + { |
| 59 | + |
| 60 | + write_addr[0] = 0xa0; |
| 61 | + write_addr[1] = 0x00; |
| 62 | + write_addr[2] = wr_addr; |
| 63 | + write_data[0] = 0x44; |
| 64 | + //Address, data, address length and data length |
| 65 | + I2CPutTX(&write_addr[0],&write_data[0],0x03,0x01); |
| 66 | + //i2c_control(); |
| 67 | + write_addr[0] = 0xa1; |
| 68 | + write_data[0] = 0x34; |
| 69 | + I2CPutRXRead(&write_addr[0],0x01,0x01); |
| 70 | + I2CGetRXData(&write_addr[0],&write_data[0]); |
| 71 | + fast_uart(data[0]); |
| 72 | + i2c_control(); |
| 73 | + if(wr_addr == 0x00) |
| 74 | + { |
| 75 | + wr_addr = 0x03; |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + wr_addr--; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +void timer1_isr () |
| 85 | +__interrupt TF1_ISR |
| 86 | +{ |
| 87 | + __asm |
| 88 | + mov a,_tx_rx //(2 cycles. Move the state into the accumulator.) |
| 89 | + CJNE A, #0x02, state //(4 cycles. If in halt state, do nothing.) |
| 90 | + ajmp finish //(3 cycles. Return from ISR if buffer is not ready to shift the data out.) |
| 91 | + state: |
| 92 | + djnz _bit_count,cont; //(4 cycles. This is sued to keep track of the number of data bytes which need to be shifted out.) |
| 93 | + mov _tx_rx,#0x02 //(3 cycles. Set our current status to busy so nothing else can interrupt us till we shift data in or out.) |
| 94 | + ajmp finish //(3 cycles. If data has been shifted, but has not been read, then jump to finish.) |
| 95 | + cont: |
| 96 | + orl _OEA,#0x40 //(3 cycles. First always set out clock pin direction. This is needed as discussed already in comments.) |
| 97 | + clr _PA6 //(2 cycles. Set the SCL line low.) |
| 98 | + mov a,_tx_rx //(2 cycles. Move the state back into the accumulator.) |
| 99 | + CJNE A, #0x00, rx //(4 cycles. Check if we are in RX or TX mode.) |
| 100 | + tx: |
| 101 | + orl _OEA,#0x80 //(3 cycles. We are in TX mode. Set the SDA direction.) |
| 102 | + mov a, _tx_i2c_buffer //(2 cycles. Move the data which needs to be transmitted into the accumulator.) |
| 103 | + rlc a //(1 cycles. Rotate and move the bit which needs to be sent out into the carry.) |
| 104 | + mov _PA7, c //(2 cycles. Move the data into the SDA pin.) |
| 105 | + mov _tx_i2c_buffer,a //(2 cycles. Now move the data back so that the next rotate can be performed when the ISR executes.) |
| 106 | + sjmp sclh //(3 cycles. Jump to toggle SCL now.) |
| 107 | + rx: |
| 108 | + anl _OEA,#0x7f //(3 cycles. Set the mode of SDA pin again.) |
| 109 | + mov a, _tx_i2c_buffer //(2 cycles. Move data received into accumulator.) |
| 110 | + mov c,_PA7 //(2 cycles. Move the SDA line value into the carry. Delay has already been introduced between setting clock low and reading the first bit of data. Otherwise, follow the MPSSE i2c routine.) |
| 111 | + rlc a //(1 cycle. Since the next bit will be received in the same ISR we need to shift the data.) |
| 112 | + mov _tx_i2c_buffer,a //(2 cycles. Move the data back into accumulator for next ISR execution routine) |
| 113 | + nop //(1 cycle. Wait for 1 cycle before setting the bit high again.) |
| 114 | + sclh: |
| 115 | + setb _PA6 //(2 cycles. Set the SCL high) |
| 116 | + finish: |
| 117 | + nop |
| 118 | + __endasm; |
| 119 | +} |
| 120 | + |
| 121 | +/************************** |
| 122 | +Will be DELETED once the pull requests are merged |
| 123 | +**************************/ |
| 124 | +void fast_uart(BYTE a) { |
| 125 | + OEA |= 0x04; |
| 126 | + //An efficient UART bitbang routine in assembly |
| 127 | + __asm |
| 128 | + //Like #define in C. Can easily be used to change the pin |
| 129 | + .equ TX_PIN, _PA2 |
| 130 | + //Disable interrupts |
| 131 | + clr _EA |
| 132 | + //Move the data to be sent into the ACC |
| 133 | + mov a , dpl |
| 134 | + //Clear carry |
| 135 | + clr c |
| 136 | + //We need to send out 8 bits of data |
| 137 | + //Load r0 with value 8 |
| 138 | + mov r0, #0x08; |
| 139 | + //Create the start bit |
| 140 | + clr TX_PIN; |
| 141 | + //Precalculated delay since 1 cycle takes 88.33ns |
| 142 | + //Mov takes 2 cycles |
| 143 | + mov r1, #0x22; |
| 144 | + 0006$: |
| 145 | + //DJNZ on Rn takes 3 cycle. This waits for |
| 146 | + //23*3, 69 cycles |
| 147 | + //71 cycle delay |
| 148 | + djnz r1, 0006$; |
| 149 | + //Add 2 more cycles of delay |
| 150 | + //97 cycles |
| 151 | + nop; |
| 152 | + nop; |
| 153 | + 0001$: |
| 154 | + //2 cycles |
| 155 | + rrc a; |
| 156 | + //Move the carry into the port |
| 157 | + mov _PA2, c |
| 158 | + //Now we need to add delay for the next |
| 159 | + //2 cycles of delay |
| 160 | + mov r1, #0x1F; |
| 161 | + //31*3 , 93 cycles of delay |
| 162 | + 0004$: |
| 163 | + djnz r1, 0004$; |
| 164 | + //1 more cycle, now upto 94 |
| 165 | + nop; |
| 166 | + //3 more cycles of delay |
| 167 | + //97 cycles |
| 168 | + djnz r0, 0001$; |
| 169 | + setb _PA2; |
| 170 | + mov r1, #0x80; |
| 171 | + 0005$: |
| 172 | + djnz r1, 0005$; |
| 173 | + nop |
| 174 | + setb _EA; |
| 175 | + __endasm; |
| 176 | +} |
0 commit comments