void send_cb(void *arg, const void *p, size_t len) { if (len > 0) { BaseChannel *ch = (BaseChannel *)arg; chnWriteTimeout(ch, p, len, MS2ST(100)); } }
/* * Application entry point. */ int main(void) { /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. */ halInit(); /* * Enabling interrupts, initialization done. */ osalSysEnable(); /* * Activates the serial driver 2 using the driver default configuration. * PA2(TX) and PA3(RX) are routed to USART2. */ sdStart(&SD2, NULL); palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7)); palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7)); /* * Normal main() thread activity, in this demo it just performs * a shell respawn upon its termination. */ while (true) { chnWriteTimeout(&SD2, (uint8_t *)"Hello World!\r\n", 14, TIME_INFINITE); palSetPad(GPIOD, GPIOD_LED3); /* Orange. */ osalThreadSleepMilliseconds(500); palClearPad(GPIOD, GPIOD_LED3); /* Orange. */ osalThreadSleepMilliseconds(500); } }
size_t USBSendData(uint8_t *data, size_t size, systime_t timeout) { size_t sent; sent = chnWriteTimeout(&SDU1, data, size, timeout); return sent; }
void consoleOutputBuffer(const uint8_t *buf, int size) { lastWriteSize = size; #if !EFI_UART_ECHO_TEST_MODE lastWriteActual = chnWriteTimeout(getConsoleChannel(), buf, size, CONSOLE_WRITE_TIMEOUT); // if (r != size) // firmwareError(OBD_PCM_Processor_Fault, "Partial console write"); #endif /* EFI_UART_ECHO_TEST_MODE */ }
void tunerStudioWriteData(ts_channel_s *tsChannel, const uint8_t * buffer, int size) { efiAssertVoid(getRemainingStack(chThdSelf()) > 64, "tunerStudioWriteData"); #if EFI_SIMULATOR || defined(__DOXYGEN__) logMsg("chSequentialStreamWrite [%d]\r\n", size); #endif int transferred = chnWriteTimeout(tsChannel->channel, buffer, size, BINARY_IO_TIMEOUT); #if EFI_SIMULATOR || defined(__DOXYGEN__) logMsg("transferred [%d]\r\n", transferred); #endif if (transferred != size) { #if EFI_SIMULATOR || defined(__DOXYGEN__) logMsg("!!! NOT ACCEPTED %d out of %d !!!", transferred, size); #endif scheduleMsg(&tsLogger, "!!! NOT ACCEPTED %d out of %d !!!", transferred, size); } }
THD_FUNCTION(Thread2, arg) { (void)arg; /* * Activates the serial driver 1 using the driver default configuration. */ sdStart(&SD1, NULL); /* Welcome message.*/ chnWriteTimeout(&SD1, (uint8_t *)"Hello World!\r\n", 14, TIME_INFINITE); /* Waiting for button push and activation of the test suite.*/ while (true) { if (palReadPad(PORT_E, PE_BUTTON1)) test_execute((BaseSequentialStream *)&SD1); chThdSleepMilliseconds(500); } }
/** * Application entry point. */ int 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. * PA9 and PA10 are routed to USART1. */ sdStart(&SD1, &serialConfig); palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(1)); /* USART1 TX. */ palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(1)); /* USART1 RX. */ /** * Creates the blinker threads. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL); /** * Normal main() thread activity, in this demo it does nothing except * sleeping in a loop and check the button state, when the button is * pressed the test procedure is launched with output on the serial * driver 1. */ while (TRUE) { if (palReadPad(GPIOA, GPIOA_BUTTON)) { chnWriteTimeout(&SD1, (const uint8_t*)"Test Message!", 13, TIME_INFINITE); } chThdSleepMilliseconds(500); } return 0; }
void sr5WriteData(ts_channel_s *tsChannel, const uint8_t * buffer, int size) { efiAssertVoid(CUSTOM_ERR_6570, getRemainingStack(chThdGetSelfX()) > 64, "tunerStudioWriteData"); #if EFI_SIMULATOR || defined(__DOXYGEN__) logMsg("chSequentialStreamWrite [%d]\r\n", size); #endif #if TS_UART_DMA_MODE && EFI_PROD_CODE UNUSED(tsChannel); int transferred = size; uartSendTimeout(TS_DMA_UART_DEVICE, (size_t *)&transferred, buffer, BINARY_IO_TIMEOUT); #else if (tsChannel->channel == NULL) return; // int transferred = chnWriteTimeout(tsChannel->channel, buffer, size, BINARY_IO_TIMEOUT); // temporary attempt to work around #553 // instead of one huge packet let's try sending a few smaller packets int transferred = 0; int stillToTransfer = size; while (stillToTransfer > 0) { int thisTransferSize = minI(stillToTransfer, 768); transferred += chnWriteTimeout(tsChannel->channel, buffer, thisTransferSize, BINARY_IO_TIMEOUT); buffer += thisTransferSize; stillToTransfer -= thisTransferSize; } #endif #if EFI_SIMULATOR || defined(__DOXYGEN__) logMsg("transferred [%d]\r\n", transferred); #endif if (transferred != size) { #if EFI_SIMULATOR || defined(__DOXYGEN__) logMsg("!!! NOT ACCEPTED %d out of %d !!!", transferred, size); #endif /* EFI_SIMULATOR */ scheduleMsg(&tsLogger, "!!! NOT ACCEPTED %d out of %d !!!", transferred, size); } }
//Wireless static void cmd_nrf(BaseSequentialStream *chp, int argc, char *argv[]) { if(*(argv[0]) == 'c'){ channel = strtol(argv[1], NULL, 0); nrf24l01SetChannel(&nrf24l01, channel); chprintf((BaseSequentialStream*)&SD1, "Channel set to %d.\n\r", channel); } else if(*(argv[0]) == 'a'){ if(strlen(argv[1]) == 5){ int i = 0; uint8_t add[5]; for(i; i<5; i++){ addr[i] = *(argv[1]+i); } serialOutBuf[0] = msg_seq; serialOutBuf[2] = addr[0]; serialOutBuf[3] = addr[1]; serialOutBuf[4] = addr[2]; serialOutBuf[5] = addr[3]; serialOutBuf[6] = addr[4]; nrf24l01SetRXAddress(&nrf24l01, 0, addr); chprintf((BaseSequentialStream*)&SD1, "Personal Address set to %s.\n\r", addr); //addr = add; }else { chprintf((BaseSequentialStream*)&SD1, "Not an appropriate length address."); } } else if(*(argv[0]) == 't'){ int i=0; for(i; i<5; i++){ target_addr[i] = *(argv[1] + i); } chprintf((BaseSequentialStream*)&SD1, "%s.\n\r", target_addr); //chprintf((BaseSequentialStream*)&SD1, "Sending..."); nrf24l01SetTXAddress(&nrf24l01, target_addr); int msg_size = strlen(argv[2]); serialOutBuf[0] = msg_seq; serialOutBuf[7] = ','; serialOutBuf[8] = msg_type; serialOutBuf[9] = ','; serialOutBuf[10] = msg_size; serialOutBuf[11] = ','; for(i=0;i<(msg_size);i++){ serialOutBuf[i+12] = *(argv[2] + i); } wait_for_ack = 1; for(i=0; i<5; i++){ chMtxLock(&nrfMutex); chnWriteTimeout(&nrf24l01.channels[0], serialOutBuf, 32, MS2ST(100)); chMtxUnlock(&nrfMutex); chThdSleepMilliseconds(200); if (!wait_for_ack) break; chprintf((BaseSequentialStream*)&SD1, "failed to send %d\n\r", i); } msg_seq = msg_seq ^ 1; } else if(*(argv[0]) == 'r'){ if (*(argv[1]) == 'l'){ int i,j; for(i=0; i<iter; i++){ chprintf((BaseSequentialStream*)&SD1, "%d. <", i+1); for(j=0; j<5; j++){ chprintf((BaseSequentialStream*)&SD1, "%c", rec_list[(iter-1)-i][j]); } chprintf((BaseSequentialStream*)&SD1, ">, <%d> \n\r", rec_list[(iter-1)-i][5]); } }else{ int j; fifo = (fifo+1)%10; if(fifo != iter){ for(j=0; j<5; j++){ chprintf((BaseSequentialStream*)&SD1, "%c", rec_list[fifo][j]); } chprintf((BaseSequentialStream*)&SD1, ": " ); for(j=0; j<(serialInBuf[10]);j++){ chprintf((BaseSequentialStream*)&SD1, "%c ", serialInBuf[12+j]); } chprintf((BaseSequentialStream*)&SD1, "\n\r"); } for(j=0; j<26; j++){ rec_list[fifo][j] = 0; } } } }
static msg_t receiverThread(void *arg) { int i; UNUSED (arg); chRegSetThreadName("receiver"); while (TRUE) { chMtxLock(&nrfMutex); size_t s = chnReadTimeout(&nrf24l01.channels[0], serialInBuf, 32, MS2ST(10)); chMtxUnlock(&nrfMutex); if (s) { if(!wait_for_ack){ // serialOutBuf[0]= serialInBuf[0]; serialInBuf[8]= 1; for(i=0; i<5; i++){ target_addr[i] = serialInBuf[2+i]; } nrf24l01SetTXAddress(&nrf24l01, target_addr); chMtxLock(&nrfMutex); chnWriteTimeout(&nrf24l01.channels[0], serialInBuf, 32, MS2ST(100)); chMtxUnlock(&nrfMutex); if(wait_for_ack){ break; } for(i=0;i<5; i++){ rec_list[iter][i] = serialInBuf[2+i]; } rec_list[iter][5] = serialInBuf[10]; for(i=0; i<(serialInBuf[10]);i++){ rec_list[iter][6+i] = serialInBuf[12+i]; } iter = (iter+1)%10; for (i=0;i<(int)s;i++) { chprintf((BaseSequentialStream*)&SD1, "%c ", serialInBuf[i]); } chprintf((BaseSequentialStream*)&SD1, "\n\r", s); } if(wait_for_ack){ if((serialInBuf[0] = msg_seq) && (serialInBuf[8]==1)){ wait_for_ack = 0; chprintf((BaseSequentialStream*)&SD1, "Acknowledged.\n\r"); } //else keep trying to send. } } chSchDoYieldS(); } return 0; }
static THD_FUNCTION(Thread1, arg) { (void)arg; /* * Activates the serial driver 1 using the driver default configuration. */ sdStart(&SD1, NULL); while (TRUE) { unsigned i; chnWriteTimeout(&SD1, (uint8_t *)"Hello World!\r\n", 14, TIME_INFINITE); for (i = 0; i < 4; i++) { palClearPad(PORT_E, PE_LED1); chThdSleepMilliseconds(100); palClearPad(PORT_E, PE_LED2); chThdSleepMilliseconds(100); palClearPad(PORT_E, PE_LED3); chThdSleepMilliseconds(100); palClearPad(PORT_E, PE_LED4); chThdSleepMilliseconds(100); palSetPad(PORT_E, PE_LED1); chThdSleepMilliseconds(100); palSetPad(PORT_E, PE_LED2); chThdSleepMilliseconds(100); palSetPad(PORT_E, PE_LED3); chThdSleepMilliseconds(100); palSetPad(PORT_E, PE_LED4); chThdSleepMilliseconds(300); } for (i = 0; i < 4; i++) { palTogglePort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED3) | PAL_PORT_BIT(PE_LED4)); chThdSleepMilliseconds(500); palTogglePort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED3) | PAL_PORT_BIT(PE_LED4)); chThdSleepMilliseconds(500); } for (i = 0; i < 4; i++) { palTogglePad(PORT_E, PE_LED1); chThdSleepMilliseconds(250); palTogglePad(PORT_E, PE_LED1); palTogglePad(PORT_E, PE_LED2); chThdSleepMilliseconds(250); palTogglePad(PORT_E, PE_LED2); palTogglePad(PORT_E, PE_LED3); chThdSleepMilliseconds(250); palTogglePad(PORT_E, PE_LED3); palTogglePad(PORT_E, PE_LED4); chThdSleepMilliseconds(250); palTogglePad(PORT_E, PE_LED4); } for (i = 0; i < 4; i++) { palClearPort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED3)); palSetPort(PORT_E, PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED4)); chThdSleepMilliseconds(500); palClearPort(PORT_E, PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED4)); palSetPort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED3)); chThdSleepMilliseconds(500); } palSetPort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED3) | PAL_PORT_BIT(PE_LED4)); } }
void consolePutChar(int x) { chnWriteTimeout(getConsoleChannel(), (const uint8_t *)&x, 1, CONSOLE_WRITE_TIMEOUT); }
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; }