Commit 126d469e authored by Sam Calisch's avatar Sam Calisch

just got uarte working

parent cac871d9
Pipeline #728 passed with stage
in 3 seconds
const uint8_t pin_rx = 8;
const uint8_t pin_tx = 6;
int16_t values = 0; //place to storage current and voltage values
static uint8_t txBuffer[20] = {0};
static uint8_t rxBuffer[20] = {0};
void adc_setup(){
//configure SAADC resolution
NRF_SAADC->RESOLUTION = SAADC_RESOLUTION_VAL_10bit;
//configure sample rate via internal timer
//NRF_SAADC->SAMPLERATE = (( SAADC_SAMPLERATE_MODE_Timers << SAADC_SAMPLERATE_MODE_Pos) & SAADC_SAMPLERATE_MODE_Msk) |
// (( 2047 << SAADC_SAMPLERATE_CC_Pos) & SAADC_SAMPLERATE_CC_Msk);
//enable oversampling -- do not combine with scan mode!
//NRF_SAADC->OVERSAMPLE = (SAADC_OVERSAMPLE_OVERSAMPLE_Over256x << SAADC_OVERSAMPLE_OVERSAMPLE_Pos) & SAADC_OVERSAMPLE_OVERSAMPLE_Msk ;
//NRF_SAADC->OVERSAMPLE = (SAADC_OVERSAMPLE_OVERSAMPLE_Bypass << SAADC_OVERSAMPLE_OVERSAMPLE_Pos) & SAADC_OVERSAMPLE_OVERSAMPLE_Msk ;
//enable SAADC
NRF_SAADC->ENABLE = (SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos);
//set result pointer
NRF_SAADC->RESULT.PTR = (uint32_t)&values;
NRF_SAADC->RESULT.MAXCNT = 1; // One sample
//set channel 0 resistor network, gain, reference, sample time, and mode
NRF_SAADC->CH[0].CONFIG = ((SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESP_Pos) & SAADC_CH_CONFIG_RESP_Msk)
| ((SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESN_Pos) & SAADC_CH_CONFIG_RESN_Msk)
| ((SAADC_CH_CONFIG_GAIN_Gain1 << SAADC_CH_CONFIG_GAIN_Pos) & SAADC_CH_CONFIG_GAIN_Msk)
| ((SAADC_CH_CONFIG_REFSEL_Internal << SAADC_CH_CONFIG_REFSEL_Pos) & SAADC_CH_CONFIG_REFSEL_Msk)
| ((SAADC_CH_CONFIG_TACQ_3us << SAADC_CH_CONFIG_TACQ_Pos) & SAADC_CH_CONFIG_TACQ_Msk)
| ((SAADC_CH_CONFIG_BURST_Disabled << SAADC_CH_CONFIG_BURST_Pos) & SAADC_CH_CONFIG_BURST_Msk)
| ((SAADC_CH_CONFIG_MODE_Diff << SAADC_CH_CONFIG_MODE_Pos) & SAADC_CH_CONFIG_MODE_Msk);
//configure Channel 0 to use A0 as positive, A1 as negative
NRF_SAADC->CH[0].PSELP = SAADC_CH_PSELP_PSELP_AnalogInput0;
NRF_SAADC->CH[0].PSELN = SAADC_CH_PSELP_PSELP_AnalogInput1;
//set channel 1 resistor network, gain, reference, sample time, and mode
//NRF_SAADC->CH[1].CONFIG = ((SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESP_Pos) & SAADC_CH_CONFIG_RESP_Msk)
// | ((SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESN_Pos) & SAADC_CH_CONFIG_RESN_Msk)
// | ((SAADC_CH_CONFIG_GAIN_Gain4 << SAADC_CH_CONFIG_GAIN_Pos) & SAADC_CH_CONFIG_GAIN_Msk)
// | ((SAADC_CH_CONFIG_REFSEL_Internal << SAADC_CH_CONFIG_REFSEL_Pos) & SAADC_CH_CONFIG_REFSEL_Msk)
// | ((SAADC_CH_CONFIG_TACQ_10us << SAADC_CH_CONFIG_TACQ_Pos) & SAADC_CH_CONFIG_TACQ_Msk)
// | ((SAADC_CH_CONFIG_BURST_Disabled << SAADC_CH_CONFIG_BURST_Pos) & SAADC_CH_CONFIG_BURST_Msk)
// | ((SAADC_CH_CONFIG_MODE_Diff << SAADC_CH_CONFIG_MODE_Pos) & SAADC_CH_CONFIG_MODE_Msk);
//configure Channel 1 to use A2 as positive, A4 as negative
//NRF_SAADC->CH[1].PSELP = SAADC_CH_PSELP_PSELP_AnalogInput2;
//NRF_SAADC->CH[1].PSELN = SAADC_CH_PSELP_PSELP_AnalogInput4;
}
void timer_setup(){
//configure adc sample rate via PPI and timer1
//set timer mode
NRF_TIMER1->MODE = (TIMER_MODE_MODE_Timer << TIMER_MODE_MODE_Pos) & TIMER_MODE_MODE_Msk;
NRF_TIMER1->TASKS_CLEAR = 1; //clear tasks
//set prescalar = 4 for 1MHz tick frequency, 9 for 31.25kHz
NRF_TIMER1->PRESCALER = (9 << TIMER_PRESCALER_PRESCALER_Pos) & TIMER_PRESCALER_PRESCALER_Msk;
NRF_TIMER1->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
//set CC = 1000 for 1kHz overflow frequency
NRF_TIMER1->CC[0] = (10000 << TIMER_CC_CC_Pos) & TIMER_CC_CC_Msk;
//set shortcut between compare0 and clear
NRF_TIMER1->SHORTS = ((TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos) & TIMER_SHORTS_COMPARE0_CLEAR_Msk);
//connect compare0 event to adc
//for now, how about we just send something over serial?
//enable interrupt for COMPARE[0] event
//NVIC_DisableIRQ(TIMER1_IRQn);
//NVIC_ClearPendingIRQ(TIMER1_IRQn);
//NVIC_SetPriority(TIMER1_IRQn, 3); //lower priority to avoid conflict with soft device
//NVIC_EnableIRQ(TIMER1_IRQn);
//NRF_TIMER1->INTENSET = ((TIMER_INTENSET_COMPARE0_Set << TIMER_INTENSET_COMPARE0_Pos) & TIMER_INTENSET_COMPARE0_Msk);
//NRF_TIMER1->TASKS_START = 1;
}
void uart_setup(){
//serial for debug
//TODO: switch to UARTE (uart with dma)
NRF_UARTE0->PSEL.TXD = (pin_tx << UARTE_PSEL_TXD_PIN_Pos) & UARTE_PSEL_TXD_PIN_Msk;
NRF_UARTE0->PSEL.RXD = (pin_rx << UARTE_PSEL_RXD_PIN_Pos) & UARTE_PSEL_RXD_PIN_Msk;
NRF_UARTE0->CONFIG = ((UART_CONFIG_PARITY_Excluded << UARTE_CONFIG_PARITY_Pos) & UARTE_CONFIG_PARITY_Msk)
| ((UARTE_CONFIG_HWFC_Disabled << UARTE_CONFIG_HWFC_Pos) & UARTE_CONFIG_HWFC_Msk);
NRF_UARTE0->BAUDRATE = UARTE_BAUDRATE_BAUDRATE_Baud115200;
NRF_UARTE0->ENABLE = (UARTE_ENABLE_ENABLE_Enabled << UARTE_ENABLE_ENABLE_Pos) & UARTE_ENABLE_ENABLE_Msk;
//NRF_UARTE0->RXD.PTR = (uint32_t)((uint8_t *) rxBuffer);
//NRF_UARTE0->RXD.MAXCNT = sizeof(rxBuffer);
//NRF_UARTE0->TASKS_STARTRX = 1;
//NRF_UART0->EVENTS_RXDRDY = 0x0UL;
//NRF_UART0->EVENTS_TXDRDY = 0x0UL;
//NRF_UART0->TASKS_STARTRX = 0x1UL;
//NRF_UART0->TASKS_STARTTX = 0x1UL;
}
void setup() {
uart_setup();
//adc_setup();
//timer_setup();
//start a conversion
/*NRF_SAADC->TASKS_START = 0x01UL;
while (!NRF_SAADC->EVENTS_STARTED); NRF_SAADC->EVENTS_STARTED = 0x00UL;
//Serial.println("started");
NRF_SAADC->TASKS_SAMPLE = 0x01UL;
while (!NRF_SAADC->EVENTS_END); NRF_SAADC->EVENTS_END = 0x00UL;
//Serial.println("ended");
NRF_SAADC->TASKS_STOP = 0x01UL;
while (!NRF_SAADC->EVENTS_STOPPED); NRF_SAADC->EVENTS_STOPPED = 0x00UL;
*/
txBuffer[0] = 50;
txBuffer[1] = 10;
while(true){
NRF_UARTE0->TXD.PTR = (uint32_t)((uint8_t *) txBuffer);
NRF_UARTE0->TXD.MAXCNT = 2; //sizeof(txBuffer);
NRF_UARTE0->TASKS_STARTTX = 1;
//NRF_UART0->TXD = 50; //data
//while(!NRF_UART0->EVENTS_TXDRDY); NRF_UART0->EVENTS_TXDRDY = 0x0UL;
//NRF_UART0->TXD = 10; //newline
//while(!NRF_UART0->EVENTS_TXDRDY); NRF_UART0->EVENTS_TXDRDY = 0x0UL;
delay(100);
}
}
/*void TIMER1_IRQHandler(void){
if(NRF_TIMER1->EVENTS_COMPARE[0] != 0)
{
NRF_TIMER1->EVENTS_COMPARE[0] = 0;
//serial message
NRF_UART0->TXD = 51;
while(!NRF_UART0->EVENTS_TXDRDY); NRF_UART0->EVENTS_TXDRDY = 0x0UL;
NRF_UART0->TXD = 10;
while(!NRF_UART0->EVENTS_TXDRDY); NRF_UART0->EVENTS_TXDRDY = 0x0UL;
}
}*/
void loop() {}
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