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@1:58e2c02413aa, 2015-09-25 (annotated)
- Committer:
- true
- Date:
- Fri Sep 25 04:14:31 2015 +0000
- Revision:
- 1:58e2c02413aa
- Parent:
- 0:d4c9af9bd744
- Child:
- 2:f12658f35bbd
Fixed PC14/PC15 usage, moved to the next available pins which are sadly in the Input section, but OUT14/OUT15 are next to IN14/IN15 if that is any help
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 | 0:d4c9af9bd744 | 21 | PC_5, PC_6, PC_8, PC_9, |
| true | 0:d4c9af9bd744 | 22 | PB_8, PB_9, PA_6, PA_7, |
| true | 0:d4c9af9bd744 | 23 | PB_6, PC_7, PA_9, PA_8, |
| true | 0:d4c9af9bd744 | 24 | PB_10, PB_4, PB_5, PB_3 |
| 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 | 1:58e2c02413aa | 29 | PH_0, PH_1, PC_2, PC_3, |
| true | 1:58e2c02413aa | 30 | PC_0, PC_1, PB_0, PA_4, |
| true | 1:58e2c02413aa | 31 | PA_1, PA_0, PA_2, PA_3 |
| 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 | } |
