commprt.c 1018 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * commprt.c
  3. *
  4. * Created on: Jan 25, 2022
  5. * Author: radioman
  6. */
  7. #include <string.h>
  8. #include "stm32l1xx_hal.h"
  9. #include "commprt.h"
  10. #include "uartio.h"
  11. #include "usbd_cdc_if.h"
  12. #define COMM_HEADER_BYTE 0x55;
  13. #define COMM_BUFFER_SIZE 260
  14. extern UART_HandleTypeDef huart1;
  15. static uint8_t comm_buffer[COMM_BUFFER_SIZE];
  16. uint8_t COMM_Checksum(const uint8_t *data, uint8_t len)
  17. {
  18. uint8_t chk = 0;
  19. while(len--)
  20. chk ^= data[len];
  21. return chk;
  22. }
  23. COMM_StatusTypeDef COMM_Receive(uint8_t *buf, COMM_IfaceTypeDef iface)
  24. {
  25. return COMM_OK;
  26. }
  27. COMM_StatusTypeDef COMM_Transmit(uint8_t *data, uint8_t len, COMM_IfaceTypeDef iface)
  28. {
  29. comm_buffer[0] = COMM_HEADER_BYTE;
  30. comm_buffer[1] = len;
  31. memcpy(&comm_buffer[2], data, len);
  32. comm_buffer[len + 2] = COMM_Checksum(comm_buffer, len + 2);
  33. switch(iface)
  34. {
  35. case COMM_BT:
  36. UARTIO_Transmit(&huart1, comm_buffer, len + 3);
  37. break;
  38. case COMM_USB:
  39. CDC_Transmit_FS(comm_buffer, len + 3);
  40. break;
  41. default:
  42. return COMM_FAIL;
  43. }
  44. return COMM_OK;
  45. }