Commit 9f61f116 authored by Jake Read's avatar Jake Read

begin apaport

parent 35abaa84
......@@ -2,6 +2,8 @@
# Automatically-generated file. Do not edit or delete the file
################################################################################
src\apaport.c
src\pin.c
src\ringbuffer.c
......
......@@ -365,7 +365,6 @@
<Value>../src/ASF/common/services/delay</Value>
</ListValues>
</armgcc.compiler.directories.IncludePaths>
<armgcc.compiler.optimization.level>Optimize (-O1)</armgcc.compiler.optimization.level>
<armgcc.compiler.optimization.OtherFlags>-fdata-sections</armgcc.compiler.optimization.OtherFlags>
<armgcc.compiler.optimization.PrepareFunctionsForGarbageCollection>True</armgcc.compiler.optimization.PrepareFunctionsForGarbageCollection>
<armgcc.compiler.optimization.DebugLevel>Maximum (-g3)</armgcc.compiler.optimization.DebugLevel>
......@@ -383,6 +382,7 @@
</ListValues>
</armgcc.linker.libraries.LibrarySearchPaths>
<armgcc.linker.optimization.GarbageCollectUnusedSections>True</armgcc.linker.optimization.GarbageCollectUnusedSections>
<armgcc.linker.memorysettings.ExternalRAM />
<armgcc.linker.miscellaneous.LinkerFlags>-Wl,--entry=Reset_Handler -Wl,--cref -mthumb -T../src/ASF/sam/utils/linker_scripts/sams70/sams70n20/gcc/flash.ld</armgcc.linker.miscellaneous.LinkerFlags>
<armgcc.assembler.general.IncludePaths>
<ListValues>
......@@ -474,6 +474,12 @@
<Folder Include="src\config\" />
</ItemGroup>
<ItemGroup>
<Compile Include="src\apaport.c">
<SubType>compile</SubType>
</Compile>
<Compile Include="src\apaport.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="src\hardware.h">
<SubType>compile</SubType>
</Compile>
......
/*
* apaport.c
*
* Created: 2/23/2018 9:17:48 AM
* Author: Jake
*/
#include "apaport.h"
void apaport_build(apaport_t *apap, uint8_t portnum, uartport_t *uart, pin_t *stlr, pin_t *stlb){
apap->uart = uart;
apap->portnum = portnum;
apap->stlr = stlr;
apap->stlb = stlb;
}
void apaport_reset(apaport_t *apap){
apap->packet_num = 0;
apap->packets_ready = 0;
apap->packet_state = APAPORT_OUTSIDE_PACKET;
apap->packet_position = 0;
pin_set(apap->stlr);
pin_set(apap->stlb);
}
void apaport_scan(apaport_t *apap, uint32_t maxpackets){
// scan through for completely received packets
while(apap->packets_ready <= maxpackets && !rb_empty(apap->uart->rbrx)){
// pull bytes out of buffer and segment into packets
pin_clear(apap->stlb);
delay_ms(15);
pin_set(apap->stlb);
delay_ms(15);
apap->packets[apap->packet_num][apap->packet_position] = rb_get(apap->uart->rbrx);
apap->packet_position ++;
if(apap->packet_position >= apap->packets[apap->packet_num][0]){
// length is 1st byte, like array[n] not array[n-1]
// now volley for next pass
apap->packet_num = (apap->packet_num + 1) % APAPORT_NUM_STATIC_PACKETS; // inc. and loop
apap->packets_ready ++;
apap->packet_position = 0;
}
} // end 1st scan for packets, now we know we have apaport->packet_num packets completely received
// handle packets
while(apap->packets_ready > 0){
uint32_t p = (apap->packet_num + APAPORT_NUM_STATIC_PACKETS - apap->packets_ready) % APAPORT_NUM_STATIC_PACKETS;
// handle these!
pin_clear(apap->stlr);
delay_ms(150);
pin_set(apap->stlr);
delay_ms(150);
// now to handle: 1st, a reply for debug of what we received
uart_sendchars_buffered(apap->uart, apap->packets[p], apap->packets[p][0]);//apap->packets[p][0]);
/*
now, handle:
- packet is for us [1]th element is pointer
- route pointer movement & add port incoming
- next port, and fire on
- floods
*/
apap->packets_ready --;
}
}
\ No newline at end of file
/*
* apaport.h
*
* Created: 2/23/2018 9:17:34 AM
* Author: Jake
*/
#ifndef APAPORT_H_
#define APAPORT_H_
#include <asf.h>
#include "uartport.h"
#include "pin.h"
#define APAPORT_NUM_STATIC_PACKETS 4
#define APAPORT_OUTSIDE_PACKET 0
#define APAPORT_INSIDE_PACKET 1
#define APA_END_ADDR_DELIMITER 255
#define APA_ADDR_POINTER 254
#define APA_ADDR_FLOOD 253
typedef struct{
uartport_t *uart;
pin_t *stlr;
pin_t *stlb;
uint8_t portnum; // which port are we
uint32_t packet_num;
uint32_t packet_position;
uint32_t packets_ready;
uint32_t packet_state;
uint8_t packets[APAPORT_NUM_STATIC_PACKETS][256]; // packets for handling by app
}apaport_t;
void apaport_build(apaport_t *apap, uint8_t portnum, uartport_t *uart, pin_t *stlr, pin_t *stlb);
void apaport_reset(apaport_t *apap);
void apaport_scan(apaport_t *apap, uint32_t maxpackets);
#endif /* APAPORT_H_ */
\ No newline at end of file
......@@ -12,6 +12,7 @@
#include "pin.h"
#include "ringbuffer.h"
#include "uartport.h"
#include "apaport.h"
// status lights
pin_t np1stlr;
......@@ -46,6 +47,10 @@ uartport_t up4;
uartport_t up5;
uartport_t *ups[] = {&up1, &up2, &up3, &up4, &up5};
// apaports
apaport_t apap1;
#endif /* HARDWARE_H_ */
\ No newline at end of file
......@@ -76,89 +76,92 @@ void setupinterrupts(void){
}
void lightsetup(void){
np1stlr = pin_new(PIOD, PIO_PER_P12);
pin_init(&np1stlr, PIOD, PIO_PER_P12);
pin_output(&np1stlr);
np1stlb = pin_new(PIOA, PIO_PER_P2);
pin_init(&np1stlb, PIOA, PIO_PER_P2);
pin_output(&np1stlb);
np2stlr = pin_new(PIOA, PIO_PER_P11);
pin_init(&np2stlr, PIOA, PIO_PER_P11);
pin_output(&np2stlr);
np2stlb = pin_new(PIOD, PIO_PER_P20);
pin_init(&np2stlb, PIOD, PIO_PER_P20);
pin_output(&np2stlb);
np3stlr = pin_new(PIOA, PIO_PER_P15);
pin_init(&np3stlr, PIOA, PIO_PER_P15);
pin_output(&np3stlr);
np3stlb = pin_new(PIOD, PIO_PER_P27);
pin_init(&np3stlb, PIOD, PIO_PER_P27);
pin_output(&np3stlb);
np4stlr = pin_new(PIOA, PIO_PER_P22);
pin_init(&np4stlr, PIOA, PIO_PER_P22);
pin_output(&np4stlr);
np4stlb = pin_new(PIOA, PIO_PER_P8);
pin_init(&np4stlb, PIOA, PIO_PER_P8);
pin_output(&np4stlb);
np5stlr = pin_new(PIOB, PIO_PER_P0);
pin_init(&np5stlr, PIOB, PIO_PER_P0);
pin_output(&np5stlr);
np5stlb = pin_new(PIOB, PIO_PER_P1);
pin_init(&np5stlb, PIOB, PIO_PER_P1);
pin_output(&np5stlb);
}
void lightstoggle(void){
for(int i = 0; i < 10; i++){
for(int i = 2; i < 10; i++){
pin_toggle(lights[i]);
}
}
int main (void)
{
sysclk_init();
// I think we can do 300, are at 150
board_init();
setupperipherals();
setupinterrupts();
lightsetup();
void initports(void){
// RBs 1
rb_init(&np1rbrx);
rb_init(&np1rbtx);
rb_init(&np1rbtx);
// UP1 on UART0, RX 9 TX 10 on PIOA
up1 = uart_new(UART0, PIOA, 9, 10, &np1rbrx, &np1rbtx);
uart_build(&up1, UART0, PIOA, 9, 10, &np1rbrx, &np1rbtx);
uart_init(&up1, 81, UART_IS_PERIPHERAL_A);
// RBs 2
rb_init(&np2rbrx);
rb_init(&np2rbtx);
// UP2 on UART1, RX 5 TX 4 on PIOA
up2 = uart_new(UART1, PIOA, 5, 4, &np2rbrx, &np2rbtx);
uart_build(&up2, UART1, PIOA, 5, 4, &np2rbrx, &np2rbtx);
uart_init(&up2, 81, UART_IS_PERIPHERAL_C); // 81 for FTDI 115200 :|
// RBs 3
rb_init(&np3rbrx);
rb_init(&np3rbtx);
// UP3 on UART2, RX 25 TX 26 on PIOD
up3 = uart_new(UART2, PIOD, 25, 26, &np3rbrx, &np3rbtx);
uart_build(&up3, UART2, PIOD, 25, 26, &np3rbrx, &np3rbtx);
uart_init(&up3, 81, UART_IS_PERIPHERAL_C);
// RBs 4
rb_init(&np4rbrx);
rb_init(&np4rbtx);
// UP4 on UART3, RX 28 TX 30 on PIOD
up4 = uart_new(UART3, PIOD, 28, 30, &np4rbrx, &np4rbtx);
uart_build(&up4, UART3, PIOD, 28, 30, &np4rbrx, &np4rbtx);
uart_init(&up4, 81, UART_IS_PERIPHERAL_A);
// RBs 5
// RBs 5
rb_init(&np5rbrx);
rb_init(&np5rbtx);
// UP5 on UART4, RX 18 TX 19 on PIOD
up5 = uart_new(UART4, PIOD, 18, 19, &np5rbrx, &np5rbtx);
uart_build(&up5, UART4, PIOD, 18, 19, &np5rbrx, &np5rbtx);
uart_init(&up5, 81, UART_IS_PERIPHERAL_C);
}
// currently: unit test, setup ABCDSR registers for uarts, do the main while loop below
int main (void){
sysclk_init();
board_init();
setupperipherals();
setupinterrupts();
lightsetup();
initports();
apaport_build(&apap1, 1, &up1, &np1stlr, &np1stlb);
apaport_reset(&apap1);
while(1){
lightstoggle();
apaport_scan(&apap1, 2);
/*
uint8_t hello[] = {'h', 'e', 'l', 'l', 'o'};
for(int i = 0; i < 5; i ++){
if(!rb_empty(ups[i]->rbrx)){
......@@ -166,6 +169,7 @@ int main (void)
uart_sendchar_buffered(ups[i], rb_get(ups[i]->rbrx));
}
}
*/
delay_ms(75);
}
}
......
......@@ -8,13 +8,12 @@
#include "pin.h"
#include <asf.h>
pin_t pin_new(Pio *port, uint32_t pin_bitmask){
pin_t pin;
pin.port = port;
pin.pin_bm = pin_bitmask;
return pin;
// don't return aggregates
// https://bytes.com/topic/c/answers/644271-aggregate-return-warnings
void pin_init(pin_t *pin, Pio *port, uint32_t pin_bitmask){
pin->port = port;
pin->pin_bm = pin_bitmask;
}
void pin_output(pin_t *pin){
......
......@@ -20,7 +20,7 @@ typedef struct{
uint32_t pin_bm;
}pin_t;
pin_t pin_new(Pio *port, uint32_t pin_bitmask);
void pin_init(pin_t *pin, Pio *port, uint32_t pin_bitmask);
void pin_output(pin_t *pin); // set as output
void pin_set(pin_t *pin);
......
......@@ -47,16 +47,21 @@ uint8_t rb_putchar(ringbuffer_t *rb, uint8_t data){
}
uint8_t rb_putdata(ringbuffer_t *rb, uint8_t *data, uint8_t size){
/*
if(rb_freespace(rb) >= size){
// rb_freespace, not working?
return 0;
} else {
*/
uint8_t i = 0;
while(i < size){
// TODO: check if this is interrupt safe?
// should be...
rb_putchar(rb, data[i]);
i ++;
}
return 1;
}
//}
}
uint8_t rb_get(ringbuffer_t *rb){
......
......@@ -7,21 +7,17 @@
#include "uartport.h"
uartport_t uart_new(Uart *com, Pio *port, uint32_t pinrx, uint32_t pintx, ringbuffer_t *rbrx, ringbuffer_t *rbtx){
uartport_t uart;
void uart_build(uartport_t *uart, Uart *com, Pio *port, uint32_t pinrx, uint32_t pintx, ringbuffer_t *rbrx, ringbuffer_t *rbtx){
uart->com = com;
uart->port = port;
uart.com = com;
uart.port = port;
uart->pinrx = pinrx;
uart->pinrx_bm = 1 << pinrx;
uart->pintx = pintx;
uart->pintx_bm = 1 << pintx;
uart.pinrx = pinrx;
uart.pinrx_bm = 1 << pinrx;
uart.pintx = pintx;
uart.pintx_bm = 1 << pintx;
uart.rbrx = rbrx;
uart.rbtx = rbtx;
return uart;
uart->rbrx = rbrx;
uart->rbtx = rbtx;
}
void uart_init(uartport_t *uart, uint32_t baud, uint32_t abcdsr){
......
......@@ -30,7 +30,7 @@ typedef struct{
ringbuffer_t *rbtx;
}uartport_t;
uartport_t uart_new(Uart *uart, Pio *port, uint32_t pinrx, uint32_t pintx, ringbuffer_t *rbrx, ringbuffer_t *rbtx);
void uart_build(uartport_t *uart, Uart *com, Pio *port, uint32_t pinrx, uint32_t pintx, ringbuffer_t *rbrx, ringbuffer_t *rbtx);
void uart_init(uartport_t *uart, uint32_t baud, uint32_t abcdsr);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment