00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "pic24_all.h"
00030
00031
00038 #define CPU1 1
00039 #define CPU2 2
00040
00041 #if (CPU_ID == CPU1)
00042 #define SLAVE_I2C_ADDR 0x60
00043 #define MY_ADDR 0x68
00044 #else
00045 #define SLAVE_I2C_ADDR 0x68
00046 #define MY_ADDR 0x60
00047 #endif
00048
00049 typedef enum {
00050 STATE_WAIT_FOR_ADDR,
00051 STATE_WAIT_FOR_WRITE_DATA,
00052 } STATE;
00053
00054 volatile STATE e_mystate = STATE_WAIT_FOR_ADDR;
00055 #define BUFSIZE 64
00056 char sz_1[BUFSIZE];
00057 char sz_2[BUFSIZE];
00058 volatile uint8 u8_gotString = 0;
00059 volatile uint16 u16_index;
00060
00061 void _ISRFAST _SI2C1Interrupt(void) {
00062 uint8 u8_c;
00063 _SI2C1IF = 0;
00064 switch (e_mystate) {
00065 case STATE_WAIT_FOR_ADDR:
00066 u8_c = I2C1RCV;
00067 u16_index = 0;
00068 e_mystate = STATE_WAIT_FOR_WRITE_DATA;
00069 break;
00070 case STATE_WAIT_FOR_WRITE_DATA:
00071
00072 sz_1[u16_index++] = I2C1RCV;
00073 if (sz_1[u16_index-1] == 0) {
00074 u8_gotString = 1;
00075 e_mystate = STATE_WAIT_FOR_ADDR;
00076 }
00077 break;
00078 default:
00079 e_mystate = STATE_WAIT_FOR_ADDR;
00080 }
00081 }
00082
00083 int16 getStringLength(char* psz_1) {
00084 uint16 u16_len;
00085 u16_len = 0;
00086 while (*psz_1) {
00087 psz_1++;
00088 u16_len++;
00089 }
00090 u16_len++;
00091 return u16_len;
00092 }
00093
00094 void reverseString(char *psz_s1, char *psz_s2) {
00095 char *psz_s1end;
00096 if (!(*psz_s1)) {
00097 *psz_s2 = 0;
00098 return;
00099 }
00100 psz_s1end = psz_s1;
00101
00102 while (*psz_s1end) psz_s1end++;
00103 psz_s1end--;
00104
00105 while (psz_s1end != psz_s1) {
00106 *psz_s2 = *psz_s1end;
00107 psz_s1end--;
00108 psz_s2++;
00109 }
00110
00111 *psz_s2 = *psz_s1end;
00112 psz_s2++;
00113
00114 *psz_s2 = 0;
00115 }
00116
00117
00118 int main (void) {
00119 uint16 u16_len;
00120 configBasic(HELLO_MSG);
00121 configI2C1(400);
00122 I2C1ADD = MY_ADDR>>1;
00123 _SI2C1IF = 0;
00124 #ifdef _SI2C1IP
00125 _SI2C1IP = 1;
00126 #else
00127 #ifdef _SI2C1IP0
00128 _SI2C1IP0 = 1;
00129 _SI2C1IP1 = 0;
00130 _SI2C1IP2 = 0;
00131 #endif
00132 #endif
00133
00134 _SI2C1IE = 1;
00135 while (1) {
00136 u8_gotString = 0;
00137 #if (CPU_ID == CPU1)
00138 inStringEcho(sz_2,BUFSIZE);
00139 if (*sz_2 == 0) continue;
00140 u16_len = getStringLength(sz_2);
00141 writeNI2C1(SLAVE_I2C_ADDR,(uint8 *)sz_2,u16_len);
00142
00143 while (!u8_gotString) doHeartbeat();
00144 outString(sz_1);
00145 outString("\n");
00146 #else
00147 while (!u8_gotString) doHeartbeat();
00148 reverseString(sz_1,sz_2);
00149 u16_len=getStringLength(sz_2);
00150 writeNI2C1(SLAVE_I2C_ADDR,(uint8 *)sz_2,u16_len);
00151 #endif
00152
00153 }
00154 }
00155