Skip to content

Instantly share code, notes, and snippets.

@irfanm96
Created May 27, 2020 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irfanm96/522294b24da2cecaf04177cfa484f1e2 to your computer and use it in GitHub Desktop.
Save irfanm96/522294b24da2cecaf04177cfa484f1e2 to your computer and use it in GitHub Desktop.
CO326 LAB 04 Resources
/*******************************************************************************
Copyright 2016 Microchip Technology Inc. (www.microchip.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
To request to license the code under the MLA license (www.microchip.com/mla_license),
please contact mla_licensing@microchip.com
*******************************************************************************/
/** INCLUDES *******************************************************/
#include "system.h"
#include <stdint.h>
#include <string.h>
#include <stddef.h>
#include "usb.h"
#include "app_led_usb_status.h"
#include "app_device_cdc_basic.h"
#include "usb_config.h"
/** VARIABLES ******************************************************/
static bool buttonPressed;
static char buttonMessage[] = "Please enter a string.\r\n";
static uint8_t readBuffer[CDC_DATA_OUT_EP_SIZE];
static uint8_t writeBuffer[CDC_DATA_IN_EP_SIZE];
static uint8_t count = 0;
/*********************************************************************
* Function: void APP_DeviceCDCBasicDemoInitialize(void);
*
* Overview: Initializes the demo code
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
********************************************************************/
void APP_DeviceCDCBasicDemoInitialize()
{
line_coding.bCharFormat = 0;
line_coding.bDataBits = 8;
line_coding.bParityType = 0;
line_coding.dwDTERate = 9600;
buttonPressed = false;
}
/*********************************************************************
* Function: void APP_DeviceCDCBasicDemoTasks(void);
*
* Overview: Keeps the demo running.
*
* PreCondition: The demo should have been initialized and started via
* the APP_DeviceCDCBasicDemoInitialize() and APP_DeviceCDCBasicDemoStart() demos
* respectively.
*
* Input: None
*
* Output: None
*
********************************************************************/
void APP_DeviceCDCBasicDemoTasks()
{
/* If the USB device isn't configured yet, we can't really do anything
* else since we don't have a host to talk to. So jump back to the
* top of the while loop. */
if( USBGetDeviceState() < CONFIGURED_STATE )
{
return;
}
/* If we are currently suspended, then we need to see if we need to
* issue a remote wakeup. In either case, we shouldn't process any
* keyboard commands since we aren't currently communicating to the host
* thus just continue back to the start of the while loop. */
if( USBIsDeviceSuspended()== true )
{
return;
}
/* If the user has pressed the button associated with this demo, then we
* are going to send a "Button Pressed" message to the terminal.
*/
if(BUTTON_IsPressed(BUTTON_DEVICE_CDC_BASIC_DEMO) == true)
{
/* Make sure that we only send the message once per button press and
* not continuously as the button is held.
*/
if(buttonPressed == false)
{
/* Make sure that the CDC driver is ready for a transmission.
*/
if(mUSBUSARTIsTxTrfReady() == true)
{
putrsUSBUSART(buttonMessage);
buttonPressed = true;
}
}
}
else
{
/* If the button is released, we can then allow a new message to be
* sent the next time the button is pressed.
*/
buttonPressed = false;
}
/* Check to see if there is a transmission in progress, if there isn't, then
* we can see about performing an echo response to data received.
*/
if( USBUSARTIsTxTrfReady() == true)
{
uint8_t i;
uint8_t numBytesRead;
numBytesRead = getsUSBUSART(readBuffer, sizeof(readBuffer));
/* For every byte that was read... */
for(i=0; i<numBytesRead; i++)
{
switch(readBuffer[i])
{
/*
* for newline or \r
*/
case 0x0A:
case 0x0D:
writeBuffer[count] = readBuffer[i];
// add a newline character at the end
writeBuffer[count+1] = '\n';
count +=2;
// send the write buffer
putUSBUSART(writeBuffer,count);
count = 0;
break;
default:
if(readBuffer[i] >= 'a' && readBuffer[i] <= 'z'){
/* Make the letter to upper case one */
writeBuffer[count] = readBuffer[i] + 'A' - 'a';
count++;
}else{
/* these are not lowercase letters so no change */
writeBuffer[count] = readBuffer[i];
count++;
}
break;
}
}
}
CDCTxService();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment