Skip to content

Commit

Permalink
Second checkin of bulk code
Browse files Browse the repository at this point in the history
Added new clear ring buffer command
Removed duplicate defines
Fixed "GMD" typo
Fixed typo in comment in uart bridge
  • Loading branch information
macpod committed Mar 24, 2014
1 parent 5264972 commit 7986ca2
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 62 deletions.
2 changes: 2 additions & 0 deletions lasershark/inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ How to use:
#define USR1_LED_BIT 9 // Bit on port for led
#define USR2_LED_BIT 10 // Bit on port for led

// Set this to zero if you are debugging.
#define WATCHDOG_ENABLED 1

/*********************************************************************************
** End Of File
Expand Down
14 changes: 8 additions & 6 deletions lasershark/inc/lasershark.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ along with Lasershark. If not, see <http://www.gnu.org/licenses/>.
// Get max dac value
#define LASERSHARK_CMD_GET_DAC_MAX 0x88

// Get min dac value
#define LASERSHARK_CMD_GET_DAC_MIN 0x87
// Get max dac value
#define LASERSHARK_CMD_GET_DAC_MAX 0x88

// Get the number of samples the ring buffer is able to store
#define LASERSHARK_CMD_GET_RINGBUFFER_SAMPLE_COUNT 0X89
Expand All @@ -64,10 +60,12 @@ along with Lasershark. If not, see <http://www.gnu.org/licenses/>.

// Version Info
#define LASERSHARK_FW_MAJOR_VERSION 2
#define LASERSHARK_FW_MINOR_VERSION 1
#define LASERSHARK_FW_MINOR_VERSION 2
#define LASERSHARK_CMD_GET_LASERSHARK_FW_MAJOR_VERSION 0X8B
#define LASERSHARK_GMD_GET_LASERSHARK_FW_MINOR_VERSION 0X8C
#define LASERSHARK_CMD_GET_LASERSHARK_FW_MINOR_VERSION 0X8C

// Clears ring buffer
#define LASERSHARK_CMD_CLEAR_RINGBUFFER 0x8D

#define LASERSHARK_X_DAC_REG DAC124S085_INPUT_REG_C
#define LASERSHARK_Y_DAC_REG DAC124S085_INPUT_REG_D
Expand Down Expand Up @@ -129,6 +127,10 @@ __inline uint32_t lasershark_get_empty_sample_count();

__inline void lasershark_process_data(uint32_t cnt);

void lasershark_set_bulk_data_interrupt_needs_retrigger();

void lasershark_handle_bulk_data_interrupt_retrigger();

void TIMER32_1_IRQHandler(void);

#endif /* LASERSHARK_H_ */
37 changes: 36 additions & 1 deletion lasershark/src/lasershark.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

static bool lasershark_output_enabled;

static bool lasershark_bulk_interrupt_retrigger;


