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.
Fork of Nucleo_IOReg by
main.cpp@3:b6d0e0830e82, 2015-11-05 (annotated)
- Committer:
- dwangoAC
- Date:
- Thu Nov 05 00:33:50 2015 +0000
- Revision:
- 3:b6d0e0830e82
- Parent:
- 2:f12658f35bbd
Shift output pins by 1 to work around PH_0 not functioning.
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, |
| dwangoAC | 3:b6d0e0830e82 | 29 | PH_1, PC_2, PC_3, PC_0, |
| dwangoAC | 3:b6d0e0830e82 | 30 | PC_1, PB_0, PA_4, PA_1, |
| dwangoAC | 3:b6d0e0830e82 | 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 | 0:d4c9af9bd744 | 53 | HAL_RCC_DeInit(); |
| true | 0:d4c9af9bd744 | 54 | |
| true | 0:d4c9af9bd744 | 55 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
| true | 0:d4c9af9bd744 | 56 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
| true | 0:d4c9af9bd744 | 57 | RCC_OscInitStruct.HSICalibrationValue = 16; |
| true | 0:d4c9af9bd744 | 58 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; |
| true | 0:d4c9af9bd744 | 59 | HAL_RCC_OscConfig(&RCC_OscInitStruct); |
| true | 0:d4c9af9bd744 | 60 | |
| true | 0:d4c9af9bd744 | 61 | // HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); |
| true | 0:d4c9af9bd744 | 62 | |
| true | 0:d4c9af9bd744 | 63 | HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); |
| true | 0:d4c9af9bd744 | 64 | |
| true | 0:d4c9af9bd744 | 65 | SystemCoreClockUpdate(); |
| true | 0:d4c9af9bd744 | 66 | }; |
| true | 0:d4c9af9bd744 | 67 | |
| true | 0:d4c9af9bd744 | 68 | int main() |
| true | 0:d4c9af9bd744 | 69 | { |
| true | 0:d4c9af9bd744 | 70 | setup(); |
| true | 0:d4c9af9bd744 | 71 | |
| true | 0:d4c9af9bd744 | 72 | // reinitialize the serial port since the clock changed |
| true | 0:d4c9af9bd744 | 73 | Serial ser(SERIAL_TX, SERIAL_RX); |
| true | 0:d4c9af9bd744 | 74 | ser.baud(9600); |
| true | 0:d4c9af9bd744 | 75 | |
| true | 0:d4c9af9bd744 | 76 | // initialize the input ticker |
| true | 0:d4c9af9bd744 | 77 | Ticker input_autoprint; |
| true | 0:d4c9af9bd744 | 78 | |
| true | 0:d4c9af9bd744 | 79 | ser.printf("\r\ntrue Nucleo IOReg for dwangoAC v0.1\r\n"); |
| true | 0:d4c9af9bd744 | 80 | ser.printf("Running at %d Hz\r\n", SystemCoreClock); |
| true | 0:d4c9af9bd744 | 81 | ser.printf("Use command \"i\" to print input pin state\r\n"); |
| true | 0:d4c9af9bd744 | 82 | ser.printf("Use command \"oXX\" to set outputs where XX is raw 16bit value\r\n"); |
| true | 0:d4c9af9bd744 | 83 | ser.printf(" 1 = short pin to GND, 0 = HiZ pin\r\n"); |
| true | 0:d4c9af9bd744 | 84 | |
| true | 0:d4c9af9bd744 | 85 | while (1) { |
| true | 0:d4c9af9bd744 | 86 | if (ser.readable()) { |
| true | 0:d4c9af9bd744 | 87 | switch (ser.getc()) { |
| true | 0:d4c9af9bd744 | 88 | case 'i': { |
| true | 0:d4c9af9bd744 | 89 | input_state_print(); |
| true | 0:d4c9af9bd744 | 90 | break; |
| true | 0:d4c9af9bd744 | 91 | } |
| true | 0:d4c9af9bd744 | 92 | case 'I': { // automatically send input from 0.1 to 9.9 seconds |
| true | 0:d4c9af9bd744 | 93 | uint8_t rate0 = ser.getc(); |
| true | 0:d4c9af9bd744 | 94 | uint8_t rate1 = ser.getc(); |
| true | 0:d4c9af9bd744 | 95 | |
| true | 0:d4c9af9bd744 | 96 | if (rate0 >= 0x30 && rate0 <= 0x39) { |
| true | 0:d4c9af9bd744 | 97 | rate0 -= 0x30; |
| true | 0:d4c9af9bd744 | 98 | if (rate1 >= 0x30 && rate1 <= 0x39) { |
| true | 0:d4c9af9bd744 | 99 | rate1 -= 0x30; |
| true | 0:d4c9af9bd744 | 100 | uint8_t rate = (rate0 * 10) + rate1; |
| true | 0:d4c9af9bd744 | 101 | |
| true | 0:d4c9af9bd744 | 102 | if (rate == 0) { |
| true | 0:d4c9af9bd744 | 103 | input_autoprint.detach(); |
| true | 0:d4c9af9bd744 | 104 | } else { |
| true | 0:d4c9af9bd744 | 105 | input_autoprint.attach(&input_state_print, 0.1 * rate); |
| true | 0:d4c9af9bd744 | 106 | } |
| true | 0:d4c9af9bd744 | 107 | } |
| true | 0:d4c9af9bd744 | 108 | } |
| true | 0:d4c9af9bd744 | 109 | |
| true | 0:d4c9af9bd744 | 110 | break; |
| true | 0:d4c9af9bd744 | 111 | } |
| true | 0:d4c9af9bd744 | 112 | |
| true | 0:d4c9af9bd744 | 113 | case 'o': { |
| true | 0:d4c9af9bd744 | 114 | uint8_t bh = ser.getc(); |
| true | 0:d4c9af9bd744 | 115 | uint8_t bl = ser.getc(); |
| true | 0:d4c9af9bd744 | 116 | |
| true | 0:d4c9af9bd744 | 117 | // set light confirming command receipt |
| true | 0:d4c9af9bd744 | 118 | userled = 1; |
| true | 0:d4c9af9bd744 | 119 | |
| true | 0:d4c9af9bd744 | 120 | // we can give feedback to our program what is being set |
| true | 0:d4c9af9bd744 | 121 | printf("o"BYTEBINPATTERN BYTEBINPATTERN"\r\n", BYTETOBIN(bh), BYTETOBIN(bl)); |
| true | 0:d4c9af9bd744 | 122 | |
| true | 0:d4c9af9bd744 | 123 | // actually set pins = 1 sets active low, 0 sets hiz |
| true | 0:d4c9af9bd744 | 124 | uint16_t bits = bh << 8 | bl; |
| true | 0:d4c9af9bd744 | 125 | |
| true | 0:d4c9af9bd744 | 126 | for (int i = 0; i < 16; i++) { |
| true | 0:d4c9af9bd744 | 127 | if (bits & (1 << i)) { |
| true | 0:d4c9af9bd744 | 128 | // active low output |
| true | 0:d4c9af9bd744 | 129 | out[i] = 0; |
| true | 0:d4c9af9bd744 | 130 | out[i].output(); |
| true | 0:d4c9af9bd744 | 131 | } else { |
| true | 0:d4c9af9bd744 | 132 | // hi-z input |
| true | 0:d4c9af9bd744 | 133 | out[i].input(); |
| true | 0:d4c9af9bd744 | 134 | } |
| true | 0:d4c9af9bd744 | 135 | } |
| true | 0:d4c9af9bd744 | 136 | |
| true | 0:d4c9af9bd744 | 137 | // wait a moment, unset light |
| true | 0:d4c9af9bd744 | 138 | wait(0.2); |
| true | 0:d4c9af9bd744 | 139 | userled = 0; |
| true | 0:d4c9af9bd744 | 140 | break; |
| true | 0:d4c9af9bd744 | 141 | } |
| true | 0:d4c9af9bd744 | 142 | } |
| true | 0:d4c9af9bd744 | 143 | } |
| true | 0:d4c9af9bd744 | 144 | } |
| true | 0:d4c9af9bd744 | 145 | } |
