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
72 int8_t drv_sci_send(uint32_t length, uint8_t* data, portTickType wait)
75 if(sciOutBuffer.buf == NULL) {
79 if(xSemaphoreTake(sciOutBuffer.mutex, wait) != pdTRUE) {
83 portBASE_TYPE ret = pdTRUE;
85 int ofs = crlf_conv(*data, &ch);
89 if(!sciOutBuffer.flags & BUF_TRANSFER_IN_PROGRESS) {
91 if (!sciOutBuffer.flags & BUF_TRANSFER_IN_PROGRESS) {
92 sciOutBuffer.flags |= BUF_TRANSFER_IN_PROGRESS;
93 sciREG->SETINT = SCI_TX_INT; // Start new transfer by sending first byte
101 ret = xQueueSend(sciOutBuffer.buf, (void*)&ch, wait);
103 xSemaphoreGive(sciOutBuffer.mutex);
108 xSemaphoreGive(sciOutBuffer.mutex);
113 int8_t drv_sci_flush_buffer(tBuffer* buffer)
115 if(uxQueueMessagesWaiting(buffer->buf) == 0) {
118 xSemaphoreTake(buffer->mutex, portMAX_DELAY);
119 // FIXME: FreeRTOS 7.0.2 doesn't have xQueueReset
120 //xQueueReset(buffer->buf);
123 while(uxQueueMessagesWaiting(buffer->buf) > 0) {
124 xQueueReceive(buffer->buf, &dummy, 0);
127 xSemaphoreGive(buffer->mutex);
131 int8_t drv_sci_flush(boolean_t buf)
134 return drv_sci_flush_buffer(&sciInBuffer);
136 return drv_sci_flush_buffer(&sciOutBuffer);