TS-7600 NBUS example: Difference between revisions
From embeddedTS Manuals
(Created page with "When writing applications that should communicate over the NBUS you should use the calls in [ftp://ftp.embeddedarm.com/ts-arm-sbc/ts-7600-linux/sources/nbus.c nbus.c] and [ftp...") |
m (Links auto-updated for 2022 re-branding ( https://files.embeddedarm.com/ts-arm-sbc/ts-7600-linux/samples/dio.c → https://files.embeddedTS.com/ts-arm-sbc/ts-7600-linux/samples/dio.c https://files.embeddedarm.com/ts-arm-sbc/ts-7600-linux/sources/nbus.c → https://files.embeddedTS.com/ts-arm-sbc/ts-7600-linux/sources/nbus.c https://files.embeddedarm.com/ts-arm-sbc/ts-7600-linux/sources/nbus.h → https://files.embeddedTS.com/ts-arm-sbc/ts-7600-linux/sources/nbus.h)) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
<source lang=c> | <source lang=c> | ||
/* When compiling use the following gcc command: | |||
* gcc -oexample example.c nbus.c -mcpu=arm9 | |||
* | |||
* nbus.c and nbus.h must be in the same folder where the gcc command is being run from | |||
*/ | |||
#include <stdio.h> | #include <stdio.h> | ||
#include <stdint.h> | #include <stdint.h> | ||
Line 15: | Line 16: | ||
nbuslock(); | nbuslock(); | ||
/ | /* Set DIO 7 low | ||
* Set output value to 0 | |||
*/ | |||
val = nbuspeek16(0xa); | val = nbuspeek16(0xa); | ||
nbuspoke16(0xa, val & ~(1 << 7)); | nbuspoke16(0xa, val & ~(1 << 7)); | ||
Line 23: | Line 25: | ||
nbuspoke16(0xc, val | (1 << 7)); | nbuspoke16(0xc, val | (1 << 7)); | ||
/ | /* Set DIO 7 high | ||
* DDR is already set to output, so | |||
* set output value | |||
*/ | |||
val = nbuspeek16(0xa); | val = nbuspeek16(0xa); | ||
nbuspoke16(0xa, val | (1 << 7)); | nbuspoke16(0xa, val | (1 << 7)); | ||
// Toggle Red LED 10 times | |||
val = nbuspeek16(0x2); | val = nbuspeek16(0x2); | ||
/* The NBUS lock should be held as little as possible | |||
* since other peripherals will need access. When | |||
* going into an operation like a sleep, a flush, or | |||
* any other syscal that will stall the system without | |||
* actually needing the lock, it should be released first. | |||
*/ | |||
nbusunlock(); | |||
printf("Starting loop\n"); | |||
nbuslock(); | |||
for(i = 0; i < 10; i++) { | for(i = 0; i < 10; i++) { | ||
if(i % 2) { | if(i % 2) { | ||
nbuspoke16(0x2, val & ~(1 << 14)); | nbuspoke16(0x2, val & ~(1 << 14)); | ||
Line 41: | Line 52: | ||
nbuspoke16(0x2, val | ( 1 << 14)); | nbuspoke16(0x2, val | ( 1 << 14)); | ||
} | } | ||
/* nbuspreempt() can be used to check if there | |||
* are other processes waiting to use the bus. If there | |||
* are, then the bus is unlocked, given to other processes | |||
* and then the bus is re-locked. When nbuspreempt() | |||
* returns the calling process will have the lock again | |||
*/ | |||
nbuspreempt(); | |||
} | } | ||
nbusunlock(); | |||
return 0; | return 0; | ||
} | } | ||
</source> | </source> | ||
Another NBUS example can be found in [https://files.embeddedTS.com/ts-arm-sbc/ts-7600-linux/samples/dio.c dio.c], this also requires the [https://files.embeddedTS.com/ts-arm-sbc/ts-7600-linux/sources/nbus.c nbus.c] and [https://files.embeddedTS.com/ts-arm-sbc/ts-7600-linux/sources/nbus.h nbus.h] files in order to compile. |
Latest revision as of 17:19, 17 January 2022
/* When compiling use the following gcc command:
* gcc -oexample example.c nbus.c -mcpu=arm9
*
* nbus.c and nbus.h must be in the same folder where the gcc command is being run from
*/
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include "nbus.h"
int main (int argc, char **argv)
{
uint16_t val;
int i;
nbuslock();
/* Set DIO 7 low
* Set output value to 0
*/
val = nbuspeek16(0xa);
nbuspoke16(0xa, val & ~(1 << 7));
// Set dio 7 direction to output
val = nbuspeek16(0xc);
nbuspoke16(0xc, val | (1 << 7));
/* Set DIO 7 high
* DDR is already set to output, so
* set output value
*/
val = nbuspeek16(0xa);
nbuspoke16(0xa, val | (1 << 7));
// Toggle Red LED 10 times
val = nbuspeek16(0x2);
/* The NBUS lock should be held as little as possible
* since other peripherals will need access. When
* going into an operation like a sleep, a flush, or
* any other syscal that will stall the system without
* actually needing the lock, it should be released first.
*/
nbusunlock();
printf("Starting loop\n");
nbuslock();
for(i = 0; i < 10; i++) {
if(i % 2) {
nbuspoke16(0x2, val & ~(1 << 14));
} else {
nbuspoke16(0x2, val | ( 1 << 14));
}
/* nbuspreempt() can be used to check if there
* are other processes waiting to use the bus. If there
* are, then the bus is unlocked, given to other processes
* and then the bus is re-locked. When nbuspreempt()
* returns the calling process will have the lock again
*/
nbuspreempt();
}
nbusunlock();
return 0;
}
Another NBUS example can be found in dio.c, this also requires the nbus.c and nbus.h files in order to compile.