9 void drv_sci_set_baudrate(uint32_t baud)
12 sciSetBaudrate(sciREG, baud);
15 /** Declared in ti_drv_sci.c */
16 extern tBuffer sciOutBuffer;
17 extern tBuffer sciInBuffer;
19 uint16_t drv_sci_available()
21 return (uint16_t)uxQueueMessagesWaiting(sciInBuffer.buf);
25 int8_t drv_sci_receive(uint32_t amount, uint8_t* buffer, portTickType wait)
27 if(sciInBuffer.buf == NULL) {
31 if(xSemaphoreTake(sciInBuffer.mutex, wait) != pdTRUE) {
35 // If non block mode, return if not enough data is available
37 if(drv_sci_available() < amount) {
38 xSemaphoreGive(sciInBuffer.mutex);
44 unsigned portBASE_TYPE status;
46 status = xQueueReceive(sciInBuffer.buf, &buffer[i], wait);
47 if(status != pdTRUE) {
48 xSemaphoreGive(sciInBuffer.mutex);
54 xSemaphoreGive(sciInBuffer.mutex);
58 static int crlf_conv(uint8_t ch_in, uint8_t *ch_out)
60 static bool was_cr = false;
61 if (ch_in == '\n' && !was_cr) {
64 return 0; // Retry the same char next time
67 was_cr = (ch_in == '\r');
68 return 1; // Move to the next character
71 int8_t drv_sci_send_imm(uint32_t length, uint8_t* data)
75 int ofs = crlf_conv(*data, &ch);
78 while(!(sciREG->FLR & SCI_TX_INT)); /* wait till previous char was sent */
84 int8_t drv_sci_send_try_append(uint32_t length, uint8_t* data)
88 if(sciOutBuffer.buf == NULL) {
92 portBASE_TYPE ret = pdTRUE;
94 int ofs = crlf_conv(*data, &ch);
98 if(!sciOutBuffer.flags & BUF_TRANSFER_IN_PROGRESS) {
99 if (!sciOutBuffer.flags & BUF_TRANSFER_IN_PROGRESS) {
100 sciOutBuffer.flags |= BUF_TRANSFER_IN_PROGRESS;
101 sciREG->SETINT = SCI_TX_INT; // Start new transfer by sending first byte
107 ret = xQueueSendToBackFromISR(sciOutBuffer.buf, (void*)&ch, NULL);
116 int8_t drv_sci_send(uint32_t length, uint8_t* data, portTickType wait)
119 if(sciOutBuffer.buf == NULL) {
123 if(xSemaphoreTake(sciOutBuffer.mutex, wait) != pdTRUE) {
127 portBASE_TYPE ret = pdTRUE;
129 int ofs = crlf_conv(*data, &ch);
133 if(!sciOutBuffer.flags & BUF_TRANSFER_IN_PROGRESS) {
134 taskENTER_CRITICAL();
135 if (!sciOutBuffer.flags & BUF_TRANSFER_IN_PROGRESS) {
136 sciOutBuffer.flags |= BUF_TRANSFER_IN_PROGRESS;
137 sciREG->SETINT = SCI_TX_INT; // Start new transfer by sending first byte
145 ret = xQueueSend(sciOutBuffer.buf, (void*)&ch, wait);
147 xSemaphoreGive(sciOutBuffer.mutex);
152 xSemaphoreGive(sciOutBuffer.mutex);
157 int8_t drv_sci_flush_buffer(tBuffer* buffer)
159 if(uxQueueMessagesWaiting(buffer->buf) == 0) {
162 xSemaphoreTake(buffer->mutex, portMAX_DELAY);
163 // FIXME: FreeRTOS 7.0.2 doesn't have xQueueReset
164 //xQueueReset(buffer->buf);
167 while(uxQueueMessagesWaiting(buffer->buf) > 0) {
168 xQueueReceive(buffer->buf, &dummy, 0);
171 xSemaphoreGive(buffer->mutex);
175 int8_t drv_sci_flush(boolean_t buf)
178 return drv_sci_flush_buffer(&sciInBuffer);
180 return drv_sci_flush_buffer(&sciOutBuffer);