![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
test
Dependencies: MODDMA MODSERIAL mbed
Fork of IRIS_MBED by
main.cpp@0:2676c97df44e, 2015-03-15 (annotated)
- Committer:
- JonathanAshworth
- Date:
- Sun Mar 15 21:48:23 2015 +0000
- Revision:
- 0:2676c97df44e
- Child:
- 1:ccecc816c5e8
Interrupt driven. Passed pre-check testing using a terminal program but now requires pc:mbed testing.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JonathanAshworth | 0:2676c97df44e | 1 | #include "mbed.h" |
JonathanAshworth | 0:2676c97df44e | 2 | #include "main.h" |
JonathanAshworth | 0:2676c97df44e | 3 | #include "RIT.h" |
JonathanAshworth | 0:2676c97df44e | 4 | |
JonathanAshworth | 0:2676c97df44e | 5 | #define TIMEOUT_MESSAGE "MBED detected a timeout - eStop!\n\n\n\n" |
JonathanAshworth | 0:2676c97df44e | 6 | #define USB_BUFFER_SIZE 1024 |
JonathanAshworth | 0:2676c97df44e | 7 | |
JonathanAshworth | 0:2676c97df44e | 8 | Serial serial(USBTX, USBRX); |
JonathanAshworth | 0:2676c97df44e | 9 | Timer usbTimer; |
JonathanAshworth | 0:2676c97df44e | 10 | RIT usbRIT(100); |
JonathanAshworth | 0:2676c97df44e | 11 | Flags flags; |
JonathanAshworth | 0:2676c97df44e | 12 | |
JonathanAshworth | 0:2676c97df44e | 13 | char txBuffer[USB_BUFFER_SIZE] = {0}; |
JonathanAshworth | 0:2676c97df44e | 14 | char rxBuffer[USB_BUFFER_SIZE] = {0}; |
JonathanAshworth | 0:2676c97df44e | 15 | |
JonathanAshworth | 0:2676c97df44e | 16 | int main(void) { |
JonathanAshworth | 0:2676c97df44e | 17 | |
JonathanAshworth | 0:2676c97df44e | 18 | flags.rxNewData = 0; |
JonathanAshworth | 0:2676c97df44e | 19 | flags.txSentData = 0; |
JonathanAshworth | 0:2676c97df44e | 20 | flags.usbTimeout = 0; |
JonathanAshworth | 0:2676c97df44e | 21 | |
JonathanAshworth | 0:2676c97df44e | 22 | serial.baud(115200); |
JonathanAshworth | 0:2676c97df44e | 23 | |
JonathanAshworth | 0:2676c97df44e | 24 | //NVIC_SetPriority(TIMER3_IRQn, 1);//priority 1 |
JonathanAshworth | 0:2676c97df44e | 25 | //NVIC_SetPriority(USB_IRQn, 2);//priority 2 |
JonathanAshworth | 0:2676c97df44e | 26 | |
JonathanAshworth | 0:2676c97df44e | 27 | NVIC_SetPriority( NonMaskableInt_IRQn, 100 ); |
JonathanAshworth | 0:2676c97df44e | 28 | NVIC_SetPriority(MemoryManagement_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 29 | |
JonathanAshworth | 0:2676c97df44e | 30 | NVIC_SetPriority(BusFault_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 31 | NVIC_SetPriority(UsageFault_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 32 | NVIC_SetPriority(SVCall_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 33 | NVIC_SetPriority(DebugMonitor_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 34 | NVIC_SetPriority(PendSV_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 35 | NVIC_SetPriority(SysTick_IRQn, 50); |
JonathanAshworth | 0:2676c97df44e | 36 | NVIC_SetPriority(WDT_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 37 | NVIC_SetPriority(TIMER0_IRQn, 85); |
JonathanAshworth | 0:2676c97df44e | 38 | NVIC_SetPriority(TIMER1_IRQn, 85); |
JonathanAshworth | 0:2676c97df44e | 39 | NVIC_SetPriority(TIMER2_IRQn, 85); |
JonathanAshworth | 0:2676c97df44e | 40 | NVIC_SetPriority(TIMER3_IRQn, 85); |
JonathanAshworth | 0:2676c97df44e | 41 | NVIC_SetPriority(UART0_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 42 | NVIC_SetPriority(UART1_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 43 | NVIC_SetPriority(UART2_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 44 | NVIC_SetPriority(UART3_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 45 | |
JonathanAshworth | 0:2676c97df44e | 46 | NVIC_SetPriority(PWM1_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 47 | NVIC_SetPriority(I2C0_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 48 | NVIC_SetPriority(I2C1_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 49 | NVIC_SetPriority(I2C2_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 50 | NVIC_SetPriority(SPI_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 51 | NVIC_SetPriority(SSP0_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 52 | NVIC_SetPriority(SSP1_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 53 | NVIC_SetPriority(PLL0_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 54 | NVIC_SetPriority(RTC_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 55 | NVIC_SetPriority(EINT0_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 56 | NVIC_SetPriority(EINT1_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 57 | |
JonathanAshworth | 0:2676c97df44e | 58 | NVIC_SetPriority(EINT2_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 59 | NVIC_SetPriority(EINT3_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 60 | NVIC_SetPriority(ADC_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 61 | NVIC_SetPriority(BOD_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 62 | NVIC_SetPriority(USB_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 63 | NVIC_SetPriority(CAN_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 64 | NVIC_SetPriority(DMA_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 65 | |
JonathanAshworth | 0:2676c97df44e | 66 | NVIC_SetPriority(I2S_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 67 | NVIC_SetPriority(ENET_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 68 | NVIC_SetPriority(RIT_IRQn, 1); |
JonathanAshworth | 0:2676c97df44e | 69 | NVIC_SetPriority(MCPWM_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 70 | NVIC_SetPriority(QEI_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 71 | NVIC_SetPriority(PLL1_IRQn, 100); |
JonathanAshworth | 0:2676c97df44e | 72 | |
JonathanAshworth | 0:2676c97df44e | 73 | serial.attach(&serialRx_ISR, Serial::RxIrq); |
JonathanAshworth | 0:2676c97df44e | 74 | usbRIT.append(&usbTimer_ISR); |
JonathanAshworth | 0:2676c97df44e | 75 | usbRIT.disable(); |
JonathanAshworth | 0:2676c97df44e | 76 | |
JonathanAshworth | 0:2676c97df44e | 77 | char responseData[USB_BUFFER_SIZE] = {0}; |
JonathanAshworth | 0:2676c97df44e | 78 | char requestData[USB_BUFFER_SIZE] = {0}; |
JonathanAshworth | 0:2676c97df44e | 79 | |
JonathanAshworth | 0:2676c97df44e | 80 | //LPC_TIMER3->; |
JonathanAshworth | 0:2676c97df44e | 81 | |
JonathanAshworth | 0:2676c97df44e | 82 | //setup ISRs |
JonathanAshworth | 0:2676c97df44e | 83 | |
JonathanAshworth | 0:2676c97df44e | 84 | while(1) { |
JonathanAshworth | 0:2676c97df44e | 85 | if (flags.usbTimeout) { |
JonathanAshworth | 0:2676c97df44e | 86 | usbTimeout(); |
JonathanAshworth | 0:2676c97df44e | 87 | } |
JonathanAshworth | 0:2676c97df44e | 88 | if (flags.rxNewData) { |
JonathanAshworth | 0:2676c97df44e | 89 | memcpy(requestData, rxBuffer, USB_BUFFER_SIZE); //requestData = rxBuffer; |
JonathanAshworth | 0:2676c97df44e | 90 | memset(rxBuffer, 0, USB_BUFFER_SIZE); //reset rx buffer |
JonathanAshworth | 0:2676c97df44e | 91 | |
JonathanAshworth | 0:2676c97df44e | 92 | //deconstruct and validate request |
JonathanAshworth | 0:2676c97df44e | 93 | |
JonathanAshworth | 0:2676c97df44e | 94 | //construct response: |
JonathanAshworth | 0:2676c97df44e | 95 | memcpy(responseData, requestData, USB_BUFFER_SIZE); //just echo request back for now |
JonathanAshworth | 0:2676c97df44e | 96 | |
JonathanAshworth | 0:2676c97df44e | 97 | memcpy(txBuffer, responseData, USB_BUFFER_SIZE); //txBuffer = responseData; |
JonathanAshworth | 0:2676c97df44e | 98 | |
JonathanAshworth | 0:2676c97df44e | 99 | serialTx(); |
JonathanAshworth | 0:2676c97df44e | 100 | |
JonathanAshworth | 0:2676c97df44e | 101 | flags.rxNewData = 0; |
JonathanAshworth | 0:2676c97df44e | 102 | } |
JonathanAshworth | 0:2676c97df44e | 103 | } |
JonathanAshworth | 0:2676c97df44e | 104 | } |
JonathanAshworth | 0:2676c97df44e | 105 | |
JonathanAshworth | 0:2676c97df44e | 106 | void serialTx (void) { |
JonathanAshworth | 0:2676c97df44e | 107 | __disable_irq(); |
JonathanAshworth | 0:2676c97df44e | 108 | char buffPntr; |
JonathanAshworth | 0:2676c97df44e | 109 | bool terminated = 0; |
JonathanAshworth | 0:2676c97df44e | 110 | for(buffPntr=0; buffPntr<4; buffPntr++) { |
JonathanAshworth | 0:2676c97df44e | 111 | serial.putc(txBuffer[buffPntr]); |
JonathanAshworth | 0:2676c97df44e | 112 | } |
JonathanAshworth | 0:2676c97df44e | 113 | while ((!terminated)&&(buffPntr != USB_BUFFER_SIZE-1)) { |
JonathanAshworth | 0:2676c97df44e | 114 | if ((txBuffer[buffPntr-3] == 0x0A)&&(txBuffer[buffPntr-2] == 0x0A)&&(txBuffer[buffPntr-1] == 0x0A)&&(txBuffer[buffPntr] == 0x0A)) { |
JonathanAshworth | 0:2676c97df44e | 115 | terminated = 1; |
JonathanAshworth | 0:2676c97df44e | 116 | } |
JonathanAshworth | 0:2676c97df44e | 117 | else { |
JonathanAshworth | 0:2676c97df44e | 118 | serial.putc(txBuffer[buffPntr]); |
JonathanAshworth | 0:2676c97df44e | 119 | } |
JonathanAshworth | 0:2676c97df44e | 120 | buffPntr++; |
JonathanAshworth | 0:2676c97df44e | 121 | } |
JonathanAshworth | 0:2676c97df44e | 122 | //send data from tx buffer |
JonathanAshworth | 0:2676c97df44e | 123 | memset(txBuffer, 0, USB_BUFFER_SIZE); //reset tx buffer |
JonathanAshworth | 0:2676c97df44e | 124 | flags.txSentData = 1; |
JonathanAshworth | 0:2676c97df44e | 125 | __enable_irq(); |
JonathanAshworth | 0:2676c97df44e | 126 | } |
JonathanAshworth | 0:2676c97df44e | 127 | |
JonathanAshworth | 0:2676c97df44e | 128 | void usbTimeout(void) { |
JonathanAshworth | 0:2676c97df44e | 129 | memcpy(txBuffer, TIMEOUT_MESSAGE, USB_BUFFER_SIZE); //send eStop over usb serial |
JonathanAshworth | 0:2676c97df44e | 130 | serialTx(); |
JonathanAshworth | 0:2676c97df44e | 131 | shutdown(); |
JonathanAshworth | 0:2676c97df44e | 132 | __disable_irq(); |
JonathanAshworth | 0:2676c97df44e | 133 | while(1) {;} //remain here until hardware reset |
JonathanAshworth | 0:2676c97df44e | 134 | } |
JonathanAshworth | 0:2676c97df44e | 135 | |
JonathanAshworth | 0:2676c97df44e | 136 | void shutdown(void) { |
JonathanAshworth | 0:2676c97df44e | 137 | //turn off each peripheral safely |
JonathanAshworth | 0:2676c97df44e | 138 | } |
JonathanAshworth | 0:2676c97df44e | 139 | |
JonathanAshworth | 0:2676c97df44e | 140 | |
JonathanAshworth | 0:2676c97df44e | 141 | /**************** ISRs ****************/ |
JonathanAshworth | 0:2676c97df44e | 142 | |
JonathanAshworth | 0:2676c97df44e | 143 | void serialRx_ISR(void) { |
JonathanAshworth | 0:2676c97df44e | 144 | char buffPntr = 0; |
JonathanAshworth | 0:2676c97df44e | 145 | bool terminated = 0; |
JonathanAshworth | 0:2676c97df44e | 146 | |
JonathanAshworth | 0:2676c97df44e | 147 | usbRIT.disable(); |
JonathanAshworth | 0:2676c97df44e | 148 | usbTimer.stop(); |
JonathanAshworth | 0:2676c97df44e | 149 | usbTimer.reset(); |
JonathanAshworth | 0:2676c97df44e | 150 | usbTimer.start(); |
JonathanAshworth | 0:2676c97df44e | 151 | usbRIT.enable(); |
JonathanAshworth | 0:2676c97df44e | 152 | if (!flags.rxNewData) { //prevent too short windows |
JonathanAshworth | 0:2676c97df44e | 153 | while ((buffPntr != USB_BUFFER_SIZE-1)&&(!flags.usbTimeout)) { |
JonathanAshworth | 0:2676c97df44e | 154 | if (buffPntr >= 3) { |
JonathanAshworth | 0:2676c97df44e | 155 | if ((rxBuffer[buffPntr-3] == 0x0A)&&(rxBuffer[buffPntr-2] == 0x0A)&&(rxBuffer[buffPntr-1] == 0x0A)&&(rxBuffer[buffPntr] == 0x0A)) { |
JonathanAshworth | 0:2676c97df44e | 156 | terminated = 1; |
JonathanAshworth | 0:2676c97df44e | 157 | } |
JonathanAshworth | 0:2676c97df44e | 158 | } |
JonathanAshworth | 0:2676c97df44e | 159 | if (!terminated) { |
JonathanAshworth | 0:2676c97df44e | 160 | while((!serial.readable())&&(!flags.usbTimeout)) {;} |
JonathanAshworth | 0:2676c97df44e | 161 | if (serial.readable()) { |
JonathanAshworth | 0:2676c97df44e | 162 | rxBuffer[buffPntr] = serial.getc();//whenDataReady, insert into rx buffer |
JonathanAshworth | 0:2676c97df44e | 163 | } |
JonathanAshworth | 0:2676c97df44e | 164 | } |
JonathanAshworth | 0:2676c97df44e | 165 | else { |
JonathanAshworth | 0:2676c97df44e | 166 | rxBuffer[buffPntr] = 0; |
JonathanAshworth | 0:2676c97df44e | 167 | } |
JonathanAshworth | 0:2676c97df44e | 168 | buffPntr++; |
JonathanAshworth | 0:2676c97df44e | 169 | } |
JonathanAshworth | 0:2676c97df44e | 170 | if (!terminated) { |
JonathanAshworth | 0:2676c97df44e | 171 | flags.usbTimeout = 1; //no valid termination seen |
JonathanAshworth | 0:2676c97df44e | 172 | } |
JonathanAshworth | 0:2676c97df44e | 173 | else { |
JonathanAshworth | 0:2676c97df44e | 174 | flags.rxNewData = 1; |
JonathanAshworth | 0:2676c97df44e | 175 | } |
JonathanAshworth | 0:2676c97df44e | 176 | } |
JonathanAshworth | 0:2676c97df44e | 177 | else { |
JonathanAshworth | 0:2676c97df44e | 178 | flags.usbTimeout = 1; //received another window too early |
JonathanAshworth | 0:2676c97df44e | 179 | } |
JonathanAshworth | 0:2676c97df44e | 180 | flags.rxNewData = 1; |
JonathanAshworth | 0:2676c97df44e | 181 | } |
JonathanAshworth | 0:2676c97df44e | 182 | |
JonathanAshworth | 0:2676c97df44e | 183 | void usbTimer_ISR(void) { |
JonathanAshworth | 0:2676c97df44e | 184 | usbRIT.disable(); |
JonathanAshworth | 0:2676c97df44e | 185 | usbTimer.stop(); |
JonathanAshworth | 0:2676c97df44e | 186 | if ((flags.rxNewData) && (flags.txSentData)) { |
JonathanAshworth | 0:2676c97df44e | 187 | usbTimer.reset(); |
JonathanAshworth | 0:2676c97df44e | 188 | usbTimer.start(); |
JonathanAshworth | 0:2676c97df44e | 189 | usbRIT.enable(); |
JonathanAshworth | 0:2676c97df44e | 190 | flags.rxNewData = 0; |
JonathanAshworth | 0:2676c97df44e | 191 | flags.txSentData = 0; |
JonathanAshworth | 0:2676c97df44e | 192 | } |
JonathanAshworth | 0:2676c97df44e | 193 | else { |
JonathanAshworth | 0:2676c97df44e | 194 | flags.usbTimeout = 1; |
JonathanAshworth | 0:2676c97df44e | 195 | } |
JonathanAshworth | 0:2676c97df44e | 196 | } |