1 /* Copyright (C) 2012-2013 Czech Technical University in Prague
4 * - Michal Horn <hornmich@fel.cvut.cz>
6 * This document contains proprietary information belonging to Czech
7 * Technical University in Prague. Passing on and copying of this
8 * document, and communication of its contents is not permitted
9 * without prior written authorization.
17 static boolean_t crlf_conv_enabled = TRUE;
23 sciSetBaudrate(serialLine, 115200);
26 void drv_sci_set_baudrate(uint32_t baud)
29 sciSetBaudrate(serialLine, baud);
32 /** Declared in ti_drv_sci.c */
33 extern tBuffer sciOutBuffer;
34 extern tBuffer sciInBuffer;
36 uint16_t drv_sci_available()
38 return (uint16_t)uxQueueMessagesWaiting(sciInBuffer.buf);
42 int8_t drv_sci_receive(uint32_t amount, uint8_t *buffer, portTickType wait)
44 if (sciInBuffer.buf == NULL)
47 if (xSemaphoreTake(sciInBuffer.mutex, wait) != pdTRUE)
50 // If non block mode, return if not enough data is available
52 if (drv_sci_available() < amount) {
53 xSemaphoreGive(sciInBuffer.mutex);
58 unsigned portBASE_TYPE status;
60 status = xQueueReceive(sciInBuffer.buf, &buffer[i], wait);
61 if (status != pdTRUE) {
62 xSemaphoreGive(sciInBuffer.mutex);
68 xSemaphoreGive(sciInBuffer.mutex);
72 static int crlf_conv(uint8_t ch_in, uint8_t *ch_out)
74 static bool was_cr = false;
76 if (crlf_conv_enabled)
77 if (ch_in == '\n' && !was_cr) {
80 return 0; // Retry the same char next time
83 was_cr = (ch_in == '\r');
84 return 1; // Move to the next character
90 * Sends data to the serial line without blocking. If the hardware TX
91 * buffer is not free, it busy-waits.
96 void drv_sci_send_imm(uint32_t length, uint8_t *data)
101 int ofs = crlf_conv(*data, &ch);
104 while (!(serialLine->FLR & SCI_TX_INT)) ; /* wait till previous char was sent */
109 int8_t drv_sci_send_try_append(uint32_t length, uint8_t *data)
113 if (sciOutBuffer.buf == NULL)
116 portBASE_TYPE ret = pdTRUE;
118 int ofs = crlf_conv(*data, &ch);
122 if (!sciOutBuffer.flags & BUF_TRANSFER_IN_PROGRESS)
123 if (!sciOutBuffer.flags & BUF_TRANSFER_IN_PROGRESS) {
124 sciOutBuffer.flags |= BUF_TRANSFER_IN_PROGRESS;
125 serialLine->SETINT = SCI_TX_INT; // Start new transfer by sending first byte
130 ret = xQueueSendToBackFromISR(sciOutBuffer.buf, (void *)&ch, NULL);
138 int8_t drv_sci_send(uint32_t length, uint8_t *data, portTickType wait)
142 if (sciOutBuffer.buf == NULL)
145 if (xSemaphoreTake(sciOutBuffer.mutex, wait) != pdTRUE)
148 portBASE_TYPE ret = pdTRUE;
150 int ofs = crlf_conv(*data, &ch);
154 if (!sciOutBuffer.flags & BUF_TRANSFER_IN_PROGRESS) {
155 taskENTER_CRITICAL();
156 if (!sciOutBuffer.flags & BUF_TRANSFER_IN_PROGRESS) {
157 sciOutBuffer.flags |= BUF_TRANSFER_IN_PROGRESS;
158 serialLine->SETINT = SCI_TX_INT; // Start new transfer by sending first byte
166 ret = xQueueSend(sciOutBuffer.buf, (void *)&ch, wait);
168 xSemaphoreGive(sciOutBuffer.mutex);
173 xSemaphoreGive(sciOutBuffer.mutex);
178 int8_t drv_sci_flush_buffer(tBuffer *buffer)
180 if (uxQueueMessagesWaiting(buffer->buf) == 0)
182 xSemaphoreTake(buffer->mutex, portMAX_DELAY);
183 // FIXME: FreeRTOS 7.0.2 doesn't have xQueueReset
184 //xQueueReset(buffer->buf);
187 while (uxQueueMessagesWaiting(buffer->buf) > 0)
188 xQueueReceive(buffer->buf, &dummy, 0);
190 xSemaphoreGive(buffer->mutex);
194 int8_t drv_sci_flush(boolean_t buf)
197 return drv_sci_flush_buffer(&sciInBuffer);
198 return drv_sci_flush_buffer(&sciOutBuffer);
201 void drv_sci_set_crlf_conv_en(boolean_t enable)
203 crlf_conv_enabled = enable;
206 boolean_t drv_sci_get_crlf_conv_en()
208 return crlf_conv_enabled;