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 #include "pic24_all.h"
00031
00039 #define CONFIG_SLAVE_ENABLE() CONFIG_RB3_AS_DIG_OUTPUT()
00040 #define SLAVE_ENABLE() _LATB3 = 0 //low true assertion
00041 #define SLAVE_DISABLE() _LATB3 = 1
00042
00043 #define CONFIG_SLAVE_ORDY() CONFIG_RB2_AS_DIG_INPUT()
00044 #define SLAVE_ORDY _RB2
00045
00046 void configSPI1(void) {
00047
00048 SPI1CON1 = SEC_PRESCAL_1_1 |
00049 PRI_PRESCAL_4_1 |
00050 CLK_POL_ACTIVE_HIGH |
00051 SPI_CKE_ON |
00052 SPI_MODE8_ON |
00053 MASTER_ENABLE_ON;
00054
00055 CONFIG_SDO1_TO_RP(6);
00056 CONFIG_RP6_AS_DIG_PIN();
00057 CONFIG_SCK1OUT_TO_RP(7);
00058 CONFIG_RP7_AS_DIG_PIN();
00059 CONFIG_SDI1_TO_RP(5);
00060 CONFIG_RP5_AS_DIG_PIN();
00061 CONFIG_SLAVE_ENABLE();
00062 CONFIG_SLAVE_ORDY();
00063 SLAVE_DISABLE();
00064 SPI1STATbits.SPIEN = 1;
00065 }
00066
00067 typedef enum {
00068 STATE_GET_IN_STRING = 0,
00069 STATE_GET_REV_STRING,
00070 } STATE;
00071
00072 void sendStringSPI1(char* psz_s1) {
00073 SLAVE_ENABLE();
00074 while (*psz_s1) {
00075 ioMasterSPI1(*psz_s1);
00076 psz_s1++;
00077 }
00078 ioMasterSPI1(*psz_s1);
00079 SLAVE_DISABLE();
00080 }
00081
00082 void getStringSPI1(char* psz_s1, uint16 u16_maxCount) {
00083 uint16 u16_i = 0;
00084 if (!u16_maxCount) return;
00085 SLAVE_ENABLE();
00086 do {
00087 *psz_s1 = ioMasterSPI1(0);
00088 psz_s1++;
00089 u16_i++;
00090 } while (*(psz_s1-1) && (u16_i <u16_maxCount));
00091 SLAVE_DISABLE();
00092 psz_s1--;
00093 *psz_s1 = 0;
00094 }
00095
00096
00097 #define BUFSIZE 63
00098 char sz_1[BUFSIZE+1];
00099
00100 int main (void) {
00101 STATE e_mystate;
00102 configBasic(HELLO_MSG);
00103 configSPI1();
00104 e_mystate = STATE_GET_IN_STRING;
00105 while (1) {
00106 switch (e_mystate) {
00107 case STATE_GET_IN_STRING:
00108 inStringEcho(sz_1,BUFSIZE);
00109 if (*sz_1) {
00110 sendStringSPI1(sz_1);
00111 e_mystate = STATE_GET_REV_STRING;
00112 }
00113 break;
00114 case STATE_GET_REV_STRING:
00115 if (SLAVE_ORDY) {
00116 getStringSPI1(sz_1,BUFSIZE+1);
00117 outString(sz_1);
00118 outString("\n");
00119 e_mystate = STATE_GET_IN_STRING;
00120 }
00121 break;
00122 default:
00123 e_mystate = STATE_GET_IN_STRING;
00124 }
00125 doHeartbeat();
00126 }
00127 }