/* * Application entry point. */ void main(void) { /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. */ halInit(); chSysInit(); /* * Activates the serial driver 1 using the driver default configuration. */ sdStart(&SD1, NULL); /* * Creates the blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); /* * Normal main() thread activity. */ while (TRUE) { if (palReadPad(IOPORT7, PG_BT5) == PAL_LOW) TestThread(&SD1); if (palReadPad(IOPORT7, PG_BT6) == PAL_LOW) sdWriteTimeout(&SD1, "Hello World!\r\n", 14, TIME_INFINITE); chThdSleepMilliseconds(1000); } }
static void println(char *p) { while (*p) { sdPut(&SD2, *p++); } sdWriteTimeout(&SD2, (uint8_t *)"\r\n", 2, TIME_INFINITE); }
bool fiveBaudInit(SerialDriver *sd) { uint8_t input_buf[3]; size_t bytes; // Set pin mode to GPIO palSetPadMode(PORT_KLINE_TX, PAD_KLINE_RX, PAL_MODE_INPUT_ANALOG); palSetPadMode(PORT_KLINE_TX, PAD_KLINE_TX, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); const uint8_t prio = chThdGetPriorityX(); chThdSetPriority(HIGHPRIO); K_HIGH(320); // As per ISO standard // Send 0x33 at 5 baud (00110011) // K-line level: |_--__--__-| K_LOW(200); // Low for 200ms - start bit K_HIGH(400); // High for 400ms - 00 K_LOW(400); // Low for 400ms - 11 K_HIGH(400); // High for 400ms - 00 K_LOW(400); // Low for 400ms - 11 K_HIGH(200); // High for 200ms - stop bit // Set pin mode back to UART palSetPadMode(PORT_KLINE_TX, PAD_KLINE_TX, PAL_MODE_ALTERNATE(7) | \ PAL_STM32_OSPEED_HIGHEST | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUPDR_PULLUP); palSetPadMode(PORT_KLINE_TX, PAD_KLINE_RX, PAL_MODE_ALTERNATE(7) | \ PAL_STM32_OTYPE_OPENDRAIN); chThdSetPriority(prio); // Revert back original priority chThdSleepMilliseconds(25); bytes = sdReadTimeout(sd, input_buf, sizeof(input_buf), MS2ST(500)); // 300ms max according to ISO9141 if (bytes != 3 || input_buf[0] != 0x55) { return false; } chThdSleepMilliseconds(35); // 25-50 ms pause per ISO standard uint8_t key = input_buf[2] ^ 0xFF; // Invert key byte sdWriteTimeout(sd, &key, 1, MS2ST(100)); chThdSleepMilliseconds(35); // 25-50 ms pause per ISO standard bytes = sdReadTimeout(sd, input_buf, 1, MS2ST(100)); if (bytes != 1 || input_buf[0] != 0xCC) { return false; } return true; }
CCM_FUNC static THD_FUNCTION(ThreadSDU, arg) { (void)arg; uint8_t in_buffer[SERIAL_BUFFERS_SIZE]; uint8_t out_buffer[SERIAL_BUFFERS_SIZE]; //uint8_t buffer_check[SERIAL_BUFFERS_SIZE/2]; size_t in, out; thread_t* th_shell = NULL; chRegSetThreadName("SDU"); while(USBD1.state != USB_READY) chThdSleepMilliseconds(10); while(SDU1.state != SDU_READY) chThdSleepMilliseconds(10); while(SD3.state != SD_READY) chThdSleepMilliseconds(10); // Enable K-line transceiver palSetPad(PORT_KLINE_CS, PAD_KLINE_CS); shellInit(); while (TRUE) { if (settings.serialMode == SERIAL_MODE_SHELL) { if (th_shell == NULL || chThdTerminatedX(th_shell)) { th_shell = shellCreateStatic(&shell_cfg1, waThreadShell, sizeof(waThreadShell), NORMALPRIO +1); } chThdSleepMilliseconds(10); continue; } if (settings.serialMode != SERIAL_MODE_KLINE) { chThdSleepMilliseconds(10); continue; } /* In case we stop it to change baudrate */ while (SD3.state != SD_READY) chThdSleepMilliseconds(10); if (doKLineInit && 0) { sdStop(&SD3); klineInit(false); //fiveBaudInit(&SD3); //sdReadTimeout(&SD3, buffer_check, 1, MS2ST(5)); // noise doKLineInit = false; sdStart(&SD3, &uart1Cfg); } in = chnReadTimeout(&SDU1, in_buffer, sizeof(in_buffer), TIME_IMMEDIATE); out = sdReadTimeout(&SD3, out_buffer, sizeof(out_buffer), TIME_IMMEDIATE); while (in == 0 && out == 0) { chThdSleepMilliseconds(1); in = chnReadTimeout(&SDU1, in_buffer, sizeof(in_buffer), TIME_IMMEDIATE); out = sdReadTimeout(&SD3, out_buffer, sizeof(out_buffer), TIME_IMMEDIATE); } if (in > 0) { sdWriteTimeout(&SD3, in_buffer, in, MS2ST(10)); } if (out > 0) { chnWriteTimeout(&SDU1, out_buffer, out, MS2ST(10)); } } return; }