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
00030
00036
00037 #include "esos_pic24_i2c.h"
00038
00039
00040 struct stTask __stChildTaskI2C, __stGrandChildTaskI2C;
00041 uint8 __esos_i2c_dataBytes[2];
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00065 void esos_pic24_configI2C1(uint16 u16_fKHz) {
00066 uint32 u32_temp;
00067
00068 u32_temp = (FCY/1000L)/((uint32) u16_fKHz);
00069 u32_temp = u32_temp - FCY/10000000L - 1;
00070 I2C1BRG = u32_temp;
00071 I2C1CONbits.I2CEN = 1;
00072 }
00073
00074 ESOS_CHILD_TASK( __esos_pic24_getI2C1, uint8* pu8_x, uint8 u8_ack2Send) {
00075 static uint8 u8_tempAck;
00076
00077 ESOS_TASK_BEGIN();
00078 u8_tempAck = u8_ack2Send;
00079
00080 ESOS_TASK_WAIT_WHILE(I2C1CON & 0x1F);
00081 I2C1CONbits.RCEN = 1;
00082 ESOS_TASK_WAIT_UNTIL(I2C1STATbits.RBF);
00083 *pu8_x = I2C1RCV;
00084
00085 ESOS_TASK_WAIT_WHILE(I2C1CON & 0x1F);
00086 I2C1CONbits.ACKDT = u8_tempAck;
00087 I2C1CONbits.ACKEN = 1;
00088 ESOS_TASK_WAIT_WHILE(I2C1CONbits.ACKEN);
00089 ESOS_TASK_END();
00090 }
00091
00098 ESOS_CHILD_TASK( __esos_pic24_writeNI2C1, uint8 u8_addr, uint8* pu8_d, uint16 u16_cnt) {
00099 static uint8 u8_tempAddr;
00100 static uint8* pu8_tempPtr;
00101 static uint16 u16_tempCnt, u16_i;
00102
00103 ESOS_TASK_BEGIN();
00104 u8_tempAddr=u8_addr; pu8_tempPtr=pu8_d; u16_tempCnt=u16_cnt;
00105 __PIC24_I2C1_START();
00106 __PIC24_I2C1_PUT(I2C_WADDR(u8_tempAddr));
00107 for (u16_i=0; u16_i < u16_tempCnt; u16_i++) {
00108 __PIC24_I2C1_PUT(*pu8_tempPtr);
00109 pu8_tempPtr++;
00110 }
00111 __PIC24_I2C1_STOP();
00112 ESOS_TASK_END();
00113 }
00114
00122 ESOS_CHILD_TASK( __esos_pic24_readNI2C1, uint8 u8_addr, uint8* pu8_d, uint16 u16_cnt) {
00123 static uint8 u8_tempAddr;
00124 static uint8* pu8_tempD;
00125 static uint16 u16_tempCnt, u16_i;
00126
00127 ESOS_TASK_BEGIN();
00128 u8_tempAddr=u8_addr; pu8_tempD=pu8_d; u16_tempCnt=u16_cnt;
00129 __PIC24_I2C1_START();
00130 __PIC24_I2C1_PUT(I2C_RADDR(u8_tempAddr));
00131 for (u16_i=0; u16_i < u16_tempCnt-1; u16_i++) {
00132 ESOS_TASK_WAIT_ON_GETI2C1(pu8_tempD, I2C_ACK);
00133 pu8_tempD++;
00134 }
00135 ESOS_TASK_WAIT_ON_GETI2C1(pu8_tempD, I2C_NAK);
00136 __PIC24_I2C1_STOP();
00137 ESOS_TASK_END();
00139 }
00140