Trueserve Dot Org / Mbed 2 deprecated Nucleo_IOReg

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 //------------------------------------
00004 // 9600 baud, 8-bit data, no parity
00005 //------------------------------------
00006 
00007 #define BYTEBINPATTERN "%d%d%d%d%d%d%d%d"
00008 #define BYTETOBIN(byte)  \
00009   (byte & 0x80 ? 1 : 0), \
00010   (byte & 0x40 ? 1 : 0), \
00011   (byte & 0x20 ? 1 : 0), \
00012   (byte & 0x10 ? 1 : 0), \
00013   (byte & 0x08 ? 1 : 0), \
00014   (byte & 0x04 ? 1 : 0), \
00015   (byte & 0x02 ? 1 : 0), \
00016   (byte & 0x01 ? 1 : 0)
00017 
00018 Serial ser(SERIAL_TX, SERIAL_RX);
00019 
00020 DigitalIn in[16] = {
00021     PB_12, PA_11, PA_12, PC_5,
00022     PC_6, PC_8, PC_9, PB_8,
00023     PB_9, PA_6, PA_7, PB_6,
00024     PC_7, PA_9, PA_8, PB_10
00025 };
00026     
00027 DigitalInOut out[16] = {
00028     PD_2, PC_11, PC_10, PC_12,
00029     PH_1, PC_2, PC_3, PC_0,
00030     PC_1, PB_0, PA_4, PA_1,
00031     PA_0, PB_3, PB_5, PB_4
00032 };
00033 
00034 DigitalOut userled(LED1);
00035 
00036 //------------------------------------
00037 
00038 void input_state_print()
00039 {
00040     ser.printf("i");
00041         
00042     for (int i = 0; i < 16; i++) {
00043         ser.printf("%c", in[i] ? 0x31 : 0x30);
00044     }
00045         
00046     ser.printf("\r\n");
00047 }
00048 
00049 void setup(void)
00050 {
00051   RCC_OscInitTypeDef RCC_OscInitStruct;
00052 
00053   // use internal osc - we don't need the external one for any reason
00054   // and we want to also use an osc pin
00055   HAL_RCC_DeInit();
00056   
00057   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
00058   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
00059   RCC_OscInitStruct.HSICalibrationValue = 16;
00060   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
00061   HAL_RCC_OscConfig(&RCC_OscInitStruct);
00062   
00063   // HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
00064 
00065   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
00066   
00067   SystemCoreClockUpdate();
00068 };
00069 
00070 int main()
00071 {
00072     setup();
00073     
00074     // reinitialize the serial port since the clock changed
00075     Serial ser(SERIAL_TX, SERIAL_RX);
00076     ser.baud(9600);
00077     
00078     // initialize the input ticker
00079     Ticker input_autoprint;
00080     
00081     ser.printf("\r\ntrue Nucleo IOReg for dwangoAC v0.1\r\n");
00082     ser.printf("Running at %d Hz\r\n", SystemCoreClock);
00083     ser.printf("Use command \"i\" to print input pin state\r\n");
00084     ser.printf("Use command \"oXX\" to set outputs where XX is raw 16bit value\r\n");
00085     ser.printf("  1 = short pin to GND, 0 = HiZ pin\r\n");
00086     
00087     while (1) {
00088         if (ser.readable()) {
00089             switch (ser.getc()) {
00090                 case 'i': {
00091                     input_state_print();
00092                     break;
00093                 }
00094                 case 'I': {     // automatically send input from 0.1 to 9.9 seconds
00095                     uint8_t rate0 = ser.getc();
00096                     uint8_t rate1 = ser.getc();
00097                     
00098                     if (rate0 >= 0x30 && rate0 <= 0x39) {
00099                         rate0 -= 0x30;
00100                         if (rate1 >= 0x30 && rate1 <= 0x39) {
00101                             rate1 -= 0x30;
00102                             uint8_t rate = (rate0 * 10) + rate1;
00103                             
00104                             if (rate == 0) {
00105                                 input_autoprint.detach();
00106                             } else {
00107                                 input_autoprint.attach(&input_state_print, 0.1 * rate);   
00108                             }    
00109                         }    
00110                     }
00111                     
00112                     break;
00113                 }
00114                 
00115                 case 'o': {
00116                     uint8_t bh = ser.getc();
00117                     uint8_t bl = ser.getc();
00118                     
00119                     // set light confirming command receipt
00120                     userled = 1;
00121                     
00122                     // we can give feedback to our program what is being set
00123                     printf("o"BYTEBINPATTERN BYTEBINPATTERN"\r\n", BYTETOBIN(bh), BYTETOBIN(bl));
00124                     
00125                     // actually set pins = 1 sets active low, 0 sets hiz
00126                     uint16_t bits = bh << 8 | bl;
00127                     
00128                     for (int i = 0; i < 16; i++) {
00129                         if (bits & (1 << i)) {
00130                             // active low output
00131                             out[i] = 0;
00132                             out[i].output();
00133                         } else {
00134                             // hi-z input
00135                             out[i].input();
00136                         }
00137                     }
00138                     
00139                     // wait a moment, unset light
00140                     wait(0.2);
00141                     userled = 0;
00142                     break;
00143                 }
00144             }
00145         }
00146     }
00147 }