Lizzy project
Dependencies: aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT
source/main.cpp@6:55607d577a42, 2018-03-13 (annotated)
- Committer:
- dbartolovic
- Date:
- Tue Mar 13 15:41:35 2018 +0000
- Revision:
- 6:55607d577a42
- Parent:
- 5:9903738f2587
- Child:
- 7:ac8277568115
Added simple test of leds and buzzer.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jurica238814 | 0:fc77522f4d28 | 1 | /* |
jurica238814 | 0:fc77522f4d28 | 2 | * aconno.de |
jurica238814 | 0:fc77522f4d28 | 3 | * Made by Jurica Resetar |
jurica238814 | 0:fc77522f4d28 | 4 | * All right reserved |
jurica238814 | 0:fc77522f4d28 | 5 | * |
jurica238814 | 0:fc77522f4d28 | 6 | */ |
jurica238814 | 0:fc77522f4d28 | 7 | |
jurica238814 | 0:fc77522f4d28 | 8 | #include "main.h" |
jurica238814 | 0:fc77522f4d28 | 9 | |
jurica238814 | 0:fc77522f4d28 | 10 | static advertising_packet advertisementPacket; |
jurica238814 | 0:fc77522f4d28 | 11 | static GapAdvertisingData adv_data = GapAdvertisingData(); |
jurica238814 | 0:fc77522f4d28 | 12 | |
jurica238814 | 0:fc77522f4d28 | 13 | void disableI2C(){ |
jurica238814 | 0:fc77522f4d28 | 14 | NRF_TWI0->ENABLE = 0; |
jurica238814 | 0:fc77522f4d28 | 15 | NRF_TWI0->PSELSCL = 0xFFFFFFFF; |
jurica238814 | 0:fc77522f4d28 | 16 | NRF_TWI1->ENABLE = 0; |
jurica238814 | 0:fc77522f4d28 | 17 | NRF_TWI1->PSELSCL = 0xFFFFFFFF; |
jurica238814 | 0:fc77522f4d28 | 18 | NRF_TWIM0->ENABLE = 0; |
jurica238814 | 0:fc77522f4d28 | 19 | NRF_TWIM0->PSEL.SCL = 0x80000000; |
jurica238814 | 0:fc77522f4d28 | 20 | NRF_TWIM0->PSEL.SDA = 0x80000000; |
jurica238814 | 0:fc77522f4d28 | 21 | NRF_TWIM1->ENABLE = 0; |
jurica238814 | 0:fc77522f4d28 | 22 | NRF_TWIM0->PSEL.SCL = 0x80000000; |
jurica238814 | 0:fc77522f4d28 | 23 | NRF_TWIM0->PSEL.SDA = 0x80000000; |
jurica238814 | 0:fc77522f4d28 | 24 | DigitalOut foo(I2C_DATA); |
jurica238814 | 0:fc77522f4d28 | 25 | DigitalOut bar(I2C_CLK); |
jurica238814 | 0:fc77522f4d28 | 26 | foo = 1; |
jurica238814 | 0:fc77522f4d28 | 27 | bar = 1; |
jurica238814 | 0:fc77522f4d28 | 28 | } |
jurica238814 | 0:fc77522f4d28 | 29 | |
jurica238814 | 0:fc77522f4d28 | 30 | void bleF(BLE *ble) |
jurica238814 | 0:fc77522f4d28 | 31 | { |
jurica238814 | 0:fc77522f4d28 | 32 | while(true) |
jurica238814 | 0:fc77522f4d28 | 33 | { |
jurica238814 | 0:fc77522f4d28 | 34 | |
jurica238814 | 0:fc77522f4d28 | 35 | advertisementPacket.header = APPLICATION_ID; |
jurica238814 | 0:fc77522f4d28 | 36 | advertisementPacket.type = 0x00; |
jurica238814 | 0:fc77522f4d28 | 37 | advertisementPacket.gyroscope[0] = (int16_t)0; |
jurica238814 | 0:fc77522f4d28 | 38 | advertisementPacket.gyroscope[1] = (int16_t)0; |
jurica238814 | 0:fc77522f4d28 | 39 | advertisementPacket.gyroscope[2] = (int16_t)0; |
jurica238814 | 0:fc77522f4d28 | 40 | advertisementPacket.magnetometer[0] = (int16_t)0; |
jurica238814 | 0:fc77522f4d28 | 41 | advertisementPacket.magnetometer[1] = (int16_t)0; |
jurica238814 | 0:fc77522f4d28 | 42 | advertisementPacket.magnetometer[2] = (int16_t)0; |
jurica238814 | 0:fc77522f4d28 | 43 | |
dbartolovic | 1:198b9945994e | 44 | |
dbartolovic | 5:9903738f2587 | 45 | advertisementPacket.accelerometer[0] = (int16_t)mems.readXAxis(); |
dbartolovic | 5:9903738f2587 | 46 | advertisementPacket.accelerometer[1] = (int16_t)mems.readYAxis(); |
dbartolovic | 5:9903738f2587 | 47 | advertisementPacket.accelerometer[2] = (int16_t)mems.readZAxis(); |
dbartolovic | 3:38eadab20283 | 48 | |
dbartolovic | 3:38eadab20283 | 49 | //advertisementPacket.acc_lsb_value = 0xC000; |
dbartolovic | 5:9903738f2587 | 50 | advertisementPacket.acc_lsb_value = 0x0100; |
dbartolovic | 1:198b9945994e | 51 | |
dbartolovic | 1:198b9945994e | 52 | |
dbartolovic | 3:38eadab20283 | 53 | printf("%6d\t", advertisementPacket.accelerometer[0]); |
dbartolovic | 3:38eadab20283 | 54 | printf("%6d\t", advertisementPacket.accelerometer[1]); |
dbartolovic | 3:38eadab20283 | 55 | printf("%6d\r\n", advertisementPacket.accelerometer[2]); |
dbartolovic | 1:198b9945994e | 56 | |
jurica238814 | 0:fc77522f4d28 | 57 | //updatePayload(ble, &advertisementPacket); |
jurica238814 | 0:fc77522f4d28 | 58 | |
jurica238814 | 0:fc77522f4d28 | 59 | /* setup advertising */ |
jurica238814 | 0:fc77522f4d28 | 60 | |
jurica238814 | 0:fc77522f4d28 | 61 | |
jurica238814 | 0:fc77522f4d28 | 62 | /* |
jurica238814 | 0:fc77522f4d28 | 63 | GapAdvertisingData advetisementData = GapAdvertisingData(); |
jurica238814 | 0:fc77522f4d28 | 64 | advetisementData = ble->getAdvertisingData(); |
jurica238814 | 0:fc77522f4d28 | 65 | advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet)); |
jurica238814 | 0:fc77522f4d28 | 66 | ble->setAdvertisingData(advetisementData); |
jurica238814 | 0:fc77522f4d28 | 67 | */ |
jurica238814 | 0:fc77522f4d28 | 68 | adv_data = ble->getAdvertisingData(); |
jurica238814 | 0:fc77522f4d28 | 69 | adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket)); |
jurica238814 | 0:fc77522f4d28 | 70 | ble->setAdvertisingData(adv_data); |
jurica238814 | 0:fc77522f4d28 | 71 | |
jurica238814 | 0:fc77522f4d28 | 72 | |
jurica238814 | 0:fc77522f4d28 | 73 | //printf("Ble advertisement is ON.\r\n"); |
jurica238814 | 0:fc77522f4d28 | 74 | |
jurica238814 | 0:fc77522f4d28 | 75 | #if DEBUG_LED |
jurica238814 | 0:fc77522f4d28 | 76 | redLed = 0; |
jurica238814 | 0:fc77522f4d28 | 77 | #endif |
jurica238814 | 0:fc77522f4d28 | 78 | ble->gap().startAdvertising(); |
dbartolovic | 5:9903738f2587 | 79 | //wait_ms(1000); |
jurica238814 | 0:fc77522f4d28 | 80 | |
dbartolovic | 5:9903738f2587 | 81 | wait_ms(MEASURE_INTERVAL_MS); |
jurica238814 | 0:fc77522f4d28 | 82 | //wait_ms(1000); |
jurica238814 | 0:fc77522f4d28 | 83 | |
jurica238814 | 0:fc77522f4d28 | 84 | //printf("Ble advertisement is OFF.\r\n\r\n"); |
jurica238814 | 0:fc77522f4d28 | 85 | #if DEBUG_LED |
jurica238814 | 0:fc77522f4d28 | 86 | redLed = 1; |
jurica238814 | 0:fc77522f4d28 | 87 | #endif |
dbartolovic | 1:198b9945994e | 88 | //ble->gap().stopAdvertising(); |
jurica238814 | 0:fc77522f4d28 | 89 | /* |
jurica238814 | 0:fc77522f4d28 | 90 | GapAdvertisingData advetisementData = GapAdvertisingData(); |
jurica238814 | 0:fc77522f4d28 | 91 | advetisementData = ble->getAdvertisingData(); |
jurica238814 | 0:fc77522f4d28 | 92 | advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertising_packet)); |
jurica238814 | 0:fc77522f4d28 | 93 | ble->setAdvertisingData(advetisementData); |
jurica238814 | 0:fc77522f4d28 | 94 | */ |
dbartolovic | 5:9903738f2587 | 95 | //wait_ms(100); |
jurica238814 | 0:fc77522f4d28 | 96 | } |
jurica238814 | 0:fc77522f4d28 | 97 | } |
jurica238814 | 0:fc77522f4d28 | 98 | |
dbartolovic | 6:55607d577a42 | 99 | void led_tick(); |
dbartolovic | 6:55607d577a42 | 100 | |
dbartolovic | 6:55607d577a42 | 101 | void buzz_tick() |
dbartolovic | 6:55607d577a42 | 102 | { |
dbartolovic | 6:55607d577a42 | 103 | static int start = 1; |
dbartolovic | 6:55607d577a42 | 104 | |
dbartolovic | 6:55607d577a42 | 105 | if (start) |
dbartolovic | 6:55607d577a42 | 106 | { |
dbartolovic | 6:55607d577a42 | 107 | buzzer.enable(BUZZER_FREQUENCY_HZ); |
dbartolovic | 6:55607d577a42 | 108 | buzzer.enableChannel(0, BUZZER_PIN); |
dbartolovic | 6:55607d577a42 | 109 | buzzer.setDuty(0,0.5f); |
dbartolovic | 6:55607d577a42 | 110 | start = 0; |
dbartolovic | 6:55607d577a42 | 111 | } |
dbartolovic | 6:55607d577a42 | 112 | else |
dbartolovic | 6:55607d577a42 | 113 | { |
dbartolovic | 6:55607d577a42 | 114 | buzzer.enable(0); |
dbartolovic | 6:55607d577a42 | 115 | buzzer.setDuty(0, 0); |
dbartolovic | 6:55607d577a42 | 116 | buzzer.disable(); |
dbartolovic | 6:55607d577a42 | 117 | start = 1; |
dbartolovic | 6:55607d577a42 | 118 | led_tick(); |
dbartolovic | 6:55607d577a42 | 119 | test_ticker.detach(); |
dbartolovic | 6:55607d577a42 | 120 | test_ticker.attach(led_tick, 0.5); |
dbartolovic | 6:55607d577a42 | 121 | } |
dbartolovic | 6:55607d577a42 | 122 | } |
dbartolovic | 6:55607d577a42 | 123 | |
dbartolovic | 6:55607d577a42 | 124 | |
dbartolovic | 6:55607d577a42 | 125 | void led_tick() |
dbartolovic | 6:55607d577a42 | 126 | { |
dbartolovic | 6:55607d577a42 | 127 | static int count = 0; |
dbartolovic | 6:55607d577a42 | 128 | |
dbartolovic | 6:55607d577a42 | 129 | switch(count) |
dbartolovic | 6:55607d577a42 | 130 | { |
dbartolovic | 6:55607d577a42 | 131 | case 0: |
dbartolovic | 6:55607d577a42 | 132 | redLed = 0; |
dbartolovic | 6:55607d577a42 | 133 | break; |
dbartolovic | 6:55607d577a42 | 134 | |
dbartolovic | 6:55607d577a42 | 135 | case 1: |
dbartolovic | 6:55607d577a42 | 136 | redLed = 1; |
dbartolovic | 6:55607d577a42 | 137 | blueLed = 0; |
dbartolovic | 6:55607d577a42 | 138 | break; |
dbartolovic | 6:55607d577a42 | 139 | |
dbartolovic | 6:55607d577a42 | 140 | case 2: |
dbartolovic | 6:55607d577a42 | 141 | blueLed = 1; |
dbartolovic | 6:55607d577a42 | 142 | greenLed = 0; |
dbartolovic | 6:55607d577a42 | 143 | break; |
dbartolovic | 6:55607d577a42 | 144 | |
dbartolovic | 6:55607d577a42 | 145 | default: |
dbartolovic | 6:55607d577a42 | 146 | greenLed = 1; |
dbartolovic | 6:55607d577a42 | 147 | count = -1; |
dbartolovic | 6:55607d577a42 | 148 | buzz_tick(); |
dbartolovic | 6:55607d577a42 | 149 | test_ticker.detach(); |
dbartolovic | 6:55607d577a42 | 150 | test_ticker.attach(buzz_tick, BUZZ_TIME_S); |
dbartolovic | 6:55607d577a42 | 151 | } |
dbartolovic | 6:55607d577a42 | 152 | |
dbartolovic | 6:55607d577a42 | 153 | count++; |
dbartolovic | 6:55607d577a42 | 154 | } |
jurica238814 | 0:fc77522f4d28 | 155 | |
jurica238814 | 0:fc77522f4d28 | 156 | int main(){ |
jurica238814 | 0:fc77522f4d28 | 157 | |
dbartolovic | 6:55607d577a42 | 158 | test_ticker.attach(led_tick, 0.5); |
dbartolovic | 6:55607d577a42 | 159 | |
jurica238814 | 0:fc77522f4d28 | 160 | printf("Main program started.\r\n"); |
jurica238814 | 0:fc77522f4d28 | 161 | |
jurica238814 | 0:fc77522f4d28 | 162 | NRF_NVMC->CONFIG = 0x00000002; // Erase enable UICR |
jurica238814 | 0:fc77522f4d28 | 163 | NRF_NVMC->ERASEUICR = 0x00000001; // Erase all |
jurica238814 | 0:fc77522f4d28 | 164 | NRF_NVMC->CONFIG = 0x00000001; // Write enable UICR |
jurica238814 | 0:fc77522f4d28 | 165 | NRF_UICR->NFCPINS = 0xFFFFFFFE; // Change NFC to GPIO function |
jurica238814 | 0:fc77522f4d28 | 166 | |
dbartolovic | 6:55607d577a42 | 167 | redLed = 0; |
jurica238814 | 0:fc77522f4d28 | 168 | AccVcc = 1; |
dbartolovic | 6:55607d577a42 | 169 | |
dbartolovic | 6:55607d577a42 | 170 | blueLed = 1; |
dbartolovic | 6:55607d577a42 | 171 | greenLed = 1; |
jurica238814 | 0:fc77522f4d28 | 172 | wait_ms(POWER_UP_DELAY_MS); |
jurica238814 | 0:fc77522f4d28 | 173 | |
jurica238814 | 0:fc77522f4d28 | 174 | /* |
jurica238814 | 0:fc77522f4d28 | 175 | INT1.rise(int1Handler); |
jurica238814 | 0:fc77522f4d28 | 176 | NRF_GPIO->PIN_CNF[INT_PIN1] &= 0xFFFFFFF3; // NO PullUps |
jurica238814 | 0:fc77522f4d28 | 177 | */ |
jurica238814 | 0:fc77522f4d28 | 178 | |
jurica238814 | 0:fc77522f4d28 | 179 | BLE &ble = BLE::Instance(); |
jurica238814 | 0:fc77522f4d28 | 180 | ble.init(bleInitComplete); |
jurica238814 | 0:fc77522f4d28 | 181 | |
dbartolovic | 5:9903738f2587 | 182 | //mems.setMode(LOW_POWER); |
dbartolovic | 5:9903738f2587 | 183 | mems.setMode(HIGH_RES); |
jurica238814 | 0:fc77522f4d28 | 184 | mems.enableAxes(X_axis); |
jurica238814 | 0:fc77522f4d28 | 185 | mems.enableAxes(Y_axis); |
jurica238814 | 0:fc77522f4d28 | 186 | mems.enableAxes(Z_axis); |
jurica238814 | 0:fc77522f4d28 | 187 | mems.setODR(ODR_50Hz); |
dbartolovic | 3:38eadab20283 | 188 | //mems.setScale(_16g); |
dbartolovic | 3:38eadab20283 | 189 | mems.setScale(_2g); |
jurica238814 | 0:fc77522f4d28 | 190 | mems.int1Setup(0x40); // IntActivity 1 driven to INT1 pad |
jurica238814 | 0:fc77522f4d28 | 191 | mems.int1Latch(0x01); // Enable int1 latch |
jurica238814 | 0:fc77522f4d28 | 192 | mems.int1Threshold(INT1_THRESHOLD); |
jurica238814 | 0:fc77522f4d28 | 193 | mems.int1Duration(INT1_DUR); |
jurica238814 | 0:fc77522f4d28 | 194 | mems.int1Config(0x2A); // Enable XHigh, YHigh and ZHigh |
jurica238814 | 0:fc77522f4d28 | 195 | |
jurica238814 | 0:fc77522f4d28 | 196 | bleT.start(callback(bleF, &ble)); // Start bleT |
jurica238814 | 0:fc77522f4d28 | 197 | |
jurica238814 | 0:fc77522f4d28 | 198 | |
jurica238814 | 0:fc77522f4d28 | 199 | while(1){ |
jurica238814 | 0:fc77522f4d28 | 200 | //disableI2C(); |
jurica238814 | 0:fc77522f4d28 | 201 | Thread::wait(0xFFFFFFFF); |
jurica238814 | 0:fc77522f4d28 | 202 | } |
jurica238814 | 0:fc77522f4d28 | 203 | } |