Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP
Fork of SystemManagement by
SysMngmt.cpp@12:e0adb697fcdb, 2014-10-21 (annotated)
- Committer:
- martydd3
- Date:
- Tue Oct 21 23:44:15 2014 +0000
- Revision:
- 12:e0adb697fcdb
- Parent:
- 11:1d086618dd18
- Child:
- 13:fbd9b3f5a07c
Added Error update thread, changed CAN message IDs
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| martydd3 | 0:e516fcccccda | 1 | /* |
| martydd3 | 0:e516fcccccda | 2 | Reads CAN Messages and various data inputs, outputs using Xbee radio modules |
| martydd3 | 0:e516fcccccda | 3 | |
| martydd3 | 11:1d086618dd18 | 4 | Revised Oct 19, 2014: First team repository version |
| martydd3 | 10:db13782f05d9 | 5 | */ |
| martydd3 | 10:db13782f05d9 | 6 | |
| martydd3 | 6:6a04210a3f4f | 7 | #include "Store_RTC.h" |
| martydd3 | 6:6a04210a3f4f | 8 | #include "XBee_Lib.h" |
| martydd3 | 6:6a04210a3f4f | 9 | #include "CANBuffer.h" |
| martydd3 | 0:e516fcccccda | 10 | |
| martydd3 | 6:6a04210a3f4f | 11 | #include "mbed.h" |
| martydd3 | 6:6a04210a3f4f | 12 | #include "rtos.h" |
| martydd3 | 6:6a04210a3f4f | 13 | |
| martydd3 | 9:ada056631cac | 14 | #include "Watchdog.h" |
| martydd3 | 6:6a04210a3f4f | 15 | #include "FanPump.h" |
| martydd3 | 6:6a04210a3f4f | 16 | #include "DC_DC.h" |
| martydd3 | 7:5f6e31faa08e | 17 | #include "PollSwitch.h" |
| martydd3 | 8:ecf68db484af | 18 | #include "IMD.h" |
| martydd3 | 10:db13782f05d9 | 19 | #include "CurrentMonitor.h" |
| martydd3 | 10:db13782f05d9 | 20 | |
| martydd3 | 10:db13782f05d9 | 21 | CANBuffer rxBuffer(CAN1, MEDIUM); |
| martydd3 | 12:e0adb697fcdb | 22 | Watchdog wdt; |
| martydd3 | 11:1d086618dd18 | 23 | XBee250x XbeeTx; |
| martydd3 | 10:db13782f05d9 | 24 | Serial pc1(USBTX,USBRX); |
| martydd3 | 10:db13782f05d9 | 25 | |
| martydd3 | 12:e0adb697fcdb | 26 | char sys_src_id = 0x4; // source address of system management |
| martydd3 | 10:db13782f05d9 | 27 | char reset_id = 0x010; |
| martydd3 | 10:db13782f05d9 | 28 | |
| martydd3 | 12:e0adb697fcdb | 29 | int SYSMGMT_ERROR_ID = 0x400; |
| martydd3 | 12:e0adb697fcdb | 30 | |
| martydd3 | 10:db13782f05d9 | 31 | extern "C" void mbed_reset(); |
| martydd3 | 10:db13782f05d9 | 32 | |
| martydd3 | 10:db13782f05d9 | 33 | void soft_reset(){ |
| martydd3 | 10:db13782f05d9 | 34 | //http://developer.mbed.org/forum/mbed/topic/890/ |
| martydd3 | 10:db13782f05d9 | 35 | mbed_reset(); |
| martydd3 | 10:db13782f05d9 | 36 | } |
| martydd3 | 10:db13782f05d9 | 37 | |
| martydd3 | 12:e0adb697fcdb | 38 | void error_update(void const *arg){ |
| martydd3 | 12:e0adb697fcdb | 39 | char data[1] = {0}; |
| martydd3 | 12:e0adb697fcdb | 40 | while(1){ |
| martydd3 | 12:e0adb697fcdb | 41 | data[0] = 0; |
| martydd3 | 12:e0adb697fcdb | 42 | if(wdt.checkFlag()) |
| martydd3 | 12:e0adb697fcdb | 43 | { |
| martydd3 | 12:e0adb697fcdb | 44 | data[0] |= 0x2; |
| martydd3 | 12:e0adb697fcdb | 45 | } |
| martydd3 | 12:e0adb697fcdb | 46 | CANMessage txErrorMessage(SYSMGMT_ERROR_ID, data, 1); |
| martydd3 | 12:e0adb697fcdb | 47 | rxBuffer.txWrite(txErrorMessage); |
| martydd3 | 12:e0adb697fcdb | 48 | Thread::wait(10); |
| martydd3 | 12:e0adb697fcdb | 49 | } |
| martydd3 | 12:e0adb697fcdb | 50 | } |
| martydd3 | 12:e0adb697fcdb | 51 | |
| martydd3 | 10:db13782f05d9 | 52 | int main() { |
| martydd3 | 10:db13782f05d9 | 53 | CANMessage rx_msg; |
| martydd3 | 10:db13782f05d9 | 54 | |
| martydd3 | 10:db13782f05d9 | 55 | wdt.kick(10.0); |
| martydd3 | 10:db13782f05d9 | 56 | pc1.baud(115200); |
| martydd3 | 10:db13782f05d9 | 57 | |
| martydd3 | 10:db13782f05d9 | 58 | FanPump fanPump(&rxBuffer); |
| martydd3 | 10:db13782f05d9 | 59 | DC dc_dc(&fanPump, &rxBuffer); |
| martydd3 | 10:db13782f05d9 | 60 | PollSwitch pollSwitch(&rxBuffer); |
| martydd3 | 10:db13782f05d9 | 61 | IMD imdMonitor(&rxBuffer); |
| martydd3 | 10:db13782f05d9 | 62 | CurrentMonitor curMonitor(&rxBuffer); |
| martydd3 | 10:db13782f05d9 | 63 | |
| martydd3 | 10:db13782f05d9 | 64 | fanPump.start_update(); |
| martydd3 | 10:db13782f05d9 | 65 | dc_dc.start_update(); |
| martydd3 | 10:db13782f05d9 | 66 | pollSwitch.start_update(); |
| martydd3 | 10:db13782f05d9 | 67 | imdMonitor.start_update(); |
| martydd3 | 10:db13782f05d9 | 68 | curMonitor.start_update(); |
| martydd3 | 10:db13782f05d9 | 69 | |
| martydd3 | 12:e0adb697fcdb | 70 | Thread error_thread(error_update); |
| martydd3 | 12:e0adb697fcdb | 71 | |
| martydd3 | 10:db13782f05d9 | 72 | while(1) |
| martydd3 | 10:db13782f05d9 | 73 | { |
| martydd3 | 10:db13782f05d9 | 74 | if(rxBuffer.rxRead(rx_msg)){ |
| martydd3 | 10:db13782f05d9 | 75 | if(rx_msg.id == reset_id) |
| martydd3 | 10:db13782f05d9 | 76 | soft_reset(); |
| martydd3 | 10:db13782f05d9 | 77 | |
| martydd3 | 10:db13782f05d9 | 78 | char src_addr = (rx_msg.id & 0x0700) >> 8; // get bits 10:8 |
| martydd3 | 10:db13782f05d9 | 79 | |
| martydd3 | 10:db13782f05d9 | 80 | if(src_addr == sys_src_id){ |
| martydd3 | 10:db13782f05d9 | 81 | char cont_id = (rx_msg.id & 0x00FF); // get bits 7:0 |
| martydd3 | 10:db13782f05d9 | 82 | |
| martydd3 | 10:db13782f05d9 | 83 | // only control fans of dc_dc converter is on |
| martydd3 | 12:e0adb697fcdb | 84 | if(cont_id == RX_PUMP_ID && dc_dc.is_on()) |
| martydd3 | 12:e0adb697fcdb | 85 | { |
| martydd3 | 12:e0adb697fcdb | 86 | float temp_float; |
| martydd3 | 12:e0adb697fcdb | 87 | |
| martydd3 | 12:e0adb697fcdb | 88 | memcpy(&rx_msg.data[0], &temp_float, sizeof(float)); |
| martydd3 | 12:e0adb697fcdb | 89 | fanPump.set_fan_pump(Pump1, temp_float); |
| martydd3 | 12:e0adb697fcdb | 90 | |
| martydd3 | 12:e0adb697fcdb | 91 | memcpy(&rx_msg.data[4], &temp_float, sizeof(float)); |
| martydd3 | 12:e0adb697fcdb | 92 | fanPump.set_fan_pump(Pump2, temp_float); |
| martydd3 | 12:e0adb697fcdb | 93 | } |
| martydd3 | 12:e0adb697fcdb | 94 | |
| martydd3 | 10:db13782f05d9 | 95 | if(cont_id == RX_FAN_ID && dc_dc.is_on()) |
| martydd3 | 10:db13782f05d9 | 96 | { |
| martydd3 | 12:e0adb697fcdb | 97 | float temp_float; |
| martydd3 | 12:e0adb697fcdb | 98 | |
| martydd3 | 12:e0adb697fcdb | 99 | memcpy(&rx_msg.data[0], &temp_float, sizeof(float)); |
| martydd3 | 12:e0adb697fcdb | 100 | fanPump.set_fan_pump(Fan1, temp_float); |
| martydd3 | 12:e0adb697fcdb | 101 | |
| martydd3 | 12:e0adb697fcdb | 102 | memcpy(&rx_msg.data[4], &temp_float, sizeof(float)); |
| martydd3 | 12:e0adb697fcdb | 103 | fanPump.set_fan_pump(Fan2, temp_float); |
| martydd3 | 10:db13782f05d9 | 104 | } |
| martydd3 | 10:db13782f05d9 | 105 | |
| martydd3 | 10:db13782f05d9 | 106 | if(cont_id == RX_DC_DC_ID){ |
| martydd3 | 10:db13782f05d9 | 107 | dc_dc.set(rx_msg.data[0]); |
| martydd3 | 10:db13782f05d9 | 108 | } |
| martydd3 | 10:db13782f05d9 | 109 | } // check for correct src_addr |
| martydd3 | 10:db13782f05d9 | 110 | } // check CANBuffer |
| martydd3 | 10:db13782f05d9 | 111 | |
| martydd3 | 10:db13782f05d9 | 112 | wdt.kick(); |
| martydd3 | 10:db13782f05d9 | 113 | } // main while loop |
| martydd3 | 10:db13782f05d9 | 114 | } |
| martydd3 | 10:db13782f05d9 | 115 | |
| martydd3 | 11:1d086618dd18 | 116 | /* |
| martydd3 | 0:e516fcccccda | 117 | |
| martydd3 | 11:1d086618dd18 | 118 | Below is Kiran's old code, keeping it commented as a reference. |
| martydd3 | 11:1d086618dd18 | 119 | |
| martydd3 | 11:1d086618dd18 | 120 | */ |
| martydd3 | 0:e516fcccccda | 121 | |
| martydd3 | 0:e516fcccccda | 122 | /* |
| martydd3 | 0:e516fcccccda | 123 | |
| martydd3 | 0:e516fcccccda | 124 | Attach Ticker every 10msec to |
| martydd3 | 0:e516fcccccda | 125 | Get IMD |
| martydd3 | 0:e516fcccccda | 126 | Poll Switches |
| martydd3 | 0:e516fcccccda | 127 | Temperature Read |
| martydd3 | 0:e516fcccccda | 128 | Get Battery State |
| martydd3 | 0:e516fcccccda | 129 | End Ticker Send message through CAN |
| martydd3 | 0:e516fcccccda | 130 | |
| martydd3 | 0:e516fcccccda | 131 | CAN interrupt Rx Interrupt |
| martydd3 | 0:e516fcccccda | 132 | Recieve CAN message into a buffer. Return |
| martydd3 | 0:e516fcccccda | 133 | Buffer values(as long as !empty) -> SD Card, Xbee -> remove element |
| martydd3 | 0:e516fcccccda | 134 | |
| martydd3 | 0:e516fcccccda | 135 | extern "C" void CAN_IRQHandler(void) |
| martydd3 | 0:e516fcccccda | 136 | { |
| martydd3 | 0:e516fcccccda | 137 | CANMessage Rxmsg; |
| martydd3 | 0:e516fcccccda | 138 | CAN_SysM.read(Rxmsg); |
| martydd3 | 0:e516fcccccda | 139 | RecieveBuffer.add(Rxmsg); |
| martydd3 | 0:e516fcccccda | 140 | } |
| martydd3 | 0:e516fcccccda | 141 | |
| martydd3 | 1:e02eb179aed3 | 142 | http://developer.mbed.org/users/AjK/notebook/getting-closer-to-the-hardware/ |
| martydd3 | 1:e02eb179aed3 | 143 | |
| martydd3 | 1:e02eb179aed3 | 144 | extern "C" means this is linked assuming it's C code |
| martydd3 | 1:e02eb179aed3 | 145 | C++ linker appearently adds extra crap with the function arguments keeping functions of this kind from linking properly |
| martydd3 | 1:e02eb179aed3 | 146 | |
| martydd3 | 1:e02eb179aed3 | 147 | Interrupt handler, This is probably linked to the Timer 2 interrupt request somewhere by the libraries |
| martydd3 | 2:baeb80c778f7 | 148 | |
| martydd3 | 10:db13782f05d9 | 149 | */ |
| martydd3 | 0:e516fcccccda | 150 | |
| martydd3 | 10:db13782f05d9 | 151 | |
| martydd3 | 10:db13782f05d9 | 152 | /* |
| martydd3 | 1:e02eb179aed3 | 153 | Appears to read a whole bunch of DigitalOut pins in void PollSwitch(), store the value in uint16_t Rxpoll, |
| martydd3 | 1:e02eb179aed3 | 154 | and write the result to the CAN bus |
| martydd3 | 2:baeb80c778f7 | 155 | |
| martydd3 | 0:e516fcccccda | 156 | void Poll() |
| martydd3 | 0:e516fcccccda | 157 | { |
| martydd3 | 0:e516fcccccda | 158 | uint16_t Rxpoll; |
| martydd3 | 0:e516fcccccda | 159 | uint16_t recv,temp,i=0; //Test |
| martydd3 | 0:e516fcccccda | 160 | char Result[4]={0}; |
| martydd3 | 0:e516fcccccda | 161 | Rxpoll=PollSwitch(); |
| martydd3 | 0:e516fcccccda | 162 | |
| martydd3 | 0:e516fcccccda | 163 | Result[0]=(char)(Rxpoll&0x00ff); |
| martydd3 | 0:e516fcccccda | 164 | Result[1]=(char)((Rxpoll&0xff00)>>8); |
| martydd3 | 0:e516fcccccda | 165 | CANMessage Txmsg(410,Result,sizeof(Result)); |
| martydd3 | 0:e516fcccccda | 166 | CAN_SysM.write(Txmsg); |
| martydd3 | 0:e516fcccccda | 167 | |
| martydd3 | 0:e516fcccccda | 168 | //Test |
| martydd3 | 0:e516fcccccda | 169 | recv=(((uint16_t)Txmsg.data[1]<<8) | (0x00ff&(uint16_t)Txmsg.data[0])); |
| martydd3 | 0:e516fcccccda | 170 | printf("Recv:%d\n\r",recv); |
| martydd3 | 0:e516fcccccda | 171 | |
| martydd3 | 0:e516fcccccda | 172 | while(i <= 12) |
| martydd3 | 0:e516fcccccda | 173 | { |
| martydd3 | 0:e516fcccccda | 174 | temp=recv; |
| martydd3 | 0:e516fcccccda | 175 | if(((temp & (1 << i))>>i)==1) |
| martydd3 | 0:e516fcccccda | 176 | pc.printf("Switch OFF:%d\n\r",i); |
| martydd3 | 0:e516fcccccda | 177 | ++i; |
| martydd3 | 0:e516fcccccda | 178 | } |
| martydd3 | 0:e516fcccccda | 179 | } |
| martydd3 | 0:e516fcccccda | 180 | |
| martydd3 | 0:e516fcccccda | 181 | |
| martydd3 | 0:e516fcccccda | 182 | void Temp() |
| martydd3 | 0:e516fcccccda | 183 | { |
| martydd3 | 0:e516fcccccda | 184 | float DC_DC_Temperature, Coolant1_Temperature, Coolant2_Temperature, ChargerFET_Temperature; |
| martydd3 | 0:e516fcccccda | 185 | float Resistance; |
| martydd3 | 0:e516fcccccda | 186 | float Vadc; |
| martydd3 | 0:e516fcccccda | 187 | int i; |
| martydd3 | 0:e516fcccccda | 188 | ftc send, recv; |
| martydd3 | 0:e516fcccccda | 189 | recv.FLOAT=0.0; |
| martydd3 | 0:e516fcccccda | 190 | send.FLOAT=0.0; |
| martydd3 | 0:e516fcccccda | 191 | |
| martydd3 | 0:e516fcccccda | 192 | Vadc=DC_DC.read()*VDD; |
| martydd3 | 0:e516fcccccda | 193 | Resistance=((float)R10K*Vadc)/((float)VDD + Vadc); |
| martydd3 | 0:e516fcccccda | 194 | DC_DC_Temperature=ReadTemp(TR_NXFT15XH103FA_Map, Resistance, TABLE_SIZE_NXFT15XH103FA); |
| martydd3 | 0:e516fcccccda | 195 | send.FLOAT=DC_DC_Temperature; |
| martydd3 | 0:e516fcccccda | 196 | CANMessage Txmsg_DC_DC(450,send.C_FLOAT,sizeof(send.C_FLOAT)); |
| martydd3 | 0:e516fcccccda | 197 | CAN_SysM.write(Txmsg_DC_DC); |
| martydd3 | 0:e516fcccccda | 198 | |
| martydd3 | 0:e516fcccccda | 199 | for(i=0; i<4;i++) |
| martydd3 | 0:e516fcccccda | 200 | recv.C_FLOAT[i]=Txmsg_DC_DC.data[i]; |
| martydd3 | 0:e516fcccccda | 201 | pc.printf("DC_DC:%f\n\r",recv.FLOAT); |
| martydd3 | 0:e516fcccccda | 202 | |
| martydd3 | 0:e516fcccccda | 203 | Vadc=ChargerFET.read()*VDD; |
| martydd3 | 0:e516fcccccda | 204 | Resistance=((float)R10K*Vadc)/((float)VDD + Vadc); |
| martydd3 | 0:e516fcccccda | 205 | ChargerFET_Temperature=ReadTemp(TR_NXFT15XH103FA_Map, Resistance, TABLE_SIZE_NXFT15XH103FA); |
| martydd3 | 0:e516fcccccda | 206 | send.FLOAT=ChargerFET_Temperature; |
| martydd3 | 0:e516fcccccda | 207 | CANMessage Txmsg_ChargerFET(451,send.C_FLOAT,sizeof(send.C_FLOAT)); |
| martydd3 | 0:e516fcccccda | 208 | CAN_SysM.write(Txmsg_ChargerFET); |
| martydd3 | 0:e516fcccccda | 209 | |
| martydd3 | 0:e516fcccccda | 210 | for(i=0; i<4;i++) |
| martydd3 | 0:e516fcccccda | 211 | recv.C_FLOAT[i]=Txmsg_ChargerFET.data[i]; |
| martydd3 | 0:e516fcccccda | 212 | pc.printf("ChargerFET:%f\n\r",recv.FLOAT); |
| martydd3 | 0:e516fcccccda | 213 | |
| martydd3 | 0:e516fcccccda | 214 | Vadc=Coolant1.read()*VDD; |
| martydd3 | 0:e516fcccccda | 215 | Resistance=((float)R10K*Vadc)/((float)VDD + Vadc); |
| martydd3 | 0:e516fcccccda | 216 | Coolant1_Temperature=ReadTemp(TR_NTCLP00E3103H_Map, Resistance, TABLE_SIZE_NTCLP00E3103H); |
| martydd3 | 0:e516fcccccda | 217 | send.FLOAT=Coolant1_Temperature; |
| martydd3 | 0:e516fcccccda | 218 | CANMessage Txmsg_Coolant1(452,send.C_FLOAT,sizeof(send.C_FLOAT)); |
| martydd3 | 0:e516fcccccda | 219 | CAN_SysM.write(Txmsg_Coolant1); |
| martydd3 | 0:e516fcccccda | 220 | //Control Fans |
| martydd3 | 0:e516fcccccda | 221 | |
| martydd3 | 0:e516fcccccda | 222 | for(i=0; i<4;i++) |
| martydd3 | 0:e516fcccccda | 223 | recv.C_FLOAT[i]=Txmsg_Coolant1.data[i]; |
| martydd3 | 0:e516fcccccda | 224 | pc.printf("Coolant1:%f\n\r",recv.FLOAT); |
| martydd3 | 0:e516fcccccda | 225 | |
| martydd3 | 0:e516fcccccda | 226 | Vadc=Coolant2.read()*VDD; |
| martydd3 | 0:e516fcccccda | 227 | Resistance=((float)R10K*Vadc)/((float)VDD + Vadc); |
| martydd3 | 0:e516fcccccda | 228 | Coolant2_Temperature=ReadTemp(TR_NTCLP00E3103H_Map, Resistance, TABLE_SIZE_NTCLP00E3103H); |
| martydd3 | 0:e516fcccccda | 229 | send.FLOAT=Coolant2_Temperature; |
| martydd3 | 0:e516fcccccda | 230 | CANMessage Txmsg_Coolant2(453,send.C_FLOAT,sizeof(send.C_FLOAT)); |
| martydd3 | 0:e516fcccccda | 231 | CAN_SysM.write(Txmsg_Coolant2); |
| martydd3 | 0:e516fcccccda | 232 | //Control Fans |
| martydd3 | 0:e516fcccccda | 233 | |
| martydd3 | 0:e516fcccccda | 234 | for(i=0; i<4;i++) |
| martydd3 | 0:e516fcccccda | 235 | recv.C_FLOAT[i]=Txmsg_Coolant2.data[i]; |
| martydd3 | 0:e516fcccccda | 236 | pc.printf("Coolant2:%f\n\r",recv.FLOAT); |
| martydd3 | 0:e516fcccccda | 237 | } |
| martydd3 | 0:e516fcccccda | 238 | |
| martydd3 | 0:e516fcccccda | 239 | void IMD() |
| martydd3 | 0:e516fcccccda | 240 | { |
| martydd3 | 0:e516fcccccda | 241 | IMD_Measurement_Output IMD_Signal; |
| martydd3 | 0:e516fcccccda | 242 | char status[4]; |
| martydd3 | 0:e516fcccccda | 243 | ftc send; |
| martydd3 | 0:e516fcccccda | 244 | |
| martydd3 | 0:e516fcccccda | 245 | IMD_Signal=Get_Measurement(); |
| martydd3 | 0:e516fcccccda | 246 | send.FLOAT=IMD_Signal.Frequency; |
| martydd3 | 0:e516fcccccda | 247 | CANMessage Txmsg_Frequency(421,send.C_FLOAT,sizeof(send.C_FLOAT)); |
| martydd3 | 0:e516fcccccda | 248 | CAN_SysM.write(Txmsg_Frequency); |
| martydd3 | 0:e516fcccccda | 249 | |
| martydd3 | 0:e516fcccccda | 250 | send.FLOAT=IMD_Signal.Duty_Cycle; |
| martydd3 | 0:e516fcccccda | 251 | CANMessage Txmsg_DutyCycle(422,send.C_FLOAT,sizeof(send.C_FLOAT)); |
| martydd3 | 0:e516fcccccda | 252 | CAN_SysM.write(Txmsg_DutyCycle); |
| martydd3 | 0:e516fcccccda | 253 | |
| martydd3 | 0:e516fcccccda | 254 | status[0]=Result.Encoded_Status; |
| martydd3 | 0:e516fcccccda | 255 | CANMessage Txmsg_Status(423,status,sizeof(status)); |
| martydd3 | 0:e516fcccccda | 256 | CAN_SysM.write(Txmsg_Status); |
| martydd3 | 0:e516fcccccda | 257 | } |
| martydd3 | 5:9258b685fea6 | 258 | */ |
| martydd3 | 0:e516fcccccda | 259 | |
| martydd3 | 5:9258b685fea6 | 260 | /* |
| martydd3 | 0:e516fcccccda | 261 | Activates a whole crapload of functions and pins on the chip |
| martydd3 | 2:baeb80c778f7 | 262 | |
| martydd3 | 0:e516fcccccda | 263 | void Init() |
| martydd3 | 0:e516fcccccda | 264 | { |
| martydd3 | 2:baeb80c778f7 | 265 | |
| martydd3 | 0:e516fcccccda | 266 | Timers to call various functions at different intervals |
| martydd3 | 0:e516fcccccda | 267 | These things behave weirdly when wait(ms) is involved. Probably have to rewrite |
| martydd3 | 2:baeb80c778f7 | 268 | |
| martydd3 | 0:e516fcccccda | 269 | |
| martydd3 | 0:e516fcccccda | 270 | //ReadIMD.attach(&IMD,0.1); |
| martydd3 | 0:e516fcccccda | 271 | //PollSDSwitch.attach(&Poll,0.1); |
| martydd3 | 0:e516fcccccda | 272 | |
| martydd3 | 0:e516fcccccda | 273 | //ReadTemperature.attach(&Temp,0.1); |
| martydd3 | 0:e516fcccccda | 274 | //ReadBatteryState.attach(&Battery,0.1); |
| martydd3 | 0:e516fcccccda | 275 | |
| martydd3 | 2:baeb80c778f7 | 276 | |
| martydd3 | 0:e516fcccccda | 277 | Initialize Timer2 for Battery State |
| martydd3 | 0:e516fcccccda | 278 | |
| martydd3 | 0:e516fcccccda | 279 | LPC_SC 0x400F C000 (System Control) |
| martydd3 | 0:e516fcccccda | 280 | ->PCONP 0x400F C0C4 (Power Control for Peripherals Register) |
| martydd3 | 0:e516fcccccda | 281 | |= (1<<22) 22 Bit (Timer 2 power/clock control bit) |
| martydd3 | 0:e516fcccccda | 282 | |
| martydd3 | 0:e516fcccccda | 283 | ->PCLKSEL1 Peripheral Clock Selection register 1 (controls rate of clock signal supplied to peripheral) |
| martydd3 | 0:e516fcccccda | 284 | |= ((1<<12) | (1<<13)); 12:13 Bits (Peripheral Clock Selection for TIMER2) |
| martydd3 | 0:e516fcccccda | 285 | |
| martydd3 | 0:e516fcccccda | 286 | LPC_TIM2 0x4009 0000 (Timer 2) |
| martydd3 | 0:e516fcccccda | 287 | ->TCR 0x4009 0004 (Timer Control Register) |
| martydd3 | 0:e516fcccccda | 288 | |= (1<<0); 0 Bit (Counter Enable) |
| martydd3 | 0:e516fcccccda | 289 | |
| martydd3 | 0:e516fcccccda | 290 | ->MR0 0x4009 0018 (Match Register) |
| martydd3 | 0:e516fcccccda | 291 | |
| martydd3 | 1:e02eb179aed3 | 292 | ->MCR 0x4009 0014 (Match Control Register) What to do when Match Register matches the Timer Counter |
| martydd3 | 1:e02eb179aed3 | 293 | |= (1<<0); 0 Bit (Interrupt on MR0, interrupt generated when MR0 matches the value in TC) |
| martydd3 | 0:e516fcccccda | 294 | |
| martydd3 | 2:baeb80c778f7 | 295 | |
| martydd3 | 0:e516fcccccda | 296 | LPC_SC->PCONP |= (1<<22); //PoewerOn Timer/Counter2 |
| martydd3 | 0:e516fcccccda | 297 | LPC_SC->PCLKSEL1 |= ((1<<12) | (1<<13)); //Prescale Timer2 CCLK/8 |
| martydd3 | 0:e516fcccccda | 298 | LPC_TIM2->TCR |= (1<<0); //Enable Timer2 |
| martydd3 | 0:e516fcccccda | 299 | LPC_TIM2->MR0 = 11999; // 1msec |
| martydd3 | 0:e516fcccccda | 300 | LPC_TIM2->MCR |= (1<<0); |
| martydd3 | 2:baeb80c778f7 | 301 | |
| martydd3 | 1:e02eb179aed3 | 302 | Nested Vectored Interrupt Controller (NVIC) |
| martydd3 | 1:e02eb179aed3 | 303 | |
| martydd3 | 1:e02eb179aed3 | 304 | NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority) |
| martydd3 | 1:e02eb179aed3 | 305 | sets priority of an interrupt |
| martydd3 | 1:e02eb179aed3 | 306 | |
| martydd3 | 1:e02eb179aed3 | 307 | IRQn_Type |
| martydd3 | 1:e02eb179aed3 | 308 | Interrupt number definitions |
| martydd3 | 1:e02eb179aed3 | 309 | Interrupt Request (IRQ) |
| martydd3 | 1:e02eb179aed3 | 310 | |
| martydd3 | 1:e02eb179aed3 | 311 | NVIC_EnableIRQ(IRQn_Type IRQn) |
| martydd3 | 1:e02eb179aed3 | 312 | Enable external interrupt (in this case, the TIMER2_IRQHandler(void) function above gets called every time |
| martydd3 | 1:e02eb179aed3 | 313 | Timer2 generates an interrupt signal) |
| martydd3 | 1:e02eb179aed3 | 314 | |
| martydd3 | 2:baeb80c778f7 | 315 | |
| martydd3 | 0:e516fcccccda | 316 | NVIC_SetPriority(TIMER0_IRQn,200); //IMD Capture Interrupt |
| martydd3 | 0:e516fcccccda | 317 | NVIC_SetPriority(TIMER1_IRQn,200); //IMD 1msec sampling Interrupt |
| martydd3 | 0:e516fcccccda | 318 | NVIC_SetPriority(TIMER2_IRQn,1); //Battery 1msec sampling Interrupt |
| martydd3 | 0:e516fcccccda | 319 | NVIC_SetPriority(TIMER3_IRQn,255); //mbed Timer/Ticker/Wait Interrupt |
| martydd3 | 0:e516fcccccda | 320 | NVIC_SetPriority(CAN_IRQn,2); |
| martydd3 | 0:e516fcccccda | 321 | |
| martydd3 | 0:e516fcccccda | 322 | NVIC_EnableIRQ(TIMER2_IRQn); //Enable TIMER2 IRQ |
| martydd3 | 0:e516fcccccda | 323 | |
| martydd3 | 0:e516fcccccda | 324 | CAN_SysM.mode(CAN::GlobalTest); |
| martydd3 | 2:baeb80c778f7 | 325 | |
| martydd3 | 2:baeb80c778f7 | 326 | |
| martydd3 | 0:e516fcccccda | 327 | //NVIC_EnableIRQ(CAN_IRQn); |
| martydd3 | 0:e516fcccccda | 328 | //NVIC_EnableIRQ(CANActivity_IRQn); |
| martydd3 | 0:e516fcccccda | 329 | } |
| martydd3 | 0:e516fcccccda | 330 | |
| martydd3 | 2:baeb80c778f7 | 331 | |
| martydd3 | 0:e516fcccccda | 332 | Main Loop: Currently reads CANMessages from Can interface (Pins: rd = p30, td = p29) |
| martydd3 | 0:e516fcccccda | 333 | Send CANMessage data through XBee radio transmitters |
| martydd3 | 2:baeb80c778f7 | 334 | */ |
