@@ -205,57 +205,55 @@ static void modem_cmux_bus_callback(struct modem_pipe *pipe, enum modem_pipe_eve
205205static uint16_t modem_cmux_transmit_frame (struct modem_cmux * cmux ,
206206 const struct modem_cmux_frame * frame )
207207{
208- uint8_t byte ;
208+ uint8_t buf [ MODEM_CMUX_FRAME_SIZE_MAX ] ;
209209 uint8_t fcs ;
210210 uint16_t space ;
211211 uint16_t data_len ;
212+ uint16_t buf_idx ;
212213
213214 space = ring_buf_space_get (& cmux -> transmit_rb ) - MODEM_CMUX_FRAME_SIZE_MAX ;
214215 data_len = (space < frame -> data_len ) ? space : frame -> data_len ;
215216
216217 /* SOF */
217- byte = 0xF9 ;
218- ring_buf_put (& cmux -> transmit_rb , & byte , 1 );
218+ buf [0 ] = 0xF9 ;
219219
220220 /* DLCI Address (Max 63) */
221- byte = 0x01 | (frame -> cr << 1 ) | (frame -> dlci_address << 2 );
222- fcs = crc8 (& byte , 1 , MODEM_CMUX_FCS_POLYNOMIAL , MODEM_CMUX_FCS_INIT_VALUE , true);
223- ring_buf_put (& cmux -> transmit_rb , & byte , 1 );
221+ buf [1 ] = 0x01 | (frame -> cr << 1 ) | (frame -> dlci_address << 2 );
224222
225223 /* Frame type and poll/final */
226- byte = frame -> type | (frame -> pf << 4 );
227- fcs = crc8 (& byte , 1 , MODEM_CMUX_FCS_POLYNOMIAL , fcs , true);
228- ring_buf_put (& cmux -> transmit_rb , & byte , 1 );
224+ buf [2 ] = frame -> type | (frame -> pf << 4 );
229225
230226 /* Data length */
231227 if (data_len > 127 ) {
232- byte = data_len << 1 ;
233- fcs = crc8 (& byte , 1 , MODEM_CMUX_FCS_POLYNOMIAL , fcs , true);
234- ring_buf_put (& cmux -> transmit_rb , & byte , 1 );
235- byte = data_len >> 7 ;
236- ring_buf_put (& cmux -> transmit_rb , & byte , 1 );
228+ buf [3 ] = data_len << 1 ;
229+ buf [4 ] = data_len >> 7 ;
230+ buf_idx = 5 ;
237231 } else {
238- byte = 0x01 | (data_len << 1 );
239- ring_buf_put ( & cmux -> transmit_rb , & byte , 1 ) ;
232+ buf [ 3 ] = 0x01 | (data_len << 1 );
233+ buf_idx = 4 ;
240234 }
241235
236+ /* Compute FCS for the header (exclude SOF) */
237+ fcs = crc8 (& buf [1 ], (buf_idx - 1 ), MODEM_CMUX_FCS_POLYNOMIAL , MODEM_CMUX_FCS_INIT_VALUE ,
238+ true);
239+
242240 /* FCS final */
243241 if (frame -> type == MODEM_CMUX_FRAME_TYPE_UIH ) {
244- fcs = 0xFF - crc8 ( & byte , 1 , MODEM_CMUX_FCS_POLYNOMIAL , fcs , true) ;
242+ fcs = 0xFF - fcs ;
245243 } else {
246- fcs = crc8 (& byte , 1 , MODEM_CMUX_FCS_POLYNOMIAL , fcs , true);
247244 fcs = 0xFF - crc8 (frame -> data , data_len , MODEM_CMUX_FCS_POLYNOMIAL , fcs , true);
248245 }
249246
247+ /* Frame header */
248+ ring_buf_put (& cmux -> transmit_rb , buf , buf_idx );
249+
250250 /* Data */
251251 ring_buf_put (& cmux -> transmit_rb , frame -> data , data_len );
252252
253- /* FCS */
254- ring_buf_put (& cmux -> transmit_rb , & fcs , 1 );
255-
256- /* EOF */
257- byte = 0xF9 ;
258- ring_buf_put (& cmux -> transmit_rb , & byte , 1 );
253+ /* FCS and EOF will be put on the same call */
254+ buf [0 ] = fcs ;
255+ buf [1 ] = 0xF9 ;
256+ ring_buf_put (& cmux -> transmit_rb , buf , 2 );
259257 k_work_schedule (& cmux -> transmit_work , K_NO_WAIT );
260258 return data_len ;
261259}
0 commit comments