I would like to detect if a USB device is connected and it looks like the USBDCD_STATUS register would be a good place to start. From the kinetis.h file I found this.
#define USBDCD_STATUS (*(volatile uint32_t *)0x40035008) // Status register
#define USBDCD_STATUS_ACTIVE ((uint32_t)0x00400000)
#define USBDCD_STATUS_ID ((uint32_t)0x00200000)
#define USBDCD_STATUS_ERR ((uint32_t)0x00100000)
#define USBDCD_STATUS_SEQ_STAT (uint32_t)(( & 0x3) << 18)
#define USBDCD_STATUS_SEQ_STAT_MASK ((uint32_t)0x000C0000)
#define USBDCD_STATUS_SEQ_RES (uint32_t)(( & 0x3) << 16)
#define USBDCD_STATUS_SEQ_RES_MASK ((uint32_t)0x00030000)
However when I try to access USBDCD_STATUS the sketch hangs in setup and never runs the loop code. Eventually I'd like to directly access the USBDCD_STATUS_SEQ_RES and USBDCD_STATUS_SEQ_STAT fields. How can I read the register value so I can display either on the serial port or on the LCD?
#define USBDCD_STATUS (*(volatile uint32_t *)0x40035008) // Status register
#define USBDCD_STATUS_ACTIVE ((uint32_t)0x00400000)
#define USBDCD_STATUS_ID ((uint32_t)0x00200000)
#define USBDCD_STATUS_ERR ((uint32_t)0x00100000)
#define USBDCD_STATUS_SEQ_STAT (uint32_t)(( & 0x3) << 18)
#define USBDCD_STATUS_SEQ_STAT_MASK ((uint32_t)0x000C0000)
#define USBDCD_STATUS_SEQ_RES (uint32_t)(( & 0x3) << 16)
#define USBDCD_STATUS_SEQ_RES_MASK ((uint32_t)0x00030000)
However when I try to access USBDCD_STATUS the sketch hangs in setup and never runs the loop code. Eventually I'd like to directly access the USBDCD_STATUS_SEQ_RES and USBDCD_STATUS_SEQ_STAT fields. How can I read the register value so I can display either on the serial port or on the LCD?
Code:
//runs on teensy 3.2
#include <DFRobot_LCD.h>
//#include <kinetis.h>
// USB Status detection sstudy
// 7/6/21 Jim Darrah
int r,g,b;
String dataValues;
DFRobot_LCD lcd(16,2, LCD_ADDRESS,RGB_ADDRESS); //16 characters and 2 lines of show
void setup() {
unsigned long delayStart = millis();
Serial.begin(115200);
while (!Serial && (millis() - delayStart) <= 1000);
r=0;
g=255;
b=0;
lcd.init();
// Serial.println(USBDCD_STATUS); // <<<<<<<<<< if I uncomment this line the sketch never enters the loop code
}
void loop() {
lcd.setRGB(r, g, b); //Set R,G,B Value
lcd.setCursor(0,0);
lcd.print("SEQ_RES = ");
lcd.setCursor(7,0);
// lcd.print(USBDCD_STATUS_SEQ_RES);
lcd.setCursor(0,1);
lcd.print("SEQ STAT = ");
lcd.setCursor(6,1);
// lcd.print(USBDCD_STATUS_SEQ_STAT);
delay(1);
}