use LENGTH instead of sizeof for array indexes

This commit is contained in:
King Kévin 2016-04-15 11:59:14 +02:00
parent ffee985136
commit 8f8a086eee
1 changed files with 15 additions and 14 deletions

View File

@ -34,6 +34,7 @@
#include <libopencm3/usb/cdc.h> // USB CDC library
#include <libopencm3/cm3/sync.h> // synchronisation utilities
#include "global.h" // global utilities
#include "usb_cdcacm.h" // USB CDC ACM header and definitions
/** @brief USB CDC ACM device descriptor
@ -257,17 +258,17 @@ static int cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_data *
* even though it's optional in the CDC spec, and we don't
* advertise it in the ACM functional descriptor.
*/
char local_buf[10];
struct usb_cdc_notification *notif = (void *)local_buf;
uint8_t reply[10] = {0};
struct usb_cdc_notification *notif = (void *)reply;
/* we echo signals back to host as notification. */
notif->bmRequestType = 0xA1;
notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE;
notif->wValue = 0;
notif->wIndex = 0;
notif->wLength = 2;
local_buf[8] = req->wValue & 3;
local_buf[9] = 0;
usbd_ep_write_packet(usbd_dev, 0x83, local_buf, 10);
reply[8] = req->wValue & 3;
reply[9] = 0;
usbd_ep_write_packet(usbd_dev, 0x83, reply, LENGTH(reply));
break;
case USB_CDC_REQ_SET_LINE_CODING:
// ignore if length is wrong
@ -306,8 +307,8 @@ static void cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
/* receive data */
usb_length = usbd_ep_read_packet(usbd_dev, 0x01, usb_data, sizeof(usb_data));
if (usb_length) { // copy received data
for (uint16_t i=0; i<usb_length && rx_used<sizeof(rx_buffer); i++) { // only until buffer is full
rx_buffer[(rx_i+rx_used)%sizeof(rx_buffer)] = usb_data[i]; // put character in buffer
for (uint16_t i=0; i<usb_length && rx_used<LENGTH(rx_buffer); i++) { // only until buffer is full
rx_buffer[(rx_i+rx_used)%LENGTH(rx_buffer)] = usb_data[i]; // put character in buffer
rx_used++; // update used buffer
}
cdcacm_received = rx_used; // update available data
@ -328,9 +329,9 @@ static void cdcacm_data_tx_cb(usbd_device *usbd_dev, uint8_t ep)
}
if (mutex_trylock(&tx_lock)) { // try to get lock
uint8_t usb_length = (tx_used > 64 ? 64 : tx_used); // length of data to be transmitted (respect max packet size)
usb_length = (usb_length > (sizeof(tx_buffer)-tx_i) ? sizeof(tx_buffer)-tx_i : usb_length); // since here we use the source array not as ring buffer, only go up to the end
usb_length = (usb_length > (LENGTH(tx_buffer)-tx_i) ? LENGTH(tx_buffer)-tx_i : usb_length); // since here we use the source array not as ring buffer, only go up to the end
while (usb_length != usbd_ep_write_packet(usb_device, 0x82, (void*)(&tx_buffer[tx_i]), usb_length)); // ensure data is written into transmit buffer
tx_i = (tx_i+usb_length)%sizeof(tx_buffer); // update location on buffer
tx_i = (tx_i+usb_length)%LENGTH(tx_buffer); // update location on buffer
tx_used -= usb_length; // update used size
mutex_unlock(&tx_lock); // release lock
} else {
@ -382,7 +383,7 @@ char cdcacm_getchar(void)
__WFI(); // sleep until interrupt (not sure if it's a good idea here)
}
char to_return = rx_buffer[rx_i]; // get the next available character
rx_i = (rx_i+1)%sizeof(rx_buffer); // update used buffer
rx_i = (rx_i+1)%LENGTH(rx_buffer); // update used buffer
rx_used--; // update used buffer
cdcacm_received = rx_used; // update available data
return to_return;
@ -394,12 +395,12 @@ void cdcacm_putchar(char c)
return;
}
mutex_lock(&tx_lock); // get lock to prevent race condition
if (tx_used<sizeof(tx_buffer)) { // buffer not full
tx_buffer[(tx_i+tx_used)%sizeof(tx_buffer)] = c; // put character in buffer
if (tx_used<LENGTH(tx_buffer)) { // buffer not full
tx_buffer[(tx_i+tx_used)%LENGTH(tx_buffer)] = c; // put character in buffer
tx_used++; // update used buffer
} else { // buffer full (might be that no terminal is connected to this serial)
tx_i = (tx_i+1)%sizeof(tx_buffer); // shift start
tx_buffer[(tx_i+tx_used)%sizeof(tx_buffer)] = c; // overwrite old data
tx_i = (tx_i+1)%LENGTH(tx_buffer); // shift start
tx_buffer[(tx_i+tx_used)%LENGTH(tx_buffer)] = c; // overwrite old data
}
mutex_unlock(&tx_lock); // release lock
usbd_ep_write_packet(usb_device, 0x82, NULL, 0); // trigger tx callback