Skip to content
Snippets Groups Projects
Commit 37852b60 authored by Jake Read's avatar Jake Read
Browse files

complete pin abstraction

parent f4d02d4b
Branches
No related tags found
No related merge requests found
Showing
with 100 additions and 11 deletions
......@@ -374,10 +374,50 @@ OK, enough for that wakeup, now I'm going to try to boot the USB CDC driver. Thi
So, USB is a protocol, UART over USB is what the USB CDC lets you do. This is reductionist - and this also takes up some chunk of processor time, as far as I know - however, the chip has a USB peripheral, so I imagine most of the work is offloaded there. I would be tangentially interested in integrating a USB->Serial chip to compare processor overhead for the USB CDC implementation, but what am I trying to do here? Not that.
I couldn't get this running, not sure if that was about my clock, or what - but after one day lost, I give up. I'm going to build a USB -> UART RS485 Bridge (I also need a power injector, so this is all good) and go forwards with port testing and setup.
## Port Abstractions
USB CDC, interrupts
CDC, / mods pipe -> pass objects don't parse characters: key: value
Pins are on Ports, UARTS are on Ports, everything is confusing and ugly if we just write C and bang on registers.
Writeup PR2, do encoder?
or, do 2nd board, do uart return test, order next boards, dream of experiments
```C
#ifndef PIN_H_
#define PIN_H_
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "ASF/sam/utils/cmsis/sams70/include/sams70n20.h"
typedef struct{
Pio *port;
uint32_t pin_bm;
}pin_t;
pin_t pin_new(Pio *port, uint32_t pin_bitmask);
#endif /* PIN_H_ */
```
and
```C
#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;
}
void pin_output(pin_t pin){
}
```
to begin abstracting pins - just a struct
\ No newline at end of file
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -11,4 +11,14 @@
#ifndef CONF_BOARD_H
#define CONF_BOARD_H
# define BOARD_FREQ_SLCK_XTAL (32768UL)
# define BOARD_FREQ_SLCK_BYPASS (32768UL)
# define BOARD_FREQ_MAINCK_XTAL (12000000UL)
# define BOARD_FREQ_MAINCK_BYPASS (12000000UL)
# define BOARD_OSC_STARTUP_US (15625UL)
#endif // CONF_BOARD_H
......@@ -32,6 +32,8 @@
#include "pin.h"
pin_t stlb;
pin_t stlr;
pin_t button;
int main (void)
{
......@@ -41,20 +43,24 @@ int main (void)
sysclk_init();
PMC->PMC_PCER0 = 1 << ID_PIOA;
PMC->PMC_PCER0 = 1 << ID_PIOD;
stlb = pin_new(PIOA, PIO_PER_P1);
pin_output(stlb);
stlb.port->PIO_PER = stlb.pin_bm;
stlb.port->PIO_OER = stlb.pin_bm;
stlr = pin_new(PIOD, PIO_PER_P11);
pin_output(stlr);
PIOA->PIO_PER = PIO_PER_P15;
PIOA->PIO_ODR = PIO_PER_P15;
button = pin_new(PIOA, PIO_PER_P15);
pin_input(button);
while(1){
if(PIOA->PIO_PDSR & PIO_PER_P15){
stlb.port->PIO_CODR = stlb.pin_bm;
if(pin_get_state(button)){
pin_clear(stlb);
pin_set(stlr);
} else {
stlb.port->PIO_SODR = stlb.pin_bm;
pin_set(stlb);
pin_clear(stlr);
}
}
}
......@@ -16,3 +16,29 @@ pin_t pin_new(Pio *port, uint32_t pin_bitmask){
return pin;
}
void pin_output(pin_t pin){
pin.port->PIO_PER |= pin.pin_bm;
pin.port->PIO_OER = pin.pin_bm;
}
void pin_set(pin_t pin){
pin.port->PIO_SODR = pin.pin_bm;
}
void pin_clear(pin_t pin){
pin.port->PIO_CODR = pin.pin_bm;
}
void pin_input(pin_t pin){
pin.port->PIO_PER |= pin.pin_bm;
pin.port->PIO_ODR = pin.pin_bm;
}
bool pin_get_state(pin_t pin){
if(pin.port->PIO_PDSR & pin.pin_bm){
return true;
} else {
return false;
}
}
\ No newline at end of file
......@@ -22,4 +22,11 @@ typedef struct{
pin_t pin_new(Pio *port, uint32_t pin_bitmask);
void pin_output(pin_t pin); // set as output
void pin_set(pin_t pin);
void pin_clear(pin_t pin);
void pin_input(pin_t pin);
bool pin_get_state(pin_t pin);
#endif /* PIN_H_ */
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment