This is the the starting version of autograder hardwrae

Dependencies:   mbed

Fork of AutoGrader_HW_01 by Gopal Nair

Committer:
naray23
Date:
Thu Sep 03 07:47:32 2015 +0000
Revision:
2:b3286d232452
Parent:
1:97884b766b51
This takes care of PWM an PORT pins; PWM period frequency is limted to 100hz to 100Khz

Who changed what in which revision?

UserRevisionLine numberNew contents of line
naray23 0:440ae480bff2 1 #include "mbed.h"
naray23 0:440ae480bff2 2
naray23 1:97884b766b51 3 Ticker Read_Port; //Timer Polling
naray23 1:97884b766b51 4
naray23 1:97884b766b51 5 DigitalOut Reset(PTC12); //ptc12 reset
naray23 1:97884b766b51 6 Serial Comm(USBTX, USBRX);
naray23 0:440ae480bff2 7
naray23 0:440ae480bff2 8 #define RX_BUFF_SIZE 64
naray23 0:440ae480bff2 9
naray23 0:440ae480bff2 10 int rx_write_ptr=0;
naray23 0:440ae480bff2 11 int rx_read_ptr=0;
naray23 0:440ae480bff2 12 int RX_buff[RX_BUFF_SIZE];
naray23 0:440ae480bff2 13
naray23 0:440ae480bff2 14 int Data_avl;
naray23 0:440ae480bff2 15 int Port_Data;
naray23 1:97884b766b51 16 int Port_Data_prev=0;
naray23 1:97884b766b51 17 int Time_cntr=0;
naray23 0:440ae480bff2 18
naray23 2:b3286d232452 19
naray23 2:b3286d232452 20 unsigned int ON_period;
naray23 2:b3286d232452 21 unsigned int OFF_period;
naray23 2:b3286d232452 22 unsigned int Capturestate=0;
naray23 2:b3286d232452 23
naray23 2:b3286d232452 24
naray23 2:b3286d232452 25
naray23 0:440ae480bff2 26 void Serial_Read_callback()
naray23 0:440ae480bff2 27 {
naray23 0:440ae480bff2 28 RX_buff[rx_write_ptr++]=Comm.getc();
naray23 0:440ae480bff2 29 if(rx_write_ptr >= RX_BUFF_SIZE)
naray23 0:440ae480bff2 30 rx_write_ptr=0;
naray23 0:440ae480bff2 31
naray23 0:440ae480bff2 32 }
naray23 0:440ae480bff2 33
naray23 0:440ae480bff2 34 int Serial_Read()
naray23 0:440ae480bff2 35 {
naray23 0:440ae480bff2 36 int temp;
naray23 0:440ae480bff2 37
naray23 0:440ae480bff2 38 temp=RX_buff[rx_read_ptr++];
naray23 0:440ae480bff2 39 if(rx_read_ptr >= RX_BUFF_SIZE)
naray23 0:440ae480bff2 40 rx_read_ptr=0;
naray23 0:440ae480bff2 41 return temp;
naray23 0:440ae480bff2 42 }
naray23 0:440ae480bff2 43
naray23 0:440ae480bff2 44 int Serial_Available()
naray23 0:440ae480bff2 45 {
naray23 0:440ae480bff2 46 if(rx_read_ptr != rx_write_ptr)
naray23 0:440ae480bff2 47 return 1;
naray23 0:440ae480bff2 48 else
naray23 0:440ae480bff2 49 return 0;
naray23 0:440ae480bff2 50 }
naray23 0:440ae480bff2 51
naray23 0:440ae480bff2 52 void Read_Port_callback()
naray23 0:440ae480bff2 53 {
naray23 2:b3286d232452 54 Port_Data= PTC->PDIR & 0xFF;
naray23 1:97884b766b51 55 if((Port_Data ^ Port_Data_prev) != 0)
naray23 1:97884b766b51 56 {
naray23 1:97884b766b51 57 Data_avl=1;
naray23 1:97884b766b51 58 Port_Data_prev=Port_Data;
naray23 1:97884b766b51 59 }
naray23 1:97884b766b51 60 Time_cntr++;
naray23 0:440ae480bff2 61 }
naray23 0:440ae480bff2 62
naray23 2:b3286d232452 63 static void Input_Capture_ISR(void)
naray23 2:b3286d232452 64 {
naray23 2:b3286d232452 65 TPM0->STATUS |= TPM_STATUS_CH0F_MASK;
naray23 2:b3286d232452 66 if(Capturestate == 0) //Rising Edge start the capture
naray23 2:b3286d232452 67 {
naray23 2:b3286d232452 68 OFF_period = TPM0->CONTROLS[0].CnV;
naray23 2:b3286d232452 69 TPM0->CNT = 0;//Clear the counter
naray23 2:b3286d232452 70 TPM0->CONTROLS[0].CnSC &= ~(TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK);
naray23 2:b3286d232452 71 TPM0->CONTROLS[0].CnSC |= TPM_CnSC_ELSB_MASK;
naray23 2:b3286d232452 72 TPM0->CONTROLS[0].CnSC &= ~TPM_CnSC_ELSA_MASK;
naray23 2:b3286d232452 73 Capturestate =1;//Wait for the Falling Edge
naray23 2:b3286d232452 74 Data_avl=1;
naray23 2:b3286d232452 75 }
naray23 2:b3286d232452 76 else if(Capturestate == 1)
naray23 2:b3286d232452 77 {
naray23 2:b3286d232452 78 ON_period = TPM0->CONTROLS[0].CnV;
naray23 2:b3286d232452 79 TPM0->CNT = 0;//Clear the counter
naray23 2:b3286d232452 80 TPM0->CONTROLS[0].CnSC &= ~(TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK);
naray23 2:b3286d232452 81 TPM0->CONTROLS[0].CnSC |= TPM_CnSC_ELSA_MASK;
naray23 2:b3286d232452 82 TPM0->CONTROLS[0].CnSC &= ~TPM_CnSC_ELSB_MASK;
naray23 2:b3286d232452 83 Capturestate =0;//Wait for the Falling Edge
naray23 2:b3286d232452 84 Data_avl=1;
naray23 2:b3286d232452 85 }
naray23 2:b3286d232452 86
naray23 2:b3286d232452 87 }
naray23 2:b3286d232452 88
naray23 2:b3286d232452 89 void Port_Capture_Init(void)
naray23 2:b3286d232452 90 {
naray23 2:b3286d232452 91 unsigned int i;
naray23 2:b3286d232452 92 for(i=0;i<8;i++)//Make all the pins digital I/O
naray23 2:b3286d232452 93 {
naray23 2:b3286d232452 94 PORTC->PCR[i] |= PORT_PCR_MUX(1)| PORT_PCR_PE_MASK ;
naray23 2:b3286d232452 95 PTC->PDDR &= ~(GPIO_PDDR_PDD(i));//Make all the Pins Input
naray23 2:b3286d232452 96 }
naray23 2:b3286d232452 97 }
naray23 2:b3286d232452 98 void Input_Capture_Init(void)
naray23 2:b3286d232452 99 {
naray23 2:b3286d232452 100 SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK;
naray23 2:b3286d232452 101 PORTC->PCR[1] |= PORT_PCR_MUX(4)| PORT_PCR_PE_MASK ;
naray23 2:b3286d232452 102 SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1); //Enable the Timer Clock to MCGFLLCLK
naray23 2:b3286d232452 103 //SIM->SOPT2 &= ~(SIM_SOPT2_PLLFLLSEL_MASK);//Enable MCGFLLCLK
naray23 2:b3286d232452 104 SIM->SCGC6 |= SIM_SCGC6_TPM0_MASK; // Clock TPM0
naray23 2:b3286d232452 105
naray23 2:b3286d232452 106
naray23 2:b3286d232452 107 TPM0->MOD =0xFFFF;//Maximum Value of MOD
naray23 2:b3286d232452 108 TPM0->CNT = 0;//Clear the counter
naray23 2:b3286d232452 109
naray23 2:b3286d232452 110
naray23 2:b3286d232452 111 TPM0->SC |= TPM_SC_TOF_MASK | TPM_SC_CMOD(1) |TPM_SC_PS(0);//Incrmenets on each clock and No clock divide
naray23 2:b3286d232452 112 //Channel 0 is used for Input Capture and INterrupt is Enabled
naray23 2:b3286d232452 113 TPM0->CONTROLS[0].CnSC |= TPM_CnSC_CHF_MASK | TPM_CnSC_CHIE_MASK | TPM_CnSC_ELSA_MASK ;//First is the rising edge
naray23 2:b3286d232452 114 TPM0->CONTROLS[0].CnSC &= ~(TPM_CnSC_MSB_MASK | TPM_CnSC_MSA_MASK | TPM_CnSC_ELSB_MASK);
naray23 2:b3286d232452 115
naray23 2:b3286d232452 116 //Interrupt is Enabled
naray23 2:b3286d232452 117 NVIC_SetVector(TPM0_IRQn, (uint32_t)Input_Capture_ISR);
naray23 2:b3286d232452 118 NVIC_EnableIRQ(TPM0_IRQn);
naray23 2:b3286d232452 119 }
naray23 2:b3286d232452 120
naray23 2:b3286d232452 121 void Input_Capture_Disable()
naray23 2:b3286d232452 122 {
naray23 2:b3286d232452 123 NVIC_DisableIRQ(TPM0_IRQn);
naray23 2:b3286d232452 124 }
naray23 2:b3286d232452 125
naray23 2:b3286d232452 126
naray23 0:440ae480bff2 127 int main()
naray23 0:440ae480bff2 128 {
naray23 0:440ae480bff2 129 int ch;
naray23 0:440ae480bff2 130 int state=0;
naray23 1:97884b766b51 131 int cmd=0;;
naray23 1:97884b766b51 132 int paramval[3][3];
naray23 1:97884b766b51 133 int paramstate=0;
naray23 1:97884b766b51 134 int paramdigcnt=0;
naray23 1:97884b766b51 135 int new_frame=0;
naray23 1:97884b766b51 136 int run_debug=0;
naray23 1:97884b766b51 137
naray23 1:97884b766b51 138 float scan_time;
naray23 2:b3286d232452 139 int value_cnt=0;
naray23 2:b3286d232452 140 int command_mode=0;
naray23 2:b3286d232452 141
naray23 0:440ae480bff2 142 Comm.baud(115200);
naray23 0:440ae480bff2 143 Comm.attach(&Serial_Read_callback);
naray23 0:440ae480bff2 144
naray23 0:440ae480bff2 145 while(true)
naray23 0:440ae480bff2 146 {
naray23 0:440ae480bff2 147 if(Serial_Available())
naray23 0:440ae480bff2 148 {
naray23 0:440ae480bff2 149 ch=Serial_Read();
naray23 1:97884b766b51 150 Comm.printf("%c",ch,state);
naray23 0:440ae480bff2 151 switch (state)
naray23 0:440ae480bff2 152 {
naray23 0:440ae480bff2 153 case 0: if(ch == '<')
naray23 1:97884b766b51 154 {
naray23 0:440ae480bff2 155 state++;
naray23 1:97884b766b51 156 paramstate=0;;
naray23 1:97884b766b51 157 }
naray23 1:97884b766b51 158
naray23 0:440ae480bff2 159 break;
naray23 0:440ae480bff2 160
naray23 0:440ae480bff2 161 case 1: cmd = ch;
naray23 0:440ae480bff2 162 state++;
naray23 0:440ae480bff2 163 break;
naray23 0:440ae480bff2 164
naray23 1:97884b766b51 165 case 2: if(ch == ',')
naray23 1:97884b766b51 166 {
naray23 1:97884b766b51 167 paramstate++;
naray23 1:97884b766b51 168 paramdigcnt=2;
naray23 1:97884b766b51 169 }
naray23 1:97884b766b51 170 else if(ch == '>')
naray23 0:440ae480bff2 171 {
naray23 0:440ae480bff2 172 state=0;
naray23 0:440ae480bff2 173 new_frame =1;
naray23 1:97884b766b51 174
naray23 1:97884b766b51 175 }
naray23 1:97884b766b51 176 else
naray23 1:97884b766b51 177 {
naray23 2:b3286d232452 178 paramval[paramstate-1][paramdigcnt--]=ch - '0';
naray23 0:440ae480bff2 179 }
naray23 0:440ae480bff2 180 break;
naray23 0:440ae480bff2 181
naray23 0:440ae480bff2 182
naray23 0:440ae480bff2 183 }//switch
naray23 0:440ae480bff2 184
naray23 0:440ae480bff2 185
naray23 0:440ae480bff2 186 }//Serial_available
naray23 0:440ae480bff2 187
naray23 0:440ae480bff2 188 if(new_frame)
naray23 0:440ae480bff2 189 {
naray23 1:97884b766b51 190 Comm.printf("\n\rStarted polling Command=%c \n\r",cmd);
naray23 0:440ae480bff2 191 new_frame =0;
naray23 1:97884b766b51 192 Port_Data_prev=0;
naray23 1:97884b766b51 193 Time_cntr=0;
naray23 0:440ae480bff2 194 if(cmd == 'P')
naray23 0:440ae480bff2 195 {
naray23 2:b3286d232452 196 Port_Capture_Init();
naray23 2:b3286d232452 197 scan_time= (paramval[0][0] +paramval[0][1]* 10 + paramval[0][2] * 100)*0.01;
naray23 1:97884b766b51 198 Comm.printf("Scan Time %f mS \n\r",scan_time*1000);
naray23 1:97884b766b51 199 Read_Port.attach(&Read_Port_callback,scan_time);
naray23 0:440ae480bff2 200 Reset=0;
naray23 0:440ae480bff2 201 wait(0.01);
naray23 0:440ae480bff2 202 Reset=1;
naray23 0:440ae480bff2 203 run_debug=1;
naray23 2:b3286d232452 204 command_mode=1;
naray23 2:b3286d232452 205 }
naray23 2:b3286d232452 206 else if(cmd == 'W')
naray23 2:b3286d232452 207 {
naray23 2:b3286d232452 208 value_cnt = (paramval[0][0] + paramval[0][1]* 10 + paramval[0][2] * 100);
naray23 2:b3286d232452 209 Input_Capture_Init();
naray23 2:b3286d232452 210 run_debug=1;
naray23 2:b3286d232452 211 command_mode=2;
naray23 0:440ae480bff2 212 }
naray23 0:440ae480bff2 213 else if(cmd == 'X')
naray23 0:440ae480bff2 214 {
naray23 0:440ae480bff2 215 Read_Port.detach();
naray23 2:b3286d232452 216 Input_Capture_Disable();
naray23 0:440ae480bff2 217 run_debug=0;
naray23 0:440ae480bff2 218 }
naray23 0:440ae480bff2 219
naray23 0:440ae480bff2 220 }
naray23 2:b3286d232452 221
naray23 0:440ae480bff2 222 if(run_debug)
naray23 0:440ae480bff2 223 {
naray23 2:b3286d232452 224
naray23 0:440ae480bff2 225 if(Data_avl)
naray23 0:440ae480bff2 226 {
naray23 2:b3286d232452 227 if(command_mode == 1)
naray23 2:b3286d232452 228 {
naray23 2:b3286d232452 229 Comm.printf("<%.4X,%.2X>\n\r",Time_cntr,Port_Data);
naray23 2:b3286d232452 230 Data_avl=0;
naray23 2:b3286d232452 231 Time_cntr=0;
naray23 2:b3286d232452 232 }
naray23 2:b3286d232452 233 else if (command_mode == 2 )
naray23 2:b3286d232452 234 {
naray23 2:b3286d232452 235 if(value_cnt-- != 0)
naray23 2:b3286d232452 236 {
naray23 2:b3286d232452 237 Comm.printf("<%.4X,%.4X,%.4X>\n\r",value_cnt,ON_period,OFF_period);
naray23 2:b3286d232452 238 Data_avl=0;
naray23 2:b3286d232452 239 }
naray23 2:b3286d232452 240 else
naray23 2:b3286d232452 241 {
naray23 2:b3286d232452 242 Input_Capture_Disable();
naray23 2:b3286d232452 243 }
naray23 2:b3286d232452 244 }
naray23 0:440ae480bff2 245 }
naray23 0:440ae480bff2 246 }
naray23 0:440ae480bff2 247
naray23 0:440ae480bff2 248 }//while
naray23 0:440ae480bff2 249 }