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.
main.cpp@3:d072d2d7f043, 2015-11-05 (annotated)
- Committer:
- true
- Date:
- Thu Nov 05 04:54:36 2015 +0000
- Revision:
- 3:d072d2d7f043
- Parent:
- 2:f12658f35bbd
- Updated pin offsets as PH_0 isn't working (likely AF but not good practice to use both oscillator pins anyway); - Adding mapping.txt showing human-readable input/output pairs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
true | 0:d4c9af9bd744 | 1 | #include "mbed.h" |
true | 0:d4c9af9bd744 | 2 | |
true | 0:d4c9af9bd744 | 3 | //------------------------------------ |
true | 0:d4c9af9bd744 | 4 | // 9600 baud, 8-bit data, no parity |
true | 0:d4c9af9bd744 | 5 | //------------------------------------ |
true | 0:d4c9af9bd744 | 6 | |
true | 0:d4c9af9bd744 | 7 | #define BYTEBINPATTERN "%d%d%d%d%d%d%d%d" |
true | 0:d4c9af9bd744 | 8 | #define BYTETOBIN(byte) \ |
true | 0:d4c9af9bd744 | 9 | (byte & 0x80 ? 1 : 0), \ |
true | 0:d4c9af9bd744 | 10 | (byte & 0x40 ? 1 : 0), \ |
true | 0:d4c9af9bd744 | 11 | (byte & 0x20 ? 1 : 0), \ |
true | 0:d4c9af9bd744 | 12 | (byte & 0x10 ? 1 : 0), \ |
true | 0:d4c9af9bd744 | 13 | (byte & 0x08 ? 1 : 0), \ |
true | 0:d4c9af9bd744 | 14 | (byte & 0x04 ? 1 : 0), \ |
true | 0:d4c9af9bd744 | 15 | (byte & 0x02 ? 1 : 0), \ |
true | 0:d4c9af9bd744 | 16 | (byte & 0x01 ? 1 : 0) |
true | 0:d4c9af9bd744 | 17 | |
true | 0:d4c9af9bd744 | 18 | Serial ser(SERIAL_TX, SERIAL_RX); |
true | 0:d4c9af9bd744 | 19 | |
true | 0:d4c9af9bd744 | 20 | DigitalIn in[16] = { |
true | 2:f12658f35bbd | 21 | PB_12, PA_11, PA_12, PC_5, |
true | 2:f12658f35bbd | 22 | PC_6, PC_8, PC_9, PB_8, |
true | 2:f12658f35bbd | 23 | PB_9, PA_6, PA_7, PB_6, |
true | 2:f12658f35bbd | 24 | PC_7, PA_9, PA_8, PB_10 |
true | 0:d4c9af9bd744 | 25 | }; |
true | 0:d4c9af9bd744 | 26 | |
true | 0:d4c9af9bd744 | 27 | DigitalInOut out[16] = { |
true | 0:d4c9af9bd744 | 28 | PD_2, PC_11, PC_10, PC_12, |
true | 3:d072d2d7f043 | 29 | PH_1, PC_2, PC_3, PC_0, |
true | 3:d072d2d7f043 | 30 | PC_1, PB_0, PA_4, PA_1, |
true | 3:d072d2d7f043 | 31 | PA_0, PB_3, PB_5, PB_4 |
true | 0:d4c9af9bd744 | 32 | }; |
true | 0:d4c9af9bd744 | 33 | |
true | 0:d4c9af9bd744 | 34 | DigitalOut userled(LED1); |
true | 0:d4c9af9bd744 | 35 | |
true | 0:d4c9af9bd744 | 36 | //------------------------------------ |
true | 0:d4c9af9bd744 | 37 | |
true | 0:d4c9af9bd744 | 38 | void input_state_print() |
true | 0:d4c9af9bd744 | 39 | { |
true | 0:d4c9af9bd744 | 40 | ser.printf("i"); |
true | 0:d4c9af9bd744 | 41 | |
true | 0:d4c9af9bd744 | 42 | for (int i = 0; i < 16; i++) { |
true | 0:d4c9af9bd744 | 43 | ser.printf("%c", in[i] ? 0x31 : 0x30); |
true | 0:d4c9af9bd744 | 44 | } |
true | 0:d4c9af9bd744 | 45 | |
true | 0:d4c9af9bd744 | 46 | ser.printf("\r\n"); |
true | 0:d4c9af9bd744 | 47 | } |
true | 0:d4c9af9bd744 | 48 | |
true | 0:d4c9af9bd744 | 49 | void setup(void) |
true | 0:d4c9af9bd744 | 50 | { |
true | 0:d4c9af9bd744 | 51 | RCC_OscInitTypeDef RCC_OscInitStruct; |
true | 0:d4c9af9bd744 | 52 | |
true | 3:d072d2d7f043 | 53 | // use internal osc - we don't need the external one for any reason |
true | 3:d072d2d7f043 | 54 | // and we want to also use an osc pin |
true | 0:d4c9af9bd744 | 55 | HAL_RCC_DeInit(); |
true | 0:d4c9af9bd744 | 56 | |
true | 0:d4c9af9bd744 | 57 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
true | 0:d4c9af9bd744 | 58 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
true | 0:d4c9af9bd744 | 59 | RCC_OscInitStruct.HSICalibrationValue = 16; |
true | 0:d4c9af9bd744 | 60 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
true | 0:d4c9af9bd744 | 61 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
true | 0:d4c9af9bd744 | 62 | |
true | 0:d4c9af9bd744 | 63 | // HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); |
true | 0:d4c9af9bd744 | 64 | |
true | 0:d4c9af9bd744 | 65 | HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); |
true | 0:d4c9af9bd744 | 66 | |
true | 0:d4c9af9bd744 | 67 | SystemCoreClockUpdate(); |
true | 0:d4c9af9bd744 | 68 | }; |
true | 0:d4c9af9bd744 | 69 | |
true | 0:d4c9af9bd744 | 70 | int main() |
true | 0:d4c9af9bd744 | 71 | { |
true | 0:d4c9af9bd744 | 72 | setup(); |
true | 0:d4c9af9bd744 | 73 | |
true | 0:d4c9af9bd744 | 74 | // reinitialize the serial port since the clock changed |
true | 0:d4c9af9bd744 | 75 | Serial ser(SERIAL_TX, SERIAL_RX); |
true | 0:d4c9af9bd744 | 76 | ser.baud(9600); |
true | 0:d4c9af9bd744 | 77 | |
true | 0:d4c9af9bd744 | 78 | // initialize the input ticker |
true | 0:d4c9af9bd744 | 79 | Ticker input_autoprint; |
true | 0:d4c9af9bd744 | 80 | |
true | 0:d4c9af9bd744 | 81 | ser.printf("\r\ntrue Nucleo IOReg for dwangoAC v0.1\r\n"); |
true | 0:d4c9af9bd744 | 82 | ser.printf("Running at %d Hz\r\n", SystemCoreClock); |
true | 0:d4c9af9bd744 | 83 | ser.printf("Use command \"i\" to print input pin state\r\n"); |
true | 0:d4c9af9bd744 | 84 | ser.printf("Use command \"oXX\" to set outputs where XX is raw 16bit value\r\n"); |
true | 0:d4c9af9bd744 | 85 | ser.printf(" 1 = short pin to GND, 0 = HiZ pin\r\n"); |
true | 0:d4c9af9bd744 | 86 | |
true | 0:d4c9af9bd744 | 87 | while (1) { |
true | 0:d4c9af9bd744 | 88 | if (ser.readable()) { |
true | 0:d4c9af9bd744 | 89 | switch (ser.getc()) { |
true | 0:d4c9af9bd744 | 90 | case 'i': { |
true | 0:d4c9af9bd744 | 91 | input_state_print(); |
true | 0:d4c9af9bd744 | 92 | break; |
true | 0:d4c9af9bd744 | 93 | } |
true | 0:d4c9af9bd744 | 94 | case 'I': { // automatically send input from 0.1 to 9.9 seconds |
true | 0:d4c9af9bd744 | 95 | uint8_t rate0 = ser.getc(); |
true | 0:d4c9af9bd744 | 96 | uint8_t rate1 = ser.getc(); |
true | 0:d4c9af9bd744 | 97 | |
true | 0:d4c9af9bd744 | 98 | if (rate0 >= 0x30 && rate0 <= 0x39) { |
true | 0:d4c9af9bd744 | 99 | rate0 -= 0x30; |
true | 0:d4c9af9bd744 | 100 | if (rate1 >= 0x30 && rate1 <= 0x39) { |
true | 0:d4c9af9bd744 | 101 | rate1 -= 0x30; |
true | 0:d4c9af9bd744 | 102 | uint8_t rate = (rate0 * 10) + rate1; |
true | 0:d4c9af9bd744 | 103 | |
true | 0:d4c9af9bd744 | 104 | if (rate == 0) { |
true | 0:d4c9af9bd744 | 105 | input_autoprint.detach(); |
true | 0:d4c9af9bd744 | 106 | } else { |
true | 0:d4c9af9bd744 | 107 | input_autoprint.attach(&input_state_print, 0.1 * rate); |
true | 0:d4c9af9bd744 | 108 | } |
true | 0:d4c9af9bd744 | 109 | } |
true | 0:d4c9af9bd744 | 110 | } |
true | 0:d4c9af9bd744 | 111 | |
true | 0:d4c9af9bd744 | 112 | break; |
true | 0:d4c9af9bd744 | 113 | } |
true | 0:d4c9af9bd744 | 114 | |
true | 0:d4c9af9bd744 | 115 | case 'o': { |
true | 0:d4c9af9bd744 | 116 | uint8_t bh = ser.getc(); |
true | 0:d4c9af9bd744 | 117 | uint8_t bl = ser.getc(); |
true | 0:d4c9af9bd744 | 118 | |
true | 0:d4c9af9bd744 | 119 | // set light confirming command receipt |
true | 0:d4c9af9bd744 | 120 | userled = 1; |
true | 0:d4c9af9bd744 | 121 | |
true | 0:d4c9af9bd744 | 122 | // we can give feedback to our program what is being set |
true | 0:d4c9af9bd744 | 123 | printf("o"BYTEBINPATTERN BYTEBINPATTERN"\r\n", BYTETOBIN(bh), BYTETOBIN(bl)); |
true | 0:d4c9af9bd744 | 124 | |
true | 0:d4c9af9bd744 | 125 | // actually set pins = 1 sets active low, 0 sets hiz |
true | 0:d4c9af9bd744 | 126 | uint16_t bits = bh << 8 | bl; |
true | 0:d4c9af9bd744 | 127 | |
true | 0:d4c9af9bd744 | 128 | for (int i = 0; i < 16; i++) { |
true | 0:d4c9af9bd744 | 129 | if (bits & (1 << i)) { |
true | 0:d4c9af9bd744 | 130 | // active low output |
true | 0:d4c9af9bd744 | 131 | out[i] = 0; |
true | 0:d4c9af9bd744 | 132 | out[i].output(); |
true | 0:d4c9af9bd744 | 133 | } else { |
true | 0:d4c9af9bd744 | 134 | // hi-z input |
true | 0:d4c9af9bd744 | 135 | out[i].input(); |
true | 0:d4c9af9bd744 | 136 | } |
true | 0:d4c9af9bd744 | 137 | } |
true | 0:d4c9af9bd744 | 138 | |
true | 0:d4c9af9bd744 | 139 | // wait a moment, unset light |
true | 0:d4c9af9bd744 | 140 | wait(0.2); |
true | 0:d4c9af9bd744 | 141 | userled = 0; |
true | 0:d4c9af9bd744 | 142 | break; |
true | 0:d4c9af9bd744 | 143 | } |
true | 0:d4c9af9bd744 | 144 | } |
true | 0:d4c9af9bd744 | 145 | } |
true | 0:d4c9af9bd744 | 146 | } |
true | 0:d4c9af9bd744 | 147 | } |