static __INLINE void lasershark_set_interlock_a(bool val)
{
Expand All @@ -48,6 +50,7 @@ static __INLINE void lasershark_set_c(bool val)
void lasershark_init() {
int i, j = j;
lasershark_output_enabled = false;
lasershark_bulk_interrupt_retrigger = false;
lasershark_ringbuffer_head = 0;
lasershark_ringbuffer_tail = 0;
lasershark_ringbuffer_half_full_reporting = false;
Expand Down Expand Up @@ -261,10 +264,12 @@ void lasershark_process_command() {
temp = LASERSHARK_FW_MAJOR_VERSION;
memcpy(IN1Packet + 2, &temp, sizeof(uint32_t));
break;
case LASERSHARK_GMD_GET_LASERSHARK_FW_MINOR_VERSION:
case LASERSHARK_CMD_GET_LASERSHARK_FW_MINOR_VERSION:
temp = LASERSHARK_FW_MINOR_VERSION;
memcpy(IN1Packet + 2, &temp, sizeof(uint32_t));
break;
case LASERSHARK_CMD_CLEAR_RINGBUFFER:
break;
default:
IN1Packet[1] = LASERSHARK_CMD_UNKNOWN;
break;
Expand Down Expand Up @@ -292,6 +297,21 @@ __inline uint32_t lasershark_get_empty_sample_count()
LASERSHARK_RINGBUFFER_SAMPLES - lasershark_ringbuffer_tail + lasershark_ringbuffer_head);
}

__inline uint32_t lasershark_clear_ringbuffer()
{
bool temp = lasershark_output_enabled;
lasershark_output_enabled = false;

// See if head and tail are the same. If so we don't have to do anything.
if (lasershark_ringbuffer_head != lasershark_ringbuffer_tail) {
// USB interrupt priority is 2, timer interrupt priority is 1. As such it should not be possible
// for the the head to be incremented to one after the tail.
lasershark_ringbuffer_head = lasershark_ringbuffer_tail;
}

lasershark_output_enabled = temp;
}

__inline void lasershark_process_data(uint32_t cnt) {
uint32_t dat, n, cntmod = (cnt + 3) / 4;
uint32_t *pData;
Expand All @@ -312,6 +332,21 @@ __inline void lasershark_process_data(uint32_t cnt) {
}
}

void lasershark_set_bulk_data_interrupt_needs_retrigger(void)
{
lasershark_bulk_interrupt_retrigger = true;
}

void lasershark_handle_bulk_data_interrupt_retrigger(void)
{
if (lasershark_bulk_interrupt_retrigger) {
if (LASERSHARK_USB_DATA_BULK_SIZE <= lasershark_get_empty_sample_count()) {
lasershark_bulk_interrupt_retrigger = false;
LPC_USB->DevIntSet = EP6_INT; // Physical EP 6 (Logical OUT EP 3). This is a write-only register. Don't OR-in value.
}
}
}

void TIMER32_1_IRQHandler(void) {
LPC_TMR32B1->IR = 1; /* clear interrupt flag */
uint32_t temp = (lasershark_ringbuffer_head + 1)
Expand Down
2 changes: 1 addition & 1 deletion lasershark/src/lasershark_uart_bridge.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
lasershark_uart_bridge.h - Lasershark firmware component to allow for
lasershark_uart_bridge.c - Lasershark firmware component to allow for
limited uart communication through lasershark.
Copyright (C) 2012 Jeffrey Nelson <nelsonjm@macpod.net>
Expand Down
55 changes: 6 additions & 49 deletions lasershark/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <NXP/crp.h>
#include <stdbool.h>
#include "config.h"
#include "watchdog.h"
#include "gpio.h"
#include "usb.h"
#include "usbcfg.h"
Expand All @@ -41,43 +42,6 @@
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP;

// Set this to zero if you are debugging.
#define WATCHDOG_ENABLED 1

#define WDEN (0x1<<0)
#define WDRESET (0x1<<1)

#if (WATCHDOG_ENABLED)
void watchdog_feed() {
LPC_WDT->FEED = 0xAA;
LPC_WDT->FEED = 0x55;
}

void watchdog_init() {
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 15); // Power on WTD peripheral
// LPC_SYSCON->CLKOUTCLKSEL = 0x02; // Use WDTOSC for CLKOUT pin

LPC_SYSCON->WDTOSCCTRL = 0x1 << 5 | 0xF; // FREQSEL = 1.4Mhz, 64 = ~9.375kHz
LPC_SYSCON->PDRUNCFG &= ~(0x1 << 6); // Power WDTOSC

LPC_SYSCON->WDTCLKSEL = 0x02; // Use WDTOSC as the WTD clock source

LPC_SYSCON->WDTCLKUEN = 0x01; // Update clock source
// Write 0 then 1 to apply
LPC_SYSCON->WDTCLKUEN = 0x00;
LPC_SYSCON->WDTCLKUEN = 0x01;
while (!(LPC_SYSCON->WDTCLKUEN & 0x01))
; // Wait for update to occur.

LPC_SYSCON->WDTCLKDIV = 0x01; // Enabled WDTCLK and set it to divide by 1 (7.8KHz)

NVIC_EnableIRQ(WDT_IRQn);
LPC_WDT->TC = 256; // Delay = <276 (minimum of 256, max of 2^24)>*4 / 9.375khz(WDTCLK) =0.10922666666 s
LPC_WDT->MOD = WDEN | WDRESET; // Cause reset to occur when WDT hits.

watchdog_feed();
}
#endif

int main(void) {
/* Basic chip initialization is taken care of in SystemInit() called
Expand Down Expand Up @@ -117,25 +81,18 @@ int main(void) {
NVIC_SetPriority(USB_IRQn, 2);
USB_Connect(TRUE); // USB Connect

#if (WATCHDOG_ENABLED)
watchdog_init();
#endif

while (!USB_Configuration) // wait until USB is configured
{
#if (WATCHDOG_ENABLED)
watchdog_feed();
#else
asm("nop");
#endif
watchdog_feed_or_nop();
}

while (1) {
#if (WATCHDOG_ENABLED)
watchdog_feed();
#else
asm("nop");
#endif
watchdog_feed_or_nop();
lasershark_handle_bulk_data_interrupt_retrigger();
}
// TODO handle usb disconnects better instead of resetting (i.e. for externally powered LaserSharks).

return 0;
}
8 changes: 4 additions & 4 deletions lasershark/src/usbuser.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ void USB_EndPoint3(uint32_t event) {
uint32_t cnt;
switch (event) {
case USB_EVT_OUT:
while (1) {
if (LASERSHARK_USB_DATA_BULK_SIZE <= lasershark_get_empty_sample_count()) {
break;
}
if (LASERSHARK_USB_DATA_BULK_SIZE > lasershark_get_empty_sample_count()) {
// We can't sit around here all day! Have the main thread loop trigger this when ready.
lasershark_set_bulk_data_interrupt_needs_retrigger();
return;
}

LPC_USB->Ctrl = ((USB_ENDPOINT_OUT(3) & 0x0F) << 2) | CTRL_RD_EN; // enable read
Expand Down
28 changes: 27 additions & 1 deletion lasershark/src/watchdog.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
/*
watchdog.c - Lasershark firmware.
Copyright (C) 2012 Jeffrey Nelson <nelsonjm@macpod.net>
This file is part of Lasershark's Firmware.
Lasershark is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Lasershark is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Lasershark. If not, see <http://www.gnu.org/licenses/>.
*/

#include "watchdog.h"
#include "config.h"
#ifdef __USE_CMSIS
#include "LPC13xx.h"
#endif

#if (!WATCHDOG_ENABLED)
#warning Watchdog is disabled!
#endif

__inline void watchdog_feed() {
#if (WATCHDOG_ENABLED)
Expand Down Expand Up @@ -48,4 +75,3 @@ void watchdog_init() {
#endif
}


0 comments on commit 7986ca2

Please sign in to comment